// SPDX-License-Identifier: GPL-2.0-only /* * linux/fs/nfs/dir.c * * Copyright (C) 1992 Rick Sladkey * * nfs directory handling functions * * 10 Apr 1996 Added silly rename for unlink --okir * 28 Sep 1996 Improved directory cache --okir * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de * Re-implemented silly rename for unlink, newly implemented * silly rename for nfs_rename() following the suggestions * of Olaf Kirch (okir) found in this file. * Following Linus comments on my original hack, this version * depends only on the dcache stuff and doesn't touch the inode * layer (iput() and friends). * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
*/
/* * we are freeing strings created by nfs_add_to_readdir_array()
*/ staticvoid nfs_readdir_clear_array(struct folio *folio)
{ struct nfs_cache_array *array; unsignedint i;
array = kmap_local_folio(folio, 0); for (i = 0; i < array->size; i++)
kfree(array->array[i].name);
array->size = 0;
kunmap_local(array);
}
/* * the caller is responsible for freeing qstr.name * when called by nfs_readdir_add_to_array, the strings will be freed in * nfs_clear_readdir_array()
*/ staticconstchar *nfs_readdir_copy_name(constchar *name, unsignedint len)
{ constchar *ret = kmemdup_nul(name, len, GFP_KERNEL);
/* * Avoid a kmemleak false positive. The pointer to the name is stored * in a page cache page which kmemleak does not scan.
*/ if (ret != NULL)
kmemleak_not_leak(ret); return ret;
}
#define NFS_READDIR_COOKIE_MASK (U32_MAX >> 14) /* * Hash algorithm allowing content addressible access to sequences * of directory cookies. Content is addressed by the value of the * cookie index of the first readdir entry in a page. * * We select only the first 18 bits to avoid issues with excessive * memory use for the page cache XArray. 18 bits should allow the caching * of 262144 pages of sequences of readdir entries. Since each page holds * 127 readdir entries for a typical 64-bit system, that works out to a * cache of ~ 33 million entries per directory.
*/ static pgoff_t nfs_readdir_folio_cookie_hash(u64 cookie)
{ if (cookie == 0) return 0; return hash_64(cookie, 18);
}
staticbool nfs_readdir_folio_validate(struct folio *folio, u64 last_cookie,
u64 change_attr)
{ struct nfs_cache_array *array = kmap_local_folio(folio, 0); int ret = true;
if (array->change_attr != change_attr)
ret = false; if (nfs_readdir_array_index_cookie(array) != last_cookie)
ret = false;
kunmap_local(array); return ret;
}
/* Match file and dirent using either filehandle or fileid * Note: caller is responsible for checking the fsid
*/ static int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
{ struct inode *inode; struct nfs_inode *nfsi;
if (d_really_is_negative(dentry)) return 0;
inode = d_inode(dentry); if (is_bad_inode(inode) || NFS_STALE(inode)) return 0;
/* * This function is called by the getattr code to request the * use of readdirplus to accelerate any future lookups in the same * directory.
*/ void nfs_readdir_record_entry_cache_hit(struct inode *dir)
{ struct nfs_inode *nfsi = NFS_I(dir); struct nfs_open_dir_context *ctx;
/* * This function is mainly for use by nfs_getattr(). * * If this is an 'ls -l', we want to force use of readdirplus.
*/ void nfs_readdir_record_entry_cache_miss(struct inode *dir)
{ struct nfs_inode *nfsi = NFS_I(dir); struct nfs_open_dir_context *ctx;
do {
status = nfs_readdir_entry_decode(desc, entry, &stream); if (status != 0) break;
status = nfs_readdir_folio_array_append(folio, entry, &cookie); if (status != -ENOSPC) continue;
if (folio->mapping != mapping) { if (!--narrays) break; new = nfs_readdir_folio_array_alloc(cookie, GFP_KERNEL); if (!new) break;
arrays++;
*arrays = folio = new;
} else { new = nfs_readdir_folio_get_next(mapping, cookie,
change_attr); if (!new) break; if (folio != *arrays)
nfs_readdir_folio_unlock_and_put(folio);
folio = new;
}
desc->folio_index_max++;
status = nfs_readdir_folio_array_append(folio, entry, &cookie);
} while (!status && !entry->eof);
switch (status) { case -EBADCOOKIE: if (!entry->eof) break;
nfs_readdir_folio_set_eof(folio);
fallthrough; case -EAGAIN:
status = 0; break; case -ENOSPC:
status = 0; if (!desc->plus) break; while (!nfs_readdir_entry_decode(desc, entry, &stream))
;
}
if (folio != *arrays)
nfs_readdir_folio_unlock_and_put(folio);
/* * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call * to nfs_readdir_free_pages()
*/ staticstruct page **nfs_readdir_alloc_pages(size_t npages)
{ struct page **pages;
size_t i;
pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); if (!pages) return NULL; for (i = 0; i < npages; i++) { struct page *page = alloc_page(GFP_KERNEL); if (page == NULL) goto out_freepages;
pages[i] = page;
} return pages;
folio = nfs_readdir_folio_get_locked(mapping, cookie, change_attr); if (!folio) return NULL; if (desc->clear_cache && !nfs_readdir_folio_needs_filling(folio))
nfs_readdir_folio_reinit_array(folio, cookie, change_attr); return folio;
}
/* * Returns 0 if desc->dir_cookie was found on page desc->page_index * and locks the page to prevent removal from the page cache.
*/ staticint find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
{ struct inode *inode = file_inode(desc->file); struct nfs_inode *nfsi = NFS_I(inode);
__be32 verf[NFS_DIR_VERIFIER_SIZE]; int res;
desc->folio = nfs_readdir_folio_get_cached(desc); if (!desc->folio) return -ENOMEM; if (nfs_readdir_folio_needs_filling(desc->folio)) { /* Grow the dtsize if we had to go back for more pages */ if (desc->folio_index == desc->folio_index_max)
nfs_grow_dtsize(desc);
desc->folio_index_max = desc->folio_index;
trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf,
desc->last_cookie,
desc->folio->index, desc->dtsize);
res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
&desc->folio, 1); if (res < 0) {
nfs_readdir_folio_unlock_and_put_cached(desc);
trace_nfs_readdir_cache_fill_done(inode, res); if (res == -EBADCOOKIE || res == -ENOTSYNC) {
invalidate_inode_pages2(desc->file->f_mapping);
nfs_readdir_rewind_search(desc);
trace_nfs_readdir_invalidate_cache_range(
inode, 0, MAX_LFS_FILESIZE); return -EAGAIN;
} return res;
} /* * Set the cookie verifier if the page cache was empty
*/ if (desc->last_cookie == 0 &&
memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) {
memcpy(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf));
invalidate_inode_pages2_range(desc->file->f_mapping, 1,
-1);
trace_nfs_readdir_invalidate_cache_range(
inode, 1, MAX_LFS_FILESIZE);
}
desc->clear_cache = false;
}
res = nfs_readdir_search_array(desc); if (res == 0) return 0;
nfs_readdir_folio_unlock_and_put_cached(desc); return res;
}
/* Search for desc->dir_cookie from the beginning of the page cache */ staticint readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
{ int res;
do {
res = find_and_lock_cache_page(desc);
} while (res == -EAGAIN); return res;
}
#define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL)
/* * Once we've found the start of the dirent within a page: fill 'er up...
*/ staticvoid nfs_do_filldir(struct nfs_readdir_descriptor *desc, const __be32 *verf)
{ struct file *file = desc->file; struct nfs_cache_array *array; unsignedint i; bool first_emit = !desc->dir_cookie;
array = kmap_local_folio(desc->folio, 0); for (i = desc->cache_entry_index; i < array->size; i++) { struct nfs_cache_array_entry *ent;
/* * nfs_readdir_handle_cache_misses return force clear at * (cache_misses > NFS_READDIR_CACHE_MISS_THRESHOLD) for * readdir heuristic, NFS_READDIR_CACHE_MISS_THRESHOLD + 1 * entries need be emitted here.
*/ if (first_emit && i > NFS_READDIR_CACHE_MISS_THRESHOLD + 2) {
desc->eob = true; break;
}
ent = &array->array[i]; if (!dir_emit(desc->ctx, ent->name, ent->name_len,
nfs_compat_user_ino64(ent->ino), ent->d_type)) {
desc->eob = true; break;
}
memcpy(desc->verf, verf, sizeof(desc->verf)); if (i == array->size - 1) {
desc->dir_cookie = array->last_cookie;
nfs_readdir_seek_next_array(array, desc);
} else {
desc->dir_cookie = array->array[i + 1].cookie;
desc->last_cookie = array->array[0].cookie;
} if (nfs_readdir_use_cookie(file))
desc->ctx->pos = desc->dir_cookie; else
desc->ctx->pos++;
} if (array->folio_is_eof)
desc->eof = !desc->eob;
/* * If we cannot find a cookie in our cache, we suspect that this is * because it points to a deleted file, so we ask the server to return * whatever it thinks is the next entry. We then feed this to filldir. * If all goes well, we should then be able to find our way round the * cache on the next call to readdir_search_pagecache(); * * NOTE: we cannot add the anonymous page to the pagecache because * the data it contains might not be page aligned. Besides, * we should already have a complete representation of the * directory in the page cache by the time we get here.
*/ staticint uncached_readdir(struct nfs_readdir_descriptor *desc)
{ struct folio **arrays;
size_t i, sz = 512;
__be32 verf[NFS_DIR_VERIFIER_SIZE]; int status = -ENOMEM;
dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
(unsignedlonglong)desc->dir_cookie);
arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL); if (!arrays) goto out;
arrays[0] = nfs_readdir_folio_array_alloc(desc->dir_cookie, GFP_KERNEL); if (!arrays[0]) goto out;
status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz); if (status < 0) {
trace_nfs_readdir_uncached_done(file_inode(desc->file), status); goto out_free;
}
for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
desc->folio = arrays[i];
nfs_do_filldir(desc, verf);
}
desc->folio = NULL;
/* * Grow the dtsize if we have to go back for more pages, * or shrink it if we're reading too many.
*/ if (!desc->eof) { if (!desc->eob)
nfs_grow_dtsize(desc); elseif (desc->buffer_fills == 1 &&
i < (desc->folio_index_max >> 1))
nfs_shrink_dtsize(desc);
}
out_free: for (i = 0; i < sz && arrays[i]; i++)
nfs_readdir_folio_array_free(arrays[i]);
out: if (!nfs_readdir_use_cookie(desc->file))
nfs_readdir_rewind_search(desc);
desc->folio_index_max = -1;
kfree(arrays);
dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status); return status;
}
/* The file offset position represents the dirent entry number. A last cookie cache takes care of the common case of reading the whole directory.
*/ staticint nfs_readdir(struct file *file, struct dir_context *ctx)
{ struct dentry *dentry = file_dentry(file); struct inode *inode = d_inode(dentry); struct nfs_inode *nfsi = NFS_I(inode); struct nfs_open_dir_context *dir_ctx = file->private_data; struct nfs_readdir_descriptor *desc; unsignedint cache_hits, cache_misses; bool force_clear; int res;
/* * ctx->pos points to the dirent entry number. * *desc->dir_cookie has the cookie for the next entry. We have * to either find the entry with the appropriate number or * revalidate the cookie.
*/
nfs_revalidate_mapping(inode, file->f_mapping);
res = -ENOMEM;
desc = kzalloc(sizeof(*desc), GFP_KERNEL); if (!desc) goto out;
desc->file = file;
desc->ctx = ctx;
desc->folio_index_max = -1;
if (res == -EBADCOOKIE) {
res = 0; /* This means either end of directory */ if (desc->dir_cookie && !desc->eof) { /* Or that the server has 'lost' a cookie */
res = uncached_readdir(desc); if (res == 0) continue; if (res == -EBADCOOKIE || res == -ENOTSYNC)
res = 0;
} break;
} if (res == -ETOOSMALL && desc->plus) {
nfs_zap_caches(inode);
desc->plus = false;
desc->eof = false; continue;
} if (res < 0) break;
nfs_do_filldir(desc, nfsi->cookieverf);
nfs_readdir_folio_unlock_and_put_cached(desc); if (desc->folio_index == desc->folio_index_max)
desc->clear_cache = force_clear;
} while (!desc->eob && !desc->eof);
/** * nfs_force_lookup_revalidate - Mark the directory as having changed * @dir: pointer to directory inode * * This forces the revalidation code in nfs_lookup_revalidate() to do a * full lookup on all child dentries of 'dir' whenever a change occurs * on the server that might have invalidated our dcache. * * Note that we reserve bit '0' as a tag to let us know when a dentry * was revalidated while holding a delegation on its inode. * * The caller should be holding dir->i_lock
*/ void nfs_force_lookup_revalidate(struct inode *dir)
{
NFS_I(dir)->cache_change_attribute += 2;
}
EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
/** * nfs_verify_change_attribute - Detects NFS remote directory changes * @dir: pointer to parent directory inode * @verf: previously saved change attribute * * Return "false" if the verifiers doesn't match the change attribute. * This would usually indicate that the directory contents have changed on * the server, and that any dentries need revalidating.
*/ staticbool nfs_verify_change_attribute(struct inode *dir, unsignedlong verf)
{ return (verf & ~1UL) == nfs_save_change_attribute(dir);
}
if (!dir || !nfs_verify_change_attribute(dir, verf)) return; if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ, 0))
nfs_set_verifier_delegated(&verf);
dentry->d_time = verf;
}
/** * nfs_set_verifier - save a parent directory verifier in the dentry * @dentry: pointer to dentry * @verf: verifier to save * * Saves the parent directory verifier in @dentry. If the inode has * a delegation, we also tag the dentry as having been revalidated * while holding a delegation so that we know we don't have to * look it up again after a directory change.
*/ void nfs_set_verifier(struct dentry *dentry, unsignedlong verf)
{
#if IS_ENABLED(CONFIG_NFS_V4) /** * nfs_clear_verifier_delegated - clear the dir verifier delegation tag * @inode: pointer to inode * * Iterates through the dentries in the inode alias list and clears * the tag used to indicate that the dentry has been revalidated * while holding a delegation. * This function is intended for use when the delegation is being * returned or revoked.
*/ void nfs_clear_verifier_delegated(struct inode *inode)
{ struct dentry *alias;
/* * A check for whether or not the parent directory has changed. * In the case it has, we assume that the dentries are untrustworthy * and may need to be looked up again. * If rcu_walk prevents us from performing a full check, return 0.
*/ staticint nfs_check_verifier(struct inode *dir, struct dentry *dentry, int rcu_walk)
{ if (IS_ROOT(dentry)) return 1; if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) return 0; if (!nfs_dentry_verify_change(dir, dentry)) return 0; /* Revalidate nfsi->cache_change_attribute before we declare a match */ if (nfs_mapping_need_revalidate_inode(dir)) { if (rcu_walk) return 0; if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) return 0;
} if (!nfs_dentry_verify_change(dir, dentry)) return 0; return 1;
}
/* * Use intent information to check whether or not we're going to do * an O_EXCL create using this path component.
*/ staticint nfs_is_exclusive_create(struct inode *dir, unsignedint flags)
{ if (NFS_PROTO(dir)->version == 2) return 0; return (flags & (LOOKUP_CREATE | LOOKUP_EXCL)) ==
(LOOKUP_CREATE | LOOKUP_EXCL);
}
/* * Inode and filehandle revalidation for lookups. * * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, * or if the intent information indicates that we're about to open this * particular file and the "nocto" mount flag is not set. *
*/ static int nfs_lookup_verify_inode(struct inode *inode, unsignedint flags)
{ struct nfs_server *server = NFS_SERVER(inode); int ret;
if (IS_AUTOMOUNT(inode)) return 0;
if (flags & LOOKUP_OPEN) { switch (inode->i_mode & S_IFMT) { case S_IFREG: /* A NFSv4 OPEN will revalidate later */ if (server->caps & NFS_CAP_ATOMIC_OPEN) goto out;
fallthrough; case S_IFDIR: if (server->flags & NFS_MOUNT_NOCTO) break; /* NFS close-to-open cache consistency validation */ goto out_force;
}
}
/* VFS wants an on-the-wire revalidation */ if (flags & LOOKUP_REVAL) goto out_force;
out: if (inode->i_nlink > 0 ||
(inode->i_nlink == 0 &&
test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags))) return 0; else return -ESTALE;
out_force: if (flags & LOOKUP_RCU) return -ECHILD;
ret = __nfs_revalidate_inode(server, inode); if (ret != 0) return ret; goto out;
}
/* * We judge how long we want to trust negative * dentries by looking at the parent inode mtime. * * If parent mtime has changed, we revalidate, else we wait for a * period corresponding to the parent's attribute cache timeout value. * * If LOOKUP_RCU prevents us from performing a full check, return 1 * suggesting a reval is needed. * * Note that when creating a new file, or looking up a rename target, * then it shouldn't be necessary to revalidate a negative dentry.
*/ staticinline int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, unsignedint flags)
{ if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) return 0; if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) return 1; /* Case insensitive server? Revalidate negative dentries */ if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE)) return 1; return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
}
staticint
nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, struct inode *inode, int error)
{ switch (error) { case 1: break; case -ETIMEDOUT: if (inode && (IS_ROOT(dentry) ||
NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL))
error = 1; break; case -ESTALE: case -ENOENT:
error = 0;
fallthrough; default: /* * We can't d_drop the root of a disconnected tree: * its d_hash is on the s_anon list and d_drop() would hide * it from shrink_dcache_for_unmount(), leading to busy * inodes on unmount and further oopses.
*/ if (inode && IS_ROOT(dentry))
error = 1; break;
}
trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error); return error;
}
staticint
nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, unsignedint flags)
{ int ret = 1; if (nfs_neg_need_reval(dir, dentry, flags)) { if (flags & LOOKUP_RCU) return -ECHILD;
ret = 0;
} return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
}
ret = 1;
out:
nfs_free_fattr(fattr);
nfs_free_fhandle(fhandle);
/* * If the lookup failed despite the dentry change attribute being * a match, then we should revalidate the directory cache.
*/ if (!ret && nfs_dentry_verify_change(dir, dentry))
nfs_mark_dir_for_revalidate(dir); return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
}
/* * This is called every time the dcache has a lookup hit, * and we should check whether we can really trust that * lookup. * * NOTE! The hit can be a negative hit too, don't assume * we have an inode! * * If the parent directory is seen to have changed, we throw out the * cached dentry and do a new lookup.
*/ staticint
nfs_do_lookup_revalidate(struct inode *dir, conststruct qstr *name, struct dentry *dentry, unsignedint flags)
{ struct inode *inode; int error = 0;
if (nfs_verifier_is_delegated(dentry)) return nfs_lookup_revalidate_delegated(dir, dentry, inode);
/* Force a full look up iff the parent directory has changed */ if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
error = nfs_lookup_verify_inode(inode, flags); if (error) { if (error == -ESTALE)
nfs_mark_dir_for_revalidate(dir); goto out_bad;
} goto out_valid;
}
staticvoid block_revalidate(struct dentry *dentry)
{ /* old devname - just in case */
kfree(dentry->d_fsdata);
/* Any new reference that could lead to an open * will take ->d_lock in lookup_open() -> d_lookup(). * Holding this lock ensures we cannot race with * __nfs_lookup_revalidate() and removes and need * for further barriers.
*/
lockdep_assert_held(&dentry->d_lock);
/* * A weaker form of d_revalidate for revalidating just the d_inode(dentry) * when we don't really care about the dentry name. This is called when a * pathwalk ends on a dentry that was not found via a normal lookup in the * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). * * In this situation, we just want to verify that the inode itself is OK * since the dentry might have changed on the server.
*/ staticint nfs_weak_revalidate(struct dentry *dentry, unsignedint flags)
{ struct inode *inode = d_inode(dentry); int error = 0;
/* * I believe we can only get a negative dentry here in the case of a * procfs-style symlink. Just assume it's correct for now, but we may * eventually need to do something more here.
*/ if (!inode) {
dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
__func__, dentry); return 1;
}
if (is_bad_inode(inode)) {
dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
__func__, dentry); return 0;
}
/* * This is called from dput() when d_count is going to 0.
*/ staticint nfs_dentry_delete(conststruct dentry *dentry)
{
dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
dentry, dentry->d_flags);
/* Unhash any dentry with a stale inode */ if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) return 1;
if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { /* Unhash it, so that ->d_iput() would be called */ return 1;
} if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { /* Unhash it, so that ancestors of killed async unlink
* files will be cleaned up during umount */ return 1;
} return 0;
}
/* Ensure that we revalidate inode->i_nlink */ staticvoid nfs_drop_nlink(struct inode *inode)
{
spin_lock(&inode->i_lock); /* drop the inode if we're reasonably sure this is the last link */ if (inode->i_nlink > 0)
drop_nlink(inode);
NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
nfs_set_cache_invalid(
inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
NFS_INO_INVALID_NLINK);
spin_unlock(&inode->i_lock);
}
/* * Called when the dentry loses inode. * We use it to clean up silly-renamed files.
*/ staticvoid nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
{ if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
nfs_complete_unlink(dentry, inode);
nfs_drop_nlink(inode);
}
iput(inode);
}
staticvoid nfs_d_release(struct dentry *dentry)
{ /* free cached devname value, if it survived that far */ if (unlikely(dentry->d_fsdata)) { if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
WARN_ON(1); else
kfree(dentry->d_fsdata);
}
}
if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) return ERR_PTR(-ENAMETOOLONG);
/* * If we're doing an exclusive create, optimize away the lookup * but don't hash the dentry.
*/ if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) return NULL;
res = ERR_PTR(-ENOMEM);
fhandle = nfs_alloc_fhandle();
fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir)); if (fhandle == NULL || fattr == NULL) goto out;
dir_verifier = nfs_save_change_attribute(dir);
trace_nfs_lookup_enter(dir, dentry, flags);
error = NFS_PROTO(dir)->lookup(dir, dentry, &dentry->d_name,
fhandle, fattr); if (error == -ENOENT) { if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
dir_verifier = inode_peek_iversion_raw(dir); goto no_entry;
} if (error < 0) {
res = ERR_PTR(error); goto out;
}
inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
res = ERR_CAST(inode); if (IS_ERR(res)) goto out;
/* Notify readdir to use READDIRPLUS */
nfs_lookup_advise_force_readdirplus(dir, flags);
err = nfs_check_flags(open_flags); if (err) return err;
/* NFS only supports OPEN on regular files */ if ((open_flags & O_DIRECTORY)) { if (!d_in_lookup(dentry)) { /* * Hashed negative dentry with O_DIRECTORY: dentry was * revalidated and is fine, no need to perform lookup * again
*/ return -ENOENT;
}
lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; goto no_open;
}
if (dentry->d_name.len > NFS_SERVER(dir)->namelen) return -ENAMETOOLONG;
if (open_flags & O_CREAT) { struct nfs_server *server = NFS_SERVER(dir);
if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
mode &= ~current_umask();
int nfs_atomic_open_v23(struct inode *dir, struct dentry *dentry, struct file *file, unsignedint open_flags,
umode_t mode)
{ struct dentry *res = NULL; /* Same as look+open from lookup_open(), but with different O_TRUNC * handling.
*/ int error = 0;
if (dentry->d_name.len > NFS_SERVER(dir)->namelen) return -ENAMETOOLONG;
if (open_flags & O_CREAT) {
error = nfs_do_create(dir, dentry, mode, open_flags); if (!error) {
file->f_mode |= FMODE_CREATED; return finish_open(file, dentry, NULL);
} elseif (error != -EEXIST || open_flags & O_EXCL) return error;
} if (d_in_lookup(dentry)) { /* The only flags nfs_lookup considers are * LOOKUP_EXCL and LOOKUP_RENAME_TARGET, and * we want those to be zero so the lookup isn't skipped.
*/
res = nfs_lookup(dir, dentry, 0);
} return finish_no_open(file, res);
/* * Code common to create, mkdir, and mknod.
*/ int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
{ struct dentry *d;
d = nfs_add_or_obtain(dentry, fhandle, fattr); if (IS_ERR(d)) return PTR_ERR(d);
/* Callers don't care */
dput(d); return 0;
}
EXPORT_SYMBOL_GPL(nfs_instantiate);
/* * Following a failed create operation, we drop the dentry rather * than retain a negative dentry. This avoids a problem in the event * that the operation succeeded on the server, but an error in the * reply path made it appear to have failed.
*/ staticint nfs_do_create(struct inode *dir, struct dentry *dentry,
umode_t mode, int open_flags)
{ struct iattr attr; int error;
trace_nfs_rmdir_enter(dir, dentry); if (d_really_is_positive(dentry)) {
down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); /* Ensure the VFS deletes this inode */ switch (error) { case 0:
clear_nlink(d_inode(dentry)); break; case -ENOENT:
nfs_dentry_handle_enoent(dentry);
}
up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
} else
error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
nfs_dentry_remove_handle_error(dir, dentry, error);
trace_nfs_rmdir_exit(dir, dentry, error);
return error;
}
EXPORT_SYMBOL_GPL(nfs_rmdir);
/* * Remove a file after making sure there are no pending writes, * and after checking that the file has only one user. * * We invalidate the attribute cache and free the inode prior to the operation * to avoid possible races if the server reuses the inode.
*/ staticint nfs_safe_remove(struct dentry *dentry)
{ struct inode *dir = d_inode(dentry->d_parent); struct inode *inode = d_inode(dentry); int error = -EBUSY;
/* We do silly rename. In case sillyrename() returns -EBUSY, the inode * belongs to an active ".nfs..." file and we return -EBUSY. * * If sillyrename() returns 0, we do nothing, otherwise we unlink.
*/ int nfs_unlink(struct inode *dir, struct dentry *dentry)
{ int error;
trace_nfs_unlink_enter(dir, dentry);
spin_lock(&dentry->d_lock); if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
&NFS_I(d_inode(dentry))->flags)) {
spin_unlock(&dentry->d_lock); /* Start asynchronous writeout of the inode */
write_inode_now(d_inode(dentry), 0);
error = nfs_sillyrename(dir, dentry); goto out;
} /* We must prevent any concurrent open until the unlink * completes. ->d_revalidate will wait for ->d_fsdata * to clear. We set it here to ensure no lookup succeeds until * the unlink is complete on the server.
*/
error = -ETXTBSY; if (WARN_ON(dentry->d_flags & DCACHE_NFSFS_RENAMED) ||
WARN_ON(dentry->d_fsdata == NFS_FSDATA_BLOCKED)) {
spin_unlock(&dentry->d_lock); goto out;
}
block_revalidate(dentry);
/* * To create a symbolic link, most file systems instantiate a new inode, * add a page to it containing the path, then write it out to the disk * using prepare_write/commit_write. * * Unfortunately the NFS client can't create the in-core inode first * because it needs a file handle to create an in-core inode (see * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the * symlink request has completed on the server. * * So instead we allocate a raw page, copy the symname into it, then do * the SYMLINK request with the page as the buffer. If it succeeds, we * now have a new file handle and can instantiate an in-core NFS inode * and move the raw page into its mapping.
*/ int nfs_symlink(struct mnt_idmap *idmap, struct inode *dir, struct dentry *dentry, constchar *symname)
{ struct folio *folio; char *kaddr; struct iattr attr; unsignedint pathlen = strlen(symname); int error;
/* * No big deal if we can't add this page to the page cache here. * READLINK will get the missing page from the server if needed.
*/ if (filemap_add_folio(d_inode(dentry)->i_mapping, folio, 0,
GFP_KERNEL) == 0) {
folio_mark_uptodate(folio);
folio_unlock(folio);
}
if (old_dentry->d_parent != new_dentry->d_parent) returnfalse; if (server->fh_expire_type & NFS_FH_RENAME_UNSAFE) return !(server->fh_expire_type & NFS_FH_NOEXPIRE_WITH_OPEN); returntrue;
}
/* * RENAME * FIXME: Some nfsds, like the Linux user space nfsd, may generate a * different file handle for the same inode after a rename (e.g. when * moving to a different directory). A fail-safe method to do so would * be to look up old_dir/old_name, create a link to new_dir/new_name and * rename the old file using the sillyrename stuff. This way, the original * file in old_dir will go away when the last process iput()s the inode. * * FIXED. * * It actually works quite well. One needs to have the possibility for * at least one ".nfs..." file in each directory the file ever gets * moved or linked to which happens automagically with the new * implementation that only depends on the dcache stuff instead of * using the inode layer * * Unfortunately, things are a little more complicated than indicated * above. For a cross-directory move, we want to make sure we can get * rid of the old inode after the operation. This means there must be * no pending writes (if it's a file), and the use count must be 1. * If these conditions are met, we can drop the dentries before doing * the rename.
*/ int nfs_rename(struct mnt_idmap *idmap, struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsignedint flags)
{ struct inode *old_inode = d_inode(old_dentry); struct inode *new_inode = d_inode(new_dentry); struct dentry *dentry = NULL; struct rpc_task *task; bool must_unblock = false; int error = -EBUSY;
trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); /* * For non-directories, check whether the target is busy and if so, * make a copy of the dentry and then do a silly-rename. If the * silly-rename succeeds, the copied dentry is hashed and becomes * the new target.
*/ if (new_inode && !S_ISDIR(new_inode->i_mode)) { /* We must prevent any concurrent open until the unlink * completes. ->d_revalidate will wait for ->d_fsdata * to clear. We set it here to ensure no lookup succeeds until * the unlink is complete on the server.
*/
error = -ETXTBSY; if (WARN_ON(new_dentry->d_flags & DCACHE_NFSFS_RENAMED) ||
WARN_ON(new_dentry->d_fsdata == NFS_FSDATA_BLOCKED)) goto out;
spin_lock(&new_dentry->d_lock); if (d_count(new_dentry) > 2) { int err;
spin_unlock(&new_dentry->d_lock);
/* copy the target dentry's name */
dentry = d_alloc(new_dentry->d_parent,
&new_dentry->d_name); if (!dentry) goto out;
/* silly-rename the existing target ... */
err = nfs_sillyrename(new_dir, new_dentry); if (err) goto out;
if (S_ISREG(old_inode->i_mode) &&
nfs_rename_is_unsafe_cross_dir(old_dentry, new_dentry))
nfs_sync_inode(old_inode);
task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry,
must_unblock ? nfs_unblock_rename : NULL); if (IS_ERR(task)) { if (must_unblock)
unblock_revalidate(new_dentry);
error = PTR_ERR(task); goto out;
}
error = rpc_wait_for_completion_task(task); if (error != 0) {
((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
smp_wmb();
} else
error = task->tk_status;
rpc_put_task(task); /* Ensure the inode attributes are revalidated */ if (error == 0) {
spin_lock(&old_inode->i_lock);
NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
NFS_INO_INVALID_CTIME |
NFS_INO_REVAL_FORCED);
spin_unlock(&old_inode->i_lock);
}
out:
trace_nfs_rename_exit(old_dir, old_dentry,
new_dir, new_dentry, error); if (!error) { if (new_inode != NULL)
nfs_drop_nlink(new_inode); /* * The d_move() should be here instead of in an async RPC completion * handler because we need the proper locks to move the dentry. If * we're interrupted by a signal, the async RPC completion handler * should mark the directories for revalidation.
*/
d_move(old_dentry, new_dentry);
nfs_set_verifier(old_dentry,
nfs_save_change_attribute(new_dir));
} elseif (error == -ENOENT)
nfs_dentry_handle_enoent(old_dentry);
/* new dentry created? */ if (dentry)
dput(dentry); return error;
}
EXPORT_SYMBOL_GPL(nfs_rename);
if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) return; /* Remove from global LRU init */
spin_lock(&nfs_access_lru_lock); if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
list_del_init(&NFS_I(inode)->access_cache_inode_lru);
/* The above field assignments must be visible * before this item appears on the lru. We cannot easily * use rcu_assign_pointer, so just force the memory barrier.
*/
smp_wmb();
nfs_access_add_rbtree(inode, cache, cred);
/* Add inode to global LRU list */ if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
spin_lock(&nfs_access_lru_lock); if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
&nfs_access_lru_list);
spin_unlock(&nfs_access_lru_lock);
}
nfs_access_cache_enforce_limit();
}
EXPORT_SYMBOL_GPL(nfs_access_add_cache);
staticint nfs_do_access(struct inode *inode, conststruct cred *cred, int mask)
{ struct nfs_access_entry cache; bool may_block = (mask & MAY_NOT_BLOCK) == 0; int cache_mask = -1; int status;
trace_nfs_access_enter(inode);
status = nfs_access_get_cached(inode, cred, &cache.mask, may_block); if (status == 0) goto out_cached;
status = -ECHILD; if (!may_block) goto out;
/* * Determine which access bits we want to ask for...
*/
cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
nfs_access_xattr_mask(NFS_SERVER(inode)); if (S_ISDIR(inode->i_mode))
cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; else
cache.mask |= NFS_ACCESS_EXECUTE;
status = NFS_PROTO(inode)->access(inode, &cache, cred); if (status != 0) { if (status == -ESTALE) { if (!S_ISDIR(inode->i_mode))
nfs_set_inode_stale(inode); else
nfs_zap_caches(inode);
} goto out;
}
nfs_access_add_cache(inode, &cache, cred);
out_cached:
cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
status = -EACCES;
out:
trace_nfs_access_exit(inode, mask, cache_mask, status); return status;
}
staticint nfs_open_permission_mask(int openflags)
{ int mask = 0;
if (openflags & __FMODE_EXEC) { /* ONLY check exec rights */
mask = MAY_EXEC;
} else { if ((openflags & O_ACCMODE) != O_WRONLY)
mask |= MAY_READ; if ((openflags & O_ACCMODE) != O_RDONLY)
mask |= MAY_WRITE;
}
return mask;
}
int nfs_may_open(struct inode *inode, conststruct cred *cred, int openflags)
{ return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
}
EXPORT_SYMBOL_GPL(nfs_may_open);
staticint nfs_execute_ok(struct inode *inode, int mask)
{ struct nfs_server *server = NFS_SERVER(inode); int ret = 0;
if (S_ISDIR(inode->i_mode)) return 0; if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) { if (mask & MAY_NOT_BLOCK) return -ECHILD;
ret = __nfs_revalidate_inode(server, inode);
} if (ret == 0 && !execute_ok(inode))
ret = -EACCES; return ret;
}
int nfs_permission(struct mnt_idmap *idmap, struct inode *inode, int mask)
{ conststruct cred *cred = current_cred(); int res = 0;
nfs_inc_stats(inode, NFSIOS_VFSACCESS);
if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) goto out; /* Is this sys_access() ? */ if (mask & (MAY_ACCESS | MAY_CHDIR)) goto force_lookup;
switch (inode->i_mode & S_IFMT) { case S_IFLNK: goto out; case S_IFREG: if ((mask & MAY_OPEN) &&
nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) return 0; break; case S_IFDIR: /* * Optimize away all write operations, since the server * will check permissions when we perform the op.
*/ if ((mask & MAY_WRITE) && !(mask & MAY_READ)) goto out;
}
force_lookup: if (!NFS_PROTO(inode)->access) goto out_notsup;
res = nfs_do_access(inode, cred, mask);
out: if (!res && (mask & MAY_EXEC))
res = nfs_execute_ok(inode, mask);
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.