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