blob: 79e665a35feaa4c37b2a6a66f4ee83e17cd2f3c8 [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>
David Howells91b467e2017-01-05 10:38:35 +000019#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
David Howells1cf7a152017-11-02 15:27:52 +000022static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
David Howells416351f2007-05-09 02:33:45 -070023static int afs_readpage(struct file *file, struct page *page);
Lukas Czernerd47992f2013-05-21 23:17:23 -040024static void afs_invalidatepage(struct page *page, unsigned int offset,
25 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070026static int afs_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David Howells9b3f26c2009-04-03 16:42:41 +010028static int afs_readpages(struct file *filp, struct address_space *mapping,
29 struct list_head *pages, unsigned nr_pages);
30
David Howells00d3b7a2007-04-26 15:57:07 -070031const struct file_operations afs_file_operations = {
32 .open = afs_open,
David Howells58fed942017-03-16 16:27:45 +000033 .flush = afs_flush,
David Howells00d3b7a2007-04-26 15:57:07 -070034 .release = afs_release,
35 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040036 .read_iter = generic_file_read_iter,
Al Viro50b55512014-04-03 14:13:46 -040037 .write_iter = afs_file_write,
David Howells1cf7a152017-11-02 15:27:52 +000038 .mmap = afs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020039 .splice_read = generic_file_splice_read,
David Howells31143d52007-05-09 02:33:46 -070040 .fsync = afs_fsync,
David Howellse8d6c552007-07-15 23:40:12 -070041 .lock = afs_lock,
42 .flock = afs_flock,
David Howells00d3b7a2007-04-26 15:57:07 -070043};
44
Arjan van de Ven754661f2007-02-12 00:55:38 -080045const struct inode_operations afs_file_inode_operations = {
David Howells416351f2007-05-09 02:33:45 -070046 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070047 .setattr = afs_setattr,
David Howells00d3b7a2007-04-26 15:57:07 -070048 .permission = afs_permission,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010049 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070052const struct address_space_operations afs_fs_aops = {
David Howells416351f2007-05-09 02:33:45 -070053 .readpage = afs_readpage,
David Howells9b3f26c2009-04-03 16:42:41 +010054 .readpages = afs_readpages,
David Howells31143d52007-05-09 02:33:46 -070055 .set_page_dirty = afs_set_page_dirty,
56 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070057 .releasepage = afs_releasepage,
58 .invalidatepage = afs_invalidatepage,
Nick Piggin15b46502008-10-15 22:04:32 -070059 .write_begin = afs_write_begin,
60 .write_end = afs_write_end,
David Howells31143d52007-05-09 02:33:46 -070061 .writepage = afs_writepage,
62 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
David Howells1cf7a152017-11-02 15:27:52 +000065static const struct vm_operations_struct afs_vm_ops = {
66 .fault = filemap_fault,
67 .map_pages = filemap_map_pages,
68 .page_mkwrite = afs_page_mkwrite,
69};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
David Howells4343d002017-11-02 15:27:52 +000072 * Discard a pin on a writeback key.
73 */
74void afs_put_wb_key(struct afs_wb_key *wbk)
75{
76 if (refcount_dec_and_test(&wbk->usage)) {
77 key_put(wbk->key);
78 kfree(wbk);
79 }
80}
81
82/*
83 * Cache key for writeback.
84 */
85int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
86{
87 struct afs_wb_key *wbk, *p;
88
89 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
90 if (!wbk)
91 return -ENOMEM;
92 refcount_set(&wbk->usage, 2);
93 wbk->key = af->key;
94
95 spin_lock(&vnode->wb_lock);
96 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
97 if (p->key == wbk->key)
98 goto found;
99 }
100
101 key_get(wbk->key);
102 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
103 spin_unlock(&vnode->wb_lock);
104 af->wb = wbk;
105 return 0;
106
107found:
108 refcount_inc(&p->usage);
109 spin_unlock(&vnode->wb_lock);
110 af->wb = p;
111 kfree(wbk);
112 return 0;
113}
114
115/*
David Howells00d3b7a2007-04-26 15:57:07 -0700116 * open an AFS file or directory and attach a key to it
117 */
118int afs_open(struct inode *inode, struct file *file)
119{
120 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000121 struct afs_file *af;
David Howells00d3b7a2007-04-26 15:57:07 -0700122 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700123 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700124
David Howells416351f2007-05-09 02:33:45 -0700125 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700126
127 key = afs_request_key(vnode->volume->cell);
128 if (IS_ERR(key)) {
David Howells215804a2017-11-02 15:27:52 +0000129 ret = PTR_ERR(key);
130 goto error;
131 }
132
133 af = kzalloc(sizeof(*af), GFP_KERNEL);
134 if (!af) {
135 ret = -ENOMEM;
136 goto error_key;
David Howells00d3b7a2007-04-26 15:57:07 -0700137 }
David Howells4343d002017-11-02 15:27:52 +0000138 af->key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700139
David Howells260a9802007-04-26 15:59:35 -0700140 ret = afs_validate(vnode, key);
David Howells215804a2017-11-02 15:27:52 +0000141 if (ret < 0)
142 goto error_af;
David Howells260a9802007-04-26 15:59:35 -0700143
David Howells4343d002017-11-02 15:27:52 +0000144 if (file->f_mode & FMODE_WRITE) {
145 ret = afs_cache_wb_key(vnode, af);
146 if (ret < 0)
147 goto error_af;
148 }
149
David Howells215804a2017-11-02 15:27:52 +0000150 file->private_data = af;
David Howells00d3b7a2007-04-26 15:57:07 -0700151 _leave(" = 0");
152 return 0;
David Howells215804a2017-11-02 15:27:52 +0000153
154error_af:
155 kfree(af);
156error_key:
157 key_put(key);
158error:
159 _leave(" = %d", ret);
160 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700161}
162
163/*
164 * release an AFS file or directory and discard its key
165 */
166int afs_release(struct inode *inode, struct file *file)
167{
168 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000169 struct afs_file *af = file->private_data;
David Howells00d3b7a2007-04-26 15:57:07 -0700170
David Howells416351f2007-05-09 02:33:45 -0700171 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700172
David Howells215804a2017-11-02 15:27:52 +0000173 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000174 if (af->wb)
175 afs_put_wb_key(af->wb);
David Howells215804a2017-11-02 15:27:52 +0000176 key_put(af->key);
177 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000178 afs_prune_wb_keys(vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700179 _leave(" = 0");
180 return 0;
181}
182
David Howells196ee9c2017-01-05 10:38:34 +0000183/*
184 * Dispose of a ref to a read record.
185 */
186void afs_put_read(struct afs_read *req)
187{
188 int i;
189
190 if (atomic_dec_and_test(&req->usage)) {
191 for (i = 0; i < req->nr_pages; i++)
192 if (req->pages[i])
193 put_page(req->pages[i]);
194 kfree(req);
195 }
196}
197
Matt Kraai6566abd2009-04-17 12:56:38 +0100198#ifdef CONFIG_AFS_FSCACHE
David Howells00d3b7a2007-04-26 15:57:07 -0700199/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * deal with notification that a page was read from the cache
201 */
David Howells9b3f26c2009-04-03 16:42:41 +0100202static void afs_file_readpage_read_complete(struct page *page,
203 void *data,
204 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
David Howells9b3f26c2009-04-03 16:42:41 +0100206 _enter("%p,%p,%d", page, data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
David Howells9b3f26c2009-04-03 16:42:41 +0100208 /* if the read completes with an error, we just unlock the page and let
209 * the VM reissue the readpage */
210 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 SetPageUptodate(page);
212 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700213}
Matt Kraai6566abd2009-04-17 12:56:38 +0100214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
David Howellsd2ddc772017-11-02 15:27:50 +0000217 * Fetch file data from the volume.
218 */
219int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *desc)
220{
221 struct afs_fs_cursor fc;
222 int ret;
223
224 _enter("%s{%x:%u.%u},%x,,,",
225 vnode->volume->name,
226 vnode->fid.vid,
227 vnode->fid.vnode,
228 vnode->fid.unique,
229 key_serial(key));
230
231 ret = -ERESTARTSYS;
232 if (afs_begin_vnode_operation(&fc, vnode, key)) {
233 while (afs_select_fileserver(&fc)) {
234 fc.cb_break = vnode->cb_break + vnode->cb_s_break;
235 afs_fs_fetch_data(&fc, desc);
236 }
237
238 afs_check_for_remote_deletion(&fc, fc.vnode);
239 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
240 ret = afs_end_vnode_operation(&fc);
241 }
242
243 _leave(" = %d", ret);
244 return ret;
245}
246
247/*
Al Virof6d335c2010-05-21 15:27:09 +0100248 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Al Virof6d335c2010-05-21 15:27:09 +0100250int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Al Virof6d335c2010-05-21 15:27:09 +0100252 struct inode *inode = page->mapping->host;
253 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells196ee9c2017-01-05 10:38:34 +0000254 struct afs_read *req;
Al Virof6d335c2010-05-21 15:27:09 +0100255 struct key *key = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int ret;
257
David Howells00d3b7a2007-04-26 15:57:07 -0700258 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Matt Mackallcd7619d2005-05-01 08:59:01 -0700260 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700263 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto error;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* is it cached? */
David Howells9b3f26c2009-04-03 16:42:41 +0100267#ifdef CONFIG_AFS_FSCACHE
268 ret = fscache_read_or_alloc_page(vnode->cache,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 page,
270 afs_file_readpage_read_complete,
271 NULL,
272 GFP_KERNEL);
273#else
274 ret = -ENOBUFS;
275#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 switch (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* read BIO submitted (page in cache) */
278 case 0:
279 break;
280
David Howells9b3f26c2009-04-03 16:42:41 +0100281 /* page not yet cached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 case -ENODATA:
David Howells9b3f26c2009-04-03 16:42:41 +0100283 _debug("cache said ENODATA");
284 goto go_on;
285
286 /* page will not be cached */
287 case -ENOBUFS:
288 _debug("cache said ENOBUFS");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 default:
David Howells9b3f26c2009-04-03 16:42:41 +0100290 go_on:
David Howells196ee9c2017-01-05 10:38:34 +0000291 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
292 GFP_KERNEL);
293 if (!req)
294 goto enomem;
295
David Howells6db3ac32017-03-16 16:27:44 +0000296 /* We request a full page. If the page is a partial one at the
297 * end of the file, the server will return a short read and the
298 * unmarshalling code will clear the unfilled space.
299 */
David Howells196ee9c2017-01-05 10:38:34 +0000300 atomic_set(&req->usage, 1);
301 req->pos = (loff_t)page->index << PAGE_SHIFT;
David Howells6db3ac32017-03-16 16:27:44 +0000302 req->len = PAGE_SIZE;
David Howells196ee9c2017-01-05 10:38:34 +0000303 req->nr_pages = 1;
304 req->pages[0] = page;
305 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 /* read the contents of the file from the server into the
308 * page */
David Howellsd2ddc772017-11-02 15:27:50 +0000309 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +0000310 afs_put_read(req);
David Howellsdab17c12017-11-02 15:27:52 +0000311
312 if (ret >= 0 && S_ISDIR(inode->i_mode) &&
313 !afs_dir_check_page(inode, page))
314 ret = -EIO;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700317 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 _debug("got NOENT from server"
319 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700320 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 ret = -ESTALE;
322 }
David Howells9b3f26c2009-04-03 16:42:41 +0100323
324#ifdef CONFIG_AFS_FSCACHE
325 fscache_uncache_page(vnode->cache, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100327 BUG_ON(PageFsCache(page));
David Howells68ae8492017-03-16 16:27:48 +0000328
329 if (ret == -EINTR ||
330 ret == -ENOMEM ||
331 ret == -ERESTARTSYS ||
332 ret == -EAGAIN)
333 goto error;
334 goto io_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 SetPageUptodate(page);
338
David Howells9b3f26c2009-04-03 16:42:41 +0100339 /* send the page to the cache */
340#ifdef CONFIG_AFS_FSCACHE
341 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100342 fscache_write_page(vnode->cache, page, vnode->status.size,
343 GFP_KERNEL) != 0) {
David Howells9b3f26c2009-04-03 16:42:41 +0100344 fscache_uncache_page(vnode->cache, page);
345 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100348 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 _leave(" = 0");
352 return 0;
353
David Howells68ae8492017-03-16 16:27:48 +0000354io_error:
355 SetPageError(page);
356 goto error;
David Howells196ee9c2017-01-05 10:38:34 +0000357enomem:
358 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -0700359error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 _leave(" = %d", ret);
362 return ret;
David Howellsec268152007-04-26 15:49:28 -0700363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
Al Virof6d335c2010-05-21 15:27:09 +0100366 * read page from file, directory or symlink, given a file to nominate the key
367 * to be used
368 */
369static int afs_readpage(struct file *file, struct page *page)
370{
371 struct key *key;
372 int ret;
373
374 if (file) {
David Howells215804a2017-11-02 15:27:52 +0000375 key = afs_file_key(file);
Al Virof6d335c2010-05-21 15:27:09 +0100376 ASSERT(key != NULL);
377 ret = afs_page_filler(key, page);
378 } else {
379 struct inode *inode = page->mapping->host;
David Howellsd2ddc772017-11-02 15:27:50 +0000380 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
Al Virof6d335c2010-05-21 15:27:09 +0100381 if (IS_ERR(key)) {
382 ret = PTR_ERR(key);
383 } else {
384 ret = afs_page_filler(key, page);
385 key_put(key);
386 }
387 }
388 return ret;
389}
390
391/*
David Howells91b467e2017-01-05 10:38:35 +0000392 * Make pages available as they're filled.
393 */
394static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req)
395{
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000396#ifdef CONFIG_AFS_FSCACHE
David Howells97e30432017-11-02 15:27:48 +0000397 struct afs_vnode *vnode = call->reply[0];
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000398#endif
David Howells91b467e2017-01-05 10:38:35 +0000399 struct page *page = req->pages[req->index];
400
401 req->pages[req->index] = NULL;
402 SetPageUptodate(page);
403
404 /* send the page to the cache */
405#ifdef CONFIG_AFS_FSCACHE
406 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100407 fscache_write_page(vnode->cache, page, vnode->status.size,
408 GFP_KERNEL) != 0) {
David Howells91b467e2017-01-05 10:38:35 +0000409 fscache_uncache_page(vnode->cache, page);
410 BUG_ON(PageFsCache(page));
411 }
412#endif
413 unlock_page(page);
414 put_page(page);
415}
416
417/*
418 * Read a contiguous set of pages.
419 */
420static int afs_readpages_one(struct file *file, struct address_space *mapping,
421 struct list_head *pages)
422{
423 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
424 struct afs_read *req;
425 struct list_head *p;
426 struct page *first, *page;
David Howells215804a2017-11-02 15:27:52 +0000427 struct key *key = afs_file_key(file);
David Howells91b467e2017-01-05 10:38:35 +0000428 pgoff_t index;
429 int ret, n, i;
430
431 /* Count the number of contiguous pages at the front of the list. Note
432 * that the list goes prev-wards rather than next-wards.
433 */
434 first = list_entry(pages->prev, struct page, lru);
435 index = first->index + 1;
436 n = 1;
437 for (p = first->lru.prev; p != pages; p = p->prev) {
438 page = list_entry(p, struct page, lru);
439 if (page->index != index)
440 break;
441 index++;
442 n++;
443 }
444
445 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
446 GFP_NOFS);
447 if (!req)
448 return -ENOMEM;
449
450 atomic_set(&req->usage, 1);
451 req->page_done = afs_readpages_page_done;
452 req->pos = first->index;
453 req->pos <<= PAGE_SHIFT;
454
455 /* Transfer the pages to the request. We add them in until one fails
456 * to add to the LRU and then we stop (as that'll make a hole in the
457 * contiguous run.
458 *
459 * Note that it's possible for the file size to change whilst we're
460 * doing this, but we rely on the server returning less than we asked
461 * for if the file shrank. We also rely on this to deal with a partial
462 * page at the end of the file.
463 */
464 do {
465 page = list_entry(pages->prev, struct page, lru);
466 list_del(&page->lru);
467 index = page->index;
468 if (add_to_page_cache_lru(page, mapping, index,
469 readahead_gfp_mask(mapping))) {
470#ifdef CONFIG_AFS_FSCACHE
471 fscache_uncache_page(vnode->cache, page);
472#endif
473 put_page(page);
474 break;
475 }
476
477 req->pages[req->nr_pages++] = page;
478 req->len += PAGE_SIZE;
479 } while (req->nr_pages < n);
480
481 if (req->nr_pages == 0) {
482 kfree(req);
483 return 0;
484 }
485
David Howellsd2ddc772017-11-02 15:27:50 +0000486 ret = afs_fetch_data(vnode, key, req);
David Howells91b467e2017-01-05 10:38:35 +0000487 if (ret < 0)
488 goto error;
489
490 task_io_account_read(PAGE_SIZE * req->nr_pages);
491 afs_put_read(req);
492 return 0;
493
494error:
495 if (ret == -ENOENT) {
496 _debug("got NOENT from server"
497 " - marking file deleted and stale");
498 set_bit(AFS_VNODE_DELETED, &vnode->flags);
499 ret = -ESTALE;
500 }
501
502 for (i = 0; i < req->nr_pages; i++) {
503 page = req->pages[i];
504 if (page) {
505#ifdef CONFIG_AFS_FSCACHE
506 fscache_uncache_page(vnode->cache, page);
507#endif
508 SetPageError(page);
509 unlock_page(page);
510 }
511 }
512
513 afs_put_read(req);
514 return ret;
515}
516
517/*
David Howells9b3f26c2009-04-03 16:42:41 +0100518 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
David Howells9b3f26c2009-04-03 16:42:41 +0100520static int afs_readpages(struct file *file, struct address_space *mapping,
521 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
David Howells215804a2017-11-02 15:27:52 +0000523 struct key *key = afs_file_key(file);
David Howells9b3f26c2009-04-03 16:42:41 +0100524 struct afs_vnode *vnode;
525 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Al Virof6d335c2010-05-21 15:27:09 +0100527 _enter("{%d},{%lu},,%d",
528 key_serial(key), mapping->host->i_ino, nr_pages);
529
530 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
David Howells9b3f26c2009-04-03 16:42:41 +0100532 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000533 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100534 _leave(" = -ESTALE");
535 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
David Howells9b3f26c2009-04-03 16:42:41 +0100538 /* attempt to read as many of the pages as possible */
539#ifdef CONFIG_AFS_FSCACHE
540 ret = fscache_read_or_alloc_pages(vnode->cache,
541 mapping,
542 pages,
543 &nr_pages,
544 afs_file_readpage_read_complete,
545 NULL,
546 mapping_gfp_mask(mapping));
547#else
548 ret = -ENOBUFS;
549#endif
550
551 switch (ret) {
552 /* all pages are being read from the cache */
553 case 0:
554 BUG_ON(!list_empty(pages));
555 BUG_ON(nr_pages != 0);
556 _leave(" = 0 [reading all]");
557 return 0;
558
559 /* there were pages that couldn't be read from the cache */
560 case -ENODATA:
561 case -ENOBUFS:
562 break;
563
564 /* other error */
565 default:
566 _leave(" = %d", ret);
567 return ret;
568 }
569
David Howells91b467e2017-01-05 10:38:35 +0000570 while (!list_empty(pages)) {
571 ret = afs_readpages_one(file, mapping, pages);
572 if (ret < 0)
573 break;
574 }
David Howells9b3f26c2009-04-03 16:42:41 +0100575
576 _leave(" = %d [netting]", ret);
577 return ret;
David Howellsec268152007-04-26 15:49:28 -0700578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/*
David Howells9b3f26c2009-04-03 16:42:41 +0100581 * invalidate part or all of a page
582 * - release a page and clean up its private data if offset is 0 (indicating
583 * the entire page)
584 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400585static void afs_invalidatepage(struct page *page, unsigned int offset,
586 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100587{
David Howells13524ab2017-11-02 15:27:53 +0000588 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
589 unsigned long priv;
590
Lukas Czernerd47992f2013-05-21 23:17:23 -0400591 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100592
593 BUG_ON(!PageLocked(page));
594
595 /* we clean up only if the entire page is being invalidated */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300596 if (offset == 0 && length == PAGE_SIZE) {
David Howells9b3f26c2009-04-03 16:42:41 +0100597#ifdef CONFIG_AFS_FSCACHE
598 if (PageFsCache(page)) {
599 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
600 fscache_wait_on_page_write(vnode->cache, page);
601 fscache_uncache_page(vnode->cache, page);
David Howells9b3f26c2009-04-03 16:42:41 +0100602 }
603#endif
604
605 if (PagePrivate(page)) {
David Howells13524ab2017-11-02 15:27:53 +0000606 priv = page_private(page);
607 trace_afs_page_dirty(vnode, tracepoint_string("inval"),
608 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000609 set_page_private(page, 0);
610 ClearPagePrivate(page);
David Howells9b3f26c2009-04-03 16:42:41 +0100611 }
612 }
613
614 _leave("");
615}
616
617/*
618 * release a page and clean up its private state if it's not busy
619 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
David Howells416351f2007-05-09 02:33:45 -0700621static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
David Howells416351f2007-05-09 02:33:45 -0700623 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
David Howells13524ab2017-11-02 15:27:53 +0000624 unsigned long priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
David Howells416351f2007-05-09 02:33:45 -0700626 _enter("{{%x:%u}[%lu],%lx},%x",
627 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
628 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
David Howells9b3f26c2009-04-03 16:42:41 +0100630 /* deny if page is being written to the cache and the caller hasn't
631 * elected to wait */
632#ifdef CONFIG_AFS_FSCACHE
David Howells201a1542009-11-19 18:11:35 +0000633 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
634 _leave(" = F [cache busy]");
635 return 0;
David Howells9b3f26c2009-04-03 16:42:41 +0100636 }
637#endif
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (PagePrivate(page)) {
David Howells13524ab2017-11-02 15:27:53 +0000640 priv = page_private(page);
641 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
642 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000643 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ClearPagePrivate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
David Howells9b3f26c2009-04-03 16:42:41 +0100647 /* indicate that the page can be released */
648 _leave(" = T");
649 return 1;
David Howellsec268152007-04-26 15:49:28 -0700650}
David Howells1cf7a152017-11-02 15:27:52 +0000651
652/*
653 * Handle setting up a memory mapping on an AFS file.
654 */
655static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
656{
657 int ret;
658
659 ret = generic_file_mmap(file, vma);
660 if (ret == 0)
661 vma->vm_ops = &afs_vm_ops;
662 return ret;
663}