xref: /linux/fs/nfs/dir.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
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/smp_lock.h>
33 #include <linux/namei.h>
34 
35 #include "nfs4_fs.h"
36 #include "delegation.h"
37 #include "iostat.h"
38 
39 #define NFS_PARANOIA 1
40 /* #define NFS_DEBUG_VERBOSE 1 */
41 
42 static int nfs_opendir(struct inode *, struct file *);
43 static int nfs_readdir(struct file *, void *, filldir_t);
44 static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
45 static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
46 static int nfs_mkdir(struct inode *, struct dentry *, int);
47 static int nfs_rmdir(struct inode *, struct dentry *);
48 static int nfs_unlink(struct inode *, struct dentry *);
49 static int nfs_symlink(struct inode *, struct dentry *, const char *);
50 static int nfs_link(struct dentry *, struct inode *, struct dentry *);
51 static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
52 static int nfs_rename(struct inode *, struct dentry *,
53 		      struct inode *, struct dentry *);
54 static int nfs_fsync_dir(struct file *, struct dentry *, int);
55 static loff_t nfs_llseek_dir(struct file *, loff_t, int);
56 
57 const struct file_operations nfs_dir_operations = {
58 	.llseek		= nfs_llseek_dir,
59 	.read		= generic_read_dir,
60 	.readdir	= nfs_readdir,
61 	.open		= nfs_opendir,
62 	.release	= nfs_release,
63 	.fsync		= nfs_fsync_dir,
64 };
65 
66 struct inode_operations nfs_dir_inode_operations = {
67 	.create		= nfs_create,
68 	.lookup		= nfs_lookup,
69 	.link		= nfs_link,
70 	.unlink		= nfs_unlink,
71 	.symlink	= nfs_symlink,
72 	.mkdir		= nfs_mkdir,
73 	.rmdir		= nfs_rmdir,
74 	.mknod		= nfs_mknod,
75 	.rename		= nfs_rename,
76 	.permission	= nfs_permission,
77 	.getattr	= nfs_getattr,
78 	.setattr	= nfs_setattr,
79 };
80 
81 #ifdef CONFIG_NFS_V3
82 struct inode_operations nfs3_dir_inode_operations = {
83 	.create		= nfs_create,
84 	.lookup		= nfs_lookup,
85 	.link		= nfs_link,
86 	.unlink		= nfs_unlink,
87 	.symlink	= nfs_symlink,
88 	.mkdir		= nfs_mkdir,
89 	.rmdir		= nfs_rmdir,
90 	.mknod		= nfs_mknod,
91 	.rename		= nfs_rename,
92 	.permission	= nfs_permission,
93 	.getattr	= nfs_getattr,
94 	.setattr	= nfs_setattr,
95 	.listxattr	= nfs3_listxattr,
96 	.getxattr	= nfs3_getxattr,
97 	.setxattr	= nfs3_setxattr,
98 	.removexattr	= nfs3_removexattr,
99 };
100 #endif  /* CONFIG_NFS_V3 */
101 
102 #ifdef CONFIG_NFS_V4
103 
104 static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
105 struct inode_operations nfs4_dir_inode_operations = {
106 	.create		= nfs_create,
107 	.lookup		= nfs_atomic_lookup,
108 	.link		= nfs_link,
109 	.unlink		= nfs_unlink,
110 	.symlink	= nfs_symlink,
111 	.mkdir		= nfs_mkdir,
112 	.rmdir		= nfs_rmdir,
113 	.mknod		= nfs_mknod,
114 	.rename		= nfs_rename,
115 	.permission	= nfs_permission,
116 	.getattr	= nfs_getattr,
117 	.setattr	= nfs_setattr,
118 	.getxattr       = nfs4_getxattr,
119 	.setxattr       = nfs4_setxattr,
120 	.listxattr      = nfs4_listxattr,
121 };
122 
123 #endif /* CONFIG_NFS_V4 */
124 
125 /*
126  * Open file
127  */
128 static int
129 nfs_opendir(struct inode *inode, struct file *filp)
130 {
131 	int res;
132 
133 	dfprintk(VFS, "NFS: opendir(%s/%ld)\n",
134 			inode->i_sb->s_id, inode->i_ino);
135 
136 	lock_kernel();
137 	/* Call generic open code in order to cache credentials */
138 	res = nfs_open(inode, filp);
139 	unlock_kernel();
140 	return res;
141 }
142 
143 typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
144 typedef struct {
145 	struct file	*file;
146 	struct page	*page;
147 	unsigned long	page_index;
148 	u32		*ptr;
149 	u64		*dir_cookie;
150 	loff_t		current_index;
151 	struct nfs_entry *entry;
152 	decode_dirent_t	decode;
153 	int		plus;
154 	int		error;
155 } nfs_readdir_descriptor_t;
156 
157 /* Now we cache directories properly, by stuffing the dirent
158  * data directly in the page cache.
159  *
160  * Inode invalidation due to refresh etc. takes care of
161  * _everything_, no sloppy entry flushing logic, no extraneous
162  * copying, network direct to page cache, the way it was meant
163  * to be.
164  *
165  * NOTE: Dirent information verification is done always by the
166  *	 page-in of the RPC reply, nowhere else, this simplies
167  *	 things substantially.
168  */
169 static
170 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
171 {
172 	struct file	*file = desc->file;
173 	struct inode	*inode = file->f_dentry->d_inode;
174 	struct rpc_cred	*cred = nfs_file_cred(file);
175 	unsigned long	timestamp;
176 	int		error;
177 
178 	dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
179 			__FUNCTION__, (long long)desc->entry->cookie,
180 			page->index);
181 
182  again:
183 	timestamp = jiffies;
184 	error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page,
185 					  NFS_SERVER(inode)->dtsize, desc->plus);
186 	if (error < 0) {
187 		/* We requested READDIRPLUS, but the server doesn't grok it */
188 		if (error == -ENOTSUPP && desc->plus) {
189 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
190 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
191 			desc->plus = 0;
192 			goto again;
193 		}
194 		goto error;
195 	}
196 	SetPageUptodate(page);
197 	spin_lock(&inode->i_lock);
198 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
199 	spin_unlock(&inode->i_lock);
200 	/* Ensure consistent page alignment of the data.
201 	 * Note: assumes we have exclusive access to this mapping either
202 	 *	 through inode->i_mutex or some other mechanism.
203 	 */
204 	if (page->index == 0)
205 		invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1);
206 	unlock_page(page);
207 	return 0;
208  error:
209 	SetPageError(page);
210 	unlock_page(page);
211 	nfs_zap_caches(inode);
212 	desc->error = error;
213 	return -EIO;
214 }
215 
216 static inline
217 int dir_decode(nfs_readdir_descriptor_t *desc)
218 {
219 	u32	*p = desc->ptr;
220 	p = desc->decode(p, desc->entry, desc->plus);
221 	if (IS_ERR(p))
222 		return PTR_ERR(p);
223 	desc->ptr = p;
224 	return 0;
225 }
226 
227 static inline
228 void dir_page_release(nfs_readdir_descriptor_t *desc)
229 {
230 	kunmap(desc->page);
231 	page_cache_release(desc->page);
232 	desc->page = NULL;
233 	desc->ptr = NULL;
234 }
235 
236 /*
237  * Given a pointer to a buffer that has already been filled by a call
238  * to readdir, find the next entry with cookie '*desc->dir_cookie'.
239  *
240  * If the end of the buffer has been reached, return -EAGAIN, if not,
241  * return the offset within the buffer of the next entry to be
242  * read.
243  */
244 static inline
245 int find_dirent(nfs_readdir_descriptor_t *desc)
246 {
247 	struct nfs_entry *entry = desc->entry;
248 	int		loop_count = 0,
249 			status;
250 
251 	while((status = dir_decode(desc)) == 0) {
252 		dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
253 				__FUNCTION__, (unsigned long long)entry->cookie);
254 		if (entry->prev_cookie == *desc->dir_cookie)
255 			break;
256 		if (loop_count++ > 200) {
257 			loop_count = 0;
258 			schedule();
259 		}
260 	}
261 	return status;
262 }
263 
264 /*
265  * Given a pointer to a buffer that has already been filled by a call
266  * to readdir, find the entry at offset 'desc->file->f_pos'.
267  *
268  * If the end of the buffer has been reached, return -EAGAIN, if not,
269  * return the offset within the buffer of the next entry to be
270  * read.
271  */
272 static inline
273 int find_dirent_index(nfs_readdir_descriptor_t *desc)
274 {
275 	struct nfs_entry *entry = desc->entry;
276 	int		loop_count = 0,
277 			status;
278 
279 	for(;;) {
280 		status = dir_decode(desc);
281 		if (status)
282 			break;
283 
284 		dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
285 				(unsigned long long)entry->cookie, desc->current_index);
286 
287 		if (desc->file->f_pos == desc->current_index) {
288 			*desc->dir_cookie = entry->cookie;
289 			break;
290 		}
291 		desc->current_index++;
292 		if (loop_count++ > 200) {
293 			loop_count = 0;
294 			schedule();
295 		}
296 	}
297 	return status;
298 }
299 
300 /*
301  * Find the given page, and call find_dirent() or find_dirent_index in
302  * order to try to return the next entry.
303  */
304 static inline
305 int find_dirent_page(nfs_readdir_descriptor_t *desc)
306 {
307 	struct inode	*inode = desc->file->f_dentry->d_inode;
308 	struct page	*page;
309 	int		status;
310 
311 	dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
312 			__FUNCTION__, desc->page_index,
313 			(long long) *desc->dir_cookie);
314 
315 	page = read_cache_page(inode->i_mapping, desc->page_index,
316 			       (filler_t *)nfs_readdir_filler, desc);
317 	if (IS_ERR(page)) {
318 		status = PTR_ERR(page);
319 		goto out;
320 	}
321 	if (!PageUptodate(page))
322 		goto read_error;
323 
324 	/* NOTE: Someone else may have changed the READDIRPLUS flag */
325 	desc->page = page;
326 	desc->ptr = kmap(page);		/* matching kunmap in nfs_do_filldir */
327 	if (*desc->dir_cookie != 0)
328 		status = find_dirent(desc);
329 	else
330 		status = find_dirent_index(desc);
331 	if (status < 0)
332 		dir_page_release(desc);
333  out:
334 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, status);
335 	return status;
336  read_error:
337 	page_cache_release(page);
338 	return -EIO;
339 }
340 
341 /*
342  * Recurse through the page cache pages, and return a
343  * filled nfs_entry structure of the next directory entry if possible.
344  *
345  * The target for the search is '*desc->dir_cookie' if non-0,
346  * 'desc->file->f_pos' otherwise
347  */
348 static inline
349 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
350 {
351 	int		loop_count = 0;
352 	int		res;
353 
354 	/* Always search-by-index from the beginning of the cache */
355 	if (*desc->dir_cookie == 0) {
356 		dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
357 				(long long)desc->file->f_pos);
358 		desc->page_index = 0;
359 		desc->entry->cookie = desc->entry->prev_cookie = 0;
360 		desc->entry->eof = 0;
361 		desc->current_index = 0;
362 	} else
363 		dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
364 				(unsigned long long)*desc->dir_cookie);
365 
366 	for (;;) {
367 		res = find_dirent_page(desc);
368 		if (res != -EAGAIN)
369 			break;
370 		/* Align to beginning of next page */
371 		desc->page_index ++;
372 		if (loop_count++ > 200) {
373 			loop_count = 0;
374 			schedule();
375 		}
376 	}
377 
378 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, res);
379 	return res;
380 }
381 
382 static inline unsigned int dt_type(struct inode *inode)
383 {
384 	return (inode->i_mode >> 12) & 15;
385 }
386 
387 static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
388 
389 /*
390  * Once we've found the start of the dirent within a page: fill 'er up...
391  */
392 static
393 int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
394 		   filldir_t filldir)
395 {
396 	struct file	*file = desc->file;
397 	struct nfs_entry *entry = desc->entry;
398 	struct dentry	*dentry = NULL;
399 	unsigned long	fileid;
400 	int		loop_count = 0,
401 			res;
402 
403 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
404 			(unsigned long long)entry->cookie);
405 
406 	for(;;) {
407 		unsigned d_type = DT_UNKNOWN;
408 		/* Note: entry->prev_cookie contains the cookie for
409 		 *	 retrieving the current dirent on the server */
410 		fileid = nfs_fileid_to_ino_t(entry->ino);
411 
412 		/* Get a dentry if we have one */
413 		if (dentry != NULL)
414 			dput(dentry);
415 		dentry = nfs_readdir_lookup(desc);
416 
417 		/* Use readdirplus info */
418 		if (dentry != NULL && dentry->d_inode != NULL) {
419 			d_type = dt_type(dentry->d_inode);
420 			fileid = dentry->d_inode->i_ino;
421 		}
422 
423 		res = filldir(dirent, entry->name, entry->len,
424 			      file->f_pos, fileid, d_type);
425 		if (res < 0)
426 			break;
427 		file->f_pos++;
428 		*desc->dir_cookie = entry->cookie;
429 		if (dir_decode(desc) != 0) {
430 			desc->page_index ++;
431 			break;
432 		}
433 		if (loop_count++ > 200) {
434 			loop_count = 0;
435 			schedule();
436 		}
437 	}
438 	dir_page_release(desc);
439 	if (dentry != NULL)
440 		dput(dentry);
441 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
442 			(unsigned long long)*desc->dir_cookie, res);
443 	return res;
444 }
445 
446 /*
447  * If we cannot find a cookie in our cache, we suspect that this is
448  * because it points to a deleted file, so we ask the server to return
449  * whatever it thinks is the next entry. We then feed this to filldir.
450  * If all goes well, we should then be able to find our way round the
451  * cache on the next call to readdir_search_pagecache();
452  *
453  * NOTE: we cannot add the anonymous page to the pagecache because
454  *	 the data it contains might not be page aligned. Besides,
455  *	 we should already have a complete representation of the
456  *	 directory in the page cache by the time we get here.
457  */
458 static inline
459 int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
460 		     filldir_t filldir)
461 {
462 	struct file	*file = desc->file;
463 	struct inode	*inode = file->f_dentry->d_inode;
464 	struct rpc_cred	*cred = nfs_file_cred(file);
465 	struct page	*page = NULL;
466 	int		status;
467 
468 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
469 			(unsigned long long)*desc->dir_cookie);
470 
471 	page = alloc_page(GFP_HIGHUSER);
472 	if (!page) {
473 		status = -ENOMEM;
474 		goto out;
475 	}
476 	desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, *desc->dir_cookie,
477 						page,
478 						NFS_SERVER(inode)->dtsize,
479 						desc->plus);
480 	spin_lock(&inode->i_lock);
481 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
482 	spin_unlock(&inode->i_lock);
483 	desc->page = page;
484 	desc->ptr = kmap(page);		/* matching kunmap in nfs_do_filldir */
485 	if (desc->error >= 0) {
486 		if ((status = dir_decode(desc)) == 0)
487 			desc->entry->prev_cookie = *desc->dir_cookie;
488 	} else
489 		status = -EIO;
490 	if (status < 0)
491 		goto out_release;
492 
493 	status = nfs_do_filldir(desc, dirent, filldir);
494 
495 	/* Reset read descriptor so it searches the page cache from
496 	 * the start upon the next call to readdir_search_pagecache() */
497 	desc->page_index = 0;
498 	desc->entry->cookie = desc->entry->prev_cookie = 0;
499 	desc->entry->eof = 0;
500  out:
501 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
502 			__FUNCTION__, status);
503 	return status;
504  out_release:
505 	dir_page_release(desc);
506 	goto out;
507 }
508 
509 /* The file offset position represents the dirent entry number.  A
510    last cookie cache takes care of the common case of reading the
511    whole directory.
512  */
513 static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
514 {
515 	struct dentry	*dentry = filp->f_dentry;
516 	struct inode	*inode = dentry->d_inode;
517 	nfs_readdir_descriptor_t my_desc,
518 			*desc = &my_desc;
519 	struct nfs_entry my_entry;
520 	struct nfs_fh	 fh;
521 	struct nfs_fattr fattr;
522 	long		res;
523 
524 	dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
525 			dentry->d_parent->d_name.name, dentry->d_name.name,
526 			(long long)filp->f_pos);
527 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
528 
529 	lock_kernel();
530 
531 	res = nfs_revalidate_mapping(inode, filp->f_mapping);
532 	if (res < 0) {
533 		unlock_kernel();
534 		return res;
535 	}
536 
537 	/*
538 	 * filp->f_pos points to the dirent entry number.
539 	 * *desc->dir_cookie has the cookie for the next entry. We have
540 	 * to either find the entry with the appropriate number or
541 	 * revalidate the cookie.
542 	 */
543 	memset(desc, 0, sizeof(*desc));
544 
545 	desc->file = filp;
546 	desc->dir_cookie = &((struct nfs_open_context *)filp->private_data)->dir_cookie;
547 	desc->decode = NFS_PROTO(inode)->decode_dirent;
548 	desc->plus = NFS_USE_READDIRPLUS(inode);
549 
550 	my_entry.cookie = my_entry.prev_cookie = 0;
551 	my_entry.eof = 0;
552 	my_entry.fh = &fh;
553 	my_entry.fattr = &fattr;
554 	nfs_fattr_init(&fattr);
555 	desc->entry = &my_entry;
556 
557 	while(!desc->entry->eof) {
558 		res = readdir_search_pagecache(desc);
559 
560 		if (res == -EBADCOOKIE) {
561 			/* This means either end of directory */
562 			if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
563 				/* Or that the server has 'lost' a cookie */
564 				res = uncached_readdir(desc, dirent, filldir);
565 				if (res >= 0)
566 					continue;
567 			}
568 			res = 0;
569 			break;
570 		}
571 		if (res == -ETOOSMALL && desc->plus) {
572 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
573 			nfs_zap_caches(inode);
574 			desc->plus = 0;
575 			desc->entry->eof = 0;
576 			continue;
577 		}
578 		if (res < 0)
579 			break;
580 
581 		res = nfs_do_filldir(desc, dirent, filldir);
582 		if (res < 0) {
583 			res = 0;
584 			break;
585 		}
586 	}
587 	unlock_kernel();
588 	if (res > 0)
589 		res = 0;
590 	dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n",
591 			dentry->d_parent->d_name.name, dentry->d_name.name,
592 			res);
593 	return res;
594 }
595 
596 loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
597 {
598 	mutex_lock(&filp->f_dentry->d_inode->i_mutex);
599 	switch (origin) {
600 		case 1:
601 			offset += filp->f_pos;
602 		case 0:
603 			if (offset >= 0)
604 				break;
605 		default:
606 			offset = -EINVAL;
607 			goto out;
608 	}
609 	if (offset != filp->f_pos) {
610 		filp->f_pos = offset;
611 		((struct nfs_open_context *)filp->private_data)->dir_cookie = 0;
612 	}
613 out:
614 	mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
615 	return offset;
616 }
617 
618 /*
619  * All directory operations under NFS are synchronous, so fsync()
620  * is a dummy operation.
621  */
622 int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
623 {
624 	dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n",
625 			dentry->d_parent->d_name.name, dentry->d_name.name,
626 			datasync);
627 
628 	return 0;
629 }
630 
631 /*
632  * A check for whether or not the parent directory has changed.
633  * In the case it has, we assume that the dentries are untrustworthy
634  * and may need to be looked up again.
635  */
636 static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
637 {
638 	if (IS_ROOT(dentry))
639 		return 1;
640 	if ((NFS_I(dir)->cache_validity & NFS_INO_INVALID_ATTR) != 0
641 			|| nfs_attribute_timeout(dir))
642 		return 0;
643 	return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
644 }
645 
646 static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
647 {
648 	dentry->d_fsdata = (void *)verf;
649 }
650 
651 /*
652  * Whenever an NFS operation succeeds, we know that the dentry
653  * is valid, so we update the revalidation timestamp.
654  */
655 static inline void nfs_renew_times(struct dentry * dentry)
656 {
657 	dentry->d_time = jiffies;
658 }
659 
660 /*
661  * Return the intent data that applies to this particular path component
662  *
663  * Note that the current set of intents only apply to the very last
664  * component of the path.
665  * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
666  */
667 static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
668 {
669 	if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
670 		return 0;
671 	return nd->flags & mask;
672 }
673 
674 /*
675  * Inode and filehandle revalidation for lookups.
676  *
677  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
678  * or if the intent information indicates that we're about to open this
679  * particular file and the "nocto" mount flag is not set.
680  *
681  */
682 static inline
683 int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
684 {
685 	struct nfs_server *server = NFS_SERVER(inode);
686 
687 	if (nd != NULL) {
688 		/* VFS wants an on-the-wire revalidation */
689 		if (nd->flags & LOOKUP_REVAL)
690 			goto out_force;
691 		/* This is an open(2) */
692 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
693 				!(server->flags & NFS_MOUNT_NOCTO))
694 			goto out_force;
695 	}
696 	return nfs_revalidate_inode(server, inode);
697 out_force:
698 	return __nfs_revalidate_inode(server, inode);
699 }
700 
701 /*
702  * We judge how long we want to trust negative
703  * dentries by looking at the parent inode mtime.
704  *
705  * If parent mtime has changed, we revalidate, else we wait for a
706  * period corresponding to the parent's attribute cache timeout value.
707  */
708 static inline
709 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
710 		       struct nameidata *nd)
711 {
712 	/* Don't revalidate a negative dentry if we're creating a new file */
713 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
714 		return 0;
715 	return !nfs_check_verifier(dir, dentry);
716 }
717 
718 /*
719  * This is called every time the dcache has a lookup hit,
720  * and we should check whether we can really trust that
721  * lookup.
722  *
723  * NOTE! The hit can be a negative hit too, don't assume
724  * we have an inode!
725  *
726  * If the parent directory is seen to have changed, we throw out the
727  * cached dentry and do a new lookup.
728  */
729 static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
730 {
731 	struct inode *dir;
732 	struct inode *inode;
733 	struct dentry *parent;
734 	int error;
735 	struct nfs_fh fhandle;
736 	struct nfs_fattr fattr;
737 	unsigned long verifier;
738 
739 	parent = dget_parent(dentry);
740 	lock_kernel();
741 	dir = parent->d_inode;
742 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
743 	inode = dentry->d_inode;
744 
745 	if (!inode) {
746 		if (nfs_neg_need_reval(dir, dentry, nd))
747 			goto out_bad;
748 		goto out_valid;
749 	}
750 
751 	if (is_bad_inode(inode)) {
752 		dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
753 				__FUNCTION__, dentry->d_parent->d_name.name,
754 				dentry->d_name.name);
755 		goto out_bad;
756 	}
757 
758 	/* Revalidate parent directory attribute cache */
759 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
760 		goto out_zap_parent;
761 
762 	/* Force a full look up iff the parent directory has changed */
763 	if (nfs_check_verifier(dir, dentry)) {
764 		if (nfs_lookup_verify_inode(inode, nd))
765 			goto out_zap_parent;
766 		goto out_valid;
767 	}
768 
769 	if (NFS_STALE(inode))
770 		goto out_bad;
771 
772 	verifier = nfs_save_change_attribute(dir);
773 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
774 	if (error)
775 		goto out_bad;
776 	if (nfs_compare_fh(NFS_FH(inode), &fhandle))
777 		goto out_bad;
778 	if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
779 		goto out_bad;
780 
781 	nfs_renew_times(dentry);
782 	nfs_set_verifier(dentry, verifier);
783  out_valid:
784 	unlock_kernel();
785 	dput(parent);
786 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
787 			__FUNCTION__, dentry->d_parent->d_name.name,
788 			dentry->d_name.name);
789 	return 1;
790 out_zap_parent:
791 	nfs_zap_caches(dir);
792  out_bad:
793 	NFS_CACHEINV(dir);
794 	if (inode && S_ISDIR(inode->i_mode)) {
795 		/* Purge readdir caches. */
796 		nfs_zap_caches(inode);
797 		/* If we have submounts, don't unhash ! */
798 		if (have_submounts(dentry))
799 			goto out_valid;
800 		shrink_dcache_parent(dentry);
801 	}
802 	d_drop(dentry);
803 	unlock_kernel();
804 	dput(parent);
805 	dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
806 			__FUNCTION__, dentry->d_parent->d_name.name,
807 			dentry->d_name.name);
808 	return 0;
809 }
810 
811 /*
812  * This is called from dput() when d_count is going to 0.
813  */
814 static int nfs_dentry_delete(struct dentry *dentry)
815 {
816 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
817 		dentry->d_parent->d_name.name, dentry->d_name.name,
818 		dentry->d_flags);
819 
820 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
821 		/* Unhash it, so that ->d_iput() would be called */
822 		return 1;
823 	}
824 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
825 		/* Unhash it, so that ancestors of killed async unlink
826 		 * files will be cleaned up during umount */
827 		return 1;
828 	}
829 	return 0;
830 
831 }
832 
833 /*
834  * Called when the dentry loses inode.
835  * We use it to clean up silly-renamed files.
836  */
837 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
838 {
839 	nfs_inode_return_delegation(inode);
840 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
841 		lock_kernel();
842 		inode->i_nlink--;
843 		nfs_complete_unlink(dentry);
844 		unlock_kernel();
845 	}
846 	/* When creating a negative dentry, we want to renew d_time */
847 	nfs_renew_times(dentry);
848 	iput(inode);
849 }
850 
851 struct dentry_operations nfs_dentry_operations = {
852 	.d_revalidate	= nfs_lookup_revalidate,
853 	.d_delete	= nfs_dentry_delete,
854 	.d_iput		= nfs_dentry_iput,
855 };
856 
857 /*
858  * Use intent information to check whether or not we're going to do
859  * an O_EXCL create using this path component.
860  */
861 static inline
862 int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
863 {
864 	if (NFS_PROTO(dir)->version == 2)
865 		return 0;
866 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
867 		return 0;
868 	return (nd->intent.open.flags & O_EXCL) != 0;
869 }
870 
871 static inline int nfs_reval_fsid(struct inode *dir,
872 		struct nfs_fh *fh, struct nfs_fattr *fattr)
873 {
874 	struct nfs_server *server = NFS_SERVER(dir);
875 
876 	if (!nfs_fsid_equal(&server->fsid, &fattr->fsid))
877 		/* Revalidate fsid on root dir */
878 		return __nfs_revalidate_inode(server, dir->i_sb->s_root->d_inode);
879 	return 0;
880 }
881 
882 static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
883 {
884 	struct dentry *res;
885 	struct inode *inode = NULL;
886 	int error;
887 	struct nfs_fh fhandle;
888 	struct nfs_fattr fattr;
889 
890 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
891 		dentry->d_parent->d_name.name, dentry->d_name.name);
892 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
893 
894 	res = ERR_PTR(-ENAMETOOLONG);
895 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
896 		goto out;
897 
898 	res = ERR_PTR(-ENOMEM);
899 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
900 
901 	lock_kernel();
902 
903 	/* If we're doing an exclusive create, optimize away the lookup */
904 	if (nfs_is_exclusive_create(dir, nd))
905 		goto no_entry;
906 
907 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
908 	if (error == -ENOENT)
909 		goto no_entry;
910 	if (error < 0) {
911 		res = ERR_PTR(error);
912 		goto out_unlock;
913 	}
914 	error = nfs_reval_fsid(dir, &fhandle, &fattr);
915 	if (error < 0) {
916 		res = ERR_PTR(error);
917 		goto out_unlock;
918 	}
919 	inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
920 	res = (struct dentry *)inode;
921 	if (IS_ERR(res))
922 		goto out_unlock;
923 no_entry:
924 	res = d_add_unique(dentry, inode);
925 	if (res != NULL)
926 		dentry = res;
927 	nfs_renew_times(dentry);
928 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
929 out_unlock:
930 	unlock_kernel();
931 out:
932 	return res;
933 }
934 
935 #ifdef CONFIG_NFS_V4
936 static int nfs_open_revalidate(struct dentry *, struct nameidata *);
937 
938 struct dentry_operations nfs4_dentry_operations = {
939 	.d_revalidate	= nfs_open_revalidate,
940 	.d_delete	= nfs_dentry_delete,
941 	.d_iput		= nfs_dentry_iput,
942 };
943 
944 /*
945  * Use intent information to determine whether we need to substitute
946  * the NFSv4-style stateful OPEN for the LOOKUP call
947  */
948 static int is_atomic_open(struct inode *dir, struct nameidata *nd)
949 {
950 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
951 		return 0;
952 	/* NFS does not (yet) have a stateful open for directories */
953 	if (nd->flags & LOOKUP_DIRECTORY)
954 		return 0;
955 	/* Are we trying to write to a read only partition? */
956 	if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
957 		return 0;
958 	return 1;
959 }
960 
961 static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
962 {
963 	struct dentry *res = NULL;
964 	int error;
965 
966 	dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
967 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
968 
969 	/* Check that we are indeed trying to open this file */
970 	if (!is_atomic_open(dir, nd))
971 		goto no_open;
972 
973 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
974 		res = ERR_PTR(-ENAMETOOLONG);
975 		goto out;
976 	}
977 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
978 
979 	/* Let vfs_create() deal with O_EXCL */
980 	if (nd->intent.open.flags & O_EXCL) {
981 		d_add(dentry, NULL);
982 		goto out;
983 	}
984 
985 	/* Open the file on the server */
986 	lock_kernel();
987 	/* Revalidate parent directory attribute cache */
988 	error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
989 	if (error < 0) {
990 		res = ERR_PTR(error);
991 		unlock_kernel();
992 		goto out;
993 	}
994 
995 	if (nd->intent.open.flags & O_CREAT) {
996 		nfs_begin_data_update(dir);
997 		res = nfs4_atomic_open(dir, dentry, nd);
998 		nfs_end_data_update(dir);
999 	} else
1000 		res = nfs4_atomic_open(dir, dentry, nd);
1001 	unlock_kernel();
1002 	if (IS_ERR(res)) {
1003 		error = PTR_ERR(res);
1004 		switch (error) {
1005 			/* Make a negative dentry */
1006 			case -ENOENT:
1007 				res = NULL;
1008 				goto out;
1009 			/* This turned out not to be a regular file */
1010 			case -EISDIR:
1011 			case -ENOTDIR:
1012 				goto no_open;
1013 			case -ELOOP:
1014 				if (!(nd->intent.open.flags & O_NOFOLLOW))
1015 					goto no_open;
1016 			/* case -EINVAL: */
1017 			default:
1018 				goto out;
1019 		}
1020 	} else if (res != NULL)
1021 		dentry = res;
1022 	nfs_renew_times(dentry);
1023 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1024 out:
1025 	return res;
1026 no_open:
1027 	return nfs_lookup(dir, dentry, nd);
1028 }
1029 
1030 static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1031 {
1032 	struct dentry *parent = NULL;
1033 	struct inode *inode = dentry->d_inode;
1034 	struct inode *dir;
1035 	unsigned long verifier;
1036 	int openflags, ret = 0;
1037 
1038 	parent = dget_parent(dentry);
1039 	dir = parent->d_inode;
1040 	if (!is_atomic_open(dir, nd))
1041 		goto no_open;
1042 	/* We can't create new files in nfs_open_revalidate(), so we
1043 	 * optimize away revalidation of negative dentries.
1044 	 */
1045 	if (inode == NULL)
1046 		goto out;
1047 	/* NFS only supports OPEN on regular files */
1048 	if (!S_ISREG(inode->i_mode))
1049 		goto no_open;
1050 	openflags = nd->intent.open.flags;
1051 	/* We cannot do exclusive creation on a positive dentry */
1052 	if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1053 		goto no_open;
1054 	/* We can't create new files, or truncate existing ones here */
1055 	openflags &= ~(O_CREAT|O_TRUNC);
1056 
1057 	/*
1058 	 * Note: we're not holding inode->i_mutex and so may be racing with
1059 	 * operations that change the directory. We therefore save the
1060 	 * change attribute *before* we do the RPC call.
1061 	 */
1062 	lock_kernel();
1063 	verifier = nfs_save_change_attribute(dir);
1064 	ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
1065 	if (!ret)
1066 		nfs_set_verifier(dentry, verifier);
1067 	unlock_kernel();
1068 out:
1069 	dput(parent);
1070 	if (!ret)
1071 		d_drop(dentry);
1072 	return ret;
1073 no_open:
1074 	dput(parent);
1075 	if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
1076 		return 1;
1077 	return nfs_lookup_revalidate(dentry, nd);
1078 }
1079 #endif /* CONFIG_NFSV4 */
1080 
1081 static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
1082 {
1083 	struct dentry *parent = desc->file->f_dentry;
1084 	struct inode *dir = parent->d_inode;
1085 	struct nfs_entry *entry = desc->entry;
1086 	struct dentry *dentry, *alias;
1087 	struct qstr name = {
1088 		.name = entry->name,
1089 		.len = entry->len,
1090 	};
1091 	struct inode *inode;
1092 
1093 	switch (name.len) {
1094 		case 2:
1095 			if (name.name[0] == '.' && name.name[1] == '.')
1096 				return dget_parent(parent);
1097 			break;
1098 		case 1:
1099 			if (name.name[0] == '.')
1100 				return dget(parent);
1101 	}
1102 	name.hash = full_name_hash(name.name, name.len);
1103 	dentry = d_lookup(parent, &name);
1104 	if (dentry != NULL)
1105 		return dentry;
1106 	if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
1107 		return NULL;
1108 	/* Note: caller is already holding the dir->i_mutex! */
1109 	dentry = d_alloc(parent, &name);
1110 	if (dentry == NULL)
1111 		return NULL;
1112 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
1113 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
1114 	if (IS_ERR(inode)) {
1115 		dput(dentry);
1116 		return NULL;
1117 	}
1118 	alias = d_add_unique(dentry, inode);
1119 	if (alias != NULL) {
1120 		dput(dentry);
1121 		dentry = alias;
1122 	}
1123 	nfs_renew_times(dentry);
1124 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1125 	return dentry;
1126 }
1127 
1128 /*
1129  * Code common to create, mkdir, and mknod.
1130  */
1131 int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1132 				struct nfs_fattr *fattr)
1133 {
1134 	struct inode *inode;
1135 	int error = -EACCES;
1136 
1137 	/* We may have been initialized further down */
1138 	if (dentry->d_inode)
1139 		return 0;
1140 	if (fhandle->size == 0) {
1141 		struct inode *dir = dentry->d_parent->d_inode;
1142 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
1143 		if (error)
1144 			goto out_err;
1145 	}
1146 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
1147 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1148 		error = server->rpc_ops->getattr(server, fhandle, fattr);
1149 		if (error < 0)
1150 			goto out_err;
1151 	}
1152 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
1153 	error = PTR_ERR(inode);
1154 	if (IS_ERR(inode))
1155 		goto out_err;
1156 	d_instantiate(dentry, inode);
1157 	return 0;
1158 out_err:
1159 	d_drop(dentry);
1160 	return error;
1161 }
1162 
1163 /*
1164  * Following a failed create operation, we drop the dentry rather
1165  * than retain a negative dentry. This avoids a problem in the event
1166  * that the operation succeeded on the server, but an error in the
1167  * reply path made it appear to have failed.
1168  */
1169 static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1170 		struct nameidata *nd)
1171 {
1172 	struct iattr attr;
1173 	int error;
1174 	int open_flags = 0;
1175 
1176 	dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
1177 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1178 
1179 	attr.ia_mode = mode;
1180 	attr.ia_valid = ATTR_MODE;
1181 
1182 	if (nd && (nd->flags & LOOKUP_CREATE))
1183 		open_flags = nd->intent.open.flags;
1184 
1185 	lock_kernel();
1186 	nfs_begin_data_update(dir);
1187 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
1188 	nfs_end_data_update(dir);
1189 	if (error != 0)
1190 		goto out_err;
1191 	nfs_renew_times(dentry);
1192 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1193 	unlock_kernel();
1194 	return 0;
1195 out_err:
1196 	unlock_kernel();
1197 	d_drop(dentry);
1198 	return error;
1199 }
1200 
1201 /*
1202  * See comments for nfs_proc_create regarding failed operations.
1203  */
1204 static int
1205 nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1206 {
1207 	struct iattr attr;
1208 	int status;
1209 
1210 	dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
1211 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1212 
1213 	if (!new_valid_dev(rdev))
1214 		return -EINVAL;
1215 
1216 	attr.ia_mode = mode;
1217 	attr.ia_valid = ATTR_MODE;
1218 
1219 	lock_kernel();
1220 	nfs_begin_data_update(dir);
1221 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1222 	nfs_end_data_update(dir);
1223 	if (status != 0)
1224 		goto out_err;
1225 	nfs_renew_times(dentry);
1226 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1227 	unlock_kernel();
1228 	return 0;
1229 out_err:
1230 	unlock_kernel();
1231 	d_drop(dentry);
1232 	return status;
1233 }
1234 
1235 /*
1236  * See comments for nfs_proc_create regarding failed operations.
1237  */
1238 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1239 {
1240 	struct iattr attr;
1241 	int error;
1242 
1243 	dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
1244 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1245 
1246 	attr.ia_valid = ATTR_MODE;
1247 	attr.ia_mode = mode | S_IFDIR;
1248 
1249 	lock_kernel();
1250 	nfs_begin_data_update(dir);
1251 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1252 	nfs_end_data_update(dir);
1253 	if (error != 0)
1254 		goto out_err;
1255 	nfs_renew_times(dentry);
1256 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1257 	unlock_kernel();
1258 	return 0;
1259 out_err:
1260 	d_drop(dentry);
1261 	unlock_kernel();
1262 	return error;
1263 }
1264 
1265 static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1266 {
1267 	int error;
1268 
1269 	dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1270 			dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1271 
1272 	lock_kernel();
1273 	nfs_begin_data_update(dir);
1274 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1275 	/* Ensure the VFS deletes this inode */
1276 	if (error == 0 && dentry->d_inode != NULL)
1277 		dentry->d_inode->i_nlink = 0;
1278 	nfs_end_data_update(dir);
1279 	unlock_kernel();
1280 
1281 	return error;
1282 }
1283 
1284 static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
1285 {
1286 	static unsigned int sillycounter;
1287 	const int      i_inosize  = sizeof(dir->i_ino)*2;
1288 	const int      countersize = sizeof(sillycounter)*2;
1289 	const int      slen       = sizeof(".nfs") + i_inosize + countersize - 1;
1290 	char           silly[slen+1];
1291 	struct qstr    qsilly;
1292 	struct dentry *sdentry;
1293 	int            error = -EIO;
1294 
1295 	dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
1296 		dentry->d_parent->d_name.name, dentry->d_name.name,
1297 		atomic_read(&dentry->d_count));
1298 	nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
1299 
1300 #ifdef NFS_PARANOIA
1301 if (!dentry->d_inode)
1302 printk("NFS: silly-renaming %s/%s, negative dentry??\n",
1303 dentry->d_parent->d_name.name, dentry->d_name.name);
1304 #endif
1305 	/*
1306 	 * We don't allow a dentry to be silly-renamed twice.
1307 	 */
1308 	error = -EBUSY;
1309 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1310 		goto out;
1311 
1312 	sprintf(silly, ".nfs%*.*lx",
1313 		i_inosize, i_inosize, dentry->d_inode->i_ino);
1314 
1315 	/* Return delegation in anticipation of the rename */
1316 	nfs_inode_return_delegation(dentry->d_inode);
1317 
1318 	sdentry = NULL;
1319 	do {
1320 		char *suffix = silly + slen - countersize;
1321 
1322 		dput(sdentry);
1323 		sillycounter++;
1324 		sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
1325 
1326 		dfprintk(VFS, "NFS: trying to rename %s to %s\n",
1327 				dentry->d_name.name, silly);
1328 
1329 		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
1330 		/*
1331 		 * N.B. Better to return EBUSY here ... it could be
1332 		 * dangerous to delete the file while it's in use.
1333 		 */
1334 		if (IS_ERR(sdentry))
1335 			goto out;
1336 	} while(sdentry->d_inode != NULL); /* need negative lookup */
1337 
1338 	qsilly.name = silly;
1339 	qsilly.len  = strlen(silly);
1340 	nfs_begin_data_update(dir);
1341 	if (dentry->d_inode) {
1342 		nfs_begin_data_update(dentry->d_inode);
1343 		error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1344 				dir, &qsilly);
1345 		nfs_mark_for_revalidate(dentry->d_inode);
1346 		nfs_end_data_update(dentry->d_inode);
1347 	} else
1348 		error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
1349 				dir, &qsilly);
1350 	nfs_end_data_update(dir);
1351 	if (!error) {
1352 		nfs_renew_times(dentry);
1353 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1354 		d_move(dentry, sdentry);
1355 		error = nfs_async_unlink(dentry);
1356  		/* If we return 0 we don't unlink */
1357 	}
1358 	dput(sdentry);
1359 out:
1360 	return error;
1361 }
1362 
1363 /*
1364  * Remove a file after making sure there are no pending writes,
1365  * and after checking that the file has only one user.
1366  *
1367  * We invalidate the attribute cache and free the inode prior to the operation
1368  * to avoid possible races if the server reuses the inode.
1369  */
1370 static int nfs_safe_remove(struct dentry *dentry)
1371 {
1372 	struct inode *dir = dentry->d_parent->d_inode;
1373 	struct inode *inode = dentry->d_inode;
1374 	int error = -EBUSY;
1375 
1376 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
1377 		dentry->d_parent->d_name.name, dentry->d_name.name);
1378 
1379 	/* If the dentry was sillyrenamed, we simply call d_delete() */
1380 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1381 		error = 0;
1382 		goto out;
1383 	}
1384 
1385 	nfs_begin_data_update(dir);
1386 	if (inode != NULL) {
1387 		nfs_inode_return_delegation(inode);
1388 		nfs_begin_data_update(inode);
1389 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1390 		/* The VFS may want to delete this inode */
1391 		if (error == 0)
1392 			inode->i_nlink--;
1393 		nfs_mark_for_revalidate(inode);
1394 		nfs_end_data_update(inode);
1395 	} else
1396 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1397 	nfs_end_data_update(dir);
1398 out:
1399 	return error;
1400 }
1401 
1402 /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
1403  *  belongs to an active ".nfs..." file and we return -EBUSY.
1404  *
1405  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
1406  */
1407 static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1408 {
1409 	int error;
1410 	int need_rehash = 0;
1411 
1412 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1413 		dir->i_ino, dentry->d_name.name);
1414 
1415 	lock_kernel();
1416 	spin_lock(&dcache_lock);
1417 	spin_lock(&dentry->d_lock);
1418 	if (atomic_read(&dentry->d_count) > 1) {
1419 		spin_unlock(&dentry->d_lock);
1420 		spin_unlock(&dcache_lock);
1421 		error = nfs_sillyrename(dir, dentry);
1422 		unlock_kernel();
1423 		return error;
1424 	}
1425 	if (!d_unhashed(dentry)) {
1426 		__d_drop(dentry);
1427 		need_rehash = 1;
1428 	}
1429 	spin_unlock(&dentry->d_lock);
1430 	spin_unlock(&dcache_lock);
1431 	error = nfs_safe_remove(dentry);
1432 	if (!error) {
1433 		nfs_renew_times(dentry);
1434 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1435 	} else if (need_rehash)
1436 		d_rehash(dentry);
1437 	unlock_kernel();
1438 	return error;
1439 }
1440 
1441 static int
1442 nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1443 {
1444 	struct iattr attr;
1445 	struct nfs_fattr sym_attr;
1446 	struct nfs_fh sym_fh;
1447 	struct qstr qsymname;
1448 	int error;
1449 
1450 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
1451 		dir->i_ino, dentry->d_name.name, symname);
1452 
1453 #ifdef NFS_PARANOIA
1454 if (dentry->d_inode)
1455 printk("nfs_proc_symlink: %s/%s not negative!\n",
1456 dentry->d_parent->d_name.name, dentry->d_name.name);
1457 #endif
1458 	/*
1459 	 * Fill in the sattr for the call.
1460  	 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
1461 	 */
1462 	attr.ia_valid = ATTR_MODE;
1463 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
1464 
1465 	qsymname.name = symname;
1466 	qsymname.len  = strlen(symname);
1467 
1468 	lock_kernel();
1469 	nfs_begin_data_update(dir);
1470 	error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
1471 					  &attr, &sym_fh, &sym_attr);
1472 	nfs_end_data_update(dir);
1473 	if (!error) {
1474 		error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
1475 	} else {
1476 		if (error == -EEXIST)
1477 			printk("nfs_proc_symlink: %s/%s already exists??\n",
1478 			       dentry->d_parent->d_name.name, dentry->d_name.name);
1479 		d_drop(dentry);
1480 	}
1481 	unlock_kernel();
1482 	return error;
1483 }
1484 
1485 static int
1486 nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1487 {
1488 	struct inode *inode = old_dentry->d_inode;
1489 	int error;
1490 
1491 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
1492 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1493 		dentry->d_parent->d_name.name, dentry->d_name.name);
1494 
1495 	lock_kernel();
1496 	nfs_begin_data_update(dir);
1497 	nfs_begin_data_update(inode);
1498 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1499 	if (error == 0) {
1500 		atomic_inc(&inode->i_count);
1501 		d_instantiate(dentry, inode);
1502 	}
1503 	nfs_end_data_update(inode);
1504 	nfs_end_data_update(dir);
1505 	unlock_kernel();
1506 	return error;
1507 }
1508 
1509 /*
1510  * RENAME
1511  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
1512  * different file handle for the same inode after a rename (e.g. when
1513  * moving to a different directory). A fail-safe method to do so would
1514  * be to look up old_dir/old_name, create a link to new_dir/new_name and
1515  * rename the old file using the sillyrename stuff. This way, the original
1516  * file in old_dir will go away when the last process iput()s the inode.
1517  *
1518  * FIXED.
1519  *
1520  * It actually works quite well. One needs to have the possibility for
1521  * at least one ".nfs..." file in each directory the file ever gets
1522  * moved or linked to which happens automagically with the new
1523  * implementation that only depends on the dcache stuff instead of
1524  * using the inode layer
1525  *
1526  * Unfortunately, things are a little more complicated than indicated
1527  * above. For a cross-directory move, we want to make sure we can get
1528  * rid of the old inode after the operation.  This means there must be
1529  * no pending writes (if it's a file), and the use count must be 1.
1530  * If these conditions are met, we can drop the dentries before doing
1531  * the rename.
1532  */
1533 static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1534 		      struct inode *new_dir, struct dentry *new_dentry)
1535 {
1536 	struct inode *old_inode = old_dentry->d_inode;
1537 	struct inode *new_inode = new_dentry->d_inode;
1538 	struct dentry *dentry = NULL, *rehash = NULL;
1539 	int error = -EBUSY;
1540 
1541 	/*
1542 	 * To prevent any new references to the target during the rename,
1543 	 * we unhash the dentry and free the inode in advance.
1544 	 */
1545 	lock_kernel();
1546 	if (!d_unhashed(new_dentry)) {
1547 		d_drop(new_dentry);
1548 		rehash = new_dentry;
1549 	}
1550 
1551 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1552 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1553 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1554 		 atomic_read(&new_dentry->d_count));
1555 
1556 	/*
1557 	 * First check whether the target is busy ... we can't
1558 	 * safely do _any_ rename if the target is in use.
1559 	 *
1560 	 * For files, make a copy of the dentry and then do a
1561 	 * silly-rename. If the silly-rename succeeds, the
1562 	 * copied dentry is hashed and becomes the new target.
1563 	 */
1564 	if (!new_inode)
1565 		goto go_ahead;
1566 	if (S_ISDIR(new_inode->i_mode)) {
1567 		error = -EISDIR;
1568 		if (!S_ISDIR(old_inode->i_mode))
1569 			goto out;
1570 	} else if (atomic_read(&new_dentry->d_count) > 2) {
1571 		int err;
1572 		/* copy the target dentry's name */
1573 		dentry = d_alloc(new_dentry->d_parent,
1574 				 &new_dentry->d_name);
1575 		if (!dentry)
1576 			goto out;
1577 
1578 		/* silly-rename the existing target ... */
1579 		err = nfs_sillyrename(new_dir, new_dentry);
1580 		if (!err) {
1581 			new_dentry = rehash = dentry;
1582 			new_inode = NULL;
1583 			/* instantiate the replacement target */
1584 			d_instantiate(new_dentry, NULL);
1585 		} else if (atomic_read(&new_dentry->d_count) > 1) {
1586 		/* dentry still busy? */
1587 #ifdef NFS_PARANOIA
1588 			printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1589 			       new_dentry->d_parent->d_name.name,
1590 			       new_dentry->d_name.name,
1591 			       atomic_read(&new_dentry->d_count));
1592 #endif
1593 			goto out;
1594 		}
1595 	} else
1596 		new_inode->i_nlink--;
1597 
1598 go_ahead:
1599 	/*
1600 	 * ... prune child dentries and writebacks if needed.
1601 	 */
1602 	if (atomic_read(&old_dentry->d_count) > 1) {
1603 		nfs_wb_all(old_inode);
1604 		shrink_dcache_parent(old_dentry);
1605 	}
1606 	nfs_inode_return_delegation(old_inode);
1607 
1608 	if (new_inode != NULL) {
1609 		nfs_inode_return_delegation(new_inode);
1610 		d_delete(new_dentry);
1611 	}
1612 
1613 	nfs_begin_data_update(old_dir);
1614 	nfs_begin_data_update(new_dir);
1615 	nfs_begin_data_update(old_inode);
1616 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1617 					   new_dir, &new_dentry->d_name);
1618 	nfs_mark_for_revalidate(old_inode);
1619 	nfs_end_data_update(old_inode);
1620 	nfs_end_data_update(new_dir);
1621 	nfs_end_data_update(old_dir);
1622 out:
1623 	if (rehash)
1624 		d_rehash(rehash);
1625 	if (!error) {
1626 		if (!S_ISDIR(old_inode->i_mode))
1627 			d_move(old_dentry, new_dentry);
1628 		nfs_renew_times(new_dentry);
1629 		nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
1630 	}
1631 
1632 	/* new dentry created? */
1633 	if (dentry)
1634 		dput(dentry);
1635 	unlock_kernel();
1636 	return error;
1637 }
1638 
1639 int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
1640 {
1641 	struct nfs_inode *nfsi = NFS_I(inode);
1642 	struct nfs_access_entry *cache = &nfsi->cache_access;
1643 
1644 	if (cache->cred != cred
1645 			|| time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
1646 			|| (nfsi->cache_validity & NFS_INO_INVALID_ACCESS))
1647 		return -ENOENT;
1648 	memcpy(res, cache, sizeof(*res));
1649 	return 0;
1650 }
1651 
1652 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
1653 {
1654 	struct nfs_inode *nfsi = NFS_I(inode);
1655 	struct nfs_access_entry *cache = &nfsi->cache_access;
1656 
1657 	if (cache->cred != set->cred) {
1658 		if (cache->cred)
1659 			put_rpccred(cache->cred);
1660 		cache->cred = get_rpccred(set->cred);
1661 	}
1662 	/* FIXME: replace current access_cache BKL reliance with inode->i_lock */
1663 	spin_lock(&inode->i_lock);
1664 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
1665 	spin_unlock(&inode->i_lock);
1666 	cache->jiffies = set->jiffies;
1667 	cache->mask = set->mask;
1668 }
1669 
1670 static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
1671 {
1672 	struct nfs_access_entry cache;
1673 	int status;
1674 
1675 	status = nfs_access_get_cached(inode, cred, &cache);
1676 	if (status == 0)
1677 		goto out;
1678 
1679 	/* Be clever: ask server to check for all possible rights */
1680 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
1681 	cache.cred = cred;
1682 	cache.jiffies = jiffies;
1683 	status = NFS_PROTO(inode)->access(inode, &cache);
1684 	if (status != 0)
1685 		return status;
1686 	nfs_access_add_cache(inode, &cache);
1687 out:
1688 	if ((cache.mask & mask) == mask)
1689 		return 0;
1690 	return -EACCES;
1691 }
1692 
1693 int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1694 {
1695 	struct rpc_cred *cred;
1696 	int res = 0;
1697 
1698 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
1699 
1700 	if (mask == 0)
1701 		goto out;
1702 	/* Is this sys_access() ? */
1703 	if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
1704 		goto force_lookup;
1705 
1706 	switch (inode->i_mode & S_IFMT) {
1707 		case S_IFLNK:
1708 			goto out;
1709 		case S_IFREG:
1710 			/* NFSv4 has atomic_open... */
1711 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
1712 					&& nd != NULL
1713 					&& (nd->flags & LOOKUP_OPEN))
1714 				goto out;
1715 			break;
1716 		case S_IFDIR:
1717 			/*
1718 			 * Optimize away all write operations, since the server
1719 			 * will check permissions when we perform the op.
1720 			 */
1721 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
1722 				goto out;
1723 	}
1724 
1725 force_lookup:
1726 	lock_kernel();
1727 
1728 	if (!NFS_PROTO(inode)->access)
1729 		goto out_notsup;
1730 
1731 	cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
1732 	if (!IS_ERR(cred)) {
1733 		res = nfs_do_access(inode, cred, mask);
1734 		put_rpccred(cred);
1735 	} else
1736 		res = PTR_ERR(cred);
1737 	unlock_kernel();
1738 out:
1739 	dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
1740 		inode->i_sb->s_id, inode->i_ino, mask, res);
1741 	return res;
1742 out_notsup:
1743 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1744 	if (res == 0)
1745 		res = generic_permission(inode, mask, NULL);
1746 	unlock_kernel();
1747 	goto out;
1748 }
1749 
1750 /*
1751  * Local variables:
1752  *  version-control: t
1753  *  kept-new-versions: 5
1754  * End:
1755  */
1756