hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/nfs/dir.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/fs/nfs/dir.c
34 *
....@@ -57,7 +58,7 @@
5758 const struct file_operations nfs_dir_operations = {
5859 .llseek = nfs_llseek_dir,
5960 .read = generic_read_dir,
60
- .iterate = nfs_readdir,
61
+ .iterate_shared = nfs_readdir,
6162 .open = nfs_opendir,
6263 .release = nfs_closedir,
6364 .fsync = nfs_fsync_dir,
....@@ -67,7 +68,7 @@
6768 .freepage = nfs_readdir_clear_array,
6869 };
6970
70
-static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
71
+static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred)
7172 {
7273 struct nfs_inode *nfsi = NFS_I(dir);
7374 struct nfs_open_dir_context *ctx;
....@@ -77,8 +78,12 @@
7778 ctx->attr_gencount = nfsi->attr_gencount;
7879 ctx->dir_cookie = 0;
7980 ctx->dup_cookie = 0;
80
- ctx->cred = get_rpccred(cred);
81
+ ctx->cred = get_cred(cred);
8182 spin_lock(&dir->i_lock);
83
+ if (list_empty(&nfsi->open_files) &&
84
+ (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
85
+ nfsi->cache_validity |= NFS_INO_INVALID_DATA |
86
+ NFS_INO_REVAL_FORCED;
8287 list_add(&ctx->list, &nfsi->open_files);
8388 spin_unlock(&dir->i_lock);
8489 return ctx;
....@@ -91,7 +96,7 @@
9196 spin_lock(&dir->i_lock);
9297 list_del(&ctx->list);
9398 spin_unlock(&dir->i_lock);
94
- put_rpccred(ctx->cred);
99
+ put_cred(ctx->cred);
95100 kfree(ctx);
96101 }
97102
....@@ -103,23 +108,18 @@
103108 {
104109 int res = 0;
105110 struct nfs_open_dir_context *ctx;
106
- struct rpc_cred *cred;
107111
108112 dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
109113
110114 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
111115
112
- cred = rpc_lookup_cred();
113
- if (IS_ERR(cred))
114
- return PTR_ERR(cred);
115
- ctx = alloc_nfs_open_dir_context(inode, cred);
116
+ ctx = alloc_nfs_open_dir_context(inode, current_cred());
116117 if (IS_ERR(ctx)) {
117118 res = PTR_ERR(ctx);
118119 goto out;
119120 }
120121 filp->private_data = ctx;
121122 out:
122
- put_rpccred(cred);
123123 return res;
124124 }
125125
....@@ -141,10 +141,9 @@
141141 int size;
142142 int eof_index;
143143 u64 last_cookie;
144
- struct nfs_cache_array_entry array[0];
144
+ struct nfs_cache_array_entry array[];
145145 };
146146
147
-typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool);
148147 typedef struct {
149148 struct file *file;
150149 struct page *page;
....@@ -153,8 +152,9 @@
153152 u64 *dir_cookie;
154153 u64 last_cookie;
155154 loff_t current_index;
156
- decode_dirent_t decode;
155
+ loff_t prev_index;
157156
157
+ unsigned long dir_verifier;
158158 unsigned long timestamp;
159159 unsigned long gencount;
160160 unsigned int cache_entry_index;
....@@ -198,7 +198,7 @@
198198 int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
199199 {
200200 string->len = len;
201
- string->name = kmemdup(name, len, GFP_KERNEL);
201
+ string->name = kmemdup_nul(name, len, GFP_KERNEL);
202202 if (string->name == NULL)
203203 return -ENOMEM;
204204 /*
....@@ -237,6 +237,25 @@
237237 out:
238238 kunmap(page);
239239 return ret;
240
+}
241
+
242
+static inline
243
+int is_32bit_api(void)
244
+{
245
+#ifdef CONFIG_COMPAT
246
+ return in_compat_syscall();
247
+#else
248
+ return (BITS_PER_LONG == 32);
249
+#endif
250
+}
251
+
252
+static
253
+bool nfs_readdir_use_cookie(const struct file *filp)
254
+{
255
+ if ((filp->f_mode & FMODE_32BITHASH) ||
256
+ (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
257
+ return false;
258
+ return true;
240259 }
241260
242261 static
....@@ -288,7 +307,7 @@
288307 !nfs_readdir_inode_mapping_valid(nfsi)) {
289308 ctx->duped = 0;
290309 ctx->attr_gencount = nfsi->attr_gencount;
291
- } else if (new_pos < desc->ctx->pos) {
310
+ } else if (new_pos < desc->prev_index) {
292311 if (ctx->duped > 0
293312 && ctx->dup_cookie == *desc->dir_cookie) {
294313 if (printk_ratelimit()) {
....@@ -304,7 +323,11 @@
304323 ctx->dup_cookie = *desc->dir_cookie;
305324 ctx->duped = -1;
306325 }
307
- desc->ctx->pos = new_pos;
326
+ if (nfs_readdir_use_cookie(desc->file))
327
+ desc->ctx->pos = *desc->dir_cookie;
328
+ else
329
+ desc->ctx->pos = new_pos;
330
+ desc->prev_index = new_pos;
308331 desc->cache_entry_index = i;
309332 return 0;
310333 }
....@@ -346,13 +369,14 @@
346369 struct nfs_entry *entry, struct file *file, struct inode *inode)
347370 {
348371 struct nfs_open_dir_context *ctx = file->private_data;
349
- struct rpc_cred *cred = ctx->cred;
372
+ const struct cred *cred = ctx->cred;
350373 unsigned long timestamp, gencount;
351374 int error;
352375
353376 again:
354377 timestamp = jiffies;
355378 gencount = nfs_inc_attr_generation_counter();
379
+ desc->dir_verifier = nfs_save_change_attribute(inode);
356380 error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
357381 NFS_SERVER(inode)->dtsize, desc->plus);
358382 if (error < 0) {
....@@ -374,9 +398,10 @@
374398 static int xdr_decode(nfs_readdir_descriptor_t *desc,
375399 struct nfs_entry *entry, struct xdr_stream *xdr)
376400 {
401
+ struct inode *inode = file_inode(desc->file);
377402 int error;
378403
379
- error = desc->decode(xdr, entry, desc->plus);
404
+ error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
380405 if (error)
381406 return error;
382407 entry->fattr->time_start = desc->timestamp;
....@@ -449,18 +474,19 @@
449474 if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
450475 !list_empty(&nfsi->open_files)) {
451476 set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
452
- invalidate_mapping_pages(dir->i_mapping, 0, -1);
477
+ invalidate_mapping_pages(dir->i_mapping,
478
+ nfsi->page_index + 1, -1);
453479 }
454480 }
455481
456482 static
457
-void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
483
+void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
484
+ unsigned long dir_verifier)
458485 {
459486 struct qstr filename = QSTR_INIT(entry->name, entry->len);
460
- DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
487
+ DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
461488 struct dentry *dentry;
462489 struct dentry *alias;
463
- struct inode *dir = d_inode(parent);
464490 struct inode *inode;
465491 int status;
466492
....@@ -499,7 +525,7 @@
499525 if (nfs_same_file(dentry, entry)) {
500526 if (!entry->fh->size)
501527 goto out;
502
- nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
528
+ nfs_set_verifier(dentry, dir_verifier);
503529 status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
504530 if (!status)
505531 nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
....@@ -525,7 +551,7 @@
525551 dput(dentry);
526552 dentry = alias;
527553 }
528
- nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
554
+ nfs_set_verifier(dentry, dir_verifier);
529555 out:
530556 dput(dentry);
531557 }
....@@ -566,7 +592,8 @@
566592 count++;
567593
568594 if (desc->plus)
569
- nfs_prime_dcache(file_dentry(desc->file), entry);
595
+ nfs_prime_dcache(file_dentry(desc->file), entry,
596
+ desc->dir_verifier);
570597
571598 status = nfs_readdir_add_to_array(entry, page);
572599 if (status != 0)
....@@ -594,8 +621,8 @@
594621 }
595622
596623 /*
597
- * nfs_readdir_large_page will allocate pages that must be freed with a call
598
- * to nfs_readdir_free_pagearray
624
+ * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
625
+ * to nfs_readdir_free_pages()
599626 */
600627 static
601628 int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
....@@ -723,6 +750,8 @@
723750 static
724751 int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
725752 {
753
+ struct inode *inode = file_inode(desc->file);
754
+ struct nfs_inode *nfsi = NFS_I(inode);
726755 int res;
727756
728757 desc->page = get_cache_page(desc);
....@@ -734,8 +763,10 @@
734763 res = -EAGAIN;
735764 if (desc->page->mapping != NULL) {
736765 res = nfs_readdir_search_array(desc);
737
- if (res == 0)
766
+ if (res == 0) {
767
+ nfsi->page_index = desc->page_index;
738768 return 0;
769
+ }
739770 }
740771 unlock_page(desc->page);
741772 error:
....@@ -751,6 +782,7 @@
751782
752783 if (desc->page_index == 0) {
753784 desc->current_index = 0;
785
+ desc->prev_index = 0;
754786 desc->last_cookie = 0;
755787 }
756788 do {
....@@ -781,11 +813,14 @@
781813 desc->eof = true;
782814 break;
783815 }
784
- desc->ctx->pos++;
785816 if (i < (array->size-1))
786817 *desc->dir_cookie = array->array[i+1].cookie;
787818 else
788819 *desc->dir_cookie = array->last_cookie;
820
+ if (nfs_readdir_use_cookie(file))
821
+ desc->ctx->pos = *desc->dir_cookie;
822
+ else
823
+ desc->ctx->pos++;
789824 if (ctx->duped != 0)
790825 ctx->duped = 1;
791826 }
....@@ -855,9 +890,14 @@
855890 {
856891 struct dentry *dentry = file_dentry(file);
857892 struct inode *inode = d_inode(dentry);
858
- nfs_readdir_descriptor_t my_desc,
859
- *desc = &my_desc;
860893 struct nfs_open_dir_context *dir_ctx = file->private_data;
894
+ nfs_readdir_descriptor_t my_desc = {
895
+ .file = file,
896
+ .ctx = ctx,
897
+ .dir_cookie = &dir_ctx->dir_cookie,
898
+ .plus = nfs_use_readdirplus(inode, ctx),
899
+ },
900
+ *desc = &my_desc;
861901 int res = 0;
862902
863903 dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
....@@ -870,14 +910,6 @@
870910 * to either find the entry with the appropriate number or
871911 * revalidate the cookie.
872912 */
873
- memset(desc, 0, sizeof(*desc));
874
-
875
- desc->file = file;
876
- desc->ctx = ctx;
877
- desc->dir_cookie = &dir_ctx->dir_cookie;
878
- desc->decode = NFS_PROTO(inode)->decode_dirent;
879
- desc->plus = nfs_use_readdirplus(inode, ctx);
880
-
881913 if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
882914 res = nfs_revalidate_mapping(inode, file->f_mapping);
883915 if (res < 0)
....@@ -923,7 +955,6 @@
923955
924956 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
925957 {
926
- struct inode *inode = file_inode(filp);
927958 struct nfs_open_dir_context *dir_ctx = filp->private_data;
928959
929960 dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
....@@ -935,24 +966,27 @@
935966 case SEEK_SET:
936967 if (offset < 0)
937968 return -EINVAL;
938
- inode_lock(inode);
969
+ spin_lock(&filp->f_lock);
939970 break;
940971 case SEEK_CUR:
941972 if (offset == 0)
942973 return filp->f_pos;
943
- inode_lock(inode);
974
+ spin_lock(&filp->f_lock);
944975 offset += filp->f_pos;
945976 if (offset < 0) {
946
- inode_unlock(inode);
977
+ spin_unlock(&filp->f_lock);
947978 return -EINVAL;
948979 }
949980 }
950981 if (offset != filp->f_pos) {
951982 filp->f_pos = offset;
952
- dir_ctx->dir_cookie = 0;
983
+ if (nfs_readdir_use_cookie(filp))
984
+ dir_ctx->dir_cookie = offset;
985
+ else
986
+ dir_ctx->dir_cookie = 0;
953987 dir_ctx->duped = 0;
954988 }
955
- inode_unlock(inode);
989
+ spin_unlock(&filp->f_lock);
956990 return offset;
957991 }
958992
....@@ -963,31 +997,125 @@
963997 static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
964998 int datasync)
965999 {
966
- struct inode *inode = file_inode(filp);
967
-
9681000 dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
9691001
970
- inode_lock(inode);
971
- nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
972
- inode_unlock(inode);
1002
+ nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
9731003 return 0;
9741004 }
9751005
9761006 /**
9771007 * nfs_force_lookup_revalidate - Mark the directory as having changed
978
- * @dir - pointer to directory inode
1008
+ * @dir: pointer to directory inode
9791009 *
9801010 * This forces the revalidation code in nfs_lookup_revalidate() to do a
9811011 * full lookup on all child dentries of 'dir' whenever a change occurs
9821012 * on the server that might have invalidated our dcache.
9831013 *
1014
+ * Note that we reserve bit '0' as a tag to let us know when a dentry
1015
+ * was revalidated while holding a delegation on its inode.
1016
+ *
9841017 * The caller should be holding dir->i_lock
9851018 */
9861019 void nfs_force_lookup_revalidate(struct inode *dir)
9871020 {
988
- NFS_I(dir)->cache_change_attribute++;
1021
+ NFS_I(dir)->cache_change_attribute += 2;
9891022 }
9901023 EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1024
+
1025
+/**
1026
+ * nfs_verify_change_attribute - Detects NFS remote directory changes
1027
+ * @dir: pointer to parent directory inode
1028
+ * @verf: previously saved change attribute
1029
+ *
1030
+ * Return "false" if the verifiers doesn't match the change attribute.
1031
+ * This would usually indicate that the directory contents have changed on
1032
+ * the server, and that any dentries need revalidating.
1033
+ */
1034
+static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1035
+{
1036
+ return (verf & ~1UL) == nfs_save_change_attribute(dir);
1037
+}
1038
+
1039
+static void nfs_set_verifier_delegated(unsigned long *verf)
1040
+{
1041
+ *verf |= 1UL;
1042
+}
1043
+
1044
+#if IS_ENABLED(CONFIG_NFS_V4)
1045
+static void nfs_unset_verifier_delegated(unsigned long *verf)
1046
+{
1047
+ *verf &= ~1UL;
1048
+}
1049
+#endif /* IS_ENABLED(CONFIG_NFS_V4) */
1050
+
1051
+static bool nfs_test_verifier_delegated(unsigned long verf)
1052
+{
1053
+ return verf & 1;
1054
+}
1055
+
1056
+static bool nfs_verifier_is_delegated(struct dentry *dentry)
1057
+{
1058
+ return nfs_test_verifier_delegated(dentry->d_time);
1059
+}
1060
+
1061
+static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1062
+{
1063
+ struct inode *inode = d_inode(dentry);
1064
+ struct inode *dir = d_inode(dentry->d_parent);
1065
+
1066
+ if (!nfs_verify_change_attribute(dir, verf))
1067
+ return;
1068
+ if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1069
+ nfs_set_verifier_delegated(&verf);
1070
+ dentry->d_time = verf;
1071
+}
1072
+
1073
+/**
1074
+ * nfs_set_verifier - save a parent directory verifier in the dentry
1075
+ * @dentry: pointer to dentry
1076
+ * @verf: verifier to save
1077
+ *
1078
+ * Saves the parent directory verifier in @dentry. If the inode has
1079
+ * a delegation, we also tag the dentry as having been revalidated
1080
+ * while holding a delegation so that we know we don't have to
1081
+ * look it up again after a directory change.
1082
+ */
1083
+void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1084
+{
1085
+
1086
+ spin_lock(&dentry->d_lock);
1087
+ nfs_set_verifier_locked(dentry, verf);
1088
+ spin_unlock(&dentry->d_lock);
1089
+}
1090
+EXPORT_SYMBOL_GPL(nfs_set_verifier);
1091
+
1092
+#if IS_ENABLED(CONFIG_NFS_V4)
1093
+/**
1094
+ * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1095
+ * @inode: pointer to inode
1096
+ *
1097
+ * Iterates through the dentries in the inode alias list and clears
1098
+ * the tag used to indicate that the dentry has been revalidated
1099
+ * while holding a delegation.
1100
+ * This function is intended for use when the delegation is being
1101
+ * returned or revoked.
1102
+ */
1103
+void nfs_clear_verifier_delegated(struct inode *inode)
1104
+{
1105
+ struct dentry *alias;
1106
+
1107
+ if (!inode)
1108
+ return;
1109
+ spin_lock(&inode->i_lock);
1110
+ hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1111
+ spin_lock(&alias->d_lock);
1112
+ nfs_unset_verifier_delegated(&alias->d_time);
1113
+ spin_unlock(&alias->d_lock);
1114
+ }
1115
+ spin_unlock(&inode->i_lock);
1116
+}
1117
+EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1118
+#endif /* IS_ENABLED(CONFIG_NFS_V4) */
9911119
9921120 /*
9931121 * A check for whether or not the parent directory has changed.
....@@ -1050,7 +1178,7 @@
10501178 /* A NFSv4 OPEN will revalidate later */
10511179 if (server->caps & NFS_CAP_ATOMIC_OPEN)
10521180 goto out;
1053
- /* Fallthrough */
1181
+ fallthrough;
10541182 case S_IFDIR:
10551183 if (server->flags & NFS_MOUNT_NOCTO)
10561184 break;
....@@ -1071,6 +1199,15 @@
10711199 if (ret != 0)
10721200 return ret;
10731201 goto out;
1202
+}
1203
+
1204
+static void nfs_mark_dir_for_revalidate(struct inode *inode)
1205
+{
1206
+ struct nfs_inode *nfsi = NFS_I(inode);
1207
+
1208
+ spin_lock(&inode->i_lock);
1209
+ nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1210
+ spin_unlock(&inode->i_lock);
10741211 }
10751212
10761213 /*
....@@ -1107,19 +1244,14 @@
11071244 __func__, dentry);
11081245 return 1;
11091246 case 0:
1110
- nfs_mark_for_revalidate(dir);
1111
- if (inode && S_ISDIR(inode->i_mode)) {
1112
- /* Purge readdir caches. */
1113
- nfs_zap_caches(inode);
1114
- /*
1115
- * We can't d_drop the root of a disconnected tree:
1116
- * its d_hash is on the s_anon list and d_drop() would hide
1117
- * it from shrink_dcache_for_unmount(), leading to busy
1118
- * inodes on unmount and further oopses.
1119
- */
1120
- if (IS_ROOT(dentry))
1121
- return 1;
1122
- }
1247
+ /*
1248
+ * We can't d_drop the root of a disconnected tree:
1249
+ * its d_hash is on the s_anon list and d_drop() would hide
1250
+ * it from shrink_dcache_for_unmount(), leading to busy
1251
+ * inodes on unmount and further oopses.
1252
+ */
1253
+ if (inode && IS_ROOT(dentry))
1254
+ return 1;
11231255 dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
11241256 __func__, dentry);
11251257 return 0;
....@@ -1157,6 +1289,7 @@
11571289 struct nfs_fh *fhandle;
11581290 struct nfs_fattr *fattr;
11591291 struct nfs4_label *label;
1292
+ unsigned long dir_verifier;
11601293 int ret;
11611294
11621295 ret = -ENOMEM;
....@@ -1166,10 +1299,18 @@
11661299 if (fhandle == NULL || fattr == NULL || IS_ERR(label))
11671300 goto out;
11681301
1169
- ret = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
1302
+ dir_verifier = nfs_save_change_attribute(dir);
1303
+ ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
11701304 if (ret < 0) {
1171
- if (ret == -ESTALE || ret == -ENOENT)
1305
+ switch (ret) {
1306
+ case -ESTALE:
1307
+ case -ENOENT:
11721308 ret = 0;
1309
+ break;
1310
+ case -ETIMEDOUT:
1311
+ if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1312
+ ret = 1;
1313
+ }
11731314 goto out;
11741315 }
11751316 ret = 0;
....@@ -1179,7 +1320,7 @@
11791320 goto out;
11801321
11811322 nfs_setsecurity(inode, fattr, label);
1182
- nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1323
+ nfs_set_verifier(dentry, dir_verifier);
11831324
11841325 /* set a readdirplus hint that we had a cache miss */
11851326 nfs_force_use_readdirplus(dir);
....@@ -1188,6 +1329,13 @@
11881329 nfs_free_fattr(fattr);
11891330 nfs_free_fhandle(fhandle);
11901331 nfs4_label_free(label);
1332
+
1333
+ /*
1334
+ * If the lookup failed despite the dentry change attribute being
1335
+ * a match, then we should revalidate the directory cache.
1336
+ */
1337
+ if (!ret && nfs_verify_change_attribute(dir, dentry->d_time))
1338
+ nfs_mark_dir_for_revalidate(dir);
11911339 return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
11921340 }
11931341
....@@ -1221,7 +1369,7 @@
12211369 goto out_bad;
12221370 }
12231371
1224
- if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1372
+ if (nfs_verifier_is_delegated(dentry))
12251373 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
12261374
12271375 /* Force a full look up iff the parent directory has changed */
....@@ -1230,7 +1378,7 @@
12301378 error = nfs_lookup_verify_inode(inode, flags);
12311379 if (error) {
12321380 if (error == -ESTALE)
1233
- nfs_zap_caches(dir);
1381
+ nfs_mark_dir_for_revalidate(dir);
12341382 goto out_bad;
12351383 }
12361384 nfs_advise_use_readdirplus(dir);
....@@ -1406,6 +1554,7 @@
14061554 struct nfs_fh *fhandle = NULL;
14071555 struct nfs_fattr *fattr = NULL;
14081556 struct nfs4_label *label = NULL;
1557
+ unsigned long dir_verifier;
14091558 int error;
14101559
14111560 dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
....@@ -1431,8 +1580,9 @@
14311580 if (IS_ERR(label))
14321581 goto out;
14331582
1583
+ dir_verifier = nfs_save_change_attribute(dir);
14341584 trace_nfs_lookup_enter(dir, dentry, flags);
1435
- error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
1585
+ error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
14361586 if (error == -ENOENT)
14371587 goto no_entry;
14381588 if (error < 0) {
....@@ -1454,7 +1604,7 @@
14541604 goto out_label;
14551605 dentry = res;
14561606 }
1457
- nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1607
+ nfs_set_verifier(dentry, dir_verifier);
14581608 out_label:
14591609 trace_nfs_lookup_exit(dir, dentry, flags, error);
14601610 nfs4_label_free(label);
....@@ -1477,16 +1627,6 @@
14771627 .d_release = nfs_d_release,
14781628 };
14791629 EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
1480
-
1481
-static fmode_t flags_to_mode(int flags)
1482
-{
1483
- fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1484
- if ((flags & O_ACCMODE) != O_WRONLY)
1485
- res |= FMODE_READ;
1486
- if ((flags & O_ACCMODE) != O_RDONLY)
1487
- res |= FMODE_WRITE;
1488
- return res;
1489
-}
14901630
14911631 static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
14921632 {
....@@ -1520,7 +1660,7 @@
15201660 struct file *file, unsigned open_flags,
15211661 umode_t mode)
15221662 {
1523
- DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
1663
+ DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
15241664 struct nfs_open_context *ctx;
15251665 struct dentry *res;
15261666 struct iattr attr = { .ia_valid = ATTR_OPEN };
....@@ -1677,7 +1817,7 @@
16771817 if (inode == NULL)
16781818 goto full_reval;
16791819
1680
- if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1820
+ if (nfs_verifier_is_delegated(dentry))
16811821 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
16821822
16831823 /* NFS only supports OPEN on regular files */
....@@ -1697,7 +1837,7 @@
16971837 reval_dentry:
16981838 if (flags & LOOKUP_RCU)
16991839 return -ECHILD;
1700
- return nfs_lookup_revalidate_dentry(dir, dentry, inode);;
1840
+ return nfs_lookup_revalidate_dentry(dir, dentry, inode);
17011841
17021842 full_reval:
17031843 return nfs_do_lookup_revalidate(dir, dentry, flags);
....@@ -1711,10 +1851,8 @@
17111851
17121852 #endif /* CONFIG_NFSV4 */
17131853
1714
-/*
1715
- * Code common to create, mkdir, and mknod.
1716
- */
1717
-int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1854
+struct dentry *
1855
+nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
17181856 struct nfs_fattr *fattr,
17191857 struct nfs4_label *label)
17201858 {
....@@ -1722,15 +1860,12 @@
17221860 struct inode *dir = d_inode(parent);
17231861 struct inode *inode;
17241862 struct dentry *d;
1725
- int error = -EACCES;
1863
+ int error;
17261864
17271865 d_drop(dentry);
17281866
1729
- /* We may have been initialized further down */
1730
- if (d_really_is_positive(dentry))
1731
- goto out;
17321867 if (fhandle->size == 0) {
1733
- error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
1868
+ error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
17341869 if (error)
17351870 goto out_error;
17361871 }
....@@ -1744,18 +1879,31 @@
17441879 }
17451880 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
17461881 d = d_splice_alias(inode, dentry);
1747
- if (IS_ERR(d)) {
1748
- error = PTR_ERR(d);
1749
- goto out_error;
1750
- }
1751
- dput(d);
17521882 out:
17531883 dput(parent);
1754
- return 0;
1884
+ return d;
17551885 out_error:
1756
- nfs_mark_for_revalidate(dir);
1757
- dput(parent);
1758
- return error;
1886
+ d = ERR_PTR(error);
1887
+ goto out;
1888
+}
1889
+EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
1890
+
1891
+/*
1892
+ * Code common to create, mkdir, and mknod.
1893
+ */
1894
+int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1895
+ struct nfs_fattr *fattr,
1896
+ struct nfs4_label *label)
1897
+{
1898
+ struct dentry *d;
1899
+
1900
+ d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
1901
+ if (IS_ERR(d))
1902
+ return PTR_ERR(d);
1903
+
1904
+ /* Callers don't care */
1905
+ dput(d);
1906
+ return 0;
17591907 }
17601908 EXPORT_SYMBOL_GPL(nfs_instantiate);
17611909
....@@ -1858,11 +2006,7 @@
18582006
18592007 trace_nfs_rmdir_enter(dir, dentry);
18602008 if (d_really_is_positive(dentry)) {
1861
-#ifdef CONFIG_PREEMPT_RT_BASE
1862
- down(&NFS_I(d_inode(dentry))->rmdir_sem);
1863
-#else
18642009 down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1865
-#endif
18662010 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18672011 /* Ensure the VFS deletes this inode */
18682012 switch (error) {
....@@ -1872,11 +2016,7 @@
18722016 case -ENOENT:
18732017 nfs_dentry_handle_enoent(dentry);
18742018 }
1875
-#ifdef CONFIG_PREEMPT_RT_BASE
1876
- up(&NFS_I(d_inode(dentry))->rmdir_sem);
1877
-#else
18782019 up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1879
-#endif
18802020 } else
18812021 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18822022 trace_nfs_rmdir_exit(dir, dentry, error);
....@@ -2188,13 +2328,13 @@
21882328 static LIST_HEAD(nfs_access_lru_list);
21892329 static atomic_long_t nfs_access_nr_entries;
21902330
2191
-static unsigned long nfs_access_max_cachesize = ULONG_MAX;
2331
+static unsigned long nfs_access_max_cachesize = 4*1024*1024;
21922332 module_param(nfs_access_max_cachesize, ulong, 0644);
21932333 MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
21942334
21952335 static void nfs_access_free_entry(struct nfs_access_entry *entry)
21962336 {
2197
- put_rpccred(entry->cred);
2337
+ put_cred(entry->cred);
21982338 kfree_rcu(entry, rcu_head);
21992339 smp_mb__before_atomic();
22002340 atomic_long_dec(&nfs_access_nr_entries);
....@@ -2320,17 +2460,18 @@
23202460 }
23212461 EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
23222462
2323
-static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2463
+static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
23242464 {
23252465 struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2326
- struct nfs_access_entry *entry;
23272466
23282467 while (n != NULL) {
2329
- entry = rb_entry(n, struct nfs_access_entry, rb_node);
2468
+ struct nfs_access_entry *entry =
2469
+ rb_entry(n, struct nfs_access_entry, rb_node);
2470
+ int cmp = cred_fscmp(cred, entry->cred);
23302471
2331
- if (cred < entry->cred)
2472
+ if (cmp < 0)
23322473 n = n->rb_left;
2333
- else if (cred > entry->cred)
2474
+ else if (cmp > 0)
23342475 n = n->rb_right;
23352476 else
23362477 return entry;
....@@ -2338,7 +2479,7 @@
23382479 return NULL;
23392480 }
23402481
2341
-static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res, bool may_block)
2482
+static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
23422483 {
23432484 struct nfs_inode *nfsi = NFS_I(inode);
23442485 struct nfs_access_entry *cache;
....@@ -2356,11 +2497,11 @@
23562497 /* Found an entry, is our attribute cache valid? */
23572498 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
23582499 break;
2500
+ if (!retry)
2501
+ break;
23592502 err = -ECHILD;
23602503 if (!may_block)
23612504 goto out;
2362
- if (!retry)
2363
- goto out_zap;
23642505 spin_unlock(&inode->i_lock);
23652506 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
23662507 if (err)
....@@ -2368,8 +2509,7 @@
23682509 spin_lock(&inode->i_lock);
23692510 retry = false;
23702511 }
2371
- res->cred = cache->cred;
2372
- res->mask = cache->mask;
2512
+ *mask = cache->mask;
23732513 list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
23742514 err = 0;
23752515 out:
....@@ -2381,7 +2521,7 @@
23812521 return -ENOENT;
23822522 }
23832523
2384
-static int nfs_access_get_cached_rcu(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
2524
+static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
23852525 {
23862526 /* Only check the most recently returned cache entry,
23872527 * but do it without locking.
....@@ -2394,22 +2534,35 @@
23942534 rcu_read_lock();
23952535 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
23962536 goto out;
2397
- lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2537
+ lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
23982538 cache = list_entry(lh, struct nfs_access_entry, lru);
23992539 if (lh == &nfsi->access_cache_entry_lru ||
2400
- cred != cache->cred)
2540
+ cred_fscmp(cred, cache->cred) != 0)
24012541 cache = NULL;
24022542 if (cache == NULL)
24032543 goto out;
24042544 if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
24052545 goto out;
2406
- res->cred = cache->cred;
2407
- res->mask = cache->mask;
2546
+ *mask = cache->mask;
24082547 err = 0;
24092548 out:
24102549 rcu_read_unlock();
24112550 return err;
24122551 }
2552
+
2553
+int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2554
+ u32 *mask, bool may_block)
2555
+{
2556
+ int status;
2557
+
2558
+ status = nfs_access_get_cached_rcu(inode, cred, mask);
2559
+ if (status != 0)
2560
+ status = nfs_access_get_cached_locked(inode, cred, mask,
2561
+ may_block);
2562
+
2563
+ return status;
2564
+}
2565
+EXPORT_SYMBOL_GPL(nfs_access_get_cached);
24132566
24142567 static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
24152568 {
....@@ -2418,15 +2571,17 @@
24182571 struct rb_node **p = &root_node->rb_node;
24192572 struct rb_node *parent = NULL;
24202573 struct nfs_access_entry *entry;
2574
+ int cmp;
24212575
24222576 spin_lock(&inode->i_lock);
24232577 while (*p != NULL) {
24242578 parent = *p;
24252579 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2580
+ cmp = cred_fscmp(set->cred, entry->cred);
24262581
2427
- if (set->cred < entry->cred)
2582
+ if (cmp < 0)
24282583 p = &parent->rb_left;
2429
- else if (set->cred > entry->cred)
2584
+ else if (cmp > 0)
24302585 p = &parent->rb_right;
24312586 else
24322587 goto found;
....@@ -2450,7 +2605,7 @@
24502605 if (cache == NULL)
24512606 return;
24522607 RB_CLEAR_NODE(&cache->rb_node);
2453
- cache->cred = get_rpccred(set->cred);
2608
+ cache->cred = get_cred(set->cred);
24542609 cache->mask = set->mask;
24552610
24562611 /* The above field assignments must be visible
....@@ -2514,18 +2669,16 @@
25142669 }
25152670 EXPORT_SYMBOL_GPL(nfs_access_set_mask);
25162671
2517
-static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2672
+static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
25182673 {
25192674 struct nfs_access_entry cache;
25202675 bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2521
- int cache_mask;
2676
+ int cache_mask = -1;
25222677 int status;
25232678
25242679 trace_nfs_access_enter(inode);
25252680
2526
- status = nfs_access_get_cached_rcu(inode, cred, &cache);
2527
- if (status != 0)
2528
- status = nfs_access_get_cached(inode, cred, &cache, may_block);
2681
+ status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
25292682 if (status == 0)
25302683 goto out_cached;
25312684
....@@ -2537,6 +2690,10 @@
25372690 * Determine which access bits we want to ask for...
25382691 */
25392692 cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
2693
+ if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
2694
+ cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
2695
+ NFS_ACCESS_XALIST;
2696
+ }
25402697 if (S_ISDIR(inode->i_mode))
25412698 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
25422699 else
....@@ -2545,9 +2702,10 @@
25452702 status = NFS_PROTO(inode)->access(inode, &cache);
25462703 if (status != 0) {
25472704 if (status == -ESTALE) {
2548
- nfs_zap_caches(inode);
25492705 if (!S_ISDIR(inode->i_mode))
2550
- set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2706
+ nfs_set_inode_stale(inode);
2707
+ else
2708
+ nfs_zap_caches(inode);
25512709 }
25522710 goto out;
25532711 }
....@@ -2557,7 +2715,7 @@
25572715 if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
25582716 status = -EACCES;
25592717 out:
2560
- trace_nfs_access_exit(inode, status);
2718
+ trace_nfs_access_exit(inode, mask, cache_mask, status);
25612719 return status;
25622720 }
25632721
....@@ -2578,7 +2736,7 @@
25782736 return mask;
25792737 }
25802738
2581
-int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2739
+int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
25822740 {
25832741 return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
25842742 }
....@@ -2603,7 +2761,7 @@
26032761
26042762 int nfs_permission(struct inode *inode, int mask)
26052763 {
2606
- struct rpc_cred *cred;
2764
+ const struct cred *cred = current_cred();
26072765 int res = 0;
26082766
26092767 nfs_inc_stats(inode, NFSIOS_VFSACCESS);
....@@ -2635,23 +2793,7 @@
26352793 if (!NFS_PROTO(inode)->access)
26362794 goto out_notsup;
26372795
2638
- /* Always try fast lookups first */
2639
- rcu_read_lock();
2640
- cred = rpc_lookup_cred_nonblock();
2641
- if (!IS_ERR(cred))
2642
- res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2643
- else
2644
- res = PTR_ERR(cred);
2645
- rcu_read_unlock();
2646
- if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2647
- /* Fast lookup failed, try the slow way */
2648
- cred = rpc_lookup_cred();
2649
- if (!IS_ERR(cred)) {
2650
- res = nfs_do_access(inode, cred, mask);
2651
- put_rpccred(cred);
2652
- } else
2653
- res = PTR_ERR(cred);
2654
- }
2796
+ res = nfs_do_access(inode, cred, mask);
26552797 out:
26562798 if (!res && (mask & MAY_EXEC))
26572799 res = nfs_execute_ok(inode, mask);