linux/fs/afs/file.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/* AFS filesystem file handling
   3 *
   4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
   5 * Written by David Howells (dhowells@redhat.com)
   6 */
   7
   8#include <linux/kernel.h>
   9#include <linux/module.h>
  10#include <linux/init.h>
  11#include <linux/fs.h>
  12#include <linux/pagemap.h>
  13#include <linux/writeback.h>
  14#include <linux/gfp.h>
  15#include <linux/task_io_accounting_ops.h>
  16#include <linux/mm.h>
  17#include <linux/netfs.h>
  18#include "internal.h"
  19
  20static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
  21static int afs_readpage(struct file *file, struct page *page);
  22static void afs_invalidatepage(struct page *page, unsigned int offset,
  23                               unsigned int length);
  24static int afs_releasepage(struct page *page, gfp_t gfp_flags);
  25
  26static void afs_readahead(struct readahead_control *ractl);
  27static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
  28static void afs_vm_open(struct vm_area_struct *area);
  29static void afs_vm_close(struct vm_area_struct *area);
  30static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
  31
  32const struct file_operations afs_file_operations = {
  33        .open           = afs_open,
  34        .release        = afs_release,
  35        .llseek         = generic_file_llseek,
  36        .read_iter      = afs_file_read_iter,
  37        .write_iter     = afs_file_write,
  38        .mmap           = afs_file_mmap,
  39        .splice_read    = generic_file_splice_read,
  40        .splice_write   = iter_file_splice_write,
  41        .fsync          = afs_fsync,
  42        .lock           = afs_lock,
  43        .flock          = afs_flock,
  44};
  45
  46const struct inode_operations afs_file_inode_operations = {
  47        .getattr        = afs_getattr,
  48        .setattr        = afs_setattr,
  49        .permission     = afs_permission,
  50};
  51
  52const struct address_space_operations afs_fs_aops = {
  53        .readpage       = afs_readpage,
  54        .readahead      = afs_readahead,
  55        .set_page_dirty = afs_set_page_dirty,
  56        .launder_page   = afs_launder_page,
  57        .releasepage    = afs_releasepage,
  58        .invalidatepage = afs_invalidatepage,
  59        .write_begin    = afs_write_begin,
  60        .write_end      = afs_write_end,
  61        .writepage      = afs_writepage,
  62        .writepages     = afs_writepages,
  63};
  64
  65static const struct vm_operations_struct afs_vm_ops = {
  66        .open           = afs_vm_open,
  67        .close          = afs_vm_close,
  68        .fault          = filemap_fault,
  69        .map_pages      = afs_vm_map_pages,
  70        .page_mkwrite   = afs_page_mkwrite,
  71};
  72
  73/*
  74 * Discard a pin on a writeback key.
  75 */
  76void afs_put_wb_key(struct afs_wb_key *wbk)
  77{
  78        if (wbk && refcount_dec_and_test(&wbk->usage)) {
  79                key_put(wbk->key);
  80                kfree(wbk);
  81        }
  82}
  83
  84/*
  85 * Cache key for writeback.
  86 */
  87int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
  88{
  89        struct afs_wb_key *wbk, *p;
  90
  91        wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
  92        if (!wbk)
  93                return -ENOMEM;
  94        refcount_set(&wbk->usage, 2);
  95        wbk->key = af->key;
  96
  97        spin_lock(&vnode->wb_lock);
  98        list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
  99                if (p->key == wbk->key)
 100                        goto found;
 101        }
 102
 103        key_get(wbk->key);
 104        list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
 105        spin_unlock(&vnode->wb_lock);
 106        af->wb = wbk;
 107        return 0;
 108
 109found:
 110        refcount_inc(&p->usage);
 111        spin_unlock(&vnode->wb_lock);
 112        af->wb = p;
 113        kfree(wbk);
 114        return 0;
 115}
 116
 117/*
 118 * open an AFS file or directory and attach a key to it
 119 */
 120int afs_open(struct inode *inode, struct file *file)
 121{
 122        struct afs_vnode *vnode = AFS_FS_I(inode);
 123        struct afs_file *af;
 124        struct key *key;
 125        int ret;
 126
 127        _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
 128
 129        key = afs_request_key(vnode->volume->cell);
 130        if (IS_ERR(key)) {
 131                ret = PTR_ERR(key);
 132                goto error;
 133        }
 134
 135        af = kzalloc(sizeof(*af), GFP_KERNEL);
 136        if (!af) {
 137                ret = -ENOMEM;
 138                goto error_key;
 139        }
 140        af->key = key;
 141
 142        ret = afs_validate(vnode, key);
 143        if (ret < 0)
 144                goto error_af;
 145
 146        if (file->f_mode & FMODE_WRITE) {
 147                ret = afs_cache_wb_key(vnode, af);
 148                if (ret < 0)
 149                        goto error_af;
 150        }
 151
 152        if (file->f_flags & O_TRUNC)
 153                set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
 154        
 155        file->private_data = af;
 156        _leave(" = 0");
 157        return 0;
 158
 159error_af:
 160        kfree(af);
 161error_key:
 162        key_put(key);
 163error:
 164        _leave(" = %d", ret);
 165        return ret;
 166}
 167
 168/*
 169 * release an AFS file or directory and discard its key
 170 */
 171int afs_release(struct inode *inode, struct file *file)
 172{
 173        struct afs_vnode *vnode = AFS_FS_I(inode);
 174        struct afs_file *af = file->private_data;
 175        int ret = 0;
 176
 177        _enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
 178
 179        if ((file->f_mode & FMODE_WRITE))
 180                ret = vfs_fsync(file, 0);
 181
 182        file->private_data = NULL;
 183        if (af->wb)
 184                afs_put_wb_key(af->wb);
 185        key_put(af->key);
 186        kfree(af);
 187        afs_prune_wb_keys(vnode);
 188        _leave(" = %d", ret);
 189        return ret;
 190}
 191
 192/*
 193 * Allocate a new read record.
 194 */
 195struct afs_read *afs_alloc_read(gfp_t gfp)
 196{
 197        struct afs_read *req;
 198
 199        req = kzalloc(sizeof(struct afs_read), gfp);
 200        if (req)
 201                refcount_set(&req->usage, 1);
 202
 203        return req;
 204}
 205
 206/*
 207 * Dispose of a ref to a read record.
 208 */
 209void afs_put_read(struct afs_read *req)
 210{
 211        if (refcount_dec_and_test(&req->usage)) {
 212                if (req->cleanup)
 213                        req->cleanup(req);
 214                key_put(req->key);
 215                kfree(req);
 216        }
 217}
 218
 219static void afs_fetch_data_notify(struct afs_operation *op)
 220{
 221        struct afs_read *req = op->fetch.req;
 222        struct netfs_read_subrequest *subreq = req->subreq;
 223        int error = op->error;
 224
 225        if (error == -ECONNABORTED)
 226                error = afs_abort_to_error(op->ac.abort_code);
 227        req->error = error;
 228
 229        if (subreq) {
 230                __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
 231                netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
 232                req->subreq = NULL;
 233        } else if (req->done) {
 234                req->done(req);
 235        }
 236}
 237
 238static void afs_fetch_data_success(struct afs_operation *op)
 239{
 240        struct afs_vnode *vnode = op->file[0].vnode;
 241
 242        _enter("op=%08x", op->debug_id);
 243        afs_vnode_commit_status(op, &op->file[0]);
 244        afs_stat_v(vnode, n_fetches);
 245        atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
 246        afs_fetch_data_notify(op);
 247}
 248
 249static void afs_fetch_data_put(struct afs_operation *op)
 250{
 251        op->fetch.req->error = op->error;
 252        afs_put_read(op->fetch.req);
 253}
 254
 255static const struct afs_operation_ops afs_fetch_data_operation = {
 256        .issue_afs_rpc  = afs_fs_fetch_data,
 257        .issue_yfs_rpc  = yfs_fs_fetch_data,
 258        .success        = afs_fetch_data_success,
 259        .aborted        = afs_check_for_remote_deletion,
 260        .failed         = afs_fetch_data_notify,
 261        .put            = afs_fetch_data_put,
 262};
 263
 264/*
 265 * Fetch file data from the volume.
 266 */
 267int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
 268{
 269        struct afs_operation *op;
 270
 271        _enter("%s{%llx:%llu.%u},%x,,,",
 272               vnode->volume->name,
 273               vnode->fid.vid,
 274               vnode->fid.vnode,
 275               vnode->fid.unique,
 276               key_serial(req->key));
 277
 278        op = afs_alloc_operation(req->key, vnode->volume);
 279        if (IS_ERR(op)) {
 280                if (req->subreq)
 281                        netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
 282                return PTR_ERR(op);
 283        }
 284
 285        afs_op_set_vnode(op, 0, vnode);
 286
 287        op->fetch.req   = afs_get_read(req);
 288        op->ops         = &afs_fetch_data_operation;
 289        return afs_do_sync_operation(op);
 290}
 291
 292static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
 293{
 294        struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
 295        struct afs_read *fsreq;
 296
 297        fsreq = afs_alloc_read(GFP_NOFS);
 298        if (!fsreq)
 299                return netfs_subreq_terminated(subreq, -ENOMEM, false);
 300
 301        fsreq->subreq   = subreq;
 302        fsreq->pos      = subreq->start + subreq->transferred;
 303        fsreq->len      = subreq->len   - subreq->transferred;
 304        fsreq->key      = key_get(subreq->rreq->netfs_priv);
 305        fsreq->vnode    = vnode;
 306        fsreq->iter     = &fsreq->def_iter;
 307
 308        iov_iter_xarray(&fsreq->def_iter, READ,
 309                        &fsreq->vnode->vfs_inode.i_mapping->i_pages,
 310                        fsreq->pos, fsreq->len);
 311
 312        afs_fetch_data(fsreq->vnode, fsreq);
 313        afs_put_read(fsreq);
 314}
 315
 316static int afs_symlink_readpage(struct page *page)
 317{
 318        struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
 319        struct afs_read *fsreq;
 320        int ret;
 321
 322        fsreq = afs_alloc_read(GFP_NOFS);
 323        if (!fsreq)
 324                return -ENOMEM;
 325
 326        fsreq->pos      = page->index * PAGE_SIZE;
 327        fsreq->len      = PAGE_SIZE;
 328        fsreq->vnode    = vnode;
 329        fsreq->iter     = &fsreq->def_iter;
 330        iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
 331                        fsreq->pos, fsreq->len);
 332
 333        ret = afs_fetch_data(fsreq->vnode, fsreq);
 334        page_endio(page, false, ret);
 335        return ret;
 336}
 337
 338static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
 339{
 340        rreq->netfs_priv = key_get(afs_file_key(file));
 341}
 342
 343static bool afs_is_cache_enabled(struct inode *inode)
 344{
 345        struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
 346
 347        return fscache_cookie_enabled(cookie) && !hlist_empty(&cookie->backing_objects);
 348}
 349
 350static int afs_begin_cache_operation(struct netfs_read_request *rreq)
 351{
 352        struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
 353
 354        return fscache_begin_read_operation(rreq, afs_vnode_cache(vnode));
 355}
 356
 357static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
 358                                 struct page *page, void **_fsdata)
 359{
 360        struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
 361
 362        return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
 363}
 364
 365static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
 366{
 367        key_put(netfs_priv);
 368}
 369
 370const struct netfs_read_request_ops afs_req_ops = {
 371        .init_rreq              = afs_init_rreq,
 372        .is_cache_enabled       = afs_is_cache_enabled,
 373        .begin_cache_operation  = afs_begin_cache_operation,
 374        .check_write_begin      = afs_check_write_begin,
 375        .issue_op               = afs_req_issue_op,
 376        .cleanup                = afs_priv_cleanup,
 377};
 378
 379static int afs_readpage(struct file *file, struct page *page)
 380{
 381        if (!file)
 382                return afs_symlink_readpage(page);
 383
 384        return netfs_readpage(file, page, &afs_req_ops, NULL);
 385}
 386
 387static void afs_readahead(struct readahead_control *ractl)
 388{
 389        netfs_readahead(ractl, &afs_req_ops, NULL);
 390}
 391
 392/*
 393 * Adjust the dirty region of the page on truncation or full invalidation,
 394 * getting rid of the markers altogether if the region is entirely invalidated.
 395 */
 396static void afs_invalidate_dirty(struct page *page, unsigned int offset,
 397                                 unsigned int length)
 398{
 399        struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
 400        unsigned long priv;
 401        unsigned int f, t, end = offset + length;
 402
 403        priv = page_private(page);
 404
 405        /* we clean up only if the entire page is being invalidated */
 406        if (offset == 0 && length == thp_size(page))
 407                goto full_invalidate;
 408
 409         /* If the page was dirtied by page_mkwrite(), the PTE stays writable
 410          * and we don't get another notification to tell us to expand it
 411          * again.
 412          */
 413        if (afs_is_page_dirty_mmapped(priv))
 414                return;
 415
 416        /* We may need to shorten the dirty region */
 417        f = afs_page_dirty_from(page, priv);
 418        t = afs_page_dirty_to(page, priv);
 419
 420        if (t <= offset || f >= end)
 421                return; /* Doesn't overlap */
 422
 423        if (f < offset && t > end)
 424                return; /* Splits the dirty region - just absorb it */
 425
 426        if (f >= offset && t <= end)
 427                goto undirty;
 428
 429        if (f < offset)
 430                t = offset;
 431        else
 432                f = end;
 433        if (f == t)
 434                goto undirty;
 435
 436        priv = afs_page_dirty(page, f, t);
 437        set_page_private(page, priv);
 438        trace_afs_page_dirty(vnode, tracepoint_string("trunc"), page);
 439        return;
 440
 441undirty:
 442        trace_afs_page_dirty(vnode, tracepoint_string("undirty"), page);
 443        clear_page_dirty_for_io(page);
 444full_invalidate:
 445        trace_afs_page_dirty(vnode, tracepoint_string("inval"), page);
 446        detach_page_private(page);
 447}
 448
 449/*
 450 * invalidate part or all of a page
 451 * - release a page and clean up its private data if offset is 0 (indicating
 452 *   the entire page)
 453 */
 454static void afs_invalidatepage(struct page *page, unsigned int offset,
 455                               unsigned int length)
 456{
 457        _enter("{%lu},%u,%u", page->index, offset, length);
 458
 459        BUG_ON(!PageLocked(page));
 460
 461        if (PagePrivate(page))
 462                afs_invalidate_dirty(page, offset, length);
 463
 464        wait_on_page_fscache(page);
 465        _leave("");
 466}
 467
 468/*
 469 * release a page and clean up its private state if it's not busy
 470 * - return true if the page can now be released, false if not
 471 */
 472static int afs_releasepage(struct page *page, gfp_t gfp_flags)
 473{
 474        struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
 475
 476        _enter("{{%llx:%llu}[%lu],%lx},%x",
 477               vnode->fid.vid, vnode->fid.vnode, page->index, page->flags,
 478               gfp_flags);
 479
 480        /* deny if page is being written to the cache and the caller hasn't
 481         * elected to wait */
 482#ifdef CONFIG_AFS_FSCACHE
 483        if (PageFsCache(page)) {
 484                if (!(gfp_flags & __GFP_DIRECT_RECLAIM) || !(gfp_flags & __GFP_FS))
 485                        return false;
 486                wait_on_page_fscache(page);
 487        }
 488#endif
 489
 490        if (PagePrivate(page)) {
 491                trace_afs_page_dirty(vnode, tracepoint_string("rel"), page);
 492                detach_page_private(page);
 493        }
 494
 495        /* indicate that the page can be released */
 496        _leave(" = T");
 497        return 1;
 498}
 499
 500static void afs_add_open_mmap(struct afs_vnode *vnode)
 501{
 502        if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
 503                down_write(&vnode->volume->cell->fs_open_mmaps_lock);
 504
 505                list_add_tail(&vnode->cb_mmap_link,
 506                              &vnode->volume->cell->fs_open_mmaps);
 507
 508                up_write(&vnode->volume->cell->fs_open_mmaps_lock);
 509        }
 510}
 511
 512static void afs_drop_open_mmap(struct afs_vnode *vnode)
 513{
 514        if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
 515                return;
 516
 517        down_write(&vnode->volume->cell->fs_open_mmaps_lock);
 518
 519        if (atomic_read(&vnode->cb_nr_mmap) == 0)
 520                list_del_init(&vnode->cb_mmap_link);
 521
 522        up_write(&vnode->volume->cell->fs_open_mmaps_lock);
 523        flush_work(&vnode->cb_work);
 524}
 525
 526/*
 527 * Handle setting up a memory mapping on an AFS file.
 528 */
 529static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
 530{
 531        struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
 532        int ret;
 533
 534        afs_add_open_mmap(vnode);
 535
 536        ret = generic_file_mmap(file, vma);
 537        if (ret == 0)
 538                vma->vm_ops = &afs_vm_ops;
 539        else
 540                afs_drop_open_mmap(vnode);
 541        return ret;
 542}
 543
 544static void afs_vm_open(struct vm_area_struct *vma)
 545{
 546        afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
 547}
 548
 549static void afs_vm_close(struct vm_area_struct *vma)
 550{
 551        afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
 552}
 553
 554static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
 555{
 556        struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
 557        struct afs_file *af = vmf->vma->vm_file->private_data;
 558
 559        switch (afs_validate(vnode, af->key)) {
 560        case 0:
 561                return filemap_map_pages(vmf, start_pgoff, end_pgoff);
 562        case -ENOMEM:
 563                return VM_FAULT_OOM;
 564        case -EINTR:
 565        case -ERESTARTSYS:
 566                return VM_FAULT_RETRY;
 567        case -ESTALE:
 568        default:
 569                return VM_FAULT_SIGBUS;
 570        }
 571}
 572
 573static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 574{
 575        struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
 576        struct afs_file *af = iocb->ki_filp->private_data;
 577        int ret;
 578
 579        ret = afs_validate(vnode, af->key);
 580        if (ret < 0)
 581                return ret;
 582
 583        return generic_file_read_iter(iocb, iter);
 584}
 585