~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfs/dir.c

Version: ~ [ linux-5.6 ] ~ [ linux-5.5.13 ] ~ [ linux-5.4.28 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.113 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.174 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.217 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.217 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.82 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/fs/nfs/dir.c
  3  *
  4  *  Copyright (C) 1992  Rick Sladkey
  5  *
  6  *  nfs directory handling functions
  7  *
  8  * 10 Apr 1996  Added silly rename for unlink   --okir
  9  * 28 Sep 1996  Improved directory cache --okir
 10  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de 
 11  *              Re-implemented silly rename for unlink, newly implemented
 12  *              silly rename for nfs_rename() following the suggestions
 13  *              of Olaf Kirch (okir) found in this file.
 14  *              Following Linus comments on my original hack, this version
 15  *              depends only on the dcache stuff and doesn't touch the inode
 16  *              layer (iput() and friends).
 17  *  6 Jun 1999  Cache readdir lookups in the page cache. -DaveM
 18  */
 19 
 20 #include <linux/module.h>
 21 #include <linux/time.h>
 22 #include <linux/errno.h>
 23 #include <linux/stat.h>
 24 #include <linux/fcntl.h>
 25 #include <linux/string.h>
 26 #include <linux/kernel.h>
 27 #include <linux/slab.h>
 28 #include <linux/mm.h>
 29 #include <linux/sunrpc/clnt.h>
 30 #include <linux/nfs_fs.h>
 31 #include <linux/nfs_mount.h>
 32 #include <linux/pagemap.h>
 33 #include <linux/pagevec.h>
 34 #include <linux/namei.h>
 35 #include <linux/mount.h>
 36 #include <linux/swap.h>
 37 #include <linux/sched.h>
 38 #include <linux/kmemleak.h>
 39 #include <linux/xattr.h>
 40 
 41 #include "delegation.h"
 42 #include "iostat.h"
 43 #include "internal.h"
 44 #include "fscache.h"
 45 
 46 #include "nfstrace.h"
 47 
 48 /* #define NFS_DEBUG_VERBOSE 1 */
 49 
 50 static int nfs_opendir(struct inode *, struct file *);
 51 static int nfs_closedir(struct inode *, struct file *);
 52 static int nfs_readdir(struct file *, struct dir_context *);
 53 static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
 54 static loff_t nfs_llseek_dir(struct file *, loff_t, int);
 55 static void nfs_readdir_clear_array(struct page*);
 56 
 57 const struct file_operations nfs_dir_operations = {
 58         .llseek         = nfs_llseek_dir,
 59         .read           = generic_read_dir,
 60         .iterate        = nfs_readdir,
 61         .open           = nfs_opendir,
 62         .release        = nfs_closedir,
 63         .fsync          = nfs_fsync_dir,
 64 };
 65 
 66 const struct address_space_operations nfs_dir_aops = {
 67         .freepage = nfs_readdir_clear_array,
 68 };
 69 
 70 static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, struct rpc_cred *cred)
 71 {
 72         struct nfs_open_dir_context *ctx;
 73         ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
 74         if (ctx != NULL) {
 75                 ctx->duped = 0;
 76                 ctx->attr_gencount = NFS_I(dir)->attr_gencount;
 77                 ctx->dir_cookie = 0;
 78                 ctx->dup_cookie = 0;
 79                 ctx->cred = get_rpccred(cred);
 80                 return ctx;
 81         }
 82         return  ERR_PTR(-ENOMEM);
 83 }
 84 
 85 static void put_nfs_open_dir_context(struct nfs_open_dir_context *ctx)
 86 {
 87         put_rpccred(ctx->cred);
 88         kfree(ctx);
 89 }
 90 
 91 /*
 92  * Open file
 93  */
 94 static int
 95 nfs_opendir(struct inode *inode, struct file *filp)
 96 {
 97         int res = 0;
 98         struct nfs_open_dir_context *ctx;
 99         struct rpc_cred *cred;
100 
101         dfprintk(FILE, "NFS: open dir(%s/%s)\n",
102                         filp->f_path.dentry->d_parent->d_name.name,
103                         filp->f_path.dentry->d_name.name);
104 
105         nfs_inc_stats(inode, NFSIOS_VFSOPEN);
106 
107         cred = rpc_lookup_cred();
108         if (IS_ERR(cred))
109                 return PTR_ERR(cred);
110         ctx = alloc_nfs_open_dir_context(inode, cred);
111         if (IS_ERR(ctx)) {
112                 res = PTR_ERR(ctx);
113                 goto out;
114         }
115         filp->private_data = ctx;
116         if (filp->f_path.dentry == filp->f_path.mnt->mnt_root) {
117                 /* This is a mountpoint, so d_revalidate will never
118                  * have been called, so we need to refresh the
119                  * inode (for close-open consistency) ourselves.
120                  */
121                 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
122         }
123 out:
124         put_rpccred(cred);
125         return res;
126 }
127 
128 static int
129 nfs_closedir(struct inode *inode, struct file *filp)
130 {
131         put_nfs_open_dir_context(filp->private_data);
132         return 0;
133 }
134 
135 struct nfs_cache_array_entry {
136         u64 cookie;
137         u64 ino;
138         struct qstr string;
139         unsigned char d_type;
140 };
141 
142 struct nfs_cache_array {
143         int size;
144         int eof_index;
145         u64 last_cookie;
146         struct nfs_cache_array_entry array[0];
147 };
148 
149 typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, int);
150 typedef struct {
151         struct file     *file;
152         struct page     *page;
153         struct dir_context *ctx;
154         unsigned long   page_index;
155         u64             *dir_cookie;
156         u64             last_cookie;
157         loff_t          current_index;
158         decode_dirent_t decode;
159 
160         unsigned long   timestamp;
161         unsigned long   gencount;
162         unsigned int    cache_entry_index;
163         unsigned int    plus:1;
164         unsigned int    eof:1;
165 } nfs_readdir_descriptor_t;
166 
167 /*
168  * The caller is responsible for calling nfs_readdir_release_array(page)
169  */
170 static
171 struct nfs_cache_array *nfs_readdir_get_array(struct page *page)
172 {
173         void *ptr;
174         if (page == NULL)
175                 return ERR_PTR(-EIO);
176         ptr = kmap(page);
177         if (ptr == NULL)
178                 return ERR_PTR(-ENOMEM);
179         return ptr;
180 }
181 
182 static
183 void nfs_readdir_release_array(struct page *page)
184 {
185         kunmap(page);
186 }
187 
188 /*
189  * we are freeing strings created by nfs_add_to_readdir_array()
190  */
191 static
192 void nfs_readdir_clear_array(struct page *page)
193 {
194         struct nfs_cache_array *array;
195         int i;
196 
197         array = kmap_atomic(page);
198         for (i = 0; i < array->size; i++)
199                 kfree(array->array[i].string.name);
200         kunmap_atomic(array);
201 }
202 
203 /*
204  * the caller is responsible for freeing qstr.name
205  * when called by nfs_readdir_add_to_array, the strings will be freed in
206  * nfs_clear_readdir_array()
207  */
208 static
209 int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
210 {
211         string->len = len;
212         string->name = kmemdup(name, len, GFP_KERNEL);
213         if (string->name == NULL)
214                 return -ENOMEM;
215         /*
216          * Avoid a kmemleak false positive. The pointer to the name is stored
217          * in a page cache page which kmemleak does not scan.
218          */
219         kmemleak_not_leak(string->name);
220         string->hash = full_name_hash(name, len);
221         return 0;
222 }
223 
224 static
225 int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
226 {
227         struct nfs_cache_array *array = nfs_readdir_get_array(page);
228         struct nfs_cache_array_entry *cache_entry;
229         int ret;
230 
231         if (IS_ERR(array))
232                 return PTR_ERR(array);
233 
234         cache_entry = &array->array[array->size];
235 
236         /* Check that this entry lies within the page bounds */
237         ret = -ENOSPC;
238         if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
239                 goto out;
240 
241         cache_entry->cookie = entry->prev_cookie;
242         cache_entry->ino = entry->ino;
243         cache_entry->d_type = entry->d_type;
244         ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
245         if (ret)
246                 goto out;
247         array->last_cookie = entry->cookie;
248         array->size++;
249         if (entry->eof != 0)
250                 array->eof_index = array->size;
251 out:
252         nfs_readdir_release_array(page);
253         return ret;
254 }
255 
256 static
257 int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
258 {
259         loff_t diff = desc->ctx->pos - desc->current_index;
260         unsigned int index;
261 
262         if (diff < 0)
263                 goto out_eof;
264         if (diff >= array->size) {
265                 if (array->eof_index >= 0)
266                         goto out_eof;
267                 return -EAGAIN;
268         }
269 
270         index = (unsigned int)diff;
271         *desc->dir_cookie = array->array[index].cookie;
272         desc->cache_entry_index = index;
273         return 0;
274 out_eof:
275         desc->eof = 1;
276         return -EBADCOOKIE;
277 }
278 
279 static bool
280 nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
281 {
282         if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
283                 return false;
284         smp_rmb();
285         return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
286 }
287 
288 static
289 int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
290 {
291         int i;
292         loff_t new_pos;
293         int status = -EAGAIN;
294 
295         for (i = 0; i < array->size; i++) {
296                 if (array->array[i].cookie == *desc->dir_cookie) {
297                         struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
298                         struct nfs_open_dir_context *ctx = desc->file->private_data;
299 
300                         new_pos = desc->current_index + i;
301                         if (ctx->attr_gencount != nfsi->attr_gencount ||
302                             !nfs_readdir_inode_mapping_valid(nfsi)) {
303                                 ctx->duped = 0;
304                                 ctx->attr_gencount = nfsi->attr_gencount;
305                         } else if (new_pos < desc->ctx->pos) {
306                                 if (ctx->duped > 0
307                                     && ctx->dup_cookie == *desc->dir_cookie) {
308                                         if (printk_ratelimit()) {
309                                                 pr_notice("NFS: directory %s/%s contains a readdir loop."
310                                                                 "Please contact your server vendor.  "
311                                                                 "The file: %s has duplicate cookie %llu\n",
312                                                                 desc->file->f_dentry->d_parent->d_name.name,
313                                                                 desc->file->f_dentry->d_name.name,
314                                                                 array->array[i].string.name,
315                                                                 *desc->dir_cookie);
316                                         }
317                                         status = -ELOOP;
318                                         goto out;
319                                 }
320                                 ctx->dup_cookie = *desc->dir_cookie;
321                                 ctx->duped = -1;
322                         }
323                         desc->ctx->pos = new_pos;
324                         desc->cache_entry_index = i;
325                         return 0;
326                 }
327         }
328         if (array->eof_index >= 0) {
329                 status = -EBADCOOKIE;
330                 if (*desc->dir_cookie == array->last_cookie)
331                         desc->eof = 1;
332         }
333 out:
334         return status;
335 }
336 
337 static
338 int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
339 {
340         struct nfs_cache_array *array;
341         int status;
342 
343         array = nfs_readdir_get_array(desc->page);
344         if (IS_ERR(array)) {
345                 status = PTR_ERR(array);
346                 goto out;
347         }
348 
349         if (*desc->dir_cookie == 0)
350                 status = nfs_readdir_search_for_pos(array, desc);
351         else
352                 status = nfs_readdir_search_for_cookie(array, desc);
353 
354         if (status == -EAGAIN) {
355                 desc->last_cookie = array->last_cookie;
356                 desc->current_index += array->size;
357                 desc->page_index++;
358         }
359         nfs_readdir_release_array(desc->page);
360 out:
361         return status;
362 }
363 
364 /* Fill a page with xdr information before transferring to the cache page */
365 static
366 int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
367                         struct nfs_entry *entry, struct file *file, struct inode *inode)
368 {
369         struct nfs_open_dir_context *ctx = file->private_data;
370         struct rpc_cred *cred = ctx->cred;
371         unsigned long   timestamp, gencount;
372         int             error;
373 
374  again:
375         timestamp = jiffies;
376         gencount = nfs_inc_attr_generation_counter();
377         error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, entry->cookie, pages,
378                                           NFS_SERVER(inode)->dtsize, desc->plus);
379         if (error < 0) {
380                 /* We requested READDIRPLUS, but the server doesn't grok it */
381                 if (error == -ENOTSUPP && desc->plus) {
382                         NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
383                         clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
384                         desc->plus = 0;
385                         goto again;
386                 }
387                 goto error;
388         }
389         desc->timestamp = timestamp;
390         desc->gencount = gencount;
391 error:
392         return error;
393 }
394 
395 static int xdr_decode(nfs_readdir_descriptor_t *desc,
396                       struct nfs_entry *entry, struct xdr_stream *xdr)
397 {
398         int error;
399 
400         error = desc->decode(xdr, entry, desc->plus);
401         if (error)
402                 return error;
403         entry->fattr->time_start = desc->timestamp;
404         entry->fattr->gencount = desc->gencount;
405         return 0;
406 }
407 
408 static
409 int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
410 {
411         if (dentry->d_inode == NULL)
412                 goto different;
413         if (nfs_compare_fh(entry->fh, NFS_FH(dentry->d_inode)) != 0)
414                 goto different;
415         return 1;
416 different:
417         return 0;
418 }
419 
420 static
421 bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
422 {
423         if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
424                 return false;
425         if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
426                 return true;
427         if (ctx->pos == 0)
428                 return true;
429         return false;
430 }
431 
432 /*
433  * This function is called by the lookup code to request the use of
434  * readdirplus to accelerate any future lookups in the same
435  * directory.
436  */
437 static
438 void nfs_advise_use_readdirplus(struct inode *dir)
439 {
440         set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags);
441 }
442 
443 static
444 void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
445 {
446         struct qstr filename = QSTR_INIT(entry->name, entry->len);
447         struct dentry *dentry;
448         struct dentry *alias;
449         struct inode *dir = parent->d_inode;
450         struct inode *inode;
451         int status;
452 
453         if (filename.name[0] == '.') {
454                 if (filename.len == 1)
455                         return;
456                 if (filename.len == 2 && filename.name[1] == '.')
457                         return;
458         }
459         filename.hash = full_name_hash(filename.name, filename.len);
460 
461         dentry = d_lookup(parent, &filename);
462         if (dentry != NULL) {
463                 if (nfs_same_file(dentry, entry)) {
464                         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
465                         status = nfs_refresh_inode(dentry->d_inode, entry->fattr);
466                         if (!status)
467                                 nfs_setsecurity(dentry->d_inode, entry->fattr, entry->label);
468                         goto out;
469                 } else {
470                         if (d_invalidate(dentry) != 0)
471                                 goto out;
472                         dput(dentry);
473                 }
474         }
475 
476         dentry = d_alloc(parent, &filename);
477         if (dentry == NULL)
478                 return;
479 
480         inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
481         if (IS_ERR(inode))
482                 goto out;
483 
484         alias = d_materialise_unique(dentry, inode);
485         if (IS_ERR(alias))
486                 goto out;
487         else if (alias) {
488                 nfs_set_verifier(alias, nfs_save_change_attribute(dir));
489                 dput(alias);
490         } else
491                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
492 
493 out:
494         dput(dentry);
495 }
496 
497 /* Perform conversion from xdr to cache array */
498 static
499 int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
500                                 struct page **xdr_pages, struct page *page, unsigned int buflen)
501 {
502         struct xdr_stream stream;
503         struct xdr_buf buf;
504         struct page *scratch;
505         struct nfs_cache_array *array;
506         unsigned int count = 0;
507         int status;
508 
509         scratch = alloc_page(GFP_KERNEL);
510         if (scratch == NULL)
511                 return -ENOMEM;
512 
513         if (buflen == 0)
514                 goto out_nopages;
515 
516         xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
517         xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
518 
519         do {
520                 status = xdr_decode(desc, entry, &stream);
521                 if (status != 0) {
522                         if (status == -EAGAIN)
523                                 status = 0;
524                         break;
525                 }
526 
527                 count++;
528 
529                 if (desc->plus != 0)
530                         nfs_prime_dcache(desc->file->f_path.dentry, entry);
531 
532                 status = nfs_readdir_add_to_array(entry, page);
533                 if (status != 0)
534                         break;
535         } while (!entry->eof);
536 
537 out_nopages:
538         if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
539                 array = nfs_readdir_get_array(page);
540                 if (!IS_ERR(array)) {
541                         array->eof_index = array->size;
542                         status = 0;
543                         nfs_readdir_release_array(page);
544                 } else
545                         status = PTR_ERR(array);
546         }
547 
548         put_page(scratch);
549         return status;
550 }
551 
552 static
553 void nfs_readdir_free_pagearray(struct page **pages, unsigned int npages)
554 {
555         unsigned int i;
556         for (i = 0; i < npages; i++)
557                 put_page(pages[i]);
558 }
559 
560 static
561 void nfs_readdir_free_large_page(void *ptr, struct page **pages,
562                 unsigned int npages)
563 {
564         nfs_readdir_free_pagearray(pages, npages);
565 }
566 
567 /*
568  * nfs_readdir_large_page will allocate pages that must be freed with a call
569  * to nfs_readdir_free_large_page
570  */
571 static
572 int nfs_readdir_large_page(struct page **pages, unsigned int npages)
573 {
574         unsigned int i;
575 
576         for (i = 0; i < npages; i++) {
577                 struct page *page = alloc_page(GFP_KERNEL);
578                 if (page == NULL)
579                         goto out_freepages;
580                 pages[i] = page;
581         }
582         return 0;
583 
584 out_freepages:
585         nfs_readdir_free_pagearray(pages, i);
586         return -ENOMEM;
587 }
588 
589 static
590 int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
591 {
592         struct page *pages[NFS_MAX_READDIR_PAGES];
593         void *pages_ptr = NULL;
594         struct nfs_entry entry;
595         struct file     *file = desc->file;
596         struct nfs_cache_array *array;
597         int status = -ENOMEM;
598         unsigned int array_size = ARRAY_SIZE(pages);
599 
600         entry.prev_cookie = 0;
601         entry.cookie = desc->last_cookie;
602         entry.eof = 0;
603         entry.fh = nfs_alloc_fhandle();
604         entry.fattr = nfs_alloc_fattr();
605         entry.server = NFS_SERVER(inode);
606         if (entry.fh == NULL || entry.fattr == NULL)
607                 goto out;
608 
609         entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
610         if (IS_ERR(entry.label)) {
611                 status = PTR_ERR(entry.label);
612                 goto out;
613         }
614 
615         array = nfs_readdir_get_array(page);
616         if (IS_ERR(array)) {
617                 status = PTR_ERR(array);
618                 goto out_label_free;
619         }
620         memset(array, 0, sizeof(struct nfs_cache_array));
621         array->eof_index = -1;
622 
623         status = nfs_readdir_large_page(pages, array_size);
624         if (status < 0)
625                 goto out_release_array;
626         do {
627                 unsigned int pglen;
628                 status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
629 
630                 if (status < 0)
631                         break;
632                 pglen = status;
633                 status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
634                 if (status < 0) {
635                         if (status == -ENOSPC)
636                                 status = 0;
637                         break;
638                 }
639         } while (array->eof_index < 0);
640 
641         nfs_readdir_free_large_page(pages_ptr, pages, array_size);
642 out_release_array:
643         nfs_readdir_release_array(page);
644 out_label_free:
645         nfs4_label_free(entry.label);
646 out:
647         nfs_free_fattr(entry.fattr);
648         nfs_free_fhandle(entry.fh);
649         return status;
650 }
651 
652 /*
653  * Now we cache directories properly, by converting xdr information
654  * to an array that can be used for lookups later.  This results in
655  * fewer cache pages, since we can store more information on each page.
656  * We only need to convert from xdr once so future lookups are much simpler
657  */
658 static
659 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
660 {
661         struct inode    *inode = file_inode(desc->file);
662         int ret;
663 
664         ret = nfs_readdir_xdr_to_array(desc, page, inode);
665         if (ret < 0)
666                 goto error;
667         SetPageUptodate(page);
668 
669         if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
670                 /* Should never happen */
671                 nfs_zap_mapping(inode, inode->i_mapping);
672         }
673         unlock_page(page);
674         return 0;
675  error:
676         unlock_page(page);
677         return ret;
678 }
679 
680 static
681 void cache_page_release(nfs_readdir_descriptor_t *desc)
682 {
683         if (!desc->page->mapping)
684                 nfs_readdir_clear_array(desc->page);
685         page_cache_release(desc->page);
686         desc->page = NULL;
687 }
688 
689 static
690 struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
691 {
692         return read_cache_page(file_inode(desc->file)->i_mapping,
693                         desc->page_index, (filler_t *)nfs_readdir_filler, desc);
694 }
695 
696 /*
697  * Returns 0 if desc->dir_cookie was found on page desc->page_index
698  */
699 static
700 int find_cache_page(nfs_readdir_descriptor_t *desc)
701 {
702         int res;
703 
704         desc->page = get_cache_page(desc);
705         if (IS_ERR(desc->page))
706                 return PTR_ERR(desc->page);
707 
708         res = nfs_readdir_search_array(desc);
709         if (res != 0)
710                 cache_page_release(desc);
711         return res;
712 }
713 
714 /* Search for desc->dir_cookie from the beginning of the page cache */
715 static inline
716 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
717 {
718         int res;
719 
720         if (desc->page_index == 0) {
721                 desc->current_index = 0;
722                 desc->last_cookie = 0;
723         }
724         do {
725                 res = find_cache_page(desc);
726         } while (res == -EAGAIN);
727         return res;
728 }
729 
730 /*
731  * Once we've found the start of the dirent within a page: fill 'er up...
732  */
733 static 
734 int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
735 {
736         struct file     *file = desc->file;
737         int i = 0;
738         int res = 0;
739         struct nfs_cache_array *array = NULL;
740         struct nfs_open_dir_context *ctx = file->private_data;
741 
742         array = nfs_readdir_get_array(desc->page);
743         if (IS_ERR(array)) {
744                 res = PTR_ERR(array);
745                 goto out;
746         }
747 
748         for (i = desc->cache_entry_index; i < array->size; i++) {
749                 struct nfs_cache_array_entry *ent;
750 
751                 ent = &array->array[i];
752                 if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
753                     nfs_compat_user_ino64(ent->ino), ent->d_type)) {
754                         desc->eof = 1;
755                         break;
756                 }
757                 desc->ctx->pos++;
758                 if (i < (array->size-1))
759                         *desc->dir_cookie = array->array[i+1].cookie;
760                 else
761                         *desc->dir_cookie = array->last_cookie;
762                 if (ctx->duped != 0)
763                         ctx->duped = 1;
764         }
765         if (array->eof_index >= 0)
766                 desc->eof = 1;
767 
768         nfs_readdir_release_array(desc->page);
769 out:
770         cache_page_release(desc);
771         dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
772                         (unsigned long long)*desc->dir_cookie, res);
773         return res;
774 }
775 
776 /*
777  * If we cannot find a cookie in our cache, we suspect that this is
778  * because it points to a deleted file, so we ask the server to return
779  * whatever it thinks is the next entry. We then feed this to filldir.
780  * If all goes well, we should then be able to find our way round the
781  * cache on the next call to readdir_search_pagecache();
782  *
783  * NOTE: we cannot add the anonymous page to the pagecache because
784  *       the data it contains might not be page aligned. Besides,
785  *       we should already have a complete representation of the
786  *       directory in the page cache by the time we get here.
787  */
788 static inline
789 int uncached_readdir(nfs_readdir_descriptor_t *desc)
790 {
791         struct page     *page = NULL;
792         int             status;
793         struct inode *inode = file_inode(desc->file);
794         struct nfs_open_dir_context *ctx = desc->file->private_data;
795 
796         dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
797                         (unsigned long long)*desc->dir_cookie);
798 
799         page = alloc_page(GFP_HIGHUSER);
800         if (!page) {
801                 status = -ENOMEM;
802                 goto out;
803         }
804 
805         desc->page_index = 0;
806         desc->last_cookie = *desc->dir_cookie;
807         desc->page = page;
808         ctx->duped = 0;
809 
810         status = nfs_readdir_xdr_to_array(desc, page, inode);
811         if (status < 0)
812                 goto out_release;
813 
814         status = nfs_do_filldir(desc);
815 
816  out:
817         dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
818                         __func__, status);
819         return status;
820  out_release:
821         cache_page_release(desc);
822         goto out;
823 }
824 
825 /* The file offset position represents the dirent entry number.  A
826    last cookie cache takes care of the common case of reading the
827    whole directory.
828  */
829 static int nfs_readdir(struct file *file, struct dir_context *ctx)
830 {
831         struct dentry   *dentry = file->f_path.dentry;
832         struct inode    *inode = dentry->d_inode;
833         nfs_readdir_descriptor_t my_desc,
834                         *desc = &my_desc;
835         struct nfs_open_dir_context *dir_ctx = file->private_data;
836         int res = 0;
837 
838         dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
839                         dentry->d_parent->d_name.name, dentry->d_name.name,
840                         (long long)ctx->pos);
841         nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
842 
843         /*
844          * ctx->pos points to the dirent entry number.
845          * *desc->dir_cookie has the cookie for the next entry. We have
846          * to either find the entry with the appropriate number or
847          * revalidate the cookie.
848          */
849         memset(desc, 0, sizeof(*desc));
850 
851         desc->file = file;
852         desc->ctx = ctx;
853         desc->dir_cookie = &dir_ctx->dir_cookie;
854         desc->decode = NFS_PROTO(inode)->decode_dirent;
855         desc->plus = nfs_use_readdirplus(inode, ctx) ? 1 : 0;
856 
857         nfs_block_sillyrename(dentry);
858         if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
859                 res = nfs_revalidate_mapping(inode, file->f_mapping);
860         if (res < 0)
861                 goto out;
862 
863         do {
864                 res = readdir_search_pagecache(desc);
865 
866                 if (res == -EBADCOOKIE) {
867                         res = 0;
868                         /* This means either end of directory */
869                         if (*desc->dir_cookie && desc->eof == 0) {
870                                 /* Or that the server has 'lost' a cookie */
871                                 res = uncached_readdir(desc);
872                                 if (res == 0)
873                                         continue;
874                         }
875                         break;
876                 }
877                 if (res == -ETOOSMALL && desc->plus) {
878                         clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
879                         nfs_zap_caches(inode);
880                         desc->page_index = 0;
881                         desc->plus = 0;
882                         desc->eof = 0;
883                         continue;
884                 }
885                 if (res < 0)
886                         break;
887 
888                 res = nfs_do_filldir(desc);
889                 if (res < 0)
890                         break;
891         } while (!desc->eof);
892 out:
893         nfs_unblock_sillyrename(dentry);
894         if (res > 0)
895                 res = 0;
896         dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
897                         dentry->d_parent->d_name.name, dentry->d_name.name,
898                         res);
899         return res;
900 }
901 
902 static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
903 {
904         struct dentry *dentry = filp->f_path.dentry;
905         struct inode *inode = dentry->d_inode;
906         struct nfs_open_dir_context *dir_ctx = filp->private_data;
907 
908         dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
909                         dentry->d_parent->d_name.name,
910                         dentry->d_name.name,
911                         offset, whence);
912 
913         mutex_lock(&inode->i_mutex);
914         switch (whence) {
915                 case 1:
916                         offset += filp->f_pos;
917                 case 0:
918                         if (offset >= 0)
919                                 break;
920                 default:
921                         offset = -EINVAL;
922                         goto out;
923         }
924         if (offset != filp->f_pos) {
925                 filp->f_pos = offset;
926                 dir_ctx->dir_cookie = 0;
927                 dir_ctx->duped = 0;
928         }
929 out:
930         mutex_unlock(&inode->i_mutex);
931         return offset;
932 }
933 
934 /*
935  * All directory operations under NFS are synchronous, so fsync()
936  * is a dummy operation.
937  */
938 static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
939                          int datasync)
940 {
941         struct dentry *dentry = filp->f_path.dentry;
942         struct inode *inode = dentry->d_inode;
943 
944         dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
945                         dentry->d_parent->d_name.name, dentry->d_name.name,
946                         datasync);
947 
948         mutex_lock(&inode->i_mutex);
949         nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
950         mutex_unlock(&inode->i_mutex);
951         return 0;
952 }
953 
954 /**
955  * nfs_force_lookup_revalidate - Mark the directory as having changed
956  * @dir - pointer to directory inode
957  *
958  * This forces the revalidation code in nfs_lookup_revalidate() to do a
959  * full lookup on all child dentries of 'dir' whenever a change occurs
960  * on the server that might have invalidated our dcache.
961  *
962  * The caller should be holding dir->i_lock
963  */
964 void nfs_force_lookup_revalidate(struct inode *dir)
965 {
966         NFS_I(dir)->cache_change_attribute++;
967 }
968 EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
969 
970 /*
971  * A check for whether or not the parent directory has changed.
972  * In the case it has, we assume that the dentries are untrustworthy
973  * and may need to be looked up again.
974  */
975 static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
976 {
977         if (IS_ROOT(dentry))
978                 return 1;
979         if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
980                 return 0;
981         if (!nfs_verify_change_attribute(dir, dentry->d_time))
982                 return 0;
983         /* Revalidate nfsi->cache_change_attribute before we declare a match */
984         if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
985                 return 0;
986         if (!nfs_verify_change_attribute(dir, dentry->d_time))
987                 return 0;
988         return 1;
989 }
990 
991 /*
992  * Use intent information to check whether or not we're going to do
993  * an O_EXCL create using this path component.
994  */
995 static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
996 {
997         if (NFS_PROTO(dir)->version == 2)
998                 return 0;
999         return flags & LOOKUP_EXCL;
1000 }
1001 
1002 /*
1003  * Inode and filehandle revalidation for lookups.
1004  *
1005  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
1006  * or if the intent information indicates that we're about to open this
1007  * particular file and the "nocto" mount flag is not set.
1008  *
1009  */
1010 static
1011 int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
1012 {
1013         struct nfs_server *server = NFS_SERVER(inode);
1014         int ret;
1015 
1016         if (IS_AUTOMOUNT(inode))
1017                 return 0;
1018         /* VFS wants an on-the-wire revalidation */
1019         if (flags & LOOKUP_REVAL)
1020                 goto out_force;
1021         /* This is an open(2) */
1022         if ((flags & LOOKUP_OPEN) && !(server->flags & NFS_MOUNT_NOCTO) &&
1023             (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
1024                 goto out_force;
1025 out:
1026         return (inode->i_nlink == 0) ? -ENOENT : 0;
1027 out_force:
1028         ret = __nfs_revalidate_inode(server, inode);
1029         if (ret != 0)
1030                 return ret;
1031         goto out;
1032 }
1033 
1034 /*
1035  * We judge how long we want to trust negative
1036  * dentries by looking at the parent inode mtime.
1037  *
1038  * If parent mtime has changed, we revalidate, else we wait for a
1039  * period corresponding to the parent's attribute cache timeout value.
1040  */
1041 static inline
1042 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1043                        unsigned int flags)
1044 {
1045         /* Don't revalidate a negative dentry if we're creating a new file */
1046         if (flags & LOOKUP_CREATE)
1047                 return 0;
1048         if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
1049                 return 1;
1050         return !nfs_check_verifier(dir, dentry);
1051 }
1052 
1053 /*
1054  * This is called every time the dcache has a lookup hit,
1055  * and we should check whether we can really trust that
1056  * lookup.
1057  *
1058  * NOTE! The hit can be a negative hit too, don't assume
1059  * we have an inode!
1060  *
1061  * If the parent directory is seen to have changed, we throw out the
1062  * cached dentry and do a new lookup.
1063  */
1064 static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1065 {
1066         struct inode *dir;
1067         struct inode *inode;
1068         struct dentry *parent;
1069         struct nfs_fh *fhandle = NULL;
1070         struct nfs_fattr *fattr = NULL;
1071         struct nfs4_label *label = NULL;
1072         int error;
1073 
1074         if (flags & LOOKUP_RCU)
1075                 return -ECHILD;
1076 
1077         parent = dget_parent(dentry);
1078         dir = parent->d_inode;
1079         nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
1080         inode = dentry->d_inode;
1081 
1082         if (!inode) {
1083                 if (nfs_neg_need_reval(dir, dentry, flags))
1084                         goto out_bad;
1085                 goto out_valid_noent;
1086         }
1087 
1088         if (is_bad_inode(inode)) {
1089                 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
1090                                 __func__, dentry->d_parent->d_name.name,
1091                                 dentry->d_name.name);
1092                 goto out_bad;
1093         }
1094 
1095         if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1096                 goto out_set_verifier;
1097 
1098         /* Force a full look up iff the parent directory has changed */
1099         if (!nfs_is_exclusive_create(dir, flags) && nfs_check_verifier(dir, dentry)) {
1100                 if (nfs_lookup_verify_inode(inode, flags))
1101                         goto out_zap_parent;
1102                 goto out_valid;
1103         }
1104 
1105         if (NFS_STALE(inode))
1106                 goto out_bad;
1107 
1108         error = -ENOMEM;
1109         fhandle = nfs_alloc_fhandle();
1110         fattr = nfs_alloc_fattr();
1111         if (fhandle == NULL || fattr == NULL)
1112                 goto out_error;
1113 
1114         label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
1115         if (IS_ERR(label))
1116                 goto out_error;
1117 
1118         trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
1119         error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
1120         trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
1121         if (error)
1122                 goto out_bad;
1123         if (nfs_compare_fh(NFS_FH(inode), fhandle))
1124                 goto out_bad;
1125         if ((error = nfs_refresh_inode(inode, fattr)) != 0)
1126                 goto out_bad;
1127 
1128         nfs_setsecurity(inode, fattr, label);
1129 
1130         nfs_free_fattr(fattr);
1131         nfs_free_fhandle(fhandle);
1132         nfs4_label_free(label);
1133 
1134 out_set_verifier:
1135         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1136  out_valid:
1137         /* Success: notify readdir to use READDIRPLUS */
1138         nfs_advise_use_readdirplus(dir);
1139  out_valid_noent:
1140         dput(parent);
1141         dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
1142                         __func__, dentry->d_parent->d_name.name,
1143                         dentry->d_name.name);
1144         return 1;
1145 out_zap_parent:
1146         nfs_zap_caches(dir);
1147  out_bad:
1148         nfs_free_fattr(fattr);
1149         nfs_free_fhandle(fhandle);
1150         nfs4_label_free(label);
1151         nfs_mark_for_revalidate(dir);
1152         if (inode && S_ISDIR(inode->i_mode)) {
1153                 /* Purge readdir caches. */
1154                 nfs_zap_caches(inode);
1155                 /*
1156                  * We can't d_drop the root of a disconnected tree:
1157                  * its d_hash is on the s_anon list and d_drop() would hide
1158                  * it from shrink_dcache_for_unmount(), leading to busy
1159                  * inodes on unmount and further oopses.
1160                  */
1161                 if (IS_ROOT(dentry))
1162                         goto out_valid;
1163         }
1164         /* If we have submounts, don't unhash ! */
1165         if (check_submounts_and_drop(dentry) != 0)
1166                 goto out_valid;
1167 
1168         dput(parent);
1169         dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
1170                         __func__, dentry->d_parent->d_name.name,
1171                         dentry->d_name.name);
1172         return 0;
1173 out_error:
1174         nfs_free_fattr(fattr);
1175         nfs_free_fhandle(fhandle);
1176         nfs4_label_free(label);
1177         dput(parent);
1178         dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
1179                         __func__, dentry->d_parent->d_name.name,
1180                         dentry->d_name.name, error);
1181         return error;
1182 }
1183 
1184 /*
1185  * A weaker form of d_revalidate for revalidating just the dentry->d_inode
1186  * when we don't really care about the dentry name. This is called when a
1187  * pathwalk ends on a dentry that was not found via a normal lookup in the
1188  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1189  *
1190  * In this situation, we just want to verify that the inode itself is OK
1191  * since the dentry might have changed on the server.
1192  */
1193 static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1194 {
1195         int error;
1196         struct inode *inode = dentry->d_inode;
1197 
1198         /*
1199          * I believe we can only get a negative dentry here in the case of a
1200          * procfs-style symlink. Just assume it's correct for now, but we may
1201          * eventually need to do something more here.
1202          */
1203         if (!inode) {
1204                 dfprintk(LOOKUPCACHE, "%s: %s/%s has negative inode\n",
1205                                 __func__, dentry->d_parent->d_name.name,
1206                                 dentry->d_name.name);
1207                 return 1;
1208         }
1209 
1210         if (is_bad_inode(inode)) {
1211                 dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
1212                                 __func__, dentry->d_parent->d_name.name,
1213                                 dentry->d_name.name);
1214                 return 0;
1215         }
1216 
1217         error = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1218         dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1219                         __func__, inode->i_ino, error ? "invalid" : "valid");
1220         return !error;
1221 }
1222 
1223 /*
1224  * This is called from dput() when d_count is going to 0.
1225  */
1226 static int nfs_dentry_delete(const struct dentry *dentry)
1227 {
1228         dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
1229                 dentry->d_parent->d_name.name, dentry->d_name.name,
1230                 dentry->d_flags);
1231 
1232         /* Unhash any dentry with a stale inode */
1233         if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
1234                 return 1;
1235 
1236         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1237                 /* Unhash it, so that ->d_iput() would be called */
1238                 return 1;
1239         }
1240         if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
1241                 /* Unhash it, so that ancestors of killed async unlink
1242                  * files will be cleaned up during umount */
1243                 return 1;
1244         }
1245         return 0;
1246 
1247 }
1248 
1249 /* Ensure that we revalidate inode->i_nlink */
1250 static void nfs_drop_nlink(struct inode *inode)
1251 {
1252         spin_lock(&inode->i_lock);
1253         /* drop the inode if we're reasonably sure this is the last link */
1254         if (inode->i_nlink == 1)
1255                 clear_nlink(inode);
1256         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
1257         spin_unlock(&inode->i_lock);
1258 }
1259 
1260 /*
1261  * Called when the dentry loses inode.
1262  * We use it to clean up silly-renamed files.
1263  */
1264 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
1265 {
1266         if (S_ISDIR(inode->i_mode))
1267                 /* drop any readdir cache as it could easily be old */
1268                 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
1269 
1270         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1271                 nfs_complete_unlink(dentry, inode);
1272                 nfs_drop_nlink(inode);
1273         }
1274         iput(inode);
1275 }
1276 
1277 static void nfs_d_release(struct dentry *dentry)
1278 {
1279         /* free cached devname value, if it survived that far */
1280         if (unlikely(dentry->d_fsdata)) {
1281                 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1282                         WARN_ON(1);
1283                 else
1284                         kfree(dentry->d_fsdata);
1285         }
1286 }
1287 
1288 const struct dentry_operations nfs_dentry_operations = {
1289         .d_revalidate   = nfs_lookup_revalidate,
1290         .d_weak_revalidate      = nfs_weak_revalidate,
1291         .d_delete       = nfs_dentry_delete,
1292         .d_iput         = nfs_dentry_iput,
1293         .d_automount    = nfs_d_automount,
1294         .d_release      = nfs_d_release,
1295 };
1296 EXPORT_SYMBOL_GPL(nfs_dentry_operations);
1297 
1298 struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1299 {
1300         struct dentry *res;
1301         struct dentry *parent;
1302         struct inode *inode = NULL;
1303         struct nfs_fh *fhandle = NULL;
1304         struct nfs_fattr *fattr = NULL;
1305         struct nfs4_label *label = NULL;
1306         int error;
1307 
1308         dfprintk(VFS, "NFS: lookup(%s/%s)\n",
1309                 dentry->d_parent->d_name.name, dentry->d_name.name);
1310         nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
1311 
1312         res = ERR_PTR(-ENAMETOOLONG);
1313         if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1314                 goto out;
1315 
1316         /*
1317          * If we're doing an exclusive create, optimize away the lookup
1318          * but don't hash the dentry.
1319          */
1320         if (nfs_is_exclusive_create(dir, flags)) {
1321                 d_instantiate(dentry, NULL);
1322                 res = NULL;
1323                 goto out;
1324         }
1325 
1326         res = ERR_PTR(-ENOMEM);
1327         fhandle = nfs_alloc_fhandle();
1328         fattr = nfs_alloc_fattr();
1329         if (fhandle == NULL || fattr == NULL)
1330                 goto out;
1331 
1332         label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
1333         if (IS_ERR(label))
1334                 goto out;
1335 
1336         parent = dentry->d_parent;
1337         /* Protect against concurrent sillydeletes */
1338         trace_nfs_lookup_enter(dir, dentry, flags);
1339         nfs_block_sillyrename(parent);
1340         error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
1341         if (error == -ENOENT)
1342                 goto no_entry;
1343         if (error < 0) {
1344                 res = ERR_PTR(error);
1345                 goto out_unblock_sillyrename;
1346         }
1347         inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1348         res = ERR_CAST(inode);
1349         if (IS_ERR(res))
1350                 goto out_unblock_sillyrename;
1351 
1352         /* Success: notify readdir to use READDIRPLUS */
1353         nfs_advise_use_readdirplus(dir);
1354 
1355 no_entry:
1356         res = d_materialise_unique(dentry, inode);
1357         if (res != NULL) {
1358                 if (IS_ERR(res))
1359                         goto out_unblock_sillyrename;
1360                 dentry = res;
1361         }
1362         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1363 out_unblock_sillyrename:
1364         nfs_unblock_sillyrename(parent);
1365         trace_nfs_lookup_exit(dir, dentry, flags, error);
1366         nfs4_label_free(label);
1367 out:
1368         nfs_free_fattr(fattr);
1369         nfs_free_fhandle(fhandle);
1370         return res;
1371 }
1372 EXPORT_SYMBOL_GPL(nfs_lookup);
1373 
1374 #if IS_ENABLED(CONFIG_NFS_V4)
1375 static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
1376 
1377 const struct dentry_operations nfs4_dentry_operations = {
1378         .d_revalidate   = nfs4_lookup_revalidate,
1379         .d_delete       = nfs_dentry_delete,
1380         .d_iput         = nfs_dentry_iput,
1381         .d_automount    = nfs_d_automount,
1382         .d_release      = nfs_d_release,
1383 };
1384 EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
1385 
1386 static fmode_t flags_to_mode(int flags)
1387 {
1388         fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1389         if ((flags & O_ACCMODE) != O_WRONLY)
1390                 res |= FMODE_READ;
1391         if ((flags & O_ACCMODE) != O_RDONLY)
1392                 res |= FMODE_WRITE;
1393         return res;
1394 }
1395 
1396 static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags)
1397 {
1398         return alloc_nfs_open_context(dentry, flags_to_mode(open_flags));
1399 }
1400 
1401 static int do_open(struct inode *inode, struct file *filp)
1402 {
1403         nfs_fscache_set_inode_cookie(inode, filp);
1404         return 0;
1405 }
1406 
1407 static int nfs_finish_open(struct nfs_open_context *ctx,
1408                            struct dentry *dentry,
1409                            struct file *file, unsigned open_flags,
1410                            int *opened)
1411 {
1412         int err;
1413 
1414         if ((open_flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
1415                 *opened |= FILE_CREATED;
1416 
1417         err = finish_open(file, dentry, do_open, opened);
1418         if (err)
1419                 goto out;
1420         nfs_file_set_open_context(file, ctx);
1421 
1422 out:
1423         return err;
1424 }
1425 
1426 int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
1427                     struct file *file, unsigned open_flags,
1428                     umode_t mode, int *opened)
1429 {
1430         struct nfs_open_context *ctx;
1431         struct dentry *res;
1432         struct iattr attr = { .ia_valid = ATTR_OPEN };
1433         struct inode *inode;
1434         unsigned int lookup_flags = 0;
1435         int err;
1436 
1437         /* Expect a negative dentry */
1438         BUG_ON(dentry->d_inode);
1439 
1440         dfprintk(VFS, "NFS: atomic_open(%s/%ld), %s\n",
1441                         dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1442 
1443         err = nfs_check_flags(open_flags);
1444         if (err)
1445                 return err;
1446 
1447         /* NFS only supports OPEN on regular files */
1448         if ((open_flags & O_DIRECTORY)) {
1449                 if (!d_unhashed(dentry)) {
1450                         /*
1451                          * Hashed negative dentry with O_DIRECTORY: dentry was
1452                          * revalidated and is fine, no need to perform lookup
1453                          * again
1454                          */
1455                         return -ENOENT;
1456                 }
1457                 lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
1458                 goto no_open;
1459         }
1460 
1461         if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1462                 return -ENAMETOOLONG;
1463 
1464         if (open_flags & O_CREAT) {
1465                 attr.ia_valid |= ATTR_MODE;
1466                 attr.ia_mode = mode & ~current_umask();
1467         }
1468         if (open_flags & O_TRUNC) {
1469                 attr.ia_valid |= ATTR_SIZE;
1470                 attr.ia_size = 0;
1471         }
1472 
1473         ctx = create_nfs_open_context(dentry, open_flags);
1474         err = PTR_ERR(ctx);
1475         if (IS_ERR(ctx))
1476                 goto out;
1477 
1478         trace_nfs_atomic_open_enter(dir, ctx, open_flags);
1479         nfs_block_sillyrename(dentry->d_parent);
1480         inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, opened);
1481         nfs_unblock_sillyrename(dentry->d_parent);
1482         if (IS_ERR(inode)) {
1483                 err = PTR_ERR(inode);
1484                 trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
1485                 put_nfs_open_context(ctx);
1486                 d_drop(dentry);
1487                 switch (err) {
1488                 case -ENOENT:
1489                         d_add(dentry, NULL);
1490                         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1491                         break;
1492                 case -EISDIR:
1493                 case -ENOTDIR:
1494                         goto no_open;
1495                 case -ELOOP:
1496                         if (!(open_flags & O_NOFOLLOW))
1497                                 goto no_open;
1498                         break;
1499                         /* case -EINVAL: */
1500                 default:
1501                         break;
1502                 }
1503                 goto out;
1504         }
1505 
1506         err = nfs_finish_open(ctx, ctx->dentry, file, open_flags, opened);
1507         trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
1508         put_nfs_open_context(ctx);
1509 out:
1510         return err;
1511 
1512 no_open:
1513         res = nfs_lookup(dir, dentry, lookup_flags);
1514         err = PTR_ERR(res);
1515         if (IS_ERR(res))
1516                 goto out;
1517 
1518         return finish_no_open(file, res);
1519 }
1520 EXPORT_SYMBOL_GPL(nfs_atomic_open);
1521 
1522 static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1523 {
1524         struct dentry *parent = NULL;
1525         struct inode *inode;
1526         struct inode *dir;
1527         int ret = 0;
1528 
1529         if (flags & LOOKUP_RCU)
1530                 return -ECHILD;
1531 
1532         if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1533                 goto no_open;
1534         if (d_mountpoint(dentry))
1535                 goto no_open;
1536         if (NFS_SB(dentry->d_sb)->caps & NFS_CAP_ATOMIC_OPEN_V1)
1537                 goto no_open;
1538 
1539         inode = dentry->d_inode;
1540         parent = dget_parent(dentry);
1541         dir = parent->d_inode;
1542 
1543         /* We can't create new files in nfs_open_revalidate(), so we
1544          * optimize away revalidation of negative dentries.
1545          */
1546         if (inode == NULL) {
1547                 if (!nfs_neg_need_reval(dir, dentry, flags))
1548                         ret = 1;
1549                 goto out;
1550         }
1551 
1552         /* NFS only supports OPEN on regular files */
1553         if (!S_ISREG(inode->i_mode))
1554                 goto no_open_dput;
1555         /* We cannot do exclusive creation on a positive dentry */
1556         if (flags & LOOKUP_EXCL)
1557                 goto no_open_dput;
1558 
1559         /* Let f_op->open() actually open (and revalidate) the file */
1560         ret = 1;
1561 
1562 out:
1563         dput(parent);
1564         return ret;
1565 
1566 no_open_dput:
1567         dput(parent);
1568 no_open:
1569         return nfs_lookup_revalidate(dentry, flags);
1570 }
1571 
1572 #endif /* CONFIG_NFSV4 */
1573 
1574 /*
1575  * Code common to create, mkdir, and mknod.
1576  */
1577 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1578                                 struct nfs_fattr *fattr,
1579                                 struct nfs4_label *label)
1580 {
1581         struct dentry *parent = dget_parent(dentry);
1582         struct inode *dir = parent->d_inode;
1583         struct inode *inode;
1584         int error = -EACCES;
1585 
1586         d_drop(dentry);
1587 
1588         /* We may have been initialized further down */
1589         if (dentry->d_inode)
1590                 goto out;
1591         if (fhandle->size == 0) {
1592                 error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
1593                 if (error)
1594                         goto out_error;
1595         }
1596         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1597         if (!(fattr->valid & NFS_ATTR_FATTR)) {
1598                 struct nfs_server *server = NFS_SB(dentry->d_sb);
1599                 error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr, NULL);
1600                 if (error < 0)
1601                         goto out_error;
1602         }
1603         inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1604         error = PTR_ERR(inode);
1605         if (IS_ERR(inode))
1606                 goto out_error;
1607         d_add(dentry, inode);
1608 out:
1609         dput(parent);
1610         return 0;
1611 out_error:
1612         nfs_mark_for_revalidate(dir);
1613         dput(parent);
1614         return error;
1615 }
1616 EXPORT_SYMBOL_GPL(nfs_instantiate);
1617 
1618 /*
1619  * Following a failed create operation, we drop the dentry rather
1620  * than retain a negative dentry. This avoids a problem in the event
1621  * that the operation succeeded on the server, but an error in the
1622  * reply path made it appear to have failed.
1623  */
1624 int nfs_create(struct inode *dir, struct dentry *dentry,
1625                 umode_t mode, bool excl)
1626 {
1627         struct iattr attr;
1628         int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
1629         int error;
1630 
1631         dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1632                         dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1633 
1634         attr.ia_mode = mode;
1635         attr.ia_valid = ATTR_MODE;
1636 
1637         trace_nfs_create_enter(dir, dentry, open_flags);
1638         error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
1639         trace_nfs_create_exit(dir, dentry, open_flags, error);
1640         if (error != 0)
1641                 goto out_err;
1642         return 0;
1643 out_err:
1644         d_drop(dentry);
1645         return error;
1646 }
1647 EXPORT_SYMBOL_GPL(nfs_create);
1648 
1649 /*
1650  * See comments for nfs_proc_create regarding failed operations.
1651  */
1652 int
1653 nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
1654 {
1655         struct iattr attr;
1656         int status;
1657 
1658         dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1659                         dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1660 
1661         if (!new_valid_dev(rdev))
1662                 return -EINVAL;
1663 
1664         attr.ia_mode = mode;
1665         attr.ia_valid = ATTR_MODE;
1666 
1667         trace_nfs_mknod_enter(dir, dentry);
1668         status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1669         trace_nfs_mknod_exit(dir, dentry, status);
1670         if (status != 0)
1671                 goto out_err;
1672         return 0;
1673 out_err:
1674         d_drop(dentry);
1675         return status;
1676 }
1677 EXPORT_SYMBOL_GPL(nfs_mknod);
1678 
1679 /*
1680  * See comments for nfs_proc_create regarding failed operations.
1681  */
1682 int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1683 {
1684         struct iattr attr;
1685         int error;
1686 
1687         dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1688                         dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1689 
1690         attr.ia_valid = ATTR_MODE;
1691         attr.ia_mode = mode | S_IFDIR;
1692 
1693         trace_nfs_mkdir_enter(dir, dentry);
1694         error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1695         trace_nfs_mkdir_exit(dir, dentry, error);
1696         if (error != 0)
1697                 goto out_err;
1698         return 0;
1699 out_err:
1700         d_drop(dentry);
1701         return error;
1702 }
1703 EXPORT_SYMBOL_GPL(nfs_mkdir);
1704 
1705 static void nfs_dentry_handle_enoent(struct dentry *dentry)
1706 {
1707         if (dentry->d_inode != NULL && !d_unhashed(dentry))
1708                 d_delete(dentry);
1709 }
1710 
1711 int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1712 {
1713         int error;
1714 
1715         dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1716                         dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1717 
1718         trace_nfs_rmdir_enter(dir, dentry);
1719         if (dentry->d_inode) {
1720                 nfs_wait_on_sillyrename(dentry);
1721                 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1722                 /* Ensure the VFS deletes this inode */
1723                 switch (error) {
1724                 case 0:
1725                         clear_nlink(dentry->d_inode);
1726                         break;
1727                 case -ENOENT:
1728                         nfs_dentry_handle_enoent(dentry);
1729                 }
1730         } else
1731                 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1732         trace_nfs_rmdir_exit(dir, dentry, error);
1733 
1734         return error;
1735 }
1736 EXPORT_SYMBOL_GPL(nfs_rmdir);
1737 
1738 /*
1739  * Remove a file after making sure there are no pending writes,
1740  * and after checking that the file has only one user. 
1741  *
1742  * We invalidate the attribute cache and free the inode prior to the operation
1743  * to avoid possible races if the server reuses the inode.
1744  */
1745 static int nfs_safe_remove(struct dentry *dentry)
1746 {
1747         struct inode *dir = dentry->d_parent->d_inode;
1748         struct inode *inode = dentry->d_inode;
1749         int error = -EBUSY;
1750                 
1751         dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1752                 dentry->d_parent->d_name.name, dentry->d_name.name);
1753 
1754         /* If the dentry was sillyrenamed, we simply call d_delete() */
1755         if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1756                 error = 0;
1757                 goto out;
1758         }
1759 
1760         trace_nfs_remove_enter(dir, dentry);
1761         if (inode != NULL) {
1762                 NFS_PROTO(inode)->return_delegation(inode);
1763                 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1764                 if (error == 0)
1765                         nfs_drop_nlink(inode);
1766         } else
1767                 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1768         if (error == -ENOENT)
1769                 nfs_dentry_handle_enoent(dentry);
1770         trace_nfs_remove_exit(dir, dentry, error);
1771 out:
1772         return error;
1773 }
1774 
1775 /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
1776  *  belongs to an active ".nfs..." file and we return -EBUSY.
1777  *
1778  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
1779  */
1780 int nfs_unlink(struct inode *dir, struct dentry *dentry)
1781 {
1782         int error;
1783         int need_rehash = 0;
1784 
1785         dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1786                 dir->i_ino, dentry->d_name.name);
1787 
1788         trace_nfs_unlink_enter(dir, dentry);
1789         spin_lock(&dentry->d_lock);
1790         if (d_count(dentry) > 1) {
1791                 spin_unlock(&dentry->d_lock);
1792                 /* Start asynchronous writeout of the inode */
1793                 write_inode_now(dentry->d_inode, 0);
1794                 error = nfs_sillyrename(dir, dentry);
1795                 goto out;
1796         }
1797         if (!d_unhashed(dentry)) {
1798                 __d_drop(dentry);
1799                 need_rehash = 1;
1800         }
1801         spin_unlock(&dentry->d_lock);
1802         error = nfs_safe_remove(dentry);
1803         if (!error || error == -ENOENT) {
1804                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1805         } else if (need_rehash)
1806                 d_rehash(dentry);
1807 out:
1808         trace_nfs_unlink_exit(dir, dentry, error);
1809         return error;
1810 }
1811 EXPORT_SYMBOL_GPL(nfs_unlink);
1812 
1813 /*
1814  * To create a symbolic link, most file systems instantiate a new inode,
1815  * add a page to it containing the path, then write it out to the disk
1816  * using prepare_write/commit_write.
1817  *
1818  * Unfortunately the NFS client can't create the in-core inode first
1819  * because it needs a file handle to create an in-core inode (see
1820  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1821  * symlink request has completed on the server.
1822  *
1823  * So instead we allocate a raw page, copy the symname into it, then do
1824  * the SYMLINK request with the page as the buffer.  If it succeeds, we
1825  * now have a new file handle and can instantiate an in-core NFS inode
1826  * and move the raw page into its mapping.
1827  */
1828 int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1829 {
1830         struct page *page;
1831         char *kaddr;
1832         struct iattr attr;
1833         unsigned int pathlen = strlen(symname);
1834         int error;
1835 
1836         dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1837                 dir->i_ino, dentry->d_name.name, symname);
1838 
1839         if (pathlen > PAGE_SIZE)
1840                 return -ENAMETOOLONG;
1841 
1842         attr.ia_mode = S_IFLNK | S_IRWXUGO;
1843         attr.ia_valid = ATTR_MODE;
1844 
1845         page = alloc_page(GFP_HIGHUSER);
1846         if (!page)
1847                 return -ENOMEM;
1848 
1849         kaddr = kmap_atomic(page);
1850         memcpy(kaddr, symname, pathlen);
1851         if (pathlen < PAGE_SIZE)
1852                 memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
1853         kunmap_atomic(kaddr);
1854 
1855         trace_nfs_symlink_enter(dir, dentry);
1856         error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
1857         trace_nfs_symlink_exit(dir, dentry, error);
1858         if (error != 0) {
1859                 dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
1860                         dir->i_sb->s_id, dir->i_ino,
1861                         dentry->d_name.name, symname, error);
1862                 d_drop(dentry);
1863                 __free_page(page);
1864                 return error;
1865         }
1866 
1867         /*
1868          * No big deal if we can't add this page to the page cache here.
1869          * READLINK will get the missing page from the server if needed.
1870          */
1871         if (!add_to_page_cache_lru(page, dentry->d_inode->i_mapping, 0,
1872                                                         GFP_KERNEL)) {
1873                 SetPageUptodate(page);
1874                 unlock_page(page);
1875                 /*
1876                  * add_to_page_cache_lru() grabs an extra page refcount.
1877                  * Drop it here to avoid leaking this page later.
1878                  */
1879                 page_cache_release(page);
1880         } else
1881                 __free_page(page);
1882 
1883         return 0;
1884 }
1885 EXPORT_SYMBOL_GPL(nfs_symlink);
1886 
1887 int
1888 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1889 {
1890         struct inode *inode = old_dentry->d_inode;
1891         int error;
1892 
1893         dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1894                 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1895                 dentry->d_parent->d_name.name, dentry->d_name.name);
1896 
1897         trace_nfs_link_enter(inode, dir, dentry);
1898         NFS_PROTO(inode)->return_delegation(inode);
1899 
1900         d_drop(dentry);
1901         error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1902         if (error == 0) {
1903                 ihold(inode);
1904                 d_add(dentry, inode);
1905         }
1906         trace_nfs_link_exit(inode, dir, dentry, error);
1907         return error;
1908 }
1909 EXPORT_SYMBOL_GPL(nfs_link);
1910 
1911 /*
1912  * RENAME
1913  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1914  * different file handle for the same inode after a rename (e.g. when
1915  * moving to a different directory). A fail-safe method to do so would
1916  * be to look up old_dir/old_name, create a link to new_dir/new_name and
1917  * rename the old file using the sillyrename stuff. This way, the original
1918  * file in old_dir will go away when the last process iput()s the inode.
1919  *
1920  * FIXED.
1921  * 
1922  * It actually works quite well. One needs to have the possibility for
1923  * at least one ".nfs..." file in each directory the file ever gets
1924  * moved or linked to which happens automagically with the new
1925  * implementation that only depends on the dcache stuff instead of
1926  * using the inode layer
1927  *
1928  * Unfortunately, things are a little more complicated than indicated
1929  * above. For a cross-directory move, we want to make sure we can get
1930  * rid of the old inode after the operation.  This means there must be
1931  * no pending writes (if it's a file), and the use count must be 1.
1932  * If these conditions are met, we can drop the dentries before doing
1933  * the rename.
1934  */
1935 int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1936                       struct inode *new_dir, struct dentry *new_dentry)
1937 {
1938         struct inode *old_inode = old_dentry->d_inode;
1939         struct inode *new_inode = new_dentry->d_inode;
1940         struct dentry *dentry = NULL, *rehash = NULL;
1941         int error = -EBUSY;
1942 
1943         dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1944                  old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1945                  new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1946                  d_count(new_dentry));
1947 
1948         trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
1949         /*
1950          * For non-directories, check whether the target is busy and if so,
1951          * make a copy of the dentry and then do a silly-rename. If the
1952          * silly-rename succeeds, the copied dentry is hashed and becomes
1953          * the new target.
1954          */
1955         if (new_inode && !S_ISDIR(new_inode->i_mode)) {
1956                 /*
1957                  * To prevent any new references to the target during the
1958                  * rename, we unhash the dentry in advance.
1959                  */
1960                 if (!d_unhashed(new_dentry)) {
1961                         d_drop(new_dentry);
1962                         rehash = new_dentry;
1963                 }
1964 
1965                 if (d_count(new_dentry) > 2) {
1966                         int err;
1967 
1968                         /* copy the target dentry's name */
1969                         dentry = d_alloc(new_dentry->d_parent,
1970                                          &new_dentry->d_name);
1971                         if (!dentry)
1972                                 goto out;
1973 
1974                         /* silly-rename the existing target ... */
1975                         err = nfs_sillyrename(new_dir, new_dentry);
1976                         if (err)
1977                                 goto out;
1978 
1979                         new_dentry = dentry;
1980                         rehash = NULL;
1981                         new_inode = NULL;
1982                 }
1983         }
1984 
1985         NFS_PROTO(old_inode)->return_delegation(old_inode);
1986         if (new_inode != NULL)
1987                 NFS_PROTO(new_inode)->return_delegation(new_inode);
1988 
1989         error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1990                                            new_dir, &new_dentry->d_name);
1991         nfs_mark_for_revalidate(old_inode);
1992 out:
1993         if (rehash)
1994                 d_rehash(rehash);
1995         trace_nfs_rename_exit(old_dir, old_dentry,
1996                         new_dir, new_dentry, error);
1997         if (!error) {
1998                 if (new_inode != NULL)
1999                         nfs_drop_nlink(new_inode);
2000                 d_move(old_dentry, new_dentry);
2001                 nfs_set_verifier(new_dentry,
2002                                         nfs_save_change_attribute(new_dir));
2003         } else if (error == -ENOENT)
2004                 nfs_dentry_handle_enoent(old_dentry);
2005 
2006         /* new dentry created? */
2007         if (dentry)
2008                 dput(dentry);
2009         return error;
2010 }
2011 EXPORT_SYMBOL_GPL(nfs_rename);
2012 
2013 static DEFINE_SPINLOCK(nfs_access_lru_lock);
2014 static LIST_HEAD(nfs_access_lru_list);
2015 static atomic_long_t nfs_access_nr_entries;
2016 
2017 static void nfs_access_free_entry(struct nfs_access_entry *entry)
2018 {
2019         put_rpccred(entry->cred);
2020         kfree(entry);
2021         smp_mb__before_atomic_dec();
2022         atomic_long_dec(&nfs_access_nr_entries);
2023         smp_mb__after_atomic_dec();
2024 }
2025 
2026 static void nfs_access_free_list(struct list_head *head)
2027 {
2028         struct nfs_access_entry *cache;
2029 
2030         while (!list_empty(head)) {
2031                 cache = list_entry(head->next, struct nfs_access_entry, lru);
2032                 list_del(&cache->lru);
2033                 nfs_access_free_entry(cache);
2034         }
2035 }
2036 
2037 unsigned long
2038 nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
2039 {
2040         LIST_HEAD(head);
2041         struct nfs_inode *nfsi, *next;
2042         struct nfs_access_entry *cache;
2043         int nr_to_scan = sc->nr_to_scan;
2044         gfp_t gfp_mask = sc->gfp_mask;
2045         long freed = 0;
2046 
2047         if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
2048                 return SHRINK_STOP;
2049 
2050         spin_lock(&nfs_access_lru_lock);
2051         list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2052                 struct inode *inode;
2053 
2054                 if (nr_to_scan-- == 0)
2055                         break;
2056                 inode = &nfsi->vfs_inode;
2057                 spin_lock(&inode->i_lock);
2058                 if (list_empty(&nfsi->access_cache_entry_lru))
2059                         goto remove_lru_entry;
2060                 cache = list_entry(nfsi->access_cache_entry_lru.next,
2061                                 struct nfs_access_entry, lru);
2062                 list_move(&cache->lru, &head);
2063                 rb_erase(&cache->rb_node, &nfsi->access_cache);
2064                 freed++;
2065                 if (!list_empty(&nfsi->access_cache_entry_lru))
2066                         list_move_tail(&nfsi->access_cache_inode_lru,
2067                                         &nfs_access_lru_list);
2068                 else {
2069 remove_lru_entry:
2070                         list_del_init(&nfsi->access_cache_inode_lru);
2071                         smp_mb__before_clear_bit();
2072                         clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
2073                         smp_mb__after_clear_bit();
2074                 }
2075                 spin_unlock(&inode->i_lock);
2076         }
2077         spin_unlock(&nfs_access_lru_lock);
2078         nfs_access_free_list(&head);
2079         return freed;
2080 }
2081 
2082 unsigned long
2083 nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
2084 {
2085         return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2086 }
2087 
2088 static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
2089 {
2090         struct rb_root *root_node = &nfsi->access_cache;
2091         struct rb_node *n;
2092         struct nfs_access_entry *entry;
2093 
2094         /* Unhook entries from the cache */
2095         while ((n = rb_first(root_node)) != NULL) {
2096                 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2097                 rb_erase(n, root_node);
2098                 list_move(&entry->lru, head);
2099         }
2100         nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
2101 }
2102 
2103 void nfs_access_zap_cache(struct inode *inode)
2104 {
2105         LIST_HEAD(head);
2106 
2107         if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
2108                 return;
2109         /* Remove from global LRU init */
2110         spin_lock(&nfs_access_lru_lock);
2111         if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2112                 list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2113 
2114         spin_lock(&inode->i_lock);
2115         __nfs_access_zap_cache(NFS_I(inode), &head);
2116         spin_unlock(&inode->i_lock);
2117         spin_unlock(&nfs_access_lru_lock);
2118         nfs_access_free_list(&head);
2119 }
2120 EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
2121 
2122 static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
2123 {
2124         struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
2125         struct nfs_access_entry *entry;
2126 
2127         while (n != NULL) {
2128                 entry = rb_entry(n, struct nfs_access_entry, rb_node);
2129 
2130                 if (cred < entry->cred)
2131                         n = n->rb_left;
2132                 else if (cred > entry->cred)
2133                         n = n->rb_right;
2134                 else
2135                         return entry;
2136         }
2137         return NULL;
2138 }
2139 
2140 static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
2141 {
2142         struct nfs_inode *nfsi = NFS_I(inode);
2143         struct nfs_access_entry *cache;
2144         int err = -ENOENT;
2145 
2146         spin_lock(&inode->i_lock);
2147         if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2148                 goto out_zap;
2149         cache = nfs_access_search_rbtree(inode, cred);
2150         if (cache == NULL)
2151                 goto out;
2152         if (!nfs_have_delegated_attributes(inode) &&
2153             !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
2154                 goto out_stale;
2155         res->jiffies = cache->jiffies;
2156         res->cred = cache->cred;
2157         res->mask = cache->mask;
2158         list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
2159         err = 0;
2160 out:
2161         spin_unlock(&inode->i_lock);
2162         return err;
2163 out_stale:
2164         rb_erase(&cache->rb_node, &nfsi->access_cache);
2165         list_del(&cache->lru);
2166         spin_unlock(&inode->i_lock);
2167         nfs_access_free_entry(cache);
2168         return -ENOENT;
2169 out_zap:
2170         spin_unlock(&inode->i_lock);
2171         nfs_access_zap_cache(inode);
2172         return -ENOENT;
2173 }
2174 
2175 static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
2176 {
2177         struct nfs_inode *nfsi = NFS_I(inode);
2178         struct rb_root *root_node = &nfsi->access_cache;
2179         struct rb_node **p = &root_node->rb_node;
2180         struct rb_node *parent = NULL;
2181         struct nfs_access_entry *entry;
2182 
2183         spin_lock(&inode->i_lock);
2184         while (*p != NULL) {
2185                 parent = *p;
2186                 entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2187 
2188                 if (set->cred < entry->cred)
2189                         p = &parent->rb_left;
2190                 else if (set->cred > entry->cred)
2191                         p = &parent->rb_right;
2192                 else
2193                         goto found;
2194         }
2195         rb_link_node(&set->rb_node, parent, p);
2196         rb_insert_color(&set->rb_node, root_node);
2197         list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2198         spin_unlock(&inode->i_lock);
2199         return;
2200 found:
2201         rb_replace_node(parent, &set->rb_node, root_node);
2202         list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2203         list_del(&entry->lru);
2204         spin_unlock(&inode->i_lock);
2205         nfs_access_free_entry(entry);
2206 }
2207 
2208 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
2209 {
2210         struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
2211         if (cache == NULL)
2212                 return;
2213         RB_CLEAR_NODE(&cache->rb_node);
2214         cache->jiffies = set->jiffies;
2215         cache->cred = get_rpccred(set->cred);
2216         cache->mask = set->mask;
2217 
2218         nfs_access_add_rbtree(inode, cache);
2219 
2220         /* Update accounting */
2221         smp_mb__before_atomic_inc();
2222         atomic_long_inc(&nfs_access_nr_entries);
2223         smp_mb__after_atomic_inc();
2224 
2225         /* Add inode to global LRU list */
2226         if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2227                 spin_lock(&nfs_access_lru_lock);
2228                 if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2229                         list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
2230                                         &nfs_access_lru_list);
2231                 spin_unlock(&nfs_access_lru_lock);
2232         }
2233 }
2234 EXPORT_SYMBOL_GPL(nfs_access_add_cache);
2235 
2236 void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
2237 {
2238         entry->mask = 0;
2239         if (access_result & NFS4_ACCESS_READ)
2240                 entry->mask |= MAY_READ;
2241         if (access_result &
2242             (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2243                 entry->mask |= MAY_WRITE;
2244         if (access_result & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2245                 entry->mask |= MAY_EXEC;
2246 }
2247 EXPORT_SYMBOL_GPL(nfs_access_set_mask);
2248 
2249 static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
2250 {
2251         struct nfs_access_entry cache;
2252         int status;
2253 
2254         trace_nfs_access_enter(inode);
2255 
2256         status = nfs_access_get_cached(inode, cred, &cache);
2257         if (status == 0)
2258                 goto out_cached;
2259 
2260         /* Be clever: ask server to check for all possible rights */
2261         cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
2262         cache.cred = cred;
2263         cache.jiffies = jiffies;
2264         status = NFS_PROTO(inode)->access(inode, &cache);
2265         if (status != 0) {
2266                 if (status == -ESTALE) {
2267                         nfs_zap_caches(inode);
2268                         if (!S_ISDIR(inode->i_mode))
2269                                 set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2270                 }
2271                 goto out;
2272         }
2273         nfs_access_add_cache(inode, &cache);
2274 out_cached:
2275         if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2276                 status = -EACCES;
2277 out:
2278         trace_nfs_access_exit(inode, status);
2279         return status;
2280 }
2281 
2282 static int nfs_open_permission_mask(int openflags)
2283 {
2284         int mask = 0;
2285 
2286         if (openflags & __FMODE_EXEC) {
2287                 /* ONLY check exec rights */
2288                 mask = MAY_EXEC;
2289         } else {
2290                 if ((openflags & O_ACCMODE) != O_WRONLY)
2291                         mask |= MAY_READ;
2292                 if ((openflags & O_ACCMODE) != O_RDONLY)
2293                         mask |= MAY_WRITE;
2294         }
2295 
2296         return mask;
2297 }
2298 
2299 int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
2300 {
2301         return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2302 }
2303 EXPORT_SYMBOL_GPL(nfs_may_open);
2304 
2305 int nfs_permission(struct inode *inode, int mask)
2306 {
2307         struct rpc_cred *cred;
2308         int res = 0;
2309 
2310         if (mask & MAY_NOT_BLOCK)
2311                 return -ECHILD;
2312 
2313         nfs_inc_stats(inode, NFSIOS_VFSACCESS);
2314 
2315         if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2316                 goto out;
2317         /* Is this sys_access() ? */
2318         if (mask & (MAY_ACCESS | MAY_CHDIR))
2319                 goto force_lookup;
2320 
2321         switch (inode->i_mode & S_IFMT) {
2322                 case S_IFLNK:
2323                         goto out;
2324                 case S_IFREG:
2325                         break;
2326                 case S_IFDIR:
2327                         /*
2328                          * Optimize away all write operations, since the server
2329                          * will check permissions when we perform the op.
2330                          */
2331                         if ((mask & MAY_WRITE) && !(mask & MAY_READ))
2332                                 goto out;
2333         }
2334 
2335 force_lookup:
2336         if (!NFS_PROTO(inode)->access)
2337                 goto out_notsup;
2338 
2339         cred = rpc_lookup_cred();
2340         if (!IS_ERR(cred)) {
2341                 res = nfs_do_access(inode, cred, mask);
2342                 put_rpccred(cred);
2343         } else
2344                 res = PTR_ERR(cred);
2345 out:
2346         if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
2347                 res = -EACCES;
2348 
2349         dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
2350                 inode->i_sb->s_id, inode->i_ino, mask, res);
2351         return res;
2352 out_notsup:
2353         res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
2354         if (res == 0)
2355                 res = generic_permission(inode, mask);
2356         goto out;
2357 }
2358 EXPORT_SYMBOL_GPL(nfs_permission);
2359 
2360 /*
2361  * Local variables:
2362  *  version-control: t
2363  *  kept-new-versions: 5
2364  * End:
2365  */
2366 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp