// SPDX-License-Identifier: GPL-2.0 /* * The NFSD open file cache. * * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com> * * An nfsd_file object is a per-file collection of open state that binds * together: * - a struct file * * - a user credential * - a network namespace * - a read-ahead context * - monitoring for writeback errors * * nfsd_file objects are reference-counted. Consumers acquire a new * object via the nfsd_file_acquire API. They manage their interest in * the acquired object, and hence the object's reference count, via * nfsd_file_get and nfsd_file_put. There are two varieties of nfsd_file * object: * * * non-garbage-collected: When a consumer wants to precisely control * the lifetime of a file's open state, it acquires a non-garbage- * collected nfsd_file. The final nfsd_file_put releases the open * state immediately. * * * garbage-collected: When a consumer does not control the lifetime * of open state, it acquires a garbage-collected nfsd_file. The * final nfsd_file_put allows the open state to linger for a period * during which it may be re-used.
*/
/* * If the add was successful, then return the object. * Otherwise, we need to put the reference we hold on the * nfm_mark. The fsnotify code will take a reference and put * it on failure, so we can't just free it directly. It's also * not safe to call fsnotify_destroy_mark on it as the * mark->group will be NULL. Thus, we can't let the nfm_ref * counter drive the destruction at this point.
*/ if (likely(!err))
nfm = new; else
fsnotify_put_mark(&new->nfm_mark);
} while (unlikely(err == -EEXIST));
return nfm;
}
staticstruct nfsd_file *
nfsd_file_alloc(struct net *net, struct inode *inode, unsignedchar need, bool want_gc)
{ struct nfsd_file *nf;
nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL); if (unlikely(!nf)) return NULL;
/** * nfsd_file_check_write_error - check for writeback errors on a file * @nf: nfsd_file to check for writeback errors * * Check whether a nfsd_file has an unseen error. Reset the write * verifier if so.
*/ staticvoid
nfsd_file_check_write_error(struct nfsd_file *nf)
{ struct file *file = nf->nf_file;
if ((file->f_mode & FMODE_WRITE) &&
filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)))
nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
}
nfsd_file_unhash(nf); if (nf->nf_mark)
nfsd_file_mark_put(nf->nf_mark); if (nf->nf_file) {
nfsd_file_check_write_error(nf);
nfsd_filp_close(nf->nf_file);
}
/* * If this item is still linked via nf_lru, that's a bug. * WARN and leak it to preserve system stability.
*/ if (WARN_ON_ONCE(!list_empty(&nf->nf_lru))) return;
/* File not open for write? */ if (!(file->f_mode & FMODE_WRITE)) returnfalse;
/* * Some filesystems (e.g. NFS) flush all dirty data on close. * On others, there is no need to wait for writeback.
*/ if (!(file_inode(file)->i_sb->s_export_op->flags & EXPORT_OP_FLUSH_ON_CLOSE)) returnfalse;
/** * nfsd_file_put - put the reference to a nfsd_file * @nf: nfsd_file of which to put the reference * * Put a reference to a nfsd_file. In the non-GC case, we just put the * reference immediately. In the GC case, if the reference would be * the last one, the put it on the LRU instead to be cleaned up later.
*/ void
nfsd_file_put(struct nfsd_file *nf)
{
might_sleep();
trace_nfsd_file_put(nf);
if (refcount_dec_and_test(&nf->nf_ref))
nfsd_file_free(nf);
}
/** * nfsd_file_put_local - put nfsd_file reference and arm nfsd_net_put in caller * @pnf: nfsd_file of which to put the reference * * First save the associated net to return to caller, then put * the reference of the nfsd_file.
*/ struct net *
nfsd_file_put_local(struct nfsd_file __rcu **pnf)
{ struct nfsd_file *nf; struct net *net = NULL;
nf = unrcu_pointer(xchg(pnf, NULL)); if (nf) {
net = nf->nf_net;
nfsd_file_put(nf);
} return net;
}
/** * nfsd_file_get_local - get nfsd_file reference and reference to net * @nf: nfsd_file of which to put the reference * * Get reference to both the nfsd_file and nf->nf_net.
*/ struct nfsd_file *
nfsd_file_get_local(struct nfsd_file *nf)
{ struct net *net = nf->nf_net;
if (nfsd_net_try_get(net)) {
nf = nfsd_file_get(nf); if (!nf)
nfsd_net_put(net);
} else {
nf = NULL;
} return nf;
}
/** * nfsd_file_file - get the backing file of an nfsd_file * @nf: nfsd_file of which to access the backing file. * * Return backing file for @nf.
*/ struct file *
nfsd_file_file(struct nfsd_file *nf)
{ return nf->nf_file;
}
/** * nfsd_file_dispose_list_delayed - move list of dead files to net's freeme list * @dispose: list of nfsd_files to be disposed * * Transfers each file to the "freeme" list for its nfsd_net, to eventually * be disposed of by the per-net garbage collector.
*/ staticvoid
nfsd_file_dispose_list_delayed(struct list_head *dispose)
{ while(!list_empty(dispose)) { struct nfsd_file *nf = list_first_entry(dispose, struct nfsd_file, nf_gc); struct nfsd_net *nn = net_generic(nf->nf_net, nfsd_net_id); struct nfsd_fcache_disposal *l = nn->fcache_disposal; struct svc_serv *serv;
/* * The filecache laundrette is shut down after the * nn->nfsd_serv pointer is cleared, but before the * svc_serv is freed.
*/
serv = nn->nfsd_serv; if (serv)
svc_wake_up(serv);
}
}
/** * nfsd_file_net_dispose - deal with nfsd_files waiting to be disposed. * @nn: nfsd_net in which to find files to be disposed. * * When files held open for nfsv3 are removed from the filecache, whether * due to memory pressure or garbage collection, they are queued to * a per-net-ns queue. This function completes the disposal, either * directly or by waking another nfsd thread to help with the work.
*/ void nfsd_file_net_dispose(struct nfsd_net *nn)
{ struct nfsd_fcache_disposal *l = nn->fcache_disposal;
if (!list_empty(&l->freeme)) {
LIST_HEAD(dispose); int i;
spin_lock(&l->lock); for (i = 0; i < 8 && !list_empty(&l->freeme); i++)
list_move(l->freeme.next, &dispose);
spin_unlock(&l->lock); if (!list_empty(&l->freeme)) /* Wake up another thread to share the work * *before* doing any actual disposing.
*/
svc_wake_up(nn->nfsd_serv);
nfsd_file_dispose_list(&dispose);
}
}
/** * nfsd_file_lru_cb - Examine an entry on the LRU list * @item: LRU entry to examine * @lru: controlling LRU * @arg: dispose list * * Return values: * %LRU_REMOVED: @item was removed from the LRU * %LRU_ROTATE: @item is to be moved to the LRU tail * %LRU_SKIP: @item cannot be evicted
*/ staticenum lru_status
nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru, void *arg)
{ struct list_head *head = arg; struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
/* We should only be dealing with GC entries here */
WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags));
/* * Don't throw out files that are still undergoing I/O or * that have uncleared errors pending.
*/ if (nfsd_file_check_writeback(nf)) {
trace_nfsd_file_gc_writeback(nf); return LRU_SKIP;
}
/* If it was recently added to the list, skip it */ if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
trace_nfsd_file_gc_referenced(nf); return LRU_ROTATE;
}
/* * Put the reference held on behalf of the LRU if it is the last * reference, else rotate.
*/ if (!refcount_dec_if_one(&nf->nf_ref)) {
trace_nfsd_file_gc_in_use(nf); return LRU_ROTATE;
}
/* Refcount went to zero. Unhash it and queue it to the dispose list */
nfsd_file_unhash(nf);
list_lru_isolate(lru, &nf->nf_lru);
list_add(&nf->nf_gc, head);
this_cpu_inc(nfsd_file_evictions);
trace_nfsd_file_gc_disposed(nf); return LRU_REMOVED;
}
if (test_and_clear_bit(NFSD_FILE_RECENT, &nf->nf_flags)) { /* * "REFERENCED" really means "should be at the end of the * LRU. As we are putting it there we can clear the flag.
*/
clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
trace_nfsd_file_gc_aged(nf); return LRU_ROTATE;
} return nfsd_file_lru_cb(item, lru, arg);
}
/* If the shrinker runs between calls to list_lru_walk_node() in * nfsd_file_gc(), the "remaining" count will be wrong. This could * result in premature freeing of some files. This may not matter much * but is easy to fix with this spinlock which temporarily disables * the shrinker.
*/ static DEFINE_SPINLOCK(nfsd_gc_lock); staticvoid
nfsd_file_gc(void)
{ unsignedlong ret = 0;
LIST_HEAD(dispose); int nid;
/** * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file * @nf: nfsd_file to attempt to queue * @dispose: private list to queue successfully-put objects * * Unhash an nfsd_file, try to get a reference to it, and then put that * reference. If it's the last reference, queue it to the dispose list.
*/ staticvoid
nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose)
__must_hold(RCU)
{ int decrement = 1;
/* If we raced with someone else unhashing, ignore it */ if (!nfsd_file_unhash(nf)) return;
/* If we can't get a reference, ignore it */ if (!nfsd_file_get(nf)) return;
/* Extra decrement if we remove from the LRU */ if (nfsd_file_lru_remove(nf))
++decrement;
/* If refcount goes to 0, then put on the dispose list */ if (refcount_sub_and_test(decrement, &nf->nf_ref)) {
list_add(&nf->nf_gc, dispose);
trace_nfsd_file_closing(nf);
}
}
/** * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode * @inode: inode on which to close out nfsd_files * @dispose: list on which to gather nfsd_files to close out * * An nfsd_file represents a struct file being held open on behalf of nfsd. * An open file however can block other activity (such as leases), or cause * undesirable behavior (e.g. spurious silly-renames when reexporting NFS). * * This function is intended to find open nfsd_files when this sort of * conflicting access occurs and then attempt to close those files out. * * Populates the dispose list with entries that have already had their * refcounts go to zero. The actual free of an nfsd_file can be expensive, * so we leave it up to the caller whether it wants to wait or not.
*/ staticvoid
nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose)
{ struct rhlist_head *tmp, *list; struct nfsd_file *nf;
rcu_read_lock();
list = rhltable_lookup(&nfsd_file_rhltable, &inode,
nfsd_file_rhash_params);
rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) { if (!test_bit(NFSD_FILE_GC, &nf->nf_flags)) continue;
nfsd_file_cond_queue(nf, dispose);
}
rcu_read_unlock();
}
/** * nfsd_file_close_inode - attempt a delayed close of a nfsd_file * @inode: inode of the file to attempt to remove * * Close out any open nfsd_files that can be reaped for @inode. The * actual freeing is deferred to the dispose_list_delayed infrastructure. * * This is used by the fsnotify callbacks and setlease notifier.
*/ staticvoid
nfsd_file_close_inode(struct inode *inode)
{
LIST_HEAD(dispose);
/** * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file * @inode: inode of the file to attempt to remove * * Close out any open nfsd_files that can be reaped for @inode. The * nfsd_files are closed out synchronously. * * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames * when reexporting NFS.
*/ void
nfsd_file_close_inode_sync(struct inode *inode)
{
LIST_HEAD(dispose);
/** * __nfsd_file_cache_purge: clean out the cache for shutdown * @net: net-namespace to shut down the cache (may be NULL) * * Walk the nfsd_file cache and close out any that match @net. If @net is NULL, * then close out everything. Called when an nfsd instance is being shut down, * and when the exports table is flushed.
*/ staticvoid
__nfsd_file_cache_purge(struct net *net)
{ struct rhashtable_iter iter; struct nfsd_file *nf;
LIST_HEAD(dispose);
list = rhltable_lookup(&nfsd_file_rhltable, &inode,
nfsd_file_rhash_params);
rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) { if (nf->nf_may != need) continue; if (nf->nf_net != net) continue; if (!nfsd_match_cred(nf->nf_cred, cred)) continue; if (test_bit(NFSD_FILE_GC, &nf->nf_flags) != want_gc) continue; if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) continue;
if (!nfsd_file_get(nf)) continue; return nf;
} return NULL;
}
/** * nfsd_file_is_cached - are there any cached open files for this inode? * @inode: inode to check * * The lookup matches inodes in all net namespaces and is atomic wrt * nfsd_file_acquire(). * * Return values: * %true: filecache contains at least one file matching this inode * %false: filecache contains no files matching this inode
*/ bool
nfsd_file_is_cached(struct inode *inode)
{ struct rhlist_head *tmp, *list; struct nfsd_file *nf; bool ret = false;
rcu_read_lock();
list = rhltable_lookup(&nfsd_file_rhltable, &inode,
nfsd_file_rhash_params);
rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) if (test_bit(NFSD_FILE_GC, &nf->nf_flags)) {
ret = true; break;
}
rcu_read_unlock();
/* Did construction of this file fail? */ if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
trace_nfsd_file_cons_err(rqstp, inode, may_flags, nf); if (!open_retry) {
status = nfserr_jukebox; goto construction_err;
}
nfsd_file_put(nf);
open_retry = false;
fh_put(fhp); goto retry;
}
this_cpu_inc(nfsd_file_cache_hits);
status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags)); if (status != nfs_ok) {
nfsd_file_put(nf);
nf = NULL;
}
open_file:
trace_nfsd_file_alloc(nf);
nf->nf_mark = nfsd_file_mark_find_or_create(inode); if (nf->nf_mark) { if (file) {
get_file(file);
nf->nf_file = file;
status = nfs_ok;
trace_nfsd_file_opened(nf, status);
} else {
ret = nfsd_open_verified(fhp, may_flags, &nf->nf_file); if (ret == -EOPENSTALE && stale_retry) {
stale_retry = false;
nfsd_file_unhash(nf);
clear_and_wake_up_bit(NFSD_FILE_PENDING,
&nf->nf_flags); if (refcount_dec_and_test(&nf->nf_ref))
nfsd_file_free(nf);
nf = NULL;
fh_put(fhp); goto retry;
}
status = nfserrno(ret);
trace_nfsd_file_open(nf, status); if (status == nfs_ok)
status = nfsd_file_get_dio_attrs(fhp, nf);
}
} else
status = nfserr_jukebox; /* * If construction failed, or we raced with a call to unlink() * then unhash.
*/ if (status != nfs_ok || inode->i_nlink == 0)
nfsd_file_unhash(nf); elseif (want_gc)
nfsd_file_lru_add(nf);
clear_and_wake_up_bit(NFSD_FILE_PENDING, &nf->nf_flags); if (status == nfs_ok) goto out;
construction_err: if (refcount_dec_and_test(&nf->nf_ref))
nfsd_file_free(nf);
nf = NULL; goto out;
}
/** * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file * @rqstp: the RPC transaction being executed * @fhp: the NFS filehandle of the file to be opened * @may_flags: NFSD_MAY_ settings for the file * @pnf: OUT: new or found "struct nfsd_file" object * * The nfsd_file object returned by this API is reference-counted * and garbage-collected. The object is retained for a few * seconds after the final nfsd_file_put() in case the caller * wants to re-use it. * * Return values: * %nfs_ok - @pnf points to an nfsd_file with its reference * count boosted. * * On error, an nfsstat value in network byte order is returned.
*/
__be32
nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp, unsignedint may_flags, struct nfsd_file **pnf)
{ return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
fhp, may_flags, NULL, pnf, true);
}
/** * nfsd_file_acquire - Get a struct nfsd_file with an open file * @rqstp: the RPC transaction being executed * @fhp: the NFS filehandle of the file to be opened * @may_flags: NFSD_MAY_ settings for the file * @pnf: OUT: new or found "struct nfsd_file" object * * The nfsd_file_object returned by this API is reference-counted * but not garbage-collected. The object is unhashed after the * final nfsd_file_put(). * * Return values: * %nfs_ok - @pnf points to an nfsd_file with its reference * count boosted. * * On error, an nfsstat value in network byte order is returned.
*/
__be32
nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, unsignedint may_flags, struct nfsd_file **pnf)
{ return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
fhp, may_flags, NULL, pnf, false);
}
/** * nfsd_file_acquire_local - Get a struct nfsd_file with an open file for localio * @net: The network namespace in which to perform a lookup * @cred: the user credential with which to validate access * @client: the auth_domain for LOCALIO lookup * @fhp: the NFS filehandle of the file to be opened * @may_flags: NFSD_MAY_ settings for the file * @pnf: OUT: new or found "struct nfsd_file" object * * This file lookup interface provide access to a file given the * filehandle and credential. No connection-based authorisation * is performed and in that way it is quite different to other * file access mediated by nfsd. It allows a kernel module such as the NFS * client to reach across network and filesystem namespaces to access * a file. The security implications of this should be carefully * considered before use. * * The nfsd_file_object returned by this API is reference-counted * but not garbage-collected. The object is unhashed after the * final nfsd_file_put(). * * Return values: * %nfs_ok - @pnf points to an nfsd_file with its reference * count boosted. * * On error, an nfsstat value in network byte order is returned.
*/
__be32
nfsd_file_acquire_local(struct net *net, struct svc_cred *cred, struct auth_domain *client, struct svc_fh *fhp, unsignedint may_flags, struct nfsd_file **pnf)
{ /* * Save creds before calling nfsd_file_do_acquire() (which calls * nfsd_setuser). Important because caller (LOCALIO) is from * client context.
*/ conststruct cred *save_cred = get_current_cred();
__be32 beres;
/** * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file * @rqstp: the RPC transaction being executed * @fhp: the NFS filehandle of the file just created * @may_flags: NFSD_MAY_ settings for the file * @file: cached, already-open file (may be NULL) * @pnf: OUT: new or found "struct nfsd_file" object * * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist, * and @file is non-NULL, use it to instantiate a new nfsd_file instead of * opening a new one. * * Return values: * %nfs_ok - @pnf points to an nfsd_file with its reference * count boosted. * * On error, an nfsstat value in network byte order is returned.
*/
__be32
nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp, unsignedint may_flags, struct file *file, struct nfsd_file **pnf)
{ return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL,
fhp, may_flags, file, pnf, false);
}
/* * Note that fields may be added, removed or reordered in the future. Programs * scraping this file for info should test the labels to ensure they're * getting the correct field.
*/ int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
{ unsignedlong allocations = 0, releases = 0, evictions = 0; unsignedlong hits = 0, acquisitions = 0; unsignedint i, count = 0, buckets = 0; unsignedlong lru = 0, total_age = 0;
/* Serialize with server shutdown */
mutex_lock(&nfsd_mutex); if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) { struct bucket_table *tbl; struct rhashtable *ht;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.