blob: e8d6619890a91bffb1d3ce12cf51c23547f55508 [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 ([email protected])
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 Howells91b467e02017-01-05 10:38:35 +000019#include <linux/task_io_accounting_ops.h>
Nikolay Borisovf86196e2019-01-03 15:29:02 -080020#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "internal.h"
22
David Howells1cf7a152017-11-02 15:27:52 +000023static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
David Howells416351f2007-05-09 02:33:45 -070024static int afs_readpage(struct file *file, struct page *page);
Lukas Czernerd47992f2013-05-21 23:17:23 -040025static void afs_invalidatepage(struct page *page, unsigned int offset,
26 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070027static int afs_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David Howells9b3f26c2009-04-03 16:42:41 +010029static int afs_readpages(struct file *filp, struct address_space *mapping,
30 struct list_head *pages, unsigned nr_pages);
31
David Howells00d3b7a2007-04-26 15:57:07 -070032const struct file_operations afs_file_operations = {
33 .open = afs_open,
34 .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 Howells3b6492d2018-10-20 00:57:57 +0100125 _enter("{%llx:%llu},", 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 }
David Howells5a813272018-04-06 14:17:26 +0100149
150 if (file->f_flags & O_TRUNC)
151 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howells4343d002017-11-02 15:27:52 +0000152
David Howells215804a2017-11-02 15:27:52 +0000153 file->private_data = af;
David Howells00d3b7a2007-04-26 15:57:07 -0700154 _leave(" = 0");
155 return 0;
David Howells215804a2017-11-02 15:27:52 +0000156
157error_af:
158 kfree(af);
159error_key:
160 key_put(key);
161error:
162 _leave(" = %d", ret);
163 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700164}
165
166/*
167 * release an AFS file or directory and discard its key
168 */
169int afs_release(struct inode *inode, struct file *file)
170{
171 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000172 struct afs_file *af = file->private_data;
David Howells00d3b7a2007-04-26 15:57:07 -0700173
David Howells3b6492d2018-10-20 00:57:57 +0100174 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700175
David Howells5a813272018-04-06 14:17:26 +0100176 if ((file->f_mode & FMODE_WRITE))
177 return vfs_fsync(file, 0);
178
David Howells215804a2017-11-02 15:27:52 +0000179 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000180 if (af->wb)
181 afs_put_wb_key(af->wb);
David Howells215804a2017-11-02 15:27:52 +0000182 key_put(af->key);
183 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000184 afs_prune_wb_keys(vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700185 _leave(" = 0");
186 return 0;
187}
188
David Howells196ee9c2017-01-05 10:38:34 +0000189/*
190 * Dispose of a ref to a read record.
191 */
192void afs_put_read(struct afs_read *req)
193{
194 int i;
195
David Howellsf3ddee82018-04-06 14:17:25 +0100196 if (refcount_dec_and_test(&req->usage)) {
David Howells196ee9c2017-01-05 10:38:34 +0000197 for (i = 0; i < req->nr_pages; i++)
198 if (req->pages[i])
199 put_page(req->pages[i]);
David Howellsf3ddee82018-04-06 14:17:25 +0100200 if (req->pages != req->array)
201 kfree(req->pages);
David Howells196ee9c2017-01-05 10:38:34 +0000202 kfree(req);
203 }
204}
205
Matt Kraai6566abd2009-04-17 12:56:38 +0100206#ifdef CONFIG_AFS_FSCACHE
David Howells00d3b7a2007-04-26 15:57:07 -0700207/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * deal with notification that a page was read from the cache
209 */
David Howells9b3f26c2009-04-03 16:42:41 +0100210static void afs_file_readpage_read_complete(struct page *page,
211 void *data,
212 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David Howells9b3f26c2009-04-03 16:42:41 +0100214 _enter("%p,%p,%d", page, data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
David Howells9b3f26c2009-04-03 16:42:41 +0100216 /* if the read completes with an error, we just unlock the page and let
217 * the VM reissue the readpage */
218 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 SetPageUptodate(page);
220 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700221}
Matt Kraai6566abd2009-04-17 12:56:38 +0100222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
David Howellsd2ddc772017-11-02 15:27:50 +0000225 * Fetch file data from the volume.
226 */
227int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *desc)
228{
229 struct afs_fs_cursor fc;
230 int ret;
231
David Howells3b6492d2018-10-20 00:57:57 +0100232 _enter("%s{%llx:%llu.%u},%x,,,",
David Howellsd2ddc772017-11-02 15:27:50 +0000233 vnode->volume->name,
234 vnode->fid.vid,
235 vnode->fid.vnode,
236 vnode->fid.unique,
237 key_serial(key));
238
239 ret = -ERESTARTSYS;
240 if (afs_begin_vnode_operation(&fc, vnode, key)) {
241 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +0100242 fc.cb_break = afs_calc_vnode_cb_break(vnode);
David Howellsd2ddc772017-11-02 15:27:50 +0000243 afs_fs_fetch_data(&fc, desc);
244 }
245
246 afs_check_for_remote_deletion(&fc, fc.vnode);
247 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
248 ret = afs_end_vnode_operation(&fc);
249 }
250
David Howells76a5cb62018-04-06 14:17:26 +0100251 if (ret == 0) {
252 afs_stat_v(vnode, n_fetches);
253 atomic_long_add(desc->actual_len,
254 &afs_v2net(vnode)->n_fetch_bytes);
255 }
256
David Howellsd2ddc772017-11-02 15:27:50 +0000257 _leave(" = %d", ret);
258 return ret;
259}
260
261/*
Al Virof6d335c2010-05-21 15:27:09 +0100262 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
Al Virof6d335c2010-05-21 15:27:09 +0100264int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Al Virof6d335c2010-05-21 15:27:09 +0100266 struct inode *inode = page->mapping->host;
267 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells196ee9c2017-01-05 10:38:34 +0000268 struct afs_read *req;
Al Virof6d335c2010-05-21 15:27:09 +0100269 struct key *key = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int ret;
271
David Howells00d3b7a2007-04-26 15:57:07 -0700272 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Matt Mackallcd7619d2005-05-01 08:59:01 -0700274 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700277 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto error;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* is it cached? */
David Howells9b3f26c2009-04-03 16:42:41 +0100281#ifdef CONFIG_AFS_FSCACHE
282 ret = fscache_read_or_alloc_page(vnode->cache,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 page,
284 afs_file_readpage_read_complete,
285 NULL,
286 GFP_KERNEL);
287#else
288 ret = -ENOBUFS;
289#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 switch (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* read BIO submitted (page in cache) */
292 case 0:
293 break;
294
David Howells9b3f26c2009-04-03 16:42:41 +0100295 /* page not yet cached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 case -ENODATA:
David Howells9b3f26c2009-04-03 16:42:41 +0100297 _debug("cache said ENODATA");
298 goto go_on;
299
300 /* page will not be cached */
301 case -ENOBUFS:
302 _debug("cache said ENOBUFS");
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600303
304 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 default:
David Howells9b3f26c2009-04-03 16:42:41 +0100306 go_on:
David Howells196ee9c2017-01-05 10:38:34 +0000307 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
308 GFP_KERNEL);
309 if (!req)
310 goto enomem;
311
David Howells6db3ac32017-03-16 16:27:44 +0000312 /* We request a full page. If the page is a partial one at the
313 * end of the file, the server will return a short read and the
314 * unmarshalling code will clear the unfilled space.
315 */
David Howellsf3ddee82018-04-06 14:17:25 +0100316 refcount_set(&req->usage, 1);
David Howells196ee9c2017-01-05 10:38:34 +0000317 req->pos = (loff_t)page->index << PAGE_SHIFT;
David Howells6db3ac32017-03-16 16:27:44 +0000318 req->len = PAGE_SIZE;
David Howells196ee9c2017-01-05 10:38:34 +0000319 req->nr_pages = 1;
David Howellsf3ddee82018-04-06 14:17:25 +0100320 req->pages = req->array;
David Howells196ee9c2017-01-05 10:38:34 +0000321 req->pages[0] = page;
322 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* read the contents of the file from the server into the
325 * page */
David Howellsd2ddc772017-11-02 15:27:50 +0000326 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +0000327 afs_put_read(req);
David Howellsdab17c12017-11-02 15:27:52 +0000328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700330 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 _debug("got NOENT from server"
332 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700333 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ret = -ESTALE;
335 }
David Howells9b3f26c2009-04-03 16:42:41 +0100336
337#ifdef CONFIG_AFS_FSCACHE
338 fscache_uncache_page(vnode->cache, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100340 BUG_ON(PageFsCache(page));
David Howells68ae8492017-03-16 16:27:48 +0000341
342 if (ret == -EINTR ||
343 ret == -ENOMEM ||
344 ret == -ERESTARTSYS ||
345 ret == -EAGAIN)
346 goto error;
347 goto io_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 SetPageUptodate(page);
351
David Howells9b3f26c2009-04-03 16:42:41 +0100352 /* send the page to the cache */
353#ifdef CONFIG_AFS_FSCACHE
354 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100355 fscache_write_page(vnode->cache, page, vnode->status.size,
356 GFP_KERNEL) != 0) {
David Howells9b3f26c2009-04-03 16:42:41 +0100357 fscache_uncache_page(vnode->cache, page);
358 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100361 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 _leave(" = 0");
365 return 0;
366
David Howells68ae8492017-03-16 16:27:48 +0000367io_error:
368 SetPageError(page);
369 goto error;
David Howells196ee9c2017-01-05 10:38:34 +0000370enomem:
371 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -0700372error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 _leave(" = %d", ret);
375 return ret;
David Howellsec268152007-04-26 15:49:28 -0700376}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/*
Al Virof6d335c2010-05-21 15:27:09 +0100379 * read page from file, directory or symlink, given a file to nominate the key
380 * to be used
381 */
382static int afs_readpage(struct file *file, struct page *page)
383{
384 struct key *key;
385 int ret;
386
387 if (file) {
David Howells215804a2017-11-02 15:27:52 +0000388 key = afs_file_key(file);
Al Virof6d335c2010-05-21 15:27:09 +0100389 ASSERT(key != NULL);
390 ret = afs_page_filler(key, page);
391 } else {
392 struct inode *inode = page->mapping->host;
David Howellsd2ddc772017-11-02 15:27:50 +0000393 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
Al Virof6d335c2010-05-21 15:27:09 +0100394 if (IS_ERR(key)) {
395 ret = PTR_ERR(key);
396 } else {
397 ret = afs_page_filler(key, page);
398 key_put(key);
399 }
400 }
401 return ret;
402}
403
404/*
David Howells91b467e02017-01-05 10:38:35 +0000405 * Make pages available as they're filled.
406 */
407static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req)
408{
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000409#ifdef CONFIG_AFS_FSCACHE
David Howells97e30432017-11-02 15:27:48 +0000410 struct afs_vnode *vnode = call->reply[0];
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000411#endif
David Howells91b467e02017-01-05 10:38:35 +0000412 struct page *page = req->pages[req->index];
413
414 req->pages[req->index] = NULL;
415 SetPageUptodate(page);
416
417 /* send the page to the cache */
418#ifdef CONFIG_AFS_FSCACHE
419 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100420 fscache_write_page(vnode->cache, page, vnode->status.size,
421 GFP_KERNEL) != 0) {
David Howells91b467e02017-01-05 10:38:35 +0000422 fscache_uncache_page(vnode->cache, page);
423 BUG_ON(PageFsCache(page));
424 }
425#endif
426 unlock_page(page);
427 put_page(page);
428}
429
430/*
431 * Read a contiguous set of pages.
432 */
433static int afs_readpages_one(struct file *file, struct address_space *mapping,
434 struct list_head *pages)
435{
436 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
437 struct afs_read *req;
438 struct list_head *p;
439 struct page *first, *page;
David Howells215804a2017-11-02 15:27:52 +0000440 struct key *key = afs_file_key(file);
David Howells91b467e02017-01-05 10:38:35 +0000441 pgoff_t index;
442 int ret, n, i;
443
444 /* Count the number of contiguous pages at the front of the list. Note
445 * that the list goes prev-wards rather than next-wards.
446 */
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800447 first = lru_to_page(pages);
David Howells91b467e02017-01-05 10:38:35 +0000448 index = first->index + 1;
449 n = 1;
450 for (p = first->lru.prev; p != pages; p = p->prev) {
451 page = list_entry(p, struct page, lru);
452 if (page->index != index)
453 break;
454 index++;
455 n++;
456 }
457
458 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
459 GFP_NOFS);
460 if (!req)
461 return -ENOMEM;
462
David Howellsf3ddee82018-04-06 14:17:25 +0100463 refcount_set(&req->usage, 1);
David Howells91b467e02017-01-05 10:38:35 +0000464 req->page_done = afs_readpages_page_done;
465 req->pos = first->index;
466 req->pos <<= PAGE_SHIFT;
David Howellsf3ddee82018-04-06 14:17:25 +0100467 req->pages = req->array;
David Howells91b467e02017-01-05 10:38:35 +0000468
469 /* Transfer the pages to the request. We add them in until one fails
470 * to add to the LRU and then we stop (as that'll make a hole in the
471 * contiguous run.
472 *
473 * Note that it's possible for the file size to change whilst we're
474 * doing this, but we rely on the server returning less than we asked
475 * for if the file shrank. We also rely on this to deal with a partial
476 * page at the end of the file.
477 */
478 do {
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800479 page = lru_to_page(pages);
David Howells91b467e02017-01-05 10:38:35 +0000480 list_del(&page->lru);
481 index = page->index;
482 if (add_to_page_cache_lru(page, mapping, index,
483 readahead_gfp_mask(mapping))) {
484#ifdef CONFIG_AFS_FSCACHE
485 fscache_uncache_page(vnode->cache, page);
486#endif
487 put_page(page);
488 break;
489 }
490
491 req->pages[req->nr_pages++] = page;
492 req->len += PAGE_SIZE;
493 } while (req->nr_pages < n);
494
495 if (req->nr_pages == 0) {
496 kfree(req);
497 return 0;
498 }
499
David Howellsd2ddc772017-11-02 15:27:50 +0000500 ret = afs_fetch_data(vnode, key, req);
David Howells91b467e02017-01-05 10:38:35 +0000501 if (ret < 0)
502 goto error;
503
504 task_io_account_read(PAGE_SIZE * req->nr_pages);
505 afs_put_read(req);
506 return 0;
507
508error:
509 if (ret == -ENOENT) {
510 _debug("got NOENT from server"
511 " - marking file deleted and stale");
512 set_bit(AFS_VNODE_DELETED, &vnode->flags);
513 ret = -ESTALE;
514 }
515
516 for (i = 0; i < req->nr_pages; i++) {
517 page = req->pages[i];
518 if (page) {
519#ifdef CONFIG_AFS_FSCACHE
520 fscache_uncache_page(vnode->cache, page);
521#endif
522 SetPageError(page);
523 unlock_page(page);
524 }
525 }
526
527 afs_put_read(req);
528 return ret;
529}
530
531/*
David Howells9b3f26c2009-04-03 16:42:41 +0100532 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
David Howells9b3f26c2009-04-03 16:42:41 +0100534static int afs_readpages(struct file *file, struct address_space *mapping,
535 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
David Howells215804a2017-11-02 15:27:52 +0000537 struct key *key = afs_file_key(file);
David Howells9b3f26c2009-04-03 16:42:41 +0100538 struct afs_vnode *vnode;
539 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Al Virof6d335c2010-05-21 15:27:09 +0100541 _enter("{%d},{%lu},,%d",
542 key_serial(key), mapping->host->i_ino, nr_pages);
543
544 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
David Howells9b3f26c2009-04-03 16:42:41 +0100546 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000547 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100548 _leave(" = -ESTALE");
549 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
David Howells9b3f26c2009-04-03 16:42:41 +0100552 /* attempt to read as many of the pages as possible */
553#ifdef CONFIG_AFS_FSCACHE
554 ret = fscache_read_or_alloc_pages(vnode->cache,
555 mapping,
556 pages,
557 &nr_pages,
558 afs_file_readpage_read_complete,
559 NULL,
560 mapping_gfp_mask(mapping));
561#else
562 ret = -ENOBUFS;
563#endif
564
565 switch (ret) {
566 /* all pages are being read from the cache */
567 case 0:
568 BUG_ON(!list_empty(pages));
569 BUG_ON(nr_pages != 0);
570 _leave(" = 0 [reading all]");
571 return 0;
572
573 /* there were pages that couldn't be read from the cache */
574 case -ENODATA:
575 case -ENOBUFS:
576 break;
577
578 /* other error */
579 default:
580 _leave(" = %d", ret);
581 return ret;
582 }
583
David Howells91b467e02017-01-05 10:38:35 +0000584 while (!list_empty(pages)) {
585 ret = afs_readpages_one(file, mapping, pages);
586 if (ret < 0)
587 break;
588 }
David Howells9b3f26c2009-04-03 16:42:41 +0100589
590 _leave(" = %d [netting]", ret);
591 return ret;
David Howellsec268152007-04-26 15:49:28 -0700592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/*
David Howells9b3f26c2009-04-03 16:42:41 +0100595 * invalidate part or all of a page
596 * - release a page and clean up its private data if offset is 0 (indicating
597 * the entire page)
598 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400599static void afs_invalidatepage(struct page *page, unsigned int offset,
600 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100601{
David Howells13524ab2017-11-02 15:27:53 +0000602 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
603 unsigned long priv;
604
Lukas Czernerd47992f2013-05-21 23:17:23 -0400605 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100606
607 BUG_ON(!PageLocked(page));
608
609 /* we clean up only if the entire page is being invalidated */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300610 if (offset == 0 && length == PAGE_SIZE) {
David Howells9b3f26c2009-04-03 16:42:41 +0100611#ifdef CONFIG_AFS_FSCACHE
612 if (PageFsCache(page)) {
613 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
614 fscache_wait_on_page_write(vnode->cache, page);
615 fscache_uncache_page(vnode->cache, page);
David Howells9b3f26c2009-04-03 16:42:41 +0100616 }
617#endif
618
619 if (PagePrivate(page)) {
David Howells13524ab2017-11-02 15:27:53 +0000620 priv = page_private(page);
621 trace_afs_page_dirty(vnode, tracepoint_string("inval"),
622 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000623 set_page_private(page, 0);
624 ClearPagePrivate(page);
David Howells9b3f26c2009-04-03 16:42:41 +0100625 }
626 }
627
628 _leave("");
629}
630
631/*
632 * release a page and clean up its private state if it's not busy
633 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
David Howells416351f2007-05-09 02:33:45 -0700635static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
David Howells416351f2007-05-09 02:33:45 -0700637 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
David Howells13524ab2017-11-02 15:27:53 +0000638 unsigned long priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
David Howells3b6492d2018-10-20 00:57:57 +0100640 _enter("{{%llx:%llu}[%lu],%lx},%x",
David Howells416351f2007-05-09 02:33:45 -0700641 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
642 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
David Howells9b3f26c2009-04-03 16:42:41 +0100644 /* deny if page is being written to the cache and the caller hasn't
645 * elected to wait */
646#ifdef CONFIG_AFS_FSCACHE
David Howells201a1542009-11-19 18:11:35 +0000647 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
648 _leave(" = F [cache busy]");
649 return 0;
David Howells9b3f26c2009-04-03 16:42:41 +0100650 }
651#endif
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (PagePrivate(page)) {
David Howells13524ab2017-11-02 15:27:53 +0000654 priv = page_private(page);
655 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
656 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000657 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 ClearPagePrivate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
David Howells9b3f26c2009-04-03 16:42:41 +0100661 /* indicate that the page can be released */
662 _leave(" = T");
663 return 1;
David Howellsec268152007-04-26 15:49:28 -0700664}
David Howells1cf7a152017-11-02 15:27:52 +0000665
666/*
667 * Handle setting up a memory mapping on an AFS file.
668 */
669static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
670{
671 int ret;
672
673 ret = generic_file_mmap(file, vma);
674 if (ret == 0)
675 vma->vm_ops = &afs_vm_ops;
676 return ret;
677}