12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 208e0e7c8SDavid Howells /* AFS filesystem file handling 31da177e4SLinus Torvalds * 408e0e7c8SDavid Howells * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. 51da177e4SLinus Torvalds * Written by David Howells (dhowells@redhat.com) 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/kernel.h> 91da177e4SLinus Torvalds #include <linux/module.h> 101da177e4SLinus Torvalds #include <linux/init.h> 111da177e4SLinus Torvalds #include <linux/fs.h> 121da177e4SLinus Torvalds #include <linux/pagemap.h> 1331143d5dSDavid Howells #include <linux/writeback.h> 145a0e3ad6STejun Heo #include <linux/gfp.h> 1591b467e0SDavid Howells #include <linux/task_io_accounting_ops.h> 16f86196eaSNikolay Borisov #include <linux/mm.h> 171da177e4SLinus Torvalds #include "internal.h" 181da177e4SLinus Torvalds 191cf7a151SDavid Howells static int afs_file_mmap(struct file *file, struct vm_area_struct *vma); 20416351f2SDavid Howells static int afs_readpage(struct file *file, struct page *page); 21d47992f8SLukas Czerner static void afs_invalidatepage(struct page *page, unsigned int offset, 22d47992f8SLukas Czerner unsigned int length); 23416351f2SDavid Howells static int afs_releasepage(struct page *page, gfp_t gfp_flags); 241da177e4SLinus Torvalds 259b3f26c9SDavid Howells static int afs_readpages(struct file *filp, struct address_space *mapping, 269b3f26c9SDavid Howells struct list_head *pages, unsigned nr_pages); 279b3f26c9SDavid Howells 2800d3b7a4SDavid Howells const struct file_operations afs_file_operations = { 2900d3b7a4SDavid Howells .open = afs_open, 3000d3b7a4SDavid Howells .release = afs_release, 3100d3b7a4SDavid Howells .llseek = generic_file_llseek, 32aad4f8bbSAl Viro .read_iter = generic_file_read_iter, 3350b5551dSAl Viro .write_iter = afs_file_write, 341cf7a151SDavid Howells .mmap = afs_file_mmap, 355ffc4ef4SJens Axboe .splice_read = generic_file_splice_read, 3606a17bbeSDavid Howells .splice_write = iter_file_splice_write, 3731143d5dSDavid Howells .fsync = afs_fsync, 38e8d6c554SDavid Howells .lock = afs_lock, 39e8d6c554SDavid Howells .flock = afs_flock, 4000d3b7a4SDavid Howells }; 4100d3b7a4SDavid Howells 42754661f1SArjan van de Ven const struct inode_operations afs_file_inode_operations = { 43416351f2SDavid Howells .getattr = afs_getattr, 4431143d5dSDavid Howells .setattr = afs_setattr, 4500d3b7a4SDavid Howells .permission = afs_permission, 461da177e4SLinus Torvalds }; 471da177e4SLinus Torvalds 48f5e54d6eSChristoph Hellwig const struct address_space_operations afs_fs_aops = { 49416351f2SDavid Howells .readpage = afs_readpage, 509b3f26c9SDavid Howells .readpages = afs_readpages, 5131143d5dSDavid Howells .set_page_dirty = afs_set_page_dirty, 5231143d5dSDavid Howells .launder_page = afs_launder_page, 53416351f2SDavid Howells .releasepage = afs_releasepage, 54416351f2SDavid Howells .invalidatepage = afs_invalidatepage, 5515b4650eSNick Piggin .write_begin = afs_write_begin, 5615b4650eSNick Piggin .write_end = afs_write_end, 5731143d5dSDavid Howells .writepage = afs_writepage, 5831143d5dSDavid Howells .writepages = afs_writepages, 591da177e4SLinus Torvalds }; 601da177e4SLinus Torvalds 611cf7a151SDavid Howells static const struct vm_operations_struct afs_vm_ops = { 621cf7a151SDavid Howells .fault = filemap_fault, 631cf7a151SDavid Howells .map_pages = filemap_map_pages, 641cf7a151SDavid Howells .page_mkwrite = afs_page_mkwrite, 651cf7a151SDavid Howells }; 661cf7a151SDavid Howells 671da177e4SLinus Torvalds /* 684343d008SDavid Howells * Discard a pin on a writeback key. 694343d008SDavid Howells */ 704343d008SDavid Howells void afs_put_wb_key(struct afs_wb_key *wbk) 714343d008SDavid Howells { 72e49c7b2fSDavid Howells if (wbk && refcount_dec_and_test(&wbk->usage)) { 734343d008SDavid Howells key_put(wbk->key); 744343d008SDavid Howells kfree(wbk); 754343d008SDavid Howells } 764343d008SDavid Howells } 774343d008SDavid Howells 784343d008SDavid Howells /* 794343d008SDavid Howells * Cache key for writeback. 804343d008SDavid Howells */ 814343d008SDavid Howells int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) 824343d008SDavid Howells { 834343d008SDavid Howells struct afs_wb_key *wbk, *p; 844343d008SDavid Howells 854343d008SDavid Howells wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); 864343d008SDavid Howells if (!wbk) 874343d008SDavid Howells return -ENOMEM; 884343d008SDavid Howells refcount_set(&wbk->usage, 2); 894343d008SDavid Howells wbk->key = af->key; 904343d008SDavid Howells 914343d008SDavid Howells spin_lock(&vnode->wb_lock); 924343d008SDavid Howells list_for_each_entry(p, &vnode->wb_keys, vnode_link) { 934343d008SDavid Howells if (p->key == wbk->key) 944343d008SDavid Howells goto found; 954343d008SDavid Howells } 964343d008SDavid Howells 974343d008SDavid Howells key_get(wbk->key); 984343d008SDavid Howells list_add_tail(&wbk->vnode_link, &vnode->wb_keys); 994343d008SDavid Howells spin_unlock(&vnode->wb_lock); 1004343d008SDavid Howells af->wb = wbk; 1014343d008SDavid Howells return 0; 1024343d008SDavid Howells 1034343d008SDavid Howells found: 1044343d008SDavid Howells refcount_inc(&p->usage); 1054343d008SDavid Howells spin_unlock(&vnode->wb_lock); 1064343d008SDavid Howells af->wb = p; 1074343d008SDavid Howells kfree(wbk); 1084343d008SDavid Howells return 0; 1094343d008SDavid Howells } 1104343d008SDavid Howells 1114343d008SDavid Howells /* 11200d3b7a4SDavid Howells * open an AFS file or directory and attach a key to it 11300d3b7a4SDavid Howells */ 11400d3b7a4SDavid Howells int afs_open(struct inode *inode, struct file *file) 11500d3b7a4SDavid Howells { 11600d3b7a4SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 117215804a9SDavid Howells struct afs_file *af; 11800d3b7a4SDavid Howells struct key *key; 119260a9803SDavid Howells int ret; 12000d3b7a4SDavid Howells 1213b6492dfSDavid Howells _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 12200d3b7a4SDavid Howells 12300d3b7a4SDavid Howells key = afs_request_key(vnode->volume->cell); 12400d3b7a4SDavid Howells if (IS_ERR(key)) { 125215804a9SDavid Howells ret = PTR_ERR(key); 126215804a9SDavid Howells goto error; 127215804a9SDavid Howells } 128215804a9SDavid Howells 129215804a9SDavid Howells af = kzalloc(sizeof(*af), GFP_KERNEL); 130215804a9SDavid Howells if (!af) { 131215804a9SDavid Howells ret = -ENOMEM; 132215804a9SDavid Howells goto error_key; 13300d3b7a4SDavid Howells } 1344343d008SDavid Howells af->key = key; 13500d3b7a4SDavid Howells 136260a9803SDavid Howells ret = afs_validate(vnode, key); 137215804a9SDavid Howells if (ret < 0) 138215804a9SDavid Howells goto error_af; 139260a9803SDavid Howells 1404343d008SDavid Howells if (file->f_mode & FMODE_WRITE) { 1414343d008SDavid Howells ret = afs_cache_wb_key(vnode, af); 1424343d008SDavid Howells if (ret < 0) 1434343d008SDavid Howells goto error_af; 1444343d008SDavid Howells } 1454343d008SDavid Howells 1465a813276SDavid Howells if (file->f_flags & O_TRUNC) 1475a813276SDavid Howells set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 1485a813276SDavid Howells 149215804a9SDavid Howells file->private_data = af; 15000d3b7a4SDavid Howells _leave(" = 0"); 15100d3b7a4SDavid Howells return 0; 152215804a9SDavid Howells 153215804a9SDavid Howells error_af: 154215804a9SDavid Howells kfree(af); 155215804a9SDavid Howells error_key: 156215804a9SDavid Howells key_put(key); 157215804a9SDavid Howells error: 158215804a9SDavid Howells _leave(" = %d", ret); 159215804a9SDavid Howells return ret; 16000d3b7a4SDavid Howells } 16100d3b7a4SDavid Howells 16200d3b7a4SDavid Howells /* 16300d3b7a4SDavid Howells * release an AFS file or directory and discard its key 16400d3b7a4SDavid Howells */ 16500d3b7a4SDavid Howells int afs_release(struct inode *inode, struct file *file) 16600d3b7a4SDavid Howells { 16700d3b7a4SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 168215804a9SDavid Howells struct afs_file *af = file->private_data; 169a1b879eeSDavid Howells int ret = 0; 17000d3b7a4SDavid Howells 1713b6492dfSDavid Howells _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 17200d3b7a4SDavid Howells 1735a813276SDavid Howells if ((file->f_mode & FMODE_WRITE)) 174a1b879eeSDavid Howells ret = vfs_fsync(file, 0); 1755a813276SDavid Howells 176215804a9SDavid Howells file->private_data = NULL; 1774343d008SDavid Howells if (af->wb) 1784343d008SDavid Howells afs_put_wb_key(af->wb); 179215804a9SDavid Howells key_put(af->key); 180215804a9SDavid Howells kfree(af); 1814343d008SDavid Howells afs_prune_wb_keys(vnode); 182a1b879eeSDavid Howells _leave(" = %d", ret); 183a1b879eeSDavid Howells return ret; 18400d3b7a4SDavid Howells } 18500d3b7a4SDavid Howells 186196ee9cdSDavid Howells /* 187*c4508464SDavid Howells * Handle completion of a read operation. 188*c4508464SDavid Howells */ 189*c4508464SDavid Howells static void afs_file_read_done(struct afs_read *req) 190*c4508464SDavid Howells { 191*c4508464SDavid Howells struct afs_vnode *vnode = req->vnode; 192*c4508464SDavid Howells struct page *page; 193*c4508464SDavid Howells pgoff_t index = req->pos >> PAGE_SHIFT; 194*c4508464SDavid Howells pgoff_t last = index + req->nr_pages - 1; 195*c4508464SDavid Howells 196*c4508464SDavid Howells XA_STATE(xas, &vnode->vfs_inode.i_mapping->i_pages, index); 197*c4508464SDavid Howells 198*c4508464SDavid Howells if (iov_iter_count(req->iter) > 0) { 199*c4508464SDavid Howells /* The read was short - clear the excess buffer. */ 200*c4508464SDavid Howells _debug("afterclear %zx %zx %llx/%llx", 201*c4508464SDavid Howells req->iter->iov_offset, 202*c4508464SDavid Howells iov_iter_count(req->iter), 203*c4508464SDavid Howells req->actual_len, req->len); 204*c4508464SDavid Howells iov_iter_zero(iov_iter_count(req->iter), req->iter); 205*c4508464SDavid Howells } 206*c4508464SDavid Howells 207*c4508464SDavid Howells rcu_read_lock(); 208*c4508464SDavid Howells xas_for_each(&xas, page, last) { 209*c4508464SDavid Howells page_endio(page, false, 0); 210*c4508464SDavid Howells put_page(page); 211*c4508464SDavid Howells } 212*c4508464SDavid Howells rcu_read_unlock(); 213*c4508464SDavid Howells 214*c4508464SDavid Howells task_io_account_read(req->len); 215*c4508464SDavid Howells req->cleanup = NULL; 216*c4508464SDavid Howells } 217*c4508464SDavid Howells 218*c4508464SDavid Howells /* 219*c4508464SDavid Howells * Dispose of our locks and refs on the pages if the read failed. 220*c4508464SDavid Howells */ 221*c4508464SDavid Howells static void afs_file_read_cleanup(struct afs_read *req) 222*c4508464SDavid Howells { 223*c4508464SDavid Howells struct page *page; 224*c4508464SDavid Howells pgoff_t index = req->pos >> PAGE_SHIFT; 225*c4508464SDavid Howells pgoff_t last = index + req->nr_pages - 1; 226*c4508464SDavid Howells 227*c4508464SDavid Howells if (req->iter) { 228*c4508464SDavid Howells XA_STATE(xas, &req->vnode->vfs_inode.i_mapping->i_pages, index); 229*c4508464SDavid Howells 230*c4508464SDavid Howells _enter("%lu,%u,%zu", index, req->nr_pages, iov_iter_count(req->iter)); 231*c4508464SDavid Howells 232*c4508464SDavid Howells rcu_read_lock(); 233*c4508464SDavid Howells xas_for_each(&xas, page, last) { 234*c4508464SDavid Howells BUG_ON(xa_is_value(page)); 235*c4508464SDavid Howells BUG_ON(PageCompound(page)); 236*c4508464SDavid Howells 237*c4508464SDavid Howells page_endio(page, false, req->error); 238*c4508464SDavid Howells put_page(page); 239*c4508464SDavid Howells } 240*c4508464SDavid Howells rcu_read_unlock(); 241*c4508464SDavid Howells } 242*c4508464SDavid Howells } 243*c4508464SDavid Howells 244*c4508464SDavid Howells /* 245196ee9cdSDavid Howells * Dispose of a ref to a read record. 246196ee9cdSDavid Howells */ 247196ee9cdSDavid Howells void afs_put_read(struct afs_read *req) 248196ee9cdSDavid Howells { 249f3ddee8dSDavid Howells if (refcount_dec_and_test(&req->usage)) { 250*c4508464SDavid Howells if (req->cleanup) 251*c4508464SDavid Howells req->cleanup(req); 252c69bf479SDavid Howells key_put(req->key); 253196ee9cdSDavid Howells kfree(req); 254196ee9cdSDavid Howells } 255196ee9cdSDavid Howells } 256196ee9cdSDavid Howells 257e49c7b2fSDavid Howells static void afs_fetch_data_success(struct afs_operation *op) 258e49c7b2fSDavid Howells { 259e49c7b2fSDavid Howells struct afs_vnode *vnode = op->file[0].vnode; 260e49c7b2fSDavid Howells 261e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 262e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 263e49c7b2fSDavid Howells afs_stat_v(vnode, n_fetches); 264e49c7b2fSDavid Howells atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes); 265e49c7b2fSDavid Howells } 266e49c7b2fSDavid Howells 267e49c7b2fSDavid Howells static void afs_fetch_data_put(struct afs_operation *op) 268e49c7b2fSDavid Howells { 269*c4508464SDavid Howells op->fetch.req->error = op->error; 270e49c7b2fSDavid Howells afs_put_read(op->fetch.req); 271e49c7b2fSDavid Howells } 272e49c7b2fSDavid Howells 273e49c7b2fSDavid Howells static const struct afs_operation_ops afs_fetch_data_operation = { 274e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_fetch_data, 275e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_fetch_data, 276e49c7b2fSDavid Howells .success = afs_fetch_data_success, 277728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 278e49c7b2fSDavid Howells .put = afs_fetch_data_put, 279e49c7b2fSDavid Howells }; 280e49c7b2fSDavid Howells 2811da177e4SLinus Torvalds /* 282d2ddc776SDavid Howells * Fetch file data from the volume. 283d2ddc776SDavid Howells */ 284c69bf479SDavid Howells int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req) 285d2ddc776SDavid Howells { 286e49c7b2fSDavid Howells struct afs_operation *op; 287d2ddc776SDavid Howells 2883b6492dfSDavid Howells _enter("%s{%llx:%llu.%u},%x,,,", 289d2ddc776SDavid Howells vnode->volume->name, 290d2ddc776SDavid Howells vnode->fid.vid, 291d2ddc776SDavid Howells vnode->fid.vnode, 292d2ddc776SDavid Howells vnode->fid.unique, 293c69bf479SDavid Howells key_serial(req->key)); 294d2ddc776SDavid Howells 295c69bf479SDavid Howells op = afs_alloc_operation(req->key, vnode->volume); 296e49c7b2fSDavid Howells if (IS_ERR(op)) 297e49c7b2fSDavid Howells return PTR_ERR(op); 298a58823acSDavid Howells 299e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, vnode); 300a58823acSDavid Howells 301e49c7b2fSDavid Howells op->fetch.req = afs_get_read(req); 302e49c7b2fSDavid Howells op->ops = &afs_fetch_data_operation; 303e49c7b2fSDavid Howells return afs_do_sync_operation(op); 304d2ddc776SDavid Howells } 305d2ddc776SDavid Howells 306d2ddc776SDavid Howells /* 307f6d335c0SAl Viro * read page from file, directory or symlink, given a key to use 3081da177e4SLinus Torvalds */ 309*c4508464SDavid Howells static int afs_page_filler(struct key *key, struct page *page) 3101da177e4SLinus Torvalds { 311f6d335c0SAl Viro struct inode *inode = page->mapping->host; 312f6d335c0SAl Viro struct afs_vnode *vnode = AFS_FS_I(inode); 313196ee9cdSDavid Howells struct afs_read *req; 3141da177e4SLinus Torvalds int ret; 3151da177e4SLinus Torvalds 31600d3b7a4SDavid Howells _enter("{%x},{%lu},{%lu}", key_serial(key), inode->i_ino, page->index); 3171da177e4SLinus Torvalds 318cd7619d6SMatt Mackall BUG_ON(!PageLocked(page)); 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds ret = -ESTALE; 32108e0e7c8SDavid Howells if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 3221da177e4SLinus Torvalds goto error; 3231da177e4SLinus Torvalds 324*c4508464SDavid Howells req = kzalloc(sizeof(struct afs_read), GFP_KERNEL); 325196ee9cdSDavid Howells if (!req) 326196ee9cdSDavid Howells goto enomem; 327196ee9cdSDavid Howells 328f3ddee8dSDavid Howells refcount_set(&req->usage, 1); 329*c4508464SDavid Howells req->vnode = vnode; 330c69bf479SDavid Howells req->key = key_get(key); 331196ee9cdSDavid Howells req->pos = (loff_t)page->index << PAGE_SHIFT; 3326db3ac3cSDavid Howells req->len = PAGE_SIZE; 333196ee9cdSDavid Howells req->nr_pages = 1; 334*c4508464SDavid Howells req->done = afs_file_read_done; 335*c4508464SDavid Howells req->cleanup = afs_file_read_cleanup; 336*c4508464SDavid Howells 337196ee9cdSDavid Howells get_page(page); 338*c4508464SDavid Howells iov_iter_xarray(&req->def_iter, READ, &page->mapping->i_pages, 339*c4508464SDavid Howells req->pos, req->len); 340*c4508464SDavid Howells req->iter = &req->def_iter; 3411da177e4SLinus Torvalds 342c69bf479SDavid Howells ret = afs_fetch_data(vnode, req); 343*c4508464SDavid Howells if (ret < 0) 344*c4508464SDavid Howells goto fetch_error; 345*c4508464SDavid Howells 346196ee9cdSDavid Howells afs_put_read(req); 3471da177e4SLinus Torvalds _leave(" = 0"); 3481da177e4SLinus Torvalds return 0; 3491da177e4SLinus Torvalds 350*c4508464SDavid Howells fetch_error: 351*c4508464SDavid Howells switch (ret) { 352*c4508464SDavid Howells case -EINTR: 353*c4508464SDavid Howells case -ENOMEM: 354*c4508464SDavid Howells case -ERESTARTSYS: 355*c4508464SDavid Howells case -EAGAIN: 356*c4508464SDavid Howells afs_put_read(req); 35768ae849dSDavid Howells goto error; 358*c4508464SDavid Howells case -ENOENT: 359*c4508464SDavid Howells _debug("got NOENT from server - marking file deleted and stale"); 360*c4508464SDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 361*c4508464SDavid Howells ret = -ESTALE; 362*c4508464SDavid Howells /* Fall through */ 363*c4508464SDavid Howells default: 364*c4508464SDavid Howells page_endio(page, false, ret); 365*c4508464SDavid Howells afs_put_read(req); 366*c4508464SDavid Howells _leave(" = %d", ret); 367*c4508464SDavid Howells return ret; 368*c4508464SDavid Howells } 369*c4508464SDavid Howells 370196ee9cdSDavid Howells enomem: 371196ee9cdSDavid Howells ret = -ENOMEM; 3721da177e4SLinus Torvalds error: 3731da177e4SLinus Torvalds unlock_page(page); 3741da177e4SLinus Torvalds _leave(" = %d", ret); 3751da177e4SLinus Torvalds return ret; 376ec26815aSDavid Howells } 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds /* 379f6d335c0SAl Viro * read page from file, directory or symlink, given a file to nominate the key 380f6d335c0SAl Viro * to be used 381f6d335c0SAl Viro */ 382f6d335c0SAl Viro static int afs_readpage(struct file *file, struct page *page) 383f6d335c0SAl Viro { 384f6d335c0SAl Viro struct key *key; 385f6d335c0SAl Viro int ret; 386f6d335c0SAl Viro 387f6d335c0SAl Viro if (file) { 388215804a9SDavid Howells key = afs_file_key(file); 389f6d335c0SAl Viro ASSERT(key != NULL); 390f6d335c0SAl Viro ret = afs_page_filler(key, page); 391f6d335c0SAl Viro } else { 392f6d335c0SAl Viro struct inode *inode = page->mapping->host; 393d2ddc776SDavid Howells key = afs_request_key(AFS_FS_S(inode->i_sb)->cell); 394f6d335c0SAl Viro if (IS_ERR(key)) { 395f6d335c0SAl Viro ret = PTR_ERR(key); 396f6d335c0SAl Viro } else { 397f6d335c0SAl Viro ret = afs_page_filler(key, page); 398f6d335c0SAl Viro key_put(key); 399f6d335c0SAl Viro } 400f6d335c0SAl Viro } 401f6d335c0SAl Viro return ret; 402f6d335c0SAl Viro } 403f6d335c0SAl Viro 404f6d335c0SAl Viro /* 40591b467e0SDavid Howells * Read a contiguous set of pages. 40691b467e0SDavid Howells */ 40791b467e0SDavid Howells static int afs_readpages_one(struct file *file, struct address_space *mapping, 40891b467e0SDavid Howells struct list_head *pages) 40991b467e0SDavid Howells { 41091b467e0SDavid Howells struct afs_vnode *vnode = AFS_FS_I(mapping->host); 41191b467e0SDavid Howells struct afs_read *req; 41291b467e0SDavid Howells struct list_head *p; 41391b467e0SDavid Howells struct page *first, *page; 41491b467e0SDavid Howells pgoff_t index; 415*c4508464SDavid Howells int ret, n; 41691b467e0SDavid Howells 41791b467e0SDavid Howells /* Count the number of contiguous pages at the front of the list. Note 41891b467e0SDavid Howells * that the list goes prev-wards rather than next-wards. 41991b467e0SDavid Howells */ 420f86196eaSNikolay Borisov first = lru_to_page(pages); 42191b467e0SDavid Howells index = first->index + 1; 42291b467e0SDavid Howells n = 1; 42391b467e0SDavid Howells for (p = first->lru.prev; p != pages; p = p->prev) { 42491b467e0SDavid Howells page = list_entry(p, struct page, lru); 42591b467e0SDavid Howells if (page->index != index) 42691b467e0SDavid Howells break; 42791b467e0SDavid Howells index++; 42891b467e0SDavid Howells n++; 42991b467e0SDavid Howells } 43091b467e0SDavid Howells 431*c4508464SDavid Howells req = kzalloc(sizeof(struct afs_read), GFP_NOFS); 43291b467e0SDavid Howells if (!req) 43391b467e0SDavid Howells return -ENOMEM; 43491b467e0SDavid Howells 435f3ddee8dSDavid Howells refcount_set(&req->usage, 1); 436a58823acSDavid Howells req->vnode = vnode; 437c69bf479SDavid Howells req->key = key_get(afs_file_key(file)); 438*c4508464SDavid Howells req->done = afs_file_read_done; 439*c4508464SDavid Howells req->cleanup = afs_file_read_cleanup; 44091b467e0SDavid Howells req->pos = first->index; 44191b467e0SDavid Howells req->pos <<= PAGE_SHIFT; 44291b467e0SDavid Howells 443*c4508464SDavid Howells /* Add pages to the LRU until it fails. We keep the pages ref'd and 444*c4508464SDavid Howells * locked until the read is complete. 44591b467e0SDavid Howells * 44691b467e0SDavid Howells * Note that it's possible for the file size to change whilst we're 44791b467e0SDavid Howells * doing this, but we rely on the server returning less than we asked 44891b467e0SDavid Howells * for if the file shrank. We also rely on this to deal with a partial 44991b467e0SDavid Howells * page at the end of the file. 45091b467e0SDavid Howells */ 45191b467e0SDavid Howells do { 452f86196eaSNikolay Borisov page = lru_to_page(pages); 45391b467e0SDavid Howells list_del(&page->lru); 45491b467e0SDavid Howells index = page->index; 45591b467e0SDavid Howells if (add_to_page_cache_lru(page, mapping, index, 45691b467e0SDavid Howells readahead_gfp_mask(mapping))) { 45791b467e0SDavid Howells put_page(page); 45891b467e0SDavid Howells break; 45991b467e0SDavid Howells } 46091b467e0SDavid Howells 461*c4508464SDavid Howells req->nr_pages++; 46291b467e0SDavid Howells } while (req->nr_pages < n); 46391b467e0SDavid Howells 46491b467e0SDavid Howells if (req->nr_pages == 0) { 465c69bf479SDavid Howells afs_put_read(req); 46691b467e0SDavid Howells return 0; 46791b467e0SDavid Howells } 46891b467e0SDavid Howells 469*c4508464SDavid Howells req->len = req->nr_pages * PAGE_SIZE; 470*c4508464SDavid Howells iov_iter_xarray(&req->def_iter, READ, &file->f_mapping->i_pages, 471*c4508464SDavid Howells req->pos, req->len); 472*c4508464SDavid Howells req->iter = &req->def_iter; 473*c4508464SDavid Howells 474c69bf479SDavid Howells ret = afs_fetch_data(vnode, req); 47591b467e0SDavid Howells if (ret < 0) 47691b467e0SDavid Howells goto error; 47791b467e0SDavid Howells 47891b467e0SDavid Howells afs_put_read(req); 47991b467e0SDavid Howells return 0; 48091b467e0SDavid Howells 48191b467e0SDavid Howells error: 48291b467e0SDavid Howells if (ret == -ENOENT) { 483*c4508464SDavid Howells _debug("got NOENT from server - marking file deleted and stale"); 48491b467e0SDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 48591b467e0SDavid Howells ret = -ESTALE; 48691b467e0SDavid Howells } 48791b467e0SDavid Howells 48891b467e0SDavid Howells afs_put_read(req); 48991b467e0SDavid Howells return ret; 49091b467e0SDavid Howells } 49191b467e0SDavid Howells 49291b467e0SDavid Howells /* 4939b3f26c9SDavid Howells * read a set of pages 4941da177e4SLinus Torvalds */ 4959b3f26c9SDavid Howells static int afs_readpages(struct file *file, struct address_space *mapping, 4969b3f26c9SDavid Howells struct list_head *pages, unsigned nr_pages) 4971da177e4SLinus Torvalds { 498215804a9SDavid Howells struct key *key = afs_file_key(file); 4999b3f26c9SDavid Howells struct afs_vnode *vnode; 5009b3f26c9SDavid Howells int ret = 0; 5011da177e4SLinus Torvalds 502f6d335c0SAl Viro _enter("{%d},{%lu},,%d", 503f6d335c0SAl Viro key_serial(key), mapping->host->i_ino, nr_pages); 504f6d335c0SAl Viro 505f6d335c0SAl Viro ASSERT(key != NULL); 5061da177e4SLinus Torvalds 5079b3f26c9SDavid Howells vnode = AFS_FS_I(mapping->host); 508ad2a8e60SDan Carpenter if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 5099b3f26c9SDavid Howells _leave(" = -ESTALE"); 5109b3f26c9SDavid Howells return -ESTALE; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5139b3f26c9SDavid Howells /* attempt to read as many of the pages as possible */ 51491b467e0SDavid Howells while (!list_empty(pages)) { 51591b467e0SDavid Howells ret = afs_readpages_one(file, mapping, pages); 51691b467e0SDavid Howells if (ret < 0) 51791b467e0SDavid Howells break; 51891b467e0SDavid Howells } 5199b3f26c9SDavid Howells 5209b3f26c9SDavid Howells _leave(" = %d [netting]", ret); 5219b3f26c9SDavid Howells return ret; 522ec26815aSDavid Howells } 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds /* 525f86726a6SDavid Howells * Adjust the dirty region of the page on truncation or full invalidation, 526f86726a6SDavid Howells * getting rid of the markers altogether if the region is entirely invalidated. 527f86726a6SDavid Howells */ 528f86726a6SDavid Howells static void afs_invalidate_dirty(struct page *page, unsigned int offset, 529f86726a6SDavid Howells unsigned int length) 530f86726a6SDavid Howells { 531f86726a6SDavid Howells struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 532f86726a6SDavid Howells unsigned long priv; 533f86726a6SDavid Howells unsigned int f, t, end = offset + length; 534f86726a6SDavid Howells 535f86726a6SDavid Howells priv = page_private(page); 536f86726a6SDavid Howells 537f86726a6SDavid Howells /* we clean up only if the entire page is being invalidated */ 538f86726a6SDavid Howells if (offset == 0 && length == thp_size(page)) 539f86726a6SDavid Howells goto full_invalidate; 540f86726a6SDavid Howells 541f86726a6SDavid Howells /* If the page was dirtied by page_mkwrite(), the PTE stays writable 542f86726a6SDavid Howells * and we don't get another notification to tell us to expand it 543f86726a6SDavid Howells * again. 544f86726a6SDavid Howells */ 545f86726a6SDavid Howells if (afs_is_page_dirty_mmapped(priv)) 546f86726a6SDavid Howells return; 547f86726a6SDavid Howells 548f86726a6SDavid Howells /* We may need to shorten the dirty region */ 54967d78a6fSDavid Howells f = afs_page_dirty_from(page, priv); 55067d78a6fSDavid Howells t = afs_page_dirty_to(page, priv); 551f86726a6SDavid Howells 552f86726a6SDavid Howells if (t <= offset || f >= end) 553f86726a6SDavid Howells return; /* Doesn't overlap */ 554f86726a6SDavid Howells 555f86726a6SDavid Howells if (f < offset && t > end) 556f86726a6SDavid Howells return; /* Splits the dirty region - just absorb it */ 557f86726a6SDavid Howells 558f86726a6SDavid Howells if (f >= offset && t <= end) 559f86726a6SDavid Howells goto undirty; 560f86726a6SDavid Howells 561f86726a6SDavid Howells if (f < offset) 562f86726a6SDavid Howells t = offset; 563f86726a6SDavid Howells else 564f86726a6SDavid Howells f = end; 565f86726a6SDavid Howells if (f == t) 566f86726a6SDavid Howells goto undirty; 567f86726a6SDavid Howells 56867d78a6fSDavid Howells priv = afs_page_dirty(page, f, t); 569f86726a6SDavid Howells set_page_private(page, priv); 57067d78a6fSDavid Howells trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page); 571f86726a6SDavid Howells return; 572f86726a6SDavid Howells 573f86726a6SDavid Howells undirty: 57467d78a6fSDavid Howells trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page); 575f86726a6SDavid Howells clear_page_dirty_for_io(page); 576f86726a6SDavid Howells full_invalidate: 57767d78a6fSDavid Howells detach_page_private(page); 57867d78a6fSDavid Howells trace_afs_page_dirty(vnode, tracepoint_string("inval"), page); 579f86726a6SDavid Howells } 580f86726a6SDavid Howells 581f86726a6SDavid Howells /* 5829b3f26c9SDavid Howells * invalidate part or all of a page 5839b3f26c9SDavid Howells * - release a page and clean up its private data if offset is 0 (indicating 5849b3f26c9SDavid Howells * the entire page) 5859b3f26c9SDavid Howells */ 586d47992f8SLukas Czerner static void afs_invalidatepage(struct page *page, unsigned int offset, 587d47992f8SLukas Czerner unsigned int length) 5889b3f26c9SDavid Howells { 589d47992f8SLukas Czerner _enter("{%lu},%u,%u", page->index, offset, length); 5909b3f26c9SDavid Howells 5919b3f26c9SDavid Howells BUG_ON(!PageLocked(page)); 5929b3f26c9SDavid Howells 593f86726a6SDavid Howells if (PagePrivate(page)) 594f86726a6SDavid Howells afs_invalidate_dirty(page, offset, length); 5959b3f26c9SDavid Howells 5969b3f26c9SDavid Howells _leave(""); 5979b3f26c9SDavid Howells } 5989b3f26c9SDavid Howells 5999b3f26c9SDavid Howells /* 6009b3f26c9SDavid Howells * release a page and clean up its private state if it's not busy 6019b3f26c9SDavid Howells * - return true if the page can now be released, false if not 6021da177e4SLinus Torvalds */ 603416351f2SDavid Howells static int afs_releasepage(struct page *page, gfp_t gfp_flags) 6041da177e4SLinus Torvalds { 605416351f2SDavid Howells struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 6061da177e4SLinus Torvalds 6073b6492dfSDavid Howells _enter("{{%llx:%llu}[%lu],%lx},%x", 608416351f2SDavid Howells vnode->fid.vid, vnode->fid.vnode, page->index, page->flags, 609416351f2SDavid Howells gfp_flags); 6101da177e4SLinus Torvalds 6119b3f26c9SDavid Howells /* deny if page is being written to the cache and the caller hasn't 6129b3f26c9SDavid Howells * elected to wait */ 6139b3f26c9SDavid Howells if (PagePrivate(page)) { 61467d78a6fSDavid Howells detach_page_private(page); 61567d78a6fSDavid Howells trace_afs_page_dirty(vnode, tracepoint_string("rel"), page); 6169b3f26c9SDavid Howells } 6179b3f26c9SDavid Howells 6189b3f26c9SDavid Howells /* indicate that the page can be released */ 6199b3f26c9SDavid Howells _leave(" = T"); 6209b3f26c9SDavid Howells return 1; 621ec26815aSDavid Howells } 6221cf7a151SDavid Howells 6231cf7a151SDavid Howells /* 6241cf7a151SDavid Howells * Handle setting up a memory mapping on an AFS file. 6251cf7a151SDavid Howells */ 6261cf7a151SDavid Howells static int afs_file_mmap(struct file *file, struct vm_area_struct *vma) 6271cf7a151SDavid Howells { 6281cf7a151SDavid Howells int ret; 6291cf7a151SDavid Howells 6301cf7a151SDavid Howells ret = generic_file_mmap(file, vma); 6311cf7a151SDavid Howells if (ret == 0) 6321cf7a151SDavid Howells vma->vm_ops = &afs_vm_ops; 6331cf7a151SDavid Howells return ret; 6341cf7a151SDavid Howells } 635