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/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <asm/uaccess.h>
37 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
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).
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.
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.
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
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.
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.
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.
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.
87 * [10-Sep-98 Alan Modra] Another symlink change.
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).
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...
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...
110 static int __link_path_walk(const char *name, struct nameidata *nd);
112 /* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
119 static int do_getname(const char __user *filename, char *page)
122 unsigned long len = PATH_MAX;
124 if (!segment_eq(get_fs(), KERNEL_DS)) {
125 if ((unsigned long) filename >= TASK_SIZE)
127 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
128 len = TASK_SIZE - (unsigned long) filename;
131 retval = strncpy_from_user(page, filename, len);
135 return -ENAMETOOLONG;
141 char * getname(const char __user * filename)
145 result = ERR_PTR(-ENOMEM);
148 int retval = do_getname(filename, tmp);
153 result = ERR_PTR(retval);
156 audit_getname(result);
160 #ifdef CONFIG_AUDITSYSCALL
161 void putname(const char *name)
163 if (unlikely(!audit_dummy_context()))
168 EXPORT_SYMBOL(putname);
173 * generic_permission - check for access rights on a Posix-like filesystem
174 * @inode: inode to check access rights for
175 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
176 * @check_acl: optional callback to check for Posix ACLs
178 * Used to check for read/write/execute permissions on a file.
179 * We use "fsuid" for this, letting us set arbitrary permissions
180 * for filesystem access without changing the "normal" uids which
181 * are used for other things..
183 int generic_permission(struct inode *inode, int mask,
184 int (*check_acl)(struct inode *inode, int mask))
186 umode_t mode = inode->i_mode;
188 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
190 if (current_fsuid() == inode->i_uid)
193 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
194 int error = check_acl(inode, mask);
195 if (error == -EACCES)
196 goto check_capabilities;
197 else if (error != -EAGAIN)
201 if (in_group_p(inode->i_gid))
206 * If the DACs are ok we don't need any capability check.
208 if ((mask & ~mode) == 0)
213 * Read/write DACs are always overridable.
214 * Executable DACs are overridable if at least one exec bit is set.
216 if (!(mask & MAY_EXEC) || execute_ok(inode))
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 inode_permission(struct inode *inode, int mask)
234 if (mask & MAY_WRITE) {
235 umode_t mode = inode->i_mode;
238 * Nobody gets write access to a read-only fs.
240 if (IS_RDONLY(inode) &&
241 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
245 * Nobody gets write access to an immutable file.
247 if (IS_IMMUTABLE(inode))
251 /* Ordinary permission routines do not understand MAY_APPEND. */
252 if (inode->i_op && inode->i_op->permission)
253 retval = inode->i_op->permission(inode, mask);
255 retval = generic_permission(inode, mask, NULL);
260 retval = devcgroup_inode_permission(inode, mask);
264 return security_inode_permission(inode,
265 mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
269 * vfs_permission - check for access rights to a given path
270 * @nd: lookup result that describes the path
271 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
273 * Used to check for read/write/execute permissions on a path.
274 * We use "fsuid" for this, letting us set arbitrary permissions
275 * for filesystem access without changing the "normal" uids which
276 * are used for other things.
278 int vfs_permission(struct nameidata *nd, int mask)
280 return inode_permission(nd->path.dentry->d_inode, mask);
284 * file_permission - check for additional access rights to a given file
285 * @file: file to check access rights for
286 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
288 * Used to check for read/write/execute permissions on an already opened
292 * Do not use this function in new code. All access checks should
293 * be done using vfs_permission().
295 int file_permission(struct file *file, int mask)
297 return inode_permission(file->f_path.dentry->d_inode, mask);
301 * get_write_access() gets write permission for a file.
302 * put_write_access() releases this write permission.
303 * This is used for regular files.
304 * We cannot support write (and maybe mmap read-write shared) accesses and
305 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
306 * can have the following values:
307 * 0: no writers, no VM_DENYWRITE mappings
308 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
309 * > 0: (i_writecount) users are writing to the file.
311 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
312 * except for the cases where we don't hold i_writecount yet. Then we need to
313 * use {get,deny}_write_access() - these functions check the sign and refuse
314 * to do the change if sign is wrong. Exclusion between them is provided by
315 * the inode->i_lock spinlock.
318 int get_write_access(struct inode * inode)
320 spin_lock(&inode->i_lock);
321 if (atomic_read(&inode->i_writecount) < 0) {
322 spin_unlock(&inode->i_lock);
325 atomic_inc(&inode->i_writecount);
326 spin_unlock(&inode->i_lock);
331 int deny_write_access(struct file * file)
333 struct inode *inode = file->f_path.dentry->d_inode;
335 spin_lock(&inode->i_lock);
336 if (atomic_read(&inode->i_writecount) > 0) {
337 spin_unlock(&inode->i_lock);
340 atomic_dec(&inode->i_writecount);
341 spin_unlock(&inode->i_lock);
347 * path_get - get a reference to a path
348 * @path: path to get the reference to
350 * Given a path increment the reference count to the dentry and the vfsmount.
352 void path_get(struct path *path)
357 EXPORT_SYMBOL(path_get);
360 * path_put - put a reference to a path
361 * @path: path to put the reference to
363 * Given a path decrement the reference count to the dentry and the vfsmount.
365 void path_put(struct path *path)
370 EXPORT_SYMBOL(path_put);
373 * release_open_intent - free up open intent resources
374 * @nd: pointer to nameidata
376 void release_open_intent(struct nameidata *nd)
378 if (nd->intent.open.file->f_path.dentry == NULL)
379 put_filp(nd->intent.open.file);
381 fput(nd->intent.open.file);
384 static inline struct dentry *
385 do_revalidate(struct dentry *dentry, struct nameidata *nd)
387 int status = dentry->d_op->d_revalidate(dentry, nd);
388 if (unlikely(status <= 0)) {
390 * The dentry failed validation.
391 * If d_revalidate returned 0 attempt to invalidate
392 * the dentry otherwise d_revalidate is asking us
393 * to return a fail status.
396 if (!d_invalidate(dentry)) {
402 dentry = ERR_PTR(status);
409 * Internal lookup() using the new generic dcache.
412 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
414 struct dentry * dentry = __d_lookup(parent, name);
416 /* lockess __d_lookup may fail due to concurrent d_move()
417 * in some unrelated directory, so try with d_lookup
420 dentry = d_lookup(parent, name);
422 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
423 dentry = do_revalidate(dentry, nd);
429 * Short-cut version of permission(), for calling by
430 * path_walk(), when dcache lock is held. Combines parts
431 * of permission() and generic_permission(), and tests ONLY for
432 * MAY_EXEC permission.
434 * If appropriate, check DAC only. If not appropriate, or
435 * short-cut DAC fails, then call permission() to do more
436 * complete permission check.
438 static int exec_permission_lite(struct inode *inode)
440 umode_t mode = inode->i_mode;
442 if (inode->i_op && inode->i_op->permission)
445 if (current_fsuid() == inode->i_uid)
447 else if (in_group_p(inode->i_gid))
453 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
456 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
459 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
464 return security_inode_permission(inode, MAY_EXEC);
468 * This is called when everything else fails, and we actually have
469 * to go to the low-level filesystem to find out what we should do..
471 * We get the directory semaphore, and after getting that we also
472 * make sure that nobody added the entry to the dcache in the meantime..
475 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
477 struct dentry * result;
478 struct inode *dir = parent->d_inode;
480 mutex_lock(&dir->i_mutex);
482 * First re-do the cached lookup just in case it was created
483 * while we waited for the directory semaphore..
485 * FIXME! This could use version numbering or similar to
486 * avoid unnecessary cache lookups.
488 * The "dcache_lock" is purely to protect the RCU list walker
489 * from concurrent renames at this point (we mustn't get false
490 * negatives from the RCU list walk here, unlike the optimistic
493 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
495 result = d_lookup(parent, name);
497 struct dentry *dentry;
499 /* Don't create child dentry for a dead directory. */
500 result = ERR_PTR(-ENOENT);
504 dentry = d_alloc(parent, name);
505 result = ERR_PTR(-ENOMEM);
507 result = dir->i_op->lookup(dir, dentry, nd);
514 mutex_unlock(&dir->i_mutex);
519 * Uhhuh! Nasty case: the cache was re-populated while
520 * we waited on the semaphore. Need to revalidate.
522 mutex_unlock(&dir->i_mutex);
523 if (result->d_op && result->d_op->d_revalidate) {
524 result = do_revalidate(result, nd);
526 result = ERR_PTR(-ENOENT);
532 static __always_inline void
533 walk_init_root(const char *name, struct nameidata *nd)
535 struct fs_struct *fs = current->fs;
537 read_lock(&fs->lock);
540 read_unlock(&fs->lock);
544 * Wrapper to retry pathname resolution whenever the underlying
545 * file system returns an ESTALE.
547 * Retry the whole path once, forcing real lookup requests
548 * instead of relying on the dcache.
550 static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
552 struct path save = nd->path;
555 /* make sure the stuff we saved doesn't go away */
558 result = __link_path_walk(name, nd);
559 if (result == -ESTALE) {
560 /* nd->path had been dropped */
563 nd->flags |= LOOKUP_REVAL;
564 result = __link_path_walk(name, nd);
572 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
581 walk_init_root(link, nd);
583 res = link_path_walk(link, nd);
584 if (nd->depth || res || nd->last_type!=LAST_NORM)
587 * If it is an iterative symlinks resolution in open_namei() we
588 * have to copy the last component. And all that crap because of
589 * bloody create() on broken symlinks. Furrfu...
592 if (unlikely(!name)) {
596 strcpy(name, nd->last.name);
597 nd->last.name = name;
601 return PTR_ERR(link);
604 static void path_put_conditional(struct path *path, struct nameidata *nd)
607 if (path->mnt != nd->path.mnt)
611 static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
613 dput(nd->path.dentry);
614 if (nd->path.mnt != path->mnt)
615 mntput(nd->path.mnt);
616 nd->path.mnt = path->mnt;
617 nd->path.dentry = path->dentry;
620 static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
624 struct dentry *dentry = path->dentry;
626 touch_atime(path->mnt, dentry);
627 nd_set_link(nd, NULL);
629 if (path->mnt != nd->path.mnt) {
630 path_to_nameidata(path, nd);
634 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
635 error = PTR_ERR(cookie);
636 if (!IS_ERR(cookie)) {
637 char *s = nd_get_link(nd);
640 error = __vfs_follow_link(nd, s);
641 if (dentry->d_inode->i_op->put_link)
642 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
650 * This limits recursive symlink follows to 8, while
651 * limiting consecutive symlinks to 40.
653 * Without that kind of total limit, nasty chains of consecutive
654 * symlinks can cause almost arbitrarily long lookups.
656 static inline int do_follow_link(struct path *path, struct nameidata *nd)
659 if (current->link_count >= MAX_NESTED_LINKS)
661 if (current->total_link_count >= 40)
663 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
665 err = security_inode_follow_link(path->dentry, nd);
668 current->link_count++;
669 current->total_link_count++;
671 err = __do_follow_link(path, nd);
672 current->link_count--;
676 path_put_conditional(path, nd);
681 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
683 struct vfsmount *parent;
684 struct dentry *mountpoint;
685 spin_lock(&vfsmount_lock);
686 parent=(*mnt)->mnt_parent;
687 if (parent == *mnt) {
688 spin_unlock(&vfsmount_lock);
692 mountpoint=dget((*mnt)->mnt_mountpoint);
693 spin_unlock(&vfsmount_lock);
695 *dentry = mountpoint;
701 /* no need for dcache_lock, as serialization is taken care in
704 static int __follow_mount(struct path *path)
707 while (d_mountpoint(path->dentry)) {
708 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
715 path->dentry = dget(mounted->mnt_root);
721 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
723 while (d_mountpoint(*dentry)) {
724 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
730 *dentry = dget(mounted->mnt_root);
734 /* no need for dcache_lock, as serialization is taken care in
737 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
739 struct vfsmount *mounted;
741 mounted = lookup_mnt(*mnt, *dentry);
746 *dentry = dget(mounted->mnt_root);
752 static __always_inline void follow_dotdot(struct nameidata *nd)
754 struct fs_struct *fs = current->fs;
757 struct vfsmount *parent;
758 struct dentry *old = nd->path.dentry;
760 read_lock(&fs->lock);
761 if (nd->path.dentry == fs->root.dentry &&
762 nd->path.mnt == fs->root.mnt) {
763 read_unlock(&fs->lock);
766 read_unlock(&fs->lock);
767 spin_lock(&dcache_lock);
768 if (nd->path.dentry != nd->path.mnt->mnt_root) {
769 nd->path.dentry = dget(nd->path.dentry->d_parent);
770 spin_unlock(&dcache_lock);
774 spin_unlock(&dcache_lock);
775 spin_lock(&vfsmount_lock);
776 parent = nd->path.mnt->mnt_parent;
777 if (parent == nd->path.mnt) {
778 spin_unlock(&vfsmount_lock);
782 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
783 spin_unlock(&vfsmount_lock);
785 mntput(nd->path.mnt);
786 nd->path.mnt = parent;
788 follow_mount(&nd->path.mnt, &nd->path.dentry);
792 * It's more convoluted than I'd like it to be, but... it's still fairly
793 * small and for now I'd prefer to have fast path as straight as possible.
794 * It _is_ time-critical.
796 static int do_lookup(struct nameidata *nd, struct qstr *name,
799 struct vfsmount *mnt = nd->path.mnt;
800 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
804 if (dentry->d_op && dentry->d_op->d_revalidate)
805 goto need_revalidate;
808 path->dentry = dentry;
809 __follow_mount(path);
813 dentry = real_lookup(nd->path.dentry, name, nd);
819 dentry = do_revalidate(dentry, nd);
827 return PTR_ERR(dentry);
832 * This is the basic name resolution function, turning a pathname into
833 * the final dentry. We expect 'base' to be positive and a directory.
835 * Returns 0 and nd will have valid dentry and mnt on success.
836 * Returns error and drops reference to input namei data on failure.
838 static int __link_path_walk(const char *name, struct nameidata *nd)
843 unsigned int lookup_flags = nd->flags;
850 inode = nd->path.dentry->d_inode;
852 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
854 /* At this point we know we have a real path component. */
860 nd->flags |= LOOKUP_CONTINUE;
861 err = exec_permission_lite(inode);
863 err = vfs_permission(nd, MAY_EXEC);
865 err = ima_path_check(&nd->path, MAY_EXEC);
870 c = *(const unsigned char *)name;
872 hash = init_name_hash();
875 hash = partial_name_hash(c, hash);
876 c = *(const unsigned char *)name;
877 } while (c && (c != '/'));
878 this.len = name - (const char *) this.name;
879 this.hash = end_name_hash(hash);
881 /* remove trailing slashes? */
884 while (*++name == '/');
886 goto last_with_slashes;
889 * "." and ".." are special - ".." especially so because it has
890 * to be able to know about the current root directory and
891 * parent relationships.
893 if (this.name[0] == '.') switch (this.len) {
897 if (this.name[1] != '.')
900 inode = nd->path.dentry->d_inode;
906 * See if the low-level filesystem might want
907 * to use its own hash..
909 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
910 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
915 /* This does the actual lookups.. */
916 err = do_lookup(nd, &this, &next);
921 inode = next.dentry->d_inode;
928 if (inode->i_op->follow_link) {
929 err = do_follow_link(&next, nd);
933 inode = nd->path.dentry->d_inode;
940 path_to_nameidata(&next, nd);
942 if (!inode->i_op->lookup)
945 /* here ends the main loop */
948 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
950 /* Clear LOOKUP_CONTINUE iff it was previously unset */
951 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
952 if (lookup_flags & LOOKUP_PARENT)
954 if (this.name[0] == '.') switch (this.len) {
958 if (this.name[1] != '.')
961 inode = nd->path.dentry->d_inode;
966 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
967 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
972 err = do_lookup(nd, &this, &next);
975 inode = next.dentry->d_inode;
976 if ((lookup_flags & LOOKUP_FOLLOW)
977 && inode && inode->i_op && inode->i_op->follow_link) {
978 err = do_follow_link(&next, nd);
981 inode = nd->path.dentry->d_inode;
983 path_to_nameidata(&next, nd);
987 if (lookup_flags & LOOKUP_DIRECTORY) {
989 if (!inode->i_op || !inode->i_op->lookup)
995 nd->last_type = LAST_NORM;
996 if (this.name[0] != '.')
999 nd->last_type = LAST_DOT;
1000 else if (this.len == 2 && this.name[1] == '.')
1001 nd->last_type = LAST_DOTDOT;
1006 * We bypassed the ordinary revalidation routines.
1007 * We may need to check the cached dentry for staleness.
1009 if (nd->path.dentry && nd->path.dentry->d_sb &&
1010 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
1012 /* Note: we do not d_invalidate() */
1013 if (!nd->path.dentry->d_op->d_revalidate(
1014 nd->path.dentry, nd))
1020 path_put_conditional(&next, nd);
1023 path_put(&nd->path);
1028 static int path_walk(const char *name, struct nameidata *nd)
1030 current->total_link_count = 0;
1031 return link_path_walk(name, nd);
1034 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1035 static int do_path_lookup(int dfd, const char *name,
1036 unsigned int flags, struct nameidata *nd)
1041 struct fs_struct *fs = current->fs;
1043 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1048 read_lock(&fs->lock);
1049 nd->path = fs->root;
1050 path_get(&fs->root);
1051 read_unlock(&fs->lock);
1052 } else if (dfd == AT_FDCWD) {
1053 read_lock(&fs->lock);
1056 read_unlock(&fs->lock);
1058 struct dentry *dentry;
1060 file = fget_light(dfd, &fput_needed);
1065 dentry = file->f_path.dentry;
1068 if (!S_ISDIR(dentry->d_inode->i_mode))
1071 retval = file_permission(file, MAY_EXEC);
1075 nd->path = file->f_path;
1076 path_get(&file->f_path);
1078 fput_light(file, fput_needed);
1081 retval = path_walk(name, nd);
1082 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1083 nd->path.dentry->d_inode))
1084 audit_inode(name, nd->path.dentry);
1089 fput_light(file, fput_needed);
1093 int path_lookup(const char *name, unsigned int flags,
1094 struct nameidata *nd)
1096 return do_path_lookup(AT_FDCWD, name, flags, nd);
1099 int kern_path(const char *name, unsigned int flags, struct path *path)
1101 struct nameidata nd;
1102 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1109 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1110 * @dentry: pointer to dentry of the base directory
1111 * @mnt: pointer to vfs mount of the base directory
1112 * @name: pointer to file name
1113 * @flags: lookup flags
1114 * @nd: pointer to nameidata
1116 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1117 const char *name, unsigned int flags,
1118 struct nameidata *nd)
1122 /* same as do_path_lookup */
1123 nd->last_type = LAST_ROOT;
1127 nd->path.dentry = dentry;
1129 path_get(&nd->path);
1131 retval = path_walk(name, nd);
1132 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1133 nd->path.dentry->d_inode))
1134 audit_inode(name, nd->path.dentry);
1141 * path_lookup_open - lookup a file path with open intent
1142 * @dfd: the directory to use as base, or AT_FDCWD
1143 * @name: pointer to file name
1144 * @lookup_flags: lookup intent flags
1145 * @nd: pointer to nameidata
1146 * @open_flags: open intent flags
1148 int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1149 struct nameidata *nd, int open_flags)
1151 struct file *filp = get_empty_filp();
1156 nd->intent.open.file = filp;
1157 nd->intent.open.flags = open_flags;
1158 nd->intent.open.create_mode = 0;
1159 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1160 if (IS_ERR(nd->intent.open.file)) {
1162 err = PTR_ERR(nd->intent.open.file);
1163 path_put(&nd->path);
1165 } else if (err != 0)
1166 release_open_intent(nd);
1170 static struct dentry *__lookup_hash(struct qstr *name,
1171 struct dentry *base, struct nameidata *nd)
1173 struct dentry *dentry;
1174 struct inode *inode;
1177 inode = base->d_inode;
1180 * See if the low-level filesystem might want
1181 * to use its own hash..
1183 if (base->d_op && base->d_op->d_hash) {
1184 err = base->d_op->d_hash(base, name);
1185 dentry = ERR_PTR(err);
1190 dentry = cached_lookup(base, name, nd);
1194 /* Don't create child dentry for a dead directory. */
1195 dentry = ERR_PTR(-ENOENT);
1196 if (IS_DEADDIR(inode))
1199 new = d_alloc(base, name);
1200 dentry = ERR_PTR(-ENOMEM);
1203 dentry = inode->i_op->lookup(inode, new, nd);
1214 * Restricted form of lookup. Doesn't follow links, single-component only,
1215 * needs parent already locked. Doesn't follow mounts.
1218 static struct dentry *lookup_hash(struct nameidata *nd)
1222 err = inode_permission(nd->path.dentry->d_inode, MAY_EXEC);
1224 return ERR_PTR(err);
1225 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1228 static int __lookup_one_len(const char *name, struct qstr *this,
1229 struct dentry *base, int len)
1239 hash = init_name_hash();
1241 c = *(const unsigned char *)name++;
1242 if (c == '/' || c == '\0')
1244 hash = partial_name_hash(c, hash);
1246 this->hash = end_name_hash(hash);
1251 * lookup_one_len - filesystem helper to lookup single pathname component
1252 * @name: pathname component to lookup
1253 * @base: base directory to lookup from
1254 * @len: maximum length @len should be interpreted to
1256 * Note that this routine is purely a helper for filesystem usage and should
1257 * not be called by generic code. Also note that by using this function the
1258 * nameidata argument is passed to the filesystem methods and a filesystem
1259 * using this helper needs to be prepared for that.
1261 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1266 err = __lookup_one_len(name, &this, base, len);
1268 return ERR_PTR(err);
1270 err = inode_permission(base->d_inode, MAY_EXEC);
1272 return ERR_PTR(err);
1273 return __lookup_hash(&this, base, NULL);
1277 * lookup_one_noperm - bad hack for sysfs
1278 * @name: pathname component to lookup
1279 * @base: base directory to lookup from
1281 * This is a variant of lookup_one_len that doesn't perform any permission
1282 * checks. It's a horrible hack to work around the braindead sysfs
1283 * architecture and should not be used anywhere else.
1285 * DON'T USE THIS FUNCTION EVER, thanks.
1287 struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
1292 err = __lookup_one_len(name, &this, base, strlen(name));
1294 return ERR_PTR(err);
1295 return __lookup_hash(&this, base, NULL);
1298 int user_path_at(int dfd, const char __user *name, unsigned flags,
1301 struct nameidata nd;
1302 char *tmp = getname(name);
1303 int err = PTR_ERR(tmp);
1306 BUG_ON(flags & LOOKUP_PARENT);
1308 err = do_path_lookup(dfd, tmp, flags, &nd);
1316 static int user_path_parent(int dfd, const char __user *path,
1317 struct nameidata *nd, char **name)
1319 char *s = getname(path);
1325 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1335 * It's inline, so penalty for filesystems that don't use sticky bit is
1338 static inline int check_sticky(struct inode *dir, struct inode *inode)
1340 uid_t fsuid = current_fsuid();
1342 if (!(dir->i_mode & S_ISVTX))
1344 if (inode->i_uid == fsuid)
1346 if (dir->i_uid == fsuid)
1348 return !capable(CAP_FOWNER);
1352 * Check whether we can remove a link victim from directory dir, check
1353 * whether the type of victim is right.
1354 * 1. We can't do it if dir is read-only (done in permission())
1355 * 2. We should have write and exec permissions on dir
1356 * 3. We can't remove anything from append-only dir
1357 * 4. We can't do anything with immutable dir (done in permission())
1358 * 5. If the sticky bit on dir is set we should either
1359 * a. be owner of dir, or
1360 * b. be owner of victim, or
1361 * c. have CAP_FOWNER capability
1362 * 6. If the victim is append-only or immutable we can't do antyhing with
1363 * links pointing to it.
1364 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1365 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1366 * 9. We can't remove a root or mountpoint.
1367 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1368 * nfs_async_unlink().
1370 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1374 if (!victim->d_inode)
1377 BUG_ON(victim->d_parent->d_inode != dir);
1378 audit_inode_child(victim->d_name.name, victim, dir);
1380 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1385 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1386 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1389 if (!S_ISDIR(victim->d_inode->i_mode))
1391 if (IS_ROOT(victim))
1393 } else if (S_ISDIR(victim->d_inode->i_mode))
1395 if (IS_DEADDIR(dir))
1397 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1402 /* Check whether we can create an object with dentry child in directory
1404 * 1. We can't do it if child already exists (open has special treatment for
1405 * this case, but since we are inlined it's OK)
1406 * 2. We can't do it if dir is read-only (done in permission())
1407 * 3. We should have write and exec permissions on dir
1408 * 4. We can't do it if dir is immutable (done in permission())
1410 static inline int may_create(struct inode *dir, struct dentry *child)
1414 if (IS_DEADDIR(dir))
1416 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1420 * O_DIRECTORY translates into forcing a directory lookup.
1422 static inline int lookup_flags(unsigned int f)
1424 unsigned long retval = LOOKUP_FOLLOW;
1427 retval &= ~LOOKUP_FOLLOW;
1429 if (f & O_DIRECTORY)
1430 retval |= LOOKUP_DIRECTORY;
1436 * p1 and p2 should be directories on the same fs.
1438 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1443 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1447 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1449 p = d_ancestor(p2, p1);
1451 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1452 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1456 p = d_ancestor(p1, p2);
1458 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1459 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1463 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1464 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1468 void unlock_rename(struct dentry *p1, struct dentry *p2)
1470 mutex_unlock(&p1->d_inode->i_mutex);
1472 mutex_unlock(&p2->d_inode->i_mutex);
1473 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1477 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1478 struct nameidata *nd)
1480 int error = may_create(dir, dentry);
1485 if (!dir->i_op || !dir->i_op->create)
1486 return -EACCES; /* shouldn't it be ENOSYS? */
1489 error = security_inode_create(dir, dentry, mode);
1493 error = dir->i_op->create(dir, dentry, mode, nd);
1495 fsnotify_create(dir, dentry);
1499 int may_open(struct nameidata *nd, int acc_mode, int flag)
1501 struct dentry *dentry = nd->path.dentry;
1502 struct inode *inode = dentry->d_inode;
1508 if (S_ISLNK(inode->i_mode))
1511 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
1515 * FIFO's, sockets and device files are special: they don't
1516 * actually live on the filesystem itself, and as such you
1517 * can write to them even if the filesystem is read-only.
1519 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1521 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1522 if (nd->path.mnt->mnt_flags & MNT_NODEV)
1528 error = vfs_permission(nd, acc_mode);
1532 error = ima_path_check(&nd->path,
1533 acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC));
1537 * An append-only file must be opened in append mode for writing.
1539 if (IS_APPEND(inode)) {
1540 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1546 /* O_NOATIME can only be set by the owner or superuser */
1547 if (flag & O_NOATIME)
1548 if (!is_owner_or_cap(inode))
1552 * Ensure there are no outstanding leases on the file.
1554 error = break_lease(inode, flag);
1558 if (flag & O_TRUNC) {
1559 error = get_write_access(inode);
1564 * Refuse to truncate files with mandatory locks held on them.
1566 error = locks_verify_locked(inode);
1570 error = do_truncate(dentry, 0,
1571 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1574 put_write_access(inode);
1578 if (flag & FMODE_WRITE)
1585 * Be careful about ever adding any more callers of this
1586 * function. Its flags must be in the namei format, not
1587 * what get passed to sys_open().
1589 static int __open_namei_create(struct nameidata *nd, struct path *path,
1593 struct dentry *dir = nd->path.dentry;
1595 if (!IS_POSIXACL(dir->d_inode))
1596 mode &= ~current->fs->umask;
1597 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1598 mutex_unlock(&dir->d_inode->i_mutex);
1599 dput(nd->path.dentry);
1600 nd->path.dentry = path->dentry;
1603 /* Don't check for write permission, don't truncate */
1604 return may_open(nd, 0, flag & ~O_TRUNC);
1608 * Note that while the flag value (low two bits) for sys_open means:
1613 * it is changed into
1614 * 00 - no permissions needed
1615 * 01 - read-permission
1616 * 10 - write-permission
1618 * for the internal routines (ie open_namei()/follow_link() etc)
1619 * This is more logical, and also allows the 00 "no perm needed"
1620 * to be used for symlinks (where the permissions are checked
1624 static inline int open_to_namei_flags(int flag)
1626 if ((flag+1) & O_ACCMODE)
1631 static int open_will_write_to_fs(int flag, struct inode *inode)
1634 * We'll never write to the fs underlying
1637 if (special_file(inode->i_mode))
1639 return (flag & O_TRUNC);
1643 * Note that the low bits of the passed in "open_flag"
1644 * are not the same as in the local variable "flag". See
1645 * open_to_namei_flags() for more details.
1647 struct file *do_filp_open(int dfd, const char *pathname,
1648 int open_flag, int mode)
1651 struct nameidata nd;
1652 int acc_mode, error;
1657 int flag = open_to_namei_flags(open_flag);
1659 acc_mode = MAY_OPEN | ACC_MODE(flag);
1661 /* O_TRUNC implies we need access checks for write permissions */
1663 acc_mode |= MAY_WRITE;
1665 /* Allow the LSM permission hook to distinguish append
1666 access from general write access. */
1667 if (flag & O_APPEND)
1668 acc_mode |= MAY_APPEND;
1671 * The simplest case - just a plain lookup.
1673 if (!(flag & O_CREAT)) {
1674 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1677 return ERR_PTR(error);
1682 * Create - we need to know the parent.
1684 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
1686 return ERR_PTR(error);
1689 * We have the parent and last component. First of all, check
1690 * that we are not asked to creat(2) an obvious directory - that
1694 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
1698 filp = get_empty_filp();
1701 nd.intent.open.file = filp;
1702 nd.intent.open.flags = flag;
1703 nd.intent.open.create_mode = mode;
1704 dir = nd.path.dentry;
1705 nd.flags &= ~LOOKUP_PARENT;
1706 nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
1708 nd.flags |= LOOKUP_EXCL;
1709 mutex_lock(&dir->d_inode->i_mutex);
1710 path.dentry = lookup_hash(&nd);
1711 path.mnt = nd.path.mnt;
1714 error = PTR_ERR(path.dentry);
1715 if (IS_ERR(path.dentry)) {
1716 mutex_unlock(&dir->d_inode->i_mutex);
1720 if (IS_ERR(nd.intent.open.file)) {
1721 error = PTR_ERR(nd.intent.open.file);
1722 goto exit_mutex_unlock;
1725 /* Negative dentry, just create the file */
1726 if (!path.dentry->d_inode) {
1728 * This write is needed to ensure that a
1729 * ro->rw transition does not occur between
1730 * the time when the file is created and when
1731 * a permanent write count is taken through
1732 * the 'struct file' in nameidata_to_filp().
1734 error = mnt_want_write(nd.path.mnt);
1736 goto exit_mutex_unlock;
1737 error = __open_namei_create(&nd, &path, flag, mode);
1739 mnt_drop_write(nd.path.mnt);
1742 filp = nameidata_to_filp(&nd, open_flag);
1743 mnt_drop_write(nd.path.mnt);
1748 * It already exists.
1750 mutex_unlock(&dir->d_inode->i_mutex);
1751 audit_inode(pathname, path.dentry);
1757 if (__follow_mount(&path)) {
1759 if (flag & O_NOFOLLOW)
1764 if (!path.dentry->d_inode)
1766 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1769 path_to_nameidata(&path, &nd);
1771 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1776 * 1. may_open() truncates a file
1777 * 2. a rw->ro mount transition occurs
1778 * 3. nameidata_to_filp() fails due to
1780 * That would be inconsistent, and should
1781 * be avoided. Taking this mnt write here
1782 * ensures that (2) can not occur.
1784 will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
1786 error = mnt_want_write(nd.path.mnt);
1790 error = may_open(&nd, acc_mode, flag);
1793 mnt_drop_write(nd.path.mnt);
1796 filp = nameidata_to_filp(&nd, open_flag);
1798 * It is now safe to drop the mnt write
1799 * because the filp has had a write taken
1803 mnt_drop_write(nd.path.mnt);
1807 mutex_unlock(&dir->d_inode->i_mutex);
1809 path_put_conditional(&path, &nd);
1811 if (!IS_ERR(nd.intent.open.file))
1812 release_open_intent(&nd);
1815 return ERR_PTR(error);
1819 if (flag & O_NOFOLLOW)
1822 * This is subtle. Instead of calling do_follow_link() we do the
1823 * thing by hands. The reason is that this way we have zero link_count
1824 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1825 * After that we have the parent and last component, i.e.
1826 * we are in the same situation as after the first path_walk().
1827 * Well, almost - if the last component is normal we get its copy
1828 * stored in nd->last.name and we will have to putname() it when we
1829 * are done. Procfs-like symlinks just set LAST_BIND.
1831 nd.flags |= LOOKUP_PARENT;
1832 error = security_inode_follow_link(path.dentry, &nd);
1835 error = __do_follow_link(&path, &nd);
1837 /* Does someone understand code flow here? Or it is only
1838 * me so stupid? Anathema to whoever designed this non-sense
1839 * with "intent.open".
1841 release_open_intent(&nd);
1842 return ERR_PTR(error);
1844 nd.flags &= ~LOOKUP_PARENT;
1845 if (nd.last_type == LAST_BIND)
1848 if (nd.last_type != LAST_NORM)
1850 if (nd.last.name[nd.last.len]) {
1851 __putname(nd.last.name);
1856 __putname(nd.last.name);
1859 dir = nd.path.dentry;
1860 mutex_lock(&dir->d_inode->i_mutex);
1861 path.dentry = lookup_hash(&nd);
1862 path.mnt = nd.path.mnt;
1863 __putname(nd.last.name);
1868 * filp_open - open file and return file pointer
1870 * @filename: path to open
1871 * @flags: open flags as per the open(2) second argument
1872 * @mode: mode for the new file if O_CREAT is set, else ignored
1874 * This is the helper to open a file from kernelspace if you really
1875 * have to. But in generally you should not do this, so please move
1876 * along, nothing to see here..
1878 struct file *filp_open(const char *filename, int flags, int mode)
1880 return do_filp_open(AT_FDCWD, filename, flags, mode);
1882 EXPORT_SYMBOL(filp_open);
1885 * lookup_create - lookup a dentry, creating it if it doesn't exist
1886 * @nd: nameidata info
1887 * @is_dir: directory flag
1889 * Simple function to lookup and return a dentry and create it
1890 * if it doesn't exist. Is SMP-safe.
1892 * Returns with nd->path.dentry->d_inode->i_mutex locked.
1894 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1896 struct dentry *dentry = ERR_PTR(-EEXIST);
1898 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1900 * Yucky last component or no last component at all?
1901 * (foo/., foo/.., /////)
1903 if (nd->last_type != LAST_NORM)
1905 nd->flags &= ~LOOKUP_PARENT;
1906 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
1907 nd->intent.open.flags = O_EXCL;
1910 * Do the final lookup.
1912 dentry = lookup_hash(nd);
1916 if (dentry->d_inode)
1919 * Special case - lookup gave negative, but... we had foo/bar/
1920 * From the vfs_mknod() POV we just have a negative dentry -
1921 * all is fine. Let's be bastards - you had / on the end, you've
1922 * been asking for (non-existent) directory. -ENOENT for you.
1924 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
1926 dentry = ERR_PTR(-ENOENT);
1931 dentry = ERR_PTR(-EEXIST);
1935 EXPORT_SYMBOL_GPL(lookup_create);
1937 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1939 int error = may_create(dir, dentry);
1944 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1947 if (!dir->i_op || !dir->i_op->mknod)
1950 error = devcgroup_inode_mknod(mode, dev);
1954 error = security_inode_mknod(dir, dentry, mode, dev);
1959 error = dir->i_op->mknod(dir, dentry, mode, dev);
1961 fsnotify_create(dir, dentry);
1965 static int may_mknod(mode_t mode)
1967 switch (mode & S_IFMT) {
1973 case 0: /* zero mode translates to S_IFREG */
1982 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1987 struct dentry *dentry;
1988 struct nameidata nd;
1993 error = user_path_parent(dfd, filename, &nd, &tmp);
1997 dentry = lookup_create(&nd, 0);
1998 if (IS_ERR(dentry)) {
1999 error = PTR_ERR(dentry);
2002 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2003 mode &= ~current->fs->umask;
2004 error = may_mknod(mode);
2007 error = mnt_want_write(nd.path.mnt);
2010 switch (mode & S_IFMT) {
2011 case 0: case S_IFREG:
2012 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2014 case S_IFCHR: case S_IFBLK:
2015 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2016 new_decode_dev(dev));
2018 case S_IFIFO: case S_IFSOCK:
2019 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2022 mnt_drop_write(nd.path.mnt);
2026 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2033 asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
2035 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2038 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2040 int error = may_create(dir, dentry);
2045 if (!dir->i_op || !dir->i_op->mkdir)
2048 mode &= (S_IRWXUGO|S_ISVTX);
2049 error = security_inode_mkdir(dir, dentry, mode);
2054 error = dir->i_op->mkdir(dir, dentry, mode);
2056 fsnotify_mkdir(dir, dentry);
2060 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
2064 struct dentry *dentry;
2065 struct nameidata nd;
2067 error = user_path_parent(dfd, pathname, &nd, &tmp);
2071 dentry = lookup_create(&nd, 1);
2072 error = PTR_ERR(dentry);
2076 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2077 mode &= ~current->fs->umask;
2078 error = mnt_want_write(nd.path.mnt);
2081 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2082 mnt_drop_write(nd.path.mnt);
2086 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2093 asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2095 return sys_mkdirat(AT_FDCWD, pathname, mode);
2099 * We try to drop the dentry early: we should have
2100 * a usage count of 2 if we're the only user of this
2101 * dentry, and if that is true (possibly after pruning
2102 * the dcache), then we drop the dentry now.
2104 * A low-level filesystem can, if it choses, legally
2107 * if (!d_unhashed(dentry))
2110 * if it cannot handle the case of removing a directory
2111 * that is still in use by something else..
2113 void dentry_unhash(struct dentry *dentry)
2116 shrink_dcache_parent(dentry);
2117 spin_lock(&dcache_lock);
2118 spin_lock(&dentry->d_lock);
2119 if (atomic_read(&dentry->d_count) == 2)
2121 spin_unlock(&dentry->d_lock);
2122 spin_unlock(&dcache_lock);
2125 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2127 int error = may_delete(dir, dentry, 1);
2132 if (!dir->i_op || !dir->i_op->rmdir)
2137 mutex_lock(&dentry->d_inode->i_mutex);
2138 dentry_unhash(dentry);
2139 if (d_mountpoint(dentry))
2142 error = security_inode_rmdir(dir, dentry);
2144 error = dir->i_op->rmdir(dir, dentry);
2146 dentry->d_inode->i_flags |= S_DEAD;
2149 mutex_unlock(&dentry->d_inode->i_mutex);
2158 static long do_rmdir(int dfd, const char __user *pathname)
2162 struct dentry *dentry;
2163 struct nameidata nd;
2165 error = user_path_parent(dfd, pathname, &nd, &name);
2169 switch(nd.last_type) {
2181 nd.flags &= ~LOOKUP_PARENT;
2183 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2184 dentry = lookup_hash(&nd);
2185 error = PTR_ERR(dentry);
2188 error = mnt_want_write(nd.path.mnt);
2191 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2192 mnt_drop_write(nd.path.mnt);
2196 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2203 asmlinkage long sys_rmdir(const char __user *pathname)
2205 return do_rmdir(AT_FDCWD, pathname);
2208 int vfs_unlink(struct inode *dir, struct dentry *dentry)
2210 int error = may_delete(dir, dentry, 0);
2215 if (!dir->i_op || !dir->i_op->unlink)
2220 mutex_lock(&dentry->d_inode->i_mutex);
2221 if (d_mountpoint(dentry))
2224 error = security_inode_unlink(dir, dentry);
2226 error = dir->i_op->unlink(dir, dentry);
2228 mutex_unlock(&dentry->d_inode->i_mutex);
2230 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2231 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2232 fsnotify_link_count(dentry->d_inode);
2240 * Make sure that the actual truncation of the file will occur outside its
2241 * directory's i_mutex. Truncate can take a long time if there is a lot of
2242 * writeout happening, and we don't want to prevent access to the directory
2243 * while waiting on the I/O.
2245 static long do_unlinkat(int dfd, const char __user *pathname)
2249 struct dentry *dentry;
2250 struct nameidata nd;
2251 struct inode *inode = NULL;
2253 error = user_path_parent(dfd, pathname, &nd, &name);
2258 if (nd.last_type != LAST_NORM)
2261 nd.flags &= ~LOOKUP_PARENT;
2263 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2264 dentry = lookup_hash(&nd);
2265 error = PTR_ERR(dentry);
2266 if (!IS_ERR(dentry)) {
2267 /* Why not before? Because we want correct error value */
2268 if (nd.last.name[nd.last.len])
2270 inode = dentry->d_inode;
2272 atomic_inc(&inode->i_count);
2273 error = mnt_want_write(nd.path.mnt);
2276 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2277 mnt_drop_write(nd.path.mnt);
2281 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2283 iput(inode); /* truncate the inode here */
2290 error = !dentry->d_inode ? -ENOENT :
2291 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2295 asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2297 if ((flag & ~AT_REMOVEDIR) != 0)
2300 if (flag & AT_REMOVEDIR)
2301 return do_rmdir(dfd, pathname);
2303 return do_unlinkat(dfd, pathname);
2306 asmlinkage long sys_unlink(const char __user *pathname)
2308 return do_unlinkat(AT_FDCWD, pathname);
2311 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
2313 int error = may_create(dir, dentry);
2318 if (!dir->i_op || !dir->i_op->symlink)
2321 error = security_inode_symlink(dir, dentry, oldname);
2326 error = dir->i_op->symlink(dir, dentry, oldname);
2328 fsnotify_create(dir, dentry);
2332 asmlinkage long sys_symlinkat(const char __user *oldname,
2333 int newdfd, const char __user *newname)
2338 struct dentry *dentry;
2339 struct nameidata nd;
2341 from = getname(oldname);
2343 return PTR_ERR(from);
2345 error = user_path_parent(newdfd, newname, &nd, &to);
2349 dentry = lookup_create(&nd, 0);
2350 error = PTR_ERR(dentry);
2354 error = mnt_want_write(nd.path.mnt);
2357 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2358 mnt_drop_write(nd.path.mnt);
2362 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2370 asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2372 return sys_symlinkat(oldname, AT_FDCWD, newname);
2375 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2377 struct inode *inode = old_dentry->d_inode;
2383 error = may_create(dir, new_dentry);
2387 if (dir->i_sb != inode->i_sb)
2391 * A link to an append-only or immutable file cannot be created.
2393 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2395 if (!dir->i_op || !dir->i_op->link)
2397 if (S_ISDIR(inode->i_mode))
2400 error = security_inode_link(old_dentry, dir, new_dentry);
2404 mutex_lock(&inode->i_mutex);
2406 error = dir->i_op->link(old_dentry, dir, new_dentry);
2407 mutex_unlock(&inode->i_mutex);
2409 fsnotify_link(dir, inode, new_dentry);
2414 * Hardlinks are often used in delicate situations. We avoid
2415 * security-related surprises by not following symlinks on the
2418 * We don't follow them on the oldname either to be compatible
2419 * with linux 2.0, and to avoid hard-linking to directories
2420 * and other special files. --ADM
2422 asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
2423 int newdfd, const char __user *newname,
2426 struct dentry *new_dentry;
2427 struct nameidata nd;
2428 struct path old_path;
2432 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2435 error = user_path_at(olddfd, oldname,
2436 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2441 error = user_path_parent(newdfd, newname, &nd, &to);
2445 if (old_path.mnt != nd.path.mnt)
2447 new_dentry = lookup_create(&nd, 0);
2448 error = PTR_ERR(new_dentry);
2449 if (IS_ERR(new_dentry))
2451 error = mnt_want_write(nd.path.mnt);
2454 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2455 mnt_drop_write(nd.path.mnt);
2459 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2464 path_put(&old_path);
2469 asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2471 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
2475 * The worst of all namespace operations - renaming directory. "Perverted"
2476 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2478 * a) we can get into loop creation. Check is done in is_subdir().
2479 * b) race potential - two innocent renames can create a loop together.
2480 * That's where 4.4 screws up. Current fix: serialization on
2481 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2483 * c) we have to lock _three_ objects - parents and victim (if it exists).
2484 * And that - after we got ->i_mutex on parents (until then we don't know
2485 * whether the target exists). Solution: try to be smart with locking
2486 * order for inodes. We rely on the fact that tree topology may change
2487 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2488 * move will be locked. Thus we can rank directories by the tree
2489 * (ancestors first) and rank all non-directories after them.
2490 * That works since everybody except rename does "lock parent, lookup,
2491 * lock child" and rename is under ->s_vfs_rename_mutex.
2492 * HOWEVER, it relies on the assumption that any object with ->lookup()
2493 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2494 * we'd better make sure that there's no link(2) for them.
2495 * d) some filesystems don't support opened-but-unlinked directories,
2496 * either because of layout or because they are not ready to deal with
2497 * all cases correctly. The latter will be fixed (taking this sort of
2498 * stuff into VFS), but the former is not going away. Solution: the same
2499 * trick as in rmdir().
2500 * e) conversion from fhandle to dentry may come in the wrong moment - when
2501 * we are removing the target. Solution: we will have to grab ->i_mutex
2502 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2503 * ->i_mutex on parents, which works but leads to some truely excessive
2506 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2507 struct inode *new_dir, struct dentry *new_dentry)
2510 struct inode *target;
2513 * If we are going to change the parent - check write permissions,
2514 * we'll need to flip '..'.
2516 if (new_dir != old_dir) {
2517 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
2522 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2526 target = new_dentry->d_inode;
2528 mutex_lock(&target->i_mutex);
2529 dentry_unhash(new_dentry);
2531 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2534 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2537 target->i_flags |= S_DEAD;
2538 mutex_unlock(&target->i_mutex);
2539 if (d_unhashed(new_dentry))
2540 d_rehash(new_dentry);
2544 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2545 d_move(old_dentry,new_dentry);
2549 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2550 struct inode *new_dir, struct dentry *new_dentry)
2552 struct inode *target;
2555 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2560 target = new_dentry->d_inode;
2562 mutex_lock(&target->i_mutex);
2563 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2566 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2568 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2569 d_move(old_dentry, new_dentry);
2572 mutex_unlock(&target->i_mutex);
2577 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2578 struct inode *new_dir, struct dentry *new_dentry)
2581 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2582 const char *old_name;
2584 if (old_dentry->d_inode == new_dentry->d_inode)
2587 error = may_delete(old_dir, old_dentry, is_dir);
2591 if (!new_dentry->d_inode)
2592 error = may_create(new_dir, new_dentry);
2594 error = may_delete(new_dir, new_dentry, is_dir);
2598 if (!old_dir->i_op || !old_dir->i_op->rename)
2601 DQUOT_INIT(old_dir);
2602 DQUOT_INIT(new_dir);
2604 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2607 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2609 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2611 const char *new_name = old_dentry->d_name.name;
2612 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2613 new_dentry->d_inode, old_dentry);
2615 fsnotify_oldname_free(old_name);
2620 asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2621 int newdfd, const char __user *newname)
2623 struct dentry *old_dir, *new_dir;
2624 struct dentry *old_dentry, *new_dentry;
2625 struct dentry *trap;
2626 struct nameidata oldnd, newnd;
2631 error = user_path_parent(olddfd, oldname, &oldnd, &from);
2635 error = user_path_parent(newdfd, newname, &newnd, &to);
2640 if (oldnd.path.mnt != newnd.path.mnt)
2643 old_dir = oldnd.path.dentry;
2645 if (oldnd.last_type != LAST_NORM)
2648 new_dir = newnd.path.dentry;
2649 if (newnd.last_type != LAST_NORM)
2652 oldnd.flags &= ~LOOKUP_PARENT;
2653 newnd.flags &= ~LOOKUP_PARENT;
2654 newnd.flags |= LOOKUP_RENAME_TARGET;
2656 trap = lock_rename(new_dir, old_dir);
2658 old_dentry = lookup_hash(&oldnd);
2659 error = PTR_ERR(old_dentry);
2660 if (IS_ERR(old_dentry))
2662 /* source must exist */
2664 if (!old_dentry->d_inode)
2666 /* unless the source is a directory trailing slashes give -ENOTDIR */
2667 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2669 if (oldnd.last.name[oldnd.last.len])
2671 if (newnd.last.name[newnd.last.len])
2674 /* source should not be ancestor of target */
2676 if (old_dentry == trap)
2678 new_dentry = lookup_hash(&newnd);
2679 error = PTR_ERR(new_dentry);
2680 if (IS_ERR(new_dentry))
2682 /* target should not be an ancestor of source */
2684 if (new_dentry == trap)
2687 error = mnt_want_write(oldnd.path.mnt);
2690 error = vfs_rename(old_dir->d_inode, old_dentry,
2691 new_dir->d_inode, new_dentry);
2692 mnt_drop_write(oldnd.path.mnt);
2698 unlock_rename(new_dir, old_dir);
2700 path_put(&newnd.path);
2703 path_put(&oldnd.path);
2709 asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2711 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2714 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2718 len = PTR_ERR(link);
2723 if (len > (unsigned) buflen)
2725 if (copy_to_user(buffer, link, len))
2732 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2733 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2734 * using) it for any given inode is up to filesystem.
2736 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2738 struct nameidata nd;
2743 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2745 return PTR_ERR(cookie);
2747 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2748 if (dentry->d_inode->i_op->put_link)
2749 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2753 int vfs_follow_link(struct nameidata *nd, const char *link)
2755 return __vfs_follow_link(nd, link);
2758 /* get the link contents into pagecache */
2759 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2762 struct address_space *mapping = dentry->d_inode->i_mapping;
2763 page = read_mapping_page(mapping, 0, NULL);
2770 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2772 struct page *page = NULL;
2773 char *s = page_getlink(dentry, &page);
2774 int res = vfs_readlink(dentry,buffer,buflen,s);
2777 page_cache_release(page);
2782 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2784 struct page *page = NULL;
2785 nd_set_link(nd, page_getlink(dentry, &page));
2789 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2791 struct page *page = cookie;
2795 page_cache_release(page);
2799 int __page_symlink(struct inode *inode, const char *symname, int len,
2802 struct address_space *mapping = inode->i_mapping;
2809 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2810 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
2814 kaddr = kmap_atomic(page, KM_USER0);
2815 memcpy(kaddr, symname, len-1);
2816 kunmap_atomic(kaddr, KM_USER0);
2818 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2825 mark_inode_dirty(inode);
2831 int page_symlink(struct inode *inode, const char *symname, int len)
2833 return __page_symlink(inode, symname, len,
2834 mapping_gfp_mask(inode->i_mapping));
2837 const struct inode_operations page_symlink_inode_operations = {
2838 .readlink = generic_readlink,
2839 .follow_link = page_follow_link_light,
2840 .put_link = page_put_link,
2843 EXPORT_SYMBOL(user_path_at);
2844 EXPORT_SYMBOL(follow_down);
2845 EXPORT_SYMBOL(follow_up);
2846 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2847 EXPORT_SYMBOL(getname);
2848 EXPORT_SYMBOL(lock_rename);
2849 EXPORT_SYMBOL(lookup_one_len);
2850 EXPORT_SYMBOL(page_follow_link_light);
2851 EXPORT_SYMBOL(page_put_link);
2852 EXPORT_SYMBOL(page_readlink);
2853 EXPORT_SYMBOL(__page_symlink);
2854 EXPORT_SYMBOL(page_symlink);
2855 EXPORT_SYMBOL(page_symlink_inode_operations);
2856 EXPORT_SYMBOL(path_lookup);
2857 EXPORT_SYMBOL(kern_path);
2858 EXPORT_SYMBOL(vfs_path_lookup);
2859 EXPORT_SYMBOL(inode_permission);
2860 EXPORT_SYMBOL(vfs_permission);
2861 EXPORT_SYMBOL(file_permission);
2862 EXPORT_SYMBOL(unlock_rename);
2863 EXPORT_SYMBOL(vfs_create);
2864 EXPORT_SYMBOL(vfs_follow_link);
2865 EXPORT_SYMBOL(vfs_link);
2866 EXPORT_SYMBOL(vfs_mkdir);
2867 EXPORT_SYMBOL(vfs_mknod);
2868 EXPORT_SYMBOL(generic_permission);
2869 EXPORT_SYMBOL(vfs_readlink);
2870 EXPORT_SYMBOL(vfs_rename);
2871 EXPORT_SYMBOL(vfs_rmdir);
2872 EXPORT_SYMBOL(vfs_symlink);
2873 EXPORT_SYMBOL(vfs_unlink);
2874 EXPORT_SYMBOL(dentry_unhash);
2875 EXPORT_SYMBOL(generic_readlink);