blob: c3a7bc1281f5d3ed0a9e1940262195f0ce9db4ce [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 Howells416351f2007-05-09 02:33:45 -070022static int afs_readpage(struct file *file, struct page *page);
Lukas Czernerd47992f2013-05-21 23:17:23 -040023static void afs_invalidatepage(struct page *page, unsigned int offset,
24 unsigned int length);
David Howells416351f2007-05-09 02:33:45 -070025static int afs_releasepage(struct page *page, gfp_t gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Howells9b3f26c2009-04-03 16:42:41 +010027static int afs_readpages(struct file *filp, struct address_space *mapping,
28 struct list_head *pages, unsigned nr_pages);
29
David Howells00d3b7a2007-04-26 15:57:07 -070030const struct file_operations afs_file_operations = {
31 .open = afs_open,
David Howells58fed942017-03-16 16:27:45 +000032 .flush = afs_flush,
David Howells00d3b7a2007-04-26 15:57:07 -070033 .release = afs_release,
34 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040035 .read_iter = generic_file_read_iter,
Al Viro50b55512014-04-03 14:13:46 -040036 .write_iter = afs_file_write,
David Howells00d3b7a2007-04-26 15:57:07 -070037 .mmap = generic_file_readonly_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020038 .splice_read = generic_file_splice_read,
David Howells31143d52007-05-09 02:33:46 -070039 .fsync = afs_fsync,
David Howellse8d6c552007-07-15 23:40:12 -070040 .lock = afs_lock,
41 .flock = afs_flock,
David Howells00d3b7a2007-04-26 15:57:07 -070042};
43
Arjan van de Ven754661f2007-02-12 00:55:38 -080044const struct inode_operations afs_file_inode_operations = {
David Howells416351f2007-05-09 02:33:45 -070045 .getattr = afs_getattr,
David Howells31143d52007-05-09 02:33:46 -070046 .setattr = afs_setattr,
David Howells00d3b7a2007-04-26 15:57:07 -070047 .permission = afs_permission,
David Howellsd3e3b7ea2017-07-06 15:50:27 +010048 .listxattr = afs_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070051const struct address_space_operations afs_fs_aops = {
David Howells416351f2007-05-09 02:33:45 -070052 .readpage = afs_readpage,
David Howells9b3f26c2009-04-03 16:42:41 +010053 .readpages = afs_readpages,
David Howells31143d52007-05-09 02:33:46 -070054 .set_page_dirty = afs_set_page_dirty,
55 .launder_page = afs_launder_page,
David Howells416351f2007-05-09 02:33:45 -070056 .releasepage = afs_releasepage,
57 .invalidatepage = afs_invalidatepage,
Nick Piggin15b46502008-10-15 22:04:32 -070058 .write_begin = afs_write_begin,
59 .write_end = afs_write_end,
David Howells31143d52007-05-09 02:33:46 -070060 .writepage = afs_writepage,
61 .writepages = afs_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
David Howells4343d002017-11-02 15:27:52 +000065 * Discard a pin on a writeback key.
66 */
67void afs_put_wb_key(struct afs_wb_key *wbk)
68{
69 if (refcount_dec_and_test(&wbk->usage)) {
70 key_put(wbk->key);
71 kfree(wbk);
72 }
73}
74
75/*
76 * Cache key for writeback.
77 */
78int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
79{
80 struct afs_wb_key *wbk, *p;
81
82 wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
83 if (!wbk)
84 return -ENOMEM;
85 refcount_set(&wbk->usage, 2);
86 wbk->key = af->key;
87
88 spin_lock(&vnode->wb_lock);
89 list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
90 if (p->key == wbk->key)
91 goto found;
92 }
93
94 key_get(wbk->key);
95 list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
96 spin_unlock(&vnode->wb_lock);
97 af->wb = wbk;
98 return 0;
99
100found:
101 refcount_inc(&p->usage);
102 spin_unlock(&vnode->wb_lock);
103 af->wb = p;
104 kfree(wbk);
105 return 0;
106}
107
108/*
David Howells00d3b7a2007-04-26 15:57:07 -0700109 * open an AFS file or directory and attach a key to it
110 */
111int afs_open(struct inode *inode, struct file *file)
112{
113 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000114 struct afs_file *af;
David Howells00d3b7a2007-04-26 15:57:07 -0700115 struct key *key;
David Howells260a9802007-04-26 15:59:35 -0700116 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700117
David Howells416351f2007-05-09 02:33:45 -0700118 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700119
120 key = afs_request_key(vnode->volume->cell);
121 if (IS_ERR(key)) {
David Howells215804a2017-11-02 15:27:52 +0000122 ret = PTR_ERR(key);
123 goto error;
124 }
125
126 af = kzalloc(sizeof(*af), GFP_KERNEL);
127 if (!af) {
128 ret = -ENOMEM;
129 goto error_key;
David Howells00d3b7a2007-04-26 15:57:07 -0700130 }
David Howells4343d002017-11-02 15:27:52 +0000131 af->key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700132
David Howells260a9802007-04-26 15:59:35 -0700133 ret = afs_validate(vnode, key);
David Howells215804a2017-11-02 15:27:52 +0000134 if (ret < 0)
135 goto error_af;
David Howells260a9802007-04-26 15:59:35 -0700136
David Howells4343d002017-11-02 15:27:52 +0000137 if (file->f_mode & FMODE_WRITE) {
138 ret = afs_cache_wb_key(vnode, af);
139 if (ret < 0)
140 goto error_af;
141 }
142
David Howells215804a2017-11-02 15:27:52 +0000143 file->private_data = af;
David Howells00d3b7a2007-04-26 15:57:07 -0700144 _leave(" = 0");
145 return 0;
David Howells215804a2017-11-02 15:27:52 +0000146
147error_af:
148 kfree(af);
149error_key:
150 key_put(key);
151error:
152 _leave(" = %d", ret);
153 return ret;
David Howells00d3b7a2007-04-26 15:57:07 -0700154}
155
156/*
157 * release an AFS file or directory and discard its key
158 */
159int afs_release(struct inode *inode, struct file *file)
160{
161 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells215804a2017-11-02 15:27:52 +0000162 struct afs_file *af = file->private_data;
David Howells00d3b7a2007-04-26 15:57:07 -0700163
David Howells416351f2007-05-09 02:33:45 -0700164 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700165
David Howells215804a2017-11-02 15:27:52 +0000166 file->private_data = NULL;
David Howells4343d002017-11-02 15:27:52 +0000167 if (af->wb)
168 afs_put_wb_key(af->wb);
David Howells215804a2017-11-02 15:27:52 +0000169 key_put(af->key);
170 kfree(af);
David Howells4343d002017-11-02 15:27:52 +0000171 afs_prune_wb_keys(vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700172 _leave(" = 0");
173 return 0;
174}
175
David Howells196ee9c2017-01-05 10:38:34 +0000176/*
177 * Dispose of a ref to a read record.
178 */
179void afs_put_read(struct afs_read *req)
180{
181 int i;
182
183 if (atomic_dec_and_test(&req->usage)) {
184 for (i = 0; i < req->nr_pages; i++)
185 if (req->pages[i])
186 put_page(req->pages[i]);
187 kfree(req);
188 }
189}
190
Matt Kraai6566abd2009-04-17 12:56:38 +0100191#ifdef CONFIG_AFS_FSCACHE
David Howells00d3b7a2007-04-26 15:57:07 -0700192/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * deal with notification that a page was read from the cache
194 */
David Howells9b3f26c2009-04-03 16:42:41 +0100195static void afs_file_readpage_read_complete(struct page *page,
196 void *data,
197 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
David Howells9b3f26c2009-04-03 16:42:41 +0100199 _enter("%p,%p,%d", page, data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
David Howells9b3f26c2009-04-03 16:42:41 +0100201 /* if the read completes with an error, we just unlock the page and let
202 * the VM reissue the readpage */
203 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 SetPageUptodate(page);
205 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700206}
Matt Kraai6566abd2009-04-17 12:56:38 +0100207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
David Howellsd2ddc772017-11-02 15:27:50 +0000210 * Fetch file data from the volume.
211 */
212int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *desc)
213{
214 struct afs_fs_cursor fc;
215 int ret;
216
217 _enter("%s{%x:%u.%u},%x,,,",
218 vnode->volume->name,
219 vnode->fid.vid,
220 vnode->fid.vnode,
221 vnode->fid.unique,
222 key_serial(key));
223
224 ret = -ERESTARTSYS;
225 if (afs_begin_vnode_operation(&fc, vnode, key)) {
226 while (afs_select_fileserver(&fc)) {
227 fc.cb_break = vnode->cb_break + vnode->cb_s_break;
228 afs_fs_fetch_data(&fc, desc);
229 }
230
231 afs_check_for_remote_deletion(&fc, fc.vnode);
232 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
233 ret = afs_end_vnode_operation(&fc);
234 }
235
236 _leave(" = %d", ret);
237 return ret;
238}
239
240/*
Al Virof6d335c2010-05-21 15:27:09 +0100241 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
Al Virof6d335c2010-05-21 15:27:09 +0100243int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Al Virof6d335c2010-05-21 15:27:09 +0100245 struct inode *inode = page->mapping->host;
246 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells196ee9c2017-01-05 10:38:34 +0000247 struct afs_read *req;
Al Virof6d335c2010-05-21 15:27:09 +0100248 struct key *key = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int ret;
250
David Howells00d3b7a2007-04-26 15:57:07 -0700251 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Matt Mackallcd7619d2005-05-01 08:59:01 -0700253 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700256 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto error;
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* is it cached? */
David Howells9b3f26c2009-04-03 16:42:41 +0100260#ifdef CONFIG_AFS_FSCACHE
261 ret = fscache_read_or_alloc_page(vnode->cache,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 page,
263 afs_file_readpage_read_complete,
264 NULL,
265 GFP_KERNEL);
266#else
267 ret = -ENOBUFS;
268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 switch (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* read BIO submitted (page in cache) */
271 case 0:
272 break;
273
David Howells9b3f26c2009-04-03 16:42:41 +0100274 /* page not yet cached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 case -ENODATA:
David Howells9b3f26c2009-04-03 16:42:41 +0100276 _debug("cache said ENODATA");
277 goto go_on;
278
279 /* page will not be cached */
280 case -ENOBUFS:
281 _debug("cache said ENOBUFS");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 default:
David Howells9b3f26c2009-04-03 16:42:41 +0100283 go_on:
David Howells196ee9c2017-01-05 10:38:34 +0000284 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
285 GFP_KERNEL);
286 if (!req)
287 goto enomem;
288
David Howells6db3ac32017-03-16 16:27:44 +0000289 /* We request a full page. If the page is a partial one at the
290 * end of the file, the server will return a short read and the
291 * unmarshalling code will clear the unfilled space.
292 */
David Howells196ee9c2017-01-05 10:38:34 +0000293 atomic_set(&req->usage, 1);
294 req->pos = (loff_t)page->index << PAGE_SHIFT;
David Howells6db3ac32017-03-16 16:27:44 +0000295 req->len = PAGE_SIZE;
David Howells196ee9c2017-01-05 10:38:34 +0000296 req->nr_pages = 1;
297 req->pages[0] = page;
298 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* read the contents of the file from the server into the
301 * page */
David Howellsd2ddc772017-11-02 15:27:50 +0000302 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +0000303 afs_put_read(req);
David Howellsdab17c12017-11-02 15:27:52 +0000304
305 if (ret >= 0 && S_ISDIR(inode->i_mode) &&
306 !afs_dir_check_page(inode, page))
307 ret = -EIO;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700310 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 _debug("got NOENT from server"
312 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700313 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ret = -ESTALE;
315 }
David Howells9b3f26c2009-04-03 16:42:41 +0100316
317#ifdef CONFIG_AFS_FSCACHE
318 fscache_uncache_page(vnode->cache, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100320 BUG_ON(PageFsCache(page));
David Howells68ae8492017-03-16 16:27:48 +0000321
322 if (ret == -EINTR ||
323 ret == -ENOMEM ||
324 ret == -ERESTARTSYS ||
325 ret == -EAGAIN)
326 goto error;
327 goto io_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 SetPageUptodate(page);
331
David Howells9b3f26c2009-04-03 16:42:41 +0100332 /* send the page to the cache */
333#ifdef CONFIG_AFS_FSCACHE
334 if (PageFsCache(page) &&
335 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
336 fscache_uncache_page(vnode->cache, page);
337 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100340 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 _leave(" = 0");
344 return 0;
345
David Howells68ae8492017-03-16 16:27:48 +0000346io_error:
347 SetPageError(page);
348 goto error;
David Howells196ee9c2017-01-05 10:38:34 +0000349enomem:
350 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -0700351error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 _leave(" = %d", ret);
354 return ret;
David Howellsec268152007-04-26 15:49:28 -0700355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
Al Virof6d335c2010-05-21 15:27:09 +0100358 * read page from file, directory or symlink, given a file to nominate the key
359 * to be used
360 */
361static int afs_readpage(struct file *file, struct page *page)
362{
363 struct key *key;
364 int ret;
365
366 if (file) {
David Howells215804a2017-11-02 15:27:52 +0000367 key = afs_file_key(file);
Al Virof6d335c2010-05-21 15:27:09 +0100368 ASSERT(key != NULL);
369 ret = afs_page_filler(key, page);
370 } else {
371 struct inode *inode = page->mapping->host;
David Howellsd2ddc772017-11-02 15:27:50 +0000372 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
Al Virof6d335c2010-05-21 15:27:09 +0100373 if (IS_ERR(key)) {
374 ret = PTR_ERR(key);
375 } else {
376 ret = afs_page_filler(key, page);
377 key_put(key);
378 }
379 }
380 return ret;
381}
382
383/*
David Howells91b467e2017-01-05 10:38:35 +0000384 * Make pages available as they're filled.
385 */
386static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req)
387{
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000388#ifdef CONFIG_AFS_FSCACHE
David Howells97e30432017-11-02 15:27:48 +0000389 struct afs_vnode *vnode = call->reply[0];
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000390#endif
David Howells91b467e2017-01-05 10:38:35 +0000391 struct page *page = req->pages[req->index];
392
393 req->pages[req->index] = NULL;
394 SetPageUptodate(page);
395
396 /* send the page to the cache */
397#ifdef CONFIG_AFS_FSCACHE
398 if (PageFsCache(page) &&
399 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
400 fscache_uncache_page(vnode->cache, page);
401 BUG_ON(PageFsCache(page));
402 }
403#endif
404 unlock_page(page);
405 put_page(page);
406}
407
408/*
409 * Read a contiguous set of pages.
410 */
411static int afs_readpages_one(struct file *file, struct address_space *mapping,
412 struct list_head *pages)
413{
414 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
415 struct afs_read *req;
416 struct list_head *p;
417 struct page *first, *page;
David Howells215804a2017-11-02 15:27:52 +0000418 struct key *key = afs_file_key(file);
David Howells91b467e2017-01-05 10:38:35 +0000419 pgoff_t index;
420 int ret, n, i;
421
422 /* Count the number of contiguous pages at the front of the list. Note
423 * that the list goes prev-wards rather than next-wards.
424 */
425 first = list_entry(pages->prev, struct page, lru);
426 index = first->index + 1;
427 n = 1;
428 for (p = first->lru.prev; p != pages; p = p->prev) {
429 page = list_entry(p, struct page, lru);
430 if (page->index != index)
431 break;
432 index++;
433 n++;
434 }
435
436 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
437 GFP_NOFS);
438 if (!req)
439 return -ENOMEM;
440
441 atomic_set(&req->usage, 1);
442 req->page_done = afs_readpages_page_done;
443 req->pos = first->index;
444 req->pos <<= PAGE_SHIFT;
445
446 /* Transfer the pages to the request. We add them in until one fails
447 * to add to the LRU and then we stop (as that'll make a hole in the
448 * contiguous run.
449 *
450 * Note that it's possible for the file size to change whilst we're
451 * doing this, but we rely on the server returning less than we asked
452 * for if the file shrank. We also rely on this to deal with a partial
453 * page at the end of the file.
454 */
455 do {
456 page = list_entry(pages->prev, struct page, lru);
457 list_del(&page->lru);
458 index = page->index;
459 if (add_to_page_cache_lru(page, mapping, index,
460 readahead_gfp_mask(mapping))) {
461#ifdef CONFIG_AFS_FSCACHE
462 fscache_uncache_page(vnode->cache, page);
463#endif
464 put_page(page);
465 break;
466 }
467
468 req->pages[req->nr_pages++] = page;
469 req->len += PAGE_SIZE;
470 } while (req->nr_pages < n);
471
472 if (req->nr_pages == 0) {
473 kfree(req);
474 return 0;
475 }
476
David Howellsd2ddc772017-11-02 15:27:50 +0000477 ret = afs_fetch_data(vnode, key, req);
David Howells91b467e2017-01-05 10:38:35 +0000478 if (ret < 0)
479 goto error;
480
481 task_io_account_read(PAGE_SIZE * req->nr_pages);
482 afs_put_read(req);
483 return 0;
484
485error:
486 if (ret == -ENOENT) {
487 _debug("got NOENT from server"
488 " - marking file deleted and stale");
489 set_bit(AFS_VNODE_DELETED, &vnode->flags);
490 ret = -ESTALE;
491 }
492
493 for (i = 0; i < req->nr_pages; i++) {
494 page = req->pages[i];
495 if (page) {
496#ifdef CONFIG_AFS_FSCACHE
497 fscache_uncache_page(vnode->cache, page);
498#endif
499 SetPageError(page);
500 unlock_page(page);
501 }
502 }
503
504 afs_put_read(req);
505 return ret;
506}
507
508/*
David Howells9b3f26c2009-04-03 16:42:41 +0100509 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
David Howells9b3f26c2009-04-03 16:42:41 +0100511static int afs_readpages(struct file *file, struct address_space *mapping,
512 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David Howells215804a2017-11-02 15:27:52 +0000514 struct key *key = afs_file_key(file);
David Howells9b3f26c2009-04-03 16:42:41 +0100515 struct afs_vnode *vnode;
516 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Al Virof6d335c2010-05-21 15:27:09 +0100518 _enter("{%d},{%lu},,%d",
519 key_serial(key), mapping->host->i_ino, nr_pages);
520
521 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
David Howells9b3f26c2009-04-03 16:42:41 +0100523 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000524 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100525 _leave(" = -ESTALE");
526 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
David Howells9b3f26c2009-04-03 16:42:41 +0100529 /* attempt to read as many of the pages as possible */
530#ifdef CONFIG_AFS_FSCACHE
531 ret = fscache_read_or_alloc_pages(vnode->cache,
532 mapping,
533 pages,
534 &nr_pages,
535 afs_file_readpage_read_complete,
536 NULL,
537 mapping_gfp_mask(mapping));
538#else
539 ret = -ENOBUFS;
540#endif
541
542 switch (ret) {
543 /* all pages are being read from the cache */
544 case 0:
545 BUG_ON(!list_empty(pages));
546 BUG_ON(nr_pages != 0);
547 _leave(" = 0 [reading all]");
548 return 0;
549
550 /* there were pages that couldn't be read from the cache */
551 case -ENODATA:
552 case -ENOBUFS:
553 break;
554
555 /* other error */
556 default:
557 _leave(" = %d", ret);
558 return ret;
559 }
560
David Howells91b467e2017-01-05 10:38:35 +0000561 while (!list_empty(pages)) {
562 ret = afs_readpages_one(file, mapping, pages);
563 if (ret < 0)
564 break;
565 }
David Howells9b3f26c2009-04-03 16:42:41 +0100566
567 _leave(" = %d [netting]", ret);
568 return ret;
David Howellsec268152007-04-26 15:49:28 -0700569}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/*
David Howells9b3f26c2009-04-03 16:42:41 +0100572 * invalidate part or all of a page
573 * - release a page and clean up its private data if offset is 0 (indicating
574 * the entire page)
575 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400576static void afs_invalidatepage(struct page *page, unsigned int offset,
577 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100578{
Lukas Czernerd47992f2013-05-21 23:17:23 -0400579 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100580
581 BUG_ON(!PageLocked(page));
582
583 /* we clean up only if the entire page is being invalidated */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300584 if (offset == 0 && length == PAGE_SIZE) {
David Howells9b3f26c2009-04-03 16:42:41 +0100585#ifdef CONFIG_AFS_FSCACHE
586 if (PageFsCache(page)) {
587 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
588 fscache_wait_on_page_write(vnode->cache, page);
589 fscache_uncache_page(vnode->cache, page);
David Howells9b3f26c2009-04-03 16:42:41 +0100590 }
591#endif
592
593 if (PagePrivate(page)) {
David Howells4343d002017-11-02 15:27:52 +0000594 set_page_private(page, 0);
595 ClearPagePrivate(page);
David Howells9b3f26c2009-04-03 16:42:41 +0100596 }
597 }
598
599 _leave("");
600}
601
602/*
603 * release a page and clean up its private state if it's not busy
604 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
David Howells416351f2007-05-09 02:33:45 -0700606static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
David Howells416351f2007-05-09 02:33:45 -0700608 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
David Howells416351f2007-05-09 02:33:45 -0700610 _enter("{{%x:%u}[%lu],%lx},%x",
611 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
612 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
David Howells9b3f26c2009-04-03 16:42:41 +0100614 /* deny if page is being written to the cache and the caller hasn't
615 * elected to wait */
616#ifdef CONFIG_AFS_FSCACHE
David Howells201a1542009-11-19 18:11:35 +0000617 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
618 _leave(" = F [cache busy]");
619 return 0;
David Howells9b3f26c2009-04-03 16:42:41 +0100620 }
621#endif
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (PagePrivate(page)) {
David Howells4343d002017-11-02 15:27:52 +0000624 set_page_private(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ClearPagePrivate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
David Howells9b3f26c2009-04-03 16:42:41 +0100628 /* indicate that the page can be released */
629 _leave(" = T");
630 return 1;
David Howellsec268152007-04-26 15:49:28 -0700631}