File: /usr/src/linux/fs/nfs/dir.c

1     /*
2      *  linux/fs/nfs/dir.c
3      *
4      *  Copyright (C) 1992  Rick Sladkey
5      *
6      *  nfs directory handling functions
7      *
8      * 10 Apr 1996	Added silly rename for unlink	--okir
9      * 28 Sep 1996	Improved directory cache --okir
10      * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de 
11      *              Re-implemented silly rename for unlink, newly implemented
12      *              silly rename for nfs_rename() following the suggestions
13      *              of Olaf Kirch (okir) found in this file.
14      *              Following Linus comments on my original hack, this version
15      *              depends only on the dcache stuff and doesn't touch the inode
16      *              layer (iput() and friends).
17      *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
18      */
19     
20     #include <linux/sched.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     
34     #define NFS_PARANOIA 1
35     /* #define NFS_DEBUG_VERBOSE 1 */
36     
37     static int nfs_readdir(struct file *, void *, filldir_t);
38     static struct dentry *nfs_lookup(struct inode *, struct dentry *);
39     static int nfs_create(struct inode *, struct dentry *, int);
40     static int nfs_mkdir(struct inode *, struct dentry *, int);
41     static int nfs_rmdir(struct inode *, struct dentry *);
42     static int nfs_unlink(struct inode *, struct dentry *);
43     static int nfs_symlink(struct inode *, struct dentry *, const char *);
44     static int nfs_link(struct dentry *, struct inode *, struct dentry *);
45     static int nfs_mknod(struct inode *, struct dentry *, int, int);
46     static int nfs_rename(struct inode *, struct dentry *,
47     		      struct inode *, struct dentry *);
48     
49     struct file_operations nfs_dir_operations = {
50     	read:		generic_read_dir,
51     	readdir:	nfs_readdir,
52     	open:		nfs_open,
53     	release:	nfs_release,
54     };
55     
56     struct inode_operations nfs_dir_inode_operations = {
57     	create:		nfs_create,
58     	lookup:		nfs_lookup,
59     	link:		nfs_link,
60     	unlink:		nfs_unlink,
61     	symlink:	nfs_symlink,
62     	mkdir:		nfs_mkdir,
63     	rmdir:		nfs_rmdir,
64     	mknod:		nfs_mknod,
65     	rename:		nfs_rename,
66     	permission:	nfs_permission,
67     	revalidate:	nfs_revalidate,
68     	setattr:	nfs_notify_change,
69     };
70     
71     typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
72     typedef struct {
73     	struct file	*file;
74     	struct page	*page;
75     	unsigned long	page_index;
76     	u32		*ptr;
77     	u64		target;
78     	struct nfs_entry *entry;
79     	decode_dirent_t	decode;
80     	int		plus;
81     	int		error;
82     } nfs_readdir_descriptor_t;
83     
84     /* Now we cache directories properly, by stuffing the dirent
85      * data directly in the page cache.
86      *
87      * Inode invalidation due to refresh etc. takes care of
88      * _everything_, no sloppy entry flushing logic, no extraneous
89      * copying, network direct to page cache, the way it was meant
90      * to be.
91      *
92      * NOTE: Dirent information verification is done always by the
93      *	 page-in of the RPC reply, nowhere else, this simplies
94      *	 things substantially.
95      */
96     static
97     int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
98     {
99     	struct file	*file = desc->file;
100     	struct inode	*inode = file->f_dentry->d_inode;
101     	struct rpc_cred	*cred = nfs_file_cred(file);
102     	void		*buffer = kmap(page);
103     	int		error;
104     
105     	dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
106     
107      again:
108     	error = NFS_PROTO(inode)->readdir(inode, cred, desc->entry->cookie, buffer,
109     					  NFS_SERVER(inode)->dtsize, desc->plus);
110     	/* We requested READDIRPLUS, but the server doesn't grok it */
111     	if (desc->plus && error == -ENOTSUPP) {
112     		NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
113     		desc->plus = 0;
114     		goto again;
115     	}
116     	if (error < 0)
117     		goto error;
118     	SetPageUptodate(page);
119     	kunmap(page);
120     	/* Ensure consistent page alignment of the data.
121     	 * Note: assumes we have exclusive access to this mapping either
122     	 *	 throught inode->i_sem or some other mechanism.
123     	 */
124     	if (page->index == 0)
125     		invalidate_inode_pages(inode);
126     	UnlockPage(page);
127     	return 0;
128      error:
129     	SetPageError(page);
130     	kunmap(page);
131     	UnlockPage(page);
132     	invalidate_inode_pages(inode);
133     	desc->error = error;
134     	return -EIO;
135     }
136     
137     static inline
138     int dir_decode(nfs_readdir_descriptor_t *desc)
139     {
140     	u32	*p = desc->ptr;
141     	p = desc->decode(p, desc->entry, desc->plus);
142     	if (IS_ERR(p))
143     		return PTR_ERR(p);
144     	desc->ptr = p;
145     	return 0;
146     }
147     
148     static inline
149     void dir_page_release(nfs_readdir_descriptor_t *desc)
150     {
151     	kunmap(desc->page);
152     	page_cache_release(desc->page);
153     	desc->page = NULL;
154     	desc->ptr = NULL;
155     }
156     
157     /*
158      * Given a pointer to a buffer that has already been filled by a call
159      * to readdir, find the next entry.
160      *
161      * If the end of the buffer has been reached, return -EAGAIN, if not,
162      * return the offset within the buffer of the next entry to be
163      * read.
164      */
165     static inline
166     int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
167     {
168     	struct nfs_entry *entry = desc->entry;
169     	int		loop_count = 0,
170     			status;
171     
172     	while((status = dir_decode(desc)) == 0) {
173     		dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
174     		if (entry->prev_cookie == desc->target)
175     			break;
176     		if (loop_count++ > 200) {
177     			loop_count = 0;
178     			schedule();
179     		}
180     	}
181     	dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
182     	return status;
183     }
184     
185     /*
186      * Find the given page, and call find_dirent() in order to try to
187      * return the next entry.
188      */
189     static inline
190     int find_dirent_page(nfs_readdir_descriptor_t *desc)
191     {
192     	struct inode	*inode = desc->file->f_dentry->d_inode;
193     	struct page	*page;
194     	int		status;
195     
196     	dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
197     
198     	desc->plus = NFS_USE_READDIRPLUS(inode);
199     	page = read_cache_page(&inode->i_data, desc->page_index,
200     			       (filler_t *)nfs_readdir_filler, desc);
201     	if (IS_ERR(page)) {
202     		status = PTR_ERR(page);
203     		goto out;
204     	}
205     	if (!Page_Uptodate(page))
206     		goto read_error;
207     
208     	/* NOTE: Someone else may have changed the READDIRPLUS flag */
209     	desc->page = page;
210     	desc->ptr = kmap(page);
211     	status = find_dirent(desc, page);
212     	if (status < 0)
213     		dir_page_release(desc);
214      out:
215     	dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
216     	return status;
217      read_error:
218     	page_cache_release(page);
219     	return -EIO;
220     }
221     
222     /*
223      * Recurse through the page cache pages, and return a
224      * filled nfs_entry structure of the next directory entry if possible.
225      *
226      * The target for the search is 'desc->target'.
227      */
228     static inline
229     int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
230     {
231     	int		loop_count = 0;
232     	int		res;
233     
234     	dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target);
235     	for (;;) {
236     		res = find_dirent_page(desc);
237     		if (res != -EAGAIN)
238     			break;
239     		/* Align to beginning of next page */
240     		desc->page_index ++;
241     		if (loop_count++ > 200) {
242     			loop_count = 0;
243     			schedule();
244     		}
245     	}
246     	dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
247     	return res;
248     }
249     
250     /*
251      * Once we've found the start of the dirent within a page: fill 'er up...
252      */
253     static 
254     int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
255     		   filldir_t filldir)
256     {
257     	struct file	*file = desc->file;
258     	struct nfs_entry *entry = desc->entry;
259     	unsigned long	fileid;
260     	int		loop_count = 0,
261     			res;
262     
263     	dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
264     
265     	for(;;) {
266     		/* Note: entry->prev_cookie contains the cookie for
267     		 *	 retrieving the current dirent on the server */
268     		fileid = nfs_fileid_to_ino_t(entry->ino);
269     		res = filldir(dirent, entry->name, entry->len, 
270     			      entry->prev_cookie, fileid, DT_UNKNOWN);
271     		if (res < 0)
272     			break;
273     		file->f_pos = desc->target = entry->cookie;
274     		if (dir_decode(desc) != 0) {
275     			desc->page_index ++;
276     			break;
277     		}
278     		if (loop_count++ > 200) {
279     			loop_count = 0;
280     			schedule();
281     		}
282     	}
283     	dir_page_release(desc);
284     
285     	dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
286     	return res;
287     }
288     
289     /*
290      * If we cannot find a cookie in our cache, we suspect that this is
291      * because it points to a deleted file, so we ask the server to return
292      * whatever it thinks is the next entry. We then feed this to filldir.
293      * If all goes well, we should then be able to find our way round the
294      * cache on the next call to readdir_search_pagecache();
295      *
296      * NOTE: we cannot add the anonymous page to the pagecache because
297      *	 the data it contains might not be page aligned. Besides,
298      *	 we should already have a complete representation of the
299      *	 directory in the page cache by the time we get here.
300      */
301     static inline
302     int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
303     		     filldir_t filldir)
304     {
305     	struct file	*file = desc->file;
306     	struct inode	*inode = file->f_dentry->d_inode;
307     	struct rpc_cred	*cred = nfs_file_cred(file);
308     	struct page	*page = NULL;
309     	int		status;
310     
311     	dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
312     
313     	page = alloc_page(GFP_HIGHUSER);
314     	if (!page) {
315     		status = -ENOMEM;
316     		goto out;
317     	}
318     	desc->page = page;
319     	desc->ptr = kmap(page);
320     	desc->error = NFS_PROTO(inode)->readdir(inode, cred, desc->target,
321     						desc->ptr,
322     						NFS_SERVER(inode)->dtsize,
323     						desc->plus);
324     	if (desc->error >= 0) {
325     		if ((status = dir_decode(desc)) == 0)
326     			desc->entry->prev_cookie = desc->target;
327     	} else
328     		status = -EIO;
329     	if (status < 0)
330     		goto out_release;
331     
332     	status = nfs_do_filldir(desc, dirent, filldir);
333     
334     	/* Reset read descriptor so it searches the page cache from
335     	 * the start upon the next call to readdir_search_pagecache() */
336     	desc->page_index = 0;
337     	memset(desc->entry, 0, sizeof(*desc->entry));
338      out:
339     	dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
340     	return status;
341      out_release:
342     	dir_page_release(desc);
343     	goto out;
344     }
345     
346     /* The file offset position is now represented as a true offset into the
347      * page cache as is the case in most of the other filesystems.
348      */
349     static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
350     {
351     	struct dentry	*dentry = filp->f_dentry;
352     	struct inode	*inode = dentry->d_inode;
353     	nfs_readdir_descriptor_t my_desc,
354     			*desc = &my_desc;
355     	struct nfs_entry my_entry;
356     	long		res;
357     
358     	res = nfs_revalidate(dentry);
359     	if (res < 0)
360     		return res;
361     
362     	/*
363     	 * filp->f_pos points to the file offset in the page cache.
364     	 * but if the cache has meanwhile been zapped, we need to
365     	 * read from the last dirent to revalidate f_pos
366     	 * itself.
367     	 */
368     	memset(desc, 0, sizeof(*desc));
369     	memset(&my_entry, 0, sizeof(my_entry));
370     
371     	desc->file = filp;
372     	desc->target = filp->f_pos;
373     	desc->entry = &my_entry;
374     	desc->decode = NFS_PROTO(inode)->decode_dirent;
375     
376     	while(!desc->entry->eof) {
377     		res = readdir_search_pagecache(desc);
378     		if (res == -EBADCOOKIE) {
379     			/* This means either end of directory */
380     			if (desc->entry->cookie != desc->target) {
381     				/* Or that the server has 'lost' a cookie */
382     				res = uncached_readdir(desc, dirent, filldir);
383     				if (res >= 0)
384     					continue;
385     			}
386     			res = 0;
387     			break;
388     		} else if (res < 0)
389     			break;
390     
391     		res = nfs_do_filldir(desc, dirent, filldir);
392     		if (res < 0) {
393     			res = 0;
394     			break;
395     		}
396     	}
397     	if (desc->error < 0)
398     		return desc->error;
399     	if (res < 0)
400     		return res;
401     	return 0;
402     }
403     
404     /*
405      * Whenever an NFS operation succeeds, we know that the dentry
406      * is valid, so we update the revalidation timestamp.
407      */
408     static inline void nfs_renew_times(struct dentry * dentry)
409     {
410     	dentry->d_time = jiffies;
411     }
412     
413     static inline int nfs_dentry_force_reval(struct dentry *dentry, int flags)
414     {
415     	struct inode *inode = dentry->d_inode;
416     	unsigned long timeout = NFS_ATTRTIMEO(inode);
417     
418     	/*
419     	 * If it's the last lookup in a series, we use a stricter
420     	 * cache consistency check by looking at the parent mtime.
421     	 *
422     	 * If it's been modified in the last hour, be really strict.
423     	 * (This still means that we can avoid doing unnecessary
424     	 * work on directories like /usr/share/bin etc which basically
425     	 * never change).
426     	 */
427     	if (!(flags & LOOKUP_CONTINUE)) {
428     		long diff = CURRENT_TIME - dentry->d_parent->d_inode->i_mtime;
429     
430     		if (diff < 15*60)
431     			timeout = 0;
432     	}
433     	
434     	return time_after(jiffies,dentry->d_time + timeout);
435     }
436     
437     /*
438      * We judge how long we want to trust negative
439      * dentries by looking at the parent inode mtime.
440      *
441      * If mtime is close to present time, we revalidate
442      * more often.
443      */
444     #define NFS_REVALIDATE_NEGATIVE (1 * HZ)
445     static inline int nfs_neg_need_reval(struct dentry *dentry)
446     {
447     	struct inode *dir = dentry->d_parent->d_inode;
448     	unsigned long timeout = NFS_ATTRTIMEO(dir);
449     	long diff = CURRENT_TIME - dir->i_mtime;
450     
451     	if (diff < 5*60 && timeout > NFS_REVALIDATE_NEGATIVE)
452     		timeout = NFS_REVALIDATE_NEGATIVE;
453     
454     	return time_after(jiffies, dentry->d_time + timeout);
455     }
456     
457     /*
458      * This is called every time the dcache has a lookup hit,
459      * and we should check whether we can really trust that
460      * lookup.
461      *
462      * NOTE! The hit can be a negative hit too, don't assume
463      * we have an inode!
464      *
465      * If the dentry is older than the revalidation interval, 
466      * we do a new lookup and verify that the dentry is still
467      * correct.
468      */
469     static int nfs_lookup_revalidate(struct dentry * dentry, int flags)
470     {
471     	struct inode *dir;
472     	struct inode *inode;
473     	int error;
474     	struct nfs_fh fhandle;
475     	struct nfs_fattr fattr;
476     
477     	lock_kernel();
478     	dir = dentry->d_parent->d_inode;
479     	inode = dentry->d_inode;
480     	/*
481     	 * If we don't have an inode, let's look at the parent
482     	 * directory mtime to get a hint about how often we
483     	 * should validate things..
484     	 */
485     	if (!inode) {
486     		if (nfs_neg_need_reval(dentry))
487     			goto out_bad;
488     		goto out_valid;
489     	}
490     
491     	if (is_bad_inode(inode)) {
492     		dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
493     			dentry->d_parent->d_name.name, dentry->d_name.name);
494     		goto out_bad;
495     	}
496     
497     	if (!nfs_dentry_force_reval(dentry, flags))
498     		goto out_valid;
499     
500     	if (IS_ROOT(dentry)) {
501     		__nfs_revalidate_inode(NFS_SERVER(inode), inode);
502     		goto out_valid_renew;
503     	}
504     
505     	/*
506     	 * Do a new lookup and check the dentry attributes.
507     	 */
508     	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
509     	if (error)
510     		goto out_bad;
511     
512     	/* Inode number matches? */
513     	if (!(fattr.valid & NFS_ATTR_FATTR) ||
514     	    NFS_FSID(inode) != fattr.fsid ||
515     	    NFS_FILEID(inode) != fattr.fileid)
516     		goto out_bad;
517     
518     	/* Ok, remember that we successfully checked it.. */
519     	nfs_refresh_inode(inode, &fattr);
520     
521     	if (nfs_inode_is_stale(inode, &fhandle, &fattr))
522     		goto out_bad;
523     
524      out_valid_renew:
525     	nfs_renew_times(dentry);
526     out_valid:
527     	unlock_kernel();
528     	return 1;
529     out_bad:
530     	shrink_dcache_parent(dentry);
531     	/* If we have submounts, don't unhash ! */
532     	if (have_submounts(dentry))
533     		goto out_valid;
534     	d_drop(dentry);
535     	/* Purge readdir caches. */
536     	nfs_zap_caches(dir);
537     	if (inode && S_ISDIR(inode->i_mode))
538     		nfs_zap_caches(inode);
539     	unlock_kernel();
540     	return 0;
541     }
542     
543     /*
544      * This is called from dput() when d_count is going to 0.
545      */
546     static int nfs_dentry_delete(struct dentry *dentry)
547     {
548     	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
549     		dentry->d_parent->d_name.name, dentry->d_name.name,
550     		dentry->d_flags);
551     
552     	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
553     		/* Unhash it, so that ->d_iput() would be called */
554     		return 1;
555     	}
556     	return 0;
557     
558     }
559     
560     /*
561      * Called when the dentry loses inode.
562      * We use it to clean up silly-renamed files.
563      */
564     static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
565     {
566     	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
567     		lock_kernel();
568     		nfs_complete_unlink(dentry);
569     		unlock_kernel();
570     	}
571     	iput(inode);
572     }
573     
574     struct dentry_operations nfs_dentry_operations = {
575     	d_revalidate:	nfs_lookup_revalidate,
576     	d_delete:	nfs_dentry_delete,
577     	d_iput:		nfs_dentry_iput,
578     };
579     
580     static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry)
581     {
582     	struct inode *inode;
583     	int error;
584     	struct nfs_fh fhandle;
585     	struct nfs_fattr fattr;
586     
587     	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
588     		dentry->d_parent->d_name.name, dentry->d_name.name);
589     
590     	error = -ENAMETOOLONG;
591     	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
592     		goto out;
593     
594     	error = -ENOMEM;
595     	dentry->d_op = &nfs_dentry_operations;
596     
597     	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
598     	inode = NULL;
599     	if (error == -ENOENT)
600     		goto no_entry;
601     	if (!error) {
602     		error = -EACCES;
603     		inode = nfs_fhget(dentry, &fhandle, &fattr);
604     		if (inode) {
605     	    no_entry:
606     			d_add(dentry, inode);
607     			nfs_renew_times(dentry);
608     			error = 0;
609     		}
610     	}
611     out:
612     	return ERR_PTR(error);
613     }
614     
615     /*
616      * Code common to create, mkdir, and mknod.
617      */
618     static int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
619     				struct nfs_fattr *fattr)
620     {
621     	struct inode *inode;
622     	int error = -EACCES;
623     
624     	inode = nfs_fhget(dentry, fhandle, fattr);
625     	if (inode) {
626     		d_instantiate(dentry, inode);
627     		nfs_renew_times(dentry);
628     		error = 0;
629     	}
630     	return error;
631     }
632     
633     /*
634      * Following a failed create operation, we drop the dentry rather
635      * than retain a negative dentry. This avoids a problem in the event
636      * that the operation succeeded on the server, but an error in the
637      * reply path made it appear to have failed.
638      */
639     static int nfs_create(struct inode *dir, struct dentry *dentry, int mode)
640     {
641     	struct iattr attr;
642     	struct nfs_fattr fattr;
643     	struct nfs_fh fhandle;
644     	int error;
645     
646     	dfprintk(VFS, "NFS: create(%x/%ld, %s\n",
647     		dir->i_dev, dir->i_ino, dentry->d_name.name);
648     
649     	attr.ia_mode = mode;
650     	attr.ia_valid = ATTR_MODE;
651     
652     	/*
653     	 * The 0 argument passed into the create function should one day
654     	 * contain the O_EXCL flag if requested. This allows NFSv3 to
655     	 * select the appropriate create strategy. Currently open_namei
656     	 * does not pass the create flags.
657     	 */
658     	nfs_zap_caches(dir);
659     	error = NFS_PROTO(dir)->create(dir, &dentry->d_name,
660     					 &attr, 0, &fhandle, &fattr);
661     	if (!error && fhandle.size != 0)
662     		error = nfs_instantiate(dentry, &fhandle, &fattr);
663     	if (error || fhandle.size == 0)
664     		d_drop(dentry);
665     	return error;
666     }
667     
668     /*
669      * See comments for nfs_proc_create regarding failed operations.
670      */
671     static int nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
672     {
673     	struct iattr attr;
674     	struct nfs_fattr fattr;
675     	struct nfs_fh fhandle;
676     	int error;
677     
678     	dfprintk(VFS, "NFS: mknod(%x/%ld, %s\n",
679     		dir->i_dev, dir->i_ino, dentry->d_name.name);
680     
681     	attr.ia_mode = mode;
682     	attr.ia_valid = ATTR_MODE;
683     
684     	nfs_zap_caches(dir);
685     	error = NFS_PROTO(dir)->mknod(dir, &dentry->d_name, &attr, rdev,
686     					&fhandle, &fattr);
687     	if (!error && fhandle.size != 0)
688     		error = nfs_instantiate(dentry, &fhandle, &fattr);
689     	if (error || fhandle.size == 0)
690     		d_drop(dentry);
691     	return error;
692     }
693     
694     /*
695      * See comments for nfs_proc_create regarding failed operations.
696      */
697     static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
698     {
699     	struct iattr attr;
700     	struct nfs_fattr fattr;
701     	struct nfs_fh fhandle;
702     	int error;
703     
704     	dfprintk(VFS, "NFS: mkdir(%x/%ld, %s\n",
705     		dir->i_dev, dir->i_ino, dentry->d_name.name);
706     
707     	attr.ia_valid = ATTR_MODE;
708     	attr.ia_mode = mode | S_IFDIR;
709     
710     #if 0
711     	/*
712     	 * Always drop the dentry, we can't always depend on
713     	 * the fattr returned by the server (AIX seems to be
714     	 * broken). We're better off doing another lookup than
715     	 * depending on potentially bogus information.
716     	 */
717     	d_drop(dentry);
718     #endif
719     	nfs_zap_caches(dir);
720     	error = NFS_PROTO(dir)->mkdir(dir, &dentry->d_name, &attr, &fhandle,
721     					&fattr);
722     	if (!error && fhandle.size != 0)
723     		error = nfs_instantiate(dentry, &fhandle, &fattr);
724     	if (error || fhandle.size == 0)
725     		d_drop(dentry);
726     	return error;
727     }
728     
729     static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
730     {
731     	int error;
732     
733     	dfprintk(VFS, "NFS: rmdir(%x/%ld, %s\n",
734     		dir->i_dev, dir->i_ino, dentry->d_name.name);
735     
736     	nfs_zap_caches(dir);
737     	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
738     	if (!error)
739     		dentry->d_inode->i_nlink = 0;
740     
741     	return error;
742     }
743     
744     static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
745     {
746     	static unsigned int sillycounter;
747     	const int      i_inosize  = sizeof(dir->i_ino)*2;
748     	const int      countersize = sizeof(sillycounter)*2;
749     	const int      slen       = strlen(".nfs") + i_inosize + countersize;
750     	char           silly[slen+1];
751     	struct qstr    qsilly;
752     	struct dentry *sdentry;
753     	int            error = -EIO;
754     
755     	dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
756     		dentry->d_parent->d_name.name, dentry->d_name.name, 
757     		atomic_read(&dentry->d_count));
758     
759     	if (atomic_read(&dentry->d_count) == 1)
760     		goto out;  /* No need to silly rename. */
761     
762     
763     #ifdef NFS_PARANOIA
764     if (!dentry->d_inode)
765     printk("NFS: silly-renaming %s/%s, negative dentry??\n",
766     dentry->d_parent->d_name.name, dentry->d_name.name);
767     #endif
768     	/*
769     	 * We don't allow a dentry to be silly-renamed twice.
770     	 */
771     	error = -EBUSY;
772     	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
773     		goto out;
774     
775     	sprintf(silly, ".nfs%*.*lx",
776     		i_inosize, i_inosize, dentry->d_inode->i_ino);
777     
778     	sdentry = NULL;
779     	do {
780     		char *suffix = silly + slen - countersize;
781     
782     		dput(sdentry);
783     		sillycounter++;
784     		sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
785     
786     		dfprintk(VFS, "trying to rename %s to %s\n",
787     			 dentry->d_name.name, silly);
788     		
789     		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
790     		/*
791     		 * N.B. Better to return EBUSY here ... it could be
792     		 * dangerous to delete the file while it's in use.
793     		 */
794     		if (IS_ERR(sdentry))
795     			goto out;
796     	} while(sdentry->d_inode != NULL); /* need negative lookup */
797     
798     	nfs_zap_caches(dir);
799     	qsilly.name = silly;
800     	qsilly.len  = strlen(silly);
801     	error = NFS_PROTO(dir)->rename(dir, &dentry->d_name, dir, &qsilly);
802     	if (!error) {
803     		nfs_renew_times(dentry);
804     		d_move(dentry, sdentry);
805     		error = nfs_async_unlink(dentry);
806      		/* If we return 0 we don't unlink */
807     	}
808     	dput(sdentry);
809     out:
810     	return error;
811     }
812     
813     /*
814      * Remove a file after making sure there are no pending writes,
815      * and after checking that the file has only one user. 
816      *
817      * We invalidate the attribute cache and free the inode prior to the operation
818      * to avoid possible races if the server reuses the inode.
819      */
820     static int nfs_safe_remove(struct dentry *dentry)
821     {
822     	struct inode *dir = dentry->d_parent->d_inode;
823     	struct inode *inode = dentry->d_inode;
824     	int error = -EBUSY, rehash = 0;
825     		
826     	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
827     		dentry->d_parent->d_name.name, dentry->d_name.name);
828     
829     	/*
830     	 * Unhash the dentry while we remove the file ...
831     	 */
832     	if (!d_unhashed(dentry)) {
833     		d_drop(dentry);
834     		rehash = 1;
835     	}
836     	if (atomic_read(&dentry->d_count) > 1) {
837     #ifdef NFS_PARANOIA
838     		printk("nfs_safe_remove: %s/%s busy, d_count=%d\n",
839     			dentry->d_parent->d_name.name, dentry->d_name.name,
840     			atomic_read(&dentry->d_count));
841     #endif
842     		goto out;
843     	}
844     
845     	/* If the dentry was sillyrenamed, we simply call d_delete() */
846     	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
847     		error = 0;
848     		goto out_delete;
849     	}
850     
851     	nfs_zap_caches(dir);
852     	if (inode)
853     		NFS_CACHEINV(inode);
854     	error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
855     	if (error < 0)
856     		goto out;
857     	if (inode)
858     		inode->i_nlink--;
859     
860      out_delete:
861     	/*
862     	 * Free the inode
863     	 */
864     	d_delete(dentry);
865     out:
866     	if (rehash)
867     		d_rehash(dentry);
868     	return error;
869     }
870     
871     /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
872      *  belongs to an active ".nfs..." file and we return -EBUSY.
873      *
874      *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
875      */
876     static int nfs_unlink(struct inode *dir, struct dentry *dentry)
877     {
878     	int error;
879     
880     	dfprintk(VFS, "NFS: unlink(%x/%ld, %s)\n",
881     		dir->i_dev, dir->i_ino, dentry->d_name.name);
882     
883     	error = nfs_sillyrename(dir, dentry);
884     	if (error && error != -EBUSY) {
885     		error = nfs_safe_remove(dentry);
886     		if (!error) {
887     			nfs_renew_times(dentry);
888     		}
889     	}
890     	return error;
891     }
892     
893     static int
894     nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
895     {
896     	struct iattr attr;
897     	struct nfs_fattr sym_attr;
898     	struct nfs_fh sym_fh;
899     	struct qstr qsymname;
900     	unsigned int maxlen;
901     	int error;
902     
903     	dfprintk(VFS, "NFS: symlink(%x/%ld, %s, %s)\n",
904     		dir->i_dev, dir->i_ino, dentry->d_name.name, symname);
905     
906     	error = -ENAMETOOLONG;
907     	maxlen = (NFS_PROTO(dir)->version==2) ? NFS2_MAXPATHLEN : NFS3_MAXPATHLEN;
908     	if (strlen(symname) > maxlen)
909     		goto out;
910     
911     #ifdef NFS_PARANOIA
912     if (dentry->d_inode)
913     printk("nfs_proc_symlink: %s/%s not negative!\n",
914     dentry->d_parent->d_name.name, dentry->d_name.name);
915     #endif
916     	/*
917     	 * Fill in the sattr for the call.
918      	 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
919     	 */
920     	attr.ia_valid = ATTR_MODE;
921     	attr.ia_mode = S_IFLNK | S_IRWXUGO;
922     
923     	qsymname.name = symname;
924     	qsymname.len  = strlen(symname);
925     
926     	nfs_zap_caches(dir);
927     	error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
928     					  &attr, &sym_fh, &sym_attr);
929     	if (!error && sym_fh.size != 0 && (sym_attr.valid & NFS_ATTR_FATTR)) {
930     		error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
931     	} else {
932     		if (error == -EEXIST)
933     			printk("nfs_proc_symlink: %s/%s already exists??\n",
934     			       dentry->d_parent->d_name.name, dentry->d_name.name);
935     		d_drop(dentry);
936     	}
937     
938     out:
939     	return error;
940     }
941     
942     static int 
943     nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
944     {
945     	struct inode *inode = old_dentry->d_inode;
946     	int error;
947     
948     	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
949     		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
950     		dentry->d_parent->d_name.name, dentry->d_name.name);
951     
952     	/*
953     	 * Drop the dentry in advance to force a new lookup.
954     	 * Since nfs_proc_link doesn't return a file handle,
955     	 * we can't use the existing dentry.
956     	 */
957     	d_drop(dentry);
958     	nfs_zap_caches(dir);
959     	NFS_CACHEINV(inode);
960     	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
961     	return error;
962     }
963     
964     /*
965      * RENAME
966      * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
967      * different file handle for the same inode after a rename (e.g. when
968      * moving to a different directory). A fail-safe method to do so would
969      * be to look up old_dir/old_name, create a link to new_dir/new_name and
970      * rename the old file using the sillyrename stuff. This way, the original
971      * file in old_dir will go away when the last process iput()s the inode.
972      *
973      * FIXED.
974      * 
975      * It actually works quite well. One needs to have the possibility for
976      * at least one ".nfs..." file in each directory the file ever gets
977      * moved or linked to which happens automagically with the new
978      * implementation that only depends on the dcache stuff instead of
979      * using the inode layer
980      *
981      * Unfortunately, things are a little more complicated than indicated
982      * above. For a cross-directory move, we want to make sure we can get
983      * rid of the old inode after the operation.  This means there must be
984      * no pending writes (if it's a file), and the use count must be 1.
985      * If these conditions are met, we can drop the dentries before doing
986      * the rename.
987      */
988     static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
989     		      struct inode *new_dir, struct dentry *new_dentry)
990     {
991     	struct inode *old_inode = old_dentry->d_inode;
992     	struct inode *new_inode = new_dentry->d_inode;
993     	struct dentry *dentry = NULL, *rehash = NULL;
994     	int error = -EBUSY;
995     
996     	/*
997     	 * To prevent any new references to the target during the rename,
998     	 * we unhash the dentry and free the inode in advance.
999     	 */
1000     	if (!d_unhashed(new_dentry)) {
1001     		d_drop(new_dentry);
1002     		rehash = new_dentry;
1003     	}
1004     
1005     	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
1006     		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1007     		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
1008     		 atomic_read(&new_dentry->d_count));
1009     
1010     	/*
1011     	 * First check whether the target is busy ... we can't
1012     	 * safely do _any_ rename if the target is in use.
1013     	 *
1014     	 * For files, make a copy of the dentry and then do a 
1015     	 * silly-rename. If the silly-rename succeeds, the
1016     	 * copied dentry is hashed and becomes the new target.
1017     	 */
1018     	if (!new_inode)
1019     		goto go_ahead;
1020     	if (S_ISDIR(new_inode->i_mode))
1021     		goto out;
1022     	else if (atomic_read(&new_dentry->d_count) > 1) {
1023     		int err;
1024     		/* copy the target dentry's name */
1025     		dentry = d_alloc(new_dentry->d_parent,
1026     				 &new_dentry->d_name);
1027     		if (!dentry)
1028     			goto out;
1029     
1030     		/* silly-rename the existing target ... */
1031     		err = nfs_sillyrename(new_dir, new_dentry);
1032     		if (!err) {
1033     			new_dentry = rehash = dentry;
1034     			new_inode = NULL;
1035     			/* instantiate the replacement target */
1036     			d_instantiate(new_dentry, NULL);
1037     		}
1038     
1039     		/* dentry still busy? */
1040     		if (atomic_read(&new_dentry->d_count) > 1) {
1041     #ifdef NFS_PARANOIA
1042     			printk("nfs_rename: target %s/%s busy, d_count=%d\n",
1043     			       new_dentry->d_parent->d_name.name,
1044     			       new_dentry->d_name.name,
1045     			       atomic_read(&new_dentry->d_count));
1046     #endif
1047     			goto out;
1048     		}
1049     	}
1050     
1051     go_ahead:
1052     	/*
1053     	 * ... prune child dentries and writebacks if needed.
1054     	 */
1055     	if (atomic_read(&old_dentry->d_count) > 1) {
1056     		nfs_wb_all(old_inode);
1057     		shrink_dcache_parent(old_dentry);
1058     	}
1059     
1060     	if (new_inode)
1061     		d_delete(new_dentry);
1062     
1063     	nfs_zap_caches(new_dir);
1064     	nfs_zap_caches(old_dir);
1065     	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
1066     					   new_dir, &new_dentry->d_name);
1067     out:
1068     	if (rehash)
1069     		d_rehash(rehash);
1070     	if (!error && !S_ISDIR(old_inode->i_mode))
1071     		d_move(old_dentry, new_dentry);
1072     
1073     	/* new dentry created? */
1074     	if (dentry)
1075     		dput(dentry);
1076     	return error;
1077     }
1078     
1079     int
1080     nfs_permission(struct inode *inode, int mask)
1081     {
1082     	int			error = vfs_permission(inode, mask);
1083     
1084     	if (!NFS_PROTO(inode)->access)
1085     		goto out;
1086     
1087     	if (error == -EROFS)
1088     		goto out;
1089     
1090     	/*
1091     	 * Trust UNIX mode bits except:
1092     	 *
1093     	 * 1) When override capabilities may have been invoked
1094     	 * 2) When root squashing may be involved
1095     	 * 3) When ACLs may overturn a negative answer */
1096     	if (!capable(CAP_DAC_OVERRIDE) && !capable(CAP_DAC_READ_SEARCH)
1097     	    && (current->fsuid != 0) && (current->fsgid != 0)
1098     	    && error != -EACCES)
1099     		goto out;
1100     
1101     	error = NFS_PROTO(inode)->access(inode, mask, 0);
1102     
1103     	if (error == -EACCES && NFS_CLIENT(inode)->cl_droppriv &&
1104     	    current->uid != 0 && current->gid != 0 &&
1105     	    (current->fsuid != current->uid || current->fsgid != current->gid))
1106     		error = NFS_PROTO(inode)->access(inode, mask, 1);
1107     
1108      out:
1109     	return error;
1110     }
1111     
1112     /*
1113      * Local variables:
1114      *  version-control: t
1115      *  kept-new-versions: 5
1116      * End:
1117      */
1118