UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.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/pagemap.h>
23 #include <linux/fsnotify.h>
24 #include <linux/personality.h>
25 #include <linux/security.h>
26 #include <linux/ima.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <linux/fs_struct.h>
35 #include <asm/uaccess.h>
36
37 #include "internal.h"
38
39 /* [Feb-1997 T. Schoebel-Theuer]
40  * Fundamental changes in the pathname lookup mechanisms (namei)
41  * were necessary because of omirr.  The reason is that omirr needs
42  * to know the _real_ pathname, not the user-supplied one, in case
43  * of symlinks (and also when transname replacements occur).
44  *
45  * The new code replaces the old recursive symlink resolution with
46  * an iterative one (in case of non-nested symlink chains).  It does
47  * this with calls to <fs>_follow_link().
48  * As a side effect, dir_namei(), _namei() and follow_link() are now 
49  * replaced with a single function lookup_dentry() that can handle all 
50  * the special cases of the former code.
51  *
52  * With the new dcache, the pathname is stored at each inode, at least as
53  * long as the refcount of the inode is positive.  As a side effect, the
54  * size of the dcache depends on the inode cache and thus is dynamic.
55  *
56  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57  * resolution to correspond with current state of the code.
58  *
59  * Note that the symlink resolution is not *completely* iterative.
60  * There is still a significant amount of tail- and mid- recursion in
61  * the algorithm.  Also, note that <fs>_readlink() is not used in
62  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63  * may return different results than <fs>_follow_link().  Many virtual
64  * filesystems (including /proc) exhibit this behavior.
65  */
66
67 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69  * and the name already exists in form of a symlink, try to create the new
70  * name indicated by the symlink. The old code always complained that the
71  * name already exists, due to not following the symlink even if its target
72  * is nonexistent.  The new semantics affects also mknod() and link() when
73  * the name is a symlink pointing to a non-existant name.
74  *
75  * I don't know which semantics is the right one, since I have no access
76  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78  * "old" one. Personally, I think the new semantics is much more logical.
79  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80  * file does succeed in both HP-UX and SunOs, but not in Solaris
81  * and in the old Linux semantics.
82  */
83
84 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85  * semantics.  See the comments in "open_namei" and "do_link" below.
86  *
87  * [10-Sep-98 Alan Modra] Another symlink change.
88  */
89
90 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91  *      inside the path - always follow.
92  *      in the last component in creation/removal/renaming - never follow.
93  *      if LOOKUP_FOLLOW passed - follow.
94  *      if the pathname has trailing slashes - follow.
95  *      otherwise - don't follow.
96  * (applied in that order).
97  *
98  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100  * During the 2.4 we need to fix the userland stuff depending on it -
101  * hopefully we will be able to get rid of that wart in 2.5. So far only
102  * XEmacs seems to be relying on it...
103  */
104 /*
105  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
107  * any extra contention...
108  */
109
110 /* In order to reduce some races, while at the same time doing additional
111  * checking and hopefully speeding things up, we copy filenames to the
112  * kernel data space before using them..
113  *
114  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115  * PATH_MAX includes the nul terminator --RR.
116  */
117 static int do_getname(const char __user *filename, char *page)
118 {
119         int retval;
120         unsigned long len = PATH_MAX;
121
122         if (!segment_eq(get_fs(), KERNEL_DS)) {
123                 if ((unsigned long) filename >= TASK_SIZE)
124                         return -EFAULT;
125                 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126                         len = TASK_SIZE - (unsigned long) filename;
127         }
128
129         retval = strncpy_from_user(page, filename, len);
130         if (retval > 0) {
131                 if (retval < len)
132                         return 0;
133                 return -ENAMETOOLONG;
134         } else if (!retval)
135                 retval = -ENOENT;
136         return retval;
137 }
138
139 char * getname(const char __user * filename)
140 {
141         char *tmp, *result;
142
143         result = ERR_PTR(-ENOMEM);
144         tmp = __getname();
145         if (tmp)  {
146                 int retval = do_getname(filename, tmp);
147
148                 result = tmp;
149                 if (retval < 0) {
150                         __putname(tmp);
151                         result = ERR_PTR(retval);
152                 }
153         }
154         audit_getname(result);
155         return result;
156 }
157
158 #ifdef CONFIG_AUDITSYSCALL
159 void putname(const char *name)
160 {
161         if (unlikely(!audit_dummy_context()))
162                 audit_putname(name);
163         else
164                 __putname(name);
165 }
166 EXPORT_SYMBOL(putname);
167 #endif
168
169 /*
170  * This does basic POSIX ACL permission checking
171  */
172 static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
173                 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
174 {
175         umode_t                 mode = inode->i_mode;
176
177         mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
178
179         if (current_fsuid() == inode->i_uid)
180                 mode >>= 6;
181         else {
182                 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
183                         int error = check_acl(inode, mask, flags);
184                         if (error != -EAGAIN)
185                                 return error;
186                 }
187
188                 if (in_group_p(inode->i_gid))
189                         mode >>= 3;
190         }
191
192         /*
193          * If the DACs are ok we don't need any capability check.
194          */
195         if ((mask & ~mode) == 0)
196                 return 0;
197         return -EACCES;
198 }
199
200 /**
201  * generic_permission -  check for access rights on a Posix-like filesystem
202  * @inode:      inode to check access rights for
203  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
204  * @check_acl:  optional callback to check for Posix ACLs
205  * @flags:      IPERM_FLAG_ flags.
206  *
207  * Used to check for read/write/execute permissions on a file.
208  * We use "fsuid" for this, letting us set arbitrary permissions
209  * for filesystem access without changing the "normal" uids which
210  * are used for other things.
211  *
212  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
213  * request cannot be satisfied (eg. requires blocking or too much complexity).
214  * It would then be called again in ref-walk mode.
215  */
216 int generic_permission(struct inode *inode, int mask, unsigned int flags,
217         int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
218 {
219         int ret;
220
221         /*
222          * Do the basic POSIX ACL permission checks.
223          */
224         ret = acl_permission_check(inode, mask, flags, check_acl);
225         if (ret != -EACCES)
226                 return ret;
227
228         /*
229          * Read/write DACs are always overridable.
230          * Executable DACs are overridable if at least one exec bit is set.
231          */
232         if (!(mask & MAY_EXEC) || execute_ok(inode))
233                 if (capable(CAP_DAC_OVERRIDE))
234                         return 0;
235
236         /*
237          * Searching includes executable on directories, else just read.
238          */
239         mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
240         if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
241                 if (capable(CAP_DAC_READ_SEARCH))
242                         return 0;
243
244         return -EACCES;
245 }
246
247 /**
248  * inode_permission  -  check for access rights to a given inode
249  * @inode:      inode to check permission on
250  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
251  *
252  * Used to check for read/write/execute permissions on an inode.
253  * We use "fsuid" for this, letting us set arbitrary permissions
254  * for filesystem access without changing the "normal" uids which
255  * are used for other things.
256  */
257 int inode_permission(struct inode *inode, int mask)
258 {
259         int retval;
260
261         if (mask & MAY_WRITE) {
262                 umode_t mode = inode->i_mode;
263
264                 /*
265                  * Nobody gets write access to a read-only fs.
266                  */
267                 if (IS_RDONLY(inode) &&
268                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
269                         return -EROFS;
270
271                 /*
272                  * Nobody gets write access to an immutable file.
273                  */
274                 if (IS_IMMUTABLE(inode))
275                         return -EACCES;
276         }
277
278         if (inode->i_op->permission)
279                 retval = inode->i_op->permission(inode, mask, 0);
280         else
281                 retval = generic_permission(inode, mask, 0,
282                                 inode->i_op->check_acl);
283
284         if (retval)
285                 return retval;
286
287         retval = devcgroup_inode_permission(inode, mask);
288         if (retval)
289                 return retval;
290
291         return security_inode_permission(inode, mask);
292 }
293
294 /**
295  * file_permission  -  check for additional access rights to a given file
296  * @file:       file to check access rights for
297  * @mask:       right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
298  *
299  * Used to check for read/write/execute permissions on an already opened
300  * file.
301  *
302  * Note:
303  *      Do not use this function in new code.  All access checks should
304  *      be done using inode_permission().
305  */
306 int file_permission(struct file *file, int mask)
307 {
308         return inode_permission(file->f_path.dentry->d_inode, mask);
309 }
310
311 /*
312  * get_write_access() gets write permission for a file.
313  * put_write_access() releases this write permission.
314  * This is used for regular files.
315  * We cannot support write (and maybe mmap read-write shared) accesses and
316  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
317  * can have the following values:
318  * 0: no writers, no VM_DENYWRITE mappings
319  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
320  * > 0: (i_writecount) users are writing to the file.
321  *
322  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
323  * except for the cases where we don't hold i_writecount yet. Then we need to
324  * use {get,deny}_write_access() - these functions check the sign and refuse
325  * to do the change if sign is wrong. Exclusion between them is provided by
326  * the inode->i_lock spinlock.
327  */
328
329 int get_write_access(struct inode * inode)
330 {
331         spin_lock(&inode->i_lock);
332         if (atomic_read(&inode->i_writecount) < 0) {
333                 spin_unlock(&inode->i_lock);
334                 return -ETXTBSY;
335         }
336         atomic_inc(&inode->i_writecount);
337         spin_unlock(&inode->i_lock);
338
339         return 0;
340 }
341
342 int deny_write_access(struct file * file)
343 {
344         struct inode *inode = file->f_path.dentry->d_inode;
345
346         spin_lock(&inode->i_lock);
347         if (atomic_read(&inode->i_writecount) > 0) {
348                 spin_unlock(&inode->i_lock);
349                 return -ETXTBSY;
350         }
351         atomic_dec(&inode->i_writecount);
352         spin_unlock(&inode->i_lock);
353
354         return 0;
355 }
356 EXPORT_SYMBOL(deny_write_access);
357
358 /**
359  * path_get - get a reference to a path
360  * @path: path to get the reference to
361  *
362  * Given a path increment the reference count to the dentry and the vfsmount.
363  */
364 void path_get(struct path *path)
365 {
366         mntget(path->mnt);
367         dget(path->dentry);
368 }
369 EXPORT_SYMBOL(path_get);
370
371 /**
372  * path_put - put a reference to a path
373  * @path: path to put the reference to
374  *
375  * Given a path decrement the reference count to the dentry and the vfsmount.
376  */
377 void path_put(struct path *path)
378 {
379         dput(path->dentry);
380         mntput(path->mnt);
381 }
382 EXPORT_SYMBOL(path_put);
383
384 /**
385  * nameidata_drop_rcu - drop this nameidata out of rcu-walk
386  * @nd: nameidata pathwalk data to drop
387  * Returns: 0 on success, -ECHILD on failure
388  *
389  * Path walking has 2 modes, rcu-walk and ref-walk (see
390  * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
391  * to drop out of rcu-walk mode and take normal reference counts on dentries
392  * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
393  * refcounts at the last known good point before rcu-walk got stuck, so
394  * ref-walk may continue from there. If this is not successful (eg. a seqcount
395  * has changed), then failure is returned and path walk restarts from the
396  * beginning in ref-walk mode.
397  *
398  * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
399  * ref-walk. Must be called from rcu-walk context.
400  */
401 static int nameidata_drop_rcu(struct nameidata *nd)
402 {
403         struct fs_struct *fs = current->fs;
404         struct dentry *dentry = nd->path.dentry;
405
406         BUG_ON(!(nd->flags & LOOKUP_RCU));
407         if (nd->root.mnt) {
408                 spin_lock(&fs->lock);
409                 if (nd->root.mnt != fs->root.mnt ||
410                                 nd->root.dentry != fs->root.dentry)
411                         goto err_root;
412         }
413         spin_lock(&dentry->d_lock);
414         if (!__d_rcu_to_refcount(dentry, nd->seq))
415                 goto err;
416         BUG_ON(nd->inode != dentry->d_inode);
417         spin_unlock(&dentry->d_lock);
418         if (nd->root.mnt) {
419                 path_get(&nd->root);
420                 spin_unlock(&fs->lock);
421         }
422         mntget(nd->path.mnt);
423
424         rcu_read_unlock();
425         br_read_unlock(vfsmount_lock);
426         nd->flags &= ~LOOKUP_RCU;
427         return 0;
428 err:
429         spin_unlock(&dentry->d_lock);
430 err_root:
431         if (nd->root.mnt)
432                 spin_unlock(&fs->lock);
433         return -ECHILD;
434 }
435
436 /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
437 static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
438 {
439         if (nd->flags & LOOKUP_RCU)
440                 return nameidata_drop_rcu(nd);
441         return 0;
442 }
443
444 /**
445  * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
446  * @nd: nameidata pathwalk data to drop
447  * @dentry: dentry to drop
448  * Returns: 0 on success, -ECHILD on failure
449  *
450  * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
451  * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
452  * @nd. Must be called from rcu-walk context.
453  */
454 static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
455 {
456         struct fs_struct *fs = current->fs;
457         struct dentry *parent = nd->path.dentry;
458
459         BUG_ON(!(nd->flags & LOOKUP_RCU));
460         if (nd->root.mnt) {
461                 spin_lock(&fs->lock);
462                 if (nd->root.mnt != fs->root.mnt ||
463                                 nd->root.dentry != fs->root.dentry)
464                         goto err_root;
465         }
466         spin_lock(&parent->d_lock);
467         spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
468         if (!__d_rcu_to_refcount(dentry, nd->seq))
469                 goto err;
470         /*
471          * If the sequence check on the child dentry passed, then the child has
472          * not been removed from its parent. This means the parent dentry must
473          * be valid and able to take a reference at this point.
474          */
475         BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
476         BUG_ON(!parent->d_count);
477         parent->d_count++;
478         spin_unlock(&dentry->d_lock);
479         spin_unlock(&parent->d_lock);
480         if (nd->root.mnt) {
481                 path_get(&nd->root);
482                 spin_unlock(&fs->lock);
483         }
484         mntget(nd->path.mnt);
485
486         rcu_read_unlock();
487         br_read_unlock(vfsmount_lock);
488         nd->flags &= ~LOOKUP_RCU;
489         return 0;
490 err:
491         spin_unlock(&dentry->d_lock);
492         spin_unlock(&parent->d_lock);
493 err_root:
494         if (nd->root.mnt)
495                 spin_unlock(&fs->lock);
496         return -ECHILD;
497 }
498
499 /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
500 static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
501 {
502         if (nd->flags & LOOKUP_RCU)
503                 return nameidata_dentry_drop_rcu(nd, dentry);
504         return 0;
505 }
506
507 /**
508  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
509  * @nd: nameidata pathwalk data to drop
510  * Returns: 0 on success, -ECHILD on failure
511  *
512  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
513  * nd->path should be the final element of the lookup, so nd->root is discarded.
514  * Must be called from rcu-walk context.
515  */
516 static int nameidata_drop_rcu_last(struct nameidata *nd)
517 {
518         struct dentry *dentry = nd->path.dentry;
519
520         BUG_ON(!(nd->flags & LOOKUP_RCU));
521         nd->flags &= ~LOOKUP_RCU;
522         nd->root.mnt = NULL;
523         spin_lock(&dentry->d_lock);
524         if (!__d_rcu_to_refcount(dentry, nd->seq))
525                 goto err_unlock;
526         BUG_ON(nd->inode != dentry->d_inode);
527         spin_unlock(&dentry->d_lock);
528
529         mntget(nd->path.mnt);
530
531         rcu_read_unlock();
532         br_read_unlock(vfsmount_lock);
533
534         return 0;
535
536 err_unlock:
537         spin_unlock(&dentry->d_lock);
538         rcu_read_unlock();
539         br_read_unlock(vfsmount_lock);
540         return -ECHILD;
541 }
542
543 /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
544 static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd)
545 {
546         if (likely(nd->flags & LOOKUP_RCU))
547                 return nameidata_drop_rcu_last(nd);
548         return 0;
549 }
550
551 /**
552  * release_open_intent - free up open intent resources
553  * @nd: pointer to nameidata
554  */
555 void release_open_intent(struct nameidata *nd)
556 {
557         struct file *file = nd->intent.open.file;
558
559         if (file && !IS_ERR(file)) {
560                 if (file->f_path.dentry == NULL)
561                         put_filp(file);
562                 else
563                         fput(file);
564         }
565 }
566
567 static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
568 {
569         return dentry->d_op->d_revalidate(dentry, nd);
570 }
571
572 static struct dentry *
573 do_revalidate(struct dentry *dentry, struct nameidata *nd)
574 {
575         int status = d_revalidate(dentry, nd);
576         if (unlikely(status <= 0)) {
577                 /*
578                  * The dentry failed validation.
579                  * If d_revalidate returned 0 attempt to invalidate
580                  * the dentry otherwise d_revalidate is asking us
581                  * to return a fail status.
582                  */
583                 if (status < 0) {
584                         dput(dentry);
585                         dentry = ERR_PTR(status);
586                 } else if (!d_invalidate(dentry)) {
587                         dput(dentry);
588                         dentry = NULL;
589                 }
590         }
591         return dentry;
592 }
593
594 static inline struct dentry *
595 do_revalidate_rcu(struct dentry *dentry, struct nameidata *nd)
596 {
597         int status = d_revalidate(dentry, nd);
598         if (likely(status > 0))
599                 return dentry;
600         if (status == -ECHILD) {
601                 if (nameidata_dentry_drop_rcu(nd, dentry))
602                         return ERR_PTR(-ECHILD);
603                 return do_revalidate(dentry, nd);
604         }
605         if (status < 0)
606                 return ERR_PTR(status);
607         /* Don't d_invalidate in rcu-walk mode */
608         if (nameidata_dentry_drop_rcu(nd, dentry))
609                 return ERR_PTR(-ECHILD);
610         if (!d_invalidate(dentry)) {
611                 dput(dentry);
612                 dentry = NULL;
613         }
614         return dentry;
615 }
616
617 static inline int need_reval_dot(struct dentry *dentry)
618 {
619         if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
620                 return 0;
621
622         if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
623                 return 0;
624
625         return 1;
626 }
627
628 /*
629  * force_reval_path - force revalidation of a dentry
630  *
631  * In some situations the path walking code will trust dentries without
632  * revalidating them. This causes problems for filesystems that depend on
633  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
634  * (which indicates that it's possible for the dentry to go stale), force
635  * a d_revalidate call before proceeding.
636  *
637  * Returns 0 if the revalidation was successful. If the revalidation fails,
638  * either return the error returned by d_revalidate or -ESTALE if the
639  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
640  * invalidate the dentry. It's up to the caller to handle putting references
641  * to the path if necessary.
642  */
643 static int
644 force_reval_path(struct path *path, struct nameidata *nd)
645 {
646         int status;
647         struct dentry *dentry = path->dentry;
648
649         /*
650          * only check on filesystems where it's possible for the dentry to
651          * become stale.
652          */
653         if (!need_reval_dot(dentry))
654                 return 0;
655
656         status = d_revalidate(dentry, nd);
657         if (status > 0)
658                 return 0;
659
660         if (!status) {
661                 d_invalidate(dentry);
662                 status = -ESTALE;
663         }
664         return status;
665 }
666
667 /*
668  * Short-cut version of permission(), for calling on directories
669  * during pathname resolution.  Combines parts of permission()
670  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
671  *
672  * If appropriate, check DAC only.  If not appropriate, or
673  * short-cut DAC fails, then call ->permission() to do more
674  * complete permission check.
675  */
676 static inline int exec_permission(struct inode *inode, unsigned int flags)
677 {
678         int ret;
679
680         if (inode->i_op->permission) {
681                 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
682         } else {
683                 ret = acl_permission_check(inode, MAY_EXEC, flags,
684                                 inode->i_op->check_acl);
685         }
686         if (likely(!ret))
687                 goto ok;
688         if (ret == -ECHILD)
689                 return ret;
690
691         if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
692                 goto ok;
693
694         return ret;
695 ok:
696         return security_inode_exec_permission(inode, flags);
697 }
698
699 static __always_inline void set_root(struct nameidata *nd)
700 {
701         if (!nd->root.mnt)
702                 get_fs_root(current->fs, &nd->root);
703 }
704
705 static int link_path_walk(const char *, struct nameidata *);
706
707 static __always_inline void set_root_rcu(struct nameidata *nd)
708 {
709         if (!nd->root.mnt) {
710                 struct fs_struct *fs = current->fs;
711                 unsigned seq;
712
713                 do {
714                         seq = read_seqcount_begin(&fs->seq);
715                         nd->root = fs->root;
716                         nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
717                 } while (read_seqcount_retry(&fs->seq, seq));
718         }
719 }
720
721 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
722 {
723         int ret;
724
725         if (IS_ERR(link))
726                 goto fail;
727
728         if (*link == '/') {
729                 set_root(nd);
730                 path_put(&nd->path);
731                 nd->path = nd->root;
732                 path_get(&nd->root);
733         }
734         nd->inode = nd->path.dentry->d_inode;
735
736         ret = link_path_walk(link, nd);
737         return ret;
738 fail:
739         path_put(&nd->path);
740         return PTR_ERR(link);
741 }
742
743 static void path_put_conditional(struct path *path, struct nameidata *nd)
744 {
745         dput(path->dentry);
746         if (path->mnt != nd->path.mnt)
747                 mntput(path->mnt);
748 }
749
750 static inline void path_to_nameidata(const struct path *path,
751                                         struct nameidata *nd)
752 {
753         if (!(nd->flags & LOOKUP_RCU)) {
754                 dput(nd->path.dentry);
755                 if (nd->path.mnt != path->mnt)
756                         mntput(nd->path.mnt);
757         }
758         nd->path.mnt = path->mnt;
759         nd->path.dentry = path->dentry;
760 }
761
762 static __always_inline int
763 __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
764 {
765         int error;
766         struct dentry *dentry = link->dentry;
767
768         BUG_ON(nd->flags & LOOKUP_RCU);
769
770         touch_atime(link->mnt, dentry);
771         nd_set_link(nd, NULL);
772
773         if (link->mnt == nd->path.mnt)
774                 mntget(link->mnt);
775
776         nd->last_type = LAST_BIND;
777         *p = dentry->d_inode->i_op->follow_link(dentry, nd);
778         error = PTR_ERR(*p);
779         if (!IS_ERR(*p)) {
780                 char *s = nd_get_link(nd);
781                 error = 0;
782                 if (s)
783                         error = __vfs_follow_link(nd, s);
784                 else if (nd->last_type == LAST_BIND) {
785                         error = force_reval_path(&nd->path, nd);
786                         if (error)
787                                 path_put(&nd->path);
788                 }
789         }
790         return error;
791 }
792
793 /*
794  * This limits recursive symlink follows to 8, while
795  * limiting consecutive symlinks to 40.
796  *
797  * Without that kind of total limit, nasty chains of consecutive
798  * symlinks can cause almost arbitrarily long lookups. 
799  */
800 static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
801 {
802         void *cookie;
803         int err = -ELOOP;
804
805         /* We drop rcu-walk here */
806         if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
807                 return -ECHILD;
808         BUG_ON(inode != path->dentry->d_inode);
809
810         if (current->link_count >= MAX_NESTED_LINKS)
811                 goto loop;
812         if (current->total_link_count >= 40)
813                 goto loop;
814         BUG_ON(nd->depth >= MAX_NESTED_LINKS);
815         cond_resched();
816         err = security_inode_follow_link(path->dentry, nd);
817         if (err)
818                 goto loop;
819         current->link_count++;
820         current->total_link_count++;
821         nd->depth++;
822         err = __do_follow_link(path, nd, &cookie);
823         if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
824                 path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
825         path_put(path);
826         current->link_count--;
827         nd->depth--;
828         return err;
829 loop:
830         path_put_conditional(path, nd);
831         path_put(&nd->path);
832         return err;
833 }
834
835 static int follow_up_rcu(struct path *path)
836 {
837         struct vfsmount *parent;
838         struct dentry *mountpoint;
839
840         parent = path->mnt->mnt_parent;
841         if (parent == path->mnt)
842                 return 0;
843         mountpoint = path->mnt->mnt_mountpoint;
844         path->dentry = mountpoint;
845         path->mnt = parent;
846         return 1;
847 }
848
849 int follow_up(struct path *path)
850 {
851         struct vfsmount *parent;
852         struct dentry *mountpoint;
853
854         br_read_lock(vfsmount_lock);
855         parent = path->mnt->mnt_parent;
856         if (parent == path->mnt) {
857                 br_read_unlock(vfsmount_lock);
858                 return 0;
859         }
860         mntget(parent);
861         mountpoint = dget(path->mnt->mnt_mountpoint);
862         br_read_unlock(vfsmount_lock);
863         dput(path->dentry);
864         path->dentry = mountpoint;
865         mntput(path->mnt);
866         path->mnt = parent;
867         return 1;
868 }
869
870 /*
871  * Perform an automount
872  * - return -EISDIR to tell follow_managed() to stop and return the path we
873  *   were called with.
874  */
875 static int follow_automount(struct path *path, unsigned flags,
876                             bool *need_mntput)
877 {
878         struct vfsmount *mnt;
879         int err;
880
881         if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
882                 return -EREMOTE;
883
884         /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
885          * and this is the terminal part of the path.
886          */
887         if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
888                 return -EISDIR; /* we actually want to stop here */
889
890         /* We want to mount if someone is trying to open/create a file of any
891          * type under the mountpoint, wants to traverse through the mountpoint
892          * or wants to open the mounted directory.
893          *
894          * We don't want to mount if someone's just doing a stat and they've
895          * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
896          * appended a '/' to the name.
897          */
898         if (!(flags & LOOKUP_FOLLOW) &&
899             !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
900                        LOOKUP_OPEN | LOOKUP_CREATE)))
901                 return -EISDIR;
902
903         current->total_link_count++;
904         if (current->total_link_count >= 40)
905                 return -ELOOP;
906
907         mnt = path->dentry->d_op->d_automount(path);
908         if (IS_ERR(mnt)) {
909                 /*
910                  * The filesystem is allowed to return -EISDIR here to indicate
911                  * it doesn't want to automount.  For instance, autofs would do
912                  * this so that its userspace daemon can mount on this dentry.
913                  *
914                  * However, we can only permit this if it's a terminal point in
915                  * the path being looked up; if it wasn't then the remainder of
916                  * the path is inaccessible and we should say so.
917                  */
918                 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
919                         return -EREMOTE;
920                 return PTR_ERR(mnt);
921         }
922
923         if (!mnt) /* mount collision */
924                 return 0;
925
926         err = finish_automount(mnt, path);
927
928         switch (err) {
929         case -EBUSY:
930                 /* Someone else made a mount here whilst we were busy */
931                 return 0;
932         case 0:
933                 dput(path->dentry);
934                 if (*need_mntput)
935                         mntput(path->mnt);
936                 path->mnt = mnt;
937                 path->dentry = dget(mnt->mnt_root);
938                 *need_mntput = true;
939                 return 0;
940         default:
941                 return err;
942         }
943
944 }
945
946 /*
947  * Handle a dentry that is managed in some way.
948  * - Flagged for transit management (autofs)
949  * - Flagged as mountpoint
950  * - Flagged as automount point
951  *
952  * This may only be called in refwalk mode.
953  *
954  * Serialization is taken care of in namespace.c
955  */
956 static int follow_managed(struct path *path, unsigned flags)
957 {
958         unsigned managed;
959         bool need_mntput = false;
960         int ret;
961
962         /* Given that we're not holding a lock here, we retain the value in a
963          * local variable for each dentry as we look at it so that we don't see
964          * the components of that value change under us */
965         while (managed = ACCESS_ONCE(path->dentry->d_flags),
966                managed &= DCACHE_MANAGED_DENTRY,
967                unlikely(managed != 0)) {
968                 /* Allow the filesystem to manage the transit without i_mutex
969                  * being held. */
970                 if (managed & DCACHE_MANAGE_TRANSIT) {
971                         BUG_ON(!path->dentry->d_op);
972                         BUG_ON(!path->dentry->d_op->d_manage);
973                         ret = path->dentry->d_op->d_manage(path->dentry,
974                                                            false, false);
975                         if (ret < 0)
976                                 return ret == -EISDIR ? 0 : ret;
977                 }
978
979                 /* Transit to a mounted filesystem. */
980                 if (managed & DCACHE_MOUNTED) {
981                         struct vfsmount *mounted = lookup_mnt(path);
982                         if (mounted) {
983                                 dput(path->dentry);
984                                 if (need_mntput)
985                                         mntput(path->mnt);
986                                 path->mnt = mounted;
987                                 path->dentry = dget(mounted->mnt_root);
988                                 need_mntput = true;
989                                 continue;
990                         }
991
992                         /* Something is mounted on this dentry in another
993                          * namespace and/or whatever was mounted there in this
994                          * namespace got unmounted before we managed to get the
995                          * vfsmount_lock */
996                 }
997
998                 /* Handle an automount point */
999                 if (managed & DCACHE_NEED_AUTOMOUNT) {
1000                         ret = follow_automount(path, flags, &need_mntput);
1001                         if (ret < 0)
1002                                 return ret == -EISDIR ? 0 : ret;
1003                         continue;
1004                 }
1005
1006                 /* We didn't change the current path point */
1007                 break;
1008         }
1009         return 0;
1010 }
1011
1012 int follow_down_one(struct path *path)
1013 {
1014         struct vfsmount *mounted;
1015
1016         mounted = lookup_mnt(path);
1017         if (mounted) {
1018                 dput(path->dentry);
1019                 mntput(path->mnt);
1020                 path->mnt = mounted;
1021                 path->dentry = dget(mounted->mnt_root);
1022                 return 1;
1023         }
1024         return 0;
1025 }
1026
1027 /*
1028  * Skip to top of mountpoint pile in rcuwalk mode.  We abort the rcu-walk if we
1029  * meet a managed dentry and we're not walking to "..".  True is returned to
1030  * continue, false to abort.
1031  */
1032 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1033                                struct inode **inode, bool reverse_transit)
1034 {
1035         while (d_mountpoint(path->dentry)) {
1036                 struct vfsmount *mounted;
1037                 if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1038                     !reverse_transit &&
1039                     path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1040                         return false;
1041                 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1042                 if (!mounted)
1043                         break;
1044                 path->mnt = mounted;
1045                 path->dentry = mounted->mnt_root;
1046                 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1047                 *inode = path->dentry->d_inode;
1048         }
1049
1050         if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1051                 return reverse_transit;
1052         return true;
1053 }
1054
1055 static int follow_dotdot_rcu(struct nameidata *nd)
1056 {
1057         struct inode *inode = nd->inode;
1058
1059         set_root_rcu(nd);
1060
1061         while (1) {
1062                 if (nd->path.dentry == nd->root.dentry &&
1063                     nd->path.mnt == nd->root.mnt) {
1064                         break;
1065                 }
1066                 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1067                         struct dentry *old = nd->path.dentry;
1068                         struct dentry *parent = old->d_parent;
1069                         unsigned seq;
1070
1071                         seq = read_seqcount_begin(&parent->d_seq);
1072                         if (read_seqcount_retry(&old->d_seq, nd->seq))
1073                                 return -ECHILD;
1074                         inode = parent->d_inode;
1075                         nd->path.dentry = parent;
1076                         nd->seq = seq;
1077                         break;
1078                 }
1079                 if (!follow_up_rcu(&nd->path))
1080                         break;
1081                 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1082                 inode = nd->path.dentry->d_inode;
1083         }
1084         __follow_mount_rcu(nd, &nd->path, &inode, true);
1085         nd->inode = inode;
1086
1087         return 0;
1088 }
1089
1090 /*
1091  * Follow down to the covering mount currently visible to userspace.  At each
1092  * point, the filesystem owning that dentry may be queried as to whether the
1093  * caller is permitted to proceed or not.
1094  *
1095  * Care must be taken as namespace_sem may be held (indicated by mounting_here
1096  * being true).
1097  */
1098 int follow_down(struct path *path, bool mounting_here)
1099 {
1100         unsigned managed;
1101         int ret;
1102
1103         while (managed = ACCESS_ONCE(path->dentry->d_flags),
1104                unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1105                 /* Allow the filesystem to manage the transit without i_mutex
1106                  * being held.
1107                  *
1108                  * We indicate to the filesystem if someone is trying to mount
1109                  * something here.  This gives autofs the chance to deny anyone
1110                  * other than its daemon the right to mount on its
1111                  * superstructure.
1112                  *
1113                  * The filesystem may sleep at this point.
1114                  */
1115                 if (managed & DCACHE_MANAGE_TRANSIT) {
1116                         BUG_ON(!path->dentry->d_op);
1117                         BUG_ON(!path->dentry->d_op->d_manage);
1118                         ret = path->dentry->d_op->d_manage(
1119                                 path->dentry, mounting_here, false);
1120                         if (ret < 0)
1121                                 return ret == -EISDIR ? 0 : ret;
1122                 }
1123
1124                 /* Transit to a mounted filesystem. */
1125                 if (managed & DCACHE_MOUNTED) {
1126                         struct vfsmount *mounted = lookup_mnt(path);
1127                         if (!mounted)
1128                                 break;
1129                         dput(path->dentry);
1130                         mntput(path->mnt);
1131                         path->mnt = mounted;
1132                         path->dentry = dget(mounted->mnt_root);
1133                         continue;
1134                 }
1135
1136                 /* Don't handle automount points here */
1137                 break;
1138         }
1139         return 0;
1140 }
1141
1142 /*
1143  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1144  */
1145 static void follow_mount(struct path *path)
1146 {
1147         while (d_mountpoint(path->dentry)) {
1148                 struct vfsmount *mounted = lookup_mnt(path);
1149                 if (!mounted)
1150                         break;
1151                 dput(path->dentry);
1152                 mntput(path->mnt);
1153                 path->mnt = mounted;
1154                 path->dentry = dget(mounted->mnt_root);
1155         }
1156 }
1157
1158 static void follow_dotdot(struct nameidata *nd)
1159 {
1160         set_root(nd);
1161
1162         while(1) {
1163                 struct dentry *old = nd->path.dentry;
1164
1165                 if (nd->path.dentry == nd->root.dentry &&
1166                     nd->path.mnt == nd->root.mnt) {
1167                         break;
1168                 }
1169                 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1170                         /* rare case of legitimate dget_parent()... */
1171                         nd->path.dentry = dget_parent(nd->path.dentry);
1172                         dput(old);
1173                         break;
1174                 }
1175                 if (!follow_up(&nd->path))
1176                         break;
1177         }
1178         follow_mount(&nd->path);
1179         nd->inode = nd->path.dentry->d_inode;
1180 }
1181
1182 /*
1183  * Allocate a dentry with name and parent, and perform a parent
1184  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1185  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1186  * have verified that no child exists while under i_mutex.
1187  */
1188 static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1189                                 struct qstr *name, struct nameidata *nd)
1190 {
1191         struct inode *inode = parent->d_inode;
1192         struct dentry *dentry;
1193         struct dentry *old;
1194
1195         /* Don't create child dentry for a dead directory. */
1196         if (unlikely(IS_DEADDIR(inode)))
1197                 return ERR_PTR(-ENOENT);
1198
1199         dentry = d_alloc(parent, name);
1200         if (unlikely(!dentry))
1201                 return ERR_PTR(-ENOMEM);
1202
1203         old = inode->i_op->lookup(inode, dentry, nd);
1204         if (unlikely(old)) {
1205                 dput(dentry);
1206                 dentry = old;
1207         }
1208         return dentry;
1209 }
1210
1211 /*
1212  *  It's more convoluted than I'd like it to be, but... it's still fairly
1213  *  small and for now I'd prefer to have fast path as straight as possible.
1214  *  It _is_ time-critical.
1215  */
1216 static int do_lookup(struct nameidata *nd, struct qstr *name,
1217                         struct path *path, struct inode **inode)
1218 {
1219         struct vfsmount *mnt = nd->path.mnt;
1220         struct dentry *dentry, *parent = nd->path.dentry;
1221         struct inode *dir;
1222         int err;
1223
1224         /*
1225          * See if the low-level filesystem might want
1226          * to use its own hash..
1227          */
1228         if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1229                 err = parent->d_op->d_hash(parent, nd->inode, name);
1230                 if (err < 0)
1231                         return err;
1232         }
1233
1234         /*
1235          * Rename seqlock is not required here because in the off chance
1236          * of a false negative due to a concurrent rename, we're going to
1237          * do the non-racy lookup, below.
1238          */
1239         if (nd->flags & LOOKUP_RCU) {
1240                 unsigned seq;
1241
1242                 *inode = nd->inode;
1243                 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1244                 if (!dentry) {
1245                         if (nameidata_drop_rcu(nd))
1246                                 return -ECHILD;
1247                         goto need_lookup;
1248                 }
1249                 /* Memory barrier in read_seqcount_begin of child is enough */
1250                 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1251                         return -ECHILD;
1252
1253                 nd->seq = seq;
1254                 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1255                         dentry = do_revalidate_rcu(dentry, nd);
1256                         if (!dentry)
1257                                 goto need_lookup;
1258                         if (IS_ERR(dentry))
1259                                 goto fail;
1260                         if (!(nd->flags & LOOKUP_RCU))
1261                                 goto done;
1262                 }
1263                 path->mnt = mnt;
1264                 path->dentry = dentry;
1265                 if (likely(__follow_mount_rcu(nd, path, inode, false)))
1266                         return 0;
1267                 if (nameidata_drop_rcu(nd))
1268                         return -ECHILD;
1269                 /* fallthru */
1270         }
1271         dentry = __d_lookup(parent, name);
1272         if (!dentry)
1273                 goto need_lookup;
1274 found:
1275         if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1276                 dentry = do_revalidate(dentry, nd);
1277                 if (!dentry)
1278                         goto need_lookup;
1279                 if (IS_ERR(dentry))
1280                         goto fail;
1281         }
1282 done:
1283         path->mnt = mnt;
1284         path->dentry = dentry;
1285         err = follow_managed(path, nd->flags);
1286         if (unlikely(err < 0)) {
1287                 path_put_conditional(path, nd);
1288                 return err;
1289         }
1290         *inode = path->dentry->d_inode;
1291         return 0;
1292
1293 need_lookup:
1294         dir = parent->d_inode;
1295         BUG_ON(nd->inode != dir);
1296
1297         mutex_lock(&dir->i_mutex);
1298         /*
1299          * First re-do the cached lookup just in case it was created
1300          * while we waited for the directory semaphore, or the first
1301          * lookup failed due to an unrelated rename.
1302          *
1303          * This could use version numbering or similar to avoid unnecessary
1304          * cache lookups, but then we'd have to do the first lookup in the
1305          * non-racy way. However in the common case here, everything should
1306          * be hot in cache, so would it be a big win?
1307          */
1308         dentry = d_lookup(parent, name);
1309         if (likely(!dentry)) {
1310                 dentry = d_alloc_and_lookup(parent, name, nd);
1311                 mutex_unlock(&dir->i_mutex);
1312                 if (IS_ERR(dentry))
1313                         goto fail;
1314                 goto done;
1315         }
1316         /*
1317          * Uhhuh! Nasty case: the cache was re-populated while
1318          * we waited on the semaphore. Need to revalidate.
1319          */
1320         mutex_unlock(&dir->i_mutex);
1321         goto found;
1322
1323 fail:
1324         return PTR_ERR(dentry);
1325 }
1326
1327 /*
1328  * Name resolution.
1329  * This is the basic name resolution function, turning a pathname into
1330  * the final dentry. We expect 'base' to be positive and a directory.
1331  *
1332  * Returns 0 and nd will have valid dentry and mnt on success.
1333  * Returns error and drops reference to input namei data on failure.
1334  */
1335 static int link_path_walk(const char *name, struct nameidata *nd)
1336 {
1337         struct path next;
1338         int err;
1339         unsigned int lookup_flags = nd->flags;
1340         
1341         while (*name=='/')
1342                 name++;
1343         if (!*name)
1344                 goto return_reval;
1345
1346         if (nd->depth)
1347                 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
1348
1349         /* At this point we know we have a real path component. */
1350         for(;;) {
1351                 struct inode *inode;
1352                 unsigned long hash;
1353                 struct qstr this;
1354                 unsigned int c;
1355
1356                 nd->flags |= LOOKUP_CONTINUE;
1357                 if (nd->flags & LOOKUP_RCU) {
1358                         err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1359                         if (err == -ECHILD) {
1360                                 if (nameidata_drop_rcu(nd))
1361                                         return -ECHILD;
1362                                 goto exec_again;
1363                         }
1364                 } else {
1365 exec_again:
1366                         err = exec_permission(nd->inode, 0);
1367                 }
1368                 if (err)
1369                         break;
1370
1371                 this.name = name;
1372                 c = *(const unsigned char *)name;
1373
1374                 hash = init_name_hash();
1375                 do {
1376                         name++;
1377                         hash = partial_name_hash(c, hash);
1378                         c = *(const unsigned char *)name;
1379                 } while (c && (c != '/'));
1380                 this.len = name - (const char *) this.name;
1381                 this.hash = end_name_hash(hash);
1382
1383                 /* remove trailing slashes? */
1384                 if (!c)
1385                         goto last_component;
1386                 while (*++name == '/');
1387                 if (!*name)
1388                         goto last_with_slashes;
1389
1390                 /*
1391                  * "." and ".." are special - ".." especially so because it has
1392                  * to be able to know about the current root directory and
1393                  * parent relationships.
1394                  */
1395                 if (this.name[0] == '.') switch (this.len) {
1396                         default:
1397                                 break;
1398                         case 2:
1399                                 if (this.name[1] != '.')
1400                                         break;
1401                                 if (nd->flags & LOOKUP_RCU) {
1402                                         if (follow_dotdot_rcu(nd))
1403                                                 return -ECHILD;
1404                                 } else
1405                                         follow_dotdot(nd);
1406                                 /* fallthrough */
1407                         case 1:
1408                                 continue;
1409                 }
1410                 /* This does the actual lookups.. */
1411                 err = do_lookup(nd, &this, &next, &inode);
1412                 if (err)
1413                         break;
1414                 err = -ENOENT;
1415                 if (!inode)
1416                         goto out_dput;
1417
1418                 if (inode->i_op->follow_link) {
1419                         err = do_follow_link(inode, &next, nd);
1420                         if (err)
1421                                 goto return_err;
1422                         nd->inode = nd->path.dentry->d_inode;
1423                         err = -ENOENT;
1424                         if (!nd->inode)
1425                                 break;
1426                 } else {
1427                         path_to_nameidata(&next, nd);
1428                         nd->inode = inode;
1429                 }
1430                 err = -ENOTDIR; 
1431                 if (!nd->inode->i_op->lookup)
1432                         break;
1433                 continue;
1434                 /* here ends the main loop */
1435
1436 last_with_slashes:
1437                 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1438 last_component:
1439                 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1440                 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1441                 if (lookup_flags & LOOKUP_PARENT)
1442                         goto lookup_parent;
1443                 if (this.name[0] == '.') switch (this.len) {
1444                         default:
1445                                 break;
1446                         case 2:
1447                                 if (this.name[1] != '.')
1448                                         break;
1449                                 if (nd->flags & LOOKUP_RCU) {
1450                                         if (follow_dotdot_rcu(nd))
1451                                                 return -ECHILD;
1452                                 } else
1453                                         follow_dotdot(nd);
1454                                 /* fallthrough */
1455                         case 1:
1456                                 goto return_reval;
1457                 }
1458                 err = do_lookup(nd, &this, &next, &inode);
1459                 if (err)
1460                         break;
1461                 if (inode && unlikely(inode->i_op->follow_link) &&
1462                     (lookup_flags & LOOKUP_FOLLOW)) {
1463                         err = do_follow_link(inode, &next, nd);
1464                         if (err)
1465                                 goto return_err;
1466                         nd->inode = nd->path.dentry->d_inode;
1467                 } else {
1468                         path_to_nameidata(&next, nd);
1469                         nd->inode = inode;
1470                 }
1471                 err = -ENOENT;
1472                 if (!nd->inode)
1473                         break;
1474                 if (lookup_flags & LOOKUP_DIRECTORY) {
1475                         err = -ENOTDIR; 
1476                         if (!nd->inode->i_op->lookup)
1477                                 break;
1478                 }
1479                 goto return_base;
1480 lookup_parent:
1481                 nd->last = this;
1482                 nd->last_type = LAST_NORM;
1483                 if (this.name[0] != '.')
1484                         goto return_base;
1485                 if (this.len == 1)
1486                         nd->last_type = LAST_DOT;
1487                 else if (this.len == 2 && this.name[1] == '.')
1488                         nd->last_type = LAST_DOTDOT;
1489                 else
1490                         goto return_base;
1491 return_reval:
1492                 /*
1493                  * We bypassed the ordinary revalidation routines.
1494                  * We may need to check the cached dentry for staleness.
1495                  */
1496                 if (need_reval_dot(nd->path.dentry)) {
1497                         if (nameidata_drop_rcu_last_maybe(nd))
1498                                 return -ECHILD;
1499                         /* Note: we do not d_invalidate() */
1500                         err = d_revalidate(nd->path.dentry, nd);
1501                         if (!err)
1502                                 err = -ESTALE;
1503                         if (err < 0)
1504                                 break;
1505                         return 0;
1506                 }
1507 return_base:
1508                 if (nameidata_drop_rcu_last_maybe(nd))
1509                         return -ECHILD;
1510                 return 0;
1511 out_dput:
1512                 if (!(nd->flags & LOOKUP_RCU))
1513                         path_put_conditional(&next, nd);
1514                 break;
1515         }
1516         if (!(nd->flags & LOOKUP_RCU))
1517                 path_put(&nd->path);
1518 return_err:
1519         return err;
1520 }
1521
1522 static inline int path_walk_rcu(const char *name, struct nameidata *nd)
1523 {
1524         current->total_link_count = 0;
1525
1526         return link_path_walk(name, nd);
1527 }
1528
1529 static inline int path_walk_simple(const char *name, struct nameidata *nd)
1530 {
1531         current->total_link_count = 0;
1532
1533         return link_path_walk(name, nd);
1534 }
1535
1536 static int path_walk(const char *name, struct nameidata *nd)
1537 {
1538         struct path save = nd->path;
1539         int result;
1540
1541         current->total_link_count = 0;
1542
1543         /* make sure the stuff we saved doesn't go away */
1544         path_get(&save);
1545
1546         result = link_path_walk(name, nd);
1547         if (result == -ESTALE) {
1548                 /* nd->path had been dropped */
1549                 current->total_link_count = 0;
1550                 nd->path = save;
1551                 nd->inode = save.dentry->d_inode;
1552                 path_get(&nd->path);
1553                 nd->flags |= LOOKUP_REVAL;
1554                 result = link_path_walk(name, nd);
1555         }
1556
1557         path_put(&save);
1558
1559         return result;
1560 }
1561
1562 static void path_finish_rcu(struct nameidata *nd)
1563 {
1564         if (nd->flags & LOOKUP_RCU) {
1565                 /* RCU dangling. Cancel it. */
1566                 nd->flags &= ~LOOKUP_RCU;
1567                 nd->root.mnt = NULL;
1568                 rcu_read_unlock();
1569                 br_read_unlock(vfsmount_lock);
1570         }
1571         if (nd->file)
1572                 fput(nd->file);
1573 }
1574
1575 static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1576 {
1577         int retval = 0;
1578         int fput_needed;
1579         struct file *file;
1580
1581         nd->last_type = LAST_ROOT; /* if there are only slashes... */
1582         nd->flags = flags | LOOKUP_RCU;
1583         nd->depth = 0;
1584         nd->root.mnt = NULL;
1585         nd->file = NULL;
1586
1587         if (*name=='/') {
1588                 struct fs_struct *fs = current->fs;
1589                 unsigned seq;
1590
1591                 br_read_lock(vfsmount_lock);
1592                 rcu_read_lock();
1593
1594                 do {
1595                         seq = read_seqcount_begin(&fs->seq);
1596                         nd->root = fs->root;
1597                         nd->path = nd->root;
1598                         nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1599                 } while (read_seqcount_retry(&fs->seq, seq));
1600
1601         } else if (dfd == AT_FDCWD) {
1602                 struct fs_struct *fs = current->fs;
1603                 unsigned seq;
1604
1605                 br_read_lock(vfsmount_lock);
1606                 rcu_read_lock();
1607
1608                 do {
1609                         seq = read_seqcount_begin(&fs->seq);
1610                         nd->path = fs->pwd;
1611                         nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1612                 } while (read_seqcount_retry(&fs->seq, seq));
1613
1614         } else {
1615                 struct dentry *dentry;
1616
1617                 file = fget_light(dfd, &fput_needed);
1618                 retval = -EBADF;
1619                 if (!file)
1620                         goto out_fail;
1621
1622                 dentry = file->f_path.dentry;
1623
1624                 retval = -ENOTDIR;
1625                 if (!S_ISDIR(dentry->d_inode->i_mode))
1626                         goto fput_fail;
1627
1628                 retval = file_permission(file, MAY_EXEC);
1629                 if (retval)
1630                         goto fput_fail;
1631
1632                 nd->path = file->f_path;
1633                 if (fput_needed)
1634                         nd->file = file;
1635
1636                 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1637                 br_read_lock(vfsmount_lock);
1638                 rcu_read_lock();
1639         }
1640         nd->inode = nd->path.dentry->d_inode;
1641         return 0;
1642
1643 fput_fail:
1644         fput_light(file, fput_needed);
1645 out_fail:
1646         return retval;
1647 }
1648
1649 static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1650 {
1651         int retval = 0;
1652         int fput_needed;
1653         struct file *file;
1654
1655         nd->last_type = LAST_ROOT; /* if there are only slashes... */
1656         nd->flags = flags;
1657         nd->depth = 0;
1658         nd->root.mnt = NULL;
1659
1660         if (*name=='/') {
1661                 set_root(nd);
1662                 nd->path = nd->root;
1663                 path_get(&nd->root);
1664         } else if (dfd == AT_FDCWD) {
1665                 get_fs_pwd(current->fs, &nd->path);
1666         } else {
1667                 struct dentry *dentry;
1668
1669                 file = fget_light(dfd, &fput_needed);
1670                 retval = -EBADF;
1671                 if (!file)
1672                         goto out_fail;
1673
1674                 dentry = file->f_path.dentry;
1675
1676                 retval = -ENOTDIR;
1677                 if (!S_ISDIR(dentry->d_inode->i_mode))
1678                         goto fput_fail;
1679
1680                 retval = file_permission(file, MAY_EXEC);
1681                 if (retval)
1682                         goto fput_fail;
1683
1684                 nd->path = file->f_path;
1685                 path_get(&file->f_path);
1686
1687                 fput_light(file, fput_needed);
1688         }
1689         nd->inode = nd->path.dentry->d_inode;
1690         return 0;
1691
1692 fput_fail:
1693         fput_light(file, fput_needed);
1694 out_fail:
1695         return retval;
1696 }
1697
1698 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1699 static int do_path_lookup(int dfd, const char *name,
1700                                 unsigned int flags, struct nameidata *nd)
1701 {
1702         int retval;
1703
1704         /*
1705          * Path walking is largely split up into 2 different synchronisation
1706          * schemes, rcu-walk and ref-walk (explained in
1707          * Documentation/filesystems/path-lookup.txt). These share much of the
1708          * path walk code, but some things particularly setup, cleanup, and
1709          * following mounts are sufficiently divergent that functions are
1710          * duplicated. Typically there is a function foo(), and its RCU
1711          * analogue, foo_rcu().
1712          *
1713          * -ECHILD is the error number of choice (just to avoid clashes) that
1714          * is returned if some aspect of an rcu-walk fails. Such an error must
1715          * be handled by restarting a traditional ref-walk (which will always
1716          * be able to complete).
1717          */
1718         retval = path_init_rcu(dfd, name, flags, nd);
1719         if (unlikely(retval))
1720                 return retval;
1721         retval = path_walk_rcu(name, nd);
1722         path_finish_rcu(nd);
1723         if (nd->root.mnt) {
1724                 path_put(&nd->root);
1725                 nd->root.mnt = NULL;
1726         }
1727
1728         if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
1729                 /* slower, locked walk */
1730                 if (retval == -ESTALE)
1731                         flags |= LOOKUP_REVAL;
1732                 retval = path_init(dfd, name, flags, nd);
1733                 if (unlikely(retval))
1734                         return retval;
1735                 retval = path_walk(name, nd);
1736                 if (nd->root.mnt) {
1737                         path_put(&nd->root);
1738                         nd->root.mnt = NULL;
1739                 }
1740         }
1741
1742         if (likely(!retval)) {
1743                 if (unlikely(!audit_dummy_context())) {
1744                         if (nd->path.dentry && nd->inode)
1745                                 audit_inode(name, nd->path.dentry);
1746                 }
1747         }
1748
1749         return retval;
1750 }
1751
1752 int path_lookup(const char *name, unsigned int flags,
1753                         struct nameidata *nd)
1754 {
1755         return do_path_lookup(AT_FDCWD, name, flags, nd);
1756 }
1757
1758 int kern_path(const char *name, unsigned int flags, struct path *path)
1759 {
1760         struct nameidata nd;
1761         int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1762         if (!res)
1763                 *path = nd.path;
1764         return res;
1765 }
1766
1767 /**
1768  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1769  * @dentry:  pointer to dentry of the base directory
1770  * @mnt: pointer to vfs mount of the base directory
1771  * @name: pointer to file name
1772  * @flags: lookup flags
1773  * @nd: pointer to nameidata
1774  */
1775 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1776                     const char *name, unsigned int flags,
1777                     struct nameidata *nd)
1778 {
1779         int retval;
1780
1781         /* same as do_path_lookup */
1782         nd->last_type = LAST_ROOT;
1783         nd->flags = flags;
1784         nd->depth = 0;
1785
1786         nd->path.dentry = dentry;
1787         nd->path.mnt = mnt;
1788         path_get(&nd->path);
1789         nd->root = nd->path;
1790         path_get(&nd->root);
1791         nd->inode = nd->path.dentry->d_inode;
1792
1793         retval = path_walk(name, nd);
1794         if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1795                                 nd->inode))
1796                 audit_inode(name, nd->path.dentry);
1797
1798         path_put(&nd->root);
1799         nd->root.mnt = NULL;
1800
1801         return retval;
1802 }
1803
1804 static struct dentry *__lookup_hash(struct qstr *name,
1805                 struct dentry *base, struct nameidata *nd)
1806 {
1807         struct inode *inode = base->d_inode;
1808         struct dentry *dentry;
1809         int err;
1810
1811         err = exec_permission(inode, 0);
1812         if (err)
1813                 return ERR_PTR(err);
1814
1815         /*
1816          * See if the low-level filesystem might want
1817          * to use its own hash..
1818          */
1819         if (base->d_flags & DCACHE_OP_HASH) {
1820                 err = base->d_op->d_hash(base, inode, name);
1821                 dentry = ERR_PTR(err);
1822                 if (err < 0)
1823                         goto out;
1824         }
1825
1826         /*
1827          * Don't bother with __d_lookup: callers are for creat as
1828          * well as unlink, so a lot of the time it would cost
1829          * a double lookup.
1830          */
1831         dentry = d_lookup(base, name);
1832
1833         if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
1834                 dentry = do_revalidate(dentry, nd);
1835
1836         if (!dentry)
1837                 dentry = d_alloc_and_lookup(base, name, nd);
1838 out:
1839         return dentry;
1840 }
1841
1842 /*
1843  * Restricted form of lookup. Doesn't follow links, single-component only,
1844  * needs parent already locked. Doesn't follow mounts.
1845  * SMP-safe.
1846  */
1847 struct dentry *lookup_hash(struct nameidata *nd)
1848 {
1849         return __lookup_hash(&nd->last, nd->path.dentry, nd);
1850 }
1851 EXPORT_SYMBOL(lookup_hash);
1852
1853 int __lookup_one_len(const char *name, struct qstr *this,
1854                 struct dentry *base, int len)
1855 {
1856         unsigned long hash;
1857         unsigned int c;
1858
1859         this->name = name;
1860         this->len = len;
1861         if (!len)
1862                 return -EACCES;
1863
1864         hash = init_name_hash();
1865         while (len--) {
1866                 c = *(const unsigned char *)name++;
1867                 if (c == '/' || c == '\0')
1868                         return -EACCES;
1869                 hash = partial_name_hash(c, hash);
1870         }
1871         this->hash = end_name_hash(hash);
1872         return 0;
1873 }
1874 EXPORT_SYMBOL(__lookup_one_len);
1875
1876 /**
1877  * lookup_one_len - filesystem helper to lookup single pathname component
1878  * @name:       pathname component to lookup
1879  * @base:       base directory to lookup from
1880  * @len:        maximum length @len should be interpreted to
1881  *
1882  * Note that this routine is purely a helper for filesystem usage and should
1883  * not be called by generic code.  Also note that by using this function the
1884  * nameidata argument is passed to the filesystem methods and a filesystem
1885  * using this helper needs to be prepared for that.
1886  */
1887 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1888 {
1889         int err;
1890         struct qstr this;
1891
1892         WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1893
1894         err = __lookup_one_len(name, &this, base, len);
1895         if (err)
1896                 return ERR_PTR(err);
1897
1898         return __lookup_hash(&this, base, NULL);
1899 }
1900
1901 int user_path_at(int dfd, const char __user *name, unsigned flags,
1902                  struct path *path)
1903 {
1904         struct nameidata nd;
1905         char *tmp = getname(name);
1906         int err = PTR_ERR(tmp);
1907         if (!IS_ERR(tmp)) {
1908
1909                 BUG_ON(flags & LOOKUP_PARENT);
1910
1911                 err = do_path_lookup(dfd, tmp, flags, &nd);
1912                 putname(tmp);
1913                 if (!err)
1914                         *path = nd.path;
1915         }
1916         return err;
1917 }
1918
1919 static int user_path_parent(int dfd, const char __user *path,
1920                         struct nameidata *nd, char **name)
1921 {
1922         char *s = getname(path);
1923         int error;
1924
1925         if (IS_ERR(s))
1926                 return PTR_ERR(s);
1927
1928         error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1929         if (error)
1930                 putname(s);
1931         else
1932                 *name = s;
1933
1934         return error;
1935 }
1936
1937 /*
1938  * It's inline, so penalty for filesystems that don't use sticky bit is
1939  * minimal.
1940  */
1941 static inline int check_sticky(struct inode *dir, struct inode *inode)
1942 {
1943         uid_t fsuid = current_fsuid();
1944
1945         if (!(dir->i_mode & S_ISVTX))
1946                 return 0;
1947         if (inode->i_uid == fsuid)
1948                 return 0;
1949         if (dir->i_uid == fsuid)
1950                 return 0;
1951         return !capable(CAP_FOWNER);
1952 }
1953
1954 /*
1955  *      Check whether we can remove a link victim from directory dir, check
1956  *  whether the type of victim is right.
1957  *  1. We can't do it if dir is read-only (done in permission())
1958  *  2. We should have write and exec permissions on dir
1959  *  3. We can't remove anything from append-only dir
1960  *  4. We can't do anything with immutable dir (done in permission())
1961  *  5. If the sticky bit on dir is set we should either
1962  *      a. be owner of dir, or
1963  *      b. be owner of victim, or
1964  *      c. have CAP_FOWNER capability
1965  *  6. If the victim is append-only or immutable we can't do antyhing with
1966  *     links pointing to it.
1967  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1968  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1969  *  9. We can't remove a root or mountpoint.
1970  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1971  *     nfs_async_unlink().
1972  */
1973 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1974 {
1975         int error;
1976
1977         if (!victim->d_inode)
1978                 return -ENOENT;
1979
1980         BUG_ON(victim->d_parent->d_inode != dir);
1981         audit_inode_child(victim, dir);
1982
1983         error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1984         if (error)
1985                 return error;
1986         if (IS_APPEND(dir))
1987                 return -EPERM;
1988         if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1989             IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1990                 return -EPERM;
1991         if (isdir) {
1992                 if (!S_ISDIR(victim->d_inode->i_mode))
1993                         return -ENOTDIR;
1994                 if (IS_ROOT(victim))
1995                         return -EBUSY;
1996         } else if (S_ISDIR(victim->d_inode->i_mode))
1997                 return -EISDIR;
1998         if (IS_DEADDIR(dir))
1999                 return -ENOENT;
2000         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2001                 return -EBUSY;
2002         return 0;
2003 }
2004
2005 /*      Check whether we can create an object with dentry child in directory
2006  *  dir.
2007  *  1. We can't do it if child already exists (open has special treatment for
2008  *     this case, but since we are inlined it's OK)
2009  *  2. We can't do it if dir is read-only (done in permission())
2010  *  3. We should have write and exec permissions on dir
2011  *  4. We can't do it if dir is immutable (done in permission())
2012  */
2013 static inline int may_create(struct inode *dir, struct dentry *child)
2014 {
2015         if (child->d_inode)
2016                 return -EEXIST;
2017         if (IS_DEADDIR(dir))
2018                 return -ENOENT;
2019         return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2020 }
2021
2022 /*
2023  * p1 and p2 should be directories on the same fs.
2024  */
2025 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2026 {
2027         struct dentry *p;
2028
2029         if (p1 == p2) {
2030                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2031                 return NULL;
2032         }
2033
2034         mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2035
2036         p = d_ancestor(p2, p1);
2037         if (p) {
2038                 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2039                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2040                 return p;
2041         }
2042
2043         p = d_ancestor(p1, p2);
2044         if (p) {
2045                 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2046                 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2047                 return p;
2048         }
2049
2050         mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2051         mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2052         return NULL;
2053 }
2054
2055 void unlock_rename(struct dentry *p1, struct dentry *p2)
2056 {
2057         mutex_unlock(&p1->d_inode->i_mutex);
2058         if (p1 != p2) {
2059                 mutex_unlock(&p2->d_inode->i_mutex);
2060                 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2061         }
2062 }
2063
2064 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
2065                 struct nameidata *nd)
2066 {
2067         int error = may_create(dir, dentry);
2068
2069         if (error)
2070                 return error;
2071
2072         if (!dir->i_op->create)
2073                 return -EACCES; /* shouldn't it be ENOSYS? */
2074         mode &= S_IALLUGO;
2075         mode |= S_IFREG;
2076         error = security_inode_create(dir, dentry, mode);
2077         if (error)
2078                 return error;
2079         error = dir->i_op->create(dir, dentry, mode, nd);
2080         if (!error)
2081                 fsnotify_create(dir, dentry);
2082         return error;
2083 }
2084
2085 int may_open(struct path *path, int acc_mode, int flag)
2086 {
2087         struct dentry *dentry = path->dentry;
2088         struct inode *inode = dentry->d_inode;
2089         int error;
2090
2091         if (!inode)
2092                 return -ENOENT;
2093
2094         switch (inode->i_mode & S_IFMT) {
2095         case S_IFLNK:
2096                 return -ELOOP;
2097         case S_IFDIR:
2098                 if (acc_mode & MAY_WRITE)
2099                         return -EISDIR;
2100                 break;
2101         case S_IFBLK:
2102         case S_IFCHR:
2103                 if (path->mnt->mnt_flags & MNT_NODEV)
2104                         return -EACCES;
2105                 /*FALLTHRU*/
2106         case S_IFIFO:
2107         case S_IFSOCK:
2108                 flag &= ~O_TRUNC;
2109                 break;
2110         }
2111
2112         error = inode_permission(inode, acc_mode);
2113         if (error)
2114                 return error;
2115
2116         /*
2117          * An append-only file must be opened in append mode for writing.
2118          */
2119         if (IS_APPEND(inode)) {
2120                 if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2121                         return -EPERM;
2122                 if (flag & O_TRUNC)
2123                         return -EPERM;
2124         }
2125
2126         /* O_NOATIME can only be set by the owner or superuser */
2127         if (flag & O_NOATIME && !is_owner_or_cap(inode))
2128                 return -EPERM;
2129
2130         /*
2131          * Ensure there are no outstanding leases on the file.
2132          */
2133         return break_lease(inode, flag);
2134 }
2135
2136 static int handle_truncate(struct file *filp)
2137 {
2138         struct path *path = &filp->f_path;
2139         struct inode *inode = path->dentry->d_inode;
2140         int error = get_write_access(inode);
2141         if (error)
2142                 return error;
2143         /*
2144          * Refuse to truncate files with mandatory locks held on them.
2145          */
2146         error = locks_verify_locked(inode);
2147         if (!error)
2148                 error = security_path_truncate(path);
2149         if (!error) {
2150                 error = do_truncate(path->dentry, 0,
2151                                     ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2152                                     filp);
2153         }
2154         put_write_access(inode);
2155         return error;
2156 }
2157
2158 /*
2159  * Be careful about ever adding any more callers of this
2160  * function.  Its flags must be in the namei format, not
2161  * what get passed to sys_open().
2162  */
2163 static int __open_namei_create(struct nameidata *nd, struct path *path,
2164                                 int open_flag, int mode)
2165 {
2166         int error;
2167         struct dentry *dir = nd->path.dentry;
2168
2169         if (!IS_POSIXACL(dir->d_inode))
2170                 mode &= ~current_umask();
2171         error = security_path_mknod(&nd->path, path->dentry, mode, 0);
2172         if (error)
2173                 goto out_unlock;
2174         error = vfs_create(dir->d_inode, path->dentry, mode, nd);
2175 out_unlock:
2176         mutex_unlock(&dir->d_inode->i_mutex);
2177         dput(nd->path.dentry);
2178         nd->path.dentry = path->dentry;
2179
2180         if (error)
2181                 return error;
2182         /* Don't check for write permission, don't truncate */
2183         return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
2184 }
2185
2186 /*
2187  * Note that while the flag value (low two bits) for sys_open means:
2188  *      00 - read-only
2189  *      01 - write-only
2190  *      10 - read-write
2191  *      11 - special
2192  * it is changed into
2193  *      00 - no permissions needed
2194  *      01 - read-permission
2195  *      10 - write-permission
2196  *      11 - read-write
2197  * for the internal routines (ie open_namei()/follow_link() etc)
2198  * This is more logical, and also allows the 00 "no perm needed"
2199  * to be used for symlinks (where the permissions are checked
2200  * later).
2201  *
2202 */
2203 static inline int open_to_namei_flags(int flag)
2204 {
2205         if ((flag+1) & O_ACCMODE)
2206                 flag++;
2207         return flag;
2208 }
2209
2210 static int open_will_truncate(int flag, struct inode *inode)
2211 {
2212         /*
2213          * We'll never write to the fs underlying
2214          * a device file.
2215          */
2216         if (special_file(inode->i_mode))
2217                 return 0;
2218         return (flag & O_TRUNC);
2219 }
2220
2221 static struct file *finish_open(struct nameidata *nd,
2222                                 int open_flag, int acc_mode)
2223 {
2224         struct file *filp;
2225         int will_truncate;
2226         int error;
2227
2228         will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2229         if (will_truncate) {
2230                 error = mnt_want_write(nd->path.mnt);
2231                 if (error)
2232                         goto exit;
2233         }
2234         error = may_open(&nd->path, acc_mode, open_flag);
2235         if (error) {
2236                 if (will_truncate)
2237                         mnt_drop_write(nd->path.mnt);
2238                 goto exit;
2239         }
2240         filp = nameidata_to_filp(nd);
2241         if (!IS_ERR(filp)) {
2242                 error = ima_file_check(filp, acc_mode);
2243                 if (error) {
2244                         fput(filp);
2245                         filp = ERR_PTR(error);
2246                 }
2247         }
2248         if (!IS_ERR(filp)) {
2249                 if (will_truncate) {
2250                         error = handle_truncate(filp);
2251                         if (error) {
2252                                 fput(filp);
2253                                 filp = ERR_PTR(error);
2254                         }
2255                 }
2256         }
2257         /*
2258          * It is now safe to drop the mnt write
2259          * because the filp has had a write taken
2260          * on its behalf.
2261          */
2262         if (will_truncate)
2263                 mnt_drop_write(nd->path.mnt);
2264         path_put(&nd->path);
2265         return filp;
2266
2267 exit:
2268         path_put(&nd->path);
2269         return ERR_PTR(error);
2270 }
2271
2272 /*
2273  * Handle O_CREAT case for do_filp_open
2274  */
2275 static struct file *do_last(struct nameidata *nd, struct path *path,
2276                             int open_flag, int acc_mode,
2277                             int mode, const char *pathname)
2278 {
2279         struct dentry *dir = nd->path.dentry;
2280         struct file *filp;
2281         int error = -EISDIR;
2282
2283         switch (nd->last_type) {
2284         case LAST_DOTDOT:
2285                 follow_dotdot(nd);
2286                 dir = nd->path.dentry;
2287         case LAST_DOT:
2288                 if (need_reval_dot(dir)) {
2289                         int status = d_revalidate(nd->path.dentry, nd);
2290                         if (!status)
2291                                 status = -ESTALE;
2292                         if (status < 0) {
2293                                 error = status;
2294                                 goto exit;
2295                         }
2296                 }
2297                 /* fallthrough */
2298         case LAST_ROOT:
2299                 goto exit;
2300         case LAST_BIND:
2301                 audit_inode(pathname, dir);
2302                 goto ok;
2303         }
2304
2305         /* trailing slashes? */
2306         if (nd->last.name[nd->last.len])
2307                 goto exit;
2308
2309         mutex_lock(&dir->d_inode->i_mutex);
2310
2311         path->dentry = lookup_hash(nd);
2312         path->mnt = nd->path.mnt;
2313
2314         error = PTR_ERR(path->dentry);
2315         if (IS_ERR(path->dentry)) {
2316                 mutex_unlock(&dir->d_inode->i_mutex);
2317                 goto exit;
2318         }
2319
2320         if (IS_ERR(nd->intent.open.file)) {
2321                 error = PTR_ERR(nd->intent.open.file);
2322                 goto exit_mutex_unlock;
2323         }
2324
2325         /* Negative dentry, just create the file */
2326         if (!path->dentry->d_inode) {
2327                 /*
2328                  * This write is needed to ensure that a
2329                  * ro->rw transition does not occur between
2330                  * the time when the file is created and when
2331                  * a permanent write count is taken through
2332                  * the 'struct file' in nameidata_to_filp().
2333                  */
2334                 error = mnt_want_write(nd->path.mnt);
2335                 if (error)
2336                         goto exit_mutex_unlock;
2337                 error = __open_namei_create(nd, path, open_flag, mode);
2338                 if (error) {
2339                         mnt_drop_write(nd->path.mnt);
2340                         goto exit;
2341                 }
2342                 filp = nameidata_to_filp(nd);
2343                 mnt_drop_write(nd->path.mnt);
2344                 path_put(&nd->path);
2345                 if (!IS_ERR(filp)) {
2346                         error = ima_file_check(filp, acc_mode);
2347                         if (error) {
2348                                 fput(filp);
2349                                 filp = ERR_PTR(error);
2350                         }
2351                 }
2352                 return filp;
2353         }
2354
2355         /*
2356          * It already exists.
2357          */
2358         mutex_unlock(&dir->d_inode->i_mutex);
2359         audit_inode(pathname, path->dentry);
2360
2361         error = -EEXIST;
2362         if (open_flag & O_EXCL)
2363                 goto exit_dput;
2364
2365         error = follow_managed(path, nd->flags);
2366         if (error < 0)
2367                 goto exit_dput;
2368
2369         error = -ENOENT;
2370         if (!path->dentry->d_inode)
2371                 goto exit_dput;
2372
2373         if (path->dentry->d_inode->i_op->follow_link)
2374                 return NULL;
2375
2376         path_to_nameidata(path, nd);
2377         nd->inode = path->dentry->d_inode;
2378         error = -EISDIR;
2379         if (S_ISDIR(nd->inode->i_mode))
2380                 goto exit;
2381 ok:
2382         filp = finish_open(nd, open_flag, acc_mode);
2383         return filp;
2384
2385 exit_mutex_unlock:
2386         mutex_unlock(&dir->d_inode->i_mutex);
2387 exit_dput:
2388         path_put_conditional(path, nd);
2389 exit:
2390         path_put(&nd->path);
2391         return ERR_PTR(error);
2392 }
2393
2394 /*
2395  * Note that the low bits of the passed in "open_flag"
2396  * are not the same as in the local variable "flag". See
2397  * open_to_namei_flags() for more details.
2398  */
2399 struct file *do_filp_open(int dfd, const char *pathname,
2400                 int open_flag, int mode, int acc_mode)
2401 {
2402         struct file *filp;
2403         struct nameidata nd;
2404         int error;
2405         struct path path;
2406         int count = 0;
2407         int flag = open_to_namei_flags(open_flag);
2408         int flags;
2409
2410         if (!(open_flag & O_CREAT))
2411                 mode = 0;
2412
2413         /* Must never be set by userspace */
2414         open_flag &= ~FMODE_NONOTIFY;
2415
2416         /*
2417          * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
2418          * check for O_DSYNC if the need any syncing at all we enforce it's
2419          * always set instead of having to deal with possibly weird behaviour
2420          * for malicious applications setting only __O_SYNC.
2421          */
2422         if (open_flag & __O_SYNC)
2423                 open_flag |= O_DSYNC;
2424
2425         if (!acc_mode)
2426                 acc_mode = MAY_OPEN | ACC_MODE(open_flag);
2427
2428         /* O_TRUNC implies we need access checks for write permissions */
2429         if (open_flag & O_TRUNC)
2430                 acc_mode |= MAY_WRITE;
2431
2432         /* Allow the LSM permission hook to distinguish append 
2433            access from general write access. */
2434         if (open_flag & O_APPEND)
2435                 acc_mode |= MAY_APPEND;
2436
2437         flags = LOOKUP_OPEN;
2438         if (open_flag & O_CREAT) {
2439                 flags |= LOOKUP_CREATE;
2440                 if (open_flag & O_EXCL)
2441                         flags |= LOOKUP_EXCL;
2442         }
2443         if (open_flag & O_DIRECTORY)
2444                 flags |= LOOKUP_DIRECTORY;
2445         if (!(open_flag & O_NOFOLLOW))
2446                 flags |= LOOKUP_FOLLOW;
2447
2448         filp = get_empty_filp();
2449         if (!filp)
2450                 return ERR_PTR(-ENFILE);
2451
2452         filp->f_flags = open_flag;
2453         nd.intent.open.file = filp;
2454         nd.intent.open.flags = flag;
2455         nd.intent.open.create_mode = mode;
2456
2457         if (open_flag & O_CREAT)
2458                 goto creat;
2459
2460         /* !O_CREAT, simple open */
2461         error = do_path_lookup(dfd, pathname, flags, &nd);
2462         if (unlikely(error))
2463                 goto out_filp2;
2464         error = -ELOOP;
2465         if (!(nd.flags & LOOKUP_FOLLOW)) {
2466                 if (nd.inode->i_op->follow_link)
2467                         goto out_path2;
2468         }
2469         error = -ENOTDIR;
2470         if (nd.flags & LOOKUP_DIRECTORY) {
2471                 if (!nd.inode->i_op->lookup)
2472                         goto out_path2;
2473         }
2474         audit_inode(pathname, nd.path.dentry);
2475         filp = finish_open(&nd, open_flag, acc_mode);
2476 out2:
2477         release_open_intent(&nd);
2478         return filp;
2479
2480 out_path2:
2481         path_put(&nd.path);
2482 out_filp2:
2483         filp = ERR_PTR(error);
2484         goto out2;
2485
2486 creat:
2487         /* OK, have to create the file. Find the parent. */
2488         error = path_init_rcu(dfd, pathname,
2489                         LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2490         if (error)
2491                 goto out_filp;
2492         error = path_walk_rcu(pathname, &nd);
2493         path_finish_rcu(&nd);
2494         if (unlikely(error == -ECHILD || error == -ESTALE)) {
2495                 /* slower, locked walk */
2496                 if (error == -ESTALE) {
2497 reval:
2498                         flags |= LOOKUP_REVAL;
2499                 }
2500                 error = path_init(dfd, pathname,
2501                                 LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2502                 if (error)
2503                         goto out_filp;
2504
2505                 error = path_walk_simple(pathname, &nd);
2506         }
2507         if (unlikely(error))
2508                 goto out_filp;
2509         if (unlikely(!audit_dummy_context()))
2510                 audit_inode(pathname, nd.path.dentry);
2511
2512         /*
2513          * We have the parent and last component.
2514          */
2515         nd.flags = flags;
2516         filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2517         while (unlikely(!filp)) { /* trailing symlink */
2518                 struct path link = path;
2519                 struct inode *linki = link.dentry->d_inode;
2520                 void *cookie;
2521                 error = -ELOOP;
2522                 if (!(nd.flags & LOOKUP_FOLLOW))
2523                         goto exit_dput;
2524                 if (count++ == 32)
2525                         goto exit_dput;
2526                 /*
2527                  * This is subtle. Instead of calling do_follow_link() we do
2528                  * the thing by hands. The reason is that this way we have zero
2529                  * link_count and path_walk() (called from ->follow_link)
2530                  * honoring LOOKUP_PARENT.  After that we have the parent and
2531                  * last component, i.e. we are in the same situation as after
2532                  * the first path_walk().  Well, almost - if the last component
2533                  * is normal we get its copy stored in nd->last.name and we will
2534                  * have to putname() it when we are done. Procfs-like symlinks
2535                  * just set LAST_BIND.
2536                  */
2537                 nd.flags |= LOOKUP_PARENT;
2538                 error = security_inode_follow_link(link.dentry, &nd);
2539                 if (error)
2540                         goto exit_dput;
2541                 error = __do_follow_link(&link, &nd, &cookie);
2542                 if (unlikely(error)) {
2543                         if (!IS_ERR(cookie) && linki->i_op->put_link)
2544                                 linki->i_op->put_link(link.dentry, &nd, cookie);
2545                         /* nd.path had been dropped */
2546                         nd.path = link;
2547                         goto out_path;
2548                 }
2549                 nd.flags &= ~LOOKUP_PARENT;
2550                 filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2551                 if (linki->i_op->put_link)
2552                         linki->i_op->put_link(link.dentry, &nd, cookie);
2553                 path_put(&link);
2554         }
2555 out:
2556         if (nd.root.mnt)
2557                 path_put(&nd.root);
2558         if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
2559                 goto reval;
2560         release_open_intent(&nd);
2561         return filp;
2562
2563 exit_dput:
2564         path_put_conditional(&path, &nd);
2565 out_path:
2566         path_put(&nd.path);
2567 out_filp:
2568         filp = ERR_PTR(error);
2569         goto out;
2570 }
2571
2572 /**
2573  * filp_open - open file and return file pointer
2574  *
2575  * @filename:   path to open
2576  * @flags:      open flags as per the open(2) second argument
2577  * @mode:       mode for the new file if O_CREAT is set, else ignored
2578  *
2579  * This is the helper to open a file from kernelspace if you really
2580  * have to.  But in generally you should not do this, so please move
2581  * along, nothing to see here..
2582  */
2583 struct file *filp_open(const char *filename, int flags, int mode)
2584 {
2585         return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
2586 }
2587 EXPORT_SYMBOL(filp_open);
2588
2589 /**
2590  * lookup_create - lookup a dentry, creating it if it doesn't exist
2591  * @nd: nameidata info
2592  * @is_dir: directory flag
2593  *
2594  * Simple function to lookup and return a dentry and create it
2595  * if it doesn't exist.  Is SMP-safe.
2596  *
2597  * Returns with nd->path.dentry->d_inode->i_mutex locked.
2598  */
2599 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2600 {
2601         struct dentry *dentry = ERR_PTR(-EEXIST);
2602
2603         mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2604         /*
2605          * Yucky last component or no last component at all?
2606          * (foo/., foo/.., /////)
2607          */
2608         if (nd->last_type != LAST_NORM)
2609                 goto fail;
2610         nd->flags &= ~LOOKUP_PARENT;
2611         nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2612         nd->intent.open.flags = O_EXCL;
2613
2614         /*
2615          * Do the final lookup.
2616          */
2617         dentry = lookup_hash(nd);
2618         if (IS_ERR(dentry))
2619                 goto fail;
2620
2621         if (dentry->d_inode)
2622                 goto eexist;
2623         /*
2624          * Special case - lookup gave negative, but... we had foo/bar/
2625          * From the vfs_mknod() POV we just have a negative dentry -
2626          * all is fine. Let's be bastards - you had / on the end, you've
2627          * been asking for (non-existent) directory. -ENOENT for you.
2628          */
2629         if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2630                 dput(dentry);
2631                 dentry = ERR_PTR(-ENOENT);
2632         }
2633         return dentry;
2634 eexist:
2635         dput(dentry);
2636         dentry = ERR_PTR(-EEXIST);
2637 fail:
2638         return dentry;
2639 }
2640 EXPORT_SYMBOL_GPL(lookup_create);
2641
2642 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2643 {
2644         int error = may_create(dir, dentry);
2645
2646         if (error)
2647                 return error;
2648
2649         if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2650                 return -EPERM;
2651
2652         if (!dir->i_op->mknod)
2653                 return -EPERM;
2654
2655         error = devcgroup_inode_mknod(mode, dev);
2656         if (error)
2657                 return error;
2658
2659         error = security_inode_mknod(dir, dentry, mode, dev);
2660         if (error)
2661                 return error;
2662
2663         error = dir->i_op->mknod(dir, dentry, mode, dev);
2664         if (!error)
2665                 fsnotify_create(dir, dentry);
2666         return error;
2667 }
2668
2669 static int may_mknod(mode_t mode)
2670 {
2671         switch (mode & S_IFMT) {
2672         case S_IFREG:
2673         case S_IFCHR:
2674         case S_IFBLK:
2675         case S_IFIFO:
2676         case S_IFSOCK:
2677         case 0: /* zero mode translates to S_IFREG */
2678                 return 0;
2679         case S_IFDIR:
2680                 return -EPERM;
2681         default:
2682                 return -EINVAL;
2683         }
2684 }
2685
2686 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2687                 unsigned, dev)
2688 {
2689         int error;
2690         char *tmp;
2691         struct dentry *dentry;
2692         struct nameidata nd;
2693
2694         if (S_ISDIR(mode))
2695                 return -EPERM;
2696
2697         error = user_path_parent(dfd, filename, &nd, &tmp);
2698         if (error)
2699                 return error;
2700
2701         dentry = lookup_create(&nd, 0);
2702         if (IS_ERR(dentry)) {
2703                 error = PTR_ERR(dentry);
2704                 goto out_unlock;
2705         }
2706         if (!IS_POSIXACL(nd.path.dentry->d_inode))
2707                 mode &= ~current_umask();
2708         error = may_mknod(mode);
2709         if (error)
2710                 goto out_dput;
2711         error = mnt_want_write(nd.path.mnt);
2712         if (error)
2713                 goto out_dput;
2714         error = security_path_mknod(&nd.path, dentry, mode, dev);
2715         if (error)
2716                 goto out_drop_write;
2717         switch (mode & S_IFMT) {
2718                 case 0: case S_IFREG:
2719                         error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2720                         break;
2721                 case S_IFCHR: case S_IFBLK:
2722                         error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2723                                         new_decode_dev(dev));
2724                         break;
2725                 case S_IFIFO: case S_IFSOCK:
2726                         error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2727                         break;
2728         }
2729 out_drop_write:
2730         mnt_drop_write(nd.path.mnt);
2731 out_dput:
2732         dput(dentry);
2733 out_unlock:
2734         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2735         path_put(&nd.path);
2736         putname(tmp);
2737
2738         return error;
2739 }
2740
2741 SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
2742 {
2743         return sys_mknodat(AT_FDCWD, filename, mode, dev);
2744 }
2745
2746 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2747 {
2748         int error = may_create(dir, dentry);
2749
2750         if (error)
2751                 return error;
2752
2753         if (!dir->i_op->mkdir)
2754                 return -EPERM;
2755
2756         mode &= (S_IRWXUGO|S_ISVTX);
2757         error = security_inode_mkdir(dir, dentry, mode);
2758         if (error)
2759                 return error;
2760
2761         error = dir->i_op->mkdir(dir, dentry, mode);
2762         if (!error)
2763                 fsnotify_mkdir(dir, dentry);
2764         return error;
2765 }
2766
2767 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
2768 {
2769         int error = 0;
2770         char * tmp;
2771         struct dentry *dentry;
2772         struct nameidata nd;
2773
2774         error = user_path_parent(dfd, pathname, &nd, &tmp);
2775         if (error)
2776                 goto out_err;
2777
2778         dentry = lookup_create(&nd, 1);
2779         error = PTR_ERR(dentry);
2780         if (IS_ERR(dentry))
2781                 goto out_unlock;
2782
2783         if (!IS_POSIXACL(nd.path.dentry->d_inode))
2784                 mode &= ~current_umask();
2785         error = mnt_want_write(nd.path.mnt);
2786         if (error)
2787                 goto out_dput;
2788         error = security_path_mkdir(&nd.path, dentry, mode);
2789         if (error)
2790                 goto out_drop_write;
2791         error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2792 out_drop_write:
2793         mnt_drop_write(nd.path.mnt);
2794 out_dput:
2795         dput(dentry);
2796 out_unlock:
2797         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2798         path_put(&nd.path);
2799         putname(tmp);
2800 out_err:
2801         return error;
2802 }
2803
2804 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
2805 {
2806         return sys_mkdirat(AT_FDCWD, pathname, mode);
2807 }
2808
2809 /*
2810  * We try to drop the dentry early: we should have
2811  * a usage count of 2 if we're the only user of this
2812  * dentry, and if that is true (possibly after pruning
2813  * the dcache), then we drop the dentry now.
2814  *
2815  * A low-level filesystem can, if it choses, legally
2816  * do a
2817  *
2818  *      if (!d_unhashed(dentry))
2819  *              return -EBUSY;
2820  *
2821  * if it cannot handle the case of removing a directory
2822  * that is still in use by something else..
2823  */
2824 void dentry_unhash(struct dentry *dentry)
2825 {
2826         dget(dentry);
2827         shrink_dcache_parent(dentry);
2828         spin_lock(&dentry->d_lock);
2829         if (dentry->d_count == 2)
2830                 __d_drop(dentry);
2831         spin_unlock(&dentry->d_lock);
2832 }
2833
2834 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2835 {
2836         int error = may_delete(dir, dentry, 1);
2837
2838         if (error)
2839                 return error;
2840
2841         if (!dir->i_op->rmdir)
2842                 return -EPERM;
2843
2844         mutex_lock(&dentry->d_inode->i_mutex);
2845         dentry_unhash(dentry);
2846         if (d_mountpoint(dentry))
2847                 error = -EBUSY;
2848         else {
2849                 error = security_inode_rmdir(dir, dentry);
2850                 if (!error) {
2851                         error = dir->i_op->rmdir(dir, dentry);
2852                         if (!error) {
2853                                 dentry->d_inode->i_flags |= S_DEAD;
2854                                 dont_mount(dentry);
2855                         }
2856                 }
2857         }
2858         mutex_unlock(&dentry->d_inode->i_mutex);
2859         if (!error) {
2860                 d_delete(dentry);
2861         }
2862         dput(dentry);
2863
2864         return error;
2865 }
2866
2867 static long do_rmdir(int dfd, const char __user *pathname)
2868 {
2869         int error = 0;
2870         char * name;
2871         struct dentry *dentry;
2872         struct nameidata nd;
2873
2874         error = user_path_parent(dfd, pathname, &nd, &name);
2875         if (error)
2876                 return error;
2877
2878         switch(nd.last_type) {
2879         case LAST_DOTDOT:
2880                 error = -ENOTEMPTY;
2881                 goto exit1;
2882         case LAST_DOT:
2883                 error = -EINVAL;
2884                 goto exit1;
2885         case LAST_ROOT:
2886                 error = -EBUSY;
2887                 goto exit1;
2888         }
2889
2890         nd.flags &= ~LOOKUP_PARENT;
2891
2892         mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2893         dentry = lookup_hash(&nd);
2894         error = PTR_ERR(dentry);
2895         if (IS_ERR(dentry))
2896                 goto exit2;
2897         error = mnt_want_write(nd.path.mnt);
2898         if (error)
2899                 goto exit3;
2900         error = security_path_rmdir(&nd.path, dentry);
2901         if (error)
2902                 goto exit4;
2903         error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2904 exit4:
2905         mnt_drop_write(nd.path.mnt);
2906 exit3:
2907         dput(dentry);
2908 exit2:
2909         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2910 exit1:
2911         path_put(&nd.path);
2912         putname(name);
2913         return error;
2914 }
2915
2916 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
2917 {
2918         return do_rmdir(AT_FDCWD, pathname);
2919 }
2920
2921 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2922 {
2923         int error = may_delete(dir, dentry, 0);
2924
2925         if (error)
2926                 return error;
2927
2928         if (!dir->i_op->unlink)
2929                 return -EPERM;
2930
2931         mutex_lock(&dentry->d_inode->i_mutex);
2932         if (d_mountpoint(dentry))
2933                 error = -EBUSY;
2934         else {
2935                 error = security_inode_unlink(dir, dentry);
2936                 if (!error) {
2937                         error = dir->i_op->unlink(dir, dentry);
2938                         if (!error)
2939                                 dont_mount(dentry);
2940                 }
2941         }
2942         mutex_unlock(&dentry->d_inode->i_mutex);
2943
2944         /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2945         if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2946                 fsnotify_link_count(dentry->d_inode);
2947                 d_delete(dentry);
2948         }
2949
2950         return error;
2951 }
2952
2953 /*
2954  * Make sure that the actual truncation of the file will occur outside its
2955  * directory's i_mutex.  Truncate can take a long time if there is a lot of
2956  * writeout happening, and we don't want to prevent access to the directory
2957  * while waiting on the I/O.
2958  */
2959 static long do_unlinkat(int dfd, const char __user *pathname)
2960 {
2961         int error;
2962         char *name;
2963         struct dentry *dentry;
2964         struct nameidata nd;
2965         struct inode *inode = NULL;
2966
2967         error = user_path_parent(dfd, pathname, &nd, &name);
2968         if (error)
2969                 return error;
2970
2971         error = -EISDIR;
2972         if (nd.last_type != LAST_NORM)
2973                 goto exit1;
2974
2975         nd.flags &= ~LOOKUP_PARENT;
2976
2977         mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2978         dentry = lookup_hash(&nd);
2979         error = PTR_ERR(dentry);
2980         if (!IS_ERR(dentry)) {
2981                 /* Why not before? Because we want correct error value */
2982                 if (nd.last.name[nd.last.len])
2983                         goto slashes;
2984                 inode = dentry->d_inode;
2985                 if (inode)
2986                         ihold(inode);
2987                 error = mnt_want_write(nd.path.mnt);
2988                 if (error)
2989                         goto exit2;
2990                 error = security_path_unlink(&nd.path, dentry);
2991                 if (error)
2992                         goto exit3;
2993                 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2994 exit3:
2995                 mnt_drop_write(nd.path.mnt);
2996         exit2:
2997                 dput(dentry);
2998         }
2999         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3000         if (inode)
3001                 iput(inode);    /* truncate the inode here */
3002 exit1:
3003         path_put(&nd.path);
3004         putname(name);
3005         return error;
3006
3007 slashes:
3008         error = !dentry->d_inode ? -ENOENT :
3009                 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3010         goto exit2;
3011 }
3012
3013 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3014 {
3015         if ((flag & ~AT_REMOVEDIR) != 0)
3016                 return -EINVAL;
3017
3018         if (flag & AT_REMOVEDIR)
3019                 return do_rmdir(dfd, pathname);
3020
3021         return do_unlinkat(dfd, pathname);
3022 }
3023
3024 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3025 {
3026         return do_unlinkat(AT_FDCWD, pathname);
3027 }
3028
3029 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3030 {
3031         int error = may_create(dir, dentry);
3032
3033         if (error)
3034                 return error;
3035
3036         if (!dir->i_op->symlink)
3037                 return -EPERM;
3038
3039         error = security_inode_symlink(dir, dentry, oldname);
3040         if (error)
3041                 return error;
3042
3043         error = dir->i_op->symlink(dir, dentry, oldname);
3044         if (!error)
3045                 fsnotify_create(dir, dentry);
3046         return error;
3047 }
3048
3049 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3050                 int, newdfd, const char __user *, newname)
3051 {
3052         int error;
3053         char *from;
3054         char *to;
3055         struct dentry *dentry;
3056         struct nameidata nd;
3057
3058         from = getname(oldname);
3059         if (IS_ERR(from))
3060                 return PTR_ERR(from);
3061
3062         error = user_path_parent(newdfd, newname, &nd, &to);
3063         if (error)
3064                 goto out_putname;
3065
3066         dentry = lookup_create(&nd, 0);
3067         error = PTR_ERR(dentry);
3068         if (IS_ERR(dentry))
3069                 goto out_unlock;
3070
3071         error = mnt_want_write(nd.path.mnt);
3072         if (error)
3073                 goto out_dput;
3074         error = security_path_symlink(&nd.path, dentry, from);
3075         if (error)
3076                 goto out_drop_write;
3077         error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
3078 out_drop_write:
3079         mnt_drop_write(nd.path.mnt);
3080 out_dput:
3081         dput(dentry);
3082 out_unlock:
3083         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3084         path_put(&nd.path);
3085         putname(to);
3086 out_putname:
3087         putname(from);
3088         return error;
3089 }
3090
3091 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3092 {
3093         return sys_symlinkat(oldname, AT_FDCWD, newname);
3094 }
3095
3096 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3097 {
3098         struct inode *inode = old_dentry->d_inode;
3099         int error;
3100
3101         if (!inode)
3102                 return -ENOENT;
3103
3104         error = may_create(dir, new_dentry);
3105         if (error)
3106                 return error;
3107
3108         if (dir->i_sb != inode->i_sb)
3109                 return -EXDEV;
3110
3111         /*
3112          * A link to an append-only or immutable file cannot be created.
3113          */
3114         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3115                 return -EPERM;
3116         if (!dir->i_op->link)
3117                 return -EPERM;
3118         if (S_ISDIR(inode->i_mode))
3119                 return -EPERM;
3120
3121         error = security_inode_link(old_dentry, dir, new_dentry);
3122         if (error)
3123                 return error;
3124
3125         mutex_lock(&inode->i_mutex);
3126         error = dir->i_op->link(old_dentry, dir, new_dentry);
3127         mutex_unlock(&inode->i_mutex);
3128         if (!error)
3129                 fsnotify_link(dir, inode, new_dentry);
3130         return error;
3131 }
3132
3133 /*
3134  * Hardlinks are often used in delicate situations.  We avoid
3135  * security-related surprises by not following symlinks on the
3136  * newname.  --KAB
3137  *
3138  * We don't follow them on the oldname either to be compatible
3139  * with linux 2.0, and to avoid hard-linking to directories
3140  * and other special files.  --ADM
3141  */
3142 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3143                 int, newdfd, const char __user *, newname, int, flags)
3144 {
3145         struct dentry *new_dentry;
3146         struct nameidata nd;
3147         struct path old_path;
3148         int error;
3149         char *to;
3150
3151         if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
3152                 return -EINVAL;
3153
3154         error = user_path_at(olddfd, oldname,
3155                              flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
3156                              &old_path);
3157         if (error)
3158                 return error;
3159
3160         error = user_path_parent(newdfd, newname, &nd, &to);
3161         if (error)
3162                 goto out;
3163         error = -EXDEV;
3164         if (old_path.mnt != nd.path.mnt)
3165                 goto out_release;
3166         new_dentry = lookup_create(&nd, 0);
3167         error = PTR_ERR(new_dentry);
3168         if (IS_ERR(new_dentry))
3169                 goto out_unlock;
3170         error = mnt_want_write(nd.path.mnt);
3171         if (error)
3172                 goto out_dput;
3173         error = security_path_link(old_path.dentry, &nd.path, new_dentry);
3174         if (error)
3175                 goto out_drop_write;
3176         error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
3177 out_drop_write:
3178         mnt_drop_write(nd.path.mnt);
3179 out_dput:
3180         dput(new_dentry);
3181 out_unlock:
3182         mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3183 out_release:
3184         path_put(&nd.path);
3185         putname(to);
3186 out:
3187         path_put(&old_path);
3188
3189         return error;
3190 }
3191
3192 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3193 {
3194         return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3195 }
3196
3197 /*
3198  * The worst of all namespace operations - renaming directory. "Perverted"
3199  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3200  * Problems:
3201  *      a) we can get into loop creation. Check is done in is_subdir().
3202  *      b) race potential - two innocent renames can create a loop together.
3203  *         That's where 4.4 screws up. Current fix: serialization on
3204  *         sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3205  *         story.
3206  *      c) we have to lock _three_ objects - parents and victim (if it exists).
3207  *         And that - after we got ->i_mutex on parents (until then we don't know
3208  *         whether the target exists).  Solution: try to be smart with locking
3209  *         order for inodes.  We rely on the fact that tree topology may change
3210  *         only under ->s_vfs_rename_mutex _and_ that parent of the object we
3211  *         move will be locked.  Thus we can rank directories by the tree
3212  *         (ancestors first) and rank all non-directories after them.
3213  *         That works since everybody except rename does "lock parent, lookup,
3214  *         lock child" and rename is under ->s_vfs_rename_mutex.
3215  *         HOWEVER, it relies on the assumption that any object with ->lookup()
3216  *         has no more than 1 dentry.  If "hybrid" objects will ever appear,
3217  *         we'd better make sure that there's no link(2) for them.
3218  *      d) some filesystems don't support opened-but-unlinked directories,
3219  *         either because of layout or because they are not ready to deal with
3220  *         all cases correctly. The latter will be fixed (taking this sort of
3221  *         stuff into VFS), but the former is not going away. Solution: the same
3222  *         trick as in rmdir().
3223  *      e) conversion from fhandle to dentry may come in the wrong moment - when
3224  *         we are removing the target. Solution: we will have to grab ->i_mutex
3225  *         in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3226  *         ->i_mutex on parents, which works but leads to some truly excessive
3227  *         locking].
3228  */
3229 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3230                           struct inode *new_dir, struct dentry *new_dentry)
3231 {
3232         int error = 0;
3233         struct inode *target;
3234
3235         /*
3236          * If we are going to change the parent - check write permissions,
3237          * we'll need to flip '..'.
3238          */
3239         if (new_dir != old_dir) {
3240                 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3241                 if (error)
3242                         return error;
3243         }
3244
3245         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3246         if (error)
3247                 return error;
3248
3249         target = new_dentry->d_inode;
3250         if (target)
3251                 mutex_lock(&target->i_mutex);
3252         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3253                 error = -EBUSY;
3254         else {
3255                 if (target)
3256                         dentry_unhash(new_dentry);
3257                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3258         }
3259         if (target) {
3260                 if (!error) {
3261                         target->i_flags |= S_DEAD;
3262                         dont_mount(new_dentry);
3263                 }
3264                 mutex_unlock(&target->i_mutex);
3265                 if (d_unhashed(new_dentry))
3266                         d_rehash(new_dentry);
3267                 dput(new_dentry);
3268         }
3269         if (!error)
3270                 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3271                         d_move(old_dentry,new_dentry);
3272         return error;
3273 }
3274
3275 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3276                             struct inode *new_dir, struct dentry *new_dentry)
3277 {
3278         struct inode *target;
3279         int error;
3280
3281         error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3282         if (error)
3283                 return error;
3284
3285         dget(new_dentry);
3286         target = new_dentry->d_inode;
3287         if (target)
3288                 mutex_lock(&target->i_mutex);
3289         if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3290                 error = -EBUSY;
3291         else
3292                 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3293         if (!error) {
3294                 if (target)
3295                         dont_mount(new_dentry);
3296                 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3297                         d_move(old_dentry, new_dentry);
3298         }
3299         if (target)
3300                 mutex_unlock(&target->i_mutex);
3301         dput(new_dentry);
3302         return error;
3303 }
3304
3305 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3306                struct inode *new_dir, struct dentry *new_dentry)
3307 {
3308         int error;
3309         int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3310         const unsigned char *old_name;
3311
3312         if (old_dentry->d_inode == new_dentry->d_inode)
3313                 return 0;
3314  
3315         error = may_delete(old_dir, old_dentry, is_dir);
3316         if (error)
3317                 return error;
3318
3319         if (!new_dentry->d_inode)
3320                 error = may_create(new_dir, new_dentry);
3321         else
3322                 error = may_delete(new_dir, new_dentry, is_dir);
3323         if (error)
3324                 return error;
3325
3326         if (!old_dir->i_op->rename)
3327                 return -EPERM;
3328
3329         old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3330
3331         if (is_dir)
3332                 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3333         else
3334                 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3335         if (!error)
3336                 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3337                               new_dentry->d_inode, old_dentry);
3338         fsnotify_oldname_free(old_name);
3339
3340         return error;
3341 }
3342
3343 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3344                 int, newdfd, const char __user *, newname)
3345 {
3346         struct dentry *old_dir, *new_dir;
3347         struct dentry *old_dentry, *new_dentry;
3348         struct dentry *trap;
3349         struct nameidata oldnd, newnd;
3350         char *from;
3351         char *to;
3352         int error;
3353
3354         error = user_path_parent(olddfd, oldname, &oldnd, &from);
3355         if (error)
3356                 goto exit;
3357
3358         error = user_path_parent(newdfd, newname, &newnd, &to);
3359         if (error)
3360                 goto exit1;
3361
3362         error = -EXDEV;
3363         if (oldnd.path.mnt != newnd.path.mnt)
3364                 goto exit2;
3365
3366         old_dir = oldnd.path.dentry;
3367         error = -EBUSY;
3368         if (oldnd.last_type != LAST_NORM)
3369                 goto exit2;
3370
3371         new_dir = newnd.path.dentry;
3372         if (newnd.last_type != LAST_NORM)
3373                 goto exit2;
3374
3375         oldnd.flags &= ~LOOKUP_PARENT;
3376         newnd.flags &= ~LOOKUP_PARENT;
3377         newnd.flags |= LOOKUP_RENAME_TARGET;
3378
3379         trap = lock_rename(new_dir, old_dir);
3380
3381         old_dentry = lookup_hash(&oldnd);
3382         error = PTR_ERR(old_dentry);
3383         if (IS_ERR(old_dentry))
3384                 goto exit3;
3385         /* source must exist */
3386         error = -ENOENT;
3387         if (!old_dentry->d_inode)
3388                 goto exit4;
3389         /* unless the source is a directory trailing slashes give -ENOTDIR */
3390         if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3391                 error = -ENOTDIR;
3392                 if (oldnd.last.name[oldnd.last.len])
3393                         goto exit4;
3394                 if (newnd.last.name[newnd.last.len])
3395                         goto exit4;
3396         }
3397         /* source should not be ancestor of target */
3398         error = -EINVAL;
3399         if (old_dentry == trap)
3400                 goto exit4;
3401         new_dentry = lookup_hash(&newnd);
3402         error = PTR_ERR(new_dentry);
3403         if (IS_ERR(new_dentry))
3404                 goto exit4;
3405         /* target should not be an ancestor of source */
3406         error = -ENOTEMPTY;
3407         if (new_dentry == trap)
3408                 goto exit5;
3409
3410         error = mnt_want_write(oldnd.path.mnt);
3411         if (error)
3412                 goto exit5;
3413         error = security_path_rename(&oldnd.path, old_dentry,
3414                                      &newnd.path, new_dentry);
3415         if (error)
3416                 goto exit6;
3417         error = vfs_rename(old_dir->d_inode, old_dentry,
3418                                    new_dir->d_inode, new_dentry);
3419 exit6:
3420         mnt_drop_write(oldnd.path.mnt);
3421 exit5:
3422         dput(new_dentry);
3423 exit4:
3424         dput(old_dentry);
3425 exit3:
3426         unlock_rename(new_dir, old_dir);
3427 exit2:
3428         path_put(&newnd.path);
3429         putname(to);
3430 exit1:
3431         path_put(&oldnd.path);
3432         putname(from);
3433 exit:
3434         return error;
3435 }
3436
3437 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3438 {
3439         return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3440 }
3441
3442 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3443 {
3444         int len;
3445
3446         len = PTR_ERR(link);
3447         if (IS_ERR(link))
3448                 goto out;
3449
3450         len = strlen(link);
3451         if (len > (unsigned) buflen)
3452                 len = buflen;
3453         if (copy_to_user(buffer, link, len))
3454                 len = -EFAULT;
3455 out:
3456         return len;
3457 }
3458
3459 /*
3460  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
3461  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
3462  * using) it for any given inode is up to filesystem.
3463  */
3464 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3465 {
3466         struct nameidata nd;
3467         void *cookie;
3468         int res;
3469
3470         nd.depth = 0;
3471         cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3472         if (IS_ERR(cookie))
3473                 return PTR_ERR(cookie);
3474
3475         res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3476         if (dentry->d_inode->i_op->put_link)
3477                 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3478         return res;
3479 }
3480
3481 int vfs_follow_link(struct nameidata *nd, const char *link)
3482 {
3483         return __vfs_follow_link(nd, link);
3484 }
3485
3486 /* get the link contents into pagecache */
3487 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3488 {
3489         char *kaddr;
3490         struct page *page;
3491         struct address_space *mapping = dentry->d_inode->i_mapping;
3492         page = read_mapping_page(mapping, 0, NULL);
3493         if (IS_ERR(page))
3494                 return (char*)page;
3495         *ppage = page;
3496         kaddr = kmap(page);
3497         nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3498         return kaddr;
3499 }
3500
3501 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3502 {
3503         struct page *page = NULL;
3504         char *s = page_getlink(dentry, &page);
3505         int res = vfs_readlink(dentry,buffer,buflen,s);
3506         if (page) {
3507                 kunmap(page);
3508                 page_cache_release(page);
3509         }
3510         return res;
3511 }
3512
3513 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3514 {
3515         struct page *page = NULL;
3516         nd_set_link(nd, page_getlink(dentry, &page));
3517         return page;
3518 }
3519
3520 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3521 {
3522         struct page *page = cookie;
3523
3524         if (page) {
3525                 kunmap(page);
3526                 page_cache_release(page);
3527         }
3528 }
3529
3530 /*
3531  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3532  */
3533 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3534 {
3535         struct address_space *mapping = inode->i_mapping;
3536         struct page *page;
3537         void *fsdata;
3538         int err;
3539         char *kaddr;
3540         unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3541         if (nofs)
3542                 flags |= AOP_FLAG_NOFS;
3543
3544 retry:
3545         err = pagecache_write_begin(NULL, mapping, 0, len-1,
3546                                 flags, &page, &fsdata);
3547         if (err)
3548                 goto fail;
3549
3550         kaddr = kmap_atomic(page, KM_USER0);
3551         memcpy(kaddr, symname, len-1);
3552         kunmap_atomic(kaddr, KM_USER0);
3553
3554         err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3555                                                         page, fsdata);
3556         if (err < 0)
3557                 goto fail;
3558         if (err < len-1)
3559                 goto retry;
3560
3561         mark_inode_dirty(inode);
3562         return 0;
3563 fail:
3564         return err;
3565 }
3566
3567 int page_symlink(struct inode *inode, const char *symname, int len)
3568 {
3569         return __page_symlink(inode, symname, len,
3570                         !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3571 }
3572
3573 const struct inode_operations page_symlink_inode_operations = {
3574         .readlink       = generic_readlink,
3575         .follow_link    = page_follow_link_light,
3576         .put_link       = page_put_link,
3577 };
3578
3579 EXPORT_SYMBOL(user_path_at);
3580 EXPORT_SYMBOL(follow_down_one);
3581 EXPORT_SYMBOL(follow_down);
3582 EXPORT_SYMBOL(follow_up);
3583 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3584 EXPORT_SYMBOL(getname);
3585 EXPORT_SYMBOL(lock_rename);
3586 EXPORT_SYMBOL(lookup_one_len);
3587 EXPORT_SYMBOL(page_follow_link_light);
3588 EXPORT_SYMBOL(page_put_link);
3589 EXPORT_SYMBOL(page_readlink);
3590 EXPORT_SYMBOL(__page_symlink);
3591 EXPORT_SYMBOL(page_symlink);
3592 EXPORT_SYMBOL(page_symlink_inode_operations);
3593 EXPORT_SYMBOL(path_lookup);
3594 EXPORT_SYMBOL(kern_path);
3595 EXPORT_SYMBOL(vfs_path_lookup);
3596 EXPORT_SYMBOL(inode_permission);
3597 EXPORT_SYMBOL(file_permission);
3598 EXPORT_SYMBOL(unlock_rename);
3599 EXPORT_SYMBOL(vfs_create);
3600 EXPORT_SYMBOL(vfs_follow_link);
3601 EXPORT_SYMBOL(vfs_link);
3602 EXPORT_SYMBOL(vfs_mkdir);
3603 EXPORT_SYMBOL(vfs_mknod);
3604 EXPORT_SYMBOL(generic_permission);
3605 EXPORT_SYMBOL(vfs_readlink);
3606 EXPORT_SYMBOL(vfs_rename);
3607 EXPORT_SYMBOL(vfs_rmdir);
3608 EXPORT_SYMBOL(vfs_symlink);
3609 EXPORT_SYMBOL(vfs_unlink);
3610 EXPORT_SYMBOL(dentry_unhash);
3611 EXPORT_SYMBOL(generic_readlink);