NFS: Trace effects of the readdirplus heuristic
[linux-2.6-microblaze.git] / fs / nfs / dir.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/nfs/dir.c
4  *
5  *  Copyright (C) 1992  Rick Sladkey
6  *
7  *  nfs directory handling functions
8  *
9  * 10 Apr 1996  Added silly rename for unlink   --okir
10  * 28 Sep 1996  Improved directory cache --okir
11  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de 
12  *              Re-implemented silly rename for unlink, newly implemented
13  *              silly rename for nfs_rename() following the suggestions
14  *              of Olaf Kirch (okir) found in this file.
15  *              Following Linus comments on my original hack, this version
16  *              depends only on the dcache stuff and doesn't touch the inode
17  *              layer (iput() and friends).
18  *  6 Jun 1999  Cache readdir lookups in the page cache. -DaveM
19  */
20
21 #include <linux/compat.h>
22 #include <linux/module.h>
23 #include <linux/time.h>
24 #include <linux/errno.h>
25 #include <linux/stat.h>
26 #include <linux/fcntl.h>
27 #include <linux/string.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/mm.h>
31 #include <linux/sunrpc/clnt.h>
32 #include <linux/nfs_fs.h>
33 #include <linux/nfs_mount.h>
34 #include <linux/pagemap.h>
35 #include <linux/pagevec.h>
36 #include <linux/namei.h>
37 #include <linux/mount.h>
38 #include <linux/swap.h>
39 #include <linux/sched.h>
40 #include <linux/kmemleak.h>
41 #include <linux/xattr.h>
42
43 #include "delegation.h"
44 #include "iostat.h"
45 #include "internal.h"
46 #include "fscache.h"
47
48 #include "nfstrace.h"
49
50 /* #define NFS_DEBUG_VERBOSE 1 */
51
52 static int nfs_opendir(struct inode *, struct file *);
53 static int nfs_closedir(struct inode *, struct file *);
54 static int nfs_readdir(struct file *, struct dir_context *);
55 static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
56 static loff_t nfs_llseek_dir(struct file *, loff_t, int);
57 static void nfs_readdir_clear_array(struct page*);
58
59 const struct file_operations nfs_dir_operations = {
60         .llseek         = nfs_llseek_dir,
61         .read           = generic_read_dir,
62         .iterate_shared = nfs_readdir,
63         .open           = nfs_opendir,
64         .release        = nfs_closedir,
65         .fsync          = nfs_fsync_dir,
66 };
67
68 const struct address_space_operations nfs_dir_aops = {
69         .freepage = nfs_readdir_clear_array,
70 };
71
72 #define NFS_INIT_DTSIZE PAGE_SIZE
73
74 static struct nfs_open_dir_context *
75 alloc_nfs_open_dir_context(struct inode *dir)
76 {
77         struct nfs_inode *nfsi = NFS_I(dir);
78         struct nfs_open_dir_context *ctx;
79
80         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
81         if (ctx != NULL) {
82                 ctx->attr_gencount = nfsi->attr_gencount;
83                 ctx->dtsize = NFS_INIT_DTSIZE;
84                 spin_lock(&dir->i_lock);
85                 if (list_empty(&nfsi->open_files) &&
86                     (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
87                         nfs_set_cache_invalid(dir,
88                                               NFS_INO_INVALID_DATA |
89                                                       NFS_INO_REVAL_FORCED);
90                 list_add_tail_rcu(&ctx->list, &nfsi->open_files);
91                 memcpy(ctx->verf, nfsi->cookieverf, sizeof(ctx->verf));
92                 spin_unlock(&dir->i_lock);
93                 return ctx;
94         }
95         return  ERR_PTR(-ENOMEM);
96 }
97
98 static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
99 {
100         spin_lock(&dir->i_lock);
101         list_del_rcu(&ctx->list);
102         spin_unlock(&dir->i_lock);
103         kfree_rcu(ctx, rcu_head);
104 }
105
106 /*
107  * Open file
108  */
109 static int
110 nfs_opendir(struct inode *inode, struct file *filp)
111 {
112         int res = 0;
113         struct nfs_open_dir_context *ctx;
114
115         dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
116
117         nfs_inc_stats(inode, NFSIOS_VFSOPEN);
118
119         ctx = alloc_nfs_open_dir_context(inode);
120         if (IS_ERR(ctx)) {
121                 res = PTR_ERR(ctx);
122                 goto out;
123         }
124         filp->private_data = ctx;
125 out:
126         return res;
127 }
128
129 static int
130 nfs_closedir(struct inode *inode, struct file *filp)
131 {
132         put_nfs_open_dir_context(file_inode(filp), filp->private_data);
133         return 0;
134 }
135
136 struct nfs_cache_array_entry {
137         u64 cookie;
138         u64 ino;
139         const char *name;
140         unsigned int name_len;
141         unsigned char d_type;
142 };
143
144 struct nfs_cache_array {
145         u64 change_attr;
146         u64 last_cookie;
147         unsigned int size;
148         unsigned char page_full : 1,
149                       page_is_eof : 1,
150                       cookies_are_ordered : 1;
151         struct nfs_cache_array_entry array[];
152 };
153
154 struct nfs_readdir_descriptor {
155         struct file     *file;
156         struct page     *page;
157         struct dir_context *ctx;
158         pgoff_t         page_index;
159         pgoff_t         page_index_max;
160         u64             dir_cookie;
161         u64             last_cookie;
162         u64             dup_cookie;
163         loff_t          current_index;
164         loff_t          prev_index;
165
166         __be32          verf[NFS_DIR_VERIFIER_SIZE];
167         unsigned long   dir_verifier;
168         unsigned long   timestamp;
169         unsigned long   gencount;
170         unsigned long   attr_gencount;
171         unsigned int    cache_entry_index;
172         unsigned int    buffer_fills;
173         unsigned int    dtsize;
174         signed char duped;
175         bool plus;
176         bool eob;
177         bool eof;
178 };
179
180 static void nfs_set_dtsize(struct nfs_readdir_descriptor *desc, unsigned int sz)
181 {
182         struct nfs_server *server = NFS_SERVER(file_inode(desc->file));
183         unsigned int maxsize = server->dtsize;
184
185         if (sz > maxsize)
186                 sz = maxsize;
187         if (sz < NFS_MIN_FILE_IO_SIZE)
188                 sz = NFS_MIN_FILE_IO_SIZE;
189         desc->dtsize = sz;
190 }
191
192 static void nfs_shrink_dtsize(struct nfs_readdir_descriptor *desc)
193 {
194         nfs_set_dtsize(desc, desc->dtsize >> 1);
195 }
196
197 static void nfs_grow_dtsize(struct nfs_readdir_descriptor *desc)
198 {
199         nfs_set_dtsize(desc, desc->dtsize << 1);
200 }
201
202 static void nfs_readdir_array_init(struct nfs_cache_array *array)
203 {
204         memset(array, 0, sizeof(struct nfs_cache_array));
205 }
206
207 static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie,
208                                         u64 change_attr)
209 {
210         struct nfs_cache_array *array;
211
212         array = kmap_atomic(page);
213         nfs_readdir_array_init(array);
214         array->change_attr = change_attr;
215         array->last_cookie = last_cookie;
216         array->cookies_are_ordered = 1;
217         kunmap_atomic(array);
218 }
219
220 /*
221  * we are freeing strings created by nfs_add_to_readdir_array()
222  */
223 static
224 void nfs_readdir_clear_array(struct page *page)
225 {
226         struct nfs_cache_array *array;
227         int i;
228
229         array = kmap_atomic(page);
230         for (i = 0; i < array->size; i++)
231                 kfree(array->array[i].name);
232         nfs_readdir_array_init(array);
233         kunmap_atomic(array);
234 }
235
236 static struct page *
237 nfs_readdir_page_array_alloc(u64 last_cookie, gfp_t gfp_flags)
238 {
239         struct page *page = alloc_page(gfp_flags);
240         if (page)
241                 nfs_readdir_page_init_array(page, last_cookie, 0);
242         return page;
243 }
244
245 static void nfs_readdir_page_array_free(struct page *page)
246 {
247         if (page) {
248                 nfs_readdir_clear_array(page);
249                 put_page(page);
250         }
251 }
252
253 static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
254 {
255         array->page_is_eof = 1;
256         array->page_full = 1;
257 }
258
259 static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
260 {
261         return array->page_full;
262 }
263
264 /*
265  * the caller is responsible for freeing qstr.name
266  * when called by nfs_readdir_add_to_array, the strings will be freed in
267  * nfs_clear_readdir_array()
268  */
269 static const char *nfs_readdir_copy_name(const char *name, unsigned int len)
270 {
271         const char *ret = kmemdup_nul(name, len, GFP_KERNEL);
272
273         /*
274          * Avoid a kmemleak false positive. The pointer to the name is stored
275          * in a page cache page which kmemleak does not scan.
276          */
277         if (ret != NULL)
278                 kmemleak_not_leak(ret);
279         return ret;
280 }
281
282 static size_t nfs_readdir_array_maxentries(void)
283 {
284         return (PAGE_SIZE - sizeof(struct nfs_cache_array)) /
285                sizeof(struct nfs_cache_array_entry);
286 }
287
288 /*
289  * Check that the next array entry lies entirely within the page bounds
290  */
291 static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
292 {
293         if (array->page_full)
294                 return -ENOSPC;
295         if (array->size == nfs_readdir_array_maxentries()) {
296                 array->page_full = 1;
297                 return -ENOSPC;
298         }
299         return 0;
300 }
301
302 static
303 int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
304 {
305         struct nfs_cache_array *array;
306         struct nfs_cache_array_entry *cache_entry;
307         const char *name;
308         int ret;
309
310         name = nfs_readdir_copy_name(entry->name, entry->len);
311         if (!name)
312                 return -ENOMEM;
313
314         array = kmap_atomic(page);
315         ret = nfs_readdir_array_can_expand(array);
316         if (ret) {
317                 kfree(name);
318                 goto out;
319         }
320
321         cache_entry = &array->array[array->size];
322         cache_entry->cookie = entry->prev_cookie;
323         cache_entry->ino = entry->ino;
324         cache_entry->d_type = entry->d_type;
325         cache_entry->name_len = entry->len;
326         cache_entry->name = name;
327         array->last_cookie = entry->cookie;
328         if (array->last_cookie <= cache_entry->cookie)
329                 array->cookies_are_ordered = 0;
330         array->size++;
331         if (entry->eof != 0)
332                 nfs_readdir_array_set_eof(array);
333 out:
334         kunmap_atomic(array);
335         return ret;
336 }
337
338 static bool nfs_readdir_page_validate(struct page *page, u64 last_cookie,
339                                       u64 change_attr)
340 {
341         struct nfs_cache_array *array = kmap_atomic(page);
342         int ret = true;
343
344         if (array->change_attr != change_attr)
345                 ret = false;
346         if (array->size > 0 && array->array[0].cookie != last_cookie)
347                 ret = false;
348         kunmap_atomic(array);
349         return ret;
350 }
351
352 static void nfs_readdir_page_unlock_and_put(struct page *page)
353 {
354         unlock_page(page);
355         put_page(page);
356 }
357
358 static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
359                                                 pgoff_t index, u64 last_cookie)
360 {
361         struct page *page;
362         u64 change_attr;
363
364         page = grab_cache_page(mapping, index);
365         if (!page)
366                 return NULL;
367         change_attr = inode_peek_iversion_raw(mapping->host);
368         if (PageUptodate(page)) {
369                 if (nfs_readdir_page_validate(page, last_cookie, change_attr))
370                         return page;
371                 nfs_readdir_clear_array(page);
372         }
373         nfs_readdir_page_init_array(page, last_cookie, change_attr);
374         SetPageUptodate(page);
375         return page;
376 }
377
378 static loff_t nfs_readdir_page_offset(struct page *page)
379 {
380         return (loff_t)page->index * (loff_t)nfs_readdir_array_maxentries();
381 }
382
383 static u64 nfs_readdir_page_last_cookie(struct page *page)
384 {
385         struct nfs_cache_array *array;
386         u64 ret;
387
388         array = kmap_atomic(page);
389         ret = array->last_cookie;
390         kunmap_atomic(array);
391         return ret;
392 }
393
394 static bool nfs_readdir_page_needs_filling(struct page *page)
395 {
396         struct nfs_cache_array *array;
397         bool ret;
398
399         array = kmap_atomic(page);
400         ret = !nfs_readdir_array_is_full(array);
401         kunmap_atomic(array);
402         return ret;
403 }
404
405 static void nfs_readdir_page_set_eof(struct page *page)
406 {
407         struct nfs_cache_array *array;
408
409         array = kmap_atomic(page);
410         nfs_readdir_array_set_eof(array);
411         kunmap_atomic(array);
412 }
413
414 static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
415                                               pgoff_t index, u64 cookie)
416 {
417         struct page *page;
418
419         page = nfs_readdir_page_get_locked(mapping, index, cookie);
420         if (page) {
421                 if (nfs_readdir_page_last_cookie(page) == cookie)
422                         return page;
423                 nfs_readdir_page_unlock_and_put(page);
424         }
425         return NULL;
426 }
427
428 static inline
429 int is_32bit_api(void)
430 {
431 #ifdef CONFIG_COMPAT
432         return in_compat_syscall();
433 #else
434         return (BITS_PER_LONG == 32);
435 #endif
436 }
437
438 static
439 bool nfs_readdir_use_cookie(const struct file *filp)
440 {
441         if ((filp->f_mode & FMODE_32BITHASH) ||
442             (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
443                 return false;
444         return true;
445 }
446
447 static void nfs_readdir_seek_next_array(struct nfs_cache_array *array,
448                                         struct nfs_readdir_descriptor *desc)
449 {
450         if (array->page_full) {
451                 desc->last_cookie = array->last_cookie;
452                 desc->current_index += array->size;
453                 desc->cache_entry_index = 0;
454                 desc->page_index++;
455         } else
456                 desc->last_cookie = array->array[0].cookie;
457 }
458
459 static int nfs_readdir_search_for_pos(struct nfs_cache_array *array,
460                                       struct nfs_readdir_descriptor *desc)
461 {
462         loff_t diff = desc->ctx->pos - desc->current_index;
463         unsigned int index;
464
465         if (diff < 0)
466                 goto out_eof;
467         if (diff >= array->size) {
468                 if (array->page_is_eof)
469                         goto out_eof;
470                 nfs_readdir_seek_next_array(array, desc);
471                 return -EAGAIN;
472         }
473
474         index = (unsigned int)diff;
475         desc->dir_cookie = array->array[index].cookie;
476         desc->cache_entry_index = index;
477         return 0;
478 out_eof:
479         desc->eof = true;
480         return -EBADCOOKIE;
481 }
482
483 static bool nfs_readdir_array_cookie_in_range(struct nfs_cache_array *array,
484                                               u64 cookie)
485 {
486         if (!array->cookies_are_ordered)
487                 return true;
488         /* Optimisation for monotonically increasing cookies */
489         if (cookie >= array->last_cookie)
490                 return false;
491         if (array->size && cookie < array->array[0].cookie)
492                 return false;
493         return true;
494 }
495
496 static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array,
497                                          struct nfs_readdir_descriptor *desc)
498 {
499         int i;
500         loff_t new_pos;
501         int status = -EAGAIN;
502
503         if (!nfs_readdir_array_cookie_in_range(array, desc->dir_cookie))
504                 goto check_eof;
505
506         for (i = 0; i < array->size; i++) {
507                 if (array->array[i].cookie == desc->dir_cookie) {
508                         struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
509
510                         new_pos = nfs_readdir_page_offset(desc->page) + i;
511                         if (desc->attr_gencount != nfsi->attr_gencount) {
512                                 desc->duped = 0;
513                                 desc->attr_gencount = nfsi->attr_gencount;
514                         } else if (new_pos < desc->prev_index) {
515                                 if (desc->duped > 0
516                                     && desc->dup_cookie == desc->dir_cookie) {
517                                         if (printk_ratelimit()) {
518                                                 pr_notice("NFS: directory %pD2 contains a readdir loop."
519                                                                 "Please contact your server vendor.  "
520                                                                 "The file: %s has duplicate cookie %llu\n",
521                                                                 desc->file, array->array[i].name, desc->dir_cookie);
522                                         }
523                                         status = -ELOOP;
524                                         goto out;
525                                 }
526                                 desc->dup_cookie = desc->dir_cookie;
527                                 desc->duped = -1;
528                         }
529                         if (nfs_readdir_use_cookie(desc->file))
530                                 desc->ctx->pos = desc->dir_cookie;
531                         else
532                                 desc->ctx->pos = new_pos;
533                         desc->prev_index = new_pos;
534                         desc->cache_entry_index = i;
535                         return 0;
536                 }
537         }
538 check_eof:
539         if (array->page_is_eof) {
540                 status = -EBADCOOKIE;
541                 if (desc->dir_cookie == array->last_cookie)
542                         desc->eof = true;
543         } else
544                 nfs_readdir_seek_next_array(array, desc);
545 out:
546         return status;
547 }
548
549 static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc)
550 {
551         struct nfs_cache_array *array;
552         int status;
553
554         array = kmap_atomic(desc->page);
555
556         if (desc->dir_cookie == 0)
557                 status = nfs_readdir_search_for_pos(array, desc);
558         else
559                 status = nfs_readdir_search_for_cookie(array, desc);
560
561         kunmap_atomic(array);
562         return status;
563 }
564
565 /* Fill a page with xdr information before transferring to the cache page */
566 static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc,
567                                   __be32 *verf, u64 cookie,
568                                   struct page **pages, size_t bufsize,
569                                   __be32 *verf_res)
570 {
571         struct inode *inode = file_inode(desc->file);
572         struct nfs_readdir_arg arg = {
573                 .dentry = file_dentry(desc->file),
574                 .cred = desc->file->f_cred,
575                 .verf = verf,
576                 .cookie = cookie,
577                 .pages = pages,
578                 .page_len = bufsize,
579                 .plus = desc->plus,
580         };
581         struct nfs_readdir_res res = {
582                 .verf = verf_res,
583         };
584         unsigned long   timestamp, gencount;
585         int             error;
586
587  again:
588         timestamp = jiffies;
589         gencount = nfs_inc_attr_generation_counter();
590         desc->dir_verifier = nfs_save_change_attribute(inode);
591         error = NFS_PROTO(inode)->readdir(&arg, &res);
592         if (error < 0) {
593                 /* We requested READDIRPLUS, but the server doesn't grok it */
594                 if (error == -ENOTSUPP && desc->plus) {
595                         NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
596                         desc->plus = arg.plus = false;
597                         goto again;
598                 }
599                 goto error;
600         }
601         desc->timestamp = timestamp;
602         desc->gencount = gencount;
603 error:
604         return error;
605 }
606
607 static int xdr_decode(struct nfs_readdir_descriptor *desc,
608                       struct nfs_entry *entry, struct xdr_stream *xdr)
609 {
610         struct inode *inode = file_inode(desc->file);
611         int error;
612
613         error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
614         if (error)
615                 return error;
616         entry->fattr->time_start = desc->timestamp;
617         entry->fattr->gencount = desc->gencount;
618         return 0;
619 }
620
621 /* Match file and dirent using either filehandle or fileid
622  * Note: caller is responsible for checking the fsid
623  */
624 static
625 int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
626 {
627         struct inode *inode;
628         struct nfs_inode *nfsi;
629
630         if (d_really_is_negative(dentry))
631                 return 0;
632
633         inode = d_inode(dentry);
634         if (is_bad_inode(inode) || NFS_STALE(inode))
635                 return 0;
636
637         nfsi = NFS_I(inode);
638         if (entry->fattr->fileid != nfsi->fileid)
639                 return 0;
640         if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
641                 return 0;
642         return 1;
643 }
644
645 #define NFS_READDIR_CACHE_USAGE_THRESHOLD (8UL)
646
647 static bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx,
648                                 unsigned int cache_hits,
649                                 unsigned int cache_misses)
650 {
651         if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
652                 return false;
653         if (ctx->pos == 0 ||
654             cache_hits + cache_misses > NFS_READDIR_CACHE_USAGE_THRESHOLD)
655                 return true;
656         return false;
657 }
658
659 /*
660  * This function is called by the getattr code to request the
661  * use of readdirplus to accelerate any future lookups in the same
662  * directory.
663  */
664 void nfs_readdir_record_entry_cache_hit(struct inode *dir)
665 {
666         struct nfs_inode *nfsi = NFS_I(dir);
667         struct nfs_open_dir_context *ctx;
668
669         if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
670             S_ISDIR(dir->i_mode)) {
671                 rcu_read_lock();
672                 list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
673                         atomic_inc(&ctx->cache_hits);
674                 rcu_read_unlock();
675         }
676 }
677
678 /*
679  * This function is mainly for use by nfs_getattr().
680  *
681  * If this is an 'ls -l', we want to force use of readdirplus.
682  */
683 void nfs_readdir_record_entry_cache_miss(struct inode *dir)
684 {
685         struct nfs_inode *nfsi = NFS_I(dir);
686         struct nfs_open_dir_context *ctx;
687
688         if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
689             S_ISDIR(dir->i_mode)) {
690                 rcu_read_lock();
691                 list_for_each_entry_rcu (ctx, &nfsi->open_files, list)
692                         atomic_inc(&ctx->cache_misses);
693                 rcu_read_unlock();
694         }
695 }
696
697 static void nfs_lookup_advise_force_readdirplus(struct inode *dir,
698                                                 unsigned int flags)
699 {
700         if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
701                 return;
702         if (flags & (LOOKUP_EXCL | LOOKUP_PARENT | LOOKUP_REVAL))
703                 return;
704         nfs_readdir_record_entry_cache_miss(dir);
705 }
706
707 static
708 void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
709                 unsigned long dir_verifier)
710 {
711         struct qstr filename = QSTR_INIT(entry->name, entry->len);
712         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
713         struct dentry *dentry;
714         struct dentry *alias;
715         struct inode *inode;
716         int status;
717
718         if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
719                 return;
720         if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
721                 return;
722         if (filename.len == 0)
723                 return;
724         /* Validate that the name doesn't contain any illegal '\0' */
725         if (strnlen(filename.name, filename.len) != filename.len)
726                 return;
727         /* ...or '/' */
728         if (strnchr(filename.name, filename.len, '/'))
729                 return;
730         if (filename.name[0] == '.') {
731                 if (filename.len == 1)
732                         return;
733                 if (filename.len == 2 && filename.name[1] == '.')
734                         return;
735         }
736         filename.hash = full_name_hash(parent, filename.name, filename.len);
737
738         dentry = d_lookup(parent, &filename);
739 again:
740         if (!dentry) {
741                 dentry = d_alloc_parallel(parent, &filename, &wq);
742                 if (IS_ERR(dentry))
743                         return;
744         }
745         if (!d_in_lookup(dentry)) {
746                 /* Is there a mountpoint here? If so, just exit */
747                 if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
748                                         &entry->fattr->fsid))
749                         goto out;
750                 if (nfs_same_file(dentry, entry)) {
751                         if (!entry->fh->size)
752                                 goto out;
753                         nfs_set_verifier(dentry, dir_verifier);
754                         status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
755                         if (!status)
756                                 nfs_setsecurity(d_inode(dentry), entry->fattr);
757                         trace_nfs_readdir_lookup_revalidate(d_inode(parent),
758                                                             dentry, 0, status);
759                         goto out;
760                 } else {
761                         trace_nfs_readdir_lookup_revalidate_failed(
762                                 d_inode(parent), dentry, 0);
763                         d_invalidate(dentry);
764                         dput(dentry);
765                         dentry = NULL;
766                         goto again;
767                 }
768         }
769         if (!entry->fh->size) {
770                 d_lookup_done(dentry);
771                 goto out;
772         }
773
774         inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
775         alias = d_splice_alias(inode, dentry);
776         d_lookup_done(dentry);
777         if (alias) {
778                 if (IS_ERR(alias))
779                         goto out;
780                 dput(dentry);
781                 dentry = alias;
782         }
783         nfs_set_verifier(dentry, dir_verifier);
784         trace_nfs_readdir_lookup(d_inode(parent), dentry, 0);
785 out:
786         dput(dentry);
787 }
788
789 /* Perform conversion from xdr to cache array */
790 static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
791                                    struct nfs_entry *entry,
792                                    struct page **xdr_pages,
793                                    unsigned int buflen,
794                                    struct page **arrays,
795                                    size_t narrays)
796 {
797         struct address_space *mapping = desc->file->f_mapping;
798         struct xdr_stream stream;
799         struct xdr_buf buf;
800         struct page *scratch, *new, *page = *arrays;
801         int status;
802
803         scratch = alloc_page(GFP_KERNEL);
804         if (scratch == NULL)
805                 return -ENOMEM;
806
807         xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
808         xdr_set_scratch_page(&stream, scratch);
809
810         do {
811                 if (entry->fattr->label)
812                         entry->fattr->label->len = NFS4_MAXLABELLEN;
813
814                 status = xdr_decode(desc, entry, &stream);
815                 if (status != 0)
816                         break;
817
818                 if (desc->plus)
819                         nfs_prime_dcache(file_dentry(desc->file), entry,
820                                         desc->dir_verifier);
821
822                 status = nfs_readdir_add_to_array(entry, page);
823                 if (status != -ENOSPC)
824                         continue;
825
826                 if (page->mapping != mapping) {
827                         if (!--narrays)
828                                 break;
829                         new = nfs_readdir_page_array_alloc(entry->prev_cookie,
830                                                            GFP_KERNEL);
831                         if (!new)
832                                 break;
833                         arrays++;
834                         *arrays = page = new;
835                         desc->page_index_max++;
836                 } else {
837                         new = nfs_readdir_page_get_next(mapping,
838                                                         page->index + 1,
839                                                         entry->prev_cookie);
840                         if (!new)
841                                 break;
842                         if (page != *arrays)
843                                 nfs_readdir_page_unlock_and_put(page);
844                         page = new;
845                         desc->page_index_max = new->index;
846                 }
847                 status = nfs_readdir_add_to_array(entry, page);
848         } while (!status && !entry->eof);
849
850         switch (status) {
851         case -EBADCOOKIE:
852                 if (entry->eof) {
853                         nfs_readdir_page_set_eof(page);
854                         status = 0;
855                 }
856                 break;
857         case -ENOSPC:
858         case -EAGAIN:
859                 status = 0;
860                 break;
861         }
862
863         if (page != *arrays)
864                 nfs_readdir_page_unlock_and_put(page);
865
866         put_page(scratch);
867         return status;
868 }
869
870 static void nfs_readdir_free_pages(struct page **pages, size_t npages)
871 {
872         while (npages--)
873                 put_page(pages[npages]);
874         kfree(pages);
875 }
876
877 /*
878  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
879  * to nfs_readdir_free_pages()
880  */
881 static struct page **nfs_readdir_alloc_pages(size_t npages)
882 {
883         struct page **pages;
884         size_t i;
885
886         pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
887         if (!pages)
888                 return NULL;
889         for (i = 0; i < npages; i++) {
890                 struct page *page = alloc_page(GFP_KERNEL);
891                 if (page == NULL)
892                         goto out_freepages;
893                 pages[i] = page;
894         }
895         return pages;
896
897 out_freepages:
898         nfs_readdir_free_pages(pages, i);
899         return NULL;
900 }
901
902 static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc,
903                                     __be32 *verf_arg, __be32 *verf_res,
904                                     struct page **arrays, size_t narrays)
905 {
906         struct page **pages;
907         struct page *page = *arrays;
908         struct nfs_entry *entry;
909         size_t array_size;
910         struct inode *inode = file_inode(desc->file);
911         unsigned int dtsize = desc->dtsize;
912         unsigned int pglen;
913         int status = -ENOMEM;
914
915         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
916         if (!entry)
917                 return -ENOMEM;
918         entry->cookie = nfs_readdir_page_last_cookie(page);
919         entry->fh = nfs_alloc_fhandle();
920         entry->fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
921         entry->server = NFS_SERVER(inode);
922         if (entry->fh == NULL || entry->fattr == NULL)
923                 goto out;
924
925         array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
926         pages = nfs_readdir_alloc_pages(array_size);
927         if (!pages)
928                 goto out;
929
930         status = nfs_readdir_xdr_filler(desc, verf_arg, entry->cookie, pages,
931                                         dtsize, verf_res);
932         if (status < 0)
933                 goto free_pages;
934
935         pglen = status;
936         if (pglen != 0)
937                 status = nfs_readdir_page_filler(desc, entry, pages, pglen,
938                                                  arrays, narrays);
939         else
940                 nfs_readdir_page_set_eof(page);
941         desc->buffer_fills++;
942
943 free_pages:
944         nfs_readdir_free_pages(pages, array_size);
945 out:
946         nfs_free_fattr(entry->fattr);
947         nfs_free_fhandle(entry->fh);
948         kfree(entry);
949         return status;
950 }
951
952 static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
953 {
954         put_page(desc->page);
955         desc->page = NULL;
956 }
957
958 static void
959 nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
960 {
961         unlock_page(desc->page);
962         nfs_readdir_page_put(desc);
963 }
964
965 static struct page *
966 nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
967 {
968         return nfs_readdir_page_get_locked(desc->file->f_mapping,
969                                            desc->page_index,
970                                            desc->last_cookie);
971 }
972
973 /*
974  * Returns 0 if desc->dir_cookie was found on page desc->page_index
975  * and locks the page to prevent removal from the page cache.
976  */
977 static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc)
978 {
979         struct inode *inode = file_inode(desc->file);
980         struct nfs_inode *nfsi = NFS_I(inode);
981         __be32 verf[NFS_DIR_VERIFIER_SIZE];
982         int res;
983
984         desc->page = nfs_readdir_page_get_cached(desc);
985         if (!desc->page)
986                 return -ENOMEM;
987         if (nfs_readdir_page_needs_filling(desc->page)) {
988                 /* Grow the dtsize if we had to go back for more pages */
989                 if (desc->page_index == desc->page_index_max)
990                         nfs_grow_dtsize(desc);
991                 desc->page_index_max = desc->page_index;
992                 trace_nfs_readdir_cache_fill(desc->file, nfsi->cookieverf,
993                                              desc->last_cookie,
994                                              desc->page->index, desc->dtsize);
995                 res = nfs_readdir_xdr_to_array(desc, nfsi->cookieverf, verf,
996                                                &desc->page, 1);
997                 if (res < 0) {
998                         nfs_readdir_page_unlock_and_put_cached(desc);
999                         trace_nfs_readdir_cache_fill_done(inode, res);
1000                         if (res == -EBADCOOKIE || res == -ENOTSYNC) {
1001                                 invalidate_inode_pages2(desc->file->f_mapping);
1002                                 desc->page_index = 0;
1003                                 trace_nfs_readdir_invalidate_cache_range(
1004                                         inode, 0, MAX_LFS_FILESIZE);
1005                                 return -EAGAIN;
1006                         }
1007                         return res;
1008                 }
1009                 /*
1010                  * Set the cookie verifier if the page cache was empty
1011                  */
1012                 if (desc->last_cookie == 0 &&
1013                     memcmp(nfsi->cookieverf, verf, sizeof(nfsi->cookieverf))) {
1014                         memcpy(nfsi->cookieverf, verf,
1015                                sizeof(nfsi->cookieverf));
1016                         invalidate_inode_pages2_range(desc->file->f_mapping,
1017                                                       desc->page_index_max + 1,
1018                                                       -1);
1019                         trace_nfs_readdir_invalidate_cache_range(
1020                                 inode, desc->page_index_max + 1,
1021                                 MAX_LFS_FILESIZE);
1022                 }
1023         }
1024         res = nfs_readdir_search_array(desc);
1025         if (res == 0)
1026                 return 0;
1027         nfs_readdir_page_unlock_and_put_cached(desc);
1028         return res;
1029 }
1030
1031 /* Search for desc->dir_cookie from the beginning of the page cache */
1032 static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc)
1033 {
1034         int res;
1035
1036         do {
1037                 if (desc->page_index == 0) {
1038                         desc->current_index = 0;
1039                         desc->prev_index = 0;
1040                         desc->last_cookie = 0;
1041                 }
1042                 res = find_and_lock_cache_page(desc);
1043         } while (res == -EAGAIN);
1044         return res;
1045 }
1046
1047 /*
1048  * Once we've found the start of the dirent within a page: fill 'er up...
1049  */
1050 static void nfs_do_filldir(struct nfs_readdir_descriptor *desc,
1051                            const __be32 *verf)
1052 {
1053         struct file     *file = desc->file;
1054         struct nfs_cache_array *array;
1055         unsigned int i;
1056
1057         array = kmap(desc->page);
1058         for (i = desc->cache_entry_index; i < array->size; i++) {
1059                 struct nfs_cache_array_entry *ent;
1060
1061                 ent = &array->array[i];
1062                 if (!dir_emit(desc->ctx, ent->name, ent->name_len,
1063                     nfs_compat_user_ino64(ent->ino), ent->d_type)) {
1064                         desc->eob = true;
1065                         break;
1066                 }
1067                 memcpy(desc->verf, verf, sizeof(desc->verf));
1068                 if (i == array->size - 1) {
1069                         desc->dir_cookie = array->last_cookie;
1070                         nfs_readdir_seek_next_array(array, desc);
1071                 } else {
1072                         desc->dir_cookie = array->array[i + 1].cookie;
1073                         desc->last_cookie = array->array[0].cookie;
1074                 }
1075                 if (nfs_readdir_use_cookie(file))
1076                         desc->ctx->pos = desc->dir_cookie;
1077                 else
1078                         desc->ctx->pos++;
1079                 if (desc->duped != 0)
1080                         desc->duped = 1;
1081         }
1082         if (array->page_is_eof)
1083                 desc->eof = !desc->eob;
1084
1085         kunmap(desc->page);
1086         dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n",
1087                         (unsigned long long)desc->dir_cookie);
1088 }
1089
1090 /*
1091  * If we cannot find a cookie in our cache, we suspect that this is
1092  * because it points to a deleted file, so we ask the server to return
1093  * whatever it thinks is the next entry. We then feed this to filldir.
1094  * If all goes well, we should then be able to find our way round the
1095  * cache on the next call to readdir_search_pagecache();
1096  *
1097  * NOTE: we cannot add the anonymous page to the pagecache because
1098  *       the data it contains might not be page aligned. Besides,
1099  *       we should already have a complete representation of the
1100  *       directory in the page cache by the time we get here.
1101  */
1102 static int uncached_readdir(struct nfs_readdir_descriptor *desc)
1103 {
1104         struct page     **arrays;
1105         size_t          i, sz = 512;
1106         __be32          verf[NFS_DIR_VERIFIER_SIZE];
1107         int             status = -ENOMEM;
1108
1109         dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %llu\n",
1110                         (unsigned long long)desc->dir_cookie);
1111
1112         arrays = kcalloc(sz, sizeof(*arrays), GFP_KERNEL);
1113         if (!arrays)
1114                 goto out;
1115         arrays[0] = nfs_readdir_page_array_alloc(desc->dir_cookie, GFP_KERNEL);
1116         if (!arrays[0])
1117                 goto out;
1118
1119         desc->page_index = 0;
1120         desc->cache_entry_index = 0;
1121         desc->last_cookie = desc->dir_cookie;
1122         desc->duped = 0;
1123         desc->page_index_max = 0;
1124
1125         trace_nfs_readdir_uncached(desc->file, desc->verf, desc->last_cookie,
1126                                    -1, desc->dtsize);
1127
1128         status = nfs_readdir_xdr_to_array(desc, desc->verf, verf, arrays, sz);
1129         if (status < 0) {
1130                 trace_nfs_readdir_uncached_done(file_inode(desc->file), status);
1131                 goto out_free;
1132         }
1133
1134         for (i = 0; !desc->eob && i < sz && arrays[i]; i++) {
1135                 desc->page = arrays[i];
1136                 nfs_do_filldir(desc, verf);
1137         }
1138         desc->page = NULL;
1139
1140         /*
1141          * Grow the dtsize if we have to go back for more pages,
1142          * or shrink it if we're reading too many.
1143          */
1144         if (!desc->eof) {
1145                 if (!desc->eob)
1146                         nfs_grow_dtsize(desc);
1147                 else if (desc->buffer_fills == 1 &&
1148                          i < (desc->page_index_max >> 1))
1149                         nfs_shrink_dtsize(desc);
1150         }
1151 out_free:
1152         for (i = 0; i < sz && arrays[i]; i++)
1153                 nfs_readdir_page_array_free(arrays[i]);
1154 out:
1155         desc->page_index_max = -1;
1156         kfree(arrays);
1157         dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
1158         return status;
1159 }
1160
1161 #define NFS_READDIR_CACHE_MISS_THRESHOLD (16UL)
1162
1163 static void nfs_readdir_handle_cache_misses(struct inode *inode,
1164                                             struct nfs_readdir_descriptor *desc,
1165                                             pgoff_t page_index,
1166                                             unsigned int cache_misses)
1167 {
1168         if (desc->ctx->pos == 0 ||
1169             cache_misses <= NFS_READDIR_CACHE_MISS_THRESHOLD)
1170                 return;
1171         if (invalidate_mapping_pages(inode->i_mapping, page_index + 1, -1) == 0)
1172                 return;
1173         trace_nfs_readdir_invalidate_cache_range(
1174                 inode, (loff_t)(page_index + 1) << PAGE_SHIFT,
1175                 MAX_LFS_FILESIZE);
1176 }
1177
1178 /* The file offset position represents the dirent entry number.  A
1179    last cookie cache takes care of the common case of reading the
1180    whole directory.
1181  */
1182 static int nfs_readdir(struct file *file, struct dir_context *ctx)
1183 {
1184         struct dentry   *dentry = file_dentry(file);
1185         struct inode    *inode = d_inode(dentry);
1186         struct nfs_inode *nfsi = NFS_I(inode);
1187         struct nfs_open_dir_context *dir_ctx = file->private_data;
1188         struct nfs_readdir_descriptor *desc;
1189         unsigned int cache_hits, cache_misses;
1190         pgoff_t page_index;
1191         int res;
1192
1193         dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
1194                         file, (long long)ctx->pos);
1195         nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
1196
1197         /*
1198          * ctx->pos points to the dirent entry number.
1199          * *desc->dir_cookie has the cookie for the next entry. We have
1200          * to either find the entry with the appropriate number or
1201          * revalidate the cookie.
1202          */
1203         nfs_revalidate_mapping(inode, file->f_mapping);
1204
1205         res = -ENOMEM;
1206         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
1207         if (!desc)
1208                 goto out;
1209         desc->file = file;
1210         desc->ctx = ctx;
1211         desc->page_index_max = -1;
1212
1213         spin_lock(&file->f_lock);
1214         desc->dir_cookie = dir_ctx->dir_cookie;
1215         desc->dup_cookie = dir_ctx->dup_cookie;
1216         desc->duped = dir_ctx->duped;
1217         page_index = dir_ctx->page_index;
1218         desc->page_index = page_index;
1219         desc->last_cookie = dir_ctx->last_cookie;
1220         desc->attr_gencount = dir_ctx->attr_gencount;
1221         desc->eof = dir_ctx->eof;
1222         nfs_set_dtsize(desc, dir_ctx->dtsize);
1223         memcpy(desc->verf, dir_ctx->verf, sizeof(desc->verf));
1224         cache_hits = atomic_xchg(&dir_ctx->cache_hits, 0);
1225         cache_misses = atomic_xchg(&dir_ctx->cache_misses, 0);
1226         spin_unlock(&file->f_lock);
1227
1228         if (desc->eof) {
1229                 res = 0;
1230                 goto out_free;
1231         }
1232
1233         desc->plus = nfs_use_readdirplus(inode, ctx, cache_hits, cache_misses);
1234         nfs_readdir_handle_cache_misses(inode, desc, page_index, cache_misses);
1235
1236         do {
1237                 res = readdir_search_pagecache(desc);
1238
1239                 if (res == -EBADCOOKIE) {
1240                         res = 0;
1241                         /* This means either end of directory */
1242                         if (desc->dir_cookie && !desc->eof) {
1243                                 /* Or that the server has 'lost' a cookie */
1244                                 res = uncached_readdir(desc);
1245                                 if (res == 0)
1246                                         continue;
1247                                 if (res == -EBADCOOKIE || res == -ENOTSYNC)
1248                                         res = 0;
1249                         }
1250                         break;
1251                 }
1252                 if (res == -ETOOSMALL && desc->plus) {
1253                         nfs_zap_caches(inode);
1254                         desc->page_index = 0;
1255                         desc->plus = false;
1256                         desc->eof = false;
1257                         continue;
1258                 }
1259                 if (res < 0)
1260                         break;
1261
1262                 nfs_do_filldir(desc, nfsi->cookieverf);
1263                 nfs_readdir_page_unlock_and_put_cached(desc);
1264         } while (!desc->eob && !desc->eof);
1265
1266         spin_lock(&file->f_lock);
1267         dir_ctx->dir_cookie = desc->dir_cookie;
1268         dir_ctx->dup_cookie = desc->dup_cookie;
1269         dir_ctx->last_cookie = desc->last_cookie;
1270         dir_ctx->duped = desc->duped;
1271         dir_ctx->attr_gencount = desc->attr_gencount;
1272         dir_ctx->page_index = desc->page_index;
1273         dir_ctx->eof = desc->eof;
1274         dir_ctx->dtsize = desc->dtsize;
1275         memcpy(dir_ctx->verf, desc->verf, sizeof(dir_ctx->verf));
1276         spin_unlock(&file->f_lock);
1277 out_free:
1278         kfree(desc);
1279
1280 out:
1281         dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
1282         return res;
1283 }
1284
1285 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1286 {
1287         struct nfs_open_dir_context *dir_ctx = filp->private_data;
1288
1289         dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
1290                         filp, offset, whence);
1291
1292         switch (whence) {
1293         default:
1294                 return -EINVAL;
1295         case SEEK_SET:
1296                 if (offset < 0)
1297                         return -EINVAL;
1298                 spin_lock(&filp->f_lock);
1299                 break;
1300         case SEEK_CUR:
1301                 if (offset == 0)
1302                         return filp->f_pos;
1303                 spin_lock(&filp->f_lock);
1304                 offset += filp->f_pos;
1305                 if (offset < 0) {
1306                         spin_unlock(&filp->f_lock);
1307                         return -EINVAL;
1308                 }
1309         }
1310         if (offset != filp->f_pos) {
1311                 filp->f_pos = offset;
1312                 dir_ctx->page_index = 0;
1313                 if (!nfs_readdir_use_cookie(filp))
1314                         dir_ctx->dir_cookie = 0;
1315                 else
1316                         dir_ctx->dir_cookie = offset;
1317                 if (offset == 0)
1318                         memset(dir_ctx->verf, 0, sizeof(dir_ctx->verf));
1319                 dir_ctx->duped = 0;
1320                 dir_ctx->eof = false;
1321         }
1322         spin_unlock(&filp->f_lock);
1323         return offset;
1324 }
1325
1326 /*
1327  * All directory operations under NFS are synchronous, so fsync()
1328  * is a dummy operation.
1329  */
1330 static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
1331                          int datasync)
1332 {
1333         dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
1334
1335         nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
1336         return 0;
1337 }
1338
1339 /**
1340  * nfs_force_lookup_revalidate - Mark the directory as having changed
1341  * @dir: pointer to directory inode
1342  *
1343  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1344  * full lookup on all child dentries of 'dir' whenever a change occurs
1345  * on the server that might have invalidated our dcache.
1346  *
1347  * Note that we reserve bit '0' as a tag to let us know when a dentry
1348  * was revalidated while holding a delegation on its inode.
1349  *
1350  * The caller should be holding dir->i_lock
1351  */
1352 void nfs_force_lookup_revalidate(struct inode *dir)
1353 {
1354         NFS_I(dir)->cache_change_attribute += 2;
1355 }
1356 EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1357
1358 /**
1359  * nfs_verify_change_attribute - Detects NFS remote directory changes
1360  * @dir: pointer to parent directory inode
1361  * @verf: previously saved change attribute
1362  *
1363  * Return "false" if the verifiers doesn't match the change attribute.
1364  * This would usually indicate that the directory contents have changed on
1365  * the server, and that any dentries need revalidating.
1366  */
1367 static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1368 {
1369         return (verf & ~1UL) == nfs_save_change_attribute(dir);
1370 }
1371
1372 static void nfs_set_verifier_delegated(unsigned long *verf)
1373 {
1374         *verf |= 1UL;
1375 }
1376
1377 #if IS_ENABLED(CONFIG_NFS_V4)
1378 static void nfs_unset_verifier_delegated(unsigned long *verf)
1379 {
1380         *verf &= ~1UL;
1381 }
1382 #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1383
1384 static bool nfs_test_verifier_delegated(unsigned long verf)
1385 {
1386         return verf & 1;
1387 }
1388
1389 static bool nfs_verifier_is_delegated(struct dentry *dentry)
1390 {
1391         return nfs_test_verifier_delegated(dentry->d_time);
1392 }
1393
1394 static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1395 {
1396         struct inode *inode = d_inode(dentry);
1397         struct inode *dir = d_inode(dentry->d_parent);
1398
1399         if (!nfs_verify_change_attribute(dir, verf))
1400                 return;
1401         if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1402                 nfs_set_verifier_delegated(&verf);
1403         dentry->d_time = verf;
1404 }
1405
1406 /**
1407  * nfs_set_verifier - save a parent directory verifier in the dentry
1408  * @dentry: pointer to dentry
1409  * @verf: verifier to save
1410  *
1411  * Saves the parent directory verifier in @dentry. If the inode has
1412  * a delegation, we also tag the dentry as having been revalidated
1413  * while holding a delegation so that we know we don't have to
1414  * look it up again after a directory change.
1415  */
1416 void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1417 {
1418
1419         spin_lock(&dentry->d_lock);
1420         nfs_set_verifier_locked(dentry, verf);
1421         spin_unlock(&dentry->d_lock);
1422 }
1423 EXPORT_SYMBOL_GPL(nfs_set_verifier);
1424
1425 #if IS_ENABLED(CONFIG_NFS_V4)
1426 /**
1427  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1428  * @inode: pointer to inode
1429  *
1430  * Iterates through the dentries in the inode alias list and clears
1431  * the tag used to indicate that the dentry has been revalidated
1432  * while holding a delegation.
1433  * This function is intended for use when the delegation is being
1434  * returned or revoked.
1435  */
1436 void nfs_clear_verifier_delegated(struct inode *inode)
1437 {
1438         struct dentry *alias;
1439
1440         if (!inode)
1441                 return;
1442         spin_lock(&inode->i_lock);
1443         hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1444                 spin_lock(&alias->d_lock);
1445                 nfs_unset_verifier_delegated(&alias->d_time);
1446                 spin_unlock(&alias->d_lock);
1447         }
1448         spin_unlock(&inode->i_lock);
1449 }
1450 EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1451 #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1452
1453 static int nfs_dentry_verify_change(struct inode *dir, struct dentry *dentry)
1454 {
1455         if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE) &&
1456             d_really_is_negative(dentry))
1457                 return dentry->d_time == inode_peek_iversion_raw(dir);
1458         return nfs_verify_change_attribute(dir, dentry->d_time);
1459 }
1460
1461 /*
1462  * A check for whether or not the parent directory has changed.
1463  * In the case it has, we assume that the dentries are untrustworthy
1464  * and may need to be looked up again.
1465  * If rcu_walk prevents us from performing a full check, return 0.
1466  */
1467 static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1468                               int rcu_walk)
1469 {
1470         if (IS_ROOT(dentry))
1471                 return 1;
1472         if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
1473                 return 0;
1474         if (!nfs_dentry_verify_change(dir, dentry))
1475                 return 0;
1476         /* Revalidate nfsi->cache_change_attribute before we declare a match */
1477         if (nfs_mapping_need_revalidate_inode(dir)) {
1478                 if (rcu_walk)
1479                         return 0;
1480                 if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
1481                         return 0;
1482         }
1483         if (!nfs_dentry_verify_change(dir, dentry))
1484                 return 0;
1485         return 1;
1486 }
1487
1488 /*
1489  * Use intent information to check whether or not we're going to do
1490  * an O_EXCL create using this path component.
1491  */
1492 static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1493 {
1494         if (NFS_PROTO(dir)->version == 2)
1495                 return 0;
1496         return flags & LOOKUP_EXCL;
1497 }
1498
1499 /*
1500  * Inode and filehandle revalidation for lookups.
1501  *
1502  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1503  * or if the intent information indicates that we're about to open this
1504  * particular file and the "nocto" mount flag is not set.
1505  *
1506  */
1507 static
1508 int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
1509 {
1510         struct nfs_server *server = NFS_SERVER(inode);
1511         int ret;
1512
1513         if (IS_AUTOMOUNT(inode))
1514                 return 0;
1515
1516         if (flags & LOOKUP_OPEN) {
1517                 switch (inode->i_mode & S_IFMT) {
1518                 case S_IFREG:
1519                         /* A NFSv4 OPEN will revalidate later */
1520                         if (server->caps & NFS_CAP_ATOMIC_OPEN)
1521                                 goto out;
1522                         fallthrough;
1523                 case S_IFDIR:
1524                         if (server->flags & NFS_MOUNT_NOCTO)
1525                                 break;
1526                         /* NFS close-to-open cache consistency validation */
1527                         goto out_force;
1528                 }
1529         }
1530
1531         /* VFS wants an on-the-wire revalidation */
1532         if (flags & LOOKUP_REVAL)
1533                 goto out_force;
1534 out:
1535         if (inode->i_nlink > 0 ||
1536             (inode->i_nlink == 0 &&
1537              test_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(inode)->flags)))
1538                 return 0;
1539         else
1540                 return -ESTALE;
1541 out_force:
1542         if (flags & LOOKUP_RCU)
1543                 return -ECHILD;
1544         ret = __nfs_revalidate_inode(server, inode);
1545         if (ret != 0)
1546                 return ret;
1547         goto out;
1548 }
1549
1550 static void nfs_mark_dir_for_revalidate(struct inode *inode)
1551 {
1552         spin_lock(&inode->i_lock);
1553         nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE);
1554         spin_unlock(&inode->i_lock);
1555 }
1556
1557 /*
1558  * We judge how long we want to trust negative
1559  * dentries by looking at the parent inode mtime.
1560  *
1561  * If parent mtime has changed, we revalidate, else we wait for a
1562  * period corresponding to the parent's attribute cache timeout value.
1563  *
1564  * If LOOKUP_RCU prevents us from performing a full check, return 1
1565  * suggesting a reval is needed.
1566  *
1567  * Note that when creating a new file, or looking up a rename target,
1568  * then it shouldn't be necessary to revalidate a negative dentry.
1569  */
1570 static inline
1571 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1572                        unsigned int flags)
1573 {
1574         if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1575                 return 0;
1576         if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1577                 return 1;
1578         /* Case insensitive server? Revalidate negative dentries */
1579         if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1580                 return 1;
1581         return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
1582 }
1583
1584 static int
1585 nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
1586                            struct inode *inode, int error)
1587 {
1588         switch (error) {
1589         case 1:
1590                 break;
1591         case 0:
1592                 /*
1593                  * We can't d_drop the root of a disconnected tree:
1594                  * its d_hash is on the s_anon list and d_drop() would hide
1595                  * it from shrink_dcache_for_unmount(), leading to busy
1596                  * inodes on unmount and further oopses.
1597                  */
1598                 if (inode && IS_ROOT(dentry))
1599                         error = 1;
1600                 break;
1601         }
1602         trace_nfs_lookup_revalidate_exit(dir, dentry, 0, error);
1603         return error;
1604 }
1605
1606 static int
1607 nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
1608                                unsigned int flags)
1609 {
1610         int ret = 1;
1611         if (nfs_neg_need_reval(dir, dentry, flags)) {
1612                 if (flags & LOOKUP_RCU)
1613                         return -ECHILD;
1614                 ret = 0;
1615         }
1616         return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
1617 }
1618
1619 static int
1620 nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
1621                                 struct inode *inode)
1622 {
1623         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1624         return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1625 }
1626
1627 static int nfs_lookup_revalidate_dentry(struct inode *dir,
1628                                         struct dentry *dentry,
1629                                         struct inode *inode, unsigned int flags)
1630 {
1631         struct nfs_fh *fhandle;
1632         struct nfs_fattr *fattr;
1633         unsigned long dir_verifier;
1634         int ret;
1635
1636         trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
1637
1638         ret = -ENOMEM;
1639         fhandle = nfs_alloc_fhandle();
1640         fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
1641         if (fhandle == NULL || fattr == NULL)
1642                 goto out;
1643
1644         dir_verifier = nfs_save_change_attribute(dir);
1645         ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
1646         if (ret < 0) {
1647                 switch (ret) {
1648                 case -ESTALE:
1649                 case -ENOENT:
1650                         ret = 0;
1651                         break;
1652                 case -ETIMEDOUT:
1653                         if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1654                                 ret = 1;
1655                 }
1656                 goto out;
1657         }
1658
1659         /* Request help from readdirplus */
1660         nfs_lookup_advise_force_readdirplus(dir, flags);
1661
1662         ret = 0;
1663         if (nfs_compare_fh(NFS_FH(inode), fhandle))
1664                 goto out;
1665         if (nfs_refresh_inode(inode, fattr) < 0)
1666                 goto out;
1667
1668         nfs_setsecurity(inode, fattr);
1669         nfs_set_verifier(dentry, dir_verifier);
1670
1671         ret = 1;
1672 out:
1673         nfs_free_fattr(fattr);
1674         nfs_free_fhandle(fhandle);
1675
1676         /*
1677          * If the lookup failed despite the dentry change attribute being
1678          * a match, then we should revalidate the directory cache.
1679          */
1680         if (!ret && nfs_dentry_verify_change(dir, dentry))
1681                 nfs_mark_dir_for_revalidate(dir);
1682         return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
1683 }
1684
1685 /*
1686  * This is called every time the dcache has a lookup hit,
1687  * and we should check whether we can really trust that
1688  * lookup.
1689  *
1690  * NOTE! The hit can be a negative hit too, don't assume
1691  * we have an inode!
1692  *
1693  * If the parent directory is seen to have changed, we throw out the
1694  * cached dentry and do a new lookup.
1695  */
1696 static int
1697 nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1698                          unsigned int flags)
1699 {
1700         struct inode *inode;
1701         int error;
1702
1703         nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
1704         inode = d_inode(dentry);
1705
1706         if (!inode)
1707                 return nfs_lookup_revalidate_negative(dir, dentry, flags);
1708
1709         if (is_bad_inode(inode)) {
1710                 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1711                                 __func__, dentry);
1712                 goto out_bad;
1713         }
1714
1715         if (nfs_verifier_is_delegated(dentry))
1716                 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1717
1718         /* Force a full look up iff the parent directory has changed */
1719         if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
1720             nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
1721                 error = nfs_lookup_verify_inode(inode, flags);
1722                 if (error) {
1723                         if (error == -ESTALE)
1724                                 nfs_mark_dir_for_revalidate(dir);
1725                         goto out_bad;
1726                 }
1727                 goto out_valid;
1728         }
1729
1730         if (flags & LOOKUP_RCU)
1731                 return -ECHILD;
1732
1733         if (NFS_STALE(inode))
1734                 goto out_bad;
1735
1736         return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
1737 out_valid:
1738         return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
1739 out_bad:
1740         if (flags & LOOKUP_RCU)
1741                 return -ECHILD;
1742         return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
1743 }
1744
1745 static int
1746 __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1747                         int (*reval)(struct inode *, struct dentry *, unsigned int))
1748 {
1749         struct dentry *parent;
1750         struct inode *dir;
1751         int ret;
1752
1753         if (flags & LOOKUP_RCU) {
1754                 parent = READ_ONCE(dentry->d_parent);
1755                 dir = d_inode_rcu(parent);
1756                 if (!dir)
1757                         return -ECHILD;
1758                 ret = reval(dir, dentry, flags);
1759                 if (parent != READ_ONCE(dentry->d_parent))
1760                         return -ECHILD;
1761         } else {
1762                 parent = dget_parent(dentry);
1763                 ret = reval(d_inode(parent), dentry, flags);
1764                 dput(parent);
1765         }
1766         return ret;
1767 }
1768
1769 static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1770 {
1771         return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1772 }
1773
1774 /*
1775  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1776  * when we don't really care about the dentry name. This is called when a
1777  * pathwalk ends on a dentry that was not found via a normal lookup in the
1778  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1779  *
1780  * In this situation, we just want to verify that the inode itself is OK
1781  * since the dentry might have changed on the server.
1782  */
1783 static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1784 {
1785         struct inode *inode = d_inode(dentry);
1786         int error = 0;
1787
1788         /*
1789          * I believe we can only get a negative dentry here in the case of a
1790          * procfs-style symlink. Just assume it's correct for now, but we may
1791          * eventually need to do something more here.
1792          */
1793         if (!inode) {
1794                 dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
1795                                 __func__, dentry);
1796                 return 1;
1797         }
1798
1799         if (is_bad_inode(inode)) {
1800                 dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
1801                                 __func__, dentry);
1802                 return 0;
1803         }
1804
1805         error = nfs_lookup_verify_inode(inode, flags);
1806         dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1807                         __func__, inode->i_ino, error ? "invalid" : "valid");
1808         return !error;
1809 }
1810
1811 /*
1812  * This is called from dput() when d_count is going to 0.
1813  */
1814 static int nfs_dentry_delete(const struct dentry *dentry)
1815 {
1816         dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
1817                 dentry, dentry->d_flags);
1818
1819         /* Unhash any dentry with a stale inode */
1820         if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
1821                 return 1;
1822
1823         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1824                 /* Unhash it, so that ->d_iput() would be called */
1825                 return 1;
1826         }
1827         if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
1828                 /* Unhash it, so that ancestors of killed async unlink
1829                  * files will be cleaned up during umount */
1830                 return 1;
1831         }
1832         return 0;
1833
1834 }
1835
1836 /* Ensure that we revalidate inode->i_nlink */
1837 static void nfs_drop_nlink(struct inode *inode)
1838 {
1839         spin_lock(&inode->i_lock);
1840         /* drop the inode if we're reasonably sure this is the last link */
1841         if (inode->i_nlink > 0)
1842                 drop_nlink(inode);
1843         NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
1844         nfs_set_cache_invalid(
1845                 inode, NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME |
1846                                NFS_INO_INVALID_NLINK);
1847         spin_unlock(&inode->i_lock);
1848 }
1849
1850 /*
1851  * Called when the dentry loses inode.
1852  * We use it to clean up silly-renamed files.
1853  */
1854 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1855 {
1856         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1857                 nfs_complete_unlink(dentry, inode);
1858                 nfs_drop_nlink(inode);
1859         }
1860         iput(inode);
1861 }
1862
1863 static void nfs_d_release(struct dentry *dentry)
1864 {
1865         /* free cached devname value, if it survived that far */
1866         if (unlikely(dentry->d_fsdata)) {
1867                 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1868                         WARN_ON(1);
1869                 else
1870                         kfree(dentry->d_fsdata);
1871         }
1872 }
1873
1874 const struct dentry_operations nfs_dentry_operations = {
1875         .d_revalidate   = nfs_lookup_revalidate,
1876         .d_weak_revalidate      = nfs_weak_revalidate,
1877         .d_delete       = nfs_dentry_delete,
1878         .d_iput         = nfs_dentry_iput,
1879         .d_automount    = nfs_d_automount,
1880         .d_release      = nfs_d_release,
1881 };
1882 EXPORT_SYMBOL_GPL(nfs_dentry_operations);
1883
1884 struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1885 {
1886         struct dentry *res;
1887         struct inode *inode = NULL;
1888         struct nfs_fh *fhandle = NULL;
1889         struct nfs_fattr *fattr = NULL;
1890         unsigned long dir_verifier;
1891         int error;
1892
1893         dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
1894         nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
1895
1896         if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1897                 return ERR_PTR(-ENAMETOOLONG);
1898
1899         /*
1900          * If we're doing an exclusive create, optimize away the lookup
1901          * but don't hash the dentry.
1902          */
1903         if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1904                 return NULL;
1905
1906         res = ERR_PTR(-ENOMEM);
1907         fhandle = nfs_alloc_fhandle();
1908         fattr = nfs_alloc_fattr_with_label(NFS_SERVER(dir));
1909         if (fhandle == NULL || fattr == NULL)
1910                 goto out;
1911
1912         dir_verifier = nfs_save_change_attribute(dir);
1913         trace_nfs_lookup_enter(dir, dentry, flags);
1914         error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
1915         if (error == -ENOENT) {
1916                 if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
1917                         dir_verifier = inode_peek_iversion_raw(dir);
1918                 goto no_entry;
1919         }
1920         if (error < 0) {
1921                 res = ERR_PTR(error);
1922                 goto out;
1923         }
1924         inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1925         res = ERR_CAST(inode);
1926         if (IS_ERR(res))
1927                 goto out;
1928
1929         /* Notify readdir to use READDIRPLUS */
1930         nfs_lookup_advise_force_readdirplus(dir, flags);
1931
1932 no_entry:
1933         res = d_splice_alias(inode, dentry);
1934         if (res != NULL) {
1935                 if (IS_ERR(res))
1936                         goto out;
1937                 dentry = res;
1938         }
1939         nfs_set_verifier(dentry, dir_verifier);
1940 out:
1941         trace_nfs_lookup_exit(dir, dentry, flags, PTR_ERR_OR_ZERO(res));
1942         nfs_free_fattr(fattr);
1943         nfs_free_fhandle(fhandle);
1944         return res;
1945 }
1946 EXPORT_SYMBOL_GPL(nfs_lookup);
1947
1948 void nfs_d_prune_case_insensitive_aliases(struct inode *inode)
1949 {
1950         /* Case insensitive server? Revalidate dentries */
1951         if (inode && nfs_server_capable(inode, NFS_CAP_CASE_INSENSITIVE))
1952                 d_prune_aliases(inode);
1953 }
1954 EXPORT_SYMBOL_GPL(nfs_d_prune_case_insensitive_aliases);
1955
1956 #if IS_ENABLED(CONFIG_NFS_V4)
1957 static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
1958
1959 const struct dentry_operations nfs4_dentry_operations = {
1960         .d_revalidate   = nfs4_lookup_revalidate,
1961         .d_weak_revalidate      = nfs_weak_revalidate,
1962         .d_delete       = nfs_dentry_delete,
1963         .d_iput         = nfs_dentry_iput,
1964         .d_automount    = nfs_d_automount,
1965         .d_release      = nfs_d_release,
1966 };
1967 EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
1968
1969 static fmode_t flags_to_mode(int flags)
1970 {
1971         fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1972         if ((flags & O_ACCMODE) != O_WRONLY)
1973                 res |= FMODE_READ;
1974         if ((flags & O_ACCMODE) != O_RDONLY)
1975                 res |= FMODE_WRITE;
1976         return res;
1977 }
1978
1979 static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1980 {
1981         return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1982 }
1983
1984 static int do_open(struct inode *inode, struct file *filp)
1985 {
1986         nfs_fscache_open_file(inode, filp);
1987         return 0;
1988 }
1989
1990 static int nfs_finish_open(struct nfs_open_context *ctx,
1991                            struct dentry *dentry,
1992                            struct file *file, unsigned open_flags)
1993 {
1994         int err;
1995
1996         err = finish_open(file, dentry, do_open);
1997         if (err)
1998                 goto out;
1999         if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
2000                 nfs_file_set_open_context(file, ctx);
2001         else
2002                 err = -EOPENSTALE;
2003 out:
2004         return err;
2005 }
2006
2007 int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
2008                     struct file *file, unsigned open_flags,
2009                     umode_t mode)
2010 {
2011         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2012         struct nfs_open_context *ctx;
2013         struct dentry *res;
2014         struct iattr attr = { .ia_valid = ATTR_OPEN };
2015         struct inode *inode;
2016         unsigned int lookup_flags = 0;
2017         unsigned long dir_verifier;
2018         bool switched = false;
2019         int created = 0;
2020         int err;
2021
2022         /* Expect a negative dentry */
2023         BUG_ON(d_inode(dentry));
2024
2025         dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
2026                         dir->i_sb->s_id, dir->i_ino, dentry);
2027
2028         err = nfs_check_flags(open_flags);
2029         if (err)
2030                 return err;
2031
2032         /* NFS only supports OPEN on regular files */
2033         if ((open_flags & O_DIRECTORY)) {
2034                 if (!d_in_lookup(dentry)) {
2035                         /*
2036                          * Hashed negative dentry with O_DIRECTORY: dentry was
2037                          * revalidated and is fine, no need to perform lookup
2038                          * again
2039                          */
2040                         return -ENOENT;
2041                 }
2042                 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
2043                 goto no_open;
2044         }
2045
2046         if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
2047                 return -ENAMETOOLONG;
2048
2049         if (open_flags & O_CREAT) {
2050                 struct nfs_server *server = NFS_SERVER(dir);
2051
2052                 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
2053                         mode &= ~current_umask();
2054
2055                 attr.ia_valid |= ATTR_MODE;
2056                 attr.ia_mode = mode;
2057         }
2058         if (open_flags & O_TRUNC) {
2059                 attr.ia_valid |= ATTR_SIZE;
2060                 attr.ia_size = 0;
2061         }
2062
2063         if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
2064                 d_drop(dentry);
2065                 switched = true;
2066                 dentry = d_alloc_parallel(dentry->d_parent,
2067                                           &dentry->d_name, &wq);
2068                 if (IS_ERR(dentry))
2069                         return PTR_ERR(dentry);
2070                 if (unlikely(!d_in_lookup(dentry)))
2071                         return finish_no_open(file, dentry);
2072         }
2073
2074         ctx = create_nfs_open_context(dentry, open_flags, file);
2075         err = PTR_ERR(ctx);
2076         if (IS_ERR(ctx))
2077                 goto out;
2078
2079         trace_nfs_atomic_open_enter(dir, ctx, open_flags);
2080         inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
2081         if (created)
2082                 file->f_mode |= FMODE_CREATED;
2083         if (IS_ERR(inode)) {
2084                 err = PTR_ERR(inode);
2085                 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2086                 put_nfs_open_context(ctx);
2087                 d_drop(dentry);
2088                 switch (err) {
2089                 case -ENOENT:
2090                         d_splice_alias(NULL, dentry);
2091                         if (nfs_server_capable(dir, NFS_CAP_CASE_INSENSITIVE))
2092                                 dir_verifier = inode_peek_iversion_raw(dir);
2093                         else
2094                                 dir_verifier = nfs_save_change_attribute(dir);
2095                         nfs_set_verifier(dentry, dir_verifier);
2096                         break;
2097                 case -EISDIR:
2098                 case -ENOTDIR:
2099                         goto no_open;
2100                 case -ELOOP:
2101                         if (!(open_flags & O_NOFOLLOW))
2102                                 goto no_open;
2103                         break;
2104                         /* case -EINVAL: */
2105                 default:
2106                         break;
2107                 }
2108                 goto out;
2109         }
2110
2111         err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
2112         trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
2113         put_nfs_open_context(ctx);
2114 out:
2115         if (unlikely(switched)) {
2116                 d_lookup_done(dentry);
2117                 dput(dentry);
2118         }
2119         return err;
2120
2121 no_open:
2122         res = nfs_lookup(dir, dentry, lookup_flags);
2123         if (!res) {
2124                 inode = d_inode(dentry);
2125                 if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2126                     !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)))
2127                         res = ERR_PTR(-ENOTDIR);
2128                 else if (inode && S_ISREG(inode->i_mode))
2129                         res = ERR_PTR(-EOPENSTALE);
2130         } else if (!IS_ERR(res)) {
2131                 inode = d_inode(res);
2132                 if ((lookup_flags & LOOKUP_DIRECTORY) && inode &&
2133                     !(S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))) {
2134                         dput(res);
2135                         res = ERR_PTR(-ENOTDIR);
2136                 } else if (inode && S_ISREG(inode->i_mode)) {
2137                         dput(res);
2138                         res = ERR_PTR(-EOPENSTALE);
2139                 }
2140         }
2141         if (switched) {
2142                 d_lookup_done(dentry);
2143                 if (!res)
2144                         res = dentry;
2145                 else
2146                         dput(dentry);
2147         }
2148         if (IS_ERR(res))
2149                 return PTR_ERR(res);
2150         return finish_no_open(file, res);
2151 }
2152 EXPORT_SYMBOL_GPL(nfs_atomic_open);
2153
2154 static int
2155 nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
2156                           unsigned int flags)
2157 {
2158         struct inode *inode;
2159
2160         if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
2161                 goto full_reval;
2162         if (d_mountpoint(dentry))
2163                 goto full_reval;
2164
2165         inode = d_inode(dentry);
2166
2167         /* We can't create new files in nfs_open_revalidate(), so we
2168          * optimize away revalidation of negative dentries.
2169          */
2170         if (inode == NULL)
2171                 goto full_reval;
2172
2173         if (nfs_verifier_is_delegated(dentry))
2174                 return nfs_lookup_revalidate_delegated(dir, dentry, inode);
2175
2176         /* NFS only supports OPEN on regular files */
2177         if (!S_ISREG(inode->i_mode))
2178                 goto full_reval;
2179
2180         /* We cannot do exclusive creation on a positive dentry */
2181         if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
2182                 goto reval_dentry;
2183
2184         /* Check if the directory changed */
2185         if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
2186                 goto reval_dentry;
2187
2188         /* Let f_op->open() actually open (and revalidate) the file */
2189         return 1;
2190 reval_dentry:
2191         if (flags & LOOKUP_RCU)
2192                 return -ECHILD;
2193         return nfs_lookup_revalidate_dentry(dir, dentry, inode, flags);
2194
2195 full_reval:
2196         return nfs_do_lookup_revalidate(dir, dentry, flags);
2197 }
2198
2199 static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
2200 {
2201         return __nfs_lookup_revalidate(dentry, flags,
2202                         nfs4_do_lookup_revalidate);
2203 }
2204
2205 #endif /* CONFIG_NFSV4 */
2206
2207 struct dentry *
2208 nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
2209                                 struct nfs_fattr *fattr)
2210 {
2211         struct dentry *parent = dget_parent(dentry);
2212         struct inode *dir = d_inode(parent);
2213         struct inode *inode;
2214         struct dentry *d;
2215         int error;
2216
2217         d_drop(dentry);
2218
2219         if (fhandle->size == 0) {
2220                 error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr);
2221                 if (error)
2222                         goto out_error;
2223         }
2224         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2225         if (!(fattr->valid & NFS_ATTR_FATTR)) {
2226                 struct nfs_server *server = NFS_SB(dentry->d_sb);
2227                 error = server->nfs_client->rpc_ops->getattr(server, fhandle,
2228                                 fattr, NULL);
2229                 if (error < 0)
2230                         goto out_error;
2231         }
2232         inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
2233         d = d_splice_alias(inode, dentry);
2234 out:
2235         dput(parent);
2236         return d;
2237 out_error:
2238         d = ERR_PTR(error);
2239         goto out;
2240 }
2241 EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
2242
2243 /*
2244  * Code common to create, mkdir, and mknod.
2245  */
2246 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
2247                                 struct nfs_fattr *fattr)
2248 {
2249         struct dentry *d;
2250
2251         d = nfs_add_or_obtain(dentry, fhandle, fattr);
2252         if (IS_ERR(d))
2253                 return PTR_ERR(d);
2254
2255         /* Callers don't care */
2256         dput(d);
2257         return 0;
2258 }
2259 EXPORT_SYMBOL_GPL(nfs_instantiate);
2260
2261 /*
2262  * Following a failed create operation, we drop the dentry rather
2263  * than retain a negative dentry. This avoids a problem in the event
2264  * that the operation succeeded on the server, but an error in the
2265  * reply path made it appear to have failed.
2266  */
2267 int nfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2268                struct dentry *dentry, umode_t mode, bool excl)
2269 {
2270         struct iattr attr;
2271         int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
2272         int error;
2273
2274         dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
2275                         dir->i_sb->s_id, dir->i_ino, dentry);
2276
2277         attr.ia_mode = mode;
2278         attr.ia_valid = ATTR_MODE;
2279
2280         trace_nfs_create_enter(dir, dentry, open_flags);
2281         error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
2282         trace_nfs_create_exit(dir, dentry, open_flags, error);
2283         if (error != 0)
2284                 goto out_err;
2285         return 0;
2286 out_err:
2287         d_drop(dentry);
2288         return error;
2289 }
2290 EXPORT_SYMBOL_GPL(nfs_create);
2291
2292 /*
2293  * See comments for nfs_proc_create regarding failed operations.
2294  */
2295 int
2296 nfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
2297           struct dentry *dentry, umode_t mode, dev_t rdev)
2298 {
2299         struct iattr attr;
2300         int status;
2301
2302         dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
2303                         dir->i_sb->s_id, dir->i_ino, dentry);
2304
2305         attr.ia_mode = mode;
2306         attr.ia_valid = ATTR_MODE;
2307
2308         trace_nfs_mknod_enter(dir, dentry);
2309         status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
2310         trace_nfs_mknod_exit(dir, dentry, status);
2311         if (status != 0)
2312                 goto out_err;
2313         return 0;
2314 out_err:
2315         d_drop(dentry);
2316         return status;
2317 }
2318 EXPORT_SYMBOL_GPL(nfs_mknod);
2319
2320 /*
2321  * See comments for nfs_proc_create regarding failed operations.
2322  */
2323 int nfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
2324               struct dentry *dentry, umode_t mode)
2325 {
2326         struct iattr attr;
2327         int error;
2328
2329         dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
2330                         dir->i_sb->s_id, dir->i_ino, dentry);
2331
2332         attr.ia_valid = ATTR_MODE;
2333         attr.ia_mode = mode | S_IFDIR;
2334
2335         trace_nfs_mkdir_enter(dir, dentry);
2336         error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
2337         trace_nfs_mkdir_exit(dir, dentry, error);
2338         if (error != 0)
2339                 goto out_err;
2340         return 0;
2341 out_err:
2342         d_drop(dentry);
2343         return error;
2344 }
2345 EXPORT_SYMBOL_GPL(nfs_mkdir);
2346
2347 static void nfs_dentry_handle_enoent(struct dentry *dentry)
2348 {
2349         if (simple_positive(dentry))
2350                 d_delete(dentry);
2351 }
2352
2353 static void nfs_dentry_remove_handle_error(struct inode *dir,
2354                                            struct dentry *dentry, int error)
2355 {
2356         switch (error) {
2357         case -ENOENT:
2358                 d_delete(dentry);
2359                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2360                 break;
2361         case 0:
2362                 nfs_d_prune_case_insensitive_aliases(d_inode(dentry));
2363                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2364         }
2365 }
2366
2367 int nfs_rmdir(struct inode *dir, struct dentry *dentry)
2368 {
2369         int error;
2370
2371         dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
2372                         dir->i_sb->s_id, dir->i_ino, dentry);
2373
2374         trace_nfs_rmdir_enter(dir, dentry);
2375         if (d_really_is_positive(dentry)) {
2376                 down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2377                 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2378                 /* Ensure the VFS deletes this inode */
2379                 switch (error) {
2380                 case 0:
2381                         clear_nlink(d_inode(dentry));
2382                         break;
2383                 case -ENOENT:
2384                         nfs_dentry_handle_enoent(dentry);
2385                 }
2386                 up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2387         } else
2388                 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
2389         nfs_dentry_remove_handle_error(dir, dentry, error);
2390         trace_nfs_rmdir_exit(dir, dentry, error);
2391
2392         return error;
2393 }
2394 EXPORT_SYMBOL_GPL(nfs_rmdir);
2395
2396 /*
2397  * Remove a file after making sure there are no pending writes,
2398  * and after checking that the file has only one user. 
2399  *
2400  * We invalidate the attribute cache and free the inode prior to the operation
2401  * to avoid possible races if the server reuses the inode.
2402  */
2403 static int nfs_safe_remove(struct dentry *dentry)
2404 {
2405         struct inode *dir = d_inode(dentry->d_parent);
2406         struct inode *inode = d_inode(dentry);
2407         int error = -EBUSY;
2408                 
2409         dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
2410
2411         /* If the dentry was sillyrenamed, we simply call d_delete() */
2412         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
2413                 error = 0;
2414                 goto out;
2415         }
2416
2417         trace_nfs_remove_enter(dir, dentry);
2418         if (inode != NULL) {
2419                 error = NFS_PROTO(dir)->remove(dir, dentry);
2420                 if (error == 0)
2421                         nfs_drop_nlink(inode);
2422         } else
2423                 error = NFS_PROTO(dir)->remove(dir, dentry);
2424         if (error == -ENOENT)
2425                 nfs_dentry_handle_enoent(dentry);
2426         trace_nfs_remove_exit(dir, dentry, error);
2427 out:
2428         return error;
2429 }
2430
2431 /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
2432  *  belongs to an active ".nfs..." file and we return -EBUSY.
2433  *
2434  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
2435  */
2436 int nfs_unlink(struct inode *dir, struct dentry *dentry)
2437 {
2438         int error;
2439         int need_rehash = 0;
2440
2441         dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
2442                 dir->i_ino, dentry);
2443
2444         trace_nfs_unlink_enter(dir, dentry);
2445         spin_lock(&dentry->d_lock);
2446         if (d_count(dentry) > 1 && !test_bit(NFS_INO_PRESERVE_UNLINKED,
2447                                              &NFS_I(d_inode(dentry))->flags)) {
2448                 spin_unlock(&dentry->d_lock);
2449                 /* Start asynchronous writeout of the inode */
2450                 write_inode_now(d_inode(dentry), 0);
2451                 error = nfs_sillyrename(dir, dentry);
2452                 goto out;
2453         }
2454         if (!d_unhashed(dentry)) {
2455                 __d_drop(dentry);
2456                 need_rehash = 1;
2457         }
2458         spin_unlock(&dentry->d_lock);
2459         error = nfs_safe_remove(dentry);
2460         nfs_dentry_remove_handle_error(dir, dentry, error);
2461         if (need_rehash)
2462                 d_rehash(dentry);
2463 out:
2464         trace_nfs_unlink_exit(dir, dentry, error);
2465         return error;
2466 }
2467 EXPORT_SYMBOL_GPL(nfs_unlink);
2468
2469 /*
2470  * To create a symbolic link, most file systems instantiate a new inode,
2471  * add a page to it containing the path, then write it out to the disk
2472  * using prepare_write/commit_write.
2473  *
2474  * Unfortunately the NFS client can't create the in-core inode first
2475  * because it needs a file handle to create an in-core inode (see
2476  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2477  * symlink request has completed on the server.
2478  *
2479  * So instead we allocate a raw page, copy the symname into it, then do
2480  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2481  * now have a new file handle and can instantiate an in-core NFS inode
2482  * and move the raw page into its mapping.
2483  */
2484 int nfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
2485                 struct dentry *dentry, const char *symname)
2486 {
2487         struct page *page;
2488         char *kaddr;
2489         struct iattr attr;
2490         unsigned int pathlen = strlen(symname);
2491         int error;
2492
2493         dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
2494                 dir->i_ino, dentry, symname);
2495
2496         if (pathlen > PAGE_SIZE)
2497                 return -ENAMETOOLONG;
2498
2499         attr.ia_mode = S_IFLNK | S_IRWXUGO;
2500         attr.ia_valid = ATTR_MODE;
2501
2502         page = alloc_page(GFP_USER);
2503         if (!page)
2504                 return -ENOMEM;
2505
2506         kaddr = page_address(page);
2507         memcpy(kaddr, symname, pathlen);
2508         if (pathlen < PAGE_SIZE)
2509                 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2510
2511         trace_nfs_symlink_enter(dir, dentry);
2512         error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
2513         trace_nfs_symlink_exit(dir, dentry, error);
2514         if (error != 0) {
2515                 dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2516                         dir->i_sb->s_id, dir->i_ino,
2517                         dentry, symname, error);
2518                 d_drop(dentry);
2519                 __free_page(page);
2520                 return error;
2521         }
2522
2523         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2524
2525         /*
2526          * No big deal if we can't add this page to the page cache here.
2527          * READLINK will get the missing page from the server if needed.
2528          */
2529         if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2530                                                         GFP_KERNEL)) {
2531                 SetPageUptodate(page);
2532                 unlock_page(page);
2533                 /*
2534                  * add_to_page_cache_lru() grabs an extra page refcount.
2535                  * Drop it here to avoid leaking this page later.
2536                  */
2537                 put_page(page);
2538         } else
2539                 __free_page(page);
2540
2541         return 0;
2542 }
2543 EXPORT_SYMBOL_GPL(nfs_symlink);
2544
2545 int
2546 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2547 {
2548         struct inode *inode = d_inode(old_dentry);
2549         int error;
2550
2551         dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
2552                 old_dentry, dentry);
2553
2554         trace_nfs_link_enter(inode, dir, dentry);
2555         d_drop(dentry);
2556         if (S_ISREG(inode->i_mode))
2557                 nfs_sync_inode(inode);
2558         error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2559         if (error == 0) {
2560                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2561                 ihold(inode);
2562                 d_add(dentry, inode);
2563         }
2564         trace_nfs_link_exit(inode, dir, dentry, error);
2565         return error;
2566 }
2567 EXPORT_SYMBOL_GPL(nfs_link);
2568
2569 /*
2570  * RENAME
2571  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
2572  * different file handle for the same inode after a rename (e.g. when
2573  * moving to a different directory). A fail-safe method to do so would
2574  * be to look up old_dir/old_name, create a link to new_dir/new_name and
2575  * rename the old file using the sillyrename stuff. This way, the original
2576  * file in old_dir will go away when the last process iput()s the inode.
2577  *
2578  * FIXED.
2579  * 
2580  * It actually works quite well. One needs to have the possibility for
2581  * at least one ".nfs..." file in each directory the file ever gets
2582  * moved or linked to which happens automagically with the new
2583  * implementation that only depends on the dcache stuff instead of
2584  * using the inode layer
2585  *
2586  * Unfortunately, things are a little more complicated than indicated
2587  * above. For a cross-directory move, we want to make sure we can get
2588  * rid of the old inode after the operation.  This means there must be
2589  * no pending writes (if it's a file), and the use count must be 1.
2590  * If these conditions are met, we can drop the dentries before doing
2591  * the rename.
2592  */
2593 int nfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
2594                struct dentry *old_dentry, struct inode *new_dir,
2595                struct dentry *new_dentry, unsigned int flags)
2596 {
2597         struct inode *old_inode = d_inode(old_dentry);
2598         struct inode *new_inode = d_inode(new_dentry);
2599         struct dentry *dentry = NULL, *rehash = NULL;
2600         struct rpc_task *task;
2601         int error = -EBUSY;
2602
2603         if (flags)
2604                 return -EINVAL;
2605
2606         dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
2607                  old_dentry, new_dentry,
2608                  d_count(new_dentry));
2609
2610         trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
2611         /*
2612          * For non-directories, check whether the target is busy and if so,
2613          * make a copy of the dentry and then do a silly-rename. If the
2614          * silly-rename succeeds, the copied dentry is hashed and becomes
2615          * the new target.
2616          */
2617         if (new_inode && !S_ISDIR(new_inode->i_mode)) {
2618                 /*
2619                  * To prevent any new references to the target during the
2620                  * rename, we unhash the dentry in advance.
2621                  */
2622                 if (!d_unhashed(new_dentry)) {
2623                         d_drop(new_dentry);
2624                         rehash = new_dentry;
2625                 }
2626
2627                 if (d_count(new_dentry) > 2) {
2628                         int err;
2629
2630                         /* copy the target dentry's name */
2631                         dentry = d_alloc(new_dentry->d_parent,
2632                                          &new_dentry->d_name);
2633                         if (!dentry)
2634                                 goto out;
2635
2636                         /* silly-rename the existing target ... */
2637                         err = nfs_sillyrename(new_dir, new_dentry);
2638                         if (err)
2639                                 goto out;
2640
2641                         new_dentry = dentry;
2642                         rehash = NULL;
2643                         new_inode = NULL;
2644                 }
2645         }
2646
2647         if (S_ISREG(old_inode->i_mode))
2648                 nfs_sync_inode(old_inode);
2649         task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
2650         if (IS_ERR(task)) {
2651                 error = PTR_ERR(task);
2652                 goto out;
2653         }
2654
2655         error = rpc_wait_for_completion_task(task);
2656         if (error != 0) {
2657                 ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2658                 /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2659                 smp_wmb();
2660         } else
2661                 error = task->tk_status;
2662         rpc_put_task(task);
2663         /* Ensure the inode attributes are revalidated */
2664         if (error == 0) {
2665                 spin_lock(&old_inode->i_lock);
2666                 NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
2667                 nfs_set_cache_invalid(old_inode, NFS_INO_INVALID_CHANGE |
2668                                                          NFS_INO_INVALID_CTIME |
2669                                                          NFS_INO_REVAL_FORCED);
2670                 spin_unlock(&old_inode->i_lock);
2671         }
2672 out:
2673         if (rehash)
2674                 d_rehash(rehash);
2675         trace_nfs_rename_exit(old_dir, old_dentry,
2676                         new_dir, new_dentry, error);
2677         if (!error) {
2678                 if (new_inode != NULL)
2679                         nfs_drop_nlink(new_inode);
2680                 /*
2681                  * The d_move() should be here instead of in an async RPC completion
2682                  * handler because we need the proper locks to move the dentry.  If
2683                  * we're interrupted by a signal, the async RPC completion handler
2684                  * should mark the directories for revalidation.
2685                  */
2686                 d_move(old_dentry, new_dentry);
2687                 nfs_set_verifier(old_dentry,
2688                                         nfs_save_change_attribute(new_dir));
2689         } else if (error == -ENOENT)
2690                 nfs_dentry_handle_enoent(old_dentry);
2691
2692         /* new dentry created? */
2693         if (dentry)
2694                 dput(dentry);
2695         return error;
2696 }
2697 EXPORT_SYMBOL_GPL(nfs_rename);
2698
2699 static DEFINE_SPINLOCK(nfs_access_lru_lock);
2700 static LIST_HEAD(nfs_access_lru_list);
2701 static atomic_long_t nfs_access_nr_entries;
2702
2703 static unsigned long nfs_access_max_cachesize = 4*1024*1024;
2704 module_param(nfs_access_max_cachesize, ulong, 0644);
2705 MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
2706
2707 static void nfs_access_free_entry(struct nfs_access_entry *entry)
2708 {
2709         put_group_info(entry->group_info);
2710         kfree_rcu(entry, rcu_head);
2711         smp_mb__before_atomic();
2712         atomic_long_dec(&nfs_access_nr_entries);
2713         smp_mb__after_atomic();
2714 }
2715
2716 static void nfs_access_free_list(struct list_head *head)
2717 {
2718         struct nfs_access_entry *cache;
2719
2720         while (!list_empty(head)) {
2721                 cache = list_entry(head->next, struct nfs_access_entry, lru);
2722                 list_del(&cache->lru);
2723                 nfs_access_free_entry(cache);
2724         }
2725 }
2726
2727 static unsigned long
2728 nfs_do_access_cache_scan(unsigned int nr_to_scan)
2729 {
2730         LIST_HEAD(head);
2731         struct nfs_inode *nfsi, *next;
2732         struct nfs_access_entry *cache;
2733         long freed = 0;
2734
2735         spin_lock(&nfs_access_lru_lock);
2736         list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2737                 struct inode *inode;
2738
2739                 if (nr_to_scan-- == 0)
2740                         break;
2741                 inode = &nfsi->vfs_inode;
2742                 spin_lock(&inode->i_lock);
2743                 if (list_empty(&nfsi->access_cache_entry_lru))
2744                         goto remove_lru_entry;
2745                 cache = list_entry(nfsi->access_cache_entry_lru.next,
2746                                 struct nfs_access_entry, lru);
2747                 list_move(&cache->lru, &head);
2748                 rb_erase(&cache->rb_node, &nfsi->access_cache);
2749                 freed++;
2750                 if (!list_empty(&nfsi->access_cache_entry_lru))
2751                         list_move_tail(&nfsi->access_cache_inode_lru,
2752                                         &nfs_access_lru_list);
2753                 else {
2754 remove_lru_entry:
2755                         list_del_init(&nfsi->access_cache_inode_lru);
2756                         smp_mb__before_atomic();
2757                         clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
2758                         smp_mb__after_atomic();
2759                 }
2760                 spin_unlock(&inode->i_lock);
2761         }
2762         spin_unlock(&nfs_access_lru_lock);
2763         nfs_access_free_list(&head);
2764         return freed;
2765 }
2766
2767 unsigned long
2768 nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2769 {
2770         int nr_to_scan = sc->nr_to_scan;
2771         gfp_t gfp_mask = sc->gfp_mask;
2772
2773         if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2774                 return SHRINK_STOP;
2775         return nfs_do_access_cache_scan(nr_to_scan);
2776 }
2777
2778
2779 unsigned long
2780 nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2781 {
2782         return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2783 }
2784
2785 static void
2786 nfs_access_cache_enforce_limit(void)
2787 {
2788         long nr_entries = atomic_long_read(&nfs_access_nr_entries);
2789         unsigned long diff;
2790         unsigned int nr_to_scan;
2791
2792         if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
2793                 return;
2794         nr_to_scan = 100;
2795         diff = nr_entries - nfs_access_max_cachesize;
2796         if (diff < nr_to_scan)
2797                 nr_to_scan = diff;
2798         nfs_do_access_cache_scan(nr_to_scan);
2799 }
2800
2801 static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
2802 {
2803         struct rb_root *root_node = &nfsi->access_cache;
2804         struct rb_node *n;
2805         struct nfs_access_entry *entry;
2806
2807         /* Unhook entries from the cache */
2808         while ((n = rb_first(root_node)) != NULL) {
2809                 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2810                 rb_erase(n, root_node);
2811                 list_move(&entry->lru, head);
2812         }
2813         nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
2814 }
2815
2816 void nfs_access_zap_cache(struct inode *inode)
2817 {
2818         LIST_HEAD(head);
2819
2820         if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2821                 return;
2822         /* Remove from global LRU init */
2823         spin_lock(&nfs_access_lru_lock);
2824         if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2825                 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2826
2827         spin_lock(&inode->i_lock);
2828         __nfs_access_zap_cache(NFS_I(inode), &head);
2829         spin_unlock(&inode->i_lock);
2830         spin_unlock(&nfs_access_lru_lock);
2831         nfs_access_free_list(&head);
2832 }
2833 EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
2834
2835 static int access_cmp(const struct cred *a, const struct nfs_access_entry *b)
2836 {
2837         struct group_info *ga, *gb;
2838         int g;
2839
2840         if (uid_lt(a->fsuid, b->fsuid))
2841                 return -1;
2842         if (uid_gt(a->fsuid, b->fsuid))
2843                 return 1;
2844
2845         if (gid_lt(a->fsgid, b->fsgid))
2846                 return -1;
2847         if (gid_gt(a->fsgid, b->fsgid))
2848                 return 1;
2849
2850         ga = a->group_info;
2851         gb = b->group_info;
2852         if (ga == gb)
2853                 return 0;
2854         if (ga == NULL)
2855                 return -1;
2856         if (gb == NULL)
2857                 return 1;
2858         if (ga->ngroups < gb->ngroups)
2859                 return -1;
2860         if (ga->ngroups > gb->ngroups)
2861                 return 1;
2862
2863         for (g = 0; g < ga->ngroups; g++) {
2864                 if (gid_lt(ga->gid[g], gb->gid[g]))
2865                         return -1;
2866                 if (gid_gt(ga->gid[g], gb->gid[g]))
2867                         return 1;
2868         }
2869         return 0;
2870 }
2871
2872 static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
2873 {
2874         struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2875
2876         while (n != NULL) {
2877                 struct nfs_access_entry *entry =
2878                         rb_entry(n, struct nfs_access_entry, rb_node);
2879                 int cmp = access_cmp(cred, entry);
2880
2881                 if (cmp < 0)
2882                         n = n->rb_left;
2883                 else if (cmp > 0)
2884                         n = n->rb_right;
2885                 else
2886                         return entry;
2887         }
2888         return NULL;
2889 }
2890
2891 static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, u32 *mask, bool may_block)
2892 {
2893         struct nfs_inode *nfsi = NFS_I(inode);
2894         struct nfs_access_entry *cache;
2895         bool retry = true;
2896         int err;
2897
2898         spin_lock(&inode->i_lock);
2899         for(;;) {
2900                 if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2901                         goto out_zap;
2902                 cache = nfs_access_search_rbtree(inode, cred);
2903                 err = -ENOENT;
2904                 if (cache == NULL)
2905                         goto out;
2906                 /* Found an entry, is our attribute cache valid? */
2907                 if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2908                         break;
2909                 if (!retry)
2910                         break;
2911                 err = -ECHILD;
2912                 if (!may_block)
2913                         goto out;
2914                 spin_unlock(&inode->i_lock);
2915                 err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
2916                 if (err)
2917                         return err;
2918                 spin_lock(&inode->i_lock);
2919                 retry = false;
2920         }
2921         *mask = cache->mask;
2922         list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
2923         err = 0;
2924 out:
2925         spin_unlock(&inode->i_lock);
2926         return err;
2927 out_zap:
2928         spin_unlock(&inode->i_lock);
2929         nfs_access_zap_cache(inode);
2930         return -ENOENT;
2931 }
2932
2933 static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, u32 *mask)
2934 {
2935         /* Only check the most recently returned cache entry,
2936          * but do it without locking.
2937          */
2938         struct nfs_inode *nfsi = NFS_I(inode);
2939         struct nfs_access_entry *cache;
2940         int err = -ECHILD;
2941         struct list_head *lh;
2942
2943         rcu_read_lock();
2944         if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2945                 goto out;
2946         lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2947         cache = list_entry(lh, struct nfs_access_entry, lru);
2948         if (lh == &nfsi->access_cache_entry_lru ||
2949             access_cmp(cred, cache) != 0)
2950                 cache = NULL;
2951         if (cache == NULL)
2952                 goto out;
2953         if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2954                 goto out;
2955         *mask = cache->mask;
2956         err = 0;
2957 out:
2958         rcu_read_unlock();
2959         return err;
2960 }
2961
2962 int nfs_access_get_cached(struct inode *inode, const struct cred *cred,
2963                           u32 *mask, bool may_block)
2964 {
2965         int status;
2966
2967         status = nfs_access_get_cached_rcu(inode, cred, mask);
2968         if (status != 0)
2969                 status = nfs_access_get_cached_locked(inode, cred, mask,
2970                     may_block);
2971
2972         return status;
2973 }
2974 EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2975
2976 static void nfs_access_add_rbtree(struct inode *inode,
2977                                   struct nfs_access_entry *set,
2978                                   const struct cred *cred)
2979 {
2980         struct nfs_inode *nfsi = NFS_I(inode);
2981         struct rb_root *root_node = &nfsi->access_cache;
2982         struct rb_node **p = &root_node->rb_node;
2983         struct rb_node *parent = NULL;
2984         struct nfs_access_entry *entry;
2985         int cmp;
2986
2987         spin_lock(&inode->i_lock);
2988         while (*p != NULL) {
2989                 parent = *p;
2990                 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2991                 cmp = access_cmp(cred, entry);
2992
2993                 if (cmp < 0)
2994                         p = &parent->rb_left;
2995                 else if (cmp > 0)
2996                         p = &parent->rb_right;
2997                 else
2998                         goto found;
2999         }
3000         rb_link_node(&set->rb_node, parent, p);
3001         rb_insert_color(&set->rb_node, root_node);
3002         list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
3003         spin_unlock(&inode->i_lock);
3004         return;
3005 found:
3006         rb_replace_node(parent, &set->rb_node, root_node);
3007         list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
3008         list_del(&entry->lru);
3009         spin_unlock(&inode->i_lock);
3010         nfs_access_free_entry(entry);
3011 }
3012
3013 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set,
3014                           const struct cred *cred)
3015 {
3016         struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
3017         if (cache == NULL)
3018                 return;
3019         RB_CLEAR_NODE(&cache->rb_node);
3020         cache->fsuid = cred->fsuid;
3021         cache->fsgid = cred->fsgid;
3022         cache->group_info = get_group_info(cred->group_info);
3023         cache->mask = set->mask;
3024
3025         /* The above field assignments must be visible
3026          * before this item appears on the lru.  We cannot easily
3027          * use rcu_assign_pointer, so just force the memory barrier.
3028          */
3029         smp_wmb();
3030         nfs_access_add_rbtree(inode, cache, cred);
3031
3032         /* Update accounting */
3033         smp_mb__before_atomic();
3034         atomic_long_inc(&nfs_access_nr_entries);
3035         smp_mb__after_atomic();
3036
3037         /* Add inode to global LRU list */
3038         if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
3039                 spin_lock(&nfs_access_lru_lock);
3040                 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
3041                         list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
3042                                         &nfs_access_lru_list);
3043                 spin_unlock(&nfs_access_lru_lock);
3044         }
3045         nfs_access_cache_enforce_limit();
3046 }
3047 EXPORT_SYMBOL_GPL(nfs_access_add_cache);
3048
3049 #define NFS_MAY_READ (NFS_ACCESS_READ)
3050 #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
3051                 NFS_ACCESS_EXTEND | \
3052                 NFS_ACCESS_DELETE)
3053 #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
3054                 NFS_ACCESS_EXTEND)
3055 #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
3056 #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
3057 #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
3058 static int
3059 nfs_access_calc_mask(u32 access_result, umode_t umode)
3060 {
3061         int mask = 0;
3062
3063         if (access_result & NFS_MAY_READ)
3064                 mask |= MAY_READ;
3065         if (S_ISDIR(umode)) {
3066                 if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
3067                         mask |= MAY_WRITE;
3068                 if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
3069                         mask |= MAY_EXEC;
3070         } else if (S_ISREG(umode)) {
3071                 if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
3072                         mask |= MAY_WRITE;
3073                 if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
3074                         mask |= MAY_EXEC;
3075         } else if (access_result & NFS_MAY_WRITE)
3076                         mask |= MAY_WRITE;
3077         return mask;
3078 }
3079
3080 void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
3081 {
3082         entry->mask = access_result;
3083 }
3084 EXPORT_SYMBOL_GPL(nfs_access_set_mask);
3085
3086 static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
3087 {
3088         struct nfs_access_entry cache;
3089         bool may_block = (mask & MAY_NOT_BLOCK) == 0;
3090         int cache_mask = -1;
3091         int status;
3092
3093         trace_nfs_access_enter(inode);
3094
3095         status = nfs_access_get_cached(inode, cred, &cache.mask, may_block);
3096         if (status == 0)
3097                 goto out_cached;
3098
3099         status = -ECHILD;
3100         if (!may_block)
3101                 goto out;
3102
3103         /*
3104          * Determine which access bits we want to ask for...
3105          */
3106         cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND |
3107                      nfs_access_xattr_mask(NFS_SERVER(inode));
3108         if (S_ISDIR(inode->i_mode))
3109                 cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
3110         else
3111                 cache.mask |= NFS_ACCESS_EXECUTE;
3112         status = NFS_PROTO(inode)->access(inode, &cache, cred);
3113         if (status != 0) {
3114                 if (status == -ESTALE) {
3115                         if (!S_ISDIR(inode->i_mode))
3116                                 nfs_set_inode_stale(inode);
3117                         else
3118                                 nfs_zap_caches(inode);
3119                 }
3120                 goto out;
3121         }
3122         nfs_access_add_cache(inode, &cache, cred);
3123 out_cached:
3124         cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
3125         if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
3126                 status = -EACCES;
3127 out:
3128         trace_nfs_access_exit(inode, mask, cache_mask, status);
3129         return status;
3130 }
3131
3132 static int nfs_open_permission_mask(int openflags)
3133 {
3134         int mask = 0;
3135
3136         if (openflags & __FMODE_EXEC) {
3137                 /* ONLY check exec rights */
3138                 mask = MAY_EXEC;
3139         } else {
3140                 if ((openflags & O_ACCMODE) != O_WRONLY)
3141                         mask |= MAY_READ;
3142                 if ((openflags & O_ACCMODE) != O_RDONLY)
3143                         mask |= MAY_WRITE;
3144         }
3145
3146         return mask;
3147 }
3148
3149 int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
3150 {
3151         return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
3152 }
3153 EXPORT_SYMBOL_GPL(nfs_may_open);
3154
3155 static int nfs_execute_ok(struct inode *inode, int mask)
3156 {
3157         struct nfs_server *server = NFS_SERVER(inode);
3158         int ret = 0;
3159
3160         if (S_ISDIR(inode->i_mode))
3161                 return 0;
3162         if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_MODE)) {
3163                 if (mask & MAY_NOT_BLOCK)
3164                         return -ECHILD;
3165                 ret = __nfs_revalidate_inode(server, inode);
3166         }
3167         if (ret == 0 && !execute_ok(inode))
3168                 ret = -EACCES;
3169         return ret;
3170 }
3171
3172 int nfs_permission(struct user_namespace *mnt_userns,
3173                    struct inode *inode,
3174                    int mask)
3175 {
3176         const struct cred *cred = current_cred();
3177         int res = 0;
3178
3179         nfs_inc_stats(inode, NFSIOS_VFSACCESS);
3180
3181         if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
3182                 goto out;
3183         /* Is this sys_access() ? */
3184         if (mask & (MAY_ACCESS | MAY_CHDIR))
3185                 goto force_lookup;
3186
3187         switch (inode->i_mode & S_IFMT) {
3188                 case S_IFLNK:
3189                         goto out;
3190                 case S_IFREG:
3191                         if ((mask & MAY_OPEN) &&
3192                            nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
3193                                 return 0;
3194                         break;
3195                 case S_IFDIR:
3196                         /*
3197                          * Optimize away all write operations, since the server
3198                          * will check permissions when we perform the op.
3199                          */
3200                         if ((mask & MAY_WRITE) && !(mask & MAY_READ))
3201                                 goto out;
3202         }
3203
3204 force_lookup:
3205         if (!NFS_PROTO(inode)->access)
3206                 goto out_notsup;
3207
3208         res = nfs_do_access(inode, cred, mask);
3209 out:
3210         if (!res && (mask & MAY_EXEC))
3211                 res = nfs_execute_ok(inode, mask);
3212
3213         dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
3214                 inode->i_sb->s_id, inode->i_ino, mask, res);
3215         return res;
3216 out_notsup:
3217         if (mask & MAY_NOT_BLOCK)
3218                 return -ECHILD;
3219
3220         res = nfs_revalidate_inode(inode, NFS_INO_INVALID_MODE |
3221                                                   NFS_INO_INVALID_OTHER);
3222         if (res == 0)
3223                 res = generic_permission(&init_user_ns, inode, mask);
3224         goto out;
3225 }
3226 EXPORT_SYMBOL_GPL(nfs_permission);