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