blob: ae256498f4f70684e007bae54e40ff7dd2e0b027 [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;
David Howells260a9802007-04-26 15:59:35 -070053 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -070054
55 _enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode);
56
57 key = afs_request_key(vnode->volume->cell);
58 if (IS_ERR(key)) {
59 _leave(" = %ld [key]", PTR_ERR(key));
60 return PTR_ERR(key);
61 }
62
David Howells260a9802007-04-26 15:59:35 -070063 ret = afs_validate(vnode, key);
64 if (ret < 0) {
65 _leave(" = %d [val]", ret);
66 return ret;
67 }
68
David Howells00d3b7a2007-04-26 15:57:07 -070069 file->private_data = key;
70 _leave(" = 0");
71 return 0;
72}
73
74/*
75 * release an AFS file or directory and discard its key
76 */
77int afs_release(struct inode *inode, struct file *file)
78{
79 struct afs_vnode *vnode = AFS_FS_I(inode);
80
81 _enter("{%x:%x},", vnode->fid.vid, vnode->fid.vnode);
82
83 key_put(file->private_data);
84 _leave(" = 0");
85 return 0;
86}
87
88/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * deal with notification that a page was read from the cache
90 */
91#ifdef AFS_CACHING_SUPPORT
92static void afs_file_readpage_read_complete(void *cookie_data,
93 struct page *page,
94 void *data,
95 int error)
96{
97 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
98
99 if (error)
100 SetPageError(page);
101 else
102 SetPageUptodate(page);
103 unlock_page(page);
104
David Howellsec268152007-04-26 15:49:28 -0700105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * deal with notification that a page was written to the cache
110 */
111#ifdef AFS_CACHING_SUPPORT
112static void afs_file_readpage_write_complete(void *cookie_data,
113 struct page *page,
114 void *data,
115 int error)
116{
117 _enter("%p,%p,%p,%d", cookie_data, page, data, error);
118
119 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
124 * AFS read page from file (or symlink)
125 */
126static int afs_file_readpage(struct file *file, struct page *page)
127{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct afs_vnode *vnode;
129 struct inode *inode;
David Howells00d3b7a2007-04-26 15:57:07 -0700130 struct key *key;
David Howells08e0e7c2007-04-26 15:55:03 -0700131 size_t len;
132 off_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int ret;
134
135 inode = page->mapping->host;
136
David Howells00d3b7a2007-04-26 15:57:07 -0700137 ASSERT(file != NULL);
138 key = file->private_data;
139 ASSERT(key != NULL);
140
141 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 vnode = AFS_FS_I(inode);
144
Matt Mackallcd7619d2005-05-01 08:59:01 -0700145 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700148 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto error;
150
151#ifdef AFS_CACHING_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* is it cached? */
153 ret = cachefs_read_or_alloc_page(vnode->cache,
154 page,
155 afs_file_readpage_read_complete,
156 NULL,
157 GFP_KERNEL);
158#else
159 ret = -ENOBUFS;
160#endif
161
162 switch (ret) {
163 /* read BIO submitted and wb-journal entry found */
164 case 1:
165 BUG(); // TODO - handle wb-journal match
166
167 /* read BIO submitted (page in cache) */
168 case 0:
169 break;
170
171 /* no page available in cache */
172 case -ENOBUFS:
173 case -ENODATA:
174 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700175 offset = page->index << PAGE_CACHE_SHIFT;
176 len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /* read the contents of the file from the server into the
179 * page */
David Howells00d3b7a2007-04-26 15:57:07 -0700180 ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700182 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 _debug("got NOENT from server"
184 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700185 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ret = -ESTALE;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#ifdef AFS_CACHING_SUPPORT
189 cachefs_uncache_page(vnode->cache, page);
190#endif
191 goto error;
192 }
193
194 SetPageUptodate(page);
195
196#ifdef AFS_CACHING_SUPPORT
197 if (cachefs_write_page(vnode->cache,
198 page,
199 afs_file_readpage_write_complete,
200 NULL,
201 GFP_KERNEL) != 0
202 ) {
203 cachefs_uncache_page(vnode->cache, page);
204 unlock_page(page);
205 }
206#else
207 unlock_page(page);
208#endif
209 }
210
211 _leave(" = 0");
212 return 0;
213
David Howells08e0e7c2007-04-26 15:55:03 -0700214error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 SetPageError(page);
216 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 _leave(" = %d", ret);
218 return ret;
David Howellsec268152007-04-26 15:49:28 -0700219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * get a page cookie for the specified page
223 */
224#ifdef AFS_CACHING_SUPPORT
225int afs_cache_get_page_cookie(struct page *page,
226 struct cachefs_page **_page_cookie)
227{
228 int ret;
229
230 _enter("");
231 ret = cachefs_page_get_private(page,_page_cookie, GFP_NOIO);
232
233 _leave(" = %d", ret);
234 return ret;
David Howellsec268152007-04-26 15:49:28 -0700235}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#endif
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * invalidate part or all of a page
240 */
NeilBrown2ff28e22006-03-26 01:37:18 -0800241static void afs_file_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 int ret = 1;
244
245 _enter("{%lu},%lu", page->index, offset);
246
247 BUG_ON(!PageLocked(page));
248
249 if (PagePrivate(page)) {
250#ifdef AFS_CACHING_SUPPORT
251 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
252 cachefs_uncache_page(vnode->cache,page);
253#endif
254
255 /* We release buffers only if the entire page is being
256 * invalidated.
257 * The get_block cached value has been unconditionally
258 * invalidated, so real IO is not possible anymore.
259 */
260 if (offset == 0) {
261 BUG_ON(!PageLocked(page));
262
263 ret = 0;
264 if (!PageWriteback(page))
265 ret = page->mapping->a_ops->releasepage(page,
266 0);
NeilBrown2ff28e22006-03-26 01:37:18 -0800267 /* possibly should BUG_ON(!ret); - neilb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 }
270
271 _leave(" = %d", ret);
David Howellsec268152007-04-26 15:49:28 -0700272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*
275 * release a page and cleanup its private data
276 */
Al Viro27496a82005-10-21 03:20:48 -0400277static int afs_file_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct cachefs_page *pageio;
280
281 _enter("{%lu},%x", page->index, gfp_flags);
282
283 if (PagePrivate(page)) {
284#ifdef AFS_CACHING_SUPPORT
285 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
286 cachefs_uncache_page(vnode->cache, page);
287#endif
288
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700289 pageio = (struct cachefs_page *) page_private(page);
290 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 ClearPagePrivate(page);
292
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800293 kfree(pageio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
296 _leave(" = 0");
297 return 0;
David Howellsec268152007-04-26 15:49:28 -0700298}