20 #include <linux/module.h>
21 #include <linux/time.h>
22 #include <linux/errno.h>
23 #include <linux/stat.h>
24 #include <linux/fcntl.h>
25 #include <linux/string.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
30 #include <linux/nfs_fs.h>
36 #include <linux/sched.h>
38 #include <linux/xattr.h>
47 static int nfs_opendir(
struct inode *,
struct file *);
48 static int nfs_closedir(
struct inode *,
struct file *);
50 static int nfs_fsync_dir(
struct file *, loff_t, loff_t,
int);
51 static loff_t nfs_llseek_dir(
struct file *, loff_t,
int);
52 static void nfs_readdir_clear_array(
struct page*);
55 .llseek = nfs_llseek_dir,
57 .readdir = nfs_readdir,
59 .release = nfs_closedir,
60 .fsync = nfs_fsync_dir,
64 .freepage = nfs_readdir_clear_array,
76 ctx->
cred = get_rpccred(cred);
96 struct rpc_cred *cred;
98 dfprintk(FILE,
"NFS: open dir(%s/%s)\n",
99 filp->
f_path.dentry->d_parent->d_name.name,
100 filp->
f_path.dentry->d_name.name);
106 return PTR_ERR(cred);
107 ctx = alloc_nfs_open_dir_context(inode, cred);
113 if (filp->
f_path.dentry == filp->
f_path.mnt->mnt_root) {
126 nfs_closedir(
struct inode *inode,
struct file *filp)
171 return ERR_PTR(-
EIO);
179 void nfs_readdir_release_array(
struct page *
page)
188 void nfs_readdir_clear_array(
struct page *
page)
194 for (i = 0; i < array->
size; i++)
205 int nfs_readdir_make_qstr(
struct qstr *
string,
const char *
name,
unsigned int len)
221 int nfs_readdir_add_to_array(
struct nfs_entry *
entry,
struct page *page)
228 return PTR_ERR(array);
230 cache_entry = &array->
array[array->
size];
238 cache_entry->
ino = entry->
ino;
240 ret = nfs_readdir_make_qstr(&cache_entry->
string, entry->
name, entry->
len);
248 nfs_readdir_release_array(page);
260 if (diff >= array->
size) {
266 index = (
unsigned int)diff;
282 for (i = 0; i < array->
size; i++) {
284 struct nfs_inode *nfsi = NFS_I(desc->
file->f_path.dentry->d_inode);
292 }
else if (new_pos < desc->
file->
f_pos) {
295 if (printk_ratelimit()) {
296 pr_notice(
"NFS: directory %s/%s contains a readdir loop."
297 "Please contact your server vendor. "
298 "The file: %s has duplicate cookie %llu\n",
299 desc->
file->f_dentry->d_parent->d_name.name,
300 desc->
file->f_dentry->d_name.name,
301 array->
array[i].string.name,
310 desc->
file->f_pos = new_pos;
330 array = nfs_readdir_get_array(desc->
page);
332 status = PTR_ERR(array);
337 status = nfs_readdir_search_for_pos(array, desc);
339 status = nfs_readdir_search_for_cookie(array, desc);
346 nfs_readdir_release_array(desc->
page);
357 struct rpc_cred *cred = ctx->
cred;
364 error = NFS_PROTO(inode)->readdir(file->
f_path.dentry, cred, entry->
cookie, pages,
365 NFS_SERVER(inode)->dtsize, desc->
plus);
383 struct nfs_entry *entry,
struct xdr_stream *xdr)
400 if (nfs_compare_fh(entry->
fh, NFS_FH(dentry->
d_inode)) != 0)
408 bool nfs_use_readdirplus(
struct inode *dir,
struct file *filp)
414 if (filp->
f_pos == 0)
425 void nfs_advise_use_readdirplus(
struct inode *dir)
431 void nfs_prime_dcache(
struct dentry *parent,
struct nfs_entry *entry)
434 struct dentry *dentry;
435 struct dentry *
alias;
436 struct inode *dir = parent->
d_inode;
439 if (filename.
name[0] ==
'.') {
440 if (filename.len == 1)
442 if (filename.len == 2 && filename.
name[1] ==
'.')
447 dentry =
d_lookup(parent, &filename);
448 if (dentry !=
NULL) {
449 if (nfs_same_file(dentry, entry)) {
459 dentry =
d_alloc(parent, &filename);
471 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
474 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
483 struct page **xdr_pages,
struct page *page,
unsigned int buflen)
489 unsigned int count = 0;
500 status = xdr_decode(desc, entry, &stream);
510 nfs_prime_dcache(desc->
file->f_path.dentry, entry);
512 status = nfs_readdir_add_to_array(entry, page);
515 }
while (!entry->
eof);
517 if (count == 0 || (status == -
EBADCOOKIE && entry->
eof != 0)) {
518 array = nfs_readdir_get_array(page);
519 if (!IS_ERR(array)) {
522 nfs_readdir_release_array(page);
524 status = PTR_ERR(array);
532 void nfs_readdir_free_pagearray(
struct page **pages,
unsigned int npages)
535 for (i = 0; i < npages; i++)
540 void nfs_readdir_free_large_page(
void *
ptr,
struct page **pages,
543 nfs_readdir_free_pagearray(pages, npages);
551 int nfs_readdir_large_page(
struct page **pages,
unsigned int npages)
555 for (i = 0; i < npages; i++) {
564 nfs_readdir_free_pagearray(pages, i);
572 void *pages_ptr =
NULL;
574 struct file *file = desc->
file;
584 entry.
server = NFS_SERVER(inode);
588 array = nfs_readdir_get_array(page);
590 status = PTR_ERR(array);
596 status = nfs_readdir_large_page(pages, array_size);
598 goto out_release_array;
601 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
606 status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
614 nfs_readdir_free_large_page(pages_ptr, pages, array_size);
616 nfs_readdir_release_array(page);
618 nfs_free_fattr(entry.
fattr);
619 nfs_free_fhandle(entry.
fh);
632 struct inode *inode = desc->
file->f_path.dentry->d_inode;
635 ret = nfs_readdir_xdr_to_array(desc, page, inode);
638 SetPageUptodate(page);
654 if (!desc->
page->mapping)
655 nfs_readdir_clear_array(desc->
page);
675 desc->
page = get_cache_page(desc);
676 if (IS_ERR(desc->
page))
677 return PTR_ERR(desc->
page);
679 res = nfs_readdir_search_array(desc);
681 cache_page_release(desc);
696 res = find_cache_page(desc);
708 struct file *file = desc->
file;
714 array = nfs_readdir_get_array(desc->
page);
716 res = PTR_ERR(array);
731 if (i < (array->
size-1))
741 nfs_readdir_release_array(desc->
page);
743 cache_page_release(desc);
744 dfprintk(DIRCACHE,
"NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
765 struct page *page =
NULL;
767 struct inode *inode = desc->
file->f_path.dentry->d_inode;
770 dfprintk(DIRCACHE,
"NFS: uncached_readdir() searching for cookie %Lu\n",
784 status = nfs_readdir_xdr_to_array(desc, page, inode);
788 status = nfs_do_filldir(desc, dirent, filldir);
791 dfprintk(DIRCACHE,
"NFS: %s: returns %d\n",
795 cache_page_release(desc);
803 static int nfs_readdir(
struct file *filp,
void *dirent,
filldir_t filldir)
805 struct dentry *dentry = filp->
f_path.dentry;
806 struct inode *inode = dentry->
d_inode;
812 dfprintk(FILE,
"NFS: readdir(%s/%s) starting at cookie %llu\n",
814 (
long long)filp->
f_pos);
823 memset(desc, 0,
sizeof(*desc));
827 desc->
decode = NFS_PROTO(inode)->decode_dirent;
828 desc->
plus = nfs_use_readdirplus(inode, filp) ? 1 : 0;
836 res = readdir_search_pagecache(desc);
843 res = uncached_readdir(desc, dirent, filldir);
860 res = nfs_do_filldir(desc, dirent, filldir);
863 }
while (!desc->
eof);
868 dfprintk(FILE,
"NFS: readdir(%s/%s) returns %d\n",
874 static loff_t nfs_llseek_dir(
struct file *filp, loff_t
offset,
int origin)
876 struct dentry *dentry = filp->
f_path.dentry;
877 struct inode *inode = dentry->
d_inode;
880 dfprintk(FILE,
"NFS: llseek dir(%s/%s, %lld, %d)\n",
888 offset += filp->
f_pos;
896 if (offset != filp->
f_pos) {
910 static int nfs_fsync_dir(
struct file *filp, loff_t
start, loff_t
end,
913 struct dentry *dentry = filp->
f_path.dentry;
914 struct inode *inode = dentry->
d_inode;
916 dfprintk(FILE,
"NFS: fsync dir(%s/%s) datasync %d\n",
938 NFS_I(dir)->cache_change_attribute++;
947 static int nfs_check_verifier(
struct inode *dir,
struct dentry *dentry)
953 if (!nfs_verify_change_attribute(dir, dentry->
d_time))
958 if (!nfs_verify_change_attribute(dir, dentry->
d_time))
967 static int nfs_is_exclusive_create(
struct inode *dir,
unsigned int flags)
969 if (NFS_PROTO(dir)->
version == 2)
983 int nfs_lookup_verify_inode(
struct inode *inode,
unsigned int flags)
985 struct nfs_server *server = NFS_SERVER(inode);
1009 int nfs_neg_need_reval(
struct inode *dir,
struct dentry *dentry,
1017 return !nfs_check_verifier(dir, dentry);
1031 static int nfs_lookup_revalidate(
struct dentry *dentry,
unsigned int flags)
1034 struct inode *
inode;
1035 struct dentry *parent;
1049 if (nfs_neg_need_reval(dir, dentry, flags))
1051 goto out_valid_noent;
1055 dfprintk(LOOKUPCACHE,
"%s: %s/%s has dud inode\n",
1056 __func__, dentry->
d_parent->d_name.name,
1061 if (NFS_PROTO(dir)->have_delegation(inode,
FMODE_READ))
1062 goto out_set_verifier;
1065 if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1066 if (nfs_lookup_verify_inode(inode, flags))
1067 goto out_zap_parent;
1071 if (NFS_STALE(inode))
1077 if (fhandle ==
NULL || fattr ==
NULL)
1080 error = NFS_PROTO(dir)->lookup(dir, &dentry->
d_name, fhandle, fattr);
1083 if (nfs_compare_fh(NFS_FH(inode), fhandle))
1088 nfs_free_fattr(fattr);
1089 nfs_free_fhandle(fhandle);
1091 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1094 nfs_advise_use_readdirplus(dir);
1097 dfprintk(LOOKUPCACHE,
"NFS: %s(%s/%s) is valid\n",
1098 __func__, dentry->
d_parent->d_name.name,
1104 nfs_free_fattr(fattr);
1105 nfs_free_fhandle(fhandle);
1106 nfs_mark_for_revalidate(dir);
1119 dfprintk(LOOKUPCACHE,
"NFS: %s(%s/%s) is invalid\n",
1120 __func__, dentry->
d_parent->d_name.name,
1124 nfs_free_fattr(fattr);
1125 nfs_free_fhandle(fhandle);
1127 dfprintk(LOOKUPCACHE,
"NFS: %s(%s/%s) lookup returned error %d\n",
1128 __func__, dentry->
d_parent->d_name.name,
1129 dentry->
d_name.name, error);
1136 static int nfs_dentry_delete(
const struct dentry *dentry)
1138 dfprintk(VFS,
"NFS: dentry_delete(%s/%s, %x)\n",
1159 static void nfs_drop_nlink(
struct inode *inode)
1161 spin_lock(&inode->
i_lock);
1164 spin_unlock(&inode->
i_lock);
1171 static void nfs_dentry_iput(
struct dentry *dentry,
struct inode *inode)
1184 static void nfs_d_release(
struct dentry *dentry)
1196 .d_revalidate = nfs_lookup_revalidate,
1197 .d_delete = nfs_dentry_delete,
1198 .d_iput = nfs_dentry_iput,
1200 .d_release = nfs_d_release,
1204 struct dentry *
nfs_lookup(
struct inode *dir,
struct dentry * dentry,
unsigned int flags)
1207 struct dentry *parent;
1208 struct inode *inode =
NULL;
1213 dfprintk(VFS,
"NFS: lookup(%s/%s)\n",
1225 if (nfs_is_exclusive_create(dir, flags)) {
1234 if (fhandle ==
NULL || fattr ==
NULL)
1240 error = NFS_PROTO(dir)->lookup(dir, &dentry->
d_name, fhandle, fattr);
1244 res = ERR_PTR(error);
1245 goto out_unblock_sillyrename;
1248 res = ERR_CAST(inode);
1250 goto out_unblock_sillyrename;
1253 nfs_advise_use_readdirplus(dir);
1259 goto out_unblock_sillyrename;
1262 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1263 out_unblock_sillyrename:
1266 nfs_free_fattr(fattr);
1267 nfs_free_fhandle(fhandle);
1272 #if IS_ENABLED(CONFIG_NFS_V4)
1273 static int nfs4_lookup_revalidate(
struct dentry *,
unsigned int);
1277 .d_delete = nfs_dentry_delete,
1278 .d_iput = nfs_dentry_iput,
1280 .d_release = nfs_d_release,
1284 static fmode_t flags_to_mode(
int flags)
1289 if ((flags & O_ACCMODE) !=
O_RDONLY)
1299 static int do_open(
struct inode *inode,
struct file *filp)
1306 struct dentry *dentry,
1307 struct file *file,
unsigned open_flags,
1312 if (ctx->
dentry != dentry) {
1314 ctx->
dentry = dget(dentry);
1334 int nfs_atomic_open(
struct inode *dir,
struct dentry *dentry,
1335 struct file *file,
unsigned open_flags,
1341 struct inode *
inode;
1347 dfprintk(VFS,
"NFS: atomic_open(%s/%ld), %s\n",
1352 if (!d_unhashed(dentry)) {
1375 ctx = create_nfs_open_context(dentry, open_flags);
1381 inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr);
1383 if (IS_ERR(inode)) {
1386 err = PTR_ERR(inode);
1389 d_add(dentry,
NULL);
1404 res = d_add_unique(dentry, inode);
1409 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1411 err = nfs_finish_open(ctx, dentry, file, open_flags, opened);
1427 static int nfs4_lookup_revalidate(
struct dentry *dentry,
unsigned int flags)
1429 struct dentry *parent =
NULL;
1430 struct inode *
inode;
1434 if (flags & LOOKUP_RCU)
1439 if (d_mountpoint(dentry))
1449 if (inode ==
NULL) {
1450 if (!nfs_neg_need_reval(dir, dentry, flags))
1472 return nfs_lookup_revalidate(dentry, flags);
1484 struct inode *dir = parent->
d_inode;
1485 struct inode *
inode;
1493 if (fhandle->
size == 0) {
1494 error = NFS_PROTO(dir)->lookup(dir, &dentry->
d_name, fhandle, fattr);
1498 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1501 error = server->
nfs_client->rpc_ops->getattr(server, fhandle, fattr);
1506 error = PTR_ERR(inode);
1509 d_add(dentry, inode);
1514 nfs_mark_for_revalidate(dir);
1533 dfprintk(VFS,
"NFS: create(%s/%ld), %s\n",
1539 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
1558 dfprintk(VFS,
"NFS: mknod(%s/%ld), %s\n",
1561 if (!new_valid_dev(rdev))
1567 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1585 dfprintk(VFS,
"NFS: mkdir(%s/%ld), %s\n",
1591 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1601 static void nfs_dentry_handle_enoent(
struct dentry *dentry)
1603 if (dentry->
d_inode !=
NULL && !d_unhashed(dentry))
1611 dfprintk(VFS,
"NFS: rmdir(%s/%ld), %s\n",
1614 error = NFS_PROTO(dir)->rmdir(dir, &dentry->
d_name);
1618 else if (error == -
ENOENT)
1619 nfs_dentry_handle_enoent(dentry);
1632 static int nfs_safe_remove(
struct dentry *dentry)
1634 struct inode *dir = dentry->
d_parent->d_inode;
1635 struct inode *inode = dentry->
d_inode;
1638 dfprintk(VFS,
"NFS: safe_remove(%s/%s)\n",
1647 if (inode !=
NULL) {
1648 NFS_PROTO(inode)->return_delegation(inode);
1649 error = NFS_PROTO(dir)->remove(dir, &dentry->
d_name);
1652 nfs_drop_nlink(inode);
1653 nfs_mark_for_revalidate(inode);
1655 error = NFS_PROTO(dir)->remove(dir, &dentry->
d_name);
1657 nfs_dentry_handle_enoent(dentry);
1670 int need_rehash = 0;
1672 dfprintk(VFS,
"NFS: unlink(%s/%ld, %s)\n", dir->
i_sb->s_id,
1675 spin_lock(&dentry->
d_lock);
1677 spin_unlock(&dentry->
d_lock);
1683 if (!d_unhashed(dentry)) {
1687 spin_unlock(&dentry->
d_lock);
1688 error = nfs_safe_remove(dentry);
1689 if (!error || error == -
ENOENT) {
1690 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1691 }
else if (need_rehash)
1712 int nfs_symlink(
struct inode *dir,
struct dentry *dentry,
const char *symname)
1718 unsigned int pathlen =
strlen(symname);
1721 dfprintk(VFS,
"NFS: symlink(%s/%ld, %s, %s)\n", dir->
i_sb->s_id,
1735 memcpy(kaddr, symname, pathlen);
1740 error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1742 dfprintk(VFS,
"NFS: symlink(%s/%ld, %s, %s) error %d\n",
1744 dentry->
d_name.name, symname, error);
1754 pagevec_init(&lru_pvec, 0);
1755 if (!add_to_page_cache(page, dentry->
d_inode->i_mapping, 0,
1757 pagevec_add(&lru_pvec, page);
1758 pagevec_lru_add_file(&lru_pvec);
1759 SetPageUptodate(page);
1769 nfs_link(
struct dentry *old_dentry,
struct inode *dir,
struct dentry *dentry)
1771 struct inode *inode = old_dentry->
d_inode;
1774 dfprintk(VFS,
"NFS: link(%s/%s -> %s/%s)\n",
1778 NFS_PROTO(inode)->return_delegation(inode);
1781 error = NFS_PROTO(dir)->link(inode, dir, &dentry->
d_name);
1784 d_add(dentry, inode);
1815 struct inode *new_dir,
struct dentry *new_dentry)
1817 struct inode *old_inode = old_dentry->
d_inode;
1819 struct dentry *dentry =
NULL, *rehash =
NULL;
1822 dfprintk(VFS,
"NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1838 if (!d_unhashed(new_dentry)) {
1840 rehash = new_dentry;
1843 if (new_dentry->
d_count > 2) {
1857 new_dentry = dentry;
1863 NFS_PROTO(old_inode)->return_delegation(old_inode);
1864 if (new_inode !=
NULL)
1865 NFS_PROTO(new_inode)->return_delegation(new_inode);
1867 error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->
d_name,
1868 new_dir, &new_dentry->
d_name);
1869 nfs_mark_for_revalidate(old_inode);
1874 if (new_inode !=
NULL)
1875 nfs_drop_nlink(new_inode);
1876 d_move(old_dentry, new_dentry);
1877 nfs_set_verifier(new_dentry,
1878 nfs_save_change_attribute(new_dir));
1879 }
else if (error == -
ENOENT)
1880 nfs_dentry_handle_enoent(old_dentry);
1898 atomic_long_dec(&nfs_access_nr_entries);
1906 while (!list_empty(head)) {
1909 nfs_access_free_entry(cache);
1923 return (nr_to_scan == 0) ? 0 : -1;
1925 spin_lock(&nfs_access_lru_lock);
1927 struct inode *
inode;
1929 if (nr_to_scan-- == 0)
1932 spin_lock(&inode->
i_lock);
1934 goto remove_lru_entry;
1937 list_move(&cache->
lru, &head);
1941 &nfs_access_lru_list);
1949 spin_unlock(&inode->
i_lock);
1951 spin_unlock(&nfs_access_lru_lock);
1952 nfs_access_free_list(&head);
1966 list_move(&entry->
lru, head);
1978 spin_lock(&nfs_access_lru_lock);
1980 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
1982 spin_lock(&inode->
i_lock);
1983 __nfs_access_zap_cache(NFS_I(inode), &head);
1984 spin_unlock(&inode->
i_lock);
1985 spin_unlock(&nfs_access_lru_lock);
1986 nfs_access_free_list(&head);
1990 static struct nfs_access_entry *nfs_access_search_rbtree(
struct inode *inode,
struct rpc_cred *cred)
1992 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2000 else if (cred > entry->
cred)
2008 static int nfs_access_get_cached(
struct inode *inode,
struct rpc_cred *cred,
struct nfs_access_entry *res)
2014 spin_lock(&inode->
i_lock);
2017 cache = nfs_access_search_rbtree(inode, cred);
2020 if (!nfs_have_delegated_attributes(inode) &&
2029 spin_unlock(&inode->
i_lock);
2034 spin_unlock(&inode->
i_lock);
2035 nfs_access_free_entry(cache);
2038 spin_unlock(&inode->
i_lock);
2043 static void nfs_access_add_rbtree(
struct inode *inode,
struct nfs_access_entry *
set)
2051 spin_lock(&inode->
i_lock);
2052 while (*p !=
NULL) {
2058 else if (
set->cred > entry->
cred)
2063 rb_link_node(&
set->rb_node, parent, p);
2066 spin_unlock(&inode->
i_lock);
2072 spin_unlock(&inode->
i_lock);
2073 nfs_access_free_entry(entry);
2082 cache->
jiffies =
set->jiffies;
2083 cache->
cred = get_rpccred(
set->cred);
2084 cache->
mask =
set->mask;
2086 nfs_access_add_rbtree(inode, cache);
2090 atomic_long_inc(&nfs_access_nr_entries);
2095 spin_lock(&nfs_access_lru_lock);
2098 &nfs_access_lru_list);
2099 spin_unlock(&nfs_access_lru_lock);
2117 static int nfs_do_access(
struct inode *inode,
struct rpc_cred *cred,
int mask)
2122 status = nfs_access_get_cached(inode, cred, &cache);
2130 status = NFS_PROTO(inode)->access(inode, &cache);
2146 static int nfs_open_permission_mask(
int openflags)
2150 if ((openflags & O_ACCMODE) !=
O_WRONLY)
2152 if ((openflags & O_ACCMODE) !=
O_RDONLY)
2159 int nfs_may_open(
struct inode *inode,
struct rpc_cred *cred,
int openflags)
2161 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2167 struct rpc_cred *cred;
2201 if (!NFS_PROTO(inode)->
access)
2205 if (!IS_ERR(cred)) {
2206 res = nfs_do_access(inode, cred, mask);
2209 res = PTR_ERR(cred);
2211 if (!res && (mask &
MAY_EXEC) && !execute_ok(inode))
2214 dfprintk(VFS,
"NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2215 inode->
i_sb->s_id, inode->
i_ino, mask, res);