blob: 3eb3fc7b36be6e674cf1a26d0d06a243841be0f7 [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
David Howells416351f2007-05-09 02:33:45 -070020static int afs_readpage(struct file *file, struct page *page);
21static void afs_invalidatepage(struct page *page, unsigned long offset);
22static int afs_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 = {
David Howells416351f2007-05-09 02:33:45 -070035 .getattr = afs_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 = {
David Howells416351f2007-05-09 02:33:45 -070040 .readpage = afs_readpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 .set_page_dirty = __set_page_dirty_nobuffers,
David Howells416351f2007-05-09 02:33:45 -070042 .releasepage = afs_releasepage,
43 .invalidatepage = afs_invalidatepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
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
David Howells416351f2007-05-09 02:33:45 -070055 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -070056
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
David Howells416351f2007-05-09 02:33:45 -070081 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -070082
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
David Howells416351f2007-05-09 02:33:45 -070092static void afs_readpage_read_complete(void *cookie_data,
93 struct page *page,
94 void *data,
95 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
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
David Howells416351f2007-05-09 02:33:45 -0700112static void afs_readpage_write_complete(void *cookie_data,
113 struct page *page,
114 void *data,
115 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
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/*
David Howells416351f2007-05-09 02:33:45 -0700124 * AFS read page from file, directory or symlink
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
David Howells416351f2007-05-09 02:33:45 -0700126static int afs_readpage(struct file *file, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * invalidate part or all of a page
223 */
David Howells416351f2007-05-09 02:33:45 -0700224static void afs_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int ret = 1;
227
David Howells416351f2007-05-09 02:33:45 -0700228 kenter("{%lu},%lu", page->index, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 BUG_ON(!PageLocked(page));
231
232 if (PagePrivate(page)) {
233#ifdef AFS_CACHING_SUPPORT
234 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
235 cachefs_uncache_page(vnode->cache,page);
236#endif
237
238 /* We release buffers only if the entire page is being
239 * invalidated.
240 * The get_block cached value has been unconditionally
241 * invalidated, so real IO is not possible anymore.
242 */
243 if (offset == 0) {
244 BUG_ON(!PageLocked(page));
245
246 ret = 0;
247 if (!PageWriteback(page))
248 ret = page->mapping->a_ops->releasepage(page,
249 0);
NeilBrown2ff28e22006-03-26 01:37:18 -0800250 /* possibly should BUG_ON(!ret); - neilb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 }
253
254 _leave(" = %d", ret);
David Howellsec268152007-04-26 15:49:28 -0700255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/*
258 * release a page and cleanup its private data
259 */
David Howells416351f2007-05-09 02:33:45 -0700260static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David Howells416351f2007-05-09 02:33:45 -0700262 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David Howells416351f2007-05-09 02:33:45 -0700264 _enter("{{%x:%u}[%lu],%lx},%x",
265 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
266 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (PagePrivate(page)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700269 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ClearPagePrivate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 _leave(" = 0");
274 return 0;
David Howellsec268152007-04-26 15:49:28 -0700275}