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> 17d7bdba1cSDavid Howells #include <linux/swap.h> 185cbf0398SDavid Howells #include <linux/netfs.h> 191da177e4SLinus Torvalds #include "internal.h" 201da177e4SLinus Torvalds 211cf7a151SDavid Howells static int afs_file_mmap(struct file *file, struct vm_area_struct *vma); 22416351f2SDavid Howells static int afs_readpage(struct file *file, struct page *page); 2375bd228dSDavid Howells static int afs_symlink_readpage(struct file *file, struct page *page); 24fcf227daSMatthew Wilcox (Oracle) static void afs_invalidate_folio(struct folio *folio, size_t offset, 25fcf227daSMatthew Wilcox (Oracle) size_t length); 26416351f2SDavid Howells static int afs_releasepage(struct page *page, gfp_t gfp_flags); 271da177e4SLinus Torvalds 285cbf0398SDavid Howells static void afs_readahead(struct readahead_control *ractl); 293978d816SDavid Howells static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter); 306e0e99d5SDavid Howells static void afs_vm_open(struct vm_area_struct *area); 316e0e99d5SDavid Howells static void afs_vm_close(struct vm_area_struct *area); 326e0e99d5SDavid Howells static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff); 339b3f26c9SDavid Howells 3400d3b7a4SDavid Howells const struct file_operations afs_file_operations = { 3500d3b7a4SDavid Howells .open = afs_open, 3600d3b7a4SDavid Howells .release = afs_release, 3700d3b7a4SDavid Howells .llseek = generic_file_llseek, 383978d816SDavid Howells .read_iter = afs_file_read_iter, 3950b5551dSAl Viro .write_iter = afs_file_write, 401cf7a151SDavid Howells .mmap = afs_file_mmap, 415ffc4ef4SJens Axboe .splice_read = generic_file_splice_read, 4206a17bbeSDavid Howells .splice_write = iter_file_splice_write, 4331143d5dSDavid Howells .fsync = afs_fsync, 44e8d6c554SDavid Howells .lock = afs_lock, 45e8d6c554SDavid Howells .flock = afs_flock, 4600d3b7a4SDavid Howells }; 4700d3b7a4SDavid Howells 48754661f1SArjan van de Ven const struct inode_operations afs_file_inode_operations = { 49416351f2SDavid Howells .getattr = afs_getattr, 5031143d5dSDavid Howells .setattr = afs_setattr, 5100d3b7a4SDavid Howells .permission = afs_permission, 521da177e4SLinus Torvalds }; 531da177e4SLinus Torvalds 5475bd228dSDavid Howells const struct address_space_operations afs_file_aops = { 55416351f2SDavid Howells .readpage = afs_readpage, 565cbf0398SDavid Howells .readahead = afs_readahead, 57*8fb72b4aSMatthew Wilcox (Oracle) .dirty_folio = afs_dirty_folio, 58a42442ddSMatthew Wilcox (Oracle) .launder_folio = afs_launder_folio, 59416351f2SDavid Howells .releasepage = afs_releasepage, 60fcf227daSMatthew Wilcox (Oracle) .invalidate_folio = afs_invalidate_folio, 6115b4650eSNick Piggin .write_begin = afs_write_begin, 6215b4650eSNick Piggin .write_end = afs_write_end, 6331143d5dSDavid Howells .writepage = afs_writepage, 6431143d5dSDavid Howells .writepages = afs_writepages, 651da177e4SLinus Torvalds }; 661da177e4SLinus Torvalds 6775bd228dSDavid Howells const struct address_space_operations afs_symlink_aops = { 6875bd228dSDavid Howells .readpage = afs_symlink_readpage, 6975bd228dSDavid Howells .releasepage = afs_releasepage, 70fcf227daSMatthew Wilcox (Oracle) .invalidate_folio = afs_invalidate_folio, 7175bd228dSDavid Howells }; 7275bd228dSDavid Howells 731cf7a151SDavid Howells static const struct vm_operations_struct afs_vm_ops = { 746e0e99d5SDavid Howells .open = afs_vm_open, 756e0e99d5SDavid Howells .close = afs_vm_close, 761cf7a151SDavid Howells .fault = filemap_fault, 776e0e99d5SDavid Howells .map_pages = afs_vm_map_pages, 781cf7a151SDavid Howells .page_mkwrite = afs_page_mkwrite, 791cf7a151SDavid Howells }; 801cf7a151SDavid Howells 811da177e4SLinus Torvalds /* 824343d008SDavid Howells * Discard a pin on a writeback key. 834343d008SDavid Howells */ 844343d008SDavid Howells void afs_put_wb_key(struct afs_wb_key *wbk) 854343d008SDavid Howells { 86e49c7b2fSDavid Howells if (wbk && refcount_dec_and_test(&wbk->usage)) { 874343d008SDavid Howells key_put(wbk->key); 884343d008SDavid Howells kfree(wbk); 894343d008SDavid Howells } 904343d008SDavid Howells } 914343d008SDavid Howells 924343d008SDavid Howells /* 934343d008SDavid Howells * Cache key for writeback. 944343d008SDavid Howells */ 954343d008SDavid Howells int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af) 964343d008SDavid Howells { 974343d008SDavid Howells struct afs_wb_key *wbk, *p; 984343d008SDavid Howells 994343d008SDavid Howells wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL); 1004343d008SDavid Howells if (!wbk) 1014343d008SDavid Howells return -ENOMEM; 1024343d008SDavid Howells refcount_set(&wbk->usage, 2); 1034343d008SDavid Howells wbk->key = af->key; 1044343d008SDavid Howells 1054343d008SDavid Howells spin_lock(&vnode->wb_lock); 1064343d008SDavid Howells list_for_each_entry(p, &vnode->wb_keys, vnode_link) { 1074343d008SDavid Howells if (p->key == wbk->key) 1084343d008SDavid Howells goto found; 1094343d008SDavid Howells } 1104343d008SDavid Howells 1114343d008SDavid Howells key_get(wbk->key); 1124343d008SDavid Howells list_add_tail(&wbk->vnode_link, &vnode->wb_keys); 1134343d008SDavid Howells spin_unlock(&vnode->wb_lock); 1144343d008SDavid Howells af->wb = wbk; 1154343d008SDavid Howells return 0; 1164343d008SDavid Howells 1174343d008SDavid Howells found: 1184343d008SDavid Howells refcount_inc(&p->usage); 1194343d008SDavid Howells spin_unlock(&vnode->wb_lock); 1204343d008SDavid Howells af->wb = p; 1214343d008SDavid Howells kfree(wbk); 1224343d008SDavid Howells return 0; 1234343d008SDavid Howells } 1244343d008SDavid Howells 1254343d008SDavid Howells /* 12600d3b7a4SDavid Howells * open an AFS file or directory and attach a key to it 12700d3b7a4SDavid Howells */ 12800d3b7a4SDavid Howells int afs_open(struct inode *inode, struct file *file) 12900d3b7a4SDavid Howells { 13000d3b7a4SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 131215804a9SDavid Howells struct afs_file *af; 13200d3b7a4SDavid Howells struct key *key; 133260a9803SDavid Howells int ret; 13400d3b7a4SDavid Howells 1353b6492dfSDavid Howells _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 13600d3b7a4SDavid Howells 13700d3b7a4SDavid Howells key = afs_request_key(vnode->volume->cell); 13800d3b7a4SDavid Howells if (IS_ERR(key)) { 139215804a9SDavid Howells ret = PTR_ERR(key); 140215804a9SDavid Howells goto error; 141215804a9SDavid Howells } 142215804a9SDavid Howells 143215804a9SDavid Howells af = kzalloc(sizeof(*af), GFP_KERNEL); 144215804a9SDavid Howells if (!af) { 145215804a9SDavid Howells ret = -ENOMEM; 146215804a9SDavid Howells goto error_key; 14700d3b7a4SDavid Howells } 1484343d008SDavid Howells af->key = key; 14900d3b7a4SDavid Howells 150260a9803SDavid Howells ret = afs_validate(vnode, key); 151215804a9SDavid Howells if (ret < 0) 152215804a9SDavid Howells goto error_af; 153260a9803SDavid Howells 1544343d008SDavid Howells if (file->f_mode & FMODE_WRITE) { 1554343d008SDavid Howells ret = afs_cache_wb_key(vnode, af); 1564343d008SDavid Howells if (ret < 0) 1574343d008SDavid Howells goto error_af; 1584343d008SDavid Howells } 1594343d008SDavid Howells 1605a813276SDavid Howells if (file->f_flags & O_TRUNC) 1615a813276SDavid Howells set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 1625a813276SDavid Howells 163523d27cdSDavid Howells fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE); 164523d27cdSDavid Howells 165215804a9SDavid Howells file->private_data = af; 16600d3b7a4SDavid Howells _leave(" = 0"); 16700d3b7a4SDavid Howells return 0; 168215804a9SDavid Howells 169215804a9SDavid Howells error_af: 170215804a9SDavid Howells kfree(af); 171215804a9SDavid Howells error_key: 172215804a9SDavid Howells key_put(key); 173215804a9SDavid Howells error: 174215804a9SDavid Howells _leave(" = %d", ret); 175215804a9SDavid Howells return ret; 17600d3b7a4SDavid Howells } 17700d3b7a4SDavid Howells 17800d3b7a4SDavid Howells /* 17900d3b7a4SDavid Howells * release an AFS file or directory and discard its key 18000d3b7a4SDavid Howells */ 18100d3b7a4SDavid Howells int afs_release(struct inode *inode, struct file *file) 18200d3b7a4SDavid Howells { 183523d27cdSDavid Howells struct afs_vnode_cache_aux aux; 18400d3b7a4SDavid Howells struct afs_vnode *vnode = AFS_FS_I(inode); 185215804a9SDavid Howells struct afs_file *af = file->private_data; 186523d27cdSDavid Howells loff_t i_size; 187a1b879eeSDavid Howells int ret = 0; 18800d3b7a4SDavid Howells 1893b6492dfSDavid Howells _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode); 19000d3b7a4SDavid Howells 1915a813276SDavid Howells if ((file->f_mode & FMODE_WRITE)) 192a1b879eeSDavid Howells ret = vfs_fsync(file, 0); 1935a813276SDavid Howells 194215804a9SDavid Howells file->private_data = NULL; 1954343d008SDavid Howells if (af->wb) 1964343d008SDavid Howells afs_put_wb_key(af->wb); 197523d27cdSDavid Howells 198523d27cdSDavid Howells if ((file->f_mode & FMODE_WRITE)) { 199523d27cdSDavid Howells i_size = i_size_read(&vnode->vfs_inode); 200523d27cdSDavid Howells afs_set_cache_aux(vnode, &aux); 201523d27cdSDavid Howells fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size); 202523d27cdSDavid Howells } else { 203523d27cdSDavid Howells fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL); 204523d27cdSDavid Howells } 205523d27cdSDavid Howells 206215804a9SDavid Howells key_put(af->key); 207215804a9SDavid Howells kfree(af); 2084343d008SDavid Howells afs_prune_wb_keys(vnode); 209a1b879eeSDavid Howells _leave(" = %d", ret); 210a1b879eeSDavid Howells return ret; 21100d3b7a4SDavid Howells } 21200d3b7a4SDavid Howells 213196ee9cdSDavid Howells /* 2145cbf0398SDavid Howells * Allocate a new read record. 215c4508464SDavid Howells */ 2165cbf0398SDavid Howells struct afs_read *afs_alloc_read(gfp_t gfp) 217c4508464SDavid Howells { 2185cbf0398SDavid Howells struct afs_read *req; 219c4508464SDavid Howells 2205cbf0398SDavid Howells req = kzalloc(sizeof(struct afs_read), gfp); 2215cbf0398SDavid Howells if (req) 2225cbf0398SDavid Howells refcount_set(&req->usage, 1); 223c4508464SDavid Howells 2245cbf0398SDavid Howells return req; 225c4508464SDavid Howells } 226c4508464SDavid Howells 227c4508464SDavid Howells /* 228196ee9cdSDavid Howells * Dispose of a ref to a read record. 229196ee9cdSDavid Howells */ 230196ee9cdSDavid Howells void afs_put_read(struct afs_read *req) 231196ee9cdSDavid Howells { 232f3ddee8dSDavid Howells if (refcount_dec_and_test(&req->usage)) { 233c4508464SDavid Howells if (req->cleanup) 234c4508464SDavid Howells req->cleanup(req); 235c69bf479SDavid Howells key_put(req->key); 236196ee9cdSDavid Howells kfree(req); 237196ee9cdSDavid Howells } 238196ee9cdSDavid Howells } 239196ee9cdSDavid Howells 240dc419184SDavid Howells static void afs_fetch_data_notify(struct afs_operation *op) 241dc419184SDavid Howells { 242dc419184SDavid Howells struct afs_read *req = op->fetch.req; 2435cbf0398SDavid Howells struct netfs_read_subrequest *subreq = req->subreq; 244dc419184SDavid Howells int error = op->error; 245dc419184SDavid Howells 246dc419184SDavid Howells if (error == -ECONNABORTED) 247dc419184SDavid Howells error = afs_abort_to_error(op->ac.abort_code); 248dc419184SDavid Howells req->error = error; 249dc419184SDavid Howells 2505cbf0398SDavid Howells if (subreq) { 2515cbf0398SDavid Howells __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags); 2525cbf0398SDavid Howells netfs_subreq_terminated(subreq, error ?: req->actual_len, false); 2535cbf0398SDavid Howells req->subreq = NULL; 2545cbf0398SDavid Howells } else if (req->done) { 255dc419184SDavid Howells req->done(req); 256dc419184SDavid Howells } 2575cbf0398SDavid Howells } 258dc419184SDavid Howells 259e49c7b2fSDavid Howells static void afs_fetch_data_success(struct afs_operation *op) 260e49c7b2fSDavid Howells { 261e49c7b2fSDavid Howells struct afs_vnode *vnode = op->file[0].vnode; 262e49c7b2fSDavid Howells 263e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 264e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 265e49c7b2fSDavid Howells afs_stat_v(vnode, n_fetches); 266e49c7b2fSDavid Howells atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes); 267dc419184SDavid Howells afs_fetch_data_notify(op); 268e49c7b2fSDavid Howells } 269e49c7b2fSDavid Howells 270e49c7b2fSDavid Howells static void afs_fetch_data_put(struct afs_operation *op) 271e49c7b2fSDavid Howells { 272c4508464SDavid Howells op->fetch.req->error = op->error; 273e49c7b2fSDavid Howells afs_put_read(op->fetch.req); 274e49c7b2fSDavid Howells } 275e49c7b2fSDavid Howells 276e49c7b2fSDavid Howells static const struct afs_operation_ops afs_fetch_data_operation = { 277e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_fetch_data, 278e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_fetch_data, 279e49c7b2fSDavid Howells .success = afs_fetch_data_success, 280728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 281dc419184SDavid Howells .failed = afs_fetch_data_notify, 282e49c7b2fSDavid Howells .put = afs_fetch_data_put, 283e49c7b2fSDavid Howells }; 284e49c7b2fSDavid Howells 2851da177e4SLinus Torvalds /* 286d2ddc776SDavid Howells * Fetch file data from the volume. 287d2ddc776SDavid Howells */ 288c69bf479SDavid Howells int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req) 289d2ddc776SDavid Howells { 290e49c7b2fSDavid Howells struct afs_operation *op; 291d2ddc776SDavid Howells 2923b6492dfSDavid Howells _enter("%s{%llx:%llu.%u},%x,,,", 293d2ddc776SDavid Howells vnode->volume->name, 294d2ddc776SDavid Howells vnode->fid.vid, 295d2ddc776SDavid Howells vnode->fid.vnode, 296d2ddc776SDavid Howells vnode->fid.unique, 297c69bf479SDavid Howells key_serial(req->key)); 298d2ddc776SDavid Howells 299c69bf479SDavid Howells op = afs_alloc_operation(req->key, vnode->volume); 3005cbf0398SDavid Howells if (IS_ERR(op)) { 3015cbf0398SDavid Howells if (req->subreq) 3025cbf0398SDavid Howells netfs_subreq_terminated(req->subreq, PTR_ERR(op), false); 303e49c7b2fSDavid Howells return PTR_ERR(op); 3045cbf0398SDavid Howells } 305a58823acSDavid Howells 306e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, vnode); 307a58823acSDavid Howells 308e49c7b2fSDavid Howells op->fetch.req = afs_get_read(req); 309e49c7b2fSDavid Howells op->ops = &afs_fetch_data_operation; 310e49c7b2fSDavid Howells return afs_do_sync_operation(op); 311d2ddc776SDavid Howells } 312d2ddc776SDavid Howells 3135cbf0398SDavid Howells static void afs_req_issue_op(struct netfs_read_subrequest *subreq) 3141da177e4SLinus Torvalds { 3155cbf0398SDavid Howells struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode); 3165cbf0398SDavid Howells struct afs_read *fsreq; 3175cbf0398SDavid Howells 3185cbf0398SDavid Howells fsreq = afs_alloc_read(GFP_NOFS); 3195cbf0398SDavid Howells if (!fsreq) 3205cbf0398SDavid Howells return netfs_subreq_terminated(subreq, -ENOMEM, false); 3215cbf0398SDavid Howells 3225cbf0398SDavid Howells fsreq->subreq = subreq; 3235cbf0398SDavid Howells fsreq->pos = subreq->start + subreq->transferred; 3245cbf0398SDavid Howells fsreq->len = subreq->len - subreq->transferred; 325345e1ae0SDavid Howells fsreq->key = key_get(subreq->rreq->netfs_priv); 3265cbf0398SDavid Howells fsreq->vnode = vnode; 3275cbf0398SDavid Howells fsreq->iter = &fsreq->def_iter; 3285cbf0398SDavid Howells 3295cbf0398SDavid Howells iov_iter_xarray(&fsreq->def_iter, READ, 3305cbf0398SDavid Howells &fsreq->vnode->vfs_inode.i_mapping->i_pages, 3315cbf0398SDavid Howells fsreq->pos, fsreq->len); 3325cbf0398SDavid Howells 3335cbf0398SDavid Howells afs_fetch_data(fsreq->vnode, fsreq); 334345e1ae0SDavid Howells afs_put_read(fsreq); 3355cbf0398SDavid Howells } 3365cbf0398SDavid Howells 33775bd228dSDavid Howells static int afs_symlink_readpage(struct file *file, struct page *page) 3385cbf0398SDavid Howells { 3395cbf0398SDavid Howells struct afs_vnode *vnode = AFS_FS_I(page->mapping->host); 3405cbf0398SDavid Howells struct afs_read *fsreq; 34178525c74SDavid Howells struct folio *folio = page_folio(page); 3421da177e4SLinus Torvalds int ret; 3431da177e4SLinus Torvalds 3445cbf0398SDavid Howells fsreq = afs_alloc_read(GFP_NOFS); 3455cbf0398SDavid Howells if (!fsreq) 34691b467e0SDavid Howells return -ENOMEM; 34791b467e0SDavid Howells 34878525c74SDavid Howells fsreq->pos = folio_pos(folio); 34978525c74SDavid Howells fsreq->len = folio_size(folio); 3505cbf0398SDavid Howells fsreq->vnode = vnode; 3515cbf0398SDavid Howells fsreq->iter = &fsreq->def_iter; 3525cbf0398SDavid Howells iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages, 3535cbf0398SDavid Howells fsreq->pos, fsreq->len); 35491b467e0SDavid Howells 3555cbf0398SDavid Howells ret = afs_fetch_data(fsreq->vnode, fsreq); 35678525c74SDavid Howells if (ret == 0) 35778525c74SDavid Howells SetPageUptodate(page); 35878525c74SDavid Howells unlock_page(page); 35991b467e0SDavid Howells return ret; 36091b467e0SDavid Howells } 36191b467e0SDavid Howells 3625cbf0398SDavid Howells static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file) 3631da177e4SLinus Torvalds { 3645cbf0398SDavid Howells rreq->netfs_priv = key_get(afs_file_key(file)); 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3673003bbd0SDavid Howells static bool afs_is_cache_enabled(struct inode *inode) 3683003bbd0SDavid Howells { 3693003bbd0SDavid Howells struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode)); 3703003bbd0SDavid Howells 371523d27cdSDavid Howells return fscache_cookie_enabled(cookie) && cookie->cache_priv; 3723003bbd0SDavid Howells } 3733003bbd0SDavid Howells 3745cbf0398SDavid Howells static int afs_begin_cache_operation(struct netfs_read_request *rreq) 3755cbf0398SDavid Howells { 3762cee6fbbSDavid Howells #ifdef CONFIG_AFS_FSCACHE 3775cbf0398SDavid Howells struct afs_vnode *vnode = AFS_FS_I(rreq->inode); 3785cbf0398SDavid Howells 379523d27cdSDavid Howells return fscache_begin_read_operation(&rreq->cache_resources, 380523d27cdSDavid Howells afs_vnode_cache(vnode)); 3812cee6fbbSDavid Howells #else 3822cee6fbbSDavid Howells return -ENOBUFS; 3832cee6fbbSDavid Howells #endif 38491b467e0SDavid Howells } 3859b3f26c9SDavid Howells 3863003bbd0SDavid Howells static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len, 38778525c74SDavid Howells struct folio *folio, void **_fsdata) 3883003bbd0SDavid Howells { 3893003bbd0SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 3903003bbd0SDavid Howells 3913003bbd0SDavid Howells return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0; 3923003bbd0SDavid Howells } 3933003bbd0SDavid Howells 3945cbf0398SDavid Howells static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv) 3955cbf0398SDavid Howells { 3965cbf0398SDavid Howells key_put(netfs_priv); 3975cbf0398SDavid Howells } 3985cbf0398SDavid Howells 3993003bbd0SDavid Howells const struct netfs_read_request_ops afs_req_ops = { 4005cbf0398SDavid Howells .init_rreq = afs_init_rreq, 4013003bbd0SDavid Howells .is_cache_enabled = afs_is_cache_enabled, 4025cbf0398SDavid Howells .begin_cache_operation = afs_begin_cache_operation, 4033003bbd0SDavid Howells .check_write_begin = afs_check_write_begin, 4045cbf0398SDavid Howells .issue_op = afs_req_issue_op, 4055cbf0398SDavid Howells .cleanup = afs_priv_cleanup, 4065cbf0398SDavid Howells }; 4075cbf0398SDavid Howells 4085cbf0398SDavid Howells static int afs_readpage(struct file *file, struct page *page) 4095cbf0398SDavid Howells { 41078525c74SDavid Howells struct folio *folio = page_folio(page); 41178525c74SDavid Howells 41278525c74SDavid Howells return netfs_readpage(file, folio, &afs_req_ops, NULL); 4135cbf0398SDavid Howells } 4145cbf0398SDavid Howells 4155cbf0398SDavid Howells static void afs_readahead(struct readahead_control *ractl) 4165cbf0398SDavid Howells { 4175cbf0398SDavid Howells netfs_readahead(ractl, &afs_req_ops, NULL); 418ec26815aSDavid Howells } 4191da177e4SLinus Torvalds 420c7f75ef3SDavid Howells int afs_write_inode(struct inode *inode, struct writeback_control *wbc) 421c7f75ef3SDavid Howells { 422c7f75ef3SDavid Howells fscache_unpin_writeback(wbc, afs_vnode_cache(AFS_FS_I(inode))); 423c7f75ef3SDavid Howells return 0; 424c7f75ef3SDavid Howells } 425c7f75ef3SDavid Howells 4261da177e4SLinus Torvalds /* 427f86726a6SDavid Howells * Adjust the dirty region of the page on truncation or full invalidation, 428f86726a6SDavid Howells * getting rid of the markers altogether if the region is entirely invalidated. 429f86726a6SDavid Howells */ 430fcf227daSMatthew Wilcox (Oracle) static void afs_invalidate_dirty(struct folio *folio, size_t offset, 431fcf227daSMatthew Wilcox (Oracle) size_t length) 432f86726a6SDavid Howells { 43378525c74SDavid Howells struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); 434f86726a6SDavid Howells unsigned long priv; 435f86726a6SDavid Howells unsigned int f, t, end = offset + length; 436f86726a6SDavid Howells 43778525c74SDavid Howells priv = (unsigned long)folio_get_private(folio); 438f86726a6SDavid Howells 439f86726a6SDavid Howells /* we clean up only if the entire page is being invalidated */ 44078525c74SDavid Howells if (offset == 0 && length == folio_size(folio)) 441f86726a6SDavid Howells goto full_invalidate; 442f86726a6SDavid Howells 443f86726a6SDavid Howells /* If the page was dirtied by page_mkwrite(), the PTE stays writable 444f86726a6SDavid Howells * and we don't get another notification to tell us to expand it 445f86726a6SDavid Howells * again. 446f86726a6SDavid Howells */ 44778525c74SDavid Howells if (afs_is_folio_dirty_mmapped(priv)) 448f86726a6SDavid Howells return; 449f86726a6SDavid Howells 450f86726a6SDavid Howells /* We may need to shorten the dirty region */ 45178525c74SDavid Howells f = afs_folio_dirty_from(folio, priv); 45278525c74SDavid Howells t = afs_folio_dirty_to(folio, priv); 453f86726a6SDavid Howells 454f86726a6SDavid Howells if (t <= offset || f >= end) 455f86726a6SDavid Howells return; /* Doesn't overlap */ 456f86726a6SDavid Howells 457f86726a6SDavid Howells if (f < offset && t > end) 458f86726a6SDavid Howells return; /* Splits the dirty region - just absorb it */ 459f86726a6SDavid Howells 460f86726a6SDavid Howells if (f >= offset && t <= end) 461f86726a6SDavid Howells goto undirty; 462f86726a6SDavid Howells 463f86726a6SDavid Howells if (f < offset) 464f86726a6SDavid Howells t = offset; 465f86726a6SDavid Howells else 466f86726a6SDavid Howells f = end; 467f86726a6SDavid Howells if (f == t) 468f86726a6SDavid Howells goto undirty; 469f86726a6SDavid Howells 47078525c74SDavid Howells priv = afs_folio_dirty(folio, f, t); 47178525c74SDavid Howells folio_change_private(folio, (void *)priv); 47278525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio); 473f86726a6SDavid Howells return; 474f86726a6SDavid Howells 475f86726a6SDavid Howells undirty: 47678525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio); 47778525c74SDavid Howells folio_clear_dirty_for_io(folio); 478f86726a6SDavid Howells full_invalidate: 47978525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio); 48078525c74SDavid Howells folio_detach_private(folio); 481f86726a6SDavid Howells } 482f86726a6SDavid Howells 483f86726a6SDavid Howells /* 4849b3f26c9SDavid Howells * invalidate part or all of a page 4859b3f26c9SDavid Howells * - release a page and clean up its private data if offset is 0 (indicating 4869b3f26c9SDavid Howells * the entire page) 4879b3f26c9SDavid Howells */ 488fcf227daSMatthew Wilcox (Oracle) static void afs_invalidate_folio(struct folio *folio, size_t offset, 489fcf227daSMatthew Wilcox (Oracle) size_t length) 4909b3f26c9SDavid Howells { 491fcf227daSMatthew Wilcox (Oracle) _enter("{%lu},%zu,%zu", folio->index, offset, length); 49278525c74SDavid Howells 493fcf227daSMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 4949b3f26c9SDavid Howells 495fcf227daSMatthew Wilcox (Oracle) if (folio_get_private(folio)) 49678525c74SDavid Howells afs_invalidate_dirty(folio, offset, length); 4979b3f26c9SDavid Howells 49878525c74SDavid Howells folio_wait_fscache(folio); 4999b3f26c9SDavid Howells _leave(""); 5009b3f26c9SDavid Howells } 5019b3f26c9SDavid Howells 5029b3f26c9SDavid Howells /* 5039b3f26c9SDavid Howells * release a page and clean up its private state if it's not busy 5049b3f26c9SDavid Howells * - return true if the page can now be released, false if not 5051da177e4SLinus Torvalds */ 506523d27cdSDavid Howells static int afs_releasepage(struct page *page, gfp_t gfp) 5071da177e4SLinus Torvalds { 50878525c74SDavid Howells struct folio *folio = page_folio(page); 50978525c74SDavid Howells struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio)); 5101da177e4SLinus Torvalds 5113b6492dfSDavid Howells _enter("{{%llx:%llu}[%lu],%lx},%x", 51278525c74SDavid Howells vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags, 513523d27cdSDavid Howells gfp); 5141da177e4SLinus Torvalds 5159b3f26c9SDavid Howells /* deny if page is being written to the cache and the caller hasn't 5169b3f26c9SDavid Howells * elected to wait */ 517630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE 51878525c74SDavid Howells if (folio_test_fscache(folio)) { 519d7bdba1cSDavid Howells if (current_is_kswapd() || !(gfp & __GFP_FS)) 520630f5ddaSDavid Howells return false; 52178525c74SDavid Howells folio_wait_fscache(folio); 522630f5ddaSDavid Howells } 523523d27cdSDavid Howells fscache_note_page_release(afs_vnode_cache(vnode)); 524630f5ddaSDavid Howells #endif 525630f5ddaSDavid Howells 52678525c74SDavid Howells if (folio_test_private(folio)) { 52778525c74SDavid Howells trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio); 52878525c74SDavid Howells folio_detach_private(folio); 5299b3f26c9SDavid Howells } 5309b3f26c9SDavid Howells 53178525c74SDavid Howells /* Indicate that the folio can be released */ 5329b3f26c9SDavid Howells _leave(" = T"); 53378525c74SDavid Howells return true; 534ec26815aSDavid Howells } 5351cf7a151SDavid Howells 5366e0e99d5SDavid Howells static void afs_add_open_mmap(struct afs_vnode *vnode) 5376e0e99d5SDavid Howells { 5386e0e99d5SDavid Howells if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) { 5396e0e99d5SDavid Howells down_write(&vnode->volume->cell->fs_open_mmaps_lock); 5406e0e99d5SDavid Howells 5411744a22aSDavid Howells if (list_empty(&vnode->cb_mmap_link)) 5426e0e99d5SDavid Howells list_add_tail(&vnode->cb_mmap_link, 5436e0e99d5SDavid Howells &vnode->volume->cell->fs_open_mmaps); 5446e0e99d5SDavid Howells 5456e0e99d5SDavid Howells up_write(&vnode->volume->cell->fs_open_mmaps_lock); 5466e0e99d5SDavid Howells } 5476e0e99d5SDavid Howells } 5486e0e99d5SDavid Howells 5496e0e99d5SDavid Howells static void afs_drop_open_mmap(struct afs_vnode *vnode) 5506e0e99d5SDavid Howells { 5516e0e99d5SDavid Howells if (!atomic_dec_and_test(&vnode->cb_nr_mmap)) 5526e0e99d5SDavid Howells return; 5536e0e99d5SDavid Howells 5546e0e99d5SDavid Howells down_write(&vnode->volume->cell->fs_open_mmaps_lock); 5556e0e99d5SDavid Howells 5566e0e99d5SDavid Howells if (atomic_read(&vnode->cb_nr_mmap) == 0) 5576e0e99d5SDavid Howells list_del_init(&vnode->cb_mmap_link); 5586e0e99d5SDavid Howells 5596e0e99d5SDavid Howells up_write(&vnode->volume->cell->fs_open_mmaps_lock); 5606e0e99d5SDavid Howells flush_work(&vnode->cb_work); 5616e0e99d5SDavid Howells } 5626e0e99d5SDavid Howells 5631cf7a151SDavid Howells /* 5641cf7a151SDavid Howells * Handle setting up a memory mapping on an AFS file. 5651cf7a151SDavid Howells */ 5661cf7a151SDavid Howells static int afs_file_mmap(struct file *file, struct vm_area_struct *vma) 5671cf7a151SDavid Howells { 5686e0e99d5SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 5691cf7a151SDavid Howells int ret; 5701cf7a151SDavid Howells 5716e0e99d5SDavid Howells afs_add_open_mmap(vnode); 5726e0e99d5SDavid Howells 5731cf7a151SDavid Howells ret = generic_file_mmap(file, vma); 5741cf7a151SDavid Howells if (ret == 0) 5751cf7a151SDavid Howells vma->vm_ops = &afs_vm_ops; 5766e0e99d5SDavid Howells else 5776e0e99d5SDavid Howells afs_drop_open_mmap(vnode); 5781cf7a151SDavid Howells return ret; 5791cf7a151SDavid Howells } 5803978d816SDavid Howells 5816e0e99d5SDavid Howells static void afs_vm_open(struct vm_area_struct *vma) 5826e0e99d5SDavid Howells { 5836e0e99d5SDavid Howells afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); 5846e0e99d5SDavid Howells } 5856e0e99d5SDavid Howells 5866e0e99d5SDavid Howells static void afs_vm_close(struct vm_area_struct *vma) 5876e0e99d5SDavid Howells { 5886e0e99d5SDavid Howells afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file))); 5896e0e99d5SDavid Howells } 5906e0e99d5SDavid Howells 5916e0e99d5SDavid Howells static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff) 5926e0e99d5SDavid Howells { 5936e0e99d5SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file)); 5946e0e99d5SDavid Howells struct afs_file *af = vmf->vma->vm_file->private_data; 5956e0e99d5SDavid Howells 5966e0e99d5SDavid Howells switch (afs_validate(vnode, af->key)) { 5976e0e99d5SDavid Howells case 0: 5986e0e99d5SDavid Howells return filemap_map_pages(vmf, start_pgoff, end_pgoff); 5996e0e99d5SDavid Howells case -ENOMEM: 6006e0e99d5SDavid Howells return VM_FAULT_OOM; 6016e0e99d5SDavid Howells case -EINTR: 6026e0e99d5SDavid Howells case -ERESTARTSYS: 6036e0e99d5SDavid Howells return VM_FAULT_RETRY; 6046e0e99d5SDavid Howells case -ESTALE: 6056e0e99d5SDavid Howells default: 6066e0e99d5SDavid Howells return VM_FAULT_SIGBUS; 6076e0e99d5SDavid Howells } 6086e0e99d5SDavid Howells } 6096e0e99d5SDavid Howells 6103978d816SDavid Howells static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) 6113978d816SDavid Howells { 6123978d816SDavid Howells struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 6133978d816SDavid Howells struct afs_file *af = iocb->ki_filp->private_data; 6143978d816SDavid Howells int ret; 6153978d816SDavid Howells 6163978d816SDavid Howells ret = afs_validate(vnode, af->key); 6173978d816SDavid Howells if (ret < 0) 6183978d816SDavid Howells return ret; 6193978d816SDavid Howells 6203978d816SDavid Howells return generic_file_read_iter(iocb, iter); 6213978d816SDavid Howells } 622