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/sched.h>
16 #include <linux/slab.h>
18 #include <linux/pagemap.h>
21 #include <rxrpc/call.h>
25 static int afs_file_open(struct inode
*inode
, struct file
*file
);
26 static int afs_file_release(struct inode
*inode
, struct file
*file
);
29 static int afs_file_readpage(struct file
*file
, struct page
*page
);
30 static void afs_file_invalidatepage(struct page
*page
, unsigned long offset
);
31 static int afs_file_releasepage(struct page
*page
, gfp_t gfp_flags
);
33 struct inode_operations afs_file_inode_operations
= {
34 .getattr
= afs_inode_getattr
,
37 const struct address_space_operations afs_fs_aops
= {
38 .readpage
= afs_file_readpage
,
39 .set_page_dirty
= __set_page_dirty_nobuffers
,
40 .releasepage
= afs_file_releasepage
,
41 .invalidatepage
= afs_file_invalidatepage
,
44 /*****************************************************************************/
46 * deal with notification that a page was read from the cache
48 #ifdef AFS_CACHING_SUPPORT
49 static void afs_file_readpage_read_complete(void *cookie_data
,
54 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
59 SetPageUptodate(page
);
62 } /* end afs_file_readpage_read_complete() */
65 /*****************************************************************************/
67 * deal with notification that a page was written to the cache
69 #ifdef AFS_CACHING_SUPPORT
70 static void afs_file_readpage_write_complete(void *cookie_data
,
75 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
79 } /* end afs_file_readpage_write_complete() */
82 /*****************************************************************************/
84 * AFS read page from file (or symlink)
86 static int afs_file_readpage(struct file
*file
, struct page
*page
)
88 struct afs_rxfs_fetch_descriptor desc
;
89 #ifdef AFS_CACHING_SUPPORT
90 struct cachefs_page
*pageio
;
92 struct afs_vnode
*vnode
;
96 inode
= page
->mapping
->host
;
98 _enter("{%lu},{%lu}", inode
->i_ino
, page
->index
);
100 vnode
= AFS_FS_I(inode
);
102 BUG_ON(!PageLocked(page
));
105 if (vnode
->flags
& AFS_VNODE_DELETED
)
108 #ifdef AFS_CACHING_SUPPORT
109 ret
= cachefs_page_get_private(page
, &pageio
, GFP_NOIO
);
114 ret
= cachefs_read_or_alloc_page(vnode
->cache
,
116 afs_file_readpage_read_complete
,
124 /* read BIO submitted and wb-journal entry found */
126 BUG(); // TODO - handle wb-journal match
128 /* read BIO submitted (page in cache) */
132 /* no page available in cache */
136 desc
.fid
= vnode
->fid
;
137 desc
.offset
= page
->index
<< PAGE_CACHE_SHIFT
;
138 desc
.size
= min((size_t) (inode
->i_size
- desc
.offset
),
140 desc
.buffer
= kmap(page
);
142 clear_page(desc
.buffer
);
144 /* read the contents of the file from the server into the
146 ret
= afs_vnode_fetch_data(vnode
, &desc
);
150 _debug("got NOENT from server"
151 " - marking file deleted and stale");
152 vnode
->flags
|= AFS_VNODE_DELETED
;
156 #ifdef AFS_CACHING_SUPPORT
157 cachefs_uncache_page(vnode
->cache
, page
);
162 SetPageUptodate(page
);
164 #ifdef AFS_CACHING_SUPPORT
165 if (cachefs_write_page(vnode
->cache
,
167 afs_file_readpage_write_complete
,
171 cachefs_uncache_page(vnode
->cache
, page
);
186 _leave(" = %d", ret
);
189 } /* end afs_file_readpage() */
191 /*****************************************************************************/
193 * get a page cookie for the specified page
195 #ifdef AFS_CACHING_SUPPORT
196 int afs_cache_get_page_cookie(struct page
*page
,
197 struct cachefs_page
**_page_cookie
)
202 ret
= cachefs_page_get_private(page
,_page_cookie
, GFP_NOIO
);
204 _leave(" = %d", ret
);
206 } /* end afs_cache_get_page_cookie() */
209 /*****************************************************************************/
211 * invalidate part or all of a page
213 static void afs_file_invalidatepage(struct page
*page
, unsigned long offset
)
217 _enter("{%lu},%lu", page
->index
, offset
);
219 BUG_ON(!PageLocked(page
));
221 if (PagePrivate(page
)) {
222 #ifdef AFS_CACHING_SUPPORT
223 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
224 cachefs_uncache_page(vnode
->cache
,page
);
227 /* We release buffers only if the entire page is being
229 * The get_block cached value has been unconditionally
230 * invalidated, so real IO is not possible anymore.
233 BUG_ON(!PageLocked(page
));
236 if (!PageWriteback(page
))
237 ret
= page
->mapping
->a_ops
->releasepage(page
,
239 /* possibly should BUG_ON(!ret); - neilb */
243 _leave(" = %d", ret
);
244 } /* end afs_file_invalidatepage() */
246 /*****************************************************************************/
248 * release a page and cleanup its private data
250 static int afs_file_releasepage(struct page
*page
, gfp_t gfp_flags
)
252 struct cachefs_page
*pageio
;
254 _enter("{%lu},%x", page
->index
, gfp_flags
);
256 if (PagePrivate(page
)) {
257 #ifdef AFS_CACHING_SUPPORT
258 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
259 cachefs_uncache_page(vnode
->cache
, page
);
262 pageio
= (struct cachefs_page
*) page_private(page
);
263 set_page_private(page
, 0);
264 ClearPagePrivate(page
);
271 } /* end afs_file_releasepage() */