blob: aede7eb66dd4f970a74021b41487a5a0a70c0616 [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* AFS filesystem file handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells08e0e7c2007-04-26 15:55:03 -07003 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/pagemap.h>
David Howells31143d52007-05-09 02:33:46 -070018#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "internal.h"
20
David Howells416351f2007-05-09 02:33:45 -070021static int afs_readpage(struct file *file, struct page *page);
22static void afs_invalidatepage(struct page *page, unsigned long offset);
23static int afs_releasepage(struct page *page, gfp_t gfp_flags);
David Howells31143d52007-05-09 02:33:46 -070024static int afs_launder_page(struct page *page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
David Howells00d3b7a2007-04-26 15:57:07 -070026const struct file_operations afs_file_operations = {
27 .open = afs_open,
28 .release = afs_release,
29 .llseek = generic_file_llseek,
30 .read = do_sync_read,
David Howells31143d52007-05-09 02:33:46 -070031 .write = do_sync_write,
David Howells00d3b7a2007-04-26 15:57:07 -070032 .aio_read = generic_file_aio_read,
David Howells31143d52007-05-09 02:33:46 -070033 .aio_write = afs_file_write,
David Howells00d3b7a2007-04-26 15:57:07 -070034 .mmap = generic_file_readonly_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020035 .splice_read = generic_file_splice_read,
David Howells31143d52007-05-09 02:33:46 -070036 .fsync = afs_fsync,
David Howells00d3b7a2007-04-26 15:57:07 -070037};
38
Arjan van de Ven754661f2007-02-12 00:55:38 -080039const struct inode_operations afs_file_inode_operations = {
David Howells416351f2007-05-09 02:33:45 -070040 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070041 .setattr = afs_setattr,
David Howells00d3b7a2007-04-26 15:57:07 -070042 .permission = afs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070045const struct address_space_operations afs_fs_aops = {
David Howells416351f2007-05-09 02:33:45 -070046 .readpage = afs_readpage,
David Howells31143d52007-05-09 02:33:46 -070047 .set_page_dirty = afs_set_page_dirty,
48 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070049 .releasepage = afs_releasepage,
50 .invalidatepage = afs_invalidatepage,
David Howells31143d52007-05-09 02:33:46 -070051 .prepare_write = afs_prepare_write,
52 .commit_write = afs_commit_write,
53 .writepage = afs_writepage,
54 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
David Howells00d3b7a2007-04-26 15:57:07 -070058 * open an AFS file or directory and attach a key to it
59 */
60int afs_open(struct inode *inode, struct file *file)
61{
62 struct afs_vnode *vnode = AFS_FS_I(inode);
63 struct key *key;
David Howells260a9802007-04-26 15:59:35 -070064 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -070065
David Howells416351f2007-05-09 02:33:45 -070066 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -070067
68 key = afs_request_key(vnode->volume->cell);
69 if (IS_ERR(key)) {
70 _leave(" = %ld [key]", PTR_ERR(key));
71 return PTR_ERR(key);
72 }
73
David Howells260a9802007-04-26 15:59:35 -070074 ret = afs_validate(vnode, key);
75 if (ret < 0) {
76 _leave(" = %d [val]", ret);
77 return ret;
78 }
79
David Howells00d3b7a2007-04-26 15:57:07 -070080 file->private_data = key;
81 _leave(" = 0");
82 return 0;
83}
84
85/*
86 * release an AFS file or directory and discard its key
87 */
88int afs_release(struct inode *inode, struct file *file)
89{
90 struct afs_vnode *vnode = AFS_FS_I(inode);
91
David Howells416351f2007-05-09 02:33:45 -070092 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -070093
94 key_put(file->private_data);
95 _leave(" = 0");
96 return 0;
97}
98
99/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * deal with notification that a page was read from the cache
101 */
102#ifdef AFS_CACHING_SUPPORT
David Howells416351f2007-05-09 02:33:45 -0700103static void afs_readpage_read_complete(void *cookie_data,
104 struct page *page,
105 void *data,
106 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
109
110 if (error)
111 SetPageError(page);
112 else
113 SetPageUptodate(page);
114 unlock_page(page);
115
David Howellsec268152007-04-26 15:49:28 -0700116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#endif
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * deal with notification that a page was written to the cache
121 */
122#ifdef AFS_CACHING_SUPPORT
David Howells416351f2007-05-09 02:33:45 -0700123static void afs_readpage_write_complete(void *cookie_data,
124 struct page *page,
125 void *data,
126 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
129
130 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#endif
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
David Howells416351f2007-05-09 02:33:45 -0700135 * AFS read page from file, directory or symlink
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
David Howells416351f2007-05-09 02:33:45 -0700137static int afs_readpage(struct file *file, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct afs_vnode *vnode;
140 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700141 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700142 size_t len;
143 off_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int ret;
145
146 inode = page->mapping->host;
147
David Howells00d3b7a2007-04-26 15:57:07 -0700148 ASSERT(file != NULL);
149 key = file->private_data;
150 ASSERT(key != NULL);
151
152 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 vnode = AFS_FS_I(inode);
155
Matt Mackallcd7619d2005-05-01 08:59:01 -0700156 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700159 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto error;
161
162#ifdef AFS_CACHING_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* is it cached? */
164 ret = cachefs_read_or_alloc_page(vnode->cache,
165 page,
166 afs_file_readpage_read_complete,
167 NULL,
168 GFP_KERNEL);
169#else
170 ret = -ENOBUFS;
171#endif
172
173 switch (ret) {
174 /* read BIO submitted and wb-journal entry found */
175 case 1:
176 BUG(); // TODO - handle wb-journal match
177
178 /* read BIO submitted (page in cache) */
179 case 0:
180 break;
181
182 /* no page available in cache */
183 case -ENOBUFS:
184 case -ENODATA:
185 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700186 offset = page->index << PAGE_CACHE_SHIFT;
187 len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* read the contents of the file from the server into the
190 * page */
David Howells00d3b7a2007-04-26 15:57:07 -0700191 ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700193 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 _debug("got NOENT from server"
195 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700196 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 ret = -ESTALE;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#ifdef AFS_CACHING_SUPPORT
200 cachefs_uncache_page(vnode->cache, page);
201#endif
202 goto error;
203 }
204
205 SetPageUptodate(page);
206
207#ifdef AFS_CACHING_SUPPORT
208 if (cachefs_write_page(vnode->cache,
209 page,
210 afs_file_readpage_write_complete,
211 NULL,
212 GFP_KERNEL) != 0
213 ) {
214 cachefs_uncache_page(vnode->cache, page);
215 unlock_page(page);
216 }
217#else
218 unlock_page(page);
219#endif
220 }
221
222 _leave(" = 0");
223 return 0;
224
David Howells08e0e7c2007-04-26 15:55:03 -0700225error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 SetPageError(page);
227 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 _leave(" = %d", ret);
229 return ret;
David Howellsec268152007-04-26 15:49:28 -0700230}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * invalidate part or all of a page
234 */
David Howells416351f2007-05-09 02:33:45 -0700235static void afs_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int ret = 1;
238
David Howells0f300ca2007-05-10 22:22:20 -0700239 _enter("{%lu},%lu", page->index, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 BUG_ON(!PageLocked(page));
242
243 if (PagePrivate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* We release buffers only if the entire page is being
245 * invalidated.
246 * The get_block cached value has been unconditionally
247 * invalidated, so real IO is not possible anymore.
248 */
249 if (offset == 0) {
250 BUG_ON(!PageLocked(page));
251
252 ret = 0;
253 if (!PageWriteback(page))
254 ret = page->mapping->a_ops->releasepage(page,
255 0);
NeilBrown2ff28e22006-03-26 01:37:18 -0800256 /* possibly should BUG_ON(!ret); - neilb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 }
259
260 _leave(" = %d", ret);
David Howellsec268152007-04-26 15:49:28 -0700261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
David Howells31143d52007-05-09 02:33:46 -0700264 * write back a dirty page
265 */
266static int afs_launder_page(struct page *page)
267{
268 _enter("{%lu}", page->index);
269
270 return 0;
271}
272
273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * release a page and cleanup its private data
275 */
David Howells416351f2007-05-09 02:33:45 -0700276static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
David Howells416351f2007-05-09 02:33:45 -0700278 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
David Howells31143d52007-05-09 02:33:46 -0700279 struct afs_writeback *wb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David Howells416351f2007-05-09 02:33:45 -0700281 _enter("{{%x:%u}[%lu],%lx},%x",
282 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
283 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (PagePrivate(page)) {
David Howells31143d52007-05-09 02:33:46 -0700286 wb = (struct afs_writeback *) page_private(page);
287 ASSERT(wb != NULL);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700288 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 ClearPagePrivate(page);
David Howells31143d52007-05-09 02:33:46 -0700290 afs_put_writeback(wb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
293 _leave(" = 0");
294 return 0;
David Howellsec268152007-04-26 15:49:28 -0700295}