4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.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 <asm/uaccess.h>
36 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
38 /* [Feb-1997 T. Schoebel-Theuer]
39 * Fundamental changes in the pathname lookup mechanisms (namei)
40 * were necessary because of omirr. The reason is that omirr needs
41 * to know the _real_ pathname, not the user-supplied one, in case
42 * of symlinks (and also when transname replacements occur).
44 * The new code replaces the old recursive symlink resolution with
45 * an iterative one (in case of non-nested symlink chains). It does
46 * this with calls to <fs>_follow_link().
47 * As a side effect, dir_namei(), _namei() and follow_link() are now
48 * replaced with a single function lookup_dentry() that can handle all
49 * the special cases of the former code.
51 * With the new dcache, the pathname is stored at each inode, at least as
52 * long as the refcount of the inode is positive. As a side effect, the
53 * size of the dcache depends on the inode cache and thus is dynamic.
55 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
56 * resolution to correspond with current state of the code.
58 * Note that the symlink resolution is not *completely* iterative.
59 * There is still a significant amount of tail- and mid- recursion in
60 * the algorithm. Also, note that <fs>_readlink() is not used in
61 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
62 * may return different results than <fs>_follow_link(). Many virtual
63 * filesystems (including /proc) exhibit this behavior.
66 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
67 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
68 * and the name already exists in form of a symlink, try to create the new
69 * name indicated by the symlink. The old code always complained that the
70 * name already exists, due to not following the symlink even if its target
71 * is nonexistent. The new semantics affects also mknod() and link() when
72 * the name is a symlink pointing to a non-existant name.
74 * I don't know which semantics is the right one, since I have no access
75 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
76 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
77 * "old" one. Personally, I think the new semantics is much more logical.
78 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
79 * file does succeed in both HP-UX and SunOs, but not in Solaris
80 * and in the old Linux semantics.
83 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
84 * semantics. See the comments in "open_namei" and "do_link" below.
86 * [10-Sep-98 Alan Modra] Another symlink change.
89 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
90 * inside the path - always follow.
91 * in the last component in creation/removal/renaming - never follow.
92 * if LOOKUP_FOLLOW passed - follow.
93 * if the pathname has trailing slashes - follow.
94 * otherwise - don't follow.
95 * (applied in that order).
97 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
98 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
99 * During the 2.4 we need to fix the userland stuff depending on it -
100 * hopefully we will be able to get rid of that wart in 2.5. So far only
101 * XEmacs seems to be relying on it...
104 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
105 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
106 * any extra contention...
109 static int __link_path_walk(const char *name, struct nameidata *nd);
111 /* In order to reduce some races, while at the same time doing additional
112 * checking and hopefully speeding things up, we copy filenames to the
113 * kernel data space before using them..
115 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116 * PATH_MAX includes the nul terminator --RR.
118 static int do_getname(const char __user *filename, char *page)
121 unsigned long len = PATH_MAX;
123 if (!segment_eq(get_fs(), KERNEL_DS)) {
124 if ((unsigned long) filename >= TASK_SIZE)
126 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
130 retval = strncpy_from_user(page, filename, len);
134 return -ENAMETOOLONG;
140 char * getname(const char __user * filename)
144 result = ERR_PTR(-ENOMEM);
147 int retval = do_getname(filename, tmp);
152 result = ERR_PTR(retval);
155 audit_getname(result);
159 #ifdef CONFIG_AUDITSYSCALL
160 void putname(const char *name)
162 if (unlikely(!audit_dummy_context()))
167 EXPORT_SYMBOL(putname);
172 * generic_permission - check for access rights on a Posix-like filesystem
173 * @inode: inode to check access rights for
174 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
175 * @check_acl: optional callback to check for Posix ACLs
177 * Used to check for read/write/execute permissions on a file.
178 * We use "fsuid" for this, letting us set arbitrary permissions
179 * for filesystem access without changing the "normal" uids which
180 * are used for other things..
182 int generic_permission(struct inode *inode, int mask,
183 int (*check_acl)(struct inode *inode, int mask))
185 umode_t mode = inode->i_mode;
187 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
189 if (current->fsuid == inode->i_uid)
192 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
193 int error = check_acl(inode, mask);
194 if (error == -EACCES)
195 goto check_capabilities;
196 else if (error != -EAGAIN)
200 if (in_group_p(inode->i_gid))
205 * If the DACs are ok we don't need any capability check.
207 if ((mask & ~mode) == 0)
212 * Read/write DACs are always overridable.
213 * Executable DACs are overridable if at least one exec bit is set.
215 if (!(mask & MAY_EXEC) ||
216 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
217 if (capable(CAP_DAC_OVERRIDE))
221 * Searching includes executable on directories, else just read.
223 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
224 if (capable(CAP_DAC_READ_SEARCH))
230 int permission(struct inode *inode, int mask, struct nameidata *nd)
233 struct vfsmount *mnt = NULL;
238 if (mask & MAY_WRITE) {
239 umode_t mode = inode->i_mode;
242 * Nobody gets write access to a read-only fs.
244 if (IS_RDONLY(inode) &&
245 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
249 * Nobody gets write access to an immutable file.
251 if (IS_IMMUTABLE(inode))
255 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
257 * MAY_EXEC on regular files is denied if the fs is mounted
258 * with the "noexec" flag.
260 if (mnt && (mnt->mnt_flags & MNT_NOEXEC))
264 /* Ordinary permission routines do not understand MAY_APPEND. */
265 if (inode->i_op && inode->i_op->permission) {
268 if (nd->flags & LOOKUP_ACCESS)
270 if (nd->flags & LOOKUP_OPEN)
273 retval = inode->i_op->permission(inode, mask | extra);
276 * Exec permission on a regular file is denied if none
277 * of the execute bits are set.
279 * This check should be done by the ->permission()
282 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
283 !(inode->i_mode & S_IXUGO))
287 retval = generic_permission(inode, mask, NULL);
292 retval = devcgroup_inode_permission(inode, mask);
296 return security_inode_permission(inode,
297 mask & (MAY_READ|MAY_WRITE|MAY_EXEC), nd);
301 * vfs_permission - check for access rights to a given path
302 * @nd: lookup result that describes the path
303 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
305 * Used to check for read/write/execute permissions on a path.
306 * We use "fsuid" for this, letting us set arbitrary permissions
307 * for filesystem access without changing the "normal" uids which
308 * are used for other things.
310 int vfs_permission(struct nameidata *nd, int mask)
312 return permission(nd->path.dentry->d_inode, mask, nd);
316 * file_permission - check for additional access rights to a given file
317 * @file: file to check access rights for
318 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
320 * Used to check for read/write/execute permissions on an already opened
324 * Do not use this function in new code. All access checks should
325 * be done using vfs_permission().
327 int file_permission(struct file *file, int mask)
329 return permission(file->f_path.dentry->d_inode, mask, NULL);
333 * get_write_access() gets write permission for a file.
334 * put_write_access() releases this write permission.
335 * This is used for regular files.
336 * We cannot support write (and maybe mmap read-write shared) accesses and
337 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
338 * can have the following values:
339 * 0: no writers, no VM_DENYWRITE mappings
340 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
341 * > 0: (i_writecount) users are writing to the file.
343 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
344 * except for the cases where we don't hold i_writecount yet. Then we need to
345 * use {get,deny}_write_access() - these functions check the sign and refuse
346 * to do the change if sign is wrong. Exclusion between them is provided by
347 * the inode->i_lock spinlock.
350 int get_write_access(struct inode * inode)
352 spin_lock(&inode->i_lock);
353 if (atomic_read(&inode->i_writecount) < 0) {
354 spin_unlock(&inode->i_lock);
357 atomic_inc(&inode->i_writecount);
358 spin_unlock(&inode->i_lock);
363 int deny_write_access(struct file * file)
365 struct inode *inode = file->f_path.dentry->d_inode;
367 spin_lock(&inode->i_lock);
368 if (atomic_read(&inode->i_writecount) > 0) {
369 spin_unlock(&inode->i_lock);
372 atomic_dec(&inode->i_writecount);
373 spin_unlock(&inode->i_lock);
379 * path_get - get a reference to a path
380 * @path: path to get the reference to
382 * Given a path increment the reference count to the dentry and the vfsmount.
384 void path_get(struct path *path)
389 EXPORT_SYMBOL(path_get);
392 * path_put - put a reference to a path
393 * @path: path to put the reference to
395 * Given a path decrement the reference count to the dentry and the vfsmount.
397 void path_put(struct path *path)
402 EXPORT_SYMBOL(path_put);
405 * release_open_intent - free up open intent resources
406 * @nd: pointer to nameidata
408 void release_open_intent(struct nameidata *nd)
410 if (nd->intent.open.file->f_path.dentry == NULL)
411 put_filp(nd->intent.open.file);
413 fput(nd->intent.open.file);
416 static inline struct dentry *
417 do_revalidate(struct dentry *dentry, struct nameidata *nd)
419 int status = dentry->d_op->d_revalidate(dentry, nd);
420 if (unlikely(status <= 0)) {
422 * The dentry failed validation.
423 * If d_revalidate returned 0 attempt to invalidate
424 * the dentry otherwise d_revalidate is asking us
425 * to return a fail status.
428 if (!d_invalidate(dentry)) {
434 dentry = ERR_PTR(status);
441 * Internal lookup() using the new generic dcache.
444 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
446 struct dentry * dentry = __d_lookup(parent, name);
448 /* lockess __d_lookup may fail due to concurrent d_move()
449 * in some unrelated directory, so try with d_lookup
452 dentry = d_lookup(parent, name);
454 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
455 dentry = do_revalidate(dentry, nd);
461 * Short-cut version of permission(), for calling by
462 * path_walk(), when dcache lock is held. Combines parts
463 * of permission() and generic_permission(), and tests ONLY for
464 * MAY_EXEC permission.
466 * If appropriate, check DAC only. If not appropriate, or
467 * short-cut DAC fails, then call permission() to do more
468 * complete permission check.
470 static int exec_permission_lite(struct inode *inode,
471 struct nameidata *nd)
473 umode_t mode = inode->i_mode;
475 if (inode->i_op && inode->i_op->permission)
478 if (current->fsuid == inode->i_uid)
480 else if (in_group_p(inode->i_gid))
486 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
489 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
492 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
497 return security_inode_permission(inode, MAY_EXEC, nd);
501 * This is called when everything else fails, and we actually have
502 * to go to the low-level filesystem to find out what we should do..
504 * We get the directory semaphore, and after getting that we also
505 * make sure that nobody added the entry to the dcache in the meantime..
508 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
510 struct dentry * result;
511 struct inode *dir = parent->d_inode;
513 mutex_lock(&dir->i_mutex);
515 * First re-do the cached lookup just in case it was created
516 * while we waited for the directory semaphore..
518 * FIXME! This could use version numbering or similar to
519 * avoid unnecessary cache lookups.
521 * The "dcache_lock" is purely to protect the RCU list walker
522 * from concurrent renames at this point (we mustn't get false
523 * negatives from the RCU list walk here, unlike the optimistic
526 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
528 result = d_lookup(parent, name);
530 struct dentry *dentry;
532 /* Don't create child dentry for a dead directory. */
533 result = ERR_PTR(-ENOENT);
537 dentry = d_alloc(parent, name);
538 result = ERR_PTR(-ENOMEM);
540 result = dir->i_op->lookup(dir, dentry, nd);
547 mutex_unlock(&dir->i_mutex);
552 * Uhhuh! Nasty case: the cache was re-populated while
553 * we waited on the semaphore. Need to revalidate.
555 mutex_unlock(&dir->i_mutex);
556 if (result->d_op && result->d_op->d_revalidate) {
557 result = do_revalidate(result, nd);
559 result = ERR_PTR(-ENOENT);
565 static __always_inline void
566 walk_init_root(const char *name, struct nameidata *nd)
568 struct fs_struct *fs = current->fs;
570 read_lock(&fs->lock);
573 read_unlock(&fs->lock);
577 * Wrapper to retry pathname resolution whenever the underlying
578 * file system returns an ESTALE.
580 * Retry the whole path once, forcing real lookup requests
581 * instead of relying on the dcache.
583 static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
585 struct path save = nd->path;
588 /* make sure the stuff we saved doesn't go away */
591 result = __link_path_walk(name, nd);
592 if (result == -ESTALE) {
593 /* nd->path had been dropped */
596 nd->flags |= LOOKUP_REVAL;
597 result = __link_path_walk(name, nd);
605 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
614 walk_init_root(link, nd);
616 res = link_path_walk(link, nd);
617 if (nd->depth || res || nd->last_type!=LAST_NORM)
620 * If it is an iterative symlinks resolution in open_namei() we
621 * have to copy the last component. And all that crap because of
622 * bloody create() on broken symlinks. Furrfu...
625 if (unlikely(!name)) {
629 strcpy(name, nd->last.name);
630 nd->last.name = name;
634 return PTR_ERR(link);
637 static void path_put_conditional(struct path *path, struct nameidata *nd)
640 if (path->mnt != nd->path.mnt)
644 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
646 dput(nd->path.dentry);
647 if (nd->path.mnt != path->mnt)
648 mntput(nd->path.mnt);
649 nd->path.mnt = path->mnt;
650 nd->path.dentry = path->dentry;
653 static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
657 struct dentry *dentry = path->dentry;
659 touch_atime(path->mnt, dentry);
660 nd_set_link(nd, NULL);
662 if (path->mnt != nd->path.mnt) {
663 path_to_nameidata(path, nd);
667 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
668 error = PTR_ERR(cookie);
669 if (!IS_ERR(cookie)) {
670 char *s = nd_get_link(nd);
673 error = __vfs_follow_link(nd, s);
674 if (dentry->d_inode->i_op->put_link)
675 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
683 * This limits recursive symlink follows to 8, while
684 * limiting consecutive symlinks to 40.
686 * Without that kind of total limit, nasty chains of consecutive
687 * symlinks can cause almost arbitrarily long lookups.
689 static inline int do_follow_link(struct path *path, struct nameidata *nd)
692 if (current->link_count >= MAX_NESTED_LINKS)
694 if (current->total_link_count >= 40)
696 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
698 err = security_inode_follow_link(path->dentry, nd);
701 current->link_count++;
702 current->total_link_count++;
704 err = __do_follow_link(path, nd);
705 current->link_count--;
709 path_put_conditional(path, nd);
714 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
716 struct vfsmount *parent;
717 struct dentry *mountpoint;
718 spin_lock(&vfsmount_lock);
719 parent=(*mnt)->mnt_parent;
720 if (parent == *mnt) {
721 spin_unlock(&vfsmount_lock);
725 mountpoint=dget((*mnt)->mnt_mountpoint);
726 spin_unlock(&vfsmount_lock);
728 *dentry = mountpoint;
734 /* no need for dcache_lock, as serialization is taken care in
737 static int __follow_mount(struct path *path)
740 while (d_mountpoint(path->dentry)) {
741 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
748 path->dentry = dget(mounted->mnt_root);
754 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
756 while (d_mountpoint(*dentry)) {
757 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
763 *dentry = dget(mounted->mnt_root);
767 /* no need for dcache_lock, as serialization is taken care in
770 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
772 struct vfsmount *mounted;
774 mounted = lookup_mnt(*mnt, *dentry);
779 *dentry = dget(mounted->mnt_root);
785 static __always_inline void follow_dotdot(struct nameidata *nd)
787 struct fs_struct *fs = current->fs;
790 struct vfsmount *parent;
791 struct dentry *old = nd->path.dentry;
793 read_lock(&fs->lock);
794 if (nd->path.dentry == fs->root.dentry &&
795 nd->path.mnt == fs->root.mnt) {
796 read_unlock(&fs->lock);
799 read_unlock(&fs->lock);
800 spin_lock(&dcache_lock);
801 if (nd->path.dentry != nd->path.mnt->mnt_root) {
802 nd->path.dentry = dget(nd->path.dentry->d_parent);
803 spin_unlock(&dcache_lock);
807 spin_unlock(&dcache_lock);
808 spin_lock(&vfsmount_lock);
809 parent = nd->path.mnt->mnt_parent;
810 if (parent == nd->path.mnt) {
811 spin_unlock(&vfsmount_lock);
815 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
816 spin_unlock(&vfsmount_lock);
818 mntput(nd->path.mnt);
819 nd->path.mnt = parent;
821 follow_mount(&nd->path.mnt, &nd->path.dentry);
825 * It's more convoluted than I'd like it to be, but... it's still fairly
826 * small and for now I'd prefer to have fast path as straight as possible.
827 * It _is_ time-critical.
829 static int do_lookup(struct nameidata *nd, struct qstr *name,
832 struct vfsmount *mnt = nd->path.mnt;
833 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
837 if (dentry->d_op && dentry->d_op->d_revalidate)
838 goto need_revalidate;
841 path->dentry = dentry;
842 __follow_mount(path);
846 dentry = real_lookup(nd->path.dentry, name, nd);
852 dentry = do_revalidate(dentry, nd);
860 return PTR_ERR(dentry);
865 * This is the basic name resolution function, turning a pathname into
866 * the final dentry. We expect 'base' to be positive and a directory.
868 * Returns 0 and nd will have valid dentry and mnt on success.
869 * Returns error and drops reference to input namei data on failure.
871 static int __link_path_walk(const char *name, struct nameidata *nd)
876 unsigned int lookup_flags = nd->flags;
883 inode = nd->path.dentry->d_inode;
885 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
887 /* At this point we know we have a real path component. */
893 nd->flags |= LOOKUP_CONTINUE;
894 err = exec_permission_lite(inode, nd);
896 err = vfs_permission(nd, MAY_EXEC);
901 c = *(const unsigned char *)name;
903 hash = init_name_hash();
906 hash = partial_name_hash(c, hash);
907 c = *(const unsigned char *)name;
908 } while (c && (c != '/'));
909 this.len = name - (const char *) this.name;
910 this.hash = end_name_hash(hash);
912 /* remove trailing slashes? */
915 while (*++name == '/');
917 goto last_with_slashes;
920 * "." and ".." are special - ".." especially so because it has
921 * to be able to know about the current root directory and
922 * parent relationships.
924 if (this.name[0] == '.') switch (this.len) {
928 if (this.name[1] != '.')
931 inode = nd->path.dentry->d_inode;
937 * See if the low-level filesystem might want
938 * to use its own hash..
940 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
941 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
946 /* This does the actual lookups.. */
947 err = do_lookup(nd, &this, &next);
952 inode = next.dentry->d_inode;
959 if (inode->i_op->follow_link) {
960 err = do_follow_link(&next, nd);
964 inode = nd->path.dentry->d_inode;
971 path_to_nameidata(&next, nd);
973 if (!inode->i_op->lookup)
976 /* here ends the main loop */
979 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
981 /* Clear LOOKUP_CONTINUE iff it was previously unset */
982 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
983 if (lookup_flags & LOOKUP_PARENT)
985 if (this.name[0] == '.') switch (this.len) {
989 if (this.name[1] != '.')
992 inode = nd->path.dentry->d_inode;
997 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
998 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
1003 err = do_lookup(nd, &this, &next);
1006 inode = next.dentry->d_inode;
1007 if ((lookup_flags & LOOKUP_FOLLOW)
1008 && inode && inode->i_op && inode->i_op->follow_link) {
1009 err = do_follow_link(&next, nd);
1012 inode = nd->path.dentry->d_inode;
1014 path_to_nameidata(&next, nd);
1018 if (lookup_flags & LOOKUP_DIRECTORY) {
1020 if (!inode->i_op || !inode->i_op->lookup)
1026 nd->last_type = LAST_NORM;
1027 if (this.name[0] != '.')
1030 nd->last_type = LAST_DOT;
1031 else if (this.len == 2 && this.name[1] == '.')
1032 nd->last_type = LAST_DOTDOT;
1037 * We bypassed the ordinary revalidation routines.
1038 * We may need to check the cached dentry for staleness.
1040 if (nd->path.dentry && nd->path.dentry->d_sb &&
1041 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
1043 /* Note: we do not d_invalidate() */
1044 if (!nd->path.dentry->d_op->d_revalidate(
1045 nd->path.dentry, nd))
1051 path_put_conditional(&next, nd);
1054 path_put(&nd->path);
1059 static int path_walk(const char *name, struct nameidata *nd)
1061 current->total_link_count = 0;
1062 return link_path_walk(name, nd);
1065 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1066 static int do_path_lookup(int dfd, const char *name,
1067 unsigned int flags, struct nameidata *nd)
1072 struct fs_struct *fs = current->fs;
1074 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1079 read_lock(&fs->lock);
1080 nd->path = fs->root;
1081 path_get(&fs->root);
1082 read_unlock(&fs->lock);
1083 } else if (dfd == AT_FDCWD) {
1084 read_lock(&fs->lock);
1087 read_unlock(&fs->lock);
1089 struct dentry *dentry;
1091 file = fget_light(dfd, &fput_needed);
1096 dentry = file->f_path.dentry;
1099 if (!S_ISDIR(dentry->d_inode->i_mode))
1102 retval = file_permission(file, MAY_EXEC);
1106 nd->path = file->f_path;
1107 path_get(&file->f_path);
1109 fput_light(file, fput_needed);
1112 retval = path_walk(name, nd);
1113 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1114 nd->path.dentry->d_inode))
1115 audit_inode(name, nd->path.dentry);
1120 fput_light(file, fput_needed);
1124 int path_lookup(const char *name, unsigned int flags,
1125 struct nameidata *nd)
1127 return do_path_lookup(AT_FDCWD, name, flags, nd);
1131 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1132 * @dentry: pointer to dentry of the base directory
1133 * @mnt: pointer to vfs mount of the base directory
1134 * @name: pointer to file name
1135 * @flags: lookup flags
1136 * @nd: pointer to nameidata
1138 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1139 const char *name, unsigned int flags,
1140 struct nameidata *nd)
1144 /* same as do_path_lookup */
1145 nd->last_type = LAST_ROOT;
1149 nd->path.dentry = dentry;
1151 path_get(&nd->path);
1153 retval = path_walk(name, nd);
1154 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1155 nd->path.dentry->d_inode))
1156 audit_inode(name, nd->path.dentry);
1162 static int __path_lookup_intent_open(int dfd, const char *name,
1163 unsigned int lookup_flags, struct nameidata *nd,
1164 int open_flags, int create_mode)
1166 struct file *filp = get_empty_filp();
1171 nd->intent.open.file = filp;
1172 nd->intent.open.flags = open_flags;
1173 nd->intent.open.create_mode = create_mode;
1174 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1175 if (IS_ERR(nd->intent.open.file)) {
1177 err = PTR_ERR(nd->intent.open.file);
1178 path_put(&nd->path);
1180 } else if (err != 0)
1181 release_open_intent(nd);
1186 * path_lookup_open - lookup a file path with open intent
1187 * @dfd: the directory to use as base, or AT_FDCWD
1188 * @name: pointer to file name
1189 * @lookup_flags: lookup intent flags
1190 * @nd: pointer to nameidata
1191 * @open_flags: open intent flags
1193 int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1194 struct nameidata *nd, int open_flags)
1196 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
1201 * path_lookup_create - lookup a file path with open + create intent
1202 * @dfd: the directory to use as base, or AT_FDCWD
1203 * @name: pointer to file name
1204 * @lookup_flags: lookup intent flags
1205 * @nd: pointer to nameidata
1206 * @open_flags: open intent flags
1207 * @create_mode: create intent flags
1209 static int path_lookup_create(int dfd, const char *name,
1210 unsigned int lookup_flags, struct nameidata *nd,
1211 int open_flags, int create_mode)
1213 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1214 nd, open_flags, create_mode);
1217 int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1218 struct nameidata *nd, int open_flags)
1220 char *tmp = getname(name);
1221 int err = PTR_ERR(tmp);
1224 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
1230 static struct dentry *__lookup_hash(struct qstr *name,
1231 struct dentry *base, struct nameidata *nd)
1233 struct dentry *dentry;
1234 struct inode *inode;
1237 inode = base->d_inode;
1240 * See if the low-level filesystem might want
1241 * to use its own hash..
1243 if (base->d_op && base->d_op->d_hash) {
1244 err = base->d_op->d_hash(base, name);
1245 dentry = ERR_PTR(err);
1250 dentry = cached_lookup(base, name, nd);
1254 /* Don't create child dentry for a dead directory. */
1255 dentry = ERR_PTR(-ENOENT);
1256 if (IS_DEADDIR(inode))
1259 new = d_alloc(base, name);
1260 dentry = ERR_PTR(-ENOMEM);
1263 dentry = inode->i_op->lookup(inode, new, nd);
1274 * Restricted form of lookup. Doesn't follow links, single-component only,
1275 * needs parent already locked. Doesn't follow mounts.
1278 static struct dentry *lookup_hash(struct nameidata *nd)
1282 err = permission(nd->path.dentry->d_inode, MAY_EXEC, nd);
1284 return ERR_PTR(err);
1285 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1288 static int __lookup_one_len(const char *name, struct qstr *this,
1289 struct dentry *base, int len)
1299 hash = init_name_hash();
1301 c = *(const unsigned char *)name++;
1302 if (c == '/' || c == '\0')
1304 hash = partial_name_hash(c, hash);
1306 this->hash = end_name_hash(hash);
1311 * lookup_one_len - filesystem helper to lookup single pathname component
1312 * @name: pathname component to lookup
1313 * @base: base directory to lookup from
1314 * @len: maximum length @len should be interpreted to
1316 * Note that this routine is purely a helper for filesystem usage and should
1317 * not be called by generic code. Also note that by using this function the
1318 * nameidata argument is passed to the filesystem methods and a filesystem
1319 * using this helper needs to be prepared for that.
1321 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1326 err = __lookup_one_len(name, &this, base, len);
1328 return ERR_PTR(err);
1330 err = permission(base->d_inode, MAY_EXEC, NULL);
1332 return ERR_PTR(err);
1333 return __lookup_hash(&this, base, NULL);
1337 * lookup_one_noperm - bad hack for sysfs
1338 * @name: pathname component to lookup
1339 * @base: base directory to lookup from
1341 * This is a variant of lookup_one_len that doesn't perform any permission
1342 * checks. It's a horrible hack to work around the braindead sysfs
1343 * architecture and should not be used anywhere else.
1345 * DON'T USE THIS FUNCTION EVER, thanks.
1347 struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
1352 err = __lookup_one_len(name, &this, base, strlen(name));
1354 return ERR_PTR(err);
1355 return __lookup_hash(&this, base, NULL);
1358 int __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1359 struct nameidata *nd)
1361 char *tmp = getname(name);
1362 int err = PTR_ERR(tmp);
1365 err = do_path_lookup(dfd, tmp, flags, nd);
1371 int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1373 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1377 * It's inline, so penalty for filesystems that don't use sticky bit is
1380 static inline int check_sticky(struct inode *dir, struct inode *inode)
1382 if (!(dir->i_mode & S_ISVTX))
1384 if (inode->i_uid == current->fsuid)
1386 if (dir->i_uid == current->fsuid)
1388 return !capable(CAP_FOWNER);
1392 * Check whether we can remove a link victim from directory dir, check
1393 * whether the type of victim is right.
1394 * 1. We can't do it if dir is read-only (done in permission())
1395 * 2. We should have write and exec permissions on dir
1396 * 3. We can't remove anything from append-only dir
1397 * 4. We can't do anything with immutable dir (done in permission())
1398 * 5. If the sticky bit on dir is set we should either
1399 * a. be owner of dir, or
1400 * b. be owner of victim, or
1401 * c. have CAP_FOWNER capability
1402 * 6. If the victim is append-only or immutable we can't do antyhing with
1403 * links pointing to it.
1404 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1405 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1406 * 9. We can't remove a root or mountpoint.
1407 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1408 * nfs_async_unlink().
1410 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1414 if (!victim->d_inode)
1417 BUG_ON(victim->d_parent->d_inode != dir);
1418 audit_inode_child(victim->d_name.name, victim, dir);
1420 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1425 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1426 IS_IMMUTABLE(victim->d_inode))
1429 if (!S_ISDIR(victim->d_inode->i_mode))
1431 if (IS_ROOT(victim))
1433 } else if (S_ISDIR(victim->d_inode->i_mode))
1435 if (IS_DEADDIR(dir))
1437 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1442 /* Check whether we can create an object with dentry child in directory
1444 * 1. We can't do it if child already exists (open has special treatment for
1445 * this case, but since we are inlined it's OK)
1446 * 2. We can't do it if dir is read-only (done in permission())
1447 * 3. We should have write and exec permissions on dir
1448 * 4. We can't do it if dir is immutable (done in permission())
1450 static inline int may_create(struct inode *dir, struct dentry *child,
1451 struct nameidata *nd)
1455 if (IS_DEADDIR(dir))
1457 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1461 * O_DIRECTORY translates into forcing a directory lookup.
1463 static inline int lookup_flags(unsigned int f)
1465 unsigned long retval = LOOKUP_FOLLOW;
1468 retval &= ~LOOKUP_FOLLOW;
1470 if (f & O_DIRECTORY)
1471 retval |= LOOKUP_DIRECTORY;
1477 * p1 and p2 should be directories on the same fs.
1479 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1484 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1488 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1490 for (p = p1; p->d_parent != p; p = p->d_parent) {
1491 if (p->d_parent == p2) {
1492 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1493 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1498 for (p = p2; p->d_parent != p; p = p->d_parent) {
1499 if (p->d_parent == p1) {
1500 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1501 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1506 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1507 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1511 void unlock_rename(struct dentry *p1, struct dentry *p2)
1513 mutex_unlock(&p1->d_inode->i_mutex);
1515 mutex_unlock(&p2->d_inode->i_mutex);
1516 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1520 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1521 struct nameidata *nd)
1523 int error = may_create(dir, dentry, nd);
1528 if (!dir->i_op || !dir->i_op->create)
1529 return -EACCES; /* shouldn't it be ENOSYS? */
1532 error = security_inode_create(dir, dentry, mode);
1536 error = dir->i_op->create(dir, dentry, mode, nd);
1538 fsnotify_create(dir, dentry);
1542 int may_open(struct nameidata *nd, int acc_mode, int flag)
1544 struct dentry *dentry = nd->path.dentry;
1545 struct inode *inode = dentry->d_inode;
1551 if (S_ISLNK(inode->i_mode))
1554 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
1558 * FIFO's, sockets and device files are special: they don't
1559 * actually live on the filesystem itself, and as such you
1560 * can write to them even if the filesystem is read-only.
1562 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1564 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1565 if (nd->path.mnt->mnt_flags & MNT_NODEV)
1571 error = vfs_permission(nd, acc_mode);
1575 * An append-only file must be opened in append mode for writing.
1577 if (IS_APPEND(inode)) {
1578 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1584 /* O_NOATIME can only be set by the owner or superuser */
1585 if (flag & O_NOATIME)
1586 if (!is_owner_or_cap(inode))
1590 * Ensure there are no outstanding leases on the file.
1592 error = break_lease(inode, flag);
1596 if (flag & O_TRUNC) {
1597 error = get_write_access(inode);
1602 * Refuse to truncate files with mandatory locks held on them.
1604 error = locks_verify_locked(inode);
1608 error = do_truncate(dentry, 0,
1609 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1612 put_write_access(inode);
1616 if (flag & FMODE_WRITE)
1623 * Be careful about ever adding any more callers of this
1624 * function. Its flags must be in the namei format, not
1625 * what get passed to sys_open().
1627 static int __open_namei_create(struct nameidata *nd, struct path *path,
1631 struct dentry *dir = nd->path.dentry;
1633 if (!IS_POSIXACL(dir->d_inode))
1634 mode &= ~current->fs->umask;
1635 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1636 mutex_unlock(&dir->d_inode->i_mutex);
1637 dput(nd->path.dentry);
1638 nd->path.dentry = path->dentry;
1641 /* Don't check for write permission, don't truncate */
1642 return may_open(nd, 0, flag & ~O_TRUNC);
1646 * Note that while the flag value (low two bits) for sys_open means:
1651 * it is changed into
1652 * 00 - no permissions needed
1653 * 01 - read-permission
1654 * 10 - write-permission
1656 * for the internal routines (ie open_namei()/follow_link() etc)
1657 * This is more logical, and also allows the 00 "no perm needed"
1658 * to be used for symlinks (where the permissions are checked
1662 static inline int open_to_namei_flags(int flag)
1664 if ((flag+1) & O_ACCMODE)
1669 static int open_will_write_to_fs(int flag, struct inode *inode)
1672 * We'll never write to the fs underlying
1675 if (special_file(inode->i_mode))
1677 return (flag & O_TRUNC);
1681 * Note that the low bits of the passed in "open_flag"
1682 * are not the same as in the local variable "flag". See
1683 * open_to_namei_flags() for more details.
1685 struct file *do_filp_open(int dfd, const char *pathname,
1686 int open_flag, int mode)
1689 struct nameidata nd;
1690 int acc_mode, error;
1695 int flag = open_to_namei_flags(open_flag);
1697 acc_mode = ACC_MODE(flag);
1699 /* O_TRUNC implies we need access checks for write permissions */
1701 acc_mode |= MAY_WRITE;
1703 /* Allow the LSM permission hook to distinguish append
1704 access from general write access. */
1705 if (flag & O_APPEND)
1706 acc_mode |= MAY_APPEND;
1709 * The simplest case - just a plain lookup.
1711 if (!(flag & O_CREAT)) {
1712 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1715 return ERR_PTR(error);
1720 * Create - we need to know the parent.
1722 error = path_lookup_create(dfd, pathname, LOOKUP_PARENT,
1725 return ERR_PTR(error);
1728 * We have the parent and last component. First of all, check
1729 * that we are not asked to creat(2) an obvious directory - that
1733 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
1736 dir = nd.path.dentry;
1737 nd.flags &= ~LOOKUP_PARENT;
1738 mutex_lock(&dir->d_inode->i_mutex);
1739 path.dentry = lookup_hash(&nd);
1740 path.mnt = nd.path.mnt;
1743 error = PTR_ERR(path.dentry);
1744 if (IS_ERR(path.dentry)) {
1745 mutex_unlock(&dir->d_inode->i_mutex);
1749 if (IS_ERR(nd.intent.open.file)) {
1750 error = PTR_ERR(nd.intent.open.file);
1751 goto exit_mutex_unlock;
1754 /* Negative dentry, just create the file */
1755 if (!path.dentry->d_inode) {
1757 * This write is needed to ensure that a
1758 * ro->rw transition does not occur between
1759 * the time when the file is created and when
1760 * a permanent write count is taken through
1761 * the 'struct file' in nameidata_to_filp().
1763 error = mnt_want_write(nd.path.mnt);
1765 goto exit_mutex_unlock;
1766 error = __open_namei_create(&nd, &path, flag, mode);
1768 mnt_drop_write(nd.path.mnt);
1771 filp = nameidata_to_filp(&nd, open_flag);
1772 mnt_drop_write(nd.path.mnt);
1777 * It already exists.
1779 mutex_unlock(&dir->d_inode->i_mutex);
1780 audit_inode(pathname, path.dentry);
1786 if (__follow_mount(&path)) {
1788 if (flag & O_NOFOLLOW)
1793 if (!path.dentry->d_inode)
1795 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1798 path_to_nameidata(&path, &nd);
1800 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1805 * 1. may_open() truncates a file
1806 * 2. a rw->ro mount transition occurs
1807 * 3. nameidata_to_filp() fails due to
1809 * That would be inconsistent, and should
1810 * be avoided. Taking this mnt write here
1811 * ensures that (2) can not occur.
1813 will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
1815 error = mnt_want_write(nd.path.mnt);
1819 error = may_open(&nd, acc_mode, flag);
1822 mnt_drop_write(nd.path.mnt);
1825 filp = nameidata_to_filp(&nd, open_flag);
1827 * It is now safe to drop the mnt write
1828 * because the filp has had a write taken
1832 mnt_drop_write(nd.path.mnt);
1836 mutex_unlock(&dir->d_inode->i_mutex);
1838 path_put_conditional(&path, &nd);
1840 if (!IS_ERR(nd.intent.open.file))
1841 release_open_intent(&nd);
1843 return ERR_PTR(error);
1847 if (flag & O_NOFOLLOW)
1850 * This is subtle. Instead of calling do_follow_link() we do the
1851 * thing by hands. The reason is that this way we have zero link_count
1852 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1853 * After that we have the parent and last component, i.e.
1854 * we are in the same situation as after the first path_walk().
1855 * Well, almost - if the last component is normal we get its copy
1856 * stored in nd->last.name and we will have to putname() it when we
1857 * are done. Procfs-like symlinks just set LAST_BIND.
1859 nd.flags |= LOOKUP_PARENT;
1860 error = security_inode_follow_link(path.dentry, &nd);
1863 error = __do_follow_link(&path, &nd);
1865 /* Does someone understand code flow here? Or it is only
1866 * me so stupid? Anathema to whoever designed this non-sense
1867 * with "intent.open".
1869 release_open_intent(&nd);
1870 return ERR_PTR(error);
1872 nd.flags &= ~LOOKUP_PARENT;
1873 if (nd.last_type == LAST_BIND)
1876 if (nd.last_type != LAST_NORM)
1878 if (nd.last.name[nd.last.len]) {
1879 __putname(nd.last.name);
1884 __putname(nd.last.name);
1887 dir = nd.path.dentry;
1888 mutex_lock(&dir->d_inode->i_mutex);
1889 path.dentry = lookup_hash(&nd);
1890 path.mnt = nd.path.mnt;
1891 __putname(nd.last.name);
1896 * filp_open - open file and return file pointer
1898 * @filename: path to open
1899 * @flags: open flags as per the open(2) second argument
1900 * @mode: mode for the new file if O_CREAT is set, else ignored
1902 * This is the helper to open a file from kernelspace if you really
1903 * have to. But in generally you should not do this, so please move
1904 * along, nothing to see here..
1906 struct file *filp_open(const char *filename, int flags, int mode)
1908 return do_filp_open(AT_FDCWD, filename, flags, mode);
1910 EXPORT_SYMBOL(filp_open);
1913 * lookup_create - lookup a dentry, creating it if it doesn't exist
1914 * @nd: nameidata info
1915 * @is_dir: directory flag
1917 * Simple function to lookup and return a dentry and create it
1918 * if it doesn't exist. Is SMP-safe.
1920 * Returns with nd->path.dentry->d_inode->i_mutex locked.
1922 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1924 struct dentry *dentry = ERR_PTR(-EEXIST);
1926 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1928 * Yucky last component or no last component at all?
1929 * (foo/., foo/.., /////)
1931 if (nd->last_type != LAST_NORM)
1933 nd->flags &= ~LOOKUP_PARENT;
1934 nd->flags |= LOOKUP_CREATE;
1935 nd->intent.open.flags = O_EXCL;
1938 * Do the final lookup.
1940 dentry = lookup_hash(nd);
1944 if (dentry->d_inode)
1947 * Special case - lookup gave negative, but... we had foo/bar/
1948 * From the vfs_mknod() POV we just have a negative dentry -
1949 * all is fine. Let's be bastards - you had / on the end, you've
1950 * been asking for (non-existent) directory. -ENOENT for you.
1952 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
1954 dentry = ERR_PTR(-ENOENT);
1959 dentry = ERR_PTR(-EEXIST);
1963 EXPORT_SYMBOL_GPL(lookup_create);
1965 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1967 int error = may_create(dir, dentry, NULL);
1972 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1975 if (!dir->i_op || !dir->i_op->mknod)
1978 error = devcgroup_inode_mknod(mode, dev);
1982 error = security_inode_mknod(dir, dentry, mode, dev);
1987 error = dir->i_op->mknod(dir, dentry, mode, dev);
1989 fsnotify_create(dir, dentry);
1993 static int may_mknod(mode_t mode)
1995 switch (mode & S_IFMT) {
2001 case 0: /* zero mode translates to S_IFREG */
2010 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
2015 struct dentry * dentry;
2016 struct nameidata nd;
2020 tmp = getname(filename);
2022 return PTR_ERR(tmp);
2024 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2027 dentry = lookup_create(&nd, 0);
2028 if (IS_ERR(dentry)) {
2029 error = PTR_ERR(dentry);
2032 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2033 mode &= ~current->fs->umask;
2034 error = may_mknod(mode);
2037 error = mnt_want_write(nd.path.mnt);
2040 switch (mode & S_IFMT) {
2041 case 0: case S_IFREG:
2042 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2044 case S_IFCHR: case S_IFBLK:
2045 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2046 new_decode_dev(dev));
2048 case S_IFIFO: case S_IFSOCK:
2049 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2052 mnt_drop_write(nd.path.mnt);
2056 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2064 asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
2066 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2069 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2071 int error = may_create(dir, dentry, NULL);
2076 if (!dir->i_op || !dir->i_op->mkdir)
2079 mode &= (S_IRWXUGO|S_ISVTX);
2080 error = security_inode_mkdir(dir, dentry, mode);
2085 error = dir->i_op->mkdir(dir, dentry, mode);
2087 fsnotify_mkdir(dir, dentry);
2091 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
2095 struct dentry *dentry;
2096 struct nameidata nd;
2098 tmp = getname(pathname);
2099 error = PTR_ERR(tmp);
2103 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2106 dentry = lookup_create(&nd, 1);
2107 error = PTR_ERR(dentry);
2111 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2112 mode &= ~current->fs->umask;
2113 error = mnt_want_write(nd.path.mnt);
2116 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2117 mnt_drop_write(nd.path.mnt);
2121 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2129 asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2131 return sys_mkdirat(AT_FDCWD, pathname, mode);
2135 * We try to drop the dentry early: we should have
2136 * a usage count of 2 if we're the only user of this
2137 * dentry, and if that is true (possibly after pruning
2138 * the dcache), then we drop the dentry now.
2140 * A low-level filesystem can, if it choses, legally
2143 * if (!d_unhashed(dentry))
2146 * if it cannot handle the case of removing a directory
2147 * that is still in use by something else..
2149 void dentry_unhash(struct dentry *dentry)
2152 shrink_dcache_parent(dentry);
2153 spin_lock(&dcache_lock);
2154 spin_lock(&dentry->d_lock);
2155 if (atomic_read(&dentry->d_count) == 2)
2157 spin_unlock(&dentry->d_lock);
2158 spin_unlock(&dcache_lock);
2161 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2163 int error = may_delete(dir, dentry, 1);
2168 if (!dir->i_op || !dir->i_op->rmdir)
2173 mutex_lock(&dentry->d_inode->i_mutex);
2174 dentry_unhash(dentry);
2175 if (d_mountpoint(dentry))
2178 error = security_inode_rmdir(dir, dentry);
2180 error = dir->i_op->rmdir(dir, dentry);
2182 dentry->d_inode->i_flags |= S_DEAD;
2185 mutex_unlock(&dentry->d_inode->i_mutex);
2194 static long do_rmdir(int dfd, const char __user *pathname)
2198 struct dentry *dentry;
2199 struct nameidata nd;
2201 name = getname(pathname);
2203 return PTR_ERR(name);
2205 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2209 switch(nd.last_type) {
2220 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2221 dentry = lookup_hash(&nd);
2222 error = PTR_ERR(dentry);
2225 error = mnt_want_write(nd.path.mnt);
2228 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2229 mnt_drop_write(nd.path.mnt);
2233 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2241 asmlinkage long sys_rmdir(const char __user *pathname)
2243 return do_rmdir(AT_FDCWD, pathname);
2246 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2248 int error = may_delete(dir, dentry, 0);
2253 if (!dir->i_op || !dir->i_op->unlink)
2258 mutex_lock(&dentry->d_inode->i_mutex);
2259 if (d_mountpoint(dentry))
2262 error = security_inode_unlink(dir, dentry);
2264 error = dir->i_op->unlink(dir, dentry);
2266 mutex_unlock(&dentry->d_inode->i_mutex);
2268 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2269 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2270 fsnotify_link_count(dentry->d_inode);
2278 * Make sure that the actual truncation of the file will occur outside its
2279 * directory's i_mutex. Truncate can take a long time if there is a lot of
2280 * writeout happening, and we don't want to prevent access to the directory
2281 * while waiting on the I/O.
2283 static long do_unlinkat(int dfd, const char __user *pathname)
2287 struct dentry *dentry;
2288 struct nameidata nd;
2289 struct inode *inode = NULL;
2291 name = getname(pathname);
2293 return PTR_ERR(name);
2295 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2299 if (nd.last_type != LAST_NORM)
2301 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2302 dentry = lookup_hash(&nd);
2303 error = PTR_ERR(dentry);
2304 if (!IS_ERR(dentry)) {
2305 /* Why not before? Because we want correct error value */
2306 if (nd.last.name[nd.last.len])
2308 inode = dentry->d_inode;
2310 atomic_inc(&inode->i_count);
2311 error = mnt_want_write(nd.path.mnt);
2314 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2315 mnt_drop_write(nd.path.mnt);
2319 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2321 iput(inode); /* truncate the inode here */
2329 error = !dentry->d_inode ? -ENOENT :
2330 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2334 asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2336 if ((flag & ~AT_REMOVEDIR) != 0)
2339 if (flag & AT_REMOVEDIR)
2340 return do_rmdir(dfd, pathname);
2342 return do_unlinkat(dfd, pathname);
2345 asmlinkage long sys_unlink(const char __user *pathname)
2347 return do_unlinkat(AT_FDCWD, pathname);
2350 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2352 int error = may_create(dir, dentry, NULL);
2357 if (!dir->i_op || !dir->i_op->symlink)
2360 error = security_inode_symlink(dir, dentry, oldname);
2365 error = dir->i_op->symlink(dir, dentry, oldname);
2367 fsnotify_create(dir, dentry);
2371 asmlinkage long sys_symlinkat(const char __user *oldname,
2372 int newdfd, const char __user *newname)
2377 struct dentry *dentry;
2378 struct nameidata nd;
2380 from = getname(oldname);
2382 return PTR_ERR(from);
2383 to = getname(newname);
2384 error = PTR_ERR(to);
2388 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2391 dentry = lookup_create(&nd, 0);
2392 error = PTR_ERR(dentry);
2396 error = mnt_want_write(nd.path.mnt);
2399 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2400 mnt_drop_write(nd.path.mnt);
2404 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2413 asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2415 return sys_symlinkat(oldname, AT_FDCWD, newname);
2418 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2420 struct inode *inode = old_dentry->d_inode;
2426 error = may_create(dir, new_dentry, NULL);
2430 if (dir->i_sb != inode->i_sb)
2434 * A link to an append-only or immutable file cannot be created.
2436 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2438 if (!dir->i_op || !dir->i_op->link)
2440 if (S_ISDIR(inode->i_mode))
2443 error = security_inode_link(old_dentry, dir, new_dentry);
2447 mutex_lock(&inode->i_mutex);
2449 error = dir->i_op->link(old_dentry, dir, new_dentry);
2450 mutex_unlock(&inode->i_mutex);
2452 fsnotify_link(dir, inode, new_dentry);
2457 * Hardlinks are often used in delicate situations. We avoid
2458 * security-related surprises by not following symlinks on the
2461 * We don't follow them on the oldname either to be compatible
2462 * with linux 2.0, and to avoid hard-linking to directories
2463 * and other special files. --ADM
2465 asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
2466 int newdfd, const char __user *newname,
2469 struct dentry *new_dentry;
2470 struct nameidata nd, old_nd;
2474 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2477 to = getname(newname);
2481 error = __user_walk_fd(olddfd, oldname,
2482 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2486 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2490 if (old_nd.path.mnt != nd.path.mnt)
2492 new_dentry = lookup_create(&nd, 0);
2493 error = PTR_ERR(new_dentry);
2494 if (IS_ERR(new_dentry))
2496 error = mnt_want_write(nd.path.mnt);
2499 error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
2500 mnt_drop_write(nd.path.mnt);
2504 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2508 path_put(&old_nd.path);
2515 asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2517 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2521 * The worst of all namespace operations - renaming directory. "Perverted"
2522 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2524 * a) we can get into loop creation. Check is done in is_subdir().
2525 * b) race potential - two innocent renames can create a loop together.
2526 * That's where 4.4 screws up. Current fix: serialization on
2527 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2529 * c) we have to lock _three_ objects - parents and victim (if it exists).
2530 * And that - after we got ->i_mutex on parents (until then we don't know
2531 * whether the target exists). Solution: try to be smart with locking
2532 * order for inodes. We rely on the fact that tree topology may change
2533 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2534 * move will be locked. Thus we can rank directories by the tree
2535 * (ancestors first) and rank all non-directories after them.
2536 * That works since everybody except rename does "lock parent, lookup,
2537 * lock child" and rename is under ->s_vfs_rename_mutex.
2538 * HOWEVER, it relies on the assumption that any object with ->lookup()
2539 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2540 * we'd better make sure that there's no link(2) for them.
2541 * d) some filesystems don't support opened-but-unlinked directories,
2542 * either because of layout or because they are not ready to deal with
2543 * all cases correctly. The latter will be fixed (taking this sort of
2544 * stuff into VFS), but the former is not going away. Solution: the same
2545 * trick as in rmdir().
2546 * e) conversion from fhandle to dentry may come in the wrong moment - when
2547 * we are removing the target. Solution: we will have to grab ->i_mutex
2548 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2549 * ->i_mutex on parents, which works but leads to some truely excessive
2552 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2553 struct inode *new_dir, struct dentry *new_dentry)
2556 struct inode *target;
2559 * If we are going to change the parent - check write permissions,
2560 * we'll need to flip '..'.
2562 if (new_dir != old_dir) {
2563 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2568 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2572 target = new_dentry->d_inode;
2574 mutex_lock(&target->i_mutex);
2575 dentry_unhash(new_dentry);
2577 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2580 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2583 target->i_flags |= S_DEAD;
2584 mutex_unlock(&target->i_mutex);
2585 if (d_unhashed(new_dentry))
2586 d_rehash(new_dentry);
2590 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2591 d_move(old_dentry,new_dentry);
2595 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2596 struct inode *new_dir, struct dentry *new_dentry)
2598 struct inode *target;
2601 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2606 target = new_dentry->d_inode;
2608 mutex_lock(&target->i_mutex);
2609 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2612 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2614 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2615 d_move(old_dentry, new_dentry);
2618 mutex_unlock(&target->i_mutex);
2623 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2624 struct inode *new_dir, struct dentry *new_dentry)
2627 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2628 const char *old_name;
2630 if (old_dentry->d_inode == new_dentry->d_inode)
2633 error = may_delete(old_dir, old_dentry, is_dir);
2637 if (!new_dentry->d_inode)
2638 error = may_create(new_dir, new_dentry, NULL);
2640 error = may_delete(new_dir, new_dentry, is_dir);
2644 if (!old_dir->i_op || !old_dir->i_op->rename)
2647 DQUOT_INIT(old_dir);
2648 DQUOT_INIT(new_dir);
2650 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2653 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2655 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2657 const char *new_name = old_dentry->d_name.name;
2658 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2659 new_dentry->d_inode, old_dentry);
2661 fsnotify_oldname_free(old_name);
2666 static int do_rename(int olddfd, const char *oldname,
2667 int newdfd, const char *newname)
2670 struct dentry * old_dir, * new_dir;
2671 struct dentry * old_dentry, *new_dentry;
2672 struct dentry * trap;
2673 struct nameidata oldnd, newnd;
2675 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
2679 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
2684 if (oldnd.path.mnt != newnd.path.mnt)
2687 old_dir = oldnd.path.dentry;
2689 if (oldnd.last_type != LAST_NORM)
2692 new_dir = newnd.path.dentry;
2693 if (newnd.last_type != LAST_NORM)
2696 trap = lock_rename(new_dir, old_dir);
2698 old_dentry = lookup_hash(&oldnd);
2699 error = PTR_ERR(old_dentry);
2700 if (IS_ERR(old_dentry))
2702 /* source must exist */
2704 if (!old_dentry->d_inode)
2706 /* unless the source is a directory trailing slashes give -ENOTDIR */
2707 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2709 if (oldnd.last.name[oldnd.last.len])
2711 if (newnd.last.name[newnd.last.len])
2714 /* source should not be ancestor of target */
2716 if (old_dentry == trap)
2718 new_dentry = lookup_hash(&newnd);
2719 error = PTR_ERR(new_dentry);
2720 if (IS_ERR(new_dentry))
2722 /* target should not be an ancestor of source */
2724 if (new_dentry == trap)
2727 error = mnt_want_write(oldnd.path.mnt);
2730 error = vfs_rename(old_dir->d_inode, old_dentry,
2731 new_dir->d_inode, new_dentry);
2732 mnt_drop_write(oldnd.path.mnt);
2738 unlock_rename(new_dir, old_dir);
2740 path_put(&newnd.path);
2742 path_put(&oldnd.path);
2747 asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2748 int newdfd, const char __user *newname)
2754 from = getname(oldname);
2756 return PTR_ERR(from);
2757 to = getname(newname);
2758 error = PTR_ERR(to);
2760 error = do_rename(olddfd, from, newdfd, to);
2767 asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2769 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2772 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2776 len = PTR_ERR(link);
2781 if (len > (unsigned) buflen)
2783 if (copy_to_user(buffer, link, len))
2790 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2791 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2792 * using) it for any given inode is up to filesystem.
2794 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2796 struct nameidata nd;
2801 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2803 return PTR_ERR(cookie);
2805 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2806 if (dentry->d_inode->i_op->put_link)
2807 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2811 int vfs_follow_link(struct nameidata *nd, const char *link)
2813 return __vfs_follow_link(nd, link);
2816 /* get the link contents into pagecache */
2817 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2820 struct address_space *mapping = dentry->d_inode->i_mapping;
2821 page = read_mapping_page(mapping, 0, NULL);
2828 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2830 struct page *page = NULL;
2831 char *s = page_getlink(dentry, &page);
2832 int res = vfs_readlink(dentry,buffer,buflen,s);
2835 page_cache_release(page);
2840 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2842 struct page *page = NULL;
2843 nd_set_link(nd, page_getlink(dentry, &page));
2847 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2849 struct page *page = cookie;
2853 page_cache_release(page);
2857 int __page_symlink(struct inode *inode, const char *symname, int len,
2860 struct address_space *mapping = inode->i_mapping;
2867 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2868 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
2872 kaddr = kmap_atomic(page, KM_USER0);
2873 memcpy(kaddr, symname, len-1);
2874 kunmap_atomic(kaddr, KM_USER0);
2876 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2883 mark_inode_dirty(inode);
2889 int page_symlink(struct inode *inode, const char *symname, int len)
2891 return __page_symlink(inode, symname, len,
2892 mapping_gfp_mask(inode->i_mapping));
2895 const struct inode_operations page_symlink_inode_operations = {
2896 .readlink = generic_readlink,
2897 .follow_link = page_follow_link_light,
2898 .put_link = page_put_link,
2901 EXPORT_SYMBOL(__user_walk);
2902 EXPORT_SYMBOL(__user_walk_fd);
2903 EXPORT_SYMBOL(follow_down);
2904 EXPORT_SYMBOL(follow_up);
2905 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2906 EXPORT_SYMBOL(getname);
2907 EXPORT_SYMBOL(lock_rename);
2908 EXPORT_SYMBOL(lookup_one_len);
2909 EXPORT_SYMBOL(page_follow_link_light);
2910 EXPORT_SYMBOL(page_put_link);
2911 EXPORT_SYMBOL(page_readlink);
2912 EXPORT_SYMBOL(__page_symlink);
2913 EXPORT_SYMBOL(page_symlink);
2914 EXPORT_SYMBOL(page_symlink_inode_operations);
2915 EXPORT_SYMBOL(path_lookup);
2916 EXPORT_SYMBOL(vfs_path_lookup);
2917 EXPORT_SYMBOL(permission);
2918 EXPORT_SYMBOL(vfs_permission);
2919 EXPORT_SYMBOL(file_permission);
2920 EXPORT_SYMBOL(unlock_rename);
2921 EXPORT_SYMBOL(vfs_create);
2922 EXPORT_SYMBOL(vfs_follow_link);
2923 EXPORT_SYMBOL(vfs_link);
2924 EXPORT_SYMBOL(vfs_mkdir);
2925 EXPORT_SYMBOL(vfs_mknod);
2926 EXPORT_SYMBOL(generic_permission);
2927 EXPORT_SYMBOL(vfs_readlink);
2928 EXPORT_SYMBOL(vfs_rename);
2929 EXPORT_SYMBOL(vfs_rmdir);
2930 EXPORT_SYMBOL(vfs_symlink);
2931 EXPORT_SYMBOL(vfs_unlink);
2932 EXPORT_SYMBOL(dentry_unhash);
2933 EXPORT_SYMBOL(generic_readlink);