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>
19 #include <linux/buffer_head.h>
22 #include <rxrpc/call.h>
26 static int afs_file_open(struct inode
*inode
, struct file
*file
);
27 static int afs_file_release(struct inode
*inode
, struct file
*file
);
30 static int afs_file_readpage(struct file
*file
, struct page
*page
);
31 static int afs_file_invalidatepage(struct page
*page
, unsigned long offset
);
32 static int afs_file_releasepage(struct page
*page
, gfp_t gfp_flags
);
34 struct inode_operations afs_file_inode_operations
= {
35 .getattr
= afs_inode_getattr
,
38 struct address_space_operations afs_fs_aops
= {
39 .readpage
= afs_file_readpage
,
40 .sync_page
= block_sync_page
,
41 .set_page_dirty
= __set_page_dirty_nobuffers
,
42 .releasepage
= afs_file_releasepage
,
43 .invalidatepage
= afs_file_invalidatepage
,
46 /*****************************************************************************/
48 * deal with notification that a page was read from the cache
50 #ifdef AFS_CACHING_SUPPORT
51 static void afs_file_readpage_read_complete(void *cookie_data
,
56 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
61 SetPageUptodate(page
);
64 } /* end afs_file_readpage_read_complete() */
67 /*****************************************************************************/
69 * deal with notification that a page was written to the cache
71 #ifdef AFS_CACHING_SUPPORT
72 static void afs_file_readpage_write_complete(void *cookie_data
,
77 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
81 } /* end afs_file_readpage_write_complete() */
84 /*****************************************************************************/
86 * AFS read page from file (or symlink)
88 static int afs_file_readpage(struct file
*file
, struct page
*page
)
90 struct afs_rxfs_fetch_descriptor desc
;
91 #ifdef AFS_CACHING_SUPPORT
92 struct cachefs_page
*pageio
;
94 struct afs_vnode
*vnode
;
98 inode
= page
->mapping
->host
;
100 _enter("{%lu},{%lu}", inode
->i_ino
, page
->index
);
102 vnode
= AFS_FS_I(inode
);
104 BUG_ON(!PageLocked(page
));
107 if (vnode
->flags
& AFS_VNODE_DELETED
)
110 #ifdef AFS_CACHING_SUPPORT
111 ret
= cachefs_page_get_private(page
, &pageio
, GFP_NOIO
);
116 ret
= cachefs_read_or_alloc_page(vnode
->cache
,
118 afs_file_readpage_read_complete
,
126 /* read BIO submitted and wb-journal entry found */
128 BUG(); // TODO - handle wb-journal match
130 /* read BIO submitted (page in cache) */
134 /* no page available in cache */
138 desc
.fid
= vnode
->fid
;
139 desc
.offset
= page
->index
<< PAGE_CACHE_SHIFT
;
140 desc
.size
= min((size_t) (inode
->i_size
- desc
.offset
),
142 desc
.buffer
= kmap(page
);
144 clear_page(desc
.buffer
);
146 /* read the contents of the file from the server into the
148 ret
= afs_vnode_fetch_data(vnode
, &desc
);
152 _debug("got NOENT from server"
153 " - marking file deleted and stale");
154 vnode
->flags
|= AFS_VNODE_DELETED
;
158 #ifdef AFS_CACHING_SUPPORT
159 cachefs_uncache_page(vnode
->cache
, page
);
164 SetPageUptodate(page
);
166 #ifdef AFS_CACHING_SUPPORT
167 if (cachefs_write_page(vnode
->cache
,
169 afs_file_readpage_write_complete
,
173 cachefs_uncache_page(vnode
->cache
, page
);
188 _leave(" = %d", ret
);
191 } /* end afs_file_readpage() */
193 /*****************************************************************************/
195 * get a page cookie for the specified page
197 #ifdef AFS_CACHING_SUPPORT
198 int afs_cache_get_page_cookie(struct page
*page
,
199 struct cachefs_page
**_page_cookie
)
204 ret
= cachefs_page_get_private(page
,_page_cookie
, GFP_NOIO
);
206 _leave(" = %d", ret
);
208 } /* end afs_cache_get_page_cookie() */
211 /*****************************************************************************/
213 * invalidate part or all of a page
215 static int afs_file_invalidatepage(struct page
*page
, unsigned long offset
)
219 _enter("{%lu},%lu", page
->index
, offset
);
221 BUG_ON(!PageLocked(page
));
223 if (PagePrivate(page
)) {
224 #ifdef AFS_CACHING_SUPPORT
225 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
226 cachefs_uncache_page(vnode
->cache
,page
);
229 /* We release buffers only if the entire page is being
231 * The get_block cached value has been unconditionally
232 * invalidated, so real IO is not possible anymore.
235 BUG_ON(!PageLocked(page
));
238 if (!PageWriteback(page
))
239 ret
= page
->mapping
->a_ops
->releasepage(page
,
244 _leave(" = %d", ret
);
246 } /* end afs_file_invalidatepage() */
248 /*****************************************************************************/
250 * release a page and cleanup its private data
252 static int afs_file_releasepage(struct page
*page
, gfp_t gfp_flags
)
254 struct cachefs_page
*pageio
;
256 _enter("{%lu},%x", page
->index
, gfp_flags
);
258 if (PagePrivate(page
)) {
259 #ifdef AFS_CACHING_SUPPORT
260 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
261 cachefs_uncache_page(vnode
->cache
, page
);
264 pageio
= (struct cachefs_page
*) page_private(page
);
265 set_page_private(page
, 0);
266 ClearPagePrivate(page
);
273 } /* end afs_file_releasepage() */