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