1 /* file.c: AFS filesystem file handling
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
17 #include <linux/pagemap.h>
20 #include <rxrpc/call.h>
24 static int afs_file_open(struct inode
*inode
, struct file
*file
);
25 static int afs_file_release(struct inode
*inode
, struct file
*file
);
28 static int afs_file_readpage(struct file
*file
, struct page
*page
);
29 static void afs_file_invalidatepage(struct page
*page
, unsigned long offset
);
30 static int afs_file_releasepage(struct page
*page
, gfp_t gfp_flags
);
32 const struct inode_operations afs_file_inode_operations
= {
33 .getattr
= afs_inode_getattr
,
36 const struct address_space_operations afs_fs_aops
= {
37 .readpage
= afs_file_readpage
,
38 .set_page_dirty
= __set_page_dirty_nobuffers
,
39 .releasepage
= afs_file_releasepage
,
40 .invalidatepage
= afs_file_invalidatepage
,
43 /*****************************************************************************/
45 * deal with notification that a page was read from the cache
47 #ifdef AFS_CACHING_SUPPORT
48 static void afs_file_readpage_read_complete(void *cookie_data
,
53 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
58 SetPageUptodate(page
);
61 } /* end afs_file_readpage_read_complete() */
64 /*****************************************************************************/
66 * deal with notification that a page was written to the cache
68 #ifdef AFS_CACHING_SUPPORT
69 static void afs_file_readpage_write_complete(void *cookie_data
,
74 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
78 } /* end afs_file_readpage_write_complete() */
81 /*****************************************************************************/
83 * AFS read page from file (or symlink)
85 static int afs_file_readpage(struct file
*file
, struct page
*page
)
87 struct afs_rxfs_fetch_descriptor desc
;
88 #ifdef AFS_CACHING_SUPPORT
89 struct cachefs_page
*pageio
;
91 struct afs_vnode
*vnode
;
95 inode
= page
->mapping
->host
;
97 _enter("{%lu},{%lu}", inode
->i_ino
, page
->index
);
99 vnode
= AFS_FS_I(inode
);
101 BUG_ON(!PageLocked(page
));
104 if (vnode
->flags
& AFS_VNODE_DELETED
)
107 #ifdef AFS_CACHING_SUPPORT
108 ret
= cachefs_page_get_private(page
, &pageio
, GFP_NOIO
);
113 ret
= cachefs_read_or_alloc_page(vnode
->cache
,
115 afs_file_readpage_read_complete
,
123 /* read BIO submitted and wb-journal entry found */
125 BUG(); // TODO - handle wb-journal match
127 /* read BIO submitted (page in cache) */
131 /* no page available in cache */
135 desc
.fid
= vnode
->fid
;
136 desc
.offset
= page
->index
<< PAGE_CACHE_SHIFT
;
137 desc
.size
= min((size_t) (inode
->i_size
- desc
.offset
),
139 desc
.buffer
= kmap(page
);
141 clear_page(desc
.buffer
);
143 /* read the contents of the file from the server into the
145 ret
= afs_vnode_fetch_data(vnode
, &desc
);
149 _debug("got NOENT from server"
150 " - marking file deleted and stale");
151 vnode
->flags
|= AFS_VNODE_DELETED
;
155 #ifdef AFS_CACHING_SUPPORT
156 cachefs_uncache_page(vnode
->cache
, page
);
161 SetPageUptodate(page
);
163 #ifdef AFS_CACHING_SUPPORT
164 if (cachefs_write_page(vnode
->cache
,
166 afs_file_readpage_write_complete
,
170 cachefs_uncache_page(vnode
->cache
, page
);
185 _leave(" = %d", ret
);
188 } /* end afs_file_readpage() */
190 /*****************************************************************************/
192 * get a page cookie for the specified page
194 #ifdef AFS_CACHING_SUPPORT
195 int afs_cache_get_page_cookie(struct page
*page
,
196 struct cachefs_page
**_page_cookie
)
201 ret
= cachefs_page_get_private(page
,_page_cookie
, GFP_NOIO
);
203 _leave(" = %d", ret
);
205 } /* end afs_cache_get_page_cookie() */
208 /*****************************************************************************/
210 * invalidate part or all of a page
212 static void afs_file_invalidatepage(struct page
*page
, unsigned long offset
)
216 _enter("{%lu},%lu", page
->index
, offset
);
218 BUG_ON(!PageLocked(page
));
220 if (PagePrivate(page
)) {
221 #ifdef AFS_CACHING_SUPPORT
222 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
223 cachefs_uncache_page(vnode
->cache
,page
);
226 /* We release buffers only if the entire page is being
228 * The get_block cached value has been unconditionally
229 * invalidated, so real IO is not possible anymore.
232 BUG_ON(!PageLocked(page
));
235 if (!PageWriteback(page
))
236 ret
= page
->mapping
->a_ops
->releasepage(page
,
238 /* possibly should BUG_ON(!ret); - neilb */
242 _leave(" = %d", ret
);
243 } /* end afs_file_invalidatepage() */
245 /*****************************************************************************/
247 * release a page and cleanup its private data
249 static int afs_file_releasepage(struct page
*page
, gfp_t gfp_flags
)
251 struct cachefs_page
*pageio
;
253 _enter("{%lu},%x", page
->index
, gfp_flags
);
255 if (PagePrivate(page
)) {
256 #ifdef AFS_CACHING_SUPPORT
257 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
258 cachefs_uncache_page(vnode
->cache
, page
);
261 pageio
= (struct cachefs_page
*) page_private(page
);
262 set_page_private(page
, 0);
263 ClearPagePrivate(page
);
270 } /* end afs_file_releasepage() */