blob: 101bbb8c0d8b0175f776a53469b4fde3195d9a58 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "internal.h"
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static int afs_file_readpage(struct file *file, struct page *page);
NeilBrown2ff28e22006-03-26 01:37:18 -080021static void afs_file_invalidatepage(struct page *page, unsigned long offset);
Al Viro27496a82005-10-21 03:20:48 -040022static int afs_file_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
David Howells00d3b7a2007-04-26 15:57:07 -070024const struct file_operations afs_file_operations = {
25 .open = afs_open,
26 .release = afs_release,
27 .llseek = generic_file_llseek,
28 .read = do_sync_read,
29 .aio_read = generic_file_aio_read,
30 .mmap = generic_file_readonly_mmap,
31 .sendfile = generic_file_sendfile,
32};
33
Arjan van de Ven754661f2007-02-12 00:55:38 -080034const struct inode_operations afs_file_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .getattr = afs_inode_getattr,
David Howells00d3b7a2007-04-26 15:57:07 -070036 .permission = afs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070039const struct address_space_operations afs_fs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 .readpage = afs_file_readpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 .set_page_dirty = __set_page_dirty_nobuffers,
42 .releasepage = afs_file_releasepage,
43 .invalidatepage = afs_file_invalidatepage,
44};
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
David Howells00d3b7a2007-04-26 15:57:07 -070047 * open an AFS file or directory and attach a key to it
48 */
49int afs_open(struct inode *inode, struct file *file)
50{
51 struct afs_vnode *vnode = AFS_FS_I(inode);
52 struct key *key;
53
54 _enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode);
55
56 key = afs_request_key(vnode->volume->cell);
57 if (IS_ERR(key)) {
58 _leave(" = %ld [key]", PTR_ERR(key));
59 return PTR_ERR(key);
60 }
61
62 file->private_data = key;
63 _leave(" = 0");
64 return 0;
65}
66
67/*
68 * release an AFS file or directory and discard its key
69 */
70int afs_release(struct inode *inode, struct file *file)
71{
72 struct afs_vnode *vnode = AFS_FS_I(inode);
73
74 _enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode);
75
76 key_put(file->private_data);
77 _leave(" = 0");
78 return 0;
79}
80
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * deal with notification that a page was read from the cache
83 */
84#ifdef AFS_CACHING_SUPPORT
85static void afs_file_readpage_read_complete(void *cookie_data,
86 struct page *page,
87 void *data,
88 int error)
89{
90 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
91
92 if (error)
93 SetPageError(page);
94 else
95 SetPageUptodate(page);
96 unlock_page(page);
97
David Howellsec268152007-04-26 15:49:28 -070098}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#endif
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * deal with notification that a page was written to the cache
103 */
104#ifdef AFS_CACHING_SUPPORT
105static void afs_file_readpage_write_complete(void *cookie_data,
106 struct page *page,
107 void *data,
108 int error)
109{
110 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
111
112 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * AFS read page from file (or symlink)
118 */
119static int afs_file_readpage(struct file *file, struct page *page)
120{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct afs_vnode *vnode;
122 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700123 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700124 size_t len;
125 off_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int ret;
127
128 inode = page->mapping->host;
129
David Howells00d3b7a2007-04-26 15:57:07 -0700130 ASSERT(file != NULL);
131 key = file->private_data;
132 ASSERT(key != NULL);
133
134 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 vnode = AFS_FS_I(inode);
137
Matt Mackallcd7619d2005-05-01 08:59:01 -0700138 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700141 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto error;
143
144#ifdef AFS_CACHING_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* is it cached? */
146 ret = cachefs_read_or_alloc_page(vnode->cache,
147 page,
148 afs_file_readpage_read_complete,
149 NULL,
150 GFP_KERNEL);
151#else
152 ret = -ENOBUFS;
153#endif
154
155 switch (ret) {
156 /* read BIO submitted and wb-journal entry found */
157 case 1:
158 BUG(); // TODO - handle wb-journal match
159
160 /* read BIO submitted (page in cache) */
161 case 0:
162 break;
163
164 /* no page available in cache */
165 case -ENOBUFS:
166 case -ENODATA:
167 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700168 offset = page->index << PAGE_CACHE_SHIFT;
169 len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* read the contents of the file from the server into the
172 * page */
David Howells00d3b7a2007-04-26 15:57:07 -0700173 ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700175 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 _debug("got NOENT from server"
177 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700178 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 ret = -ESTALE;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#ifdef AFS_CACHING_SUPPORT
182 cachefs_uncache_page(vnode->cache, page);
183#endif
184 goto error;
185 }
186
187 SetPageUptodate(page);
188
189#ifdef AFS_CACHING_SUPPORT
190 if (cachefs_write_page(vnode->cache,
191 page,
192 afs_file_readpage_write_complete,
193 NULL,
194 GFP_KERNEL) != 0
195 ) {
196 cachefs_uncache_page(vnode->cache, page);
197 unlock_page(page);
198 }
199#else
200 unlock_page(page);
201#endif
202 }
203
204 _leave(" = 0");
205 return 0;
206
David Howells08e0e7c2007-04-26 15:55:03 -0700207error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 SetPageError(page);
209 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 _leave(" = %d", ret);
211 return ret;
David Howellsec268152007-04-26 15:49:28 -0700212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * get a page cookie for the specified page
216 */
217#ifdef AFS_CACHING_SUPPORT
218int afs_cache_get_page_cookie(struct page *page,
219 struct cachefs_page **_page_cookie)
220{
221 int ret;
222
223 _enter("");
224 ret = cachefs_page_get_private(page,_page_cookie, GFP_NOIO);
225
226 _leave(" = %d", ret);
227 return ret;
David Howellsec268152007-04-26 15:49:28 -0700228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#endif
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
232 * invalidate part or all of a page
233 */
NeilBrown2ff28e22006-03-26 01:37:18 -0800234static void afs_file_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int ret = 1;
237
238 _enter("{%lu},%lu", page->index, offset);
239
240 BUG_ON(!PageLocked(page));
241
242 if (PagePrivate(page)) {
243#ifdef AFS_CACHING_SUPPORT
244 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
245 cachefs_uncache_page(vnode->cache,page);
246#endif
247
248 /* We release buffers only if the entire page is being
249 * invalidated.
250 * The get_block cached value has been unconditionally
251 * invalidated, so real IO is not possible anymore.
252 */
253 if (offset == 0) {
254 BUG_ON(!PageLocked(page));
255
256 ret = 0;
257 if (!PageWriteback(page))
258 ret = page->mapping->a_ops->releasepage(page,
259 0);
NeilBrown2ff28e22006-03-26 01:37:18 -0800260 /* possibly should BUG_ON(!ret); - neilb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 }
263
264 _leave(" = %d", ret);
David Howellsec268152007-04-26 15:49:28 -0700265}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
268 * release a page and cleanup its private data
269 */
Al Viro27496a82005-10-21 03:20:48 -0400270static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct cachefs_page *pageio;
273
274 _enter("{%lu},%x", page->index, gfp_flags);
275
276 if (PagePrivate(page)) {
277#ifdef AFS_CACHING_SUPPORT
278 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
279 cachefs_uncache_page(vnode->cache, page);
280#endif
281
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700282 pageio = (struct cachefs_page *) page_private(page);
283 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ClearPagePrivate(page);
285
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800286 kfree(pageio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
289 _leave(" = 0");
290 return 0;
David Howellsec268152007-04-26 15:49:28 -0700291}