blob: 67cd782a1c64f8d867ef446545e587d2c0a0da60 [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>
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 Howellsa1b879e2019-05-15 12:09:17 +0100173 int ret = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700174
David Howells3b6492d2018-10-20 00:57:57 +0100175 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700176
David Howells5a813272018-04-06 14:17:26 +0100177 if ((file->f_mode & FMODE_WRITE))
David Howellsa1b879e2019-05-15 12:09:17 +0100178 ret = vfs_fsync(file, 0);
David Howells5a813272018-04-06 14:17:26 +0100179
David Howells215804a2017-11-02 15:27:52 +0000180 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000181 if (af->wb)
182 afs_put_wb_key(af->wb);
David Howells215804a2017-11-02 15:27:52 +0000183 key_put(af->key);
184 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000185 afs_prune_wb_keys(vnode);
David Howellsa1b879e2019-05-15 12:09:17 +0100186 _leave(" = %d", ret);
187 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700188}
189
David Howells196ee9c2017-01-05 10:38:34 +0000190/*
191 * Dispose of a ref to a read record.
192 */
193void afs_put_read(struct afs_read *req)
194{
195 int i;
196
David Howellsf3ddee82018-04-06 14:17:25 +0100197 if (refcount_dec_and_test(&req->usage)) {
David Howells196ee9c2017-01-05 10:38:34 +0000198 for (i = 0; i < req->nr_pages; i++)
199 if (req->pages[i])
200 put_page(req->pages[i]);
David Howellsf3ddee82018-04-06 14:17:25 +0100201 if (req->pages != req->array)
202 kfree(req->pages);
David Howells196ee9c2017-01-05 10:38:34 +0000203 kfree(req);
204 }
205}
206
Matt Kraai6566abd2009-04-17 12:56:38 +0100207#ifdef CONFIG_AFS_FSCACHE
David Howells00d3b7a2007-04-26 15:57:07 -0700208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * deal with notification that a page was read from the cache
210 */
David Howells9b3f26c2009-04-03 16:42:41 +0100211static void afs_file_readpage_read_complete(struct page *page,
212 void *data,
213 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
David Howells9b3f26c2009-04-03 16:42:41 +0100215 _enter("%p,%p,%d", page, data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
David Howells9b3f26c2009-04-03 16:42:41 +0100217 /* if the read completes with an error, we just unlock the page and let
218 * the VM reissue the readpage */
219 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 SetPageUptodate(page);
221 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700222}
Matt Kraai6566abd2009-04-17 12:56:38 +0100223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
David Howellsd2ddc772017-11-02 15:27:50 +0000226 * Fetch file data from the volume.
227 */
228int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *desc)
229{
230 struct afs_fs_cursor fc;
David Howellsa58823a2019-05-09 15:16:10 +0100231 struct afs_status_cb *scb;
David Howellsd2ddc772017-11-02 15:27:50 +0000232 int ret;
233
David Howells3b6492d2018-10-20 00:57:57 +0100234 _enter("%s{%llx:%llu.%u},%x,,,",
David Howellsd2ddc772017-11-02 15:27:50 +0000235 vnode->volume->name,
236 vnode->fid.vid,
237 vnode->fid.vnode,
238 vnode->fid.unique,
239 key_serial(key));
240
David Howellsa58823a2019-05-09 15:16:10 +0100241 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
242 if (!scb)
243 return -ENOMEM;
244
David Howellsd2ddc772017-11-02 15:27:50 +0000245 ret = -ERESTARTSYS;
David Howells20b83912019-05-08 16:16:31 +0100246 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
David Howellsa58823a2019-05-09 15:16:10 +0100247 afs_dataversion_t data_version = vnode->status.data_version;
248
David Howellsd2ddc772017-11-02 15:27:50 +0000249 while (afs_select_fileserver(&fc)) {
David Howells68251f02018-05-12 22:31:33 +0100250 fc.cb_break = afs_calc_vnode_cb_break(vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100251 afs_fs_fetch_data(&fc, scb, desc);
David Howellsd2ddc772017-11-02 15:27:50 +0000252 }
253
David Howellsa58823a2019-05-09 15:16:10 +0100254 afs_check_for_remote_deletion(&fc, vnode);
255 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
256 &data_version, scb);
David Howellsd2ddc772017-11-02 15:27:50 +0000257 ret = afs_end_vnode_operation(&fc);
258 }
259
David Howells76a5cb62018-04-06 14:17:26 +0100260 if (ret == 0) {
261 afs_stat_v(vnode, n_fetches);
262 atomic_long_add(desc->actual_len,
263 &afs_v2net(vnode)->n_fetch_bytes);
264 }
265
David Howellsa58823a2019-05-09 15:16:10 +0100266 kfree(scb);
David Howellsd2ddc772017-11-02 15:27:50 +0000267 _leave(" = %d", ret);
268 return ret;
269}
270
271/*
Al Virof6d335c2010-05-21 15:27:09 +0100272 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Al Virof6d335c2010-05-21 15:27:09 +0100274int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Al Virof6d335c2010-05-21 15:27:09 +0100276 struct inode *inode = page->mapping->host;
277 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells196ee9c2017-01-05 10:38:34 +0000278 struct afs_read *req;
Al Virof6d335c2010-05-21 15:27:09 +0100279 struct key *key = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int ret;
281
David Howells00d3b7a2007-04-26 15:57:07 -0700282 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Matt Mackallcd7619d2005-05-01 08:59:01 -0700284 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700287 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 goto error;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* is it cached? */
David Howells9b3f26c2009-04-03 16:42:41 +0100291#ifdef CONFIG_AFS_FSCACHE
292 ret = fscache_read_or_alloc_page(vnode->cache,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 page,
294 afs_file_readpage_read_complete,
295 NULL,
296 GFP_KERNEL);
297#else
298 ret = -ENOBUFS;
299#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 switch (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* read BIO submitted (page in cache) */
302 case 0:
303 break;
304
David Howells9b3f26c2009-04-03 16:42:41 +0100305 /* page not yet cached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 case -ENODATA:
David Howells9b3f26c2009-04-03 16:42:41 +0100307 _debug("cache said ENODATA");
308 goto go_on;
309
310 /* page will not be cached */
311 case -ENOBUFS:
312 _debug("cache said ENOBUFS");
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600313
314 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 default:
David Howells9b3f26c2009-04-03 16:42:41 +0100316 go_on:
Zhengyuan Liuee102582019-06-20 18:12:17 +0100317 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
David Howells196ee9c2017-01-05 10:38:34 +0000318 if (!req)
319 goto enomem;
320
David Howells6db3ac32017-03-16 16:27:44 +0000321 /* We request a full page. If the page is a partial one at the
322 * end of the file, the server will return a short read and the
323 * unmarshalling code will clear the unfilled space.
324 */
David Howellsf3ddee82018-04-06 14:17:25 +0100325 refcount_set(&req->usage, 1);
David Howells196ee9c2017-01-05 10:38:34 +0000326 req->pos = (loff_t)page->index << PAGE_SHIFT;
David Howells6db3ac32017-03-16 16:27:44 +0000327 req->len = PAGE_SIZE;
David Howells196ee9c2017-01-05 10:38:34 +0000328 req->nr_pages = 1;
David Howellsf3ddee82018-04-06 14:17:25 +0100329 req->pages = req->array;
David Howells196ee9c2017-01-05 10:38:34 +0000330 req->pages[0] = page;
331 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* read the contents of the file from the server into the
334 * page */
David Howellsd2ddc772017-11-02 15:27:50 +0000335 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +0000336 afs_put_read(req);
David Howellsdab17c12017-11-02 15:27:52 +0000337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700339 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 _debug("got NOENT from server"
341 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700342 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ret = -ESTALE;
344 }
David Howells9b3f26c2009-04-03 16:42:41 +0100345
346#ifdef CONFIG_AFS_FSCACHE
347 fscache_uncache_page(vnode->cache, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100349 BUG_ON(PageFsCache(page));
David Howells68ae8492017-03-16 16:27:48 +0000350
351 if (ret == -EINTR ||
352 ret == -ENOMEM ||
353 ret == -ERESTARTSYS ||
354 ret == -EAGAIN)
355 goto error;
356 goto io_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 SetPageUptodate(page);
360
David Howells9b3f26c2009-04-03 16:42:41 +0100361 /* send the page to the cache */
362#ifdef CONFIG_AFS_FSCACHE
363 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100364 fscache_write_page(vnode->cache, page, vnode->status.size,
365 GFP_KERNEL) != 0) {
David Howells9b3f26c2009-04-03 16:42:41 +0100366 fscache_uncache_page(vnode->cache, page);
367 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100370 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 _leave(" = 0");
374 return 0;
375
David Howells68ae8492017-03-16 16:27:48 +0000376io_error:
377 SetPageError(page);
378 goto error;
David Howells196ee9c2017-01-05 10:38:34 +0000379enomem:
380 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -0700381error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 _leave(" = %d", ret);
384 return ret;
David Howellsec268152007-04-26 15:49:28 -0700385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
Al Virof6d335c2010-05-21 15:27:09 +0100388 * read page from file, directory or symlink, given a file to nominate the key
389 * to be used
390 */
391static int afs_readpage(struct file *file, struct page *page)
392{
393 struct key *key;
394 int ret;
395
396 if (file) {
David Howells215804a2017-11-02 15:27:52 +0000397 key = afs_file_key(file);
Al Virof6d335c2010-05-21 15:27:09 +0100398 ASSERT(key != NULL);
399 ret = afs_page_filler(key, page);
400 } else {
401 struct inode *inode = page->mapping->host;
David Howellsd2ddc772017-11-02 15:27:50 +0000402 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
Al Virof6d335c2010-05-21 15:27:09 +0100403 if (IS_ERR(key)) {
404 ret = PTR_ERR(key);
405 } else {
406 ret = afs_page_filler(key, page);
407 key_put(key);
408 }
409 }
410 return ret;
411}
412
413/*
David Howells91b467e2017-01-05 10:38:35 +0000414 * Make pages available as they're filled.
415 */
David Howellsa58823a2019-05-09 15:16:10 +0100416static void afs_readpages_page_done(struct afs_read *req)
David Howells91b467e2017-01-05 10:38:35 +0000417{
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000418#ifdef CONFIG_AFS_FSCACHE
David Howellsa58823a2019-05-09 15:16:10 +0100419 struct afs_vnode *vnode = req->vnode;
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000420#endif
David Howells91b467e2017-01-05 10:38:35 +0000421 struct page *page = req->pages[req->index];
422
423 req->pages[req->index] = NULL;
424 SetPageUptodate(page);
425
426 /* send the page to the cache */
427#ifdef CONFIG_AFS_FSCACHE
428 if (PageFsCache(page) &&
David Howellsee1235a2018-04-04 13:41:28 +0100429 fscache_write_page(vnode->cache, page, vnode->status.size,
430 GFP_KERNEL) != 0) {
David Howells91b467e2017-01-05 10:38:35 +0000431 fscache_uncache_page(vnode->cache, page);
432 BUG_ON(PageFsCache(page));
433 }
434#endif
435 unlock_page(page);
436 put_page(page);
437}
438
439/*
440 * Read a contiguous set of pages.
441 */
442static int afs_readpages_one(struct file *file, struct address_space *mapping,
443 struct list_head *pages)
444{
445 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
446 struct afs_read *req;
447 struct list_head *p;
448 struct page *first, *page;
David Howells215804a2017-11-02 15:27:52 +0000449 struct key *key = afs_file_key(file);
David Howells91b467e2017-01-05 10:38:35 +0000450 pgoff_t index;
451 int ret, n, i;
452
453 /* Count the number of contiguous pages at the front of the list. Note
454 * that the list goes prev-wards rather than next-wards.
455 */
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800456 first = lru_to_page(pages);
David Howells91b467e2017-01-05 10:38:35 +0000457 index = first->index + 1;
458 n = 1;
459 for (p = first->lru.prev; p != pages; p = p->prev) {
460 page = list_entry(p, struct page, lru);
461 if (page->index != index)
462 break;
463 index++;
464 n++;
465 }
466
Zhengyuan Liuee102582019-06-20 18:12:17 +0100467 req = kzalloc(struct_size(req, array, n), GFP_NOFS);
David Howells91b467e2017-01-05 10:38:35 +0000468 if (!req)
469 return -ENOMEM;
470
David Howellsf3ddee82018-04-06 14:17:25 +0100471 refcount_set(&req->usage, 1);
David Howellsa58823a2019-05-09 15:16:10 +0100472 req->vnode = vnode;
David Howells91b467e2017-01-05 10:38:35 +0000473 req->page_done = afs_readpages_page_done;
474 req->pos = first->index;
475 req->pos <<= PAGE_SHIFT;
David Howellsf3ddee82018-04-06 14:17:25 +0100476 req->pages = req->array;
David Howells91b467e2017-01-05 10:38:35 +0000477
478 /* Transfer the pages to the request. We add them in until one fails
479 * to add to the LRU and then we stop (as that'll make a hole in the
480 * contiguous run.
481 *
482 * Note that it's possible for the file size to change whilst we're
483 * doing this, but we rely on the server returning less than we asked
484 * for if the file shrank. We also rely on this to deal with a partial
485 * page at the end of the file.
486 */
487 do {
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800488 page = lru_to_page(pages);
David Howells91b467e2017-01-05 10:38:35 +0000489 list_del(&page->lru);
490 index = page->index;
491 if (add_to_page_cache_lru(page, mapping, index,
492 readahead_gfp_mask(mapping))) {
493#ifdef CONFIG_AFS_FSCACHE
494 fscache_uncache_page(vnode->cache, page);
495#endif
496 put_page(page);
497 break;
498 }
499
500 req->pages[req->nr_pages++] = page;
501 req->len += PAGE_SIZE;
502 } while (req->nr_pages < n);
503
504 if (req->nr_pages == 0) {
505 kfree(req);
506 return 0;
507 }
508
David Howellsd2ddc772017-11-02 15:27:50 +0000509 ret = afs_fetch_data(vnode, key, req);
David Howells91b467e2017-01-05 10:38:35 +0000510 if (ret < 0)
511 goto error;
512
513 task_io_account_read(PAGE_SIZE * req->nr_pages);
514 afs_put_read(req);
515 return 0;
516
517error:
518 if (ret == -ENOENT) {
519 _debug("got NOENT from server"
520 " - marking file deleted and stale");
521 set_bit(AFS_VNODE_DELETED, &vnode->flags);
522 ret = -ESTALE;
523 }
524
525 for (i = 0; i < req->nr_pages; i++) {
526 page = req->pages[i];
527 if (page) {
528#ifdef CONFIG_AFS_FSCACHE
529 fscache_uncache_page(vnode->cache, page);
530#endif
531 SetPageError(page);
532 unlock_page(page);
533 }
534 }
535
536 afs_put_read(req);
537 return ret;
538}
539
540/*
David Howells9b3f26c2009-04-03 16:42:41 +0100541 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
David Howells9b3f26c2009-04-03 16:42:41 +0100543static int afs_readpages(struct file *file, struct address_space *mapping,
544 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
David Howells215804a2017-11-02 15:27:52 +0000546 struct key *key = afs_file_key(file);
David Howells9b3f26c2009-04-03 16:42:41 +0100547 struct afs_vnode *vnode;
548 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Al Virof6d335c2010-05-21 15:27:09 +0100550 _enter("{%d},{%lu},,%d",
551 key_serial(key), mapping->host->i_ino, nr_pages);
552
553 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
David Howells9b3f26c2009-04-03 16:42:41 +0100555 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000556 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100557 _leave(" = -ESTALE");
558 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
David Howells9b3f26c2009-04-03 16:42:41 +0100561 /* attempt to read as many of the pages as possible */
562#ifdef CONFIG_AFS_FSCACHE
563 ret = fscache_read_or_alloc_pages(vnode->cache,
564 mapping,
565 pages,
566 &nr_pages,
567 afs_file_readpage_read_complete,
568 NULL,
569 mapping_gfp_mask(mapping));
570#else
571 ret = -ENOBUFS;
572#endif
573
574 switch (ret) {
575 /* all pages are being read from the cache */
576 case 0:
577 BUG_ON(!list_empty(pages));
578 BUG_ON(nr_pages != 0);
579 _leave(" = 0 [reading all]");
580 return 0;
581
582 /* there were pages that couldn't be read from the cache */
583 case -ENODATA:
584 case -ENOBUFS:
585 break;
586
587 /* other error */
588 default:
589 _leave(" = %d", ret);
590 return ret;
591 }
592
David Howells91b467e2017-01-05 10:38:35 +0000593 while (!list_empty(pages)) {
594 ret = afs_readpages_one(file, mapping, pages);
595 if (ret < 0)
596 break;
597 }
David Howells9b3f26c2009-04-03 16:42:41 +0100598
599 _leave(" = %d [netting]", ret);
600 return ret;
David Howellsec268152007-04-26 15:49:28 -0700601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/*
David Howells9b3f26c2009-04-03 16:42:41 +0100604 * invalidate part or all of a page
605 * - release a page and clean up its private data if offset is 0 (indicating
606 * the entire page)
607 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400608static void afs_invalidatepage(struct page *page, unsigned int offset,
609 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100610{
David Howells13524ab2017-11-02 15:27:53 +0000611 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
612 unsigned long priv;
613
Lukas Czernerd47992f2013-05-21 23:17:23 -0400614 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100615
616 BUG_ON(!PageLocked(page));
617
618 /* we clean up only if the entire page is being invalidated */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300619 if (offset == 0 && length == PAGE_SIZE) {
David Howells9b3f26c2009-04-03 16:42:41 +0100620#ifdef CONFIG_AFS_FSCACHE
621 if (PageFsCache(page)) {
622 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
623 fscache_wait_on_page_write(vnode->cache, page);
624 fscache_uncache_page(vnode->cache, page);
David Howells9b3f26c2009-04-03 16:42:41 +0100625 }
626#endif
627
628 if (PagePrivate(page)) {
David Howells13524ab2017-11-02 15:27:53 +0000629 priv = page_private(page);
630 trace_afs_page_dirty(vnode, tracepoint_string("inval"),
631 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000632 set_page_private(page, 0);
633 ClearPagePrivate(page);
David Howells9b3f26c2009-04-03 16:42:41 +0100634 }
635 }
636
637 _leave("");
638}
639
640/*
641 * release a page and clean up its private state if it's not busy
642 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
David Howells416351f2007-05-09 02:33:45 -0700644static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
David Howells416351f2007-05-09 02:33:45 -0700646 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
David Howells13524ab2017-11-02 15:27:53 +0000647 unsigned long priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
David Howells3b6492d2018-10-20 00:57:57 +0100649 _enter("{{%llx:%llu}[%lu],%lx},%x",
David Howells416351f2007-05-09 02:33:45 -0700650 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
651 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
David Howells9b3f26c2009-04-03 16:42:41 +0100653 /* deny if page is being written to the cache and the caller hasn't
654 * elected to wait */
655#ifdef CONFIG_AFS_FSCACHE
David Howells201a1542009-11-19 18:11:35 +0000656 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
657 _leave(" = F [cache busy]");
658 return 0;
David Howells9b3f26c2009-04-03 16:42:41 +0100659 }
660#endif
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (PagePrivate(page)) {
David Howells13524ab2017-11-02 15:27:53 +0000663 priv = page_private(page);
664 trace_afs_page_dirty(vnode, tracepoint_string("rel"),
665 page->index, priv);
David Howells4343d002017-11-02 15:27:52 +0000666 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ClearPagePrivate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
David Howells9b3f26c2009-04-03 16:42:41 +0100670 /* indicate that the page can be released */
671 _leave(" = T");
672 return 1;
David Howellsec268152007-04-26 15:49:28 -0700673}
David Howells1cf7a152017-11-02 15:27:52 +0000674
675/*
676 * Handle setting up a memory mapping on an AFS file.
677 */
678static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
679{
680 int ret;
681
682 ret = generic_file_mmap(file, vma);
683 if (ret == 0)
684 vma->vm_ops = &afs_vm_ops;
685 return ret;
686}