blob: 5786f68f87f143e4d3a8c786f27382f8113328f9 [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);
David Howells31143d52007-05-09 02:33:46 -070026static int afs_launder_page(struct page *page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David Howells9b3f26c2009-04-03 16:42:41 +010028static int afs_readpages(struct file *filp, struct address_space *mapping,
29 struct list_head *pages, unsigned nr_pages);
30
David Howells00d3b7a2007-04-26 15:57:07 -070031const struct file_operations afs_file_operations = {
32 .open = afs_open,
David Howells58fed942017-03-16 16:27:45 +000033 .flush = afs_flush,
David Howells00d3b7a2007-04-26 15:57:07 -070034 .release = afs_release,
35 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040036 .read_iter = generic_file_read_iter,
Al Viro50b55512014-04-03 14:13:46 -040037 .write_iter = afs_file_write,
David Howells00d3b7a2007-04-26 15:57:07 -070038 .mmap = generic_file_readonly_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
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
David Howells00d3b7a2007-04-26 15:57:07 -070066 * open an AFS file or directory and attach a key to it
67 */
68int afs_open(struct inode *inode, struct file *file)
69{
70 struct afs_vnode *vnode = AFS_FS_I(inode);
71 struct key *key;
David Howells260a9802007-04-26 15:59:35 -070072 int ret;
David Howells00d3b7a2007-04-26 15:57:07 -070073
David Howells416351f2007-05-09 02:33:45 -070074 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -070075
76 key = afs_request_key(vnode->volume->cell);
77 if (IS_ERR(key)) {
78 _leave(" = %ld [key]", PTR_ERR(key));
79 return PTR_ERR(key);
80 }
81
David Howells260a9802007-04-26 15:59:35 -070082 ret = afs_validate(vnode, key);
83 if (ret < 0) {
84 _leave(" = %d [val]", ret);
85 return ret;
86 }
87
David Howells00d3b7a2007-04-26 15:57:07 -070088 file->private_data = key;
89 _leave(" = 0");
90 return 0;
91}
92
93/*
94 * release an AFS file or directory and discard its key
95 */
96int afs_release(struct inode *inode, struct file *file)
97{
98 struct afs_vnode *vnode = AFS_FS_I(inode);
99
David Howells416351f2007-05-09 02:33:45 -0700100 _enter("{%x:%u},", vnode->fid.vid, vnode->fid.vnode);
David Howells00d3b7a2007-04-26 15:57:07 -0700101
102 key_put(file->private_data);
103 _leave(" = 0");
104 return 0;
105}
106
David Howells196ee9c2017-01-05 10:38:34 +0000107/*
108 * Dispose of a ref to a read record.
109 */
110void afs_put_read(struct afs_read *req)
111{
112 int i;
113
114 if (atomic_dec_and_test(&req->usage)) {
115 for (i = 0; i < req->nr_pages; i++)
116 if (req->pages[i])
117 put_page(req->pages[i]);
118 kfree(req);
119 }
120}
121
Matt Kraai6566abd2009-04-17 12:56:38 +0100122#ifdef CONFIG_AFS_FSCACHE
David Howells00d3b7a2007-04-26 15:57:07 -0700123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * deal with notification that a page was read from the cache
125 */
David Howells9b3f26c2009-04-03 16:42:41 +0100126static void afs_file_readpage_read_complete(struct page *page,
127 void *data,
128 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
David Howells9b3f26c2009-04-03 16:42:41 +0100130 _enter("%p,%p,%d", page, data, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Howells9b3f26c2009-04-03 16:42:41 +0100132 /* if the read completes with an error, we just unlock the page and let
133 * the VM reissue the readpage */
134 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 SetPageUptodate(page);
136 unlock_page(page);
David Howellsec268152007-04-26 15:49:28 -0700137}
Matt Kraai6566abd2009-04-17 12:56:38 +0100138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
David Howellsd2ddc772017-11-02 15:27:50 +0000141 * Fetch file data from the volume.
142 */
143int afs_fetch_data(struct afs_vnode *vnode, struct key *key, struct afs_read *desc)
144{
145 struct afs_fs_cursor fc;
146 int ret;
147
148 _enter("%s{%x:%u.%u},%x,,,",
149 vnode->volume->name,
150 vnode->fid.vid,
151 vnode->fid.vnode,
152 vnode->fid.unique,
153 key_serial(key));
154
155 ret = -ERESTARTSYS;
156 if (afs_begin_vnode_operation(&fc, vnode, key)) {
157 while (afs_select_fileserver(&fc)) {
158 fc.cb_break = vnode->cb_break + vnode->cb_s_break;
159 afs_fs_fetch_data(&fc, desc);
160 }
161
162 afs_check_for_remote_deletion(&fc, fc.vnode);
163 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
164 ret = afs_end_vnode_operation(&fc);
165 }
166
167 _leave(" = %d", ret);
168 return ret;
169}
170
171/*
Al Virof6d335c2010-05-21 15:27:09 +0100172 * read page from file, directory or symlink, given a key to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Al Virof6d335c2010-05-21 15:27:09 +0100174int afs_page_filler(void *data, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Al Virof6d335c2010-05-21 15:27:09 +0100176 struct inode *inode = page->mapping->host;
177 struct afs_vnode *vnode = AFS_FS_I(inode);
David Howells196ee9c2017-01-05 10:38:34 +0000178 struct afs_read *req;
Al Virof6d335c2010-05-21 15:27:09 +0100179 struct key *key = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int ret;
181
David Howells00d3b7a2007-04-26 15:57:07 -0700182 _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Matt Mackallcd7619d2005-05-01 08:59:01 -0700184 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 ret = -ESTALE;
David Howells08e0e7c2007-04-26 15:55:03 -0700187 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto error;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* is it cached? */
David Howells9b3f26c2009-04-03 16:42:41 +0100191#ifdef CONFIG_AFS_FSCACHE
192 ret = fscache_read_or_alloc_page(vnode->cache,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 page,
194 afs_file_readpage_read_complete,
195 NULL,
196 GFP_KERNEL);
197#else
198 ret = -ENOBUFS;
199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 switch (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* read BIO submitted (page in cache) */
202 case 0:
203 break;
204
David Howells9b3f26c2009-04-03 16:42:41 +0100205 /* page not yet cached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 case -ENODATA:
David Howells9b3f26c2009-04-03 16:42:41 +0100207 _debug("cache said ENODATA");
208 goto go_on;
209
210 /* page will not be cached */
211 case -ENOBUFS:
212 _debug("cache said ENOBUFS");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 default:
David Howells9b3f26c2009-04-03 16:42:41 +0100214 go_on:
David Howells196ee9c2017-01-05 10:38:34 +0000215 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
216 GFP_KERNEL);
217 if (!req)
218 goto enomem;
219
David Howells6db3ac32017-03-16 16:27:44 +0000220 /* We request a full page. If the page is a partial one at the
221 * end of the file, the server will return a short read and the
222 * unmarshalling code will clear the unfilled space.
223 */
David Howells196ee9c2017-01-05 10:38:34 +0000224 atomic_set(&req->usage, 1);
225 req->pos = (loff_t)page->index << PAGE_SHIFT;
David Howells6db3ac32017-03-16 16:27:44 +0000226 req->len = PAGE_SIZE;
David Howells196ee9c2017-01-05 10:38:34 +0000227 req->nr_pages = 1;
228 req->pages[0] = page;
229 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* read the contents of the file from the server into the
232 * page */
David Howellsd2ddc772017-11-02 15:27:50 +0000233 ret = afs_fetch_data(vnode, key, req);
David Howells196ee9c2017-01-05 10:38:34 +0000234 afs_put_read(req);
David Howellsdab17c12017-11-02 15:27:52 +0000235
236 if (ret >= 0 && S_ISDIR(inode->i_mode) &&
237 !afs_dir_check_page(inode, page))
238 ret = -EIO;
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700241 if (ret == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 _debug("got NOENT from server"
243 " - marking file deleted and stale");
David Howells08e0e7c2007-04-26 15:55:03 -0700244 set_bit(AFS_VNODE_DELETED, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 ret = -ESTALE;
246 }
David Howells9b3f26c2009-04-03 16:42:41 +0100247
248#ifdef CONFIG_AFS_FSCACHE
249 fscache_uncache_page(vnode->cache, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100251 BUG_ON(PageFsCache(page));
David Howells68ae8492017-03-16 16:27:48 +0000252
253 if (ret == -EINTR ||
254 ret == -ENOMEM ||
255 ret == -ERESTARTSYS ||
256 ret == -EAGAIN)
257 goto error;
258 goto io_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
261 SetPageUptodate(page);
262
David Howells9b3f26c2009-04-03 16:42:41 +0100263 /* send the page to the cache */
264#ifdef CONFIG_AFS_FSCACHE
265 if (PageFsCache(page) &&
266 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
267 fscache_uncache_page(vnode->cache, page);
268 BUG_ON(PageFsCache(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#endif
David Howells9b3f26c2009-04-03 16:42:41 +0100271 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 _leave(" = 0");
275 return 0;
276
David Howells68ae8492017-03-16 16:27:48 +0000277io_error:
278 SetPageError(page);
279 goto error;
David Howells196ee9c2017-01-05 10:38:34 +0000280enomem:
281 ret = -ENOMEM;
David Howells08e0e7c2007-04-26 15:55:03 -0700282error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 _leave(" = %d", ret);
285 return ret;
David Howellsec268152007-04-26 15:49:28 -0700286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
Al Virof6d335c2010-05-21 15:27:09 +0100289 * read page from file, directory or symlink, given a file to nominate the key
290 * to be used
291 */
292static int afs_readpage(struct file *file, struct page *page)
293{
294 struct key *key;
295 int ret;
296
297 if (file) {
298 key = file->private_data;
299 ASSERT(key != NULL);
300 ret = afs_page_filler(key, page);
301 } else {
302 struct inode *inode = page->mapping->host;
David Howellsd2ddc772017-11-02 15:27:50 +0000303 key = afs_request_key(AFS_FS_S(inode->i_sb)->cell);
Al Virof6d335c2010-05-21 15:27:09 +0100304 if (IS_ERR(key)) {
305 ret = PTR_ERR(key);
306 } else {
307 ret = afs_page_filler(key, page);
308 key_put(key);
309 }
310 }
311 return ret;
312}
313
314/*
David Howells91b467e2017-01-05 10:38:35 +0000315 * Make pages available as they're filled.
316 */
317static void afs_readpages_page_done(struct afs_call *call, struct afs_read *req)
318{
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000319#ifdef CONFIG_AFS_FSCACHE
David Howells97e30432017-11-02 15:27:48 +0000320 struct afs_vnode *vnode = call->reply[0];
Arnd Bergmann51c89e62017-01-13 14:46:19 +0000321#endif
David Howells91b467e2017-01-05 10:38:35 +0000322 struct page *page = req->pages[req->index];
323
324 req->pages[req->index] = NULL;
325 SetPageUptodate(page);
326
327 /* send the page to the cache */
328#ifdef CONFIG_AFS_FSCACHE
329 if (PageFsCache(page) &&
330 fscache_write_page(vnode->cache, page, GFP_KERNEL) != 0) {
331 fscache_uncache_page(vnode->cache, page);
332 BUG_ON(PageFsCache(page));
333 }
334#endif
335 unlock_page(page);
336 put_page(page);
337}
338
339/*
340 * Read a contiguous set of pages.
341 */
342static int afs_readpages_one(struct file *file, struct address_space *mapping,
343 struct list_head *pages)
344{
345 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
346 struct afs_read *req;
347 struct list_head *p;
348 struct page *first, *page;
349 struct key *key = file->private_data;
350 pgoff_t index;
351 int ret, n, i;
352
353 /* Count the number of contiguous pages at the front of the list. Note
354 * that the list goes prev-wards rather than next-wards.
355 */
356 first = list_entry(pages->prev, struct page, lru);
357 index = first->index + 1;
358 n = 1;
359 for (p = first->lru.prev; p != pages; p = p->prev) {
360 page = list_entry(p, struct page, lru);
361 if (page->index != index)
362 break;
363 index++;
364 n++;
365 }
366
367 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
368 GFP_NOFS);
369 if (!req)
370 return -ENOMEM;
371
372 atomic_set(&req->usage, 1);
373 req->page_done = afs_readpages_page_done;
374 req->pos = first->index;
375 req->pos <<= PAGE_SHIFT;
376
377 /* Transfer the pages to the request. We add them in until one fails
378 * to add to the LRU and then we stop (as that'll make a hole in the
379 * contiguous run.
380 *
381 * Note that it's possible for the file size to change whilst we're
382 * doing this, but we rely on the server returning less than we asked
383 * for if the file shrank. We also rely on this to deal with a partial
384 * page at the end of the file.
385 */
386 do {
387 page = list_entry(pages->prev, struct page, lru);
388 list_del(&page->lru);
389 index = page->index;
390 if (add_to_page_cache_lru(page, mapping, index,
391 readahead_gfp_mask(mapping))) {
392#ifdef CONFIG_AFS_FSCACHE
393 fscache_uncache_page(vnode->cache, page);
394#endif
395 put_page(page);
396 break;
397 }
398
399 req->pages[req->nr_pages++] = page;
400 req->len += PAGE_SIZE;
401 } while (req->nr_pages < n);
402
403 if (req->nr_pages == 0) {
404 kfree(req);
405 return 0;
406 }
407
David Howellsd2ddc772017-11-02 15:27:50 +0000408 ret = afs_fetch_data(vnode, key, req);
David Howells91b467e2017-01-05 10:38:35 +0000409 if (ret < 0)
410 goto error;
411
412 task_io_account_read(PAGE_SIZE * req->nr_pages);
413 afs_put_read(req);
414 return 0;
415
416error:
417 if (ret == -ENOENT) {
418 _debug("got NOENT from server"
419 " - marking file deleted and stale");
420 set_bit(AFS_VNODE_DELETED, &vnode->flags);
421 ret = -ESTALE;
422 }
423
424 for (i = 0; i < req->nr_pages; i++) {
425 page = req->pages[i];
426 if (page) {
427#ifdef CONFIG_AFS_FSCACHE
428 fscache_uncache_page(vnode->cache, page);
429#endif
430 SetPageError(page);
431 unlock_page(page);
432 }
433 }
434
435 afs_put_read(req);
436 return ret;
437}
438
439/*
David Howells9b3f26c2009-04-03 16:42:41 +0100440 * read a set of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
David Howells9b3f26c2009-04-03 16:42:41 +0100442static int afs_readpages(struct file *file, struct address_space *mapping,
443 struct list_head *pages, unsigned nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Al Virof6d335c2010-05-21 15:27:09 +0100445 struct key *key = file->private_data;
David Howells9b3f26c2009-04-03 16:42:41 +0100446 struct afs_vnode *vnode;
447 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Al Virof6d335c2010-05-21 15:27:09 +0100449 _enter("{%d},{%lu},,%d",
450 key_serial(key), mapping->host->i_ino, nr_pages);
451
452 ASSERT(key != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
David Howells9b3f26c2009-04-03 16:42:41 +0100454 vnode = AFS_FS_I(mapping->host);
Dan Carpenterad2a8e62012-03-20 16:58:06 +0000455 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100456 _leave(" = -ESTALE");
457 return -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
David Howells9b3f26c2009-04-03 16:42:41 +0100460 /* attempt to read as many of the pages as possible */
461#ifdef CONFIG_AFS_FSCACHE
462 ret = fscache_read_or_alloc_pages(vnode->cache,
463 mapping,
464 pages,
465 &nr_pages,
466 afs_file_readpage_read_complete,
467 NULL,
468 mapping_gfp_mask(mapping));
469#else
470 ret = -ENOBUFS;
471#endif
472
473 switch (ret) {
474 /* all pages are being read from the cache */
475 case 0:
476 BUG_ON(!list_empty(pages));
477 BUG_ON(nr_pages != 0);
478 _leave(" = 0 [reading all]");
479 return 0;
480
481 /* there were pages that couldn't be read from the cache */
482 case -ENODATA:
483 case -ENOBUFS:
484 break;
485
486 /* other error */
487 default:
488 _leave(" = %d", ret);
489 return ret;
490 }
491
David Howells91b467e2017-01-05 10:38:35 +0000492 while (!list_empty(pages)) {
493 ret = afs_readpages_one(file, mapping, pages);
494 if (ret < 0)
495 break;
496 }
David Howells9b3f26c2009-04-03 16:42:41 +0100497
498 _leave(" = %d [netting]", ret);
499 return ret;
David Howellsec268152007-04-26 15:49:28 -0700500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/*
David Howells31143d52007-05-09 02:33:46 -0700503 * write back a dirty page
504 */
505static int afs_launder_page(struct page *page)
506{
507 _enter("{%lu}", page->index);
508
509 return 0;
510}
511
512/*
David Howells9b3f26c2009-04-03 16:42:41 +0100513 * invalidate part or all of a page
514 * - release a page and clean up its private data if offset is 0 (indicating
515 * the entire page)
516 */
Lukas Czernerd47992f2013-05-21 23:17:23 -0400517static void afs_invalidatepage(struct page *page, unsigned int offset,
518 unsigned int length)
David Howells9b3f26c2009-04-03 16:42:41 +0100519{
520 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
521
Lukas Czernerd47992f2013-05-21 23:17:23 -0400522 _enter("{%lu},%u,%u", page->index, offset, length);
David Howells9b3f26c2009-04-03 16:42:41 +0100523
524 BUG_ON(!PageLocked(page));
525
526 /* we clean up only if the entire page is being invalidated */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300527 if (offset == 0 && length == PAGE_SIZE) {
David Howells9b3f26c2009-04-03 16:42:41 +0100528#ifdef CONFIG_AFS_FSCACHE
529 if (PageFsCache(page)) {
530 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
531 fscache_wait_on_page_write(vnode->cache, page);
532 fscache_uncache_page(vnode->cache, page);
David Howells9b3f26c2009-04-03 16:42:41 +0100533 }
534#endif
535
536 if (PagePrivate(page)) {
537 if (wb && !PageWriteback(page)) {
538 set_page_private(page, 0);
539 afs_put_writeback(wb);
540 }
541
542 if (!page_private(page))
543 ClearPagePrivate(page);
544 }
545 }
546
547 _leave("");
548}
549
550/*
551 * release a page and clean up its private state if it's not busy
552 * - return true if the page can now be released, false if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
David Howells416351f2007-05-09 02:33:45 -0700554static int afs_releasepage(struct page *page, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
David Howells9b3f26c2009-04-03 16:42:41 +0100556 struct afs_writeback *wb = (struct afs_writeback *) page_private(page);
David Howells416351f2007-05-09 02:33:45 -0700557 struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
David Howells416351f2007-05-09 02:33:45 -0700559 _enter("{{%x:%u}[%lu],%lx},%x",
560 vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
561 gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
David Howells9b3f26c2009-04-03 16:42:41 +0100563 /* deny if page is being written to the cache and the caller hasn't
564 * elected to wait */
565#ifdef CONFIG_AFS_FSCACHE
David Howells201a1542009-11-19 18:11:35 +0000566 if (!fscache_maybe_release_page(vnode->cache, page, gfp_flags)) {
567 _leave(" = F [cache busy]");
568 return 0;
David Howells9b3f26c2009-04-03 16:42:41 +0100569 }
570#endif
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (PagePrivate(page)) {
David Howells9b3f26c2009-04-03 16:42:41 +0100573 if (wb) {
574 set_page_private(page, 0);
575 afs_put_writeback(wb);
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ClearPagePrivate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
David Howells9b3f26c2009-04-03 16:42:41 +0100580 /* indicate that the page can be released */
581 _leave(" = T");
582 return 1;
David Howellsec268152007-04-26 15:49:28 -0700583}