blob: 6344aee4ac4bff8e768fc7c344ec7ccea3670b98 [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/fs.h>
16#include <linux/pagemap.h>
David Howells31143d52007-05-09 02:33:46 -070017#include <linux/writeback.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.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);
Lukas Czernerd47992f2013-05-21 23:17:23 -040022static void afs_invalidatepage(struct page *page, unsigned int offset,
23 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070024static int afs_releasepage(struct page *page, gfp_t gfp_flags);
David Howells31143d52007-05-09 02:33:46 -070025static int afs_launder_page(struct page *page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Howells9b3f26c2009-04-03 16:42:41 +010027static int afs_readpages(struct file *filp, struct address_space *mapping,
28 struct list_head *pages, unsigned nr_pages);
29
David Howells00d3b7a2007-04-26 15:57:07 -070030const struct file_operations afs_file_operations = {
31 .open = afs_open,
32 .release = afs_release,
33 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040034 .read_iter = generic_file_read_iter,
Al Viro50b55512014-04-03 14:13:46 -040035 .write_iter = afs_file_write,
David Howells00d3b7a2007-04-26 15:57:07 -070036 .mmap = generic_file_readonly_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020037 .splice_read = generic_file_splice_read,
David Howells31143d52007-05-09 02:33:46 -070038 .fsync = afs_fsync,
David Howellse8d6c552007-07-15 23:40:12 -070039 .lock = afs_lock,
40 .flock = afs_flock,
David Howells00d3b7a2007-04-26 15:57:07 -070041};
42
Arjan van de Ven754661f2007-02-12 00:55:38 -080043const struct inode_operations afs_file_inode_operations = {
David Howells416351f2007-05-09 02:33:45 -070044 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070045 .setattr = afs_setattr,
David Howells00d3b7a2007-04-26 15:57:07 -070046 .permission = afs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070049const struct address_space_operations afs_fs_aops = {
David Howells416351f2007-05-09 02:33:45 -070050 .readpage = afs_readpage,
David Howells9b3f26c2009-04-03 16:42:41 +010051 .readpages = afs_readpages,
David Howells31143d52007-05-09 02:33:46 -070052 .set_page_dirty = afs_set_page_dirty,
53 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070054 .releasepage = afs_releasepage,
55 .invalidatepage = afs_invalidatepage,
Nick Piggin15b46502008-10-15 22:04:32 -070056 .write_begin = afs_write_begin,
57 .write_end = afs_write_end,
David Howells31143d52007-05-09 02:33:46 -070058 .writepage = afs_writepage,
59 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
David Howells00d3b7a2007-04-26 15:57:07 -070063 * open an AFS file or directory and attach a key to it
64 */
65int afs_open(struct inode *inode, struct file *file)
66{
67 struct afs_vnode *vnode = AFS_FS_I(inode);
68 struct key *key;
David Howells260a9802007-04-26 15:59:35 -070069 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -070070
David Howells416351f2007-05-09 02:33:45 -070071 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -070072
73 key = afs_request_key(vnode->volume->cell);
74 if (IS_ERR(key)) {
75 _leave(" = %ld [key]", PTR_ERR(key));
76 return PTR_ERR(key);
77 }
78
David Howells260a9802007-04-26 15:59:35 -070079 ret = afs_validate(vnode, key);
80 if (ret < 0) {
81 _leave(" = %d [val]", ret);
82 return ret;
83 }
84
David Howells00d3b7a2007-04-26 15:57:07 -070085 file->private_data = key;
86 _leave(" = 0");
87 return 0;
88}
89
90/*
91 * release an AFS file or directory and discard its key
92 */
93int afs_release(struct inode *inode, struct file *file)
94{
95 struct afs_vnode *vnode = AFS_FS_I(inode);
96
David Howells416351f2007-05-09 02:33:45 -070097 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -070098
99 key_put(file->private_data);
100 _leave(" = 0");
101 return 0;
102}
103
Matt Kraai6566abd2009-04-17 12:56:38 +0100104#ifdef CONFIG_AFS_FSCACHE
David Howells00d3b7a2007-04-26 15:57:07 -0700105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * deal with notification that a page was read from the cache
107 */
David Howells9b3f26c2009-04-03 16:42:41 +0100108static void afs_file_readpage_read_complete(struct page *page,
109 void *data,
110 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
David Howells9b3f26c2009-04-03 16:42:41 +0100112 _enter("%p,%p,%d", page, data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David Howells9b3f26c2009-04-03 16:42:41 +0100114 /* if the read completes with an error, we just unlock the page and let
115 * the VM reissue the readpage */
116 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 SetPageUptodate(page);
118 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700119}
Matt Kraai6566abd2009-04-17 12:56:38 +0100120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
Al Virof6d335c2010-05-21 15:27:09 +0100123 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
Al Virof6d335c2010-05-21 15:27:09 +0100125int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Al Virof6d335c2010-05-21 15:27:09 +0100127 struct inode *inode = page->mapping->host;
128 struct afs_vnode *vnode = AFS_FS_I(inode);
129 struct key *key = data;
David Howells08e0e7c2007-04-26 15:55:03 -0700130 size_t len;
131 off_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int ret;
133
David Howells00d3b7a2007-04-26 15:57:07 -0700134 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Matt Mackallcd7619d2005-05-01 08:59:01 -0700136 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700139 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto error;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* is it cached? */
David Howells9b3f26c2009-04-03 16:42:41 +0100143#ifdef CONFIG_AFS_FSCACHE
144 ret = fscache_read_or_alloc_page(vnode->cache,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 page,
146 afs_file_readpage_read_complete,
147 NULL,
148 GFP_KERNEL);
149#else
150 ret = -ENOBUFS;
151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 switch (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* read BIO submitted (page in cache) */
154 case 0:
155 break;
156
David Howells9b3f26c2009-04-03 16:42:41 +0100157 /* page not yet cached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 case -ENODATA:
David Howells9b3f26c2009-04-03 16:42:41 +0100159 _debug("cache said ENODATA");
160 goto go_on;
161
162 /* page will not be cached */
163 case -ENOBUFS:
164 _debug("cache said ENOBUFS");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 default:
David Howells9b3f26c2009-04-03 16:42:41 +0100166 go_on:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300167 offset = page->index << PAGE_SHIFT;
David Howells08e0e7c2007-04-26 15:55:03 -0700168 len = min_t(size_t, i_size_read(inode) - offset, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* read the contents of the file from the server into the
171 * page */
David Howells00d3b7a2007-04-26 15:57:07 -0700172 ret = afs_vnode_fetch_data(vnode, key, offset, len, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700174 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 _debug("got NOENT from server"
176 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700177 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 ret = -ESTALE;
179 }
David Howells9b3f26c2009-04-03 16:42:41 +0100180
181#ifdef CONFIG_AFS_FSCACHE
182 fscache_uncache_page(vnode->cache, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100184 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto error;
186 }
187
188 SetPageUptodate(page);
189
David Howells9b3f26c2009-04-03 16:42:41 +0100190 /* send the page to the cache */
191#ifdef CONFIG_AFS_FSCACHE
192 if (PageFsCache(page) &&
193 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
194 fscache_uncache_page(vnode->cache, page);
195 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100198 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
201 _leave(" = 0");
202 return 0;
203
David Howells08e0e7c2007-04-26 15:55:03 -0700204error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 SetPageError(page);
206 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 _leave(" = %d", ret);
208 return ret;
David Howellsec268152007-04-26 15:49:28 -0700209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
Al Virof6d335c2010-05-21 15:27:09 +0100212 * read page from file, directory or symlink, given a file to nominate the key
213 * to be used
214 */
215static int afs_readpage(struct file *file, struct page *page)
216{
217 struct key *key;
218 int ret;
219
220 if (file) {
221 key = file->private_data;
222 ASSERT(key != NULL);
223 ret = afs_page_filler(key, page);
224 } else {
225 struct inode *inode = page->mapping->host;
226 key = afs_request_key(AFS_FS_S(inode->i_sb)->volume->cell);
227 if (IS_ERR(key)) {
228 ret = PTR_ERR(key);
229 } else {
230 ret = afs_page_filler(key, page);
231 key_put(key);
232 }
233 }
234 return ret;
235}
236
237/*
David Howells9b3f26c2009-04-03 16:42:41 +0100238 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
David Howells9b3f26c2009-04-03 16:42:41 +0100240static int afs_readpages(struct file *file, struct address_space *mapping,
241 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Al Virof6d335c2010-05-21 15:27:09 +0100243 struct key *key = file->private_data;
David Howells9b3f26c2009-04-03 16:42:41 +0100244 struct afs_vnode *vnode;
245 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Al Virof6d335c2010-05-21 15:27:09 +0100247 _enter("{%d},{%lu},,%d",
248 key_serial(key), mapping->host->i_ino, nr_pages);
249
250 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Howells9b3f26c2009-04-03 16:42:41 +0100252 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000253 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100254 _leave(" = -ESTALE");
255 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
David Howells9b3f26c2009-04-03 16:42:41 +0100258 /* attempt to read as many of the pages as possible */
259#ifdef CONFIG_AFS_FSCACHE
260 ret = fscache_read_or_alloc_pages(vnode->cache,
261 mapping,
262 pages,
263 &nr_pages,
264 afs_file_readpage_read_complete,
265 NULL,
266 mapping_gfp_mask(mapping));
267#else
268 ret = -ENOBUFS;
269#endif
270
271 switch (ret) {
272 /* all pages are being read from the cache */
273 case 0:
274 BUG_ON(!list_empty(pages));
275 BUG_ON(nr_pages != 0);
276 _leave(" = 0 [reading all]");
277 return 0;
278
279 /* there were pages that couldn't be read from the cache */
280 case -ENODATA:
281 case -ENOBUFS:
282 break;
283
284 /* other error */
285 default:
286 _leave(" = %d", ret);
287 return ret;
288 }
289
290 /* load the missing pages from the network */
Al Virof6d335c2010-05-21 15:27:09 +0100291 ret = read_cache_pages(mapping, pages, afs_page_filler, key);
David Howells9b3f26c2009-04-03 16:42:41 +0100292
293 _leave(" = %d [netting]", ret);
294 return ret;
David Howellsec268152007-04-26 15:49:28 -0700295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
David Howells31143d52007-05-09 02:33:46 -0700298 * write back a dirty page
299 */
300static int afs_launder_page(struct page *page)
301{
302 _enter("{%lu}", page->index);
303
304 return 0;
305}
306
307/*
David Howells9b3f26c2009-04-03 16:42:41 +0100308 * invalidate part or all of a page
309 * - release a page and clean up its private data if offset is 0 (indicating
310 * the entire page)
311 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400312static void afs_invalidatepage(struct page *page, unsigned int offset,
313 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100314{
315 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
316
Lukas Czernerd47992f2013-05-21 23:17:23 -0400317 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100318
319 BUG_ON(!PageLocked(page));
320
321 /* we clean up only if the entire page is being invalidated */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300322 if (offset == 0 && length == PAGE_SIZE) {
David Howells9b3f26c2009-04-03 16:42:41 +0100323#ifdef CONFIG_AFS_FSCACHE
324 if (PageFsCache(page)) {
325 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
326 fscache_wait_on_page_write(vnode->cache, page);
327 fscache_uncache_page(vnode->cache, page);
David Howells9b3f26c2009-04-03 16:42:41 +0100328 }
329#endif
330
331 if (PagePrivate(page)) {
332 if (wb && !PageWriteback(page)) {
333 set_page_private(page, 0);
334 afs_put_writeback(wb);
335 }
336
337 if (!page_private(page))
338 ClearPagePrivate(page);
339 }
340 }
341
342 _leave("");
343}
344
345/*
346 * release a page and clean up its private state if it's not busy
347 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
David Howells416351f2007-05-09 02:33:45 -0700349static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
David Howells9b3f26c2009-04-03 16:42:41 +0100351 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
David Howells416351f2007-05-09 02:33:45 -0700352 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
David Howells416351f2007-05-09 02:33:45 -0700354 _enter("{{%x:%u}[%lu],%lx},%x",
355 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
356 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David Howells9b3f26c2009-04-03 16:42:41 +0100358 /* deny if page is being written to the cache and the caller hasn't
359 * elected to wait */
360#ifdef CONFIG_AFS_FSCACHE
David Howells201a1542009-11-19 18:11:35 +0000361 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
362 _leave(" = F [cache busy]");
363 return 0;
David Howells9b3f26c2009-04-03 16:42:41 +0100364 }
365#endif
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (PagePrivate(page)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100368 if (wb) {
369 set_page_private(page, 0);
370 afs_put_writeback(wb);
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ClearPagePrivate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
David Howells9b3f26c2009-04-03 16:42:41 +0100375 /* indicate that the page can be released */
376 _leave(" = T");
377 return 1;
David Howellsec268152007-04-26 15:49:28 -0700378}