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

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

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.