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
, int gfp_flags
);
34 static ssize_t
afs_file_write(struct file
*file
, const char __user
*buf
,
35 size_t size
, loff_t
*off
);
37 struct inode_operations afs_file_inode_operations
= {
38 .getattr
= afs_inode_getattr
,
41 struct file_operations afs_file_file_operations
= {
42 .read
= generic_file_read
,
43 .write
= afs_file_write
,
44 .mmap
= generic_file_mmap
,
46 .open
= afs_file_open
,
47 .release
= afs_file_release
,
48 .fsync
= afs_file_fsync
,
52 struct address_space_operations afs_fs_aops
= {
53 .readpage
= afs_file_readpage
,
54 .sync_page
= block_sync_page
,
55 .set_page_dirty
= __set_page_dirty_nobuffers
,
56 .releasepage
= afs_file_releasepage
,
57 .invalidatepage
= afs_file_invalidatepage
,
60 /*****************************************************************************/
64 static ssize_t
afs_file_write(struct file
*file
, const char __user
*buf
,
65 size_t size
, loff_t
*off
)
67 struct afs_vnode
*vnode
;
69 vnode
= AFS_FS_I(file
->f_dentry
->d_inode
);
70 if (vnode
->flags
& AFS_VNODE_DELETED
)
74 } /* end afs_file_write() */
76 /*****************************************************************************/
78 * deal with notification that a page was read from the cache
80 #ifdef AFS_CACHING_SUPPORT
81 static void afs_file_readpage_read_complete(void *cookie_data
,
86 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
91 SetPageUptodate(page
);
94 } /* end afs_file_readpage_read_complete() */
97 /*****************************************************************************/
99 * deal with notification that a page was written to the cache
101 #ifdef AFS_CACHING_SUPPORT
102 static void afs_file_readpage_write_complete(void *cookie_data
,
107 _enter("%p,%p,%p,%d", cookie_data
, page
, data
, error
);
111 } /* end afs_file_readpage_write_complete() */
114 /*****************************************************************************/
116 * AFS read page from file (or symlink)
118 static int afs_file_readpage(struct file
*file
, struct page
*page
)
120 struct afs_rxfs_fetch_descriptor desc
;
121 #ifdef AFS_CACHING_SUPPORT
122 struct cachefs_page
*pageio
;
124 struct afs_vnode
*vnode
;
128 inode
= page
->mapping
->host
;
130 _enter("{%lu},{%lu}", inode
->i_ino
, page
->index
);
132 vnode
= AFS_FS_I(inode
);
134 if (!PageLocked(page
))
138 if (vnode
->flags
& AFS_VNODE_DELETED
)
141 #ifdef AFS_CACHING_SUPPORT
142 ret
= cachefs_page_get_private(page
, &pageio
, GFP_NOIO
);
147 ret
= cachefs_read_or_alloc_page(vnode
->cache
,
149 afs_file_readpage_read_complete
,
157 /* read BIO submitted and wb-journal entry found */
159 BUG(); // TODO - handle wb-journal match
161 /* read BIO submitted (page in cache) */
165 /* no page available in cache */
169 desc
.fid
= vnode
->fid
;
170 desc
.offset
= page
->index
<< PAGE_CACHE_SHIFT
;
171 desc
.size
= min((size_t) (inode
->i_size
- desc
.offset
),
173 desc
.buffer
= kmap(page
);
175 clear_page(desc
.buffer
);
177 /* read the contents of the file from the server into the
179 ret
= afs_vnode_fetch_data(vnode
, &desc
);
183 _debug("got NOENT from server"
184 " - marking file deleted and stale");
185 vnode
->flags
|= AFS_VNODE_DELETED
;
189 #ifdef AFS_CACHING_SUPPORT
190 cachefs_uncache_page(vnode
->cache
, page
);
195 SetPageUptodate(page
);
197 #ifdef AFS_CACHING_SUPPORT
198 if (cachefs_write_page(vnode
->cache
,
200 afs_file_readpage_write_complete
,
204 cachefs_uncache_page(vnode
->cache
, page
);
219 _leave(" = %d", ret
);
222 } /* end afs_file_readpage() */
224 /*****************************************************************************/
226 * get a page cookie for the specified page
228 #ifdef AFS_CACHING_SUPPORT
229 int afs_cache_get_page_cookie(struct page
*page
,
230 struct cachefs_page
**_page_cookie
)
235 ret
= cachefs_page_get_private(page
,_page_cookie
, GFP_NOIO
);
237 _leave(" = %d", ret
);
239 } /* end afs_cache_get_page_cookie() */
242 /*****************************************************************************/
244 * invalidate part or all of a page
246 static int afs_file_invalidatepage(struct page
*page
, unsigned long offset
)
250 _enter("{%lu},%lu", page
->index
, offset
);
252 BUG_ON(!PageLocked(page
));
254 if (PagePrivate(page
)) {
255 #ifdef AFS_CACHING_SUPPORT
256 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
257 cachefs_uncache_page(vnode
->cache
,page
);
260 /* We release buffers only if the entire page is being
262 * The get_block cached value has been unconditionally
263 * invalidated, so real IO is not possible anymore.
266 BUG_ON(!PageLocked(page
));
269 if (!PageWriteback(page
))
270 ret
= page
->mapping
->a_ops
->releasepage(page
,
275 _leave(" = %d", ret
);
277 } /* end afs_file_invalidatepage() */
279 /*****************************************************************************/
281 * release a page and cleanup its private data
283 static int afs_file_releasepage(struct page
*page
, int gfp_flags
)
285 struct cachefs_page
*pageio
;
287 _enter("{%lu},%x", page
->index
, gfp_flags
);
289 if (PagePrivate(page
)) {
290 #ifdef AFS_CACHING_SUPPORT
291 struct afs_vnode
*vnode
= AFS_FS_I(page
->mapping
->host
);
292 cachefs_uncache_page(vnode
->cache
, page
);
295 pageio
= (struct cachefs_page
*) page
->private;
297 ClearPagePrivate(page
);
305 } /* end afs_file_releasepage() */