blob: af6471defec3e0a7ad639c0b71e52f2a6e2374e1 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells08e0e7c2007-04-26 15:55:03 -07002/* AFS filesystem file handling
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells08e0e7c2007-04-26 15:55:03 -07004 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fs.h>
12#include <linux/pagemap.h>
David Howells31143d52007-05-09 02:33:46 -070013#include <linux/writeback.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
David Howells91b467e2017-01-05 10:38:35 +000015#include <linux/task_io_accounting_ops.h>
Nikolay Borisovf86196e2019-01-03 15:29:02 -080016#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "internal.h"
18
David Howells1cf7a152017-11-02 15:27:52 +000019static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
David Howells416351f2007-05-09 02:33:45 -070020static int afs_readpage(struct file *file, struct page *page);
Lukas Czernerd47992f2013-05-21 23:17:23 -040021static void afs_invalidatepage(struct page *page, unsigned int offset,
22 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070023static int afs_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David Howells9b3f26c2009-04-03 16:42:41 +010025static int afs_readpages(struct file *filp, struct address_space *mapping,
26 struct list_head *pages, unsigned nr_pages);
27
David Howells00d3b7a2007-04-26 15:57:07 -070028const struct file_operations afs_file_operations = {
29 .open = afs_open,
30 .release = afs_release,
31 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040032 .read_iter = generic_file_read_iter,
Al Viro50b55512014-04-03 14:13:46 -040033 .write_iter = afs_file_write,
David Howells1cf7a152017-11-02 15:27:52 +000034 .mmap = afs_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020035 .splice_read = generic_file_splice_read,
David Howells06a17bb2020-10-27 09:39:04 +000036 .splice_write = iter_file_splice_write,
David Howells31143d52007-05-09 02:33:46 -070037 .fsync = afs_fsync,
David Howellse8d6c552007-07-15 23:40:12 -070038 .lock = afs_lock,
39 .flock = afs_flock,
David Howells00d3b7a2007-04-26 15:57:07 -070040};
41
Arjan van de Ven754661f2007-02-12 00:55:38 -080042const struct inode_operations afs_file_inode_operations = {
David Howells416351f2007-05-09 02:33:45 -070043 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070044 .setattr = afs_setattr,
David Howells00d3b7a2007-04-26 15:57:07 -070045 .permission = afs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070048const struct address_space_operations afs_fs_aops = {
David Howells416351f2007-05-09 02:33:45 -070049 .readpage = afs_readpage,
David Howells9b3f26c2009-04-03 16:42:41 +010050 .readpages = afs_readpages,
David Howells31143d52007-05-09 02:33:46 -070051 .set_page_dirty = afs_set_page_dirty,
52 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070053 .releasepage = afs_releasepage,
54 .invalidatepage = afs_invalidatepage,
Nick Piggin15b46502008-10-15 22:04:32 -070055 .write_begin = afs_write_begin,
56 .write_end = afs_write_end,
David Howells31143d52007-05-09 02:33:46 -070057 .writepage = afs_writepage,
58 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
David Howells1cf7a152017-11-02 15:27:52 +000061static const struct vm_operations_struct afs_vm_ops = {
62 .fault = filemap_fault,
63 .map_pages = filemap_map_pages,
64 .page_mkwrite = afs_page_mkwrite,
65};
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
David Howells4343d002017-11-02 15:27:52 +000068 * Discard a pin on a writeback key.
69 */
70void afs_put_wb_key(struct afs_wb_key *wbk)
71{
David Howellse49c7b22020-04-10 20:51:51 +010072 if (wbk && refcount_dec_and_test(&wbk->usage)) {
David Howells4343d002017-11-02 15:27:52 +000073 key_put(wbk->key);
74 kfree(wbk);
75 }
76}
77
78/*
79 * Cache key for writeback.
80 */
81int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
82{
83 struct afs_wb_key *wbk, *p;
84
85 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
86 if (!wbk)
87 return -ENOMEM;
88 refcount_set(&wbk->usage, 2);
89 wbk->key = af->key;
90
91 spin_lock(&vnode->wb_lock);
92 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
93 if (p->key == wbk->key)
94 goto found;
95 }
96
97 key_get(wbk->key);
98 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
99 spin_unlock(&vnode->wb_lock);
100 af->wb = wbk;
101 return 0;
102
103found:
104 refcount_inc(&p->usage);
105 spin_unlock(&vnode->wb_lock);
106 af->wb = p;
107 kfree(wbk);
108 return 0;
109}
110
111/*
David Howells00d3b7a2007-04-26 15:57:07 -0700112 * open an AFS file or directory and attach a key to it
113 */
114int afs_open(struct inode *inode, struct file *file)
115{
116 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000117 struct afs_file *af;
David Howells00d3b7a2007-04-26 15:57:07 -0700118 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700119 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700120
David Howells3b6492d2018-10-20 00:57:57 +0100121 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700122
123 key = afs_request_key(vnode->volume->cell);
124 if (IS_ERR(key)) {
David Howells215804a2017-11-02 15:27:52 +0000125 ret = PTR_ERR(key);
126 goto error;
127 }
128
129 af = kzalloc(sizeof(*af), GFP_KERNEL);
130 if (!af) {
131 ret = -ENOMEM;
132 goto error_key;
David Howells00d3b7a2007-04-26 15:57:07 -0700133 }
David Howells4343d002017-11-02 15:27:52 +0000134 af->key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700135
David Howells260a9802007-04-26 15:59:35 -0700136 ret = afs_validate(vnode, key);
David Howells215804a2017-11-02 15:27:52 +0000137 if (ret < 0)
138 goto error_af;
David Howells260a9802007-04-26 15:59:35 -0700139
David Howells4343d002017-11-02 15:27:52 +0000140 if (file->f_mode & FMODE_WRITE) {
141 ret = afs_cache_wb_key(vnode, af);
142 if (ret < 0)
143 goto error_af;
144 }
David Howells5a813272018-04-06 14:17:26 +0100145
146 if (file->f_flags & O_TRUNC)
147 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howells4343d002017-11-02 15:27:52 +0000148
David Howells215804a2017-11-02 15:27:52 +0000149 file->private_data = af;
David Howells00d3b7a2007-04-26 15:57:07 -0700150 _leave(" = 0");
151 return 0;
David Howells215804a2017-11-02 15:27:52 +0000152
153error_af:
154 kfree(af);
155error_key:
156 key_put(key);
157error:
158 _leave(" = %d", ret);
159 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700160}
161
162/*
163 * release an AFS file or directory and discard its key
164 */
165int afs_release(struct inode *inode, struct file *file)
166{
167 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000168 struct afs_file *af = file->private_data;
David Howellsa1b879e2019-05-15 12:09:17 +0100169 int ret = 0;
David Howells00d3b7a2007-04-26 15:57:07 -0700170
David Howells3b6492d2018-10-20 00:57:57 +0100171 _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700172
David Howells5a813272018-04-06 14:17:26 +0100173 if ((file->f_mode & FMODE_WRITE))
David Howellsa1b879e2019-05-15 12:09:17 +0100174 ret = vfs_fsync(file, 0);
David Howells5a813272018-04-06 14:17:26 +0100175
David Howells215804a2017-11-02 15:27:52 +0000176 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000177 if (af->wb)
178 afs_put_wb_key(af->wb);
David Howells215804a2017-11-02 15:27:52 +0000179 key_put(af->key);
180 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000181 afs_prune_wb_keys(vnode);
David Howellsa1b879e2019-05-15 12:09:17 +0100182 _leave(" = %d", ret);
183 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700184}
185
David Howells196ee9c2017-01-05 10:38:34 +0000186/*
187 * Dispose of a ref to a read record.
188 */
189void afs_put_read(struct afs_read *req)
190{
191 int i;
192
David Howellsf3ddee82018-04-06 14:17:25 +0100193 if (refcount_dec_and_test(&req->usage)) {
Jia-Ju Baia6eed4a2019-07-30 14:38:51 +0100194 if (req->pages) {
195 for (i = 0; i < req->nr_pages; i++)
196 if (req->pages[i])
197 put_page(req->pages[i]);
198 if (req->pages != req->array)
199 kfree(req->pages);
200 }
David Howellsc69bf472020-02-06 14:22:27 +0000201 key_put(req->key);
David Howells196ee9c2017-01-05 10:38:34 +0000202 kfree(req);
203 }
204}
205
David Howellse49c7b22020-04-10 20:51:51 +0100206static void afs_fetch_data_success(struct afs_operation *op)
207{
208 struct afs_vnode *vnode = op->file[0].vnode;
209
210 _enter("op=%08x", op->debug_id);
David Howellse49c7b22020-04-10 20:51:51 +0100211 afs_vnode_commit_status(op, &op->file[0]);
212 afs_stat_v(vnode, n_fetches);
213 atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
214}
215
216static void afs_fetch_data_put(struct afs_operation *op)
217{
218 afs_put_read(op->fetch.req);
219}
220
221static const struct afs_operation_ops afs_fetch_data_operation = {
222 .issue_afs_rpc = afs_fs_fetch_data,
223 .issue_yfs_rpc = yfs_fs_fetch_data,
224 .success = afs_fetch_data_success,
David Howells728279a2020-06-16 00:34:09 +0100225 .aborted = afs_check_for_remote_deletion,
David Howellse49c7b22020-04-10 20:51:51 +0100226 .put = afs_fetch_data_put,
227};
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
David Howellsd2ddc772017-11-02 15:27:50 +0000230 * Fetch file data from the volume.
231 */
David Howellsc69bf472020-02-06 14:22:27 +0000232int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
David Howellsd2ddc772017-11-02 15:27:50 +0000233{
David Howellse49c7b22020-04-10 20:51:51 +0100234 struct afs_operation *op;
David Howellsd2ddc772017-11-02 15:27:50 +0000235
David Howells3b6492d2018-10-20 00:57:57 +0100236 _enter("%s{%llx:%llu.%u},%x,,,",
David Howellsd2ddc772017-11-02 15:27:50 +0000237 vnode->volume->name,
238 vnode->fid.vid,
239 vnode->fid.vnode,
240 vnode->fid.unique,
David Howellsc69bf472020-02-06 14:22:27 +0000241 key_serial(req->key));
David Howellsd2ddc772017-11-02 15:27:50 +0000242
David Howellsc69bf472020-02-06 14:22:27 +0000243 op = afs_alloc_operation(req->key, vnode->volume);
David Howellse49c7b22020-04-10 20:51:51 +0100244 if (IS_ERR(op))
245 return PTR_ERR(op);
David Howellsa58823a2019-05-09 15:16:10 +0100246
David Howellse49c7b22020-04-10 20:51:51 +0100247 afs_op_set_vnode(op, 0, vnode);
David Howellsa58823a2019-05-09 15:16:10 +0100248
David Howellse49c7b22020-04-10 20:51:51 +0100249 op->fetch.req = afs_get_read(req);
250 op->ops = &afs_fetch_data_operation;
251 return afs_do_sync_operation(op);
David Howellsd2ddc772017-11-02 15:27:50 +0000252}
253
254/*
Al Virof6d335c2010-05-21 15:27:09 +0100255 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Al Virof6d335c2010-05-21 15:27:09 +0100257int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Al Virof6d335c2010-05-21 15:27:09 +0100259 struct inode *inode = page->mapping->host;
260 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells196ee9c2017-01-05 10:38:34 +0000261 struct afs_read *req;
Al Virof6d335c2010-05-21 15:27:09 +0100262 struct key *key = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int ret;
264
David Howells00d3b7a2007-04-26 15:57:07 -0700265 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Matt Mackallcd7619d2005-05-01 08:59:01 -0700267 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700270 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto error;
272
David Howells03ffae92020-02-10 10:00:22 +0000273 req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
274 if (!req)
275 goto enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
David Howells03ffae92020-02-10 10:00:22 +0000277 /* We request a full page. If the page is a partial one at the
278 * end of the file, the server will return a short read and the
279 * unmarshalling code will clear the unfilled space.
280 */
281 refcount_set(&req->usage, 1);
David Howellsc69bf472020-02-06 14:22:27 +0000282 req->key = key_get(key);
David Howells03ffae92020-02-10 10:00:22 +0000283 req->pos = (loff_t)page->index << PAGE_SHIFT;
284 req->len = PAGE_SIZE;
285 req->nr_pages = 1;
286 req->pages = req->array;
287 req->pages[0] = page;
288 get_page(page);
David Howells9b3f26c2009-04-03 16:42:41 +0100289
David Howells03ffae92020-02-10 10:00:22 +0000290 /* read the contents of the file from the server into the
291 * page */
David Howellsc69bf472020-02-06 14:22:27 +0000292 ret = afs_fetch_data(vnode, req);
David Howells03ffae92020-02-10 10:00:22 +0000293 afs_put_read(req);
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600294
David Howells03ffae92020-02-10 10:00:22 +0000295 if (ret < 0) {
296 if (ret == -ENOENT) {
297 _debug("got NOENT from server"
298 " - marking file deleted and stale");
299 set_bit(AFS_VNODE_DELETED, &vnode->flags);
300 ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
David Howells03ffae92020-02-10 10:00:22 +0000303 if (ret == -EINTR ||
304 ret == -ENOMEM ||
305 ret == -ERESTARTSYS ||
306 ret == -EAGAIN)
307 goto error;
308 goto io_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
David Howells03ffae92020-02-10 10:00:22 +0000311 SetPageUptodate(page);
312 unlock_page(page);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 _leave(" = 0");
315 return 0;
316
David Howells68ae8492017-03-16 16:27:48 +0000317io_error:
318 SetPageError(page);
319 goto error;
David Howells196ee9c2017-01-05 10:38:34 +0000320enomem:
321 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -0700322error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 _leave(" = %d", ret);
325 return ret;
David Howellsec268152007-04-26 15:49:28 -0700326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*
Al Virof6d335c2010-05-21 15:27:09 +0100329 * read page from file, directory or symlink, given a file to nominate the key
330 * to be used
331 */
332static int afs_readpage(struct file *file, struct page *page)
333{
334 struct key *key;
335 int ret;
336
337 if (file) {
David Howells215804a2017-11-02 15:27:52 +0000338 key = afs_file_key(file);
Al Virof6d335c2010-05-21 15:27:09 +0100339 ASSERT(key != NULL);
340 ret = afs_page_filler(key, page);
341 } else {
342 struct inode *inode = page->mapping->host;
David Howellsd2ddc772017-11-02 15:27:50 +0000343 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
Al Virof6d335c2010-05-21 15:27:09 +0100344 if (IS_ERR(key)) {
345 ret = PTR_ERR(key);
346 } else {
347 ret = afs_page_filler(key, page);
348 key_put(key);
349 }
350 }
351 return ret;
352}
353
354/*
David Howells91b467e2017-01-05 10:38:35 +0000355 * Make pages available as they're filled.
356 */
David Howellsa58823a2019-05-09 15:16:10 +0100357static void afs_readpages_page_done(struct afs_read *req)
David Howells91b467e2017-01-05 10:38:35 +0000358{
David Howells91b467e2017-01-05 10:38:35 +0000359 struct page *page = req->pages[req->index];
360
361 req->pages[req->index] = NULL;
362 SetPageUptodate(page);
David Howells91b467e2017-01-05 10:38:35 +0000363 unlock_page(page);
364 put_page(page);
365}
366
367/*
368 * Read a contiguous set of pages.
369 */
370static int afs_readpages_one(struct file *file, struct address_space *mapping,
371 struct list_head *pages)
372{
373 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
374 struct afs_read *req;
375 struct list_head *p;
376 struct page *first, *page;
David Howells91b467e2017-01-05 10:38:35 +0000377 pgoff_t index;
378 int ret, n, i;
379
380 /* Count the number of contiguous pages at the front of the list. Note
381 * that the list goes prev-wards rather than next-wards.
382 */
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800383 first = lru_to_page(pages);
David Howells91b467e2017-01-05 10:38:35 +0000384 index = first->index + 1;
385 n = 1;
386 for (p = first->lru.prev; p != pages; p = p->prev) {
387 page = list_entry(p, struct page, lru);
388 if (page->index != index)
389 break;
390 index++;
391 n++;
392 }
393
Zhengyuan Liuee102582019-06-20 18:12:17 +0100394 req = kzalloc(struct_size(req, array, n), GFP_NOFS);
David Howells91b467e2017-01-05 10:38:35 +0000395 if (!req)
396 return -ENOMEM;
397
David Howellsf3ddee82018-04-06 14:17:25 +0100398 refcount_set(&req->usage, 1);
David Howellsa58823a2019-05-09 15:16:10 +0100399 req->vnode = vnode;
David Howellsc69bf472020-02-06 14:22:27 +0000400 req->key = key_get(afs_file_key(file));
David Howells91b467e2017-01-05 10:38:35 +0000401 req->page_done = afs_readpages_page_done;
402 req->pos = first->index;
403 req->pos <<= PAGE_SHIFT;
David Howellsf3ddee82018-04-06 14:17:25 +0100404 req->pages = req->array;
David Howells91b467e2017-01-05 10:38:35 +0000405
406 /* Transfer the pages to the request. We add them in until one fails
407 * to add to the LRU and then we stop (as that'll make a hole in the
408 * contiguous run.
409 *
410 * Note that it's possible for the file size to change whilst we're
411 * doing this, but we rely on the server returning less than we asked
412 * for if the file shrank. We also rely on this to deal with a partial
413 * page at the end of the file.
414 */
415 do {
Nikolay Borisovf86196e2019-01-03 15:29:02 -0800416 page = lru_to_page(pages);
David Howells91b467e2017-01-05 10:38:35 +0000417 list_del(&page->lru);
418 index = page->index;
419 if (add_to_page_cache_lru(page, mapping, index,
420 readahead_gfp_mask(mapping))) {
David Howells91b467e2017-01-05 10:38:35 +0000421 put_page(page);
422 break;
423 }
424
425 req->pages[req->nr_pages++] = page;
426 req->len += PAGE_SIZE;
427 } while (req->nr_pages < n);
428
429 if (req->nr_pages == 0) {
David Howellsc69bf472020-02-06 14:22:27 +0000430 afs_put_read(req);
David Howells91b467e2017-01-05 10:38:35 +0000431 return 0;
432 }
433
David Howellsc69bf472020-02-06 14:22:27 +0000434 ret = afs_fetch_data(vnode, req);
David Howells91b467e2017-01-05 10:38:35 +0000435 if (ret < 0)
436 goto error;
437
438 task_io_account_read(PAGE_SIZE * req->nr_pages);
439 afs_put_read(req);
440 return 0;
441
442error:
443 if (ret == -ENOENT) {
444 _debug("got NOENT from server"
445 " - marking file deleted and stale");
446 set_bit(AFS_VNODE_DELETED, &vnode->flags);
447 ret = -ESTALE;
448 }
449
450 for (i = 0; i < req->nr_pages; i++) {
451 page = req->pages[i];
452 if (page) {
David Howells91b467e2017-01-05 10:38:35 +0000453 SetPageError(page);
454 unlock_page(page);
455 }
456 }
457
458 afs_put_read(req);
459 return ret;
460}
461
462/*
David Howells9b3f26c2009-04-03 16:42:41 +0100463 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
David Howells9b3f26c2009-04-03 16:42:41 +0100465static int afs_readpages(struct file *file, struct address_space *mapping,
466 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
David Howells215804a2017-11-02 15:27:52 +0000468 struct key *key = afs_file_key(file);
David Howells9b3f26c2009-04-03 16:42:41 +0100469 struct afs_vnode *vnode;
470 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Al Virof6d335c2010-05-21 15:27:09 +0100472 _enter("{%d},{%lu},,%d",
473 key_serial(key), mapping->host->i_ino, nr_pages);
474
475 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
David Howells9b3f26c2009-04-03 16:42:41 +0100477 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000478 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100479 _leave(" = -ESTALE");
480 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
David Howells9b3f26c2009-04-03 16:42:41 +0100483 /* attempt to read as many of the pages as possible */
David Howells91b467e2017-01-05 10:38:35 +0000484 while (!list_empty(pages)) {
485 ret = afs_readpages_one(file, mapping, pages);
486 if (ret < 0)
487 break;
488 }
David Howells9b3f26c2009-04-03 16:42:41 +0100489
490 _leave(" = %d [netting]", ret);
491 return ret;
David Howellsec268152007-04-26 15:49:28 -0700492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
David Howellsf86726a2020-10-22 14:08:23 +0100495 * Adjust the dirty region of the page on truncation or full invalidation,
496 * getting rid of the markers altogether if the region is entirely invalidated.
497 */
498static void afs_invalidate_dirty(struct page *page, unsigned int offset,
499 unsigned int length)
500{
501 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
502 unsigned long priv;
503 unsigned int f, t, end = offset + length;
504
505 priv = page_private(page);
506
507 /* we clean up only if the entire page is being invalidated */
508 if (offset == 0 && length == thp_size(page))
509 goto full_invalidate;
510
511 /* If the page was dirtied by page_mkwrite(), the PTE stays writable
512 * and we don't get another notification to tell us to expand it
513 * again.
514 */
515 if (afs_is_page_dirty_mmapped(priv))
516 return;
517
518 /* We may need to shorten the dirty region */
David Howells67d78a62020-10-28 14:23:46 +0000519 f = afs_page_dirty_from(page, priv);
520 t = afs_page_dirty_to(page, priv);
David Howellsf86726a2020-10-22 14:08:23 +0100521
522 if (t <= offset || f >= end)
523 return; /* Doesn't overlap */
524
525 if (f < offset && t > end)
526 return; /* Splits the dirty region - just absorb it */
527
528 if (f >= offset && t <= end)
529 goto undirty;
530
531 if (f < offset)
532 t = offset;
533 else
534 f = end;
535 if (f == t)
536 goto undirty;
537
David Howells67d78a62020-10-28 14:23:46 +0000538 priv = afs_page_dirty(page, f, t);
David Howellsf86726a2020-10-22 14:08:23 +0100539 set_page_private(page, priv);
David Howells67d78a62020-10-28 14:23:46 +0000540 trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page);
David Howellsf86726a2020-10-22 14:08:23 +0100541 return;
542
543undirty:
David Howells67d78a62020-10-28 14:23:46 +0000544 trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page);
David Howellsf86726a2020-10-22 14:08:23 +0100545 clear_page_dirty_for_io(page);
546full_invalidate:
David Howells67d78a62020-10-28 14:23:46 +0000547 detach_page_private(page);
548 trace_afs_page_dirty(vnode, tracepoint_string("inval"), page);
David Howellsf86726a2020-10-22 14:08:23 +0100549}
550
551/*
David Howells9b3f26c2009-04-03 16:42:41 +0100552 * invalidate part or all of a page
553 * - release a page and clean up its private data if offset is 0 (indicating
554 * the entire page)
555 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400556static void afs_invalidatepage(struct page *page, unsigned int offset,
557 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100558{
Lukas Czernerd47992f2013-05-21 23:17:23 -0400559 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100560
561 BUG_ON(!PageLocked(page));
562
David Howellsf86726a2020-10-22 14:08:23 +0100563 if (PagePrivate(page))
564 afs_invalidate_dirty(page, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100565
566 _leave("");
567}
568
569/*
570 * release a page and clean up its private state if it's not busy
571 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
David Howells416351f2007-05-09 02:33:45 -0700573static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
David Howells416351f2007-05-09 02:33:45 -0700575 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
David Howells3b6492d2018-10-20 00:57:57 +0100577 _enter("{{%llx:%llu}[%lu],%lx},%x",
David Howells416351f2007-05-09 02:33:45 -0700578 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
579 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
David Howells9b3f26c2009-04-03 16:42:41 +0100581 /* deny if page is being written to the cache and the caller hasn't
582 * elected to wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (PagePrivate(page)) {
David Howells67d78a62020-10-28 14:23:46 +0000584 detach_page_private(page);
585 trace_afs_page_dirty(vnode, tracepoint_string("rel"), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
David Howells9b3f26c2009-04-03 16:42:41 +0100588 /* indicate that the page can be released */
589 _leave(" = T");
590 return 1;
David Howellsec268152007-04-26 15:49:28 -0700591}
David Howells1cf7a152017-11-02 15:27:52 +0000592
593/*
594 * Handle setting up a memory mapping on an AFS file.
595 */
596static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
597{
598 int ret;
599
600 ret = generic_file_mmap(file, vma);
601 if (ret == 0)
602 vma->vm_ops = &afs_vm_ops;
603 return ret;
604}