49ce7ff26095b32e14ed186ab9b24307ec24c010
[linux-2.6.git] / fs / namei.c
1 /*
2  *  linux/fs/namei.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 /*
8  * Some corrections by tytso.
9  */
10
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12  * lookup logic.
13  */
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15  */
16
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/fs.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/namei.h>
35 #include <linux/proc_fs.h>
36 #include <linux/vserver/inode.h>
37 #include <linux/vserver/debug.h>
38 #include <asm/namei.h>
39 #include <asm/uaccess.h>
40
41 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
42
43 /* [Feb-1997 T. Schoebel-Theuer]
44  * Fundamental changes in the pathname lookup mechanisms (namei)
45  * were necessary because of omirr.  The reason is that omirr needs
46  * to know the _real_ pathname, not the user-supplied one, in case
47  * of symlinks (and also when transname replacements occur).
48  *
49  * The new code replaces the old recursive symlink resolution with
50  * an iterative one (in case of non-nested symlink chains).  It does
51  * this with calls to <fs>_follow_link().
52  * As a side effect, dir_namei(), _namei() and follow_link() are now 
53  * replaced with a single function lookup_dentry() that can handle all 
54  * the special cases of the former code.
55  *
56  * With the new dcache, the pathname is stored at each inode, at least as
57  * long as the refcount of the inode is positive.  As a side effect, the
58  * size of the dcache depends on the inode cache and thus is dynamic.
59  *
60  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61  * resolution to correspond with current state of the code.
62  *
63  * Note that the symlink resolution is not *completely* iterative.
64  * There is still a significant amount of tail- and mid- recursion in
65  * the algorithm.  Also, note that <fs>_readlink() is not used in
66  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67  * may return different results than <fs>_follow_link().  Many virtual
68  * filesystems (including /proc) exhibit this behavior.
69  */
70
71 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73  * and the name already exists in form of a symlink, try to create the new
74  * name indicated by the symlink. The old code always complained that the
75  * name already exists, due to not following the symlink even if its target
76  * is nonexistent.  The new semantics affects also mknod() and link() when
77  * the name is a symlink pointing to a non-existant name.
78  *
79  * I don't know which semantics is the right one, since I have no access
80  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82  * "old" one. Personally, I think the new semantics is much more logical.
83  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84  * file does succeed in both HP-UX and SunOs, but not in Solaris
85  * and in the old Linux semantics.
86  */
87
88 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89  * semantics.  See the comments in "open_namei" and "do_link" below.
90  *
91  * [10-Sep-98 Alan Modra] Another symlink change.
92  */
93
94 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95  *      inside the path - always follow.
96  *      in the last component in creation/removal/renaming - never follow.
97  *      if LOOKUP_FOLLOW passed - follow.
98  *      if the pathname has trailing slashes - follow.
99  *      otherwise - don't follow.
100  * (applied in that order).
101  *
102  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104  * During the 2.4 we need to fix the userland stuff depending on it -
105  * hopefully we will be able to get rid of that wart in 2.5. So far only
106  * XEmacs seems to be relying on it...
107  */
108 /*
109  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
110  * implemented.  Let's see if raised priority of ->s_vfs_rename_sem gives
111  * any extra contention...
112  */
113
114 /* In order to reduce some races, while at the same time doing additional
115  * checking and hopefully speeding things up, we copy filenames to the
116  * kernel data space before using them..
117  *
118  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119  * PATH_MAX includes the nul terminator --RR.
120  */
121 static int do_getname(const char __user *filename, char *page)
122 {
123         int retval;
124         unsigned long len = PATH_MAX;
125
126         if (!segment_eq(get_fs(), KERNEL_DS)) {
127                 if ((unsigned long) filename >= TASK_SIZE)
128                         return -EFAULT;
129                 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
130                         len = TASK_SIZE - (unsigned long) filename;
131         }
132
133         retval = strncpy_from_user(page, filename, len);
134         if (retval > 0) {
135                 if (retval < len)
136                         return 0;
137                 return -ENAMETOOLONG;
138         } else if (!retval)
139                 retval = -ENOENT;
140         return retval;
141 }
142
143 char * getname(const char __user * filename)
144 {
145         char *tmp, *result;
146
147         result = ERR_PTR(-ENOMEM);
148         tmp = __getname();
149         if (tmp)  {
150                 int retval = do_getname(filename, tmp);
151
152                 result = tmp;
153                 if (retval < 0) {
154                         __putname(tmp);
155                         result = ERR_PTR(retval);
156                 }
157         }
158         audit_getname(result);
159         return result;
160 }
161
162 #ifdef CONFIG_AUDITSYSCALL
163 void putname(const char *name)
164 {
165         if (unlikely(current->audit_context))
166                 audit_putname(name);
167         else
168                 __putname(name);
169 }
170 EXPORT_SYMBOL(putname);
171 #endif
172
173
174 /**
175  * generic_permission  -  check for access rights on a Posix-like filesystem
176  * @inode:      inode to check access rights for
177  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
178  * @check_acl:  optional callback to check for Posix ACLs
179  *
180  * Used to check for read/write/execute permissions on a file.
181  * We use "fsuid" for this, letting us set arbitrary permissions
182  * for filesystem access without changing the "normal" uids which
183  * are used for other things..
184  */
185 int generic_permission(struct inode *inode, int mask,
186                 int (*check_acl)(struct inode *inode, int mask))
187 {
188         umode_t                 mode = inode->i_mode;
189
190         if (current->fsuid == inode->i_uid)
191                 mode >>= 6;
192         else {
193                 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
194                         int error = check_acl(inode, mask);
195                         if (error == -EACCES)
196                                 goto check_capabilities;
197                         else if (error != -EAGAIN)
198                                 return error;
199                 }
200
201                 if (in_group_p(inode->i_gid))
202                         mode >>= 3;
203         }
204
205         /*
206          * If the DACs are ok we don't need any capability check.
207          */
208         if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
209                 return 0;
210
211  check_capabilities:
212         /*
213          * Read/write DACs are always overridable.
214          * Executable DACs are overridable if at least one exec bit is set.
215          */
216         if (!(mask & MAY_EXEC) ||
217             (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
218                 if (capable(CAP_DAC_OVERRIDE))
219                         return 0;
220
221         /*
222          * Searching includes executable on directories, else just read.
223          */
224         if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
225                 if (capable(CAP_DAC_READ_SEARCH))
226                         return 0;
227
228         return -EACCES;
229 }
230
231 static inline int vx_barrier(struct inode *inode)
232 {
233         if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN|VX_IDENT)) {
234                 vxwprintk(1, "xid=%d did hit the barrier.",
235                         vx_current_xid());
236                 return 1;
237         }
238         return 0;
239 }
240
241 static inline int xid_permission(struct inode *inode, int mask, struct nameidata *nd)
242 {
243         if (vx_barrier(inode))
244                 return -EACCES;
245         if (inode->i_xid == 0)
246                 return 0;
247         if (vx_check(inode->i_xid, VX_ADMIN|VX_WATCH|VX_IDENT))
248                 return 0;
249
250         vxwprintk(1, "xid=%d denied access to %p[#%d,%lu] Â»%s«.",
251                 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
252                 vxd_cond_path(nd));
253         return -EACCES;
254 }
255
256 int permission(struct inode *inode, int mask, struct nameidata *nd)
257 {
258         int retval, submask;
259
260         if (mask & MAY_WRITE) {
261                 umode_t mode = inode->i_mode;
262
263                 /*
264                  * Nobody gets write access to a read-only fs.
265                  */
266                 if ((IS_RDONLY(inode) || (nd && MNT_IS_RDONLY(nd->mnt))) &&
267                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
268                         return -EROFS;
269
270                 /*
271                  * Nobody gets write access to an immutable file.
272                  */
273                 if (IS_IMMUTABLE(inode))
274                         return -EACCES;
275         }
276
277
278         /* Ordinary permission routines do not understand MAY_APPEND. */
279         submask = mask & ~MAY_APPEND;
280         if ((retval = xid_permission(inode, mask, nd)))
281                 return retval;
282         if (inode->i_op && inode->i_op->permission)
283                 retval = inode->i_op->permission(inode, submask, nd);
284         else
285                 retval = generic_permission(inode, submask, NULL);
286         if (retval)
287                 return retval;
288
289         return security_inode_permission(inode, mask, nd);
290 }
291
292 /**
293  * vfs_permission  -  check for access rights to a given path
294  * @nd:         lookup result that describes the path
295  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
296  *
297  * Used to check for read/write/execute permissions on a path.
298  * We use "fsuid" for this, letting us set arbitrary permissions
299  * for filesystem access without changing the "normal" uids which
300  * are used for other things.
301  */
302 int vfs_permission(struct nameidata *nd, int mask)
303 {
304         return permission(nd->dentry->d_inode, mask, nd);
305 }
306
307 /**
308  * file_permission  -  check for additional access rights to a given file
309  * @file:       file to check access rights for
310  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
311  *
312  * Used to check for read/write/execute permissions on an already opened
313  * file.
314  *
315  * Note:
316  *      Do not use this function in new code.  All access checks should
317  *      be done using vfs_permission().
318  */
319 int file_permission(struct file *file, int mask)
320 {
321         return permission(file->f_dentry->d_inode, mask, NULL);
322 }
323
324 /*
325  * get_write_access() gets write permission for a file.
326  * put_write_access() releases this write permission.
327  * This is used for regular files.
328  * We cannot support write (and maybe mmap read-write shared) accesses and
329  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
330  * can have the following values:
331  * 0: no writers, no VM_DENYWRITE mappings
332  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
333  * > 0: (i_writecount) users are writing to the file.
334  *
335  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
336  * except for the cases where we don't hold i_writecount yet. Then we need to
337  * use {get,deny}_write_access() - these functions check the sign and refuse
338  * to do the change if sign is wrong. Exclusion between them is provided by
339  * the inode->i_lock spinlock.
340  */
341
342 int get_write_access(struct inode * inode)
343 {
344         spin_lock(&inode->i_lock);
345         if (atomic_read(&inode->i_writecount) < 0) {
346                 spin_unlock(&inode->i_lock);
347                 return -ETXTBSY;
348         }
349         atomic_inc(&inode->i_writecount);
350         spin_unlock(&inode->i_lock);
351
352         return 0;
353 }
354
355 int deny_write_access(struct file * file)
356 {
357         struct inode *inode = file->f_dentry->d_inode;
358
359         spin_lock(&inode->i_lock);
360         if (atomic_read(&inode->i_writecount) > 0) {
361                 spin_unlock(&inode->i_lock);
362                 return -ETXTBSY;
363         }
364         atomic_dec(&inode->i_writecount);
365         spin_unlock(&inode->i_lock);
366
367         return 0;
368 }
369
370 void path_release(struct nameidata *nd)
371 {
372         dput(nd->dentry);
373         mntput(nd->mnt);
374 }
375
376 /*
377  * umount() mustn't call path_release()/mntput() as that would clear
378  * mnt_expiry_mark
379  */
380 void path_release_on_umount(struct nameidata *nd)
381 {
382         dput(nd->dentry);
383         mntput_no_expire(nd->mnt);
384 }
385
386 /**
387  * release_open_intent - free up open intent resources
388  * @nd: pointer to nameidata
389  */
390 void release_open_intent(struct nameidata *nd)
391 {
392         if (nd->intent.open.file->f_dentry == NULL)
393                 put_filp(nd->intent.open.file);
394         else
395                 fput(nd->intent.open.file);
396 }
397
398 /*
399  * Internal lookup() using the new generic dcache.
400  * SMP-safe
401  */
402 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
403 {
404         struct dentry * dentry = __d_lookup(parent, name);
405
406         /* lockess __d_lookup may fail due to concurrent d_move() 
407          * in some unrelated directory, so try with d_lookup
408          */
409         if (!dentry)
410                 dentry = d_lookup(parent, name);
411
412         if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
413                 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
414                         dput(dentry);
415                         dentry = NULL;
416                 }
417         }
418         return dentry;
419 }
420
421 /*
422  * Short-cut version of permission(), for calling by
423  * path_walk(), when dcache lock is held.  Combines parts
424  * of permission() and generic_permission(), and tests ONLY for
425  * MAY_EXEC permission.
426  *
427  * If appropriate, check DAC only.  If not appropriate, or
428  * short-cut DAC fails, then call permission() to do more
429  * complete permission check.
430  */
431 static int exec_permission_lite(struct inode *inode,
432                                        struct nameidata *nd)
433 {
434         umode_t mode = inode->i_mode;
435
436         if (vx_barrier(inode))
437                 return -EACCES;
438         if (inode->i_op && inode->i_op->permission)
439                 return -EAGAIN;
440
441         if (current->fsuid == inode->i_uid)
442                 mode >>= 6;
443         else if (in_group_p(inode->i_gid))
444                 mode >>= 3;
445
446         if (mode & MAY_EXEC)
447                 goto ok;
448
449         if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
450                 goto ok;
451
452         if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
453                 goto ok;
454
455         if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
456                 goto ok;
457
458         return -EACCES;
459 ok:
460         return security_inode_permission(inode, MAY_EXEC, nd);
461 }
462
463 /*
464  * This is called when everything else fails, and we actually have
465  * to go to the low-level filesystem to find out what we should do..
466  *
467  * We get the directory semaphore, and after getting that we also
468  * make sure that nobody added the entry to the dcache in the meantime..
469  * SMP-safe
470  */
471 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
472 {
473         struct dentry * result;
474         struct inode *dir = parent->d_inode;
475
476         mutex_lock(&dir->i_mutex);
477         /*
478          * First re-do the cached lookup just in case it was created
479          * while we waited for the directory semaphore..
480          *
481          * FIXME! This could use version numbering or similar to
482          * avoid unnecessary cache lookups.
483          *
484          * The "dcache_lock" is purely to protect the RCU list walker
485          * from concurrent renames at this point (we mustn't get false
486          * negatives from the RCU list walk here, unlike the optimistic
487          * fast walk).
488          *
489          * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
490          */
491         result = d_lookup(parent, name);
492         if (!result) {
493                 struct dentry * dentry = d_alloc(parent, name);
494                 result = ERR_PTR(-ENOMEM);
495                 if (dentry) {
496                         result = dir->i_op->lookup(dir, dentry, nd);
497                         if (result)
498                                 dput(dentry);
499                         else
500                                 result = dentry;
501                 }
502                 mutex_unlock(&dir->i_mutex);
503                 return result;
504         }
505
506         /*
507          * Uhhuh! Nasty case: the cache was re-populated while
508          * we waited on the semaphore. Need to revalidate.
509          */
510         mutex_unlock(&dir->i_mutex);
511         if (result->d_op && result->d_op->d_revalidate) {
512                 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
513                         dput(result);
514                         result = ERR_PTR(-ENOENT);
515                 }
516         }
517         return result;
518 }
519
520 static int __emul_lookup_dentry(const char *, struct nameidata *);
521
522 /* SMP-safe */
523 static __always_inline int
524 walk_init_root(const char *name, struct nameidata *nd)
525 {
526         read_lock(&current->fs->lock);
527         if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
528                 nd->mnt = mntget(current->fs->altrootmnt);
529                 nd->dentry = dget(current->fs->altroot);
530                 read_unlock(&current->fs->lock);
531                 if (__emul_lookup_dentry(name,nd))
532                         return 0;
533                 read_lock(&current->fs->lock);
534         }
535         nd->mnt = mntget(current->fs->rootmnt);
536         nd->dentry = dget(current->fs->root);
537         read_unlock(&current->fs->lock);
538         return 1;
539 }
540
541 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
542 {
543         int res = 0;
544         char *name;
545         if (IS_ERR(link))
546                 goto fail;
547
548         if (*link == '/') {
549                 path_release(nd);
550                 if (!walk_init_root(link, nd))
551                         /* weird __emul_prefix() stuff did it */
552                         goto out;
553         }
554         res = link_path_walk(link, nd);
555 out:
556         if (nd->depth || res || nd->last_type!=LAST_NORM)
557                 return res;
558         /*
559          * If it is an iterative symlinks resolution in open_namei() we
560          * have to copy the last component. And all that crap because of
561          * bloody create() on broken symlinks. Furrfu...
562          */
563         name = __getname();
564         if (unlikely(!name)) {
565                 path_release(nd);
566                 return -ENOMEM;
567         }
568         strcpy(name, nd->last.name);
569         nd->last.name = name;
570         return 0;
571 fail:
572         path_release(nd);
573         return PTR_ERR(link);
574 }
575
576 struct path {
577         struct vfsmount *mnt;
578         struct dentry *dentry;
579 };
580
581 static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
582 {
583         int error;
584         void *cookie;
585         struct dentry *dentry = path->dentry;
586
587         touch_atime(path->mnt, dentry);
588         nd_set_link(nd, NULL);
589
590         if (path->mnt == nd->mnt)
591                 mntget(path->mnt);
592         cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
593         error = PTR_ERR(cookie);
594         if (!IS_ERR(cookie)) {
595                 char *s = nd_get_link(nd);
596                 error = 0;
597                 if (s)
598                         error = __vfs_follow_link(nd, s);
599                 if (dentry->d_inode->i_op->put_link)
600                         dentry->d_inode->i_op->put_link(dentry, nd, cookie);
601         }
602         dput(dentry);
603         mntput(path->mnt);
604
605         return error;
606 }
607
608 static inline void dput_path(struct path *path, struct nameidata *nd)
609 {
610         dput(path->dentry);
611         if (path->mnt != nd->mnt)
612                 mntput(path->mnt);
613 }
614
615 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
616 {
617         dput(nd->dentry);
618         if (nd->mnt != path->mnt)
619                 mntput(nd->mnt);
620         nd->mnt = path->mnt;
621         nd->dentry = path->dentry;
622 }
623
624 /*
625  * This limits recursive symlink follows to 8, while
626  * limiting consecutive symlinks to 40.
627  *
628  * Without that kind of total limit, nasty chains of consecutive
629  * symlinks can cause almost arbitrarily long lookups. 
630  */
631 static inline int do_follow_link(struct path *path, struct nameidata *nd)
632 {
633         int err = -ELOOP;
634         if (current->link_count >= MAX_NESTED_LINKS)
635                 goto loop;
636         if (current->total_link_count >= 40)
637                 goto loop;
638         BUG_ON(nd->depth >= MAX_NESTED_LINKS);
639         cond_resched();
640         err = security_inode_follow_link(path->dentry, nd);
641         if (err)
642                 goto loop;
643         current->link_count++;
644         current->total_link_count++;
645         nd->depth++;
646         err = __do_follow_link(path, nd);
647         current->link_count--;
648         nd->depth--;
649         return err;
650 loop:
651         dput_path(path, nd);
652         path_release(nd);
653         return err;
654 }
655
656 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
657 {
658         struct vfsmount *parent;
659         struct dentry *mountpoint;
660         spin_lock(&vfsmount_lock);
661         parent=(*mnt)->mnt_parent;
662         if (parent == *mnt) {
663                 spin_unlock(&vfsmount_lock);
664                 return 0;
665         }
666         mntget(parent);
667         mountpoint=dget((*mnt)->mnt_mountpoint);
668         spin_unlock(&vfsmount_lock);
669         dput(*dentry);
670         *dentry = mountpoint;
671         mntput(*mnt);
672         *mnt = parent;
673         return 1;
674 }
675
676 /* no need for dcache_lock, as serialization is taken care in
677  * namespace.c
678  */
679 static int __follow_mount(struct path *path)
680 {
681         int res = 0;
682         while (d_mountpoint(path->dentry)) {
683                 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
684                 if (!mounted)
685                         break;
686                 dput(path->dentry);
687                 if (res)
688                         mntput(path->mnt);
689                 path->mnt = mounted;
690                 path->dentry = dget(mounted->mnt_root);
691                 res = 1;
692         }
693         return res;
694 }
695
696 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
697 {
698         while (d_mountpoint(*dentry)) {
699                 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
700                 if (!mounted)
701                         break;
702                 dput(*dentry);
703                 mntput(*mnt);
704                 *mnt = mounted;
705                 *dentry = dget(mounted->mnt_root);
706         }
707 }
708
709 /* no need for dcache_lock, as serialization is taken care in
710  * namespace.c
711  */
712 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
713 {
714         struct vfsmount *mounted;
715
716         mounted = lookup_mnt(*mnt, *dentry);
717         if (mounted) {
718                 dput(*dentry);
719                 mntput(*mnt);
720                 *mnt = mounted;
721                 *dentry = dget(mounted->mnt_root);
722                 return 1;
723         }
724         return 0;
725 }
726
727 static __always_inline void follow_dotdot(struct nameidata *nd)
728 {
729         while(1) {
730                 struct vfsmount *parent;
731                 struct dentry *old = nd->dentry;
732
733                 read_lock(&current->fs->lock);
734                 if (nd->dentry == current->fs->root &&
735                     nd->mnt == current->fs->rootmnt) {
736                         read_unlock(&current->fs->lock);
737                         /* for sane '/' avoid follow_mount() */
738                         return;
739                 }
740                 read_unlock(&current->fs->lock);
741                 spin_lock(&dcache_lock);
742                 if (nd->dentry != nd->mnt->mnt_root) {
743                         nd->dentry = dget(nd->dentry->d_parent);
744                         spin_unlock(&dcache_lock);
745                         dput(old);
746                         break;
747                 }
748                 spin_unlock(&dcache_lock);
749                 spin_lock(&vfsmount_lock);
750                 parent = nd->mnt->mnt_parent;
751                 if (parent == nd->mnt) {
752                         spin_unlock(&vfsmount_lock);
753                         break;
754                 }
755                 mntget(parent);
756                 nd->dentry = dget(nd->mnt->mnt_mountpoint);
757                 spin_unlock(&vfsmount_lock);
758                 dput(old);
759                 mntput(nd->mnt);
760                 nd->mnt = parent;
761         }
762         follow_mount(&nd->mnt, &nd->dentry);
763 }
764
765 /*
766  *  It's more convoluted than I'd like it to be, but... it's still fairly
767  *  small and for now I'd prefer to have fast path as straight as possible.
768  *  It _is_ time-critical.
769  */
770 static int do_lookup(struct nameidata *nd, struct qstr *name,
771                      struct path *path)
772 {
773         struct vfsmount *mnt = nd->mnt;
774         struct dentry *dentry = __d_lookup(nd->dentry, name);
775         struct inode *inode;
776
777         if (!dentry)
778                 goto need_lookup;
779         if (dentry->d_op && dentry->d_op->d_revalidate)
780                 goto need_revalidate;
781         inode = dentry->d_inode;
782         if (!inode)
783                 goto done;
784         if (!vx_check(inode->i_xid, VX_WATCH|VX_ADMIN|VX_HOSTID|VX_IDENT))
785                 goto hidden;
786         if (inode->i_sb->s_magic == PROC_SUPER_MAGIC) {
787                 struct proc_dir_entry *de = PDE(inode);
788
789                 if (de && !vx_hide_check(0, de->vx_flags))
790                         goto hidden;
791         }
792 done:
793         path->mnt = mnt;
794         path->dentry = dentry;
795         __follow_mount(path);
796         return 0;
797 hidden:
798         vxwprintk(1, "xid=%d did lookup hidden %p[#%d,%lu] Â»%s«.",
799                 vx_current_xid(), inode, inode->i_xid, inode->i_ino,
800                 vxd_path(dentry, mnt));
801         dput(dentry);
802         return -ENOENT;
803
804 need_lookup:
805         dentry = real_lookup(nd->dentry, name, nd);
806         if (IS_ERR(dentry))
807                 goto fail;
808         goto done;
809
810 need_revalidate:
811         if (dentry->d_op->d_revalidate(dentry, nd))
812                 goto done;
813         if (d_invalidate(dentry))
814                 goto done;
815         dput(dentry);
816         goto need_lookup;
817
818 fail:
819         return PTR_ERR(dentry);
820 }
821
822 /*
823  * Name resolution.
824  * This is the basic name resolution function, turning a pathname into
825  * the final dentry. We expect 'base' to be positive and a directory.
826  *
827  * Returns 0 and nd will have valid dentry and mnt on success.
828  * Returns error and drops reference to input namei data on failure.
829  */
830 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
831 {
832         struct path next;
833         struct inode *inode;
834         int err;
835         unsigned int lookup_flags = nd->flags;
836         
837         while (*name=='/')
838                 name++;
839         if (!*name)
840                 goto return_reval;
841
842         inode = nd->dentry->d_inode;
843         if (nd->depth)
844                 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
845
846         /* At this point we know we have a real path component. */
847         for(;;) {
848                 unsigned long hash;
849                 struct qstr this;
850                 unsigned int c;
851
852                 nd->flags |= LOOKUP_CONTINUE;
853                 err = exec_permission_lite(inode, nd);
854                 if (err == -EAGAIN)
855                         err = vfs_permission(nd, MAY_EXEC);
856                 if (err)
857                         break;
858
859                 this.name = name;
860                 c = *(const unsigned char *)name;
861
862                 hash = init_name_hash();
863                 do {
864                         name++;
865                         hash = partial_name_hash(c, hash);
866                         c = *(const unsigned char *)name;
867                 } while (c && (c != '/'));
868                 this.len = name - (const char *) this.name;
869                 this.hash = end_name_hash(hash);
870
871                 /* remove trailing slashes? */
872                 if (!c)
873                         goto last_component;
874                 while (*++name == '/');
875                 if (!*name)
876                         goto last_with_slashes;
877
878                 /*
879                  * "." and ".." are special - ".." especially so because it has
880                  * to be able to know about the current root directory and
881                  * parent relationships.
882                  */
883                 if (this.name[0] == '.') switch (this.len) {
884                         default:
885                                 break;
886                         case 2: 
887                                 if (this.name[1] != '.')
888                                         break;
889                                 follow_dotdot(nd);
890                                 inode = nd->dentry->d_inode;
891                                 /* fallthrough */
892                         case 1:
893                                 continue;
894                 }
895                 /*
896                  * See if the low-level filesystem might want
897                  * to use its own hash..
898                  */
899                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
900                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
901                         if (err < 0)
902                                 break;
903                 }
904                 /* This does the actual lookups.. */
905                 err = do_lookup(nd, &this, &next);
906                 if (err)
907                         break;
908
909                 err = -ENOENT;
910                 inode = next.dentry->d_inode;
911                 if (!inode)
912                         goto out_dput;
913                 err = -ENOTDIR; 
914                 if (!inode->i_op)
915                         goto out_dput;
916
917                 if (inode->i_op->follow_link) {
918                         err = do_follow_link(&next, nd);
919                         if (err)
920                                 goto return_err;
921                         err = -ENOENT;
922                         inode = nd->dentry->d_inode;
923                         if (!inode)
924                                 break;
925                         err = -ENOTDIR; 
926                         if (!inode->i_op)
927                                 break;
928                 } else
929                         path_to_nameidata(&next, nd);
930                 err = -ENOTDIR; 
931                 if (!inode->i_op->lookup)
932                         break;
933                 continue;
934                 /* here ends the main loop */
935
936 last_with_slashes:
937                 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
938 last_component:
939                 /* Clear LOOKUP_CONTINUE iff it was previously unset */
940                 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
941                 if (lookup_flags & LOOKUP_PARENT)
942                         goto lookup_parent;
943                 if (this.name[0] == '.') switch (this.len) {
944                         default:
945                                 break;
946                         case 2: 
947                                 if (this.name[1] != '.')
948                                         break;
949                                 follow_dotdot(nd);
950                                 inode = nd->dentry->d_inode;
951                                 /* fallthrough */
952                         case 1:
953                                 goto return_reval;
954                 }
955                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
956                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);
957                         if (err < 0)
958                                 break;
959                 }
960                 err = do_lookup(nd, &this, &next);
961                 if (err)
962                         break;
963                 inode = next.dentry->d_inode;
964                 if ((lookup_flags & LOOKUP_FOLLOW)
965                     && inode && inode->i_op && inode->i_op->follow_link) {
966                         err = do_follow_link(&next, nd);
967                         if (err)
968                                 goto return_err;
969                         inode = nd->dentry->d_inode;
970                 } else
971                         path_to_nameidata(&next, nd);
972                 err = -ENOENT;
973                 if (!inode)
974                         break;
975                 if (lookup_flags & LOOKUP_DIRECTORY) {
976                         err = -ENOTDIR; 
977                         if (!inode->i_op || !inode->i_op->lookup)
978                                 break;
979                 }
980                 goto return_base;
981 lookup_parent:
982                 nd->last = this;
983                 nd->last_type = LAST_NORM;
984                 if (this.name[0] != '.')
985                         goto return_base;
986                 if (this.len == 1)
987                         nd->last_type = LAST_DOT;
988                 else if (this.len == 2 && this.name[1] == '.')
989                         nd->last_type = LAST_DOTDOT;
990                 else
991                         goto return_base;
992 return_reval:
993                 /*
994                  * We bypassed the ordinary revalidation routines.
995                  * We may need to check the cached dentry for staleness.
996                  */
997                 if (nd->dentry && nd->dentry->d_sb &&
998                     (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
999                         err = -ESTALE;
1000                         /* Note: we do not d_invalidate() */
1001                         if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
1002                                 break;
1003                 }
1004 return_base:
1005                 return 0;
1006 out_dput:
1007                 dput_path(&next, nd);
1008                 break;
1009         }
1010         path_release(nd);
1011 return_err:
1012         return err;
1013 }
1014
1015 /*
1016  * Wrapper to retry pathname resolution whenever the underlying
1017  * file system returns an ESTALE.
1018  *
1019  * Retry the whole path once, forcing real lookup requests
1020  * instead of relying on the dcache.
1021  */
1022 int fastcall link_path_walk(const char *name, struct nameidata *nd)
1023 {
1024         struct nameidata save = *nd;
1025         int result;
1026
1027         /* make sure the stuff we saved doesn't go away */
1028         dget(save.dentry);
1029         mntget(save.mnt);
1030
1031         result = __link_path_walk(name, nd);
1032         if (result == -ESTALE) {
1033                 *nd = save;
1034                 dget(nd->dentry);
1035                 mntget(nd->mnt);
1036                 nd->flags |= LOOKUP_REVAL;
1037                 result = __link_path_walk(name, nd);
1038         }
1039
1040         dput(save.dentry);
1041         mntput(save.mnt);
1042
1043         return result;
1044 }
1045
1046 int fastcall path_walk(const char * name, struct nameidata *nd)
1047 {
1048         current->total_link_count = 0;
1049         return link_path_walk(name, nd);
1050 }
1051
1052 /* 
1053  * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1054  * everything is done. Returns 0 and drops input nd, if lookup failed;
1055  */
1056 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1057 {
1058         if (path_walk(name, nd))
1059                 return 0;               /* something went wrong... */
1060
1061         if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1062                 struct dentry *old_dentry = nd->dentry;
1063                 struct vfsmount *old_mnt = nd->mnt;
1064                 struct qstr last = nd->last;
1065                 int last_type = nd->last_type;
1066                 /*
1067                  * NAME was not found in alternate root or it's a directory.  Try to find
1068                  * it in the normal root:
1069                  */
1070                 nd->last_type = LAST_ROOT;
1071                 read_lock(&current->fs->lock);
1072                 nd->mnt = mntget(current->fs->rootmnt);
1073                 nd->dentry = dget(current->fs->root);
1074                 read_unlock(&current->fs->lock);
1075                 if (path_walk(name, nd) == 0) {
1076                         if (nd->dentry->d_inode) {
1077                                 dput(old_dentry);
1078                                 mntput(old_mnt);
1079                                 return 1;
1080                         }
1081                         path_release(nd);
1082                 }
1083                 nd->dentry = old_dentry;
1084                 nd->mnt = old_mnt;
1085                 nd->last = last;
1086                 nd->last_type = last_type;
1087         }
1088         return 1;
1089 }
1090
1091 void set_fs_altroot(void)
1092 {
1093         char *emul = __emul_prefix();
1094         struct nameidata nd;
1095         struct vfsmount *mnt = NULL, *oldmnt;
1096         struct dentry *dentry = NULL, *olddentry;
1097         int err;
1098
1099         if (!emul)
1100                 goto set_it;
1101         err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1102         if (!err) {
1103                 mnt = nd.mnt;
1104                 dentry = nd.dentry;
1105         }
1106 set_it:
1107         write_lock(&current->fs->lock);
1108         oldmnt = current->fs->altrootmnt;
1109         olddentry = current->fs->altroot;
1110         current->fs->altrootmnt = mnt;
1111         current->fs->altroot = dentry;
1112         write_unlock(&current->fs->lock);
1113         if (olddentry) {
1114                 dput(olddentry);
1115                 mntput(oldmnt);
1116         }
1117 }
1118
1119 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1120 static int fastcall do_path_lookup(int dfd, const char *name,
1121                                 unsigned int flags, struct nameidata *nd)
1122 {
1123         int retval = 0;
1124         int fput_needed;
1125         struct file *file;
1126
1127         nd->last_type = LAST_ROOT; /* if there are only slashes... */
1128         nd->flags = flags;
1129         nd->depth = 0;
1130
1131         if (*name=='/') {
1132                 read_lock(&current->fs->lock);
1133                 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1134                         nd->mnt = mntget(current->fs->altrootmnt);
1135                         nd->dentry = dget(current->fs->altroot);
1136                         read_unlock(&current->fs->lock);
1137                         if (__emul_lookup_dentry(name,nd))
1138                                 goto out; /* found in altroot */
1139                         read_lock(&current->fs->lock);
1140                 }
1141                 nd->mnt = mntget(current->fs->rootmnt);
1142                 nd->dentry = dget(current->fs->root);
1143                 read_unlock(&current->fs->lock);
1144         } else if (dfd == AT_FDCWD) {
1145                 read_lock(&current->fs->lock);
1146                 nd->mnt = mntget(current->fs->pwdmnt);
1147                 nd->dentry = dget(current->fs->pwd);
1148                 read_unlock(&current->fs->lock);
1149         } else {
1150                 struct dentry *dentry;
1151
1152                 file = fget_light(dfd, &fput_needed);
1153                 retval = -EBADF;
1154                 if (!file)
1155                         goto out_fail;
1156
1157                 dentry = file->f_dentry;
1158
1159                 retval = -ENOTDIR;
1160                 if (!S_ISDIR(dentry->d_inode->i_mode))
1161                         goto fput_fail;
1162
1163                 retval = file_permission(file, MAY_EXEC);
1164                 if (retval)
1165                         goto fput_fail;
1166
1167                 nd->mnt = mntget(file->f_vfsmnt);
1168                 nd->dentry = dget(dentry);
1169
1170                 fput_light(file, fput_needed);
1171         }
1172         current->total_link_count = 0;
1173         retval = link_path_walk(name, nd);
1174 out:
1175         if (likely(retval == 0)) {
1176                 if (unlikely(current->audit_context && nd && nd->dentry &&
1177                                 nd->dentry->d_inode))
1178                 audit_inode(name, nd->dentry->d_inode, flags);
1179         }
1180 out_fail:
1181         return retval;
1182
1183 fput_fail:
1184         fput_light(file, fput_needed);
1185         goto out_fail;
1186 }
1187
1188 int fastcall path_lookup(const char *name, unsigned int flags,
1189                         struct nameidata *nd)
1190 {
1191         return do_path_lookup(AT_FDCWD, name, flags, nd);
1192 }
1193
1194 static int __path_lookup_intent_open(int dfd, const char *name,
1195                 unsigned int lookup_flags, struct nameidata *nd,
1196                 int open_flags, int create_mode)
1197 {
1198         struct file *filp = get_empty_filp();
1199         int err;
1200
1201         if (filp == NULL)
1202                 return -ENFILE;
1203         nd->intent.open.file = filp;
1204         nd->intent.open.flags = open_flags;
1205         nd->intent.open.create_mode = create_mode;
1206         err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1207         if (IS_ERR(nd->intent.open.file)) {
1208                 if (err == 0) {
1209                         err = PTR_ERR(nd->intent.open.file);
1210                         path_release(nd);
1211                 }
1212         } else if (err != 0)
1213                 release_open_intent(nd);
1214         return err;
1215 }
1216
1217 /**
1218  * path_lookup_open - lookup a file path with open intent
1219  * @dfd: the directory to use as base, or AT_FDCWD
1220  * @name: pointer to file name
1221  * @lookup_flags: lookup intent flags
1222  * @nd: pointer to nameidata
1223  * @open_flags: open intent flags
1224  */
1225 int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1226                 struct nameidata *nd, int open_flags)
1227 {
1228         return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
1229                         open_flags, 0);
1230 }
1231
1232 /**
1233  * path_lookup_create - lookup a file path with open + create intent
1234  * @dfd: the directory to use as base, or AT_FDCWD
1235  * @name: pointer to file name
1236  * @lookup_flags: lookup intent flags
1237  * @nd: pointer to nameidata
1238  * @open_flags: open intent flags
1239  * @create_mode: create intent flags
1240  */
1241 static int path_lookup_create(int dfd, const char *name,
1242                               unsigned int lookup_flags, struct nameidata *nd,
1243                               int open_flags, int create_mode)
1244 {
1245         return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1246                         nd, open_flags, create_mode);
1247 }
1248
1249 int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1250                 struct nameidata *nd, int open_flags)
1251 {
1252         char *tmp = getname(name);
1253         int err = PTR_ERR(tmp);
1254
1255         if (!IS_ERR(tmp)) {
1256                 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
1257                 putname(tmp);
1258         }
1259         return err;
1260 }
1261
1262 /*
1263  * Restricted form of lookup. Doesn't follow links, single-component only,
1264  * needs parent already locked. Doesn't follow mounts.
1265  * SMP-safe.
1266  */
1267 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1268 {
1269         struct dentry * dentry;
1270         struct inode *inode;
1271         int err;
1272
1273         inode = base->d_inode;
1274         err = permission(inode, MAY_EXEC, nd);
1275         dentry = ERR_PTR(err);
1276         if (err)
1277                 goto out;
1278
1279         /*
1280          * See if the low-level filesystem might want
1281          * to use its own hash..
1282          */
1283         if (base->d_op && base->d_op->d_hash) {
1284                 err = base->d_op->d_hash(base, name);
1285                 dentry = ERR_PTR(err);
1286                 if (err < 0)
1287                         goto out;
1288         }
1289
1290         dentry = cached_lookup(base, name, nd);
1291         if (!dentry) {
1292                 struct dentry *new = d_alloc(base, name);
1293                 dentry = ERR_PTR(-ENOMEM);
1294                 if (!new)
1295                         goto out;
1296                 dentry = inode->i_op->lookup(inode, new, nd);
1297                 if (!dentry)
1298                         dentry = new;
1299                 else
1300                         dput(new);
1301         }
1302 out:
1303         return dentry;
1304 }
1305
1306 struct dentry * lookup_hash(struct nameidata *nd)
1307 {
1308         return __lookup_hash(&nd->last, nd->dentry, nd);
1309 }
1310
1311 /* SMP-safe */
1312 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1313 {
1314         unsigned long hash;
1315         struct qstr this;
1316         unsigned int c;
1317
1318         this.name = name;
1319         this.len = len;
1320         if (!len)
1321                 goto access;
1322
1323         hash = init_name_hash();
1324         while (len--) {
1325                 c = *(const unsigned char *)name++;
1326                 if (c == '/' || c == '\0')
1327                         goto access;
1328                 hash = partial_name_hash(c, hash);
1329         }
1330         this.hash = end_name_hash(hash);
1331
1332         return __lookup_hash(&this, base, NULL);
1333 access:
1334         return ERR_PTR(-EACCES);
1335 }
1336
1337 /*
1338  *      namei()
1339  *
1340  * is used by most simple commands to get the inode of a specified name.
1341  * Open, link etc use their own routines, but this is enough for things
1342  * like 'chmod' etc.
1343  *
1344  * namei exists in two versions: namei/lnamei. The only difference is
1345  * that namei follows links, while lnamei does not.
1346  * SMP-safe
1347  */
1348 int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1349                             struct nameidata *nd)
1350 {
1351         char *tmp = getname(name);
1352         int err = PTR_ERR(tmp);
1353
1354         if (!IS_ERR(tmp)) {
1355                 err = do_path_lookup(dfd, tmp, flags, nd);
1356                 putname(tmp);
1357         }
1358         return err;
1359 }
1360
1361 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1362 {
1363         return __user_walk_fd(AT_FDCWD, name, flags, nd);
1364 }
1365
1366 /*
1367  * It's inline, so penalty for filesystems that don't use sticky bit is
1368  * minimal.
1369  */
1370 static inline int check_sticky(struct inode *dir, struct inode *inode)
1371 {
1372         if (!(dir->i_mode & S_ISVTX))
1373                 return 0;
1374         if (inode->i_uid == current->fsuid)
1375                 return 0;
1376         if (dir->i_uid == current->fsuid)
1377                 return 0;
1378         return !capable(CAP_FOWNER);
1379 }
1380
1381 /*
1382  *      Check whether we can remove a link victim from directory dir, check
1383  *  whether the type of victim is right.
1384  *  1. We can't do it if dir is read-only (done in permission())
1385  *  2. We should have write and exec permissions on dir
1386  *  3. We can't remove anything from append-only dir
1387  *  4. We can't do anything with immutable dir (done in permission())
1388  *  5. If the sticky bit on dir is set we should either
1389  *      a. be owner of dir, or
1390  *      b. be owner of victim, or
1391  *      c. have CAP_FOWNER capability
1392  *  6. If the victim is append-only or immutable we can't do antyhing with
1393  *     links pointing to it.
1394  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1395  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1396  *  9. We can't remove a root or mountpoint.
1397  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1398  *     nfs_async_unlink().
1399  */
1400 static int may_delete(struct inode *dir, struct dentry *victim,
1401         int isdir, struct nameidata *nd)
1402 {
1403         int error;
1404
1405         if (!victim->d_inode)
1406                 return -ENOENT;
1407
1408         BUG_ON(victim->d_parent->d_inode != dir);
1409
1410         error = permission(dir,MAY_WRITE | MAY_EXEC, nd);
1411         if (error)
1412                 return error;
1413         if (IS_APPEND(dir))
1414                 return -EPERM;
1415         if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1416                 IS_IXORUNLINK(victim->d_inode))
1417                 return -EPERM;
1418         if (isdir) {
1419                 if (!S_ISDIR(victim->d_inode->i_mode))
1420                         return -ENOTDIR;
1421                 if (IS_ROOT(victim))
1422                         return -EBUSY;
1423         } else if (S_ISDIR(victim->d_inode->i_mode))
1424                 return -EISDIR;
1425         if (IS_DEADDIR(dir))
1426                 return -ENOENT;
1427         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1428                 return -EBUSY;
1429         return 0;
1430 }
1431
1432 /*      Check whether we can create an object with dentry child in directory
1433  *  dir.
1434  *  1. We can't do it if child already exists (open has special treatment for
1435  *     this case, but since we are inlined it's OK)
1436  *  2. We can't do it if dir is read-only (done in permission())
1437  *  3. We should have write and exec permissions on dir
1438  *  4. We can't do it if dir is immutable (done in permission())
1439  */
1440 static inline int may_create(struct inode *dir, struct dentry *child,
1441                              struct nameidata *nd)
1442 {
1443         if (child->d_inode)
1444                 return -EEXIST;
1445         if (IS_DEADDIR(dir))
1446                 return -ENOENT;
1447         return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1448 }
1449
1450 /* 
1451  * O_DIRECTORY translates into forcing a directory lookup.
1452  */
1453 static inline int lookup_flags(unsigned int f)
1454 {
1455         unsigned long retval = LOOKUP_FOLLOW;
1456
1457         if (f & O_NOFOLLOW)
1458                 retval &= ~LOOKUP_FOLLOW;
1459         
1460         if (f & O_DIRECTORY)
1461                 retval |= LOOKUP_DIRECTORY;
1462
1463         return retval;
1464 }
1465
1466 /*
1467  * p1 and p2 should be directories on the same fs.
1468  */
1469 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1470 {
1471         struct dentry *p;
1472
1473         if (p1 == p2) {
1474                 mutex_lock(&p1->d_inode->i_mutex);
1475                 return NULL;
1476         }
1477
1478         down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1479
1480         for (p = p1; p->d_parent != p; p = p->d_parent) {
1481                 if (p->d_parent == p2) {
1482                         mutex_lock(&p2->d_inode->i_mutex);
1483                         mutex_lock(&p1->d_inode->i_mutex);
1484                         return p;
1485                 }
1486         }
1487
1488         for (p = p2; p->d_parent != p; p = p->d_parent) {
1489                 if (p->d_parent == p1) {
1490                         mutex_lock(&p1->d_inode->i_mutex);
1491                         mutex_lock(&p2->d_inode->i_mutex);
1492                         return p;
1493                 }
1494         }
1495
1496         mutex_lock(&p1->d_inode->i_mutex);
1497         mutex_lock(&p2->d_inode->i_mutex);
1498         return NULL;
1499 }
1500
1501 void unlock_rename(struct dentry *p1, struct dentry *p2)
1502 {
1503         mutex_unlock(&p1->d_inode->i_mutex);
1504         if (p1 != p2) {
1505                 mutex_unlock(&p2->d_inode->i_mutex);
1506                 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1507         }
1508 }
1509
1510 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1511                 struct nameidata *nd)
1512 {
1513         int error = may_create(dir, dentry, nd);
1514
1515         if (error)
1516                 return error;
1517
1518         if (!dir->i_op || !dir->i_op->create)
1519                 return -EACCES; /* shouldn't it be ENOSYS? */
1520         mode &= S_IALLUGO;
1521         mode |= S_IFREG;
1522         error = security_inode_create(dir, dentry, mode);
1523         if (error)
1524                 return error;
1525         DQUOT_INIT(dir);
1526         error = dir->i_op->create(dir, dentry, mode, nd);
1527         if (!error)
1528                 fsnotify_create(dir, dentry->d_name.name);
1529         return error;
1530 }
1531
1532 int may_open(struct nameidata *nd, int acc_mode, int flag)
1533 {
1534         struct dentry *dentry = nd->dentry;
1535         struct inode *inode = dentry->d_inode;
1536         int error;
1537
1538         if (!inode)
1539                 return -ENOENT;
1540
1541         if (S_ISLNK(inode->i_mode))
1542                 return -ELOOP;
1543         
1544         if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1545                 return -EISDIR;
1546
1547         error = vfs_permission(nd, acc_mode);
1548         if (error)
1549                 return error;
1550
1551         /*
1552          * FIFO's, sockets and device files are special: they don't
1553          * actually live on the filesystem itself, and as such you
1554          * can write to them even if the filesystem is read-only.
1555          */
1556         if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1557                 flag &= ~O_TRUNC;
1558         } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1559                 if (nd->mnt->mnt_flags & MNT_NODEV)
1560                         return -EACCES;
1561
1562                 flag &= ~O_TRUNC;
1563         } else if ((IS_RDONLY(inode) || MNT_IS_RDONLY(nd->mnt))
1564                 && (flag & FMODE_WRITE))
1565                 return -EROFS;
1566         /*
1567          * An append-only file must be opened in append mode for writing.
1568          */
1569         if (IS_APPEND(inode)) {
1570                 if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1571                         return -EPERM;
1572                 if (flag & O_TRUNC)
1573                         return -EPERM;
1574         }
1575
1576         /* O_NOATIME can only be set by the owner or superuser */
1577         if (flag & O_NOATIME)
1578                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1579                         return -EPERM;
1580
1581         /*
1582          * Ensure there are no outstanding leases on the file.
1583          */
1584         error = break_lease(inode, flag);
1585         if (error)
1586                 return error;
1587
1588         if (flag & O_TRUNC) {
1589                 error = get_write_access(inode);
1590                 if (error)
1591                         return error;
1592
1593                 /*
1594                  * Refuse to truncate files with mandatory locks held on them.
1595                  */
1596                 error = locks_verify_locked(inode);
1597                 if (!error) {
1598                         DQUOT_INIT(inode);
1599                         
1600                         error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
1601                 }
1602                 put_write_access(inode);
1603                 if (error)
1604                         return error;
1605         } else
1606                 if (flag & FMODE_WRITE)
1607                         DQUOT_INIT(inode);
1608
1609         return 0;
1610 }
1611
1612 /*
1613  *      open_namei()
1614  *
1615  * namei for open - this is in fact almost the whole open-routine.
1616  *
1617  * Note that the low bits of "flag" aren't the same as in the open
1618  * system call - they are 00 - no permissions needed
1619  *                        01 - read permission needed
1620  *                        10 - write permission needed
1621  *                        11 - read/write permissions needed
1622  * which is a lot more logical, and also allows the "no perm" needed
1623  * for symlinks (where the permissions are checked later).
1624  * SMP-safe
1625  */
1626 int open_namei(int dfd, const char *pathname, int flag,
1627                 int mode, struct nameidata *nd)
1628 {
1629         int acc_mode, error;
1630         struct path path;
1631         struct dentry *dir;
1632         int count = 0;
1633
1634         acc_mode = ACC_MODE(flag);
1635
1636         /* O_TRUNC implies we need access checks for write permissions */
1637         if (flag & O_TRUNC)
1638                 acc_mode |= MAY_WRITE;
1639
1640         /* Allow the LSM permission hook to distinguish append 
1641            access from general write access. */
1642         if (flag & O_APPEND)
1643                 acc_mode |= MAY_APPEND;
1644
1645         /*
1646          * The simplest case - just a plain lookup.
1647          */
1648         if (!(flag & O_CREAT)) {
1649                 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1650                                          nd, flag);
1651                 if (error)
1652                         return error;
1653                 goto ok;
1654         }
1655
1656         /*
1657          * Create - we need to know the parent.
1658          */
1659         error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
1660         if (error)
1661                 return error;
1662
1663         /*
1664          * We have the parent and last component. First of all, check
1665          * that we are not asked to creat(2) an obvious directory - that
1666          * will not do.
1667          */
1668         error = -EISDIR;
1669         if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1670                 goto exit;
1671
1672         dir = nd->dentry;
1673         nd->flags &= ~LOOKUP_PARENT;
1674         mutex_lock(&dir->d_inode->i_mutex);
1675         path.dentry = lookup_hash(nd);
1676         path.mnt = nd->mnt;
1677
1678 do_last:
1679         error = PTR_ERR(path.dentry);
1680         if (IS_ERR(path.dentry)) {
1681                 mutex_unlock(&dir->d_inode->i_mutex);
1682                 goto exit;
1683         }
1684
1685         if (IS_ERR(nd->intent.open.file)) {
1686                 mutex_unlock(&dir->d_inode->i_mutex);
1687                 error = PTR_ERR(nd->intent.open.file);
1688                 goto exit_dput;
1689         }
1690
1691         /* Negative dentry, just create the file */
1692         if (!path.dentry->d_inode) {
1693                 if (!IS_POSIXACL(dir->d_inode))
1694                         mode &= ~current->fs->umask;
1695                 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
1696                 mutex_unlock(&dir->d_inode->i_mutex);
1697                 dput(nd->dentry);
1698                 nd->dentry = path.dentry;
1699                 if (error)
1700                         goto exit;
1701                 /* Don't check for write permission, don't truncate */
1702                 acc_mode = 0;
1703                 flag &= ~O_TRUNC;
1704                 goto ok;
1705         }
1706
1707         /*
1708          * It already exists.
1709          */
1710         mutex_unlock(&dir->d_inode->i_mutex);
1711
1712         error = -EEXIST;
1713         if (flag & O_EXCL)
1714                 goto exit_dput;
1715
1716         if (__follow_mount(&path)) {
1717                 error = -ELOOP;
1718                 if (flag & O_NOFOLLOW)
1719                         goto exit_dput;
1720         }
1721         error = -ENOENT;
1722         if (!path.dentry->d_inode)
1723                 goto exit_dput;
1724         if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1725                 goto do_link;
1726
1727         path_to_nameidata(&path, nd);
1728         error = -EISDIR;
1729         if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1730                 goto exit;
1731 ok:
1732         error = may_open(nd, acc_mode, flag);
1733         if (error)
1734                 goto exit;
1735         return 0;
1736
1737 exit_dput:
1738         dput_path(&path, nd);
1739 exit:
1740         if (!IS_ERR(nd->intent.open.file))
1741                 release_open_intent(nd);
1742         path_release(nd);
1743         return error;
1744
1745 do_link:
1746         error = -ELOOP;
1747         if (flag & O_NOFOLLOW)
1748                 goto exit_dput;
1749         /*
1750          * This is subtle. Instead of calling do_follow_link() we do the
1751          * thing by hands. The reason is that this way we have zero link_count
1752          * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1753          * After that we have the parent and last component, i.e.
1754          * we are in the same situation as after the first path_walk().
1755          * Well, almost - if the last component is normal we get its copy
1756          * stored in nd->last.name and we will have to putname() it when we
1757          * are done. Procfs-like symlinks just set LAST_BIND.
1758          */
1759         nd->flags |= LOOKUP_PARENT;
1760         error = security_inode_follow_link(path.dentry, nd);
1761         if (error)
1762                 goto exit_dput;
1763         error = __do_follow_link(&path, nd);
1764         if (error) {
1765                 /* Does someone understand code flow here? Or it is only
1766                  * me so stupid? Anathema to whoever designed this non-sense
1767                  * with "intent.open".
1768                  */
1769                 release_open_intent(nd);
1770                 return error;
1771         }
1772         nd->flags &= ~LOOKUP_PARENT;
1773         if (nd->last_type == LAST_BIND)
1774                 goto ok;
1775         error = -EISDIR;
1776         if (nd->last_type != LAST_NORM)
1777                 goto exit;
1778         if (nd->last.name[nd->last.len]) {
1779                 __putname(nd->last.name);
1780                 goto exit;
1781         }
1782         error = -ELOOP;
1783         if (count++==32) {
1784                 __putname(nd->last.name);
1785                 goto exit;
1786         }
1787         dir = nd->dentry;
1788         mutex_lock(&dir->d_inode->i_mutex);
1789         path.dentry = lookup_hash(nd);
1790         path.mnt = nd->mnt;
1791         __putname(nd->last.name);
1792         goto do_last;
1793 }
1794
1795 /**
1796  * lookup_create - lookup a dentry, creating it if it doesn't exist
1797  * @nd: nameidata info
1798  * @is_dir: directory flag
1799  *
1800  * Simple function to lookup and return a dentry and create it
1801  * if it doesn't exist.  Is SMP-safe.
1802  *
1803  * Returns with nd->dentry->d_inode->i_mutex locked.
1804  */
1805 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1806 {
1807         struct dentry *dentry = ERR_PTR(-EEXIST);
1808
1809         mutex_lock(&nd->dentry->d_inode->i_mutex);
1810         /*
1811          * Yucky last component or no last component at all?
1812          * (foo/., foo/.., /////)
1813          */
1814         if (nd->last_type != LAST_NORM)
1815                 goto fail;
1816         nd->flags &= ~LOOKUP_PARENT;
1817
1818         /*
1819          * Do the final lookup.
1820          */
1821         dentry = lookup_hash(nd);
1822         if (IS_ERR(dentry))
1823                 goto fail;
1824
1825         /*
1826          * Special case - lookup gave negative, but... we had foo/bar/
1827          * From the vfs_mknod() POV we just have a negative dentry -
1828          * all is fine. Let's be bastards - you had / on the end, you've
1829          * been asking for (non-existent) directory. -ENOENT for you.
1830          */
1831         if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1832                 goto enoent;
1833         return dentry;
1834 enoent:
1835         dput(dentry);
1836         dentry = ERR_PTR(-ENOENT);
1837 fail:
1838         return dentry;
1839 }
1840 EXPORT_SYMBOL_GPL(lookup_create);
1841
1842 int vfs_mknod(struct inode *dir, struct dentry *dentry,
1843         int mode, dev_t dev, struct nameidata *nd)
1844 {
1845         int error = may_create(dir, dentry, nd);
1846
1847         if (error)
1848                 return error;
1849
1850         if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1851                 return -EPERM;
1852
1853         if (!dir->i_op || !dir->i_op->mknod)
1854                 return -EPERM;
1855
1856         error = security_inode_mknod(dir, dentry, mode, dev);
1857         if (error)
1858                 return error;
1859
1860         DQUOT_INIT(dir);
1861         error = dir->i_op->mknod(dir, dentry, mode, dev);
1862         if (!error)
1863                 fsnotify_create(dir, dentry->d_name.name);
1864         return error;
1865 }
1866
1867 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1868                                 unsigned dev)
1869 {
1870         int error = 0;
1871         char * tmp;
1872         struct dentry * dentry;
1873         struct nameidata nd;
1874
1875         if (S_ISDIR(mode))
1876                 return -EPERM;
1877         tmp = getname(filename);
1878         if (IS_ERR(tmp))
1879                 return PTR_ERR(tmp);
1880
1881         error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1882         if (error)
1883                 goto out;
1884         dentry = lookup_create(&nd, 0);
1885         error = PTR_ERR(dentry);
1886
1887         if (!IS_POSIXACL(nd.dentry->d_inode))
1888                 mode &= ~current->fs->umask;
1889         if (!IS_ERR(dentry)) {
1890                 switch (mode & S_IFMT) {
1891                 case 0: case S_IFREG:
1892                         error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1893                         break;
1894                 case S_IFCHR: case S_IFBLK:
1895                         error = vfs_mknod(nd.dentry->d_inode, dentry, mode,
1896                                         new_decode_dev(dev), &nd);
1897                         break;
1898                 case S_IFIFO: case S_IFSOCK:
1899                         error = vfs_mknod(nd.dentry->d_inode, dentry, mode,
1900                                         0, &nd);
1901                         break;
1902                 case S_IFDIR:
1903                         error = -EPERM;
1904                         break;
1905                 default:
1906                         error = -EINVAL;
1907                 }
1908                 dput(dentry);
1909         }
1910         mutex_unlock(&nd.dentry->d_inode->i_mutex);
1911         path_release(&nd);
1912 out:
1913         putname(tmp);
1914
1915         return error;
1916 }
1917
1918 asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
1919 {
1920         return sys_mknodat(AT_FDCWD, filename, mode, dev);
1921 }
1922
1923 int vfs_mkdir(struct inode *dir, struct dentry *dentry,
1924         int mode, struct nameidata *nd)
1925 {
1926         int error = may_create(dir, dentry, nd);
1927
1928         if (error)
1929                 return error;
1930
1931         if (!dir->i_op || !dir->i_op->mkdir)
1932                 return -EPERM;
1933
1934         mode &= (S_IRWXUGO|S_ISVTX);
1935         error = security_inode_mkdir(dir, dentry, mode);
1936         if (error)
1937                 return error;
1938
1939         DQUOT_INIT(dir);
1940         error = dir->i_op->mkdir(dir, dentry, mode);
1941         if (!error)
1942                 fsnotify_mkdir(dir, dentry->d_name.name);
1943         return error;
1944 }
1945
1946 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
1947 {
1948         int error = 0;
1949         char * tmp;
1950
1951         tmp = getname(pathname);
1952         error = PTR_ERR(tmp);
1953         if (!IS_ERR(tmp)) {
1954                 struct dentry *dentry;
1955                 struct nameidata nd;
1956
1957                 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
1958                 if (error)
1959                         goto out;
1960                 dentry = lookup_create(&nd, 1);
1961                 error = PTR_ERR(dentry);
1962                 if (!IS_ERR(dentry)) {
1963                         if (!IS_POSIXACL(nd.dentry->d_inode))
1964                                 mode &= ~current->fs->umask;
1965                         error = vfs_mkdir(nd.dentry->d_inode, dentry,
1966                                 mode, &nd);
1967                         dput(dentry);
1968                 }
1969                 mutex_unlock(&nd.dentry->d_inode->i_mutex);
1970                 path_release(&nd);
1971 out:
1972                 putname(tmp);
1973         }
1974
1975         return error;
1976 }
1977
1978 asmlinkage long sys_mkdir(const char __user *pathname, int mode)
1979 {
1980         return sys_mkdirat(AT_FDCWD, pathname, mode);
1981 }
1982
1983 /*
1984  * We try to drop the dentry early: we should have
1985  * a usage count of 2 if we're the only user of this
1986  * dentry, and if that is true (possibly after pruning
1987  * the dcache), then we drop the dentry now.
1988  *
1989  * A low-level filesystem can, if it choses, legally
1990  * do a
1991  *
1992  *      if (!d_unhashed(dentry))
1993  *              return -EBUSY;
1994  *
1995  * if it cannot handle the case of removing a directory
1996  * that is still in use by something else..
1997  */
1998 void dentry_unhash(struct dentry *dentry)
1999 {
2000         dget(dentry);
2001         if (atomic_read(&dentry->d_count))
2002                 shrink_dcache_parent(dentry);
2003         spin_lock(&dcache_lock);
2004         spin_lock(&dentry->d_lock);
2005         if (atomic_read(&dentry->d_count) == 2)
2006                 __d_drop(dentry);
2007         spin_unlock(&dentry->d_lock);
2008         spin_unlock(&dcache_lock);
2009 }
2010
2011 int vfs_rmdir(struct inode *dir, struct dentry *dentry,
2012         struct nameidata *nd)
2013 {
2014         int error = may_delete(dir, dentry, 1, nd);
2015
2016         if (error)
2017                 return error;
2018
2019         if (!dir->i_op || !dir->i_op->rmdir)
2020                 return -EPERM;
2021
2022         DQUOT_INIT(dir);
2023
2024         mutex_lock(&dentry->d_inode->i_mutex);
2025         dentry_unhash(dentry);
2026         if (d_mountpoint(dentry))
2027                 error = -EBUSY;
2028         else {
2029                 error = security_inode_rmdir(dir, dentry);
2030                 if (!error) {
2031                         error = dir->i_op->rmdir(dir, dentry);
2032                         if (!error)
2033                                 dentry->d_inode->i_flags |= S_DEAD;
2034                 }
2035         }
2036         mutex_unlock(&dentry->d_inode->i_mutex);
2037         if (!error) {
2038                 d_delete(dentry);
2039         }
2040         dput(dentry);
2041
2042         return error;
2043 }
2044
2045 static long do_rmdir(int dfd, const char __user *pathname)
2046 {
2047         int error = 0;
2048         char * name;
2049         struct dentry *dentry;
2050         struct nameidata nd;
2051
2052         name = getname(pathname);
2053         if(IS_ERR(name))
2054                 return PTR_ERR(name);
2055
2056         error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2057         if (error)
2058                 goto exit;
2059
2060         switch(nd.last_type) {
2061                 case LAST_DOTDOT:
2062                         error = -ENOTEMPTY;
2063                         goto exit1;
2064                 case LAST_DOT:
2065                         error = -EINVAL;
2066                         goto exit1;
2067                 case LAST_ROOT:
2068                         error = -EBUSY;
2069                         goto exit1;
2070         }
2071         mutex_lock(&nd.dentry->d_inode->i_mutex);
2072         dentry = lookup_hash(&nd);
2073         error = PTR_ERR(dentry);
2074         if (!IS_ERR(dentry)) {
2075                 error = vfs_rmdir(nd.dentry->d_inode, dentry, &nd);
2076                 dput(dentry);
2077         }
2078         mutex_unlock(&nd.dentry->d_inode->i_mutex);
2079 exit1:
2080         path_release(&nd);
2081 exit:
2082         putname(name);
2083         return error;
2084 }
2085
2086 asmlinkage long sys_rmdir(const char __user *pathname)
2087 {
2088         return do_rmdir(AT_FDCWD, pathname);
2089 }
2090
2091 int vfs_unlink(struct inode *dir, struct dentry *dentry,
2092         struct nameidata *nd)
2093 {
2094         int error = may_delete(dir, dentry, 0, nd);
2095
2096         if (error)
2097                 return error;
2098
2099         if (!dir->i_op || !dir->i_op->unlink)
2100                 return -EPERM;
2101
2102         DQUOT_INIT(dir);
2103
2104         mutex_lock(&dentry->d_inode->i_mutex);
2105         if (d_mountpoint(dentry))
2106                 error = -EBUSY;
2107         else {
2108                 error = security_inode_unlink(dir, dentry);
2109                 if (!error)
2110                         error = dir->i_op->unlink(dir, dentry);
2111         }
2112         mutex_unlock(&dentry->d_inode->i_mutex);
2113
2114         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2115         if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2116                 d_delete(dentry);
2117         }
2118
2119         return error;
2120 }
2121
2122 /*
2123  * Make sure that the actual truncation of the file will occur outside its
2124  * directory's i_mutex.  Truncate can take a long time if there is a lot of
2125  * writeout happening, and we don't want to prevent access to the directory
2126  * while waiting on the I/O.
2127  */
2128 static long do_unlinkat(int dfd, const char __user *pathname)
2129 {
2130         int error = 0;
2131         char * name;
2132         struct dentry *dentry;
2133         struct nameidata nd;
2134         struct inode *inode = NULL;
2135
2136         name = getname(pathname);
2137         if(IS_ERR(name))
2138                 return PTR_ERR(name);
2139
2140         error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2141         if (error)
2142                 goto exit;
2143         error = -EISDIR;
2144         if (nd.last_type != LAST_NORM)
2145                 goto exit1;
2146         mutex_lock(&nd.dentry->d_inode->i_mutex);
2147         dentry = lookup_hash(&nd);
2148         error = PTR_ERR(dentry);
2149         if (!IS_ERR(dentry)) {
2150                 /* Why not before? Because we want correct error value */
2151                 if (nd.last.name[nd.last.len])
2152                         goto slashes;
2153                 inode = dentry->d_inode;
2154                 if (inode)
2155                         atomic_inc(&inode->i_count);
2156                 error = vfs_unlink(nd.dentry->d_inode, dentry, &nd);
2157         exit2:
2158                 dput(dentry);
2159         }
2160         mutex_unlock(&nd.dentry->d_inode->i_mutex);
2161         if (inode)
2162                 iput(inode);    /* truncate the inode here */
2163 exit1:
2164         path_release(&nd);
2165 exit:
2166         putname(name);
2167         return error;
2168
2169 slashes:
2170         error = !dentry->d_inode ? -ENOENT :
2171                 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2172         goto exit2;
2173 }
2174
2175 asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2176 {
2177         if ((flag & ~AT_REMOVEDIR) != 0)
2178                 return -EINVAL;
2179
2180         if (flag & AT_REMOVEDIR)
2181                 return do_rmdir(dfd, pathname);
2182
2183         return do_unlinkat(dfd, pathname);
2184 }
2185
2186 asmlinkage long sys_unlink(const char __user *pathname)
2187 {
2188         return do_unlinkat(AT_FDCWD, pathname);
2189 }
2190
2191 int vfs_symlink(struct inode *dir, struct dentry *dentry,
2192         const char *oldname, int mode, struct nameidata *nd)
2193 {
2194         int error = may_create(dir, dentry, nd);
2195
2196         if (error)
2197                 return error;
2198
2199         if (!dir->i_op || !dir->i_op->symlink)
2200                 return -EPERM;
2201
2202         error = security_inode_symlink(dir, dentry, oldname);
2203         if (error)
2204                 return error;
2205
2206         DQUOT_INIT(dir);
2207         error = dir->i_op->symlink(dir, dentry, oldname);
2208         if (!error)
2209                 fsnotify_create(dir, dentry->d_name.name);
2210         return error;
2211 }
2212
2213 asmlinkage long sys_symlinkat(const char __user *oldname,
2214                               int newdfd, const char __user *newname)
2215 {
2216         int error = 0;
2217         char * from;
2218         char * to;
2219
2220         from = getname(oldname);
2221         if(IS_ERR(from))
2222                 return PTR_ERR(from);
2223         to = getname(newname);
2224         error = PTR_ERR(to);
2225         if (!IS_ERR(to)) {
2226                 struct dentry *dentry;
2227                 struct nameidata nd;
2228
2229                 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2230                 if (error)
2231                         goto out;
2232                 dentry = lookup_create(&nd, 0);
2233                 error = PTR_ERR(dentry);
2234                 if (!IS_ERR(dentry)) {
2235                         error = vfs_symlink(nd.dentry->d_inode, dentry,
2236                                 from, S_IALLUGO, &nd);
2237                         dput(dentry);
2238                 }
2239                 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2240                 path_release(&nd);
2241 out:
2242                 putname(to);
2243         }
2244         putname(from);
2245         return error;
2246 }
2247
2248 asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2249 {
2250         return sys_symlinkat(oldname, AT_FDCWD, newname);
2251 }
2252
2253 int vfs_link(struct dentry *old_dentry, struct inode *dir,
2254         struct dentry *new_dentry, struct nameidata *nd)
2255 {
2256         struct inode *inode = old_dentry->d_inode;
2257         int error;
2258
2259         if (!inode)
2260                 return -ENOENT;
2261
2262         error = may_create(dir, new_dentry, nd);
2263         if (error)
2264                 return error;
2265
2266         if (dir->i_sb != inode->i_sb)
2267                 return -EXDEV;
2268
2269         /*
2270          * A link to an append-only or immutable file cannot be created.
2271          */
2272         if (IS_APPEND(inode) || IS_IXORUNLINK(inode))
2273                 return -EPERM;
2274         if (!dir->i_op || !dir->i_op->link)
2275                 return -EPERM;
2276         if (S_ISDIR(old_dentry->d_inode->i_mode))
2277                 return -EPERM;
2278
2279         error = security_inode_link(old_dentry, dir, new_dentry);
2280         if (error)
2281                 return error;
2282
2283         mutex_lock(&old_dentry->d_inode->i_mutex);
2284         DQUOT_INIT(dir);
2285         error = dir->i_op->link(old_dentry, dir, new_dentry);
2286         mutex_unlock(&old_dentry->d_inode->i_mutex);
2287         if (!error)
2288                 fsnotify_create(dir, new_dentry->d_name.name);
2289         return error;
2290 }
2291
2292 /*
2293  * Hardlinks are often used in delicate situations.  We avoid
2294  * security-related surprises by not following symlinks on the
2295  * newname.  --KAB
2296  *
2297  * We don't follow them on the oldname either to be compatible
2298  * with linux 2.0, and to avoid hard-linking to directories
2299  * and other special files.  --ADM
2300  */
2301 asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
2302                            int newdfd, const char __user *newname,
2303                            int flags)
2304 {
2305         struct dentry *new_dentry;
2306         struct nameidata nd, old_nd;
2307         int error;
2308         char * to;
2309
2310         if (flags != 0)
2311                 return -EINVAL;
2312
2313         to = getname(newname);
2314         if (IS_ERR(to))
2315                 return PTR_ERR(to);
2316
2317         error = __user_walk_fd(olddfd, oldname, 0, &old_nd);
2318         if (error)
2319                 goto exit;
2320         error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2321         if (error)
2322                 goto out;
2323         error = -EXDEV;
2324         if (old_nd.mnt != nd.mnt)
2325                 goto out_release;
2326         new_dentry = lookup_create(&nd, 0);
2327         error = PTR_ERR(new_dentry);
2328         if (!IS_ERR(new_dentry)) {
2329                 error = vfs_link(old_nd.dentry, nd.dentry->d_inode,
2330                         new_dentry, &nd);
2331                 dput(new_dentry);
2332         }
2333         mutex_unlock(&nd.dentry->d_inode->i_mutex);
2334 out_release:
2335         path_release(&nd);
2336 out:
2337         path_release(&old_nd);
2338 exit:
2339         putname(to);
2340
2341         return error;
2342 }
2343
2344 asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2345 {
2346         return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2347 }
2348
2349 /*
2350  * The worst of all namespace operations - renaming directory. "Perverted"
2351  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2352  * Problems:
2353  *      a) we can get into loop creation. Check is done in is_subdir().
2354  *      b) race potential - two innocent renames can create a loop together.
2355  *         That's where 4.4 screws up. Current fix: serialization on
2356  *         sb->s_vfs_rename_sem. We might be more accurate, but that's another
2357  *         story.
2358  *      c) we have to lock _three_ objects - parents and victim (if it exists).
2359  *         And that - after we got ->i_mutex on parents (until then we don't know
2360  *         whether the target exists).  Solution: try to be smart with locking
2361  *         order for inodes.  We rely on the fact that tree topology may change
2362  *         only under ->s_vfs_rename_sem _and_ that parent of the object we
2363  *         move will be locked.  Thus we can rank directories by the tree
2364  *         (ancestors first) and rank all non-directories after them.
2365  *         That works since everybody except rename does "lock parent, lookup,
2366  *         lock child" and rename is under ->s_vfs_rename_sem.
2367  *         HOWEVER, it relies on the assumption that any object with ->lookup()
2368  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
2369  *         we'd better make sure that there's no link(2) for them.
2370  *      d) some filesystems don't support opened-but-unlinked directories,
2371  *         either because of layout or because they are not ready to deal with
2372  *         all cases correctly. The latter will be fixed (taking this sort of
2373  *         stuff into VFS), but the former is not going away. Solution: the same
2374  *         trick as in rmdir().
2375  *      e) conversion from fhandle to dentry may come in the wrong moment - when
2376  *         we are removing the target. Solution: we will have to grab ->i_mutex
2377  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2378  *         ->i_mutex on parents, which works but leads to some truely excessive
2379  *         locking].
2380  */
2381 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2382                           struct inode *new_dir, struct dentry *new_dentry)
2383 {
2384         int error = 0;
2385         struct inode *target;
2386
2387         /*
2388          * If we are going to change the parent - check write permissions,
2389          * we'll need to flip '..'.
2390          */
2391         if (new_dir != old_dir) {
2392                 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2393                 if (error)
2394                         return error;
2395         }
2396
2397         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2398         if (error)
2399                 return error;
2400
2401         target = new_dentry->d_inode;
2402         if (target) {
2403                 mutex_lock(&target->i_mutex);
2404                 dentry_unhash(new_dentry);
2405         }
2406         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2407                 error = -EBUSY;
2408         else 
2409                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2410         if (target) {
2411                 if (!error)
2412                         target->i_flags |= S_DEAD;
2413                 mutex_unlock(&target->i_mutex);
2414                 if (d_unhashed(new_dentry))
2415                         d_rehash(new_dentry);
2416                 dput(new_dentry);
2417         }
2418         if (!error)
2419                 d_move(old_dentry,new_dentry);
2420         return error;
2421 }
2422
2423 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2424                             struct inode *new_dir, struct dentry *new_dentry)
2425 {
2426         struct inode *target;
2427         int error;
2428
2429         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2430         if (error)
2431                 return error;
2432
2433         dget(new_dentry);
2434         target = new_dentry->d_inode;
2435         if (target)
2436                 mutex_lock(&target->i_mutex);
2437         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2438                 error = -EBUSY;
2439         else
2440                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2441         if (!error) {
2442                 /* The following d_move() should become unconditional */
2443                 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2444                         d_move(old_dentry, new_dentry);
2445         }
2446         if (target)
2447                 mutex_unlock(&target->i_mutex);
2448         dput(new_dentry);
2449         return error;
2450 }
2451
2452 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2453                struct inode *new_dir, struct dentry *new_dentry)
2454 {
2455         int error;
2456         int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2457         const char *old_name;
2458
2459         if (old_dentry->d_inode == new_dentry->d_inode)
2460                 return 0;
2461  
2462         error = may_delete(old_dir, old_dentry, is_dir, NULL);
2463         if (error)
2464                 return error;
2465
2466         if (!new_dentry->d_inode)
2467                 error = may_create(new_dir, new_dentry, NULL);
2468         else
2469                 error = may_delete(new_dir, new_dentry, is_dir, NULL);
2470         if (error)
2471                 return error;
2472
2473         if (!old_dir->i_op || !old_dir->i_op->rename)
2474                 return -EPERM;
2475
2476         DQUOT_INIT(old_dir);
2477         DQUOT_INIT(new_dir);
2478
2479         old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2480
2481         if (is_dir)
2482                 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2483         else
2484                 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2485         if (!error) {
2486                 const char *new_name = old_dentry->d_name.name;
2487                 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2488                               new_dentry->d_inode, old_dentry->d_inode);
2489         }
2490         fsnotify_oldname_free(old_name);
2491
2492         return error;
2493 }
2494
2495 static int do_rename(int olddfd, const char *oldname,
2496                         int newdfd, const char *newname)
2497 {
2498         int error = 0;
2499         struct dentry * old_dir, * new_dir;
2500         struct dentry * old_dentry, *new_dentry;
2501         struct dentry * trap;
2502         struct nameidata oldnd, newnd;
2503
2504         error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
2505         if (error)
2506                 goto exit;
2507
2508         error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
2509         if (error)
2510                 goto exit1;
2511
2512         error = -EXDEV;
2513         if (oldnd.mnt != newnd.mnt)
2514                 goto exit2;
2515
2516         old_dir = oldnd.dentry;
2517         error = -EBUSY;
2518         if (oldnd.last_type != LAST_NORM)
2519                 goto exit2;
2520
2521         new_dir = newnd.dentry;
2522         if (newnd.last_type != LAST_NORM)
2523                 goto exit2;
2524
2525         trap = lock_rename(new_dir, old_dir);
2526
2527         old_dentry = lookup_hash(&oldnd);
2528         error = PTR_ERR(old_dentry);
2529         if (IS_ERR(old_dentry))
2530                 goto exit3;
2531         /* source must exist */
2532         error = -ENOENT;
2533         if (!old_dentry->d_inode)
2534                 goto exit4;
2535         /* unless the source is a directory trailing slashes give -ENOTDIR */
2536         if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2537                 error = -ENOTDIR;
2538                 if (oldnd.last.name[oldnd.last.len])
2539                         goto exit4;
2540                 if (newnd.last.name[newnd.last.len])
2541                         goto exit4;
2542         }
2543         /* source should not be ancestor of target */
2544         error = -EINVAL;
2545         if (old_dentry == trap)
2546                 goto exit4;
2547         error = -EROFS;
2548         if (MNT_IS_RDONLY(newnd.mnt))
2549                 goto exit4;
2550         new_dentry = lookup_hash(&newnd);
2551         error = PTR_ERR(new_dentry);
2552         if (IS_ERR(new_dentry))
2553                 goto exit4;
2554         /* target should not be an ancestor of source */
2555         error = -ENOTEMPTY;
2556         if (new_dentry == trap)
2557                 goto exit5;
2558
2559         error = vfs_rename(old_dir->d_inode, old_dentry,
2560                                    new_dir->d_inode, new_dentry);
2561 exit5:
2562         dput(new_dentry);
2563 exit4:
2564         dput(old_dentry);
2565 exit3:
2566         unlock_rename(new_dir, old_dir);
2567 exit2:
2568         path_release(&newnd);
2569 exit1:
2570         path_release(&oldnd);
2571 exit:
2572         return error;
2573 }
2574
2575 asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2576                              int newdfd, const char __user *newname)
2577 {
2578         int error;
2579         char * from;
2580         char * to;
2581
2582         from = getname(oldname);
2583         if(IS_ERR(from))
2584                 return PTR_ERR(from);
2585         to = getname(newname);
2586         error = PTR_ERR(to);
2587         if (!IS_ERR(to)) {
2588                 error = do_rename(olddfd, from, newdfd, to);
2589                 putname(to);
2590         }
2591         putname(from);
2592         return error;
2593 }
2594
2595 asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2596 {
2597         return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2598 }
2599
2600 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2601 {
2602         int len;
2603
2604         len = PTR_ERR(link);
2605         if (IS_ERR(link))
2606                 goto out;
2607
2608         len = strlen(link);
2609         if (len > (unsigned) buflen)
2610                 len = buflen;
2611         if (copy_to_user(buffer, link, len))
2612                 len = -EFAULT;
2613 out:
2614         return len;
2615 }
2616
2617 /*
2618  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
2619  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
2620  * using) it for any given inode is up to filesystem.
2621  */
2622 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2623 {
2624         struct nameidata nd;
2625         void *cookie;
2626
2627         nd.depth = 0;
2628         cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2629         if (!IS_ERR(cookie)) {
2630                 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2631                 if (dentry->d_inode->i_op->put_link)
2632                         dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2633                 cookie = ERR_PTR(res);
2634         }
2635         return PTR_ERR(cookie);
2636 }
2637
2638 int vfs_follow_link(struct nameidata *nd, const char *link)
2639 {
2640         return __vfs_follow_link(nd, link);
2641 }
2642
2643 /* get the link contents into pagecache */
2644 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2645 {
2646         struct page * page;
2647         struct address_space *mapping = dentry->d_inode->i_mapping;
2648         page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2649                                 NULL);
2650         if (IS_ERR(page))
2651                 goto sync_fail;
2652         wait_on_page_locked(page);
2653         if (!PageUptodate(page))
2654                 goto async_fail;
2655         *ppage = page;
2656         return kmap(page);
2657
2658 async_fail:
2659         page_cache_release(page);
2660         return ERR_PTR(-EIO);
2661
2662 sync_fail:
2663         return (char*)page;
2664 }
2665
2666 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2667 {
2668         struct page *page = NULL;
2669         char *s = page_getlink(dentry, &page);
2670         int res = vfs_readlink(dentry,buffer,buflen,s);
2671         if (page) {
2672                 kunmap(page);
2673                 page_cache_release(page);
2674         }
2675         return res;
2676 }
2677
2678 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2679 {
2680         struct page *page = NULL;
2681         nd_set_link(nd, page_getlink(dentry, &page));
2682         return page;
2683 }
2684
2685 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2686 {
2687         struct page *page = cookie;
2688
2689         if (page) {
2690                 kunmap(page);
2691                 page_cache_release(page);
2692         }
2693 }
2694
2695 int __page_symlink(struct inode *inode, const char *symname, int len,
2696                 gfp_t gfp_mask)
2697 {
2698         struct address_space *mapping = inode->i_mapping;
2699         struct page *page;
2700         int err = -ENOMEM;
2701         char *kaddr;
2702
2703         page = find_or_create_page(mapping, 0, gfp_mask);
2704         if (!page)
2705                 goto fail;
2706         err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2707         if (err)
2708                 goto fail_map;
2709         kaddr = kmap_atomic(page, KM_USER0);
2710         memcpy(kaddr, symname, len-1);
2711         kunmap_atomic(kaddr, KM_USER0);
2712         mapping->a_ops->commit_write(NULL, page, 0, len-1);
2713         /*
2714          * Notice that we are _not_ going to block here - end of page is
2715          * unmapped, so this will only try to map the rest of page, see
2716          * that it is unmapped (typically even will not look into inode -
2717          * ->i_size will be enough for everything) and zero it out.
2718          * OTOH it's obviously correct and should make the page up-to-date.
2719          */
2720         if (!PageUptodate(page)) {
2721                 err = mapping->a_ops->readpage(NULL, page);
2722                 wait_on_page_locked(page);
2723         } else {
2724                 unlock_page(page);
2725         }
2726         page_cache_release(page);
2727         if (err < 0)
2728                 goto fail;
2729         mark_inode_dirty(inode);
2730         return 0;
2731 fail_map:
2732         unlock_page(page);
2733         page_cache_release(page);
2734 fail:
2735         return err;
2736 }
2737
2738 int page_symlink(struct inode *inode, const char *symname, int len)
2739 {
2740         return __page_symlink(inode, symname, len,
2741                         mapping_gfp_mask(inode->i_mapping));
2742 }
2743
2744 struct inode_operations page_symlink_inode_operations = {
2745         .readlink       = generic_readlink,
2746         .follow_link    = page_follow_link_light,
2747         .put_link       = page_put_link,
2748 };
2749
2750 EXPORT_SYMBOL(__user_walk);
2751 EXPORT_SYMBOL(__user_walk_fd);
2752 EXPORT_SYMBOL(follow_down);
2753 EXPORT_SYMBOL(follow_up);
2754 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2755 EXPORT_SYMBOL(getname);
2756 EXPORT_SYMBOL(lock_rename);
2757 EXPORT_SYMBOL(lookup_hash);
2758 EXPORT_SYMBOL(lookup_one_len);
2759 EXPORT_SYMBOL(page_follow_link_light);
2760 EXPORT_SYMBOL(page_put_link);
2761 EXPORT_SYMBOL(page_readlink);
2762 EXPORT_SYMBOL(__page_symlink);
2763 EXPORT_SYMBOL(page_symlink);
2764 EXPORT_SYMBOL(page_symlink_inode_operations);
2765 EXPORT_SYMBOL(path_lookup);
2766 EXPORT_SYMBOL(path_release);
2767 EXPORT_SYMBOL(path_walk);
2768 EXPORT_SYMBOL(permission);
2769 EXPORT_SYMBOL(vfs_permission);
2770 EXPORT_SYMBOL(file_permission);
2771 EXPORT_SYMBOL(unlock_rename);
2772 EXPORT_SYMBOL(vfs_create);
2773 EXPORT_SYMBOL(vfs_follow_link);
2774 EXPORT_SYMBOL(vfs_link);
2775 EXPORT_SYMBOL(vfs_mkdir);
2776 EXPORT_SYMBOL(vfs_mknod);
2777 EXPORT_SYMBOL(generic_permission);
2778 EXPORT_SYMBOL(vfs_readlink);
2779 EXPORT_SYMBOL(vfs_rename);
2780 EXPORT_SYMBOL(vfs_rmdir);
2781 EXPORT_SYMBOL(vfs_symlink);
2782 EXPORT_SYMBOL(vfs_unlink);
2783 EXPORT_SYMBOL(dentry_unhash);
2784 EXPORT_SYMBOL(generic_readlink);