Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* AFS filesystem file handling
0003  *
0004  * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/init.h>
0011 #include <linux/fs.h>
0012 #include <linux/pagemap.h>
0013 #include <linux/writeback.h>
0014 #include <linux/gfp.h>
0015 #include <linux/task_io_accounting_ops.h>
0016 #include <linux/mm.h>
0017 #include <linux/swap.h>
0018 #include <linux/netfs.h>
0019 #include "internal.h"
0020 
0021 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
0022 static int afs_symlink_read_folio(struct file *file, struct folio *folio);
0023 static void afs_invalidate_folio(struct folio *folio, size_t offset,
0024                    size_t length);
0025 static bool afs_release_folio(struct folio *folio, gfp_t gfp_flags);
0026 
0027 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
0028 static void afs_vm_open(struct vm_area_struct *area);
0029 static void afs_vm_close(struct vm_area_struct *area);
0030 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
0031 
0032 const struct file_operations afs_file_operations = {
0033     .open       = afs_open,
0034     .release    = afs_release,
0035     .llseek     = generic_file_llseek,
0036     .read_iter  = afs_file_read_iter,
0037     .write_iter = afs_file_write,
0038     .mmap       = afs_file_mmap,
0039     .splice_read    = generic_file_splice_read,
0040     .splice_write   = iter_file_splice_write,
0041     .fsync      = afs_fsync,
0042     .lock       = afs_lock,
0043     .flock      = afs_flock,
0044 };
0045 
0046 const struct inode_operations afs_file_inode_operations = {
0047     .getattr    = afs_getattr,
0048     .setattr    = afs_setattr,
0049     .permission = afs_permission,
0050 };
0051 
0052 const struct address_space_operations afs_file_aops = {
0053     .read_folio = netfs_read_folio,
0054     .readahead  = netfs_readahead,
0055     .dirty_folio    = afs_dirty_folio,
0056     .launder_folio  = afs_launder_folio,
0057     .release_folio  = afs_release_folio,
0058     .invalidate_folio = afs_invalidate_folio,
0059     .write_begin    = afs_write_begin,
0060     .write_end  = afs_write_end,
0061     .writepage  = afs_writepage,
0062     .writepages = afs_writepages,
0063 };
0064 
0065 const struct address_space_operations afs_symlink_aops = {
0066     .read_folio = afs_symlink_read_folio,
0067     .release_folio  = afs_release_folio,
0068     .invalidate_folio = afs_invalidate_folio,
0069 };
0070 
0071 static const struct vm_operations_struct afs_vm_ops = {
0072     .open       = afs_vm_open,
0073     .close      = afs_vm_close,
0074     .fault      = filemap_fault,
0075     .map_pages  = afs_vm_map_pages,
0076     .page_mkwrite   = afs_page_mkwrite,
0077 };
0078 
0079 /*
0080  * Discard a pin on a writeback key.
0081  */
0082 void afs_put_wb_key(struct afs_wb_key *wbk)
0083 {
0084     if (wbk && refcount_dec_and_test(&wbk->usage)) {
0085         key_put(wbk->key);
0086         kfree(wbk);
0087     }
0088 }
0089 
0090 /*
0091  * Cache key for writeback.
0092  */
0093 int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
0094 {
0095     struct afs_wb_key *wbk, *p;
0096 
0097     wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
0098     if (!wbk)
0099         return -ENOMEM;
0100     refcount_set(&wbk->usage, 2);
0101     wbk->key = af->key;
0102 
0103     spin_lock(&vnode->wb_lock);
0104     list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
0105         if (p->key == wbk->key)
0106             goto found;
0107     }
0108 
0109     key_get(wbk->key);
0110     list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
0111     spin_unlock(&vnode->wb_lock);
0112     af->wb = wbk;
0113     return 0;
0114 
0115 found:
0116     refcount_inc(&p->usage);
0117     spin_unlock(&vnode->wb_lock);
0118     af->wb = p;
0119     kfree(wbk);
0120     return 0;
0121 }
0122 
0123 /*
0124  * open an AFS file or directory and attach a key to it
0125  */
0126 int afs_open(struct inode *inode, struct file *file)
0127 {
0128     struct afs_vnode *vnode = AFS_FS_I(inode);
0129     struct afs_file *af;
0130     struct key *key;
0131     int ret;
0132 
0133     _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
0134 
0135     key = afs_request_key(vnode->volume->cell);
0136     if (IS_ERR(key)) {
0137         ret = PTR_ERR(key);
0138         goto error;
0139     }
0140 
0141     af = kzalloc(sizeof(*af), GFP_KERNEL);
0142     if (!af) {
0143         ret = -ENOMEM;
0144         goto error_key;
0145     }
0146     af->key = key;
0147 
0148     ret = afs_validate(vnode, key);
0149     if (ret < 0)
0150         goto error_af;
0151 
0152     if (file->f_mode & FMODE_WRITE) {
0153         ret = afs_cache_wb_key(vnode, af);
0154         if (ret < 0)
0155             goto error_af;
0156     }
0157 
0158     if (file->f_flags & O_TRUNC)
0159         set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
0160 
0161     fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
0162 
0163     file->private_data = af;
0164     _leave(" = 0");
0165     return 0;
0166 
0167 error_af:
0168     kfree(af);
0169 error_key:
0170     key_put(key);
0171 error:
0172     _leave(" = %d", ret);
0173     return ret;
0174 }
0175 
0176 /*
0177  * release an AFS file or directory and discard its key
0178  */
0179 int afs_release(struct inode *inode, struct file *file)
0180 {
0181     struct afs_vnode_cache_aux aux;
0182     struct afs_vnode *vnode = AFS_FS_I(inode);
0183     struct afs_file *af = file->private_data;
0184     loff_t i_size;
0185     int ret = 0;
0186 
0187     _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
0188 
0189     if ((file->f_mode & FMODE_WRITE))
0190         ret = vfs_fsync(file, 0);
0191 
0192     file->private_data = NULL;
0193     if (af->wb)
0194         afs_put_wb_key(af->wb);
0195 
0196     if ((file->f_mode & FMODE_WRITE)) {
0197         i_size = i_size_read(&vnode->netfs.inode);
0198         afs_set_cache_aux(vnode, &aux);
0199         fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
0200     } else {
0201         fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
0202     }
0203 
0204     key_put(af->key);
0205     kfree(af);
0206     afs_prune_wb_keys(vnode);
0207     _leave(" = %d", ret);
0208     return ret;
0209 }
0210 
0211 /*
0212  * Allocate a new read record.
0213  */
0214 struct afs_read *afs_alloc_read(gfp_t gfp)
0215 {
0216     struct afs_read *req;
0217 
0218     req = kzalloc(sizeof(struct afs_read), gfp);
0219     if (req)
0220         refcount_set(&req->usage, 1);
0221 
0222     return req;
0223 }
0224 
0225 /*
0226  * Dispose of a ref to a read record.
0227  */
0228 void afs_put_read(struct afs_read *req)
0229 {
0230     if (refcount_dec_and_test(&req->usage)) {
0231         if (req->cleanup)
0232             req->cleanup(req);
0233         key_put(req->key);
0234         kfree(req);
0235     }
0236 }
0237 
0238 static void afs_fetch_data_notify(struct afs_operation *op)
0239 {
0240     struct afs_read *req = op->fetch.req;
0241     struct netfs_io_subrequest *subreq = req->subreq;
0242     int error = op->error;
0243 
0244     if (error == -ECONNABORTED)
0245         error = afs_abort_to_error(op->ac.abort_code);
0246     req->error = error;
0247 
0248     if (subreq) {
0249         __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
0250         netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
0251         req->subreq = NULL;
0252     } else if (req->done) {
0253         req->done(req);
0254     }
0255 }
0256 
0257 static void afs_fetch_data_success(struct afs_operation *op)
0258 {
0259     struct afs_vnode *vnode = op->file[0].vnode;
0260 
0261     _enter("op=%08x", op->debug_id);
0262     afs_vnode_commit_status(op, &op->file[0]);
0263     afs_stat_v(vnode, n_fetches);
0264     atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
0265     afs_fetch_data_notify(op);
0266 }
0267 
0268 static void afs_fetch_data_put(struct afs_operation *op)
0269 {
0270     op->fetch.req->error = op->error;
0271     afs_put_read(op->fetch.req);
0272 }
0273 
0274 static const struct afs_operation_ops afs_fetch_data_operation = {
0275     .issue_afs_rpc  = afs_fs_fetch_data,
0276     .issue_yfs_rpc  = yfs_fs_fetch_data,
0277     .success    = afs_fetch_data_success,
0278     .aborted    = afs_check_for_remote_deletion,
0279     .failed     = afs_fetch_data_notify,
0280     .put        = afs_fetch_data_put,
0281 };
0282 
0283 /*
0284  * Fetch file data from the volume.
0285  */
0286 int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
0287 {
0288     struct afs_operation *op;
0289 
0290     _enter("%s{%llx:%llu.%u},%x,,,",
0291            vnode->volume->name,
0292            vnode->fid.vid,
0293            vnode->fid.vnode,
0294            vnode->fid.unique,
0295            key_serial(req->key));
0296 
0297     op = afs_alloc_operation(req->key, vnode->volume);
0298     if (IS_ERR(op)) {
0299         if (req->subreq)
0300             netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
0301         return PTR_ERR(op);
0302     }
0303 
0304     afs_op_set_vnode(op, 0, vnode);
0305 
0306     op->fetch.req   = afs_get_read(req);
0307     op->ops     = &afs_fetch_data_operation;
0308     return afs_do_sync_operation(op);
0309 }
0310 
0311 static void afs_issue_read(struct netfs_io_subrequest *subreq)
0312 {
0313     struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
0314     struct afs_read *fsreq;
0315 
0316     fsreq = afs_alloc_read(GFP_NOFS);
0317     if (!fsreq)
0318         return netfs_subreq_terminated(subreq, -ENOMEM, false);
0319 
0320     fsreq->subreq   = subreq;
0321     fsreq->pos  = subreq->start + subreq->transferred;
0322     fsreq->len  = subreq->len   - subreq->transferred;
0323     fsreq->key  = key_get(subreq->rreq->netfs_priv);
0324     fsreq->vnode    = vnode;
0325     fsreq->iter = &fsreq->def_iter;
0326 
0327     iov_iter_xarray(&fsreq->def_iter, READ,
0328             &fsreq->vnode->netfs.inode.i_mapping->i_pages,
0329             fsreq->pos, fsreq->len);
0330 
0331     afs_fetch_data(fsreq->vnode, fsreq);
0332     afs_put_read(fsreq);
0333 }
0334 
0335 static int afs_symlink_read_folio(struct file *file, struct folio *folio)
0336 {
0337     struct afs_vnode *vnode = AFS_FS_I(folio->mapping->host);
0338     struct afs_read *fsreq;
0339     int ret;
0340 
0341     fsreq = afs_alloc_read(GFP_NOFS);
0342     if (!fsreq)
0343         return -ENOMEM;
0344 
0345     fsreq->pos  = folio_pos(folio);
0346     fsreq->len  = folio_size(folio);
0347     fsreq->vnode    = vnode;
0348     fsreq->iter = &fsreq->def_iter;
0349     iov_iter_xarray(&fsreq->def_iter, READ, &folio->mapping->i_pages,
0350             fsreq->pos, fsreq->len);
0351 
0352     ret = afs_fetch_data(fsreq->vnode, fsreq);
0353     if (ret == 0)
0354         folio_mark_uptodate(folio);
0355     folio_unlock(folio);
0356     return ret;
0357 }
0358 
0359 static int afs_init_request(struct netfs_io_request *rreq, struct file *file)
0360 {
0361     rreq->netfs_priv = key_get(afs_file_key(file));
0362     return 0;
0363 }
0364 
0365 static int afs_begin_cache_operation(struct netfs_io_request *rreq)
0366 {
0367 #ifdef CONFIG_AFS_FSCACHE
0368     struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
0369 
0370     return fscache_begin_read_operation(&rreq->cache_resources,
0371                         afs_vnode_cache(vnode));
0372 #else
0373     return -ENOBUFS;
0374 #endif
0375 }
0376 
0377 static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
0378                  struct folio **foliop, void **_fsdata)
0379 {
0380     struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
0381 
0382     return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
0383 }
0384 
0385 static void afs_free_request(struct netfs_io_request *rreq)
0386 {
0387     key_put(rreq->netfs_priv);
0388 }
0389 
0390 const struct netfs_request_ops afs_req_ops = {
0391     .init_request       = afs_init_request,
0392     .free_request       = afs_free_request,
0393     .begin_cache_operation  = afs_begin_cache_operation,
0394     .check_write_begin  = afs_check_write_begin,
0395     .issue_read     = afs_issue_read,
0396 };
0397 
0398 int afs_write_inode(struct inode *inode, struct writeback_control *wbc)
0399 {
0400     fscache_unpin_writeback(wbc, afs_vnode_cache(AFS_FS_I(inode)));
0401     return 0;
0402 }
0403 
0404 /*
0405  * Adjust the dirty region of the page on truncation or full invalidation,
0406  * getting rid of the markers altogether if the region is entirely invalidated.
0407  */
0408 static void afs_invalidate_dirty(struct folio *folio, size_t offset,
0409                  size_t length)
0410 {
0411     struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
0412     unsigned long priv;
0413     unsigned int f, t, end = offset + length;
0414 
0415     priv = (unsigned long)folio_get_private(folio);
0416 
0417     /* we clean up only if the entire page is being invalidated */
0418     if (offset == 0 && length == folio_size(folio))
0419         goto full_invalidate;
0420 
0421      /* If the page was dirtied by page_mkwrite(), the PTE stays writable
0422       * and we don't get another notification to tell us to expand it
0423       * again.
0424       */
0425     if (afs_is_folio_dirty_mmapped(priv))
0426         return;
0427 
0428     /* We may need to shorten the dirty region */
0429     f = afs_folio_dirty_from(folio, priv);
0430     t = afs_folio_dirty_to(folio, priv);
0431 
0432     if (t <= offset || f >= end)
0433         return; /* Doesn't overlap */
0434 
0435     if (f < offset && t > end)
0436         return; /* Splits the dirty region - just absorb it */
0437 
0438     if (f >= offset && t <= end)
0439         goto undirty;
0440 
0441     if (f < offset)
0442         t = offset;
0443     else
0444         f = end;
0445     if (f == t)
0446         goto undirty;
0447 
0448     priv = afs_folio_dirty(folio, f, t);
0449     folio_change_private(folio, (void *)priv);
0450     trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio);
0451     return;
0452 
0453 undirty:
0454     trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio);
0455     folio_clear_dirty_for_io(folio);
0456 full_invalidate:
0457     trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio);
0458     folio_detach_private(folio);
0459 }
0460 
0461 /*
0462  * invalidate part or all of a page
0463  * - release a page and clean up its private data if offset is 0 (indicating
0464  *   the entire page)
0465  */
0466 static void afs_invalidate_folio(struct folio *folio, size_t offset,
0467                    size_t length)
0468 {
0469     _enter("{%lu},%zu,%zu", folio->index, offset, length);
0470 
0471     BUG_ON(!folio_test_locked(folio));
0472 
0473     if (folio_get_private(folio))
0474         afs_invalidate_dirty(folio, offset, length);
0475 
0476     folio_wait_fscache(folio);
0477     _leave("");
0478 }
0479 
0480 /*
0481  * release a page and clean up its private state if it's not busy
0482  * - return true if the page can now be released, false if not
0483  */
0484 static bool afs_release_folio(struct folio *folio, gfp_t gfp)
0485 {
0486     struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
0487 
0488     _enter("{{%llx:%llu}[%lu],%lx},%x",
0489            vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags,
0490            gfp);
0491 
0492     /* deny if folio is being written to the cache and the caller hasn't
0493      * elected to wait */
0494 #ifdef CONFIG_AFS_FSCACHE
0495     if (folio_test_fscache(folio)) {
0496         if (current_is_kswapd() || !(gfp & __GFP_FS))
0497             return false;
0498         folio_wait_fscache(folio);
0499     }
0500     fscache_note_page_release(afs_vnode_cache(vnode));
0501 #endif
0502 
0503     if (folio_test_private(folio)) {
0504         trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio);
0505         folio_detach_private(folio);
0506     }
0507 
0508     /* Indicate that the folio can be released */
0509     _leave(" = T");
0510     return true;
0511 }
0512 
0513 static void afs_add_open_mmap(struct afs_vnode *vnode)
0514 {
0515     if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
0516         down_write(&vnode->volume->cell->fs_open_mmaps_lock);
0517 
0518         if (list_empty(&vnode->cb_mmap_link))
0519             list_add_tail(&vnode->cb_mmap_link,
0520                       &vnode->volume->cell->fs_open_mmaps);
0521 
0522         up_write(&vnode->volume->cell->fs_open_mmaps_lock);
0523     }
0524 }
0525 
0526 static void afs_drop_open_mmap(struct afs_vnode *vnode)
0527 {
0528     if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
0529         return;
0530 
0531     down_write(&vnode->volume->cell->fs_open_mmaps_lock);
0532 
0533     if (atomic_read(&vnode->cb_nr_mmap) == 0)
0534         list_del_init(&vnode->cb_mmap_link);
0535 
0536     up_write(&vnode->volume->cell->fs_open_mmaps_lock);
0537     flush_work(&vnode->cb_work);
0538 }
0539 
0540 /*
0541  * Handle setting up a memory mapping on an AFS file.
0542  */
0543 static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
0544 {
0545     struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
0546     int ret;
0547 
0548     afs_add_open_mmap(vnode);
0549 
0550     ret = generic_file_mmap(file, vma);
0551     if (ret == 0)
0552         vma->vm_ops = &afs_vm_ops;
0553     else
0554         afs_drop_open_mmap(vnode);
0555     return ret;
0556 }
0557 
0558 static void afs_vm_open(struct vm_area_struct *vma)
0559 {
0560     afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
0561 }
0562 
0563 static void afs_vm_close(struct vm_area_struct *vma)
0564 {
0565     afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
0566 }
0567 
0568 static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
0569 {
0570     struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
0571     struct afs_file *af = vmf->vma->vm_file->private_data;
0572 
0573     switch (afs_validate(vnode, af->key)) {
0574     case 0:
0575         return filemap_map_pages(vmf, start_pgoff, end_pgoff);
0576     case -ENOMEM:
0577         return VM_FAULT_OOM;
0578     case -EINTR:
0579     case -ERESTARTSYS:
0580         return VM_FAULT_RETRY;
0581     case -ESTALE:
0582     default:
0583         return VM_FAULT_SIGBUS;
0584     }
0585 }
0586 
0587 static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
0588 {
0589     struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
0590     struct afs_file *af = iocb->ki_filp->private_data;
0591     int ret;
0592 
0593     ret = afs_validate(vnode, af->key);
0594     if (ret < 0)
0595         return ret;
0596 
0597     return generic_file_read_iter(iocb, iter);
0598 }