mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-27 04:54:41 +08:00
8834147f95
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEqG5UsNXhtOCrfGQP+7dXa6fLC2sFAmHeBGsACgkQ+7dXa6fL C2tyLw/8C2Gs/XvOZvRO7KPetKI9BbQSFoCe7uvGbiPq5CEmgcjWzQxvQGklBiZD qYa6pMNye1iGpsHOY3Yu210b7vMQiRLnnxvVle0UrjpZR7CcxYS0gGV+6yRdbDGy W1X6GFiX06qiNsgBH4msYp0SmbhhfkTyAx1BeBZAEtX8iFgaPfOldPY2nLMcTDD6 6FT1nTzRcMHx9IUQZJtpeatzc70Qg8+fOr2UAY2nOIypXh6+vAMBO80xtUjGVU+1 pWD1E+8cXSLfwEEzquFWoWTsTX7hNfsesEN10FmBf1bVCH9ZDFE01MOl6B8+CkFl +xfkvDNFC3yyUwAMVAV4+A4Be+cVLSqN2R91QIKJnAj9w1OjxASrwZJ1YeZp6KP4 h0XKuPs3sRwwbNPVL/nP0UPNexoJnOUAaHesl4uKkRrExmxz9xGOIqIri2+tUIO+ HkGyNns1huymj1K1ja4AQbDiZZX39GgYVleyg9g3uuy1FS4k+/myJcXo/CqWn3ON 4oeNwxwLvlcqIQnPrESvwev50lFZYB4pfwvez6T2C5dL/Wk/xdeJK9iG81RWgx7y 5XcDeoGDE08gMCGWVPjuhOCXypeiRGHhRNlcxTtq5kLwBZGkcYg/wFFnWn+6hzc4 kyXw2kS5WZq4Q/FPh7BdY0eHp6xv0EpAOZwceneLB9lhNINdxcQ= =ISJ6 -----END PGP SIGNATURE----- Merge tag 'fscache-rewrite-20220111' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs Pull fscache rewrite from David Howells: "This is a set of patches that rewrites the fscache driver and the cachefiles driver, significantly simplifying the code compared to what's upstream, removing the complex operation scheduling and object state machine in favour of something much smaller and simpler. The series is structured such that the first few patches disable fscache use by the network filesystems using it, remove the cachefiles driver entirely and as much of the fscache driver as can be got away with without causing build failures in the network filesystems. The patches after that recreate fscache and then cachefiles, attempting to add the pieces in a logical order. Finally, the filesystems are reenabled and then the very last patch changes the documentation. [!] Note: I have dropped the cifs patch for the moment, leaving local caching in cifs disabled. I've been having trouble getting that working. I think I have it done, but it needs more testing (there seem to be some test failures occurring with v5.16 also from xfstests), so I propose deferring that patch to the end of the merge window. WHY REWRITE? ============ Fscache's operation scheduling API was intended to handle sequencing of cache operations, which were all required (where possible) to run asynchronously in parallel with the operations being done by the network filesystem, whilst allowing the cache to be brought online and offline and to interrupt service for invalidation. With the advent of the tmpfile capacity in the VFS, however, an opportunity arises to do invalidation much more simply, without having to wait for I/O that's actually in progress: Cachefiles can simply create a tmpfile, cut over the file pointer for the backing object attached to a cookie and abandon the in-progress I/O, dismissing it upon completion. Future work here would involve using Omar Sandoval's vfs_link() with AT_LINK_REPLACE[1] to allow an extant file to be displaced by a new hard link from a tmpfile as currently I have to unlink the old file first. These patches can also simplify the object state handling as I/O operations to the cache don't all have to be brought to a stop in order to invalidate a file. To that end, and with an eye on to writing a new backing cache model in the future, I've taken the opportunity to simplify the indexing structure. I've separated the index cookie concept from the file cookie concept by C type now. The former is now called a "volume cookie" (struct fscache_volume) and there is a container of file cookies. There are then just the two levels. All the index cookie levels are collapsed into a single volume cookie, and this has a single printable string as a key. For instance, an AFS volume would have a key of something like "afs,example.com,1000555", combining the filesystem name, cell name and volume ID. This is freeform, but must not have '/' chars in it. I've also eliminated all pointers back from fscache into the network filesystem. This required the duplication of a little bit of data in the cookie (cookie key, coherency data and file size), but it's not actually that much. This gets rid of problems with making sure we keep netfs data structures around so that the cache can access them. These patches mean that most of the code that was in the drivers before is simply gone and those drivers are now almost entirely new code. That being the case, there doesn't seem any particular reason to try and maintain bisectability across it. Further, there has to be a point in the middle where things are cut over as there's a single point everything has to go through (ie. /dev/cachefiles) and it can't be in use by two drivers at once. ISSUES YET OUTSTANDING ====================== There are some issues still outstanding, unaddressed by this patchset, that will need fixing in future patchsets, but that don't stop this series from being usable: (1) The cachefiles driver needs to stop using the backing filesystem's metadata to store information about what parts of the cache are populated. This is not reliable with modern extent-based filesystems. Fixing this is deferred to a separate patchset as it involves negotiation with the network filesystem and the VM as to how much data to download to fulfil a read - which brings me on to (2)... (2) NFS (and CIFS with the dropped patch) do not take account of how the cache would like I/O to be structured to meet its granularity requirements. Previously, the cache used page granularity, which was fine as the network filesystems also dealt in page granularity, and the backing filesystem (ext4, xfs or whatever) did whatever it did out of sight. However, we now have folios to deal with and the cache will now have to store its own metadata to track its contents. The change I'm looking at making for cachefiles is to store content bitmaps in one or more xattrs and making a bit in the map correspond to something like a 256KiB block. However, the size of an xattr and the fact that they have to be read/updated in one go means that I'm looking at covering 1GiB of data per 512-byte map and storing each map in an xattr. Cachefiles has the potential to grow into a fully fledged filesystem of its very own if I'm not careful. However, I'm also looking at changing things even more radically and going to a different model of how the cache is arranged and managed - one that's more akin to the way, say, openafs does things - which brings me on to (3)... (3) The way cachefilesd does culling is very inefficient for large caches and it would be better to move it into the kernel if I can as cachefilesd has to keep asking the kernel if it can cull a file. Changing the way the backend works would allow this to be addressed. BITS THAT MAY BE CONTROVERSIAL ============================== There are some bits I've added that may be controversial: (1) I've provided a flag, S_KERNEL_FILE, that cachefiles uses to check if a files is already being used by some other kernel service (e.g. a duplicate cachefiles cache in the same directory) and reject it if it is. This isn't entirely necessary, but it helps prevent accidental data corruption. I don't want to use S_SWAPFILE as that has other effects, but quite possibly swapon() should set S_KERNEL_FILE too. Note that it doesn't prevent userspace from interfering, though perhaps it should. (I have made it prevent a marked directory from being rmdir-able). (2) Cachefiles wants to keep the backing file for a cookie open whilst we might need to write to it from network filesystem writeback. The problem is that the network filesystem unuses its cookie when its file is closed, and so we have nothing pinning the cachefiles file open and it will get closed automatically after a short time to avoid EMFILE/ENFILE problems. Reopening the cache file, however, is a problem if this is being done due to writeback triggered by exit(). Some filesystems will oops if we try to open a file in that context because they want to access current->fs or suchlike. To get around this, I added the following: (A) An inode flag, I_PINNING_FSCACHE_WB, to be set on a network filesystem inode to indicate that we have a usage count on the cookie caching that inode. (B) A flag in struct writeback_control, unpinned_fscache_wb, that is set when __writeback_single_inode() clears the last dirty page from i_pages - at which point it clears I_PINNING_FSCACHE_WB and sets this flag. This has to be done here so that clearing I_PINNING_FSCACHE_WB can be done atomically with the check of PAGECACHE_TAG_DIRTY that clears I_DIRTY_PAGES. (C) A function, fscache_set_page_dirty(), which if it is not set, sets I_PINNING_FSCACHE_WB and calls fscache_use_cookie() to pin the cache resources. (D) A function, fscache_unpin_writeback(), to be called by ->write_inode() to unuse the cookie. (E) A function, fscache_clear_inode_writeback(), to be called when the inode is evicted, before clear_inode() is called. This cleans up any lingering I_PINNING_FSCACHE_WB. The network filesystem can then use these tools to make sure that fscache_write_to_cache() can write locally modified data to the cache as well as to the server. For the future, I'm working on write helpers for netfs lib that should allow this facility to be removed by keeping track of the dirty regions separately - but that's incomplete at the moment and is also going to be affected by folios, one way or another, since it deals with pages" Link: https://lore.kernel.org/all/510611.1641942444@warthog.procyon.org.uk/ Tested-by: Dominique Martinet <asmadeus@codewreck.org> # 9p Tested-by: kafs-testing@auristor.com # afs Tested-by: Jeff Layton <jlayton@kernel.org> # ceph Tested-by: Dave Wysochanski <dwysocha@redhat.com> # nfs Tested-by: Daire Byrne <daire@dneg.com> # nfs * tag 'fscache-rewrite-20220111' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs: (67 commits) 9p, afs, ceph, nfs: Use current_is_kswapd() rather than gfpflags_allow_blocking() fscache: Add a tracepoint for cookie use/unuse fscache: Rewrite documentation ceph: add fscache writeback support ceph: conversion to new fscache API nfs: Implement cache I/O by accessing the cache directly nfs: Convert to new fscache volume/cookie API 9p: Copy local writes to the cache when writing to the server 9p: Use fscache indexing rewrite and reenable caching afs: Skip truncation on the server of data we haven't written yet afs: Copy local writes to the cache when writing to the server afs: Convert afs to use the new fscache API fscache, cachefiles: Display stat of culling events fscache, cachefiles: Display stats of no-space events cachefiles: Allow cachefiles to actually function fscache, cachefiles: Store the volume coherency data cachefiles: Implement the I/O routines cachefiles: Implement cookie resize for truncate cachefiles: Implement begin and end I/O operation cachefiles: Implement backing file wrangling ...
624 lines
15 KiB
C
624 lines
15 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* AFS filesystem file handling
|
|
*
|
|
* Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/init.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/pagemap.h>
|
|
#include <linux/writeback.h>
|
|
#include <linux/gfp.h>
|
|
#include <linux/task_io_accounting_ops.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/swap.h>
|
|
#include <linux/netfs.h>
|
|
#include "internal.h"
|
|
|
|
static int afs_file_mmap(struct file *file, struct vm_area_struct *vma);
|
|
static int afs_readpage(struct file *file, struct page *page);
|
|
static int afs_symlink_readpage(struct file *file, struct page *page);
|
|
static void afs_invalidatepage(struct page *page, unsigned int offset,
|
|
unsigned int length);
|
|
static int afs_releasepage(struct page *page, gfp_t gfp_flags);
|
|
|
|
static void afs_readahead(struct readahead_control *ractl);
|
|
static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
|
|
static void afs_vm_open(struct vm_area_struct *area);
|
|
static void afs_vm_close(struct vm_area_struct *area);
|
|
static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff);
|
|
|
|
const struct file_operations afs_file_operations = {
|
|
.open = afs_open,
|
|
.release = afs_release,
|
|
.llseek = generic_file_llseek,
|
|
.read_iter = afs_file_read_iter,
|
|
.write_iter = afs_file_write,
|
|
.mmap = afs_file_mmap,
|
|
.splice_read = generic_file_splice_read,
|
|
.splice_write = iter_file_splice_write,
|
|
.fsync = afs_fsync,
|
|
.lock = afs_lock,
|
|
.flock = afs_flock,
|
|
};
|
|
|
|
const struct inode_operations afs_file_inode_operations = {
|
|
.getattr = afs_getattr,
|
|
.setattr = afs_setattr,
|
|
.permission = afs_permission,
|
|
};
|
|
|
|
const struct address_space_operations afs_file_aops = {
|
|
.readpage = afs_readpage,
|
|
.readahead = afs_readahead,
|
|
.set_page_dirty = afs_set_page_dirty,
|
|
.launder_page = afs_launder_page,
|
|
.releasepage = afs_releasepage,
|
|
.invalidatepage = afs_invalidatepage,
|
|
.write_begin = afs_write_begin,
|
|
.write_end = afs_write_end,
|
|
.writepage = afs_writepage,
|
|
.writepages = afs_writepages,
|
|
};
|
|
|
|
const struct address_space_operations afs_symlink_aops = {
|
|
.readpage = afs_symlink_readpage,
|
|
.releasepage = afs_releasepage,
|
|
.invalidatepage = afs_invalidatepage,
|
|
};
|
|
|
|
static const struct vm_operations_struct afs_vm_ops = {
|
|
.open = afs_vm_open,
|
|
.close = afs_vm_close,
|
|
.fault = filemap_fault,
|
|
.map_pages = afs_vm_map_pages,
|
|
.page_mkwrite = afs_page_mkwrite,
|
|
};
|
|
|
|
/*
|
|
* Discard a pin on a writeback key.
|
|
*/
|
|
void afs_put_wb_key(struct afs_wb_key *wbk)
|
|
{
|
|
if (wbk && refcount_dec_and_test(&wbk->usage)) {
|
|
key_put(wbk->key);
|
|
kfree(wbk);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Cache key for writeback.
|
|
*/
|
|
int afs_cache_wb_key(struct afs_vnode *vnode, struct afs_file *af)
|
|
{
|
|
struct afs_wb_key *wbk, *p;
|
|
|
|
wbk = kzalloc(sizeof(struct afs_wb_key), GFP_KERNEL);
|
|
if (!wbk)
|
|
return -ENOMEM;
|
|
refcount_set(&wbk->usage, 2);
|
|
wbk->key = af->key;
|
|
|
|
spin_lock(&vnode->wb_lock);
|
|
list_for_each_entry(p, &vnode->wb_keys, vnode_link) {
|
|
if (p->key == wbk->key)
|
|
goto found;
|
|
}
|
|
|
|
key_get(wbk->key);
|
|
list_add_tail(&wbk->vnode_link, &vnode->wb_keys);
|
|
spin_unlock(&vnode->wb_lock);
|
|
af->wb = wbk;
|
|
return 0;
|
|
|
|
found:
|
|
refcount_inc(&p->usage);
|
|
spin_unlock(&vnode->wb_lock);
|
|
af->wb = p;
|
|
kfree(wbk);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* open an AFS file or directory and attach a key to it
|
|
*/
|
|
int afs_open(struct inode *inode, struct file *file)
|
|
{
|
|
struct afs_vnode *vnode = AFS_FS_I(inode);
|
|
struct afs_file *af;
|
|
struct key *key;
|
|
int ret;
|
|
|
|
_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
|
|
|
|
key = afs_request_key(vnode->volume->cell);
|
|
if (IS_ERR(key)) {
|
|
ret = PTR_ERR(key);
|
|
goto error;
|
|
}
|
|
|
|
af = kzalloc(sizeof(*af), GFP_KERNEL);
|
|
if (!af) {
|
|
ret = -ENOMEM;
|
|
goto error_key;
|
|
}
|
|
af->key = key;
|
|
|
|
ret = afs_validate(vnode, key);
|
|
if (ret < 0)
|
|
goto error_af;
|
|
|
|
if (file->f_mode & FMODE_WRITE) {
|
|
ret = afs_cache_wb_key(vnode, af);
|
|
if (ret < 0)
|
|
goto error_af;
|
|
}
|
|
|
|
if (file->f_flags & O_TRUNC)
|
|
set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
|
|
|
|
fscache_use_cookie(afs_vnode_cache(vnode), file->f_mode & FMODE_WRITE);
|
|
|
|
file->private_data = af;
|
|
_leave(" = 0");
|
|
return 0;
|
|
|
|
error_af:
|
|
kfree(af);
|
|
error_key:
|
|
key_put(key);
|
|
error:
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* release an AFS file or directory and discard its key
|
|
*/
|
|
int afs_release(struct inode *inode, struct file *file)
|
|
{
|
|
struct afs_vnode_cache_aux aux;
|
|
struct afs_vnode *vnode = AFS_FS_I(inode);
|
|
struct afs_file *af = file->private_data;
|
|
loff_t i_size;
|
|
int ret = 0;
|
|
|
|
_enter("{%llx:%llu},", vnode->fid.vid, vnode->fid.vnode);
|
|
|
|
if ((file->f_mode & FMODE_WRITE))
|
|
ret = vfs_fsync(file, 0);
|
|
|
|
file->private_data = NULL;
|
|
if (af->wb)
|
|
afs_put_wb_key(af->wb);
|
|
|
|
if ((file->f_mode & FMODE_WRITE)) {
|
|
i_size = i_size_read(&vnode->vfs_inode);
|
|
afs_set_cache_aux(vnode, &aux);
|
|
fscache_unuse_cookie(afs_vnode_cache(vnode), &aux, &i_size);
|
|
} else {
|
|
fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
|
|
}
|
|
|
|
key_put(af->key);
|
|
kfree(af);
|
|
afs_prune_wb_keys(vnode);
|
|
_leave(" = %d", ret);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Allocate a new read record.
|
|
*/
|
|
struct afs_read *afs_alloc_read(gfp_t gfp)
|
|
{
|
|
struct afs_read *req;
|
|
|
|
req = kzalloc(sizeof(struct afs_read), gfp);
|
|
if (req)
|
|
refcount_set(&req->usage, 1);
|
|
|
|
return req;
|
|
}
|
|
|
|
/*
|
|
* Dispose of a ref to a read record.
|
|
*/
|
|
void afs_put_read(struct afs_read *req)
|
|
{
|
|
if (refcount_dec_and_test(&req->usage)) {
|
|
if (req->cleanup)
|
|
req->cleanup(req);
|
|
key_put(req->key);
|
|
kfree(req);
|
|
}
|
|
}
|
|
|
|
static void afs_fetch_data_notify(struct afs_operation *op)
|
|
{
|
|
struct afs_read *req = op->fetch.req;
|
|
struct netfs_read_subrequest *subreq = req->subreq;
|
|
int error = op->error;
|
|
|
|
if (error == -ECONNABORTED)
|
|
error = afs_abort_to_error(op->ac.abort_code);
|
|
req->error = error;
|
|
|
|
if (subreq) {
|
|
__set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
|
|
netfs_subreq_terminated(subreq, error ?: req->actual_len, false);
|
|
req->subreq = NULL;
|
|
} else if (req->done) {
|
|
req->done(req);
|
|
}
|
|
}
|
|
|
|
static void afs_fetch_data_success(struct afs_operation *op)
|
|
{
|
|
struct afs_vnode *vnode = op->file[0].vnode;
|
|
|
|
_enter("op=%08x", op->debug_id);
|
|
afs_vnode_commit_status(op, &op->file[0]);
|
|
afs_stat_v(vnode, n_fetches);
|
|
atomic_long_add(op->fetch.req->actual_len, &op->net->n_fetch_bytes);
|
|
afs_fetch_data_notify(op);
|
|
}
|
|
|
|
static void afs_fetch_data_put(struct afs_operation *op)
|
|
{
|
|
op->fetch.req->error = op->error;
|
|
afs_put_read(op->fetch.req);
|
|
}
|
|
|
|
static const struct afs_operation_ops afs_fetch_data_operation = {
|
|
.issue_afs_rpc = afs_fs_fetch_data,
|
|
.issue_yfs_rpc = yfs_fs_fetch_data,
|
|
.success = afs_fetch_data_success,
|
|
.aborted = afs_check_for_remote_deletion,
|
|
.failed = afs_fetch_data_notify,
|
|
.put = afs_fetch_data_put,
|
|
};
|
|
|
|
/*
|
|
* Fetch file data from the volume.
|
|
*/
|
|
int afs_fetch_data(struct afs_vnode *vnode, struct afs_read *req)
|
|
{
|
|
struct afs_operation *op;
|
|
|
|
_enter("%s{%llx:%llu.%u},%x,,,",
|
|
vnode->volume->name,
|
|
vnode->fid.vid,
|
|
vnode->fid.vnode,
|
|
vnode->fid.unique,
|
|
key_serial(req->key));
|
|
|
|
op = afs_alloc_operation(req->key, vnode->volume);
|
|
if (IS_ERR(op)) {
|
|
if (req->subreq)
|
|
netfs_subreq_terminated(req->subreq, PTR_ERR(op), false);
|
|
return PTR_ERR(op);
|
|
}
|
|
|
|
afs_op_set_vnode(op, 0, vnode);
|
|
|
|
op->fetch.req = afs_get_read(req);
|
|
op->ops = &afs_fetch_data_operation;
|
|
return afs_do_sync_operation(op);
|
|
}
|
|
|
|
static void afs_req_issue_op(struct netfs_read_subrequest *subreq)
|
|
{
|
|
struct afs_vnode *vnode = AFS_FS_I(subreq->rreq->inode);
|
|
struct afs_read *fsreq;
|
|
|
|
fsreq = afs_alloc_read(GFP_NOFS);
|
|
if (!fsreq)
|
|
return netfs_subreq_terminated(subreq, -ENOMEM, false);
|
|
|
|
fsreq->subreq = subreq;
|
|
fsreq->pos = subreq->start + subreq->transferred;
|
|
fsreq->len = subreq->len - subreq->transferred;
|
|
fsreq->key = key_get(subreq->rreq->netfs_priv);
|
|
fsreq->vnode = vnode;
|
|
fsreq->iter = &fsreq->def_iter;
|
|
|
|
iov_iter_xarray(&fsreq->def_iter, READ,
|
|
&fsreq->vnode->vfs_inode.i_mapping->i_pages,
|
|
fsreq->pos, fsreq->len);
|
|
|
|
afs_fetch_data(fsreq->vnode, fsreq);
|
|
afs_put_read(fsreq);
|
|
}
|
|
|
|
static int afs_symlink_readpage(struct file *file, struct page *page)
|
|
{
|
|
struct afs_vnode *vnode = AFS_FS_I(page->mapping->host);
|
|
struct afs_read *fsreq;
|
|
struct folio *folio = page_folio(page);
|
|
int ret;
|
|
|
|
fsreq = afs_alloc_read(GFP_NOFS);
|
|
if (!fsreq)
|
|
return -ENOMEM;
|
|
|
|
fsreq->pos = folio_pos(folio);
|
|
fsreq->len = folio_size(folio);
|
|
fsreq->vnode = vnode;
|
|
fsreq->iter = &fsreq->def_iter;
|
|
iov_iter_xarray(&fsreq->def_iter, READ, &page->mapping->i_pages,
|
|
fsreq->pos, fsreq->len);
|
|
|
|
ret = afs_fetch_data(fsreq->vnode, fsreq);
|
|
if (ret == 0)
|
|
SetPageUptodate(page);
|
|
unlock_page(page);
|
|
return ret;
|
|
}
|
|
|
|
static void afs_init_rreq(struct netfs_read_request *rreq, struct file *file)
|
|
{
|
|
rreq->netfs_priv = key_get(afs_file_key(file));
|
|
}
|
|
|
|
static bool afs_is_cache_enabled(struct inode *inode)
|
|
{
|
|
struct fscache_cookie *cookie = afs_vnode_cache(AFS_FS_I(inode));
|
|
|
|
return fscache_cookie_enabled(cookie) && cookie->cache_priv;
|
|
}
|
|
|
|
static int afs_begin_cache_operation(struct netfs_read_request *rreq)
|
|
{
|
|
#ifdef CONFIG_AFS_FSCACHE
|
|
struct afs_vnode *vnode = AFS_FS_I(rreq->inode);
|
|
|
|
return fscache_begin_read_operation(&rreq->cache_resources,
|
|
afs_vnode_cache(vnode));
|
|
#else
|
|
return -ENOBUFS;
|
|
#endif
|
|
}
|
|
|
|
static int afs_check_write_begin(struct file *file, loff_t pos, unsigned len,
|
|
struct folio *folio, void **_fsdata)
|
|
{
|
|
struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
|
|
|
|
return test_bit(AFS_VNODE_DELETED, &vnode->flags) ? -ESTALE : 0;
|
|
}
|
|
|
|
static void afs_priv_cleanup(struct address_space *mapping, void *netfs_priv)
|
|
{
|
|
key_put(netfs_priv);
|
|
}
|
|
|
|
const struct netfs_read_request_ops afs_req_ops = {
|
|
.init_rreq = afs_init_rreq,
|
|
.is_cache_enabled = afs_is_cache_enabled,
|
|
.begin_cache_operation = afs_begin_cache_operation,
|
|
.check_write_begin = afs_check_write_begin,
|
|
.issue_op = afs_req_issue_op,
|
|
.cleanup = afs_priv_cleanup,
|
|
};
|
|
|
|
static int afs_readpage(struct file *file, struct page *page)
|
|
{
|
|
struct folio *folio = page_folio(page);
|
|
|
|
return netfs_readpage(file, folio, &afs_req_ops, NULL);
|
|
}
|
|
|
|
static void afs_readahead(struct readahead_control *ractl)
|
|
{
|
|
netfs_readahead(ractl, &afs_req_ops, NULL);
|
|
}
|
|
|
|
int afs_write_inode(struct inode *inode, struct writeback_control *wbc)
|
|
{
|
|
fscache_unpin_writeback(wbc, afs_vnode_cache(AFS_FS_I(inode)));
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Adjust the dirty region of the page on truncation or full invalidation,
|
|
* getting rid of the markers altogether if the region is entirely invalidated.
|
|
*/
|
|
static void afs_invalidate_dirty(struct folio *folio, unsigned int offset,
|
|
unsigned int length)
|
|
{
|
|
struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
|
|
unsigned long priv;
|
|
unsigned int f, t, end = offset + length;
|
|
|
|
priv = (unsigned long)folio_get_private(folio);
|
|
|
|
/* we clean up only if the entire page is being invalidated */
|
|
if (offset == 0 && length == folio_size(folio))
|
|
goto full_invalidate;
|
|
|
|
/* If the page was dirtied by page_mkwrite(), the PTE stays writable
|
|
* and we don't get another notification to tell us to expand it
|
|
* again.
|
|
*/
|
|
if (afs_is_folio_dirty_mmapped(priv))
|
|
return;
|
|
|
|
/* We may need to shorten the dirty region */
|
|
f = afs_folio_dirty_from(folio, priv);
|
|
t = afs_folio_dirty_to(folio, priv);
|
|
|
|
if (t <= offset || f >= end)
|
|
return; /* Doesn't overlap */
|
|
|
|
if (f < offset && t > end)
|
|
return; /* Splits the dirty region - just absorb it */
|
|
|
|
if (f >= offset && t <= end)
|
|
goto undirty;
|
|
|
|
if (f < offset)
|
|
t = offset;
|
|
else
|
|
f = end;
|
|
if (f == t)
|
|
goto undirty;
|
|
|
|
priv = afs_folio_dirty(folio, f, t);
|
|
folio_change_private(folio, (void *)priv);
|
|
trace_afs_folio_dirty(vnode, tracepoint_string("trunc"), folio);
|
|
return;
|
|
|
|
undirty:
|
|
trace_afs_folio_dirty(vnode, tracepoint_string("undirty"), folio);
|
|
folio_clear_dirty_for_io(folio);
|
|
full_invalidate:
|
|
trace_afs_folio_dirty(vnode, tracepoint_string("inval"), folio);
|
|
folio_detach_private(folio);
|
|
}
|
|
|
|
/*
|
|
* invalidate part or all of a page
|
|
* - release a page and clean up its private data if offset is 0 (indicating
|
|
* the entire page)
|
|
*/
|
|
static void afs_invalidatepage(struct page *page, unsigned int offset,
|
|
unsigned int length)
|
|
{
|
|
struct folio *folio = page_folio(page);
|
|
|
|
_enter("{%lu},%u,%u", folio_index(folio), offset, length);
|
|
|
|
BUG_ON(!PageLocked(page));
|
|
|
|
if (PagePrivate(page))
|
|
afs_invalidate_dirty(folio, offset, length);
|
|
|
|
folio_wait_fscache(folio);
|
|
_leave("");
|
|
}
|
|
|
|
/*
|
|
* release a page and clean up its private state if it's not busy
|
|
* - return true if the page can now be released, false if not
|
|
*/
|
|
static int afs_releasepage(struct page *page, gfp_t gfp)
|
|
{
|
|
struct folio *folio = page_folio(page);
|
|
struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
|
|
|
|
_enter("{{%llx:%llu}[%lu],%lx},%x",
|
|
vnode->fid.vid, vnode->fid.vnode, folio_index(folio), folio->flags,
|
|
gfp);
|
|
|
|
/* deny if page is being written to the cache and the caller hasn't
|
|
* elected to wait */
|
|
#ifdef CONFIG_AFS_FSCACHE
|
|
if (folio_test_fscache(folio)) {
|
|
if (current_is_kswapd() || !(gfp & __GFP_FS))
|
|
return false;
|
|
folio_wait_fscache(folio);
|
|
}
|
|
fscache_note_page_release(afs_vnode_cache(vnode));
|
|
#endif
|
|
|
|
if (folio_test_private(folio)) {
|
|
trace_afs_folio_dirty(vnode, tracepoint_string("rel"), folio);
|
|
folio_detach_private(folio);
|
|
}
|
|
|
|
/* Indicate that the folio can be released */
|
|
_leave(" = T");
|
|
return true;
|
|
}
|
|
|
|
static void afs_add_open_mmap(struct afs_vnode *vnode)
|
|
{
|
|
if (atomic_inc_return(&vnode->cb_nr_mmap) == 1) {
|
|
down_write(&vnode->volume->cell->fs_open_mmaps_lock);
|
|
|
|
if (list_empty(&vnode->cb_mmap_link))
|
|
list_add_tail(&vnode->cb_mmap_link,
|
|
&vnode->volume->cell->fs_open_mmaps);
|
|
|
|
up_write(&vnode->volume->cell->fs_open_mmaps_lock);
|
|
}
|
|
}
|
|
|
|
static void afs_drop_open_mmap(struct afs_vnode *vnode)
|
|
{
|
|
if (!atomic_dec_and_test(&vnode->cb_nr_mmap))
|
|
return;
|
|
|
|
down_write(&vnode->volume->cell->fs_open_mmaps_lock);
|
|
|
|
if (atomic_read(&vnode->cb_nr_mmap) == 0)
|
|
list_del_init(&vnode->cb_mmap_link);
|
|
|
|
up_write(&vnode->volume->cell->fs_open_mmaps_lock);
|
|
flush_work(&vnode->cb_work);
|
|
}
|
|
|
|
/*
|
|
* Handle setting up a memory mapping on an AFS file.
|
|
*/
|
|
static int afs_file_mmap(struct file *file, struct vm_area_struct *vma)
|
|
{
|
|
struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
|
|
int ret;
|
|
|
|
afs_add_open_mmap(vnode);
|
|
|
|
ret = generic_file_mmap(file, vma);
|
|
if (ret == 0)
|
|
vma->vm_ops = &afs_vm_ops;
|
|
else
|
|
afs_drop_open_mmap(vnode);
|
|
return ret;
|
|
}
|
|
|
|
static void afs_vm_open(struct vm_area_struct *vma)
|
|
{
|
|
afs_add_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
|
|
}
|
|
|
|
static void afs_vm_close(struct vm_area_struct *vma)
|
|
{
|
|
afs_drop_open_mmap(AFS_FS_I(file_inode(vma->vm_file)));
|
|
}
|
|
|
|
static vm_fault_t afs_vm_map_pages(struct vm_fault *vmf, pgoff_t start_pgoff, pgoff_t end_pgoff)
|
|
{
|
|
struct afs_vnode *vnode = AFS_FS_I(file_inode(vmf->vma->vm_file));
|
|
struct afs_file *af = vmf->vma->vm_file->private_data;
|
|
|
|
switch (afs_validate(vnode, af->key)) {
|
|
case 0:
|
|
return filemap_map_pages(vmf, start_pgoff, end_pgoff);
|
|
case -ENOMEM:
|
|
return VM_FAULT_OOM;
|
|
case -EINTR:
|
|
case -ERESTARTSYS:
|
|
return VM_FAULT_RETRY;
|
|
case -ESTALE:
|
|
default:
|
|
return VM_FAULT_SIGBUS;
|
|
}
|
|
}
|
|
|
|
static ssize_t afs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
|
|
{
|
|
struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
|
|
struct afs_file *af = iocb->ki_filp->private_data;
|
|
int ret;
|
|
|
|
ret = afs_validate(vnode, af->key);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
return generic_file_read_iter(iocb, iter);
|
|
}
|