[PATCH] kill altroot
[linux-2.6/verdex.git] / fs / namei.c
blob095818089ac10c71e47f7e0ce55e1110fec1afe7
1 /*
2 * linux/fs/namei.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * Some corrections by tytso.
9 */
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
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>
20 #include <linux/fs.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/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)
120 int retval;
121 unsigned long len = PATH_MAX;
123 if (!segment_eq(get_fs(), KERNEL_DS)) {
124 if ((unsigned long) filename >= TASK_SIZE)
125 return -EFAULT;
126 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
130 retval = strncpy_from_user(page, filename, len);
131 if (retval > 0) {
132 if (retval < len)
133 return 0;
134 return -ENAMETOOLONG;
135 } else if (!retval)
136 retval = -ENOENT;
137 return retval;
140 char * getname(const char __user * filename)
142 char *tmp, *result;
144 result = ERR_PTR(-ENOMEM);
145 tmp = __getname();
146 if (tmp) {
147 int retval = do_getname(filename, tmp);
149 result = tmp;
150 if (retval < 0) {
151 __putname(tmp);
152 result = ERR_PTR(retval);
155 audit_getname(result);
156 return result;
159 #ifdef CONFIG_AUDITSYSCALL
160 void putname(const char *name)
162 if (unlikely(!audit_dummy_context()))
163 audit_putname(name);
164 else
165 __putname(name);
167 EXPORT_SYMBOL(putname);
168 #endif
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)
190 mode >>= 6;
191 else {
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)
197 return error;
200 if (in_group_p(inode->i_gid))
201 mode >>= 3;
205 * If the DACs are ok we don't need any capability check.
207 if ((mask & ~mode) == 0)
208 return 0;
210 check_capabilities:
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))
218 return 0;
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))
225 return 0;
227 return -EACCES;
230 int permission(struct inode *inode, int mask, struct nameidata *nd)
232 int retval;
233 struct vfsmount *mnt = NULL;
235 if (nd)
236 mnt = nd->path.mnt;
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)))
246 return -EROFS;
249 * Nobody gets write access to an immutable file.
251 if (IS_IMMUTABLE(inode))
252 return -EACCES;
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))
261 return -EACCES;
264 /* Ordinary permission routines do not understand MAY_APPEND. */
265 if (inode->i_op && inode->i_op->permission) {
266 int extra = 0;
267 if (nd) {
268 if (nd->flags & LOOKUP_ACCESS)
269 extra |= MAY_ACCESS;
270 if (nd->flags & LOOKUP_OPEN)
271 extra |= MAY_OPEN;
273 retval = inode->i_op->permission(inode, mask | extra);
274 if (!retval) {
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()
280 * method.
282 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
283 !(inode->i_mode & S_IXUGO))
284 return -EACCES;
286 } else {
287 retval = generic_permission(inode, mask, NULL);
289 if (retval)
290 return retval;
292 retval = devcgroup_inode_permission(inode, mask);
293 if (retval)
294 return retval;
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
321 * file.
323 * Note:
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);
355 return -ETXTBSY;
357 atomic_inc(&inode->i_writecount);
358 spin_unlock(&inode->i_lock);
360 return 0;
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);
370 return -ETXTBSY;
372 atomic_dec(&inode->i_writecount);
373 spin_unlock(&inode->i_lock);
375 return 0;
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)
386 mntget(path->mnt);
387 dget(path->dentry);
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)
399 dput(path->dentry);
400 mntput(path->mnt);
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);
412 else
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.
427 if (!status) {
428 if (!d_invalidate(dentry)) {
429 dput(dentry);
430 dentry = NULL;
432 } else {
433 dput(dentry);
434 dentry = ERR_PTR(status);
437 return dentry;
441 * Internal lookup() using the new generic dcache.
442 * SMP-safe
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
451 if (!dentry)
452 dentry = d_lookup(parent, name);
454 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
455 dentry = do_revalidate(dentry, nd);
457 return dentry;
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)
476 return -EAGAIN;
478 if (current->fsuid == inode->i_uid)
479 mode >>= 6;
480 else if (in_group_p(inode->i_gid))
481 mode >>= 3;
483 if (mode & MAY_EXEC)
484 goto ok;
486 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
487 goto ok;
489 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
490 goto ok;
492 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
493 goto ok;
495 return -EACCES;
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..
506 * SMP-safe
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
524 * fast walk).
526 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
528 result = d_lookup(parent, name);
529 if (!result) {
530 struct dentry *dentry;
532 /* Don't create child dentry for a dead directory. */
533 result = ERR_PTR(-ENOENT);
534 if (IS_DEADDIR(dir))
535 goto out_unlock;
537 dentry = d_alloc(parent, name);
538 result = ERR_PTR(-ENOMEM);
539 if (dentry) {
540 result = dir->i_op->lookup(dir, dentry, nd);
541 if (result)
542 dput(dentry);
543 else
544 result = dentry;
546 out_unlock:
547 mutex_unlock(&dir->i_mutex);
548 return result;
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);
558 if (!result)
559 result = ERR_PTR(-ENOENT);
561 return result;
564 /* SMP-safe */
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);
571 nd->path = fs->root;
572 path_get(&fs->root);
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;
586 int result;
588 /* make sure the stuff we saved doesn't go away */
589 path_get(&save);
591 result = __link_path_walk(name, nd);
592 if (result == -ESTALE) {
593 /* nd->path had been dropped */
594 nd->path = save;
595 path_get(&nd->path);
596 nd->flags |= LOOKUP_REVAL;
597 result = __link_path_walk(name, nd);
600 path_put(&save);
602 return result;
605 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
607 int res = 0;
608 char *name;
609 if (IS_ERR(link))
610 goto fail;
612 if (*link == '/') {
613 path_put(&nd->path);
614 walk_init_root(link, nd);
616 res = link_path_walk(link, nd);
617 if (nd->depth || res || nd->last_type!=LAST_NORM)
618 return res;
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...
624 name = __getname();
625 if (unlikely(!name)) {
626 path_put(&nd->path);
627 return -ENOMEM;
629 strcpy(name, nd->last.name);
630 nd->last.name = name;
631 return 0;
632 fail:
633 path_put(&nd->path);
634 return PTR_ERR(link);
637 static void path_put_conditional(struct path *path, struct nameidata *nd)
639 dput(path->dentry);
640 if (path->mnt != nd->path.mnt)
641 mntput(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)
655 int error;
656 void *cookie;
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);
664 dget(dentry);
666 mntget(path->mnt);
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);
671 error = 0;
672 if (s)
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);
677 path_put(path);
679 return error;
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)
691 int err = -ELOOP;
692 if (current->link_count >= MAX_NESTED_LINKS)
693 goto loop;
694 if (current->total_link_count >= 40)
695 goto loop;
696 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
697 cond_resched();
698 err = security_inode_follow_link(path->dentry, nd);
699 if (err)
700 goto loop;
701 current->link_count++;
702 current->total_link_count++;
703 nd->depth++;
704 err = __do_follow_link(path, nd);
705 current->link_count--;
706 nd->depth--;
707 return err;
708 loop:
709 path_put_conditional(path, nd);
710 path_put(&nd->path);
711 return err;
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);
722 return 0;
724 mntget(parent);
725 mountpoint=dget((*mnt)->mnt_mountpoint);
726 spin_unlock(&vfsmount_lock);
727 dput(*dentry);
728 *dentry = mountpoint;
729 mntput(*mnt);
730 *mnt = parent;
731 return 1;
734 /* no need for dcache_lock, as serialization is taken care in
735 * namespace.c
737 static int __follow_mount(struct path *path)
739 int res = 0;
740 while (d_mountpoint(path->dentry)) {
741 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
742 if (!mounted)
743 break;
744 dput(path->dentry);
745 if (res)
746 mntput(path->mnt);
747 path->mnt = mounted;
748 path->dentry = dget(mounted->mnt_root);
749 res = 1;
751 return res;
754 static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
756 while (d_mountpoint(*dentry)) {
757 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
758 if (!mounted)
759 break;
760 dput(*dentry);
761 mntput(*mnt);
762 *mnt = mounted;
763 *dentry = dget(mounted->mnt_root);
767 /* no need for dcache_lock, as serialization is taken care in
768 * namespace.c
770 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
772 struct vfsmount *mounted;
774 mounted = lookup_mnt(*mnt, *dentry);
775 if (mounted) {
776 dput(*dentry);
777 mntput(*mnt);
778 *mnt = mounted;
779 *dentry = dget(mounted->mnt_root);
780 return 1;
782 return 0;
785 static __always_inline void follow_dotdot(struct nameidata *nd)
787 struct fs_struct *fs = current->fs;
789 while(1) {
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);
797 break;
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);
804 dput(old);
805 break;
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);
812 break;
814 mntget(parent);
815 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
816 spin_unlock(&vfsmount_lock);
817 dput(old);
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,
830 struct path *path)
832 struct vfsmount *mnt = nd->path.mnt;
833 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
835 if (!dentry)
836 goto need_lookup;
837 if (dentry->d_op && dentry->d_op->d_revalidate)
838 goto need_revalidate;
839 done:
840 path->mnt = mnt;
841 path->dentry = dentry;
842 __follow_mount(path);
843 return 0;
845 need_lookup:
846 dentry = real_lookup(nd->path.dentry, name, nd);
847 if (IS_ERR(dentry))
848 goto fail;
849 goto done;
851 need_revalidate:
852 dentry = do_revalidate(dentry, nd);
853 if (!dentry)
854 goto need_lookup;
855 if (IS_ERR(dentry))
856 goto fail;
857 goto done;
859 fail:
860 return PTR_ERR(dentry);
864 * Name resolution.
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)
873 struct path next;
874 struct inode *inode;
875 int err;
876 unsigned int lookup_flags = nd->flags;
878 while (*name=='/')
879 name++;
880 if (!*name)
881 goto return_reval;
883 inode = nd->path.dentry->d_inode;
884 if (nd->depth)
885 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
887 /* At this point we know we have a real path component. */
888 for(;;) {
889 unsigned long hash;
890 struct qstr this;
891 unsigned int c;
893 nd->flags |= LOOKUP_CONTINUE;
894 err = exec_permission_lite(inode, nd);
895 if (err == -EAGAIN)
896 err = vfs_permission(nd, MAY_EXEC);
897 if (err)
898 break;
900 this.name = name;
901 c = *(const unsigned char *)name;
903 hash = init_name_hash();
904 do {
905 name++;
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? */
913 if (!c)
914 goto last_component;
915 while (*++name == '/');
916 if (!*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) {
925 default:
926 break;
927 case 2:
928 if (this.name[1] != '.')
929 break;
930 follow_dotdot(nd);
931 inode = nd->path.dentry->d_inode;
932 /* fallthrough */
933 case 1:
934 continue;
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,
942 &this);
943 if (err < 0)
944 break;
946 /* This does the actual lookups.. */
947 err = do_lookup(nd, &this, &next);
948 if (err)
949 break;
951 err = -ENOENT;
952 inode = next.dentry->d_inode;
953 if (!inode)
954 goto out_dput;
955 err = -ENOTDIR;
956 if (!inode->i_op)
957 goto out_dput;
959 if (inode->i_op->follow_link) {
960 err = do_follow_link(&next, nd);
961 if (err)
962 goto return_err;
963 err = -ENOENT;
964 inode = nd->path.dentry->d_inode;
965 if (!inode)
966 break;
967 err = -ENOTDIR;
968 if (!inode->i_op)
969 break;
970 } else
971 path_to_nameidata(&next, nd);
972 err = -ENOTDIR;
973 if (!inode->i_op->lookup)
974 break;
975 continue;
976 /* here ends the main loop */
978 last_with_slashes:
979 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
980 last_component:
981 /* Clear LOOKUP_CONTINUE iff it was previously unset */
982 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
983 if (lookup_flags & LOOKUP_PARENT)
984 goto lookup_parent;
985 if (this.name[0] == '.') switch (this.len) {
986 default:
987 break;
988 case 2:
989 if (this.name[1] != '.')
990 break;
991 follow_dotdot(nd);
992 inode = nd->path.dentry->d_inode;
993 /* fallthrough */
994 case 1:
995 goto return_reval;
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,
999 &this);
1000 if (err < 0)
1001 break;
1003 err = do_lookup(nd, &this, &next);
1004 if (err)
1005 break;
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);
1010 if (err)
1011 goto return_err;
1012 inode = nd->path.dentry->d_inode;
1013 } else
1014 path_to_nameidata(&next, nd);
1015 err = -ENOENT;
1016 if (!inode)
1017 break;
1018 if (lookup_flags & LOOKUP_DIRECTORY) {
1019 err = -ENOTDIR;
1020 if (!inode->i_op || !inode->i_op->lookup)
1021 break;
1023 goto return_base;
1024 lookup_parent:
1025 nd->last = this;
1026 nd->last_type = LAST_NORM;
1027 if (this.name[0] != '.')
1028 goto return_base;
1029 if (this.len == 1)
1030 nd->last_type = LAST_DOT;
1031 else if (this.len == 2 && this.name[1] == '.')
1032 nd->last_type = LAST_DOTDOT;
1033 else
1034 goto return_base;
1035 return_reval:
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)) {
1042 err = -ESTALE;
1043 /* Note: we do not d_invalidate() */
1044 if (!nd->path.dentry->d_op->d_revalidate(
1045 nd->path.dentry, nd))
1046 break;
1048 return_base:
1049 return 0;
1050 out_dput:
1051 path_put_conditional(&next, nd);
1052 break;
1054 path_put(&nd->path);
1055 return_err:
1056 return err;
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)
1069 int retval = 0;
1070 int fput_needed;
1071 struct file *file;
1072 struct fs_struct *fs = current->fs;
1074 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1075 nd->flags = flags;
1076 nd->depth = 0;
1078 if (*name=='/') {
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);
1085 nd->path = fs->pwd;
1086 path_get(&fs->pwd);
1087 read_unlock(&fs->lock);
1088 } else {
1089 struct dentry *dentry;
1091 file = fget_light(dfd, &fput_needed);
1092 retval = -EBADF;
1093 if (!file)
1094 goto out_fail;
1096 dentry = file->f_path.dentry;
1098 retval = -ENOTDIR;
1099 if (!S_ISDIR(dentry->d_inode->i_mode))
1100 goto fput_fail;
1102 retval = file_permission(file, MAY_EXEC);
1103 if (retval)
1104 goto fput_fail;
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);
1116 out_fail:
1117 return retval;
1119 fput_fail:
1120 fput_light(file, fput_needed);
1121 goto out_fail;
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)
1142 int retval;
1144 /* same as do_path_lookup */
1145 nd->last_type = LAST_ROOT;
1146 nd->flags = flags;
1147 nd->depth = 0;
1149 nd->path.dentry = dentry;
1150 nd->path.mnt = mnt;
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);
1158 return retval;
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();
1167 int err;
1169 if (filp == NULL)
1170 return -ENFILE;
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)) {
1176 if (err == 0) {
1177 err = PTR_ERR(nd->intent.open.file);
1178 path_put(&nd->path);
1180 } else if (err != 0)
1181 release_open_intent(nd);
1182 return err;
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,
1197 open_flags, 0);
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);
1223 if (!IS_ERR(tmp)) {
1224 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
1225 putname(tmp);
1227 return err;
1230 static struct dentry *__lookup_hash(struct qstr *name,
1231 struct dentry *base, struct nameidata *nd)
1233 struct dentry *dentry;
1234 struct inode *inode;
1235 int err;
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);
1246 if (err < 0)
1247 goto out;
1250 dentry = cached_lookup(base, name, nd);
1251 if (!dentry) {
1252 struct dentry *new;
1254 /* Don't create child dentry for a dead directory. */
1255 dentry = ERR_PTR(-ENOENT);
1256 if (IS_DEADDIR(inode))
1257 goto out;
1259 new = d_alloc(base, name);
1260 dentry = ERR_PTR(-ENOMEM);
1261 if (!new)
1262 goto out;
1263 dentry = inode->i_op->lookup(inode, new, nd);
1264 if (!dentry)
1265 dentry = new;
1266 else
1267 dput(new);
1269 out:
1270 return dentry;
1274 * Restricted form of lookup. Doesn't follow links, single-component only,
1275 * needs parent already locked. Doesn't follow mounts.
1276 * SMP-safe.
1278 static struct dentry *lookup_hash(struct nameidata *nd)
1280 int err;
1282 err = permission(nd->path.dentry->d_inode, MAY_EXEC, nd);
1283 if (err)
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)
1291 unsigned long hash;
1292 unsigned int c;
1294 this->name = name;
1295 this->len = len;
1296 if (!len)
1297 return -EACCES;
1299 hash = init_name_hash();
1300 while (len--) {
1301 c = *(const unsigned char *)name++;
1302 if (c == '/' || c == '\0')
1303 return -EACCES;
1304 hash = partial_name_hash(c, hash);
1306 this->hash = end_name_hash(hash);
1307 return 0;
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)
1323 int err;
1324 struct qstr this;
1326 err = __lookup_one_len(name, &this, base, len);
1327 if (err)
1328 return ERR_PTR(err);
1330 err = permission(base->d_inode, MAY_EXEC, NULL);
1331 if (err)
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)
1349 int err;
1350 struct qstr this;
1352 err = __lookup_one_len(name, &this, base, strlen(name));
1353 if (err)
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);
1364 if (!IS_ERR(tmp)) {
1365 err = do_path_lookup(dfd, tmp, flags, nd);
1366 putname(tmp);
1368 return err;
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
1378 * minimal.
1380 static inline int check_sticky(struct inode *dir, struct inode *inode)
1382 if (!(dir->i_mode & S_ISVTX))
1383 return 0;
1384 if (inode->i_uid == current->fsuid)
1385 return 0;
1386 if (dir->i_uid == current->fsuid)
1387 return 0;
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)
1412 int error;
1414 if (!victim->d_inode)
1415 return -ENOENT;
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);
1421 if (error)
1422 return error;
1423 if (IS_APPEND(dir))
1424 return -EPERM;
1425 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1426 IS_IMMUTABLE(victim->d_inode))
1427 return -EPERM;
1428 if (isdir) {
1429 if (!S_ISDIR(victim->d_inode->i_mode))
1430 return -ENOTDIR;
1431 if (IS_ROOT(victim))
1432 return -EBUSY;
1433 } else if (S_ISDIR(victim->d_inode->i_mode))
1434 return -EISDIR;
1435 if (IS_DEADDIR(dir))
1436 return -ENOENT;
1437 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1438 return -EBUSY;
1439 return 0;
1442 /* Check whether we can create an object with dentry child in directory
1443 * dir.
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)
1453 if (child->d_inode)
1454 return -EEXIST;
1455 if (IS_DEADDIR(dir))
1456 return -ENOENT;
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;
1467 if (f & O_NOFOLLOW)
1468 retval &= ~LOOKUP_FOLLOW;
1470 if (f & O_DIRECTORY)
1471 retval |= LOOKUP_DIRECTORY;
1473 return retval;
1477 * p1 and p2 should be directories on the same fs.
1479 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1481 struct dentry *p;
1483 if (p1 == p2) {
1484 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1485 return NULL;
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);
1494 return p;
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);
1502 return p;
1506 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1507 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1508 return NULL;
1511 void unlock_rename(struct dentry *p1, struct dentry *p2)
1513 mutex_unlock(&p1->d_inode->i_mutex);
1514 if (p1 != p2) {
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);
1525 if (error)
1526 return error;
1528 if (!dir->i_op || !dir->i_op->create)
1529 return -EACCES; /* shouldn't it be ENOSYS? */
1530 mode &= S_IALLUGO;
1531 mode |= S_IFREG;
1532 error = security_inode_create(dir, dentry, mode);
1533 if (error)
1534 return error;
1535 DQUOT_INIT(dir);
1536 error = dir->i_op->create(dir, dentry, mode, nd);
1537 if (!error)
1538 fsnotify_create(dir, dentry);
1539 return error;
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;
1546 int error;
1548 if (!inode)
1549 return -ENOENT;
1551 if (S_ISLNK(inode->i_mode))
1552 return -ELOOP;
1554 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
1555 return -EISDIR;
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)) {
1563 flag &= ~O_TRUNC;
1564 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1565 if (nd->path.mnt->mnt_flags & MNT_NODEV)
1566 return -EACCES;
1568 flag &= ~O_TRUNC;
1571 error = vfs_permission(nd, acc_mode);
1572 if (error)
1573 return error;
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))
1579 return -EPERM;
1580 if (flag & O_TRUNC)
1581 return -EPERM;
1584 /* O_NOATIME can only be set by the owner or superuser */
1585 if (flag & O_NOATIME)
1586 if (!is_owner_or_cap(inode))
1587 return -EPERM;
1590 * Ensure there are no outstanding leases on the file.
1592 error = break_lease(inode, flag);
1593 if (error)
1594 return error;
1596 if (flag & O_TRUNC) {
1597 error = get_write_access(inode);
1598 if (error)
1599 return error;
1602 * Refuse to truncate files with mandatory locks held on them.
1604 error = locks_verify_locked(inode);
1605 if (!error) {
1606 DQUOT_INIT(inode);
1608 error = do_truncate(dentry, 0,
1609 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1610 NULL);
1612 put_write_access(inode);
1613 if (error)
1614 return error;
1615 } else
1616 if (flag & FMODE_WRITE)
1617 DQUOT_INIT(inode);
1619 return 0;
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,
1628 int flag, int mode)
1630 int error;
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;
1639 if (error)
1640 return error;
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:
1647 * 00 - read-only
1648 * 01 - write-only
1649 * 10 - read-write
1650 * 11 - special
1651 * it is changed into
1652 * 00 - no permissions needed
1653 * 01 - read-permission
1654 * 10 - write-permission
1655 * 11 - read-write
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
1659 * later).
1662 static inline int open_to_namei_flags(int flag)
1664 if ((flag+1) & O_ACCMODE)
1665 flag++;
1666 return flag;
1669 static int open_will_write_to_fs(int flag, struct inode *inode)
1672 * We'll never write to the fs underlying
1673 * a device file.
1675 if (special_file(inode->i_mode))
1676 return 0;
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)
1688 struct file *filp;
1689 struct nameidata nd;
1690 int acc_mode, error;
1691 struct path path;
1692 struct dentry *dir;
1693 int count = 0;
1694 int will_write;
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 */
1700 if (flag & O_TRUNC)
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),
1713 &nd, flag);
1714 if (error)
1715 return ERR_PTR(error);
1716 goto ok;
1720 * Create - we need to know the parent.
1722 error = path_lookup_create(dfd, pathname, LOOKUP_PARENT,
1723 &nd, flag, mode);
1724 if (error)
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
1730 * will not do.
1732 error = -EISDIR;
1733 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
1734 goto exit;
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;
1742 do_last:
1743 error = PTR_ERR(path.dentry);
1744 if (IS_ERR(path.dentry)) {
1745 mutex_unlock(&dir->d_inode->i_mutex);
1746 goto exit;
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);
1764 if (error)
1765 goto exit_mutex_unlock;
1766 error = __open_namei_create(&nd, &path, flag, mode);
1767 if (error) {
1768 mnt_drop_write(nd.path.mnt);
1769 goto exit;
1771 filp = nameidata_to_filp(&nd, open_flag);
1772 mnt_drop_write(nd.path.mnt);
1773 return filp;
1777 * It already exists.
1779 mutex_unlock(&dir->d_inode->i_mutex);
1780 audit_inode(pathname, path.dentry);
1782 error = -EEXIST;
1783 if (flag & O_EXCL)
1784 goto exit_dput;
1786 if (__follow_mount(&path)) {
1787 error = -ELOOP;
1788 if (flag & O_NOFOLLOW)
1789 goto exit_dput;
1792 error = -ENOENT;
1793 if (!path.dentry->d_inode)
1794 goto exit_dput;
1795 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1796 goto do_link;
1798 path_to_nameidata(&path, &nd);
1799 error = -EISDIR;
1800 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1801 goto exit;
1804 * Consider:
1805 * 1. may_open() truncates a file
1806 * 2. a rw->ro mount transition occurs
1807 * 3. nameidata_to_filp() fails due to
1808 * the ro mount.
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);
1814 if (will_write) {
1815 error = mnt_want_write(nd.path.mnt);
1816 if (error)
1817 goto exit;
1819 error = may_open(&nd, acc_mode, flag);
1820 if (error) {
1821 if (will_write)
1822 mnt_drop_write(nd.path.mnt);
1823 goto exit;
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
1829 * on its behalf.
1831 if (will_write)
1832 mnt_drop_write(nd.path.mnt);
1833 return filp;
1835 exit_mutex_unlock:
1836 mutex_unlock(&dir->d_inode->i_mutex);
1837 exit_dput:
1838 path_put_conditional(&path, &nd);
1839 exit:
1840 if (!IS_ERR(nd.intent.open.file))
1841 release_open_intent(&nd);
1842 path_put(&nd.path);
1843 return ERR_PTR(error);
1845 do_link:
1846 error = -ELOOP;
1847 if (flag & O_NOFOLLOW)
1848 goto exit_dput;
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);
1861 if (error)
1862 goto exit_dput;
1863 error = __do_follow_link(&path, &nd);
1864 if (error) {
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)
1874 goto ok;
1875 error = -EISDIR;
1876 if (nd.last_type != LAST_NORM)
1877 goto exit;
1878 if (nd.last.name[nd.last.len]) {
1879 __putname(nd.last.name);
1880 goto exit;
1882 error = -ELOOP;
1883 if (count++==32) {
1884 __putname(nd.last.name);
1885 goto exit;
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);
1892 goto do_last;
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)
1932 goto fail;
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);
1941 if (IS_ERR(dentry))
1942 goto fail;
1944 if (dentry->d_inode)
1945 goto eexist;
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])) {
1953 dput(dentry);
1954 dentry = ERR_PTR(-ENOENT);
1956 return dentry;
1957 eexist:
1958 dput(dentry);
1959 dentry = ERR_PTR(-EEXIST);
1960 fail:
1961 return dentry;
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);
1969 if (error)
1970 return error;
1972 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1973 return -EPERM;
1975 if (!dir->i_op || !dir->i_op->mknod)
1976 return -EPERM;
1978 error = devcgroup_inode_mknod(mode, dev);
1979 if (error)
1980 return error;
1982 error = security_inode_mknod(dir, dentry, mode, dev);
1983 if (error)
1984 return error;
1986 DQUOT_INIT(dir);
1987 error = dir->i_op->mknod(dir, dentry, mode, dev);
1988 if (!error)
1989 fsnotify_create(dir, dentry);
1990 return error;
1993 static int may_mknod(mode_t mode)
1995 switch (mode & S_IFMT) {
1996 case S_IFREG:
1997 case S_IFCHR:
1998 case S_IFBLK:
1999 case S_IFIFO:
2000 case S_IFSOCK:
2001 case 0: /* zero mode translates to S_IFREG */
2002 return 0;
2003 case S_IFDIR:
2004 return -EPERM;
2005 default:
2006 return -EINVAL;
2010 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
2011 unsigned dev)
2013 int error = 0;
2014 char * tmp;
2015 struct dentry * dentry;
2016 struct nameidata nd;
2018 if (S_ISDIR(mode))
2019 return -EPERM;
2020 tmp = getname(filename);
2021 if (IS_ERR(tmp))
2022 return PTR_ERR(tmp);
2024 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2025 if (error)
2026 goto out;
2027 dentry = lookup_create(&nd, 0);
2028 if (IS_ERR(dentry)) {
2029 error = PTR_ERR(dentry);
2030 goto out_unlock;
2032 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2033 mode &= ~current->fs->umask;
2034 error = may_mknod(mode);
2035 if (error)
2036 goto out_dput;
2037 error = mnt_want_write(nd.path.mnt);
2038 if (error)
2039 goto out_dput;
2040 switch (mode & S_IFMT) {
2041 case 0: case S_IFREG:
2042 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
2043 break;
2044 case S_IFCHR: case S_IFBLK:
2045 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
2046 new_decode_dev(dev));
2047 break;
2048 case S_IFIFO: case S_IFSOCK:
2049 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
2050 break;
2052 mnt_drop_write(nd.path.mnt);
2053 out_dput:
2054 dput(dentry);
2055 out_unlock:
2056 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2057 path_put(&nd.path);
2058 out:
2059 putname(tmp);
2061 return error;
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);
2073 if (error)
2074 return error;
2076 if (!dir->i_op || !dir->i_op->mkdir)
2077 return -EPERM;
2079 mode &= (S_IRWXUGO|S_ISVTX);
2080 error = security_inode_mkdir(dir, dentry, mode);
2081 if (error)
2082 return error;
2084 DQUOT_INIT(dir);
2085 error = dir->i_op->mkdir(dir, dentry, mode);
2086 if (!error)
2087 fsnotify_mkdir(dir, dentry);
2088 return error;
2091 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
2093 int error = 0;
2094 char * tmp;
2095 struct dentry *dentry;
2096 struct nameidata nd;
2098 tmp = getname(pathname);
2099 error = PTR_ERR(tmp);
2100 if (IS_ERR(tmp))
2101 goto out_err;
2103 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2104 if (error)
2105 goto out;
2106 dentry = lookup_create(&nd, 1);
2107 error = PTR_ERR(dentry);
2108 if (IS_ERR(dentry))
2109 goto out_unlock;
2111 if (!IS_POSIXACL(nd.path.dentry->d_inode))
2112 mode &= ~current->fs->umask;
2113 error = mnt_want_write(nd.path.mnt);
2114 if (error)
2115 goto out_dput;
2116 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2117 mnt_drop_write(nd.path.mnt);
2118 out_dput:
2119 dput(dentry);
2120 out_unlock:
2121 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2122 path_put(&nd.path);
2123 out:
2124 putname(tmp);
2125 out_err:
2126 return error;
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
2141 * do a
2143 * if (!d_unhashed(dentry))
2144 * return -EBUSY;
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)
2151 dget(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)
2156 __d_drop(dentry);
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);
2165 if (error)
2166 return error;
2168 if (!dir->i_op || !dir->i_op->rmdir)
2169 return -EPERM;
2171 DQUOT_INIT(dir);
2173 mutex_lock(&dentry->d_inode->i_mutex);
2174 dentry_unhash(dentry);
2175 if (d_mountpoint(dentry))
2176 error = -EBUSY;
2177 else {
2178 error = security_inode_rmdir(dir, dentry);
2179 if (!error) {
2180 error = dir->i_op->rmdir(dir, dentry);
2181 if (!error)
2182 dentry->d_inode->i_flags |= S_DEAD;
2185 mutex_unlock(&dentry->d_inode->i_mutex);
2186 if (!error) {
2187 d_delete(dentry);
2189 dput(dentry);
2191 return error;
2194 static long do_rmdir(int dfd, const char __user *pathname)
2196 int error = 0;
2197 char * name;
2198 struct dentry *dentry;
2199 struct nameidata nd;
2201 name = getname(pathname);
2202 if(IS_ERR(name))
2203 return PTR_ERR(name);
2205 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2206 if (error)
2207 goto exit;
2209 switch(nd.last_type) {
2210 case LAST_DOTDOT:
2211 error = -ENOTEMPTY;
2212 goto exit1;
2213 case LAST_DOT:
2214 error = -EINVAL;
2215 goto exit1;
2216 case LAST_ROOT:
2217 error = -EBUSY;
2218 goto exit1;
2220 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2221 dentry = lookup_hash(&nd);
2222 error = PTR_ERR(dentry);
2223 if (IS_ERR(dentry))
2224 goto exit2;
2225 error = mnt_want_write(nd.path.mnt);
2226 if (error)
2227 goto exit3;
2228 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2229 mnt_drop_write(nd.path.mnt);
2230 exit3:
2231 dput(dentry);
2232 exit2:
2233 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2234 exit1:
2235 path_put(&nd.path);
2236 exit:
2237 putname(name);
2238 return error;
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);
2250 if (error)
2251 return error;
2253 if (!dir->i_op || !dir->i_op->unlink)
2254 return -EPERM;
2256 DQUOT_INIT(dir);
2258 mutex_lock(&dentry->d_inode->i_mutex);
2259 if (d_mountpoint(dentry))
2260 error = -EBUSY;
2261 else {
2262 error = security_inode_unlink(dir, dentry);
2263 if (!error)
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);
2271 d_delete(dentry);
2274 return error;
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)
2285 int error = 0;
2286 char * name;
2287 struct dentry *dentry;
2288 struct nameidata nd;
2289 struct inode *inode = NULL;
2291 name = getname(pathname);
2292 if(IS_ERR(name))
2293 return PTR_ERR(name);
2295 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
2296 if (error)
2297 goto exit;
2298 error = -EISDIR;
2299 if (nd.last_type != LAST_NORM)
2300 goto exit1;
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])
2307 goto slashes;
2308 inode = dentry->d_inode;
2309 if (inode)
2310 atomic_inc(&inode->i_count);
2311 error = mnt_want_write(nd.path.mnt);
2312 if (error)
2313 goto exit2;
2314 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2315 mnt_drop_write(nd.path.mnt);
2316 exit2:
2317 dput(dentry);
2319 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2320 if (inode)
2321 iput(inode); /* truncate the inode here */
2322 exit1:
2323 path_put(&nd.path);
2324 exit:
2325 putname(name);
2326 return error;
2328 slashes:
2329 error = !dentry->d_inode ? -ENOENT :
2330 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2331 goto exit2;
2334 asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2336 if ((flag & ~AT_REMOVEDIR) != 0)
2337 return -EINVAL;
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);
2354 if (error)
2355 return error;
2357 if (!dir->i_op || !dir->i_op->symlink)
2358 return -EPERM;
2360 error = security_inode_symlink(dir, dentry, oldname);
2361 if (error)
2362 return error;
2364 DQUOT_INIT(dir);
2365 error = dir->i_op->symlink(dir, dentry, oldname);
2366 if (!error)
2367 fsnotify_create(dir, dentry);
2368 return error;
2371 asmlinkage long sys_symlinkat(const char __user *oldname,
2372 int newdfd, const char __user *newname)
2374 int error = 0;
2375 char * from;
2376 char * to;
2377 struct dentry *dentry;
2378 struct nameidata nd;
2380 from = getname(oldname);
2381 if(IS_ERR(from))
2382 return PTR_ERR(from);
2383 to = getname(newname);
2384 error = PTR_ERR(to);
2385 if (IS_ERR(to))
2386 goto out_putname;
2388 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2389 if (error)
2390 goto out;
2391 dentry = lookup_create(&nd, 0);
2392 error = PTR_ERR(dentry);
2393 if (IS_ERR(dentry))
2394 goto out_unlock;
2396 error = mnt_want_write(nd.path.mnt);
2397 if (error)
2398 goto out_dput;
2399 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2400 mnt_drop_write(nd.path.mnt);
2401 out_dput:
2402 dput(dentry);
2403 out_unlock:
2404 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2405 path_put(&nd.path);
2406 out:
2407 putname(to);
2408 out_putname:
2409 putname(from);
2410 return error;
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;
2421 int error;
2423 if (!inode)
2424 return -ENOENT;
2426 error = may_create(dir, new_dentry, NULL);
2427 if (error)
2428 return error;
2430 if (dir->i_sb != inode->i_sb)
2431 return -EXDEV;
2434 * A link to an append-only or immutable file cannot be created.
2436 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2437 return -EPERM;
2438 if (!dir->i_op || !dir->i_op->link)
2439 return -EPERM;
2440 if (S_ISDIR(inode->i_mode))
2441 return -EPERM;
2443 error = security_inode_link(old_dentry, dir, new_dentry);
2444 if (error)
2445 return error;
2447 mutex_lock(&inode->i_mutex);
2448 DQUOT_INIT(dir);
2449 error = dir->i_op->link(old_dentry, dir, new_dentry);
2450 mutex_unlock(&inode->i_mutex);
2451 if (!error)
2452 fsnotify_link(dir, inode, new_dentry);
2453 return error;
2457 * Hardlinks are often used in delicate situations. We avoid
2458 * security-related surprises by not following symlinks on the
2459 * newname. --KAB
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,
2467 int flags)
2469 struct dentry *new_dentry;
2470 struct nameidata nd, old_nd;
2471 int error;
2472 char * to;
2474 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2475 return -EINVAL;
2477 to = getname(newname);
2478 if (IS_ERR(to))
2479 return PTR_ERR(to);
2481 error = __user_walk_fd(olddfd, oldname,
2482 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2483 &old_nd);
2484 if (error)
2485 goto exit;
2486 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2487 if (error)
2488 goto out;
2489 error = -EXDEV;
2490 if (old_nd.path.mnt != nd.path.mnt)
2491 goto out_release;
2492 new_dentry = lookup_create(&nd, 0);
2493 error = PTR_ERR(new_dentry);
2494 if (IS_ERR(new_dentry))
2495 goto out_unlock;
2496 error = mnt_want_write(nd.path.mnt);
2497 if (error)
2498 goto out_dput;
2499 error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
2500 mnt_drop_write(nd.path.mnt);
2501 out_dput:
2502 dput(new_dentry);
2503 out_unlock:
2504 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2505 out_release:
2506 path_put(&nd.path);
2507 out:
2508 path_put(&old_nd.path);
2509 exit:
2510 putname(to);
2512 return error;
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...
2523 * Problems:
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
2528 * story.
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
2550 * locking].
2552 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2553 struct inode *new_dir, struct dentry *new_dentry)
2555 int error = 0;
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);
2564 if (error)
2565 return error;
2568 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2569 if (error)
2570 return error;
2572 target = new_dentry->d_inode;
2573 if (target) {
2574 mutex_lock(&target->i_mutex);
2575 dentry_unhash(new_dentry);
2577 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2578 error = -EBUSY;
2579 else
2580 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2581 if (target) {
2582 if (!error)
2583 target->i_flags |= S_DEAD;
2584 mutex_unlock(&target->i_mutex);
2585 if (d_unhashed(new_dentry))
2586 d_rehash(new_dentry);
2587 dput(new_dentry);
2589 if (!error)
2590 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2591 d_move(old_dentry,new_dentry);
2592 return error;
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;
2599 int error;
2601 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2602 if (error)
2603 return error;
2605 dget(new_dentry);
2606 target = new_dentry->d_inode;
2607 if (target)
2608 mutex_lock(&target->i_mutex);
2609 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2610 error = -EBUSY;
2611 else
2612 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2613 if (!error) {
2614 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2615 d_move(old_dentry, new_dentry);
2617 if (target)
2618 mutex_unlock(&target->i_mutex);
2619 dput(new_dentry);
2620 return error;
2623 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2624 struct inode *new_dir, struct dentry *new_dentry)
2626 int error;
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)
2631 return 0;
2633 error = may_delete(old_dir, old_dentry, is_dir);
2634 if (error)
2635 return error;
2637 if (!new_dentry->d_inode)
2638 error = may_create(new_dir, new_dentry, NULL);
2639 else
2640 error = may_delete(new_dir, new_dentry, is_dir);
2641 if (error)
2642 return error;
2644 if (!old_dir->i_op || !old_dir->i_op->rename)
2645 return -EPERM;
2647 DQUOT_INIT(old_dir);
2648 DQUOT_INIT(new_dir);
2650 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2652 if (is_dir)
2653 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2654 else
2655 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2656 if (!error) {
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);
2663 return error;
2666 static int do_rename(int olddfd, const char *oldname,
2667 int newdfd, const char *newname)
2669 int error = 0;
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);
2676 if (error)
2677 goto exit;
2679 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
2680 if (error)
2681 goto exit1;
2683 error = -EXDEV;
2684 if (oldnd.path.mnt != newnd.path.mnt)
2685 goto exit2;
2687 old_dir = oldnd.path.dentry;
2688 error = -EBUSY;
2689 if (oldnd.last_type != LAST_NORM)
2690 goto exit2;
2692 new_dir = newnd.path.dentry;
2693 if (newnd.last_type != LAST_NORM)
2694 goto exit2;
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))
2701 goto exit3;
2702 /* source must exist */
2703 error = -ENOENT;
2704 if (!old_dentry->d_inode)
2705 goto exit4;
2706 /* unless the source is a directory trailing slashes give -ENOTDIR */
2707 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2708 error = -ENOTDIR;
2709 if (oldnd.last.name[oldnd.last.len])
2710 goto exit4;
2711 if (newnd.last.name[newnd.last.len])
2712 goto exit4;
2714 /* source should not be ancestor of target */
2715 error = -EINVAL;
2716 if (old_dentry == trap)
2717 goto exit4;
2718 new_dentry = lookup_hash(&newnd);
2719 error = PTR_ERR(new_dentry);
2720 if (IS_ERR(new_dentry))
2721 goto exit4;
2722 /* target should not be an ancestor of source */
2723 error = -ENOTEMPTY;
2724 if (new_dentry == trap)
2725 goto exit5;
2727 error = mnt_want_write(oldnd.path.mnt);
2728 if (error)
2729 goto exit5;
2730 error = vfs_rename(old_dir->d_inode, old_dentry,
2731 new_dir->d_inode, new_dentry);
2732 mnt_drop_write(oldnd.path.mnt);
2733 exit5:
2734 dput(new_dentry);
2735 exit4:
2736 dput(old_dentry);
2737 exit3:
2738 unlock_rename(new_dir, old_dir);
2739 exit2:
2740 path_put(&newnd.path);
2741 exit1:
2742 path_put(&oldnd.path);
2743 exit:
2744 return error;
2747 asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2748 int newdfd, const char __user *newname)
2750 int error;
2751 char * from;
2752 char * to;
2754 from = getname(oldname);
2755 if(IS_ERR(from))
2756 return PTR_ERR(from);
2757 to = getname(newname);
2758 error = PTR_ERR(to);
2759 if (!IS_ERR(to)) {
2760 error = do_rename(olddfd, from, newdfd, to);
2761 putname(to);
2763 putname(from);
2764 return error;
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)
2774 int len;
2776 len = PTR_ERR(link);
2777 if (IS_ERR(link))
2778 goto out;
2780 len = strlen(link);
2781 if (len > (unsigned) buflen)
2782 len = buflen;
2783 if (copy_to_user(buffer, link, len))
2784 len = -EFAULT;
2785 out:
2786 return 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;
2797 void *cookie;
2798 int res;
2800 nd.depth = 0;
2801 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2802 if (IS_ERR(cookie))
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);
2808 return res;
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)
2819 struct page * page;
2820 struct address_space *mapping = dentry->d_inode->i_mapping;
2821 page = read_mapping_page(mapping, 0, NULL);
2822 if (IS_ERR(page))
2823 return (char*)page;
2824 *ppage = page;
2825 return kmap(page);
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);
2833 if (page) {
2834 kunmap(page);
2835 page_cache_release(page);
2837 return res;
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));
2844 return page;
2847 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
2849 struct page *page = cookie;
2851 if (page) {
2852 kunmap(page);
2853 page_cache_release(page);
2857 int __page_symlink(struct inode *inode, const char *symname, int len,
2858 gfp_t gfp_mask)
2860 struct address_space *mapping = inode->i_mapping;
2861 struct page *page;
2862 void *fsdata;
2863 int err;
2864 char *kaddr;
2866 retry:
2867 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2868 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
2869 if (err)
2870 goto fail;
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,
2877 page, fsdata);
2878 if (err < 0)
2879 goto fail;
2880 if (err < len-1)
2881 goto retry;
2883 mark_inode_dirty(inode);
2884 return 0;
2885 fail:
2886 return err;
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);