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/dnotify.h>
25 #include <linux/smp_lock.h>
26 #include <linux/personality.h>
27 #include <linux/security.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <asm/namei.h>
32 #include <asm/uaccess.h>
34 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
36 /* [Feb-1997 T. Schoebel-Theuer]
37 * Fundamental changes in the pathname lookup mechanisms (namei)
38 * were necessary because of omirr. The reason is that omirr needs
39 * to know the _real_ pathname, not the user-supplied one, in case
40 * of symlinks (and also when transname replacements occur).
42 * The new code replaces the old recursive symlink resolution with
43 * an iterative one (in case of non-nested symlink chains). It does
44 * this with calls to <fs>_follow_link().
45 * As a side effect, dir_namei(), _namei() and follow_link() are now
46 * replaced with a single function lookup_dentry() that can handle all
47 * the special cases of the former code.
49 * With the new dcache, the pathname is stored at each inode, at least as
50 * long as the refcount of the inode is positive. As a side effect, the
51 * size of the dcache depends on the inode cache and thus is dynamic.
53 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
54 * resolution to correspond with current state of the code.
56 * Note that the symlink resolution is not *completely* iterative.
57 * There is still a significant amount of tail- and mid- recursion in
58 * the algorithm. Also, note that <fs>_readlink() is not used in
59 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
60 * may return different results than <fs>_follow_link(). Many virtual
61 * filesystems (including /proc) exhibit this behavior.
64 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
65 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
66 * and the name already exists in form of a symlink, try to create the new
67 * name indicated by the symlink. The old code always complained that the
68 * name already exists, due to not following the symlink even if its target
69 * is nonexistent. The new semantics affects also mknod() and link() when
70 * the name is a symlink pointing to a non-existant name.
72 * I don't know which semantics is the right one, since I have no access
73 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
74 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
75 * "old" one. Personally, I think the new semantics is much more logical.
76 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
77 * file does succeed in both HP-UX and SunOs, but not in Solaris
78 * and in the old Linux semantics.
81 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
82 * semantics. See the comments in "open_namei" and "do_link" below.
84 * [10-Sep-98 Alan Modra] Another symlink change.
87 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
88 * inside the path - always follow.
89 * in the last component in creation/removal/renaming - never follow.
90 * if LOOKUP_FOLLOW passed - follow.
91 * if the pathname has trailing slashes - follow.
92 * otherwise - don't follow.
93 * (applied in that order).
95 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
96 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
97 * During the 2.4 we need to fix the userland stuff depending on it -
98 * hopefully we will be able to get rid of that wart in 2.5. So far only
99 * XEmacs seems to be relying on it...
102 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
103 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
104 * any extra contention...
107 /* In order to reduce some races, while at the same time doing additional
108 * checking and hopefully speeding things up, we copy filenames to the
109 * kernel data space before using them..
111 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
112 * PATH_MAX includes the nul terminator --RR.
114 static inline int do_getname(const char __user
*filename
, char *page
)
117 unsigned long len
= PATH_MAX
;
119 if ((unsigned long) filename
>= TASK_SIZE
) {
120 if (!segment_eq(get_fs(), KERNEL_DS
))
122 } else if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
123 len
= TASK_SIZE
- (unsigned long) filename
;
125 retval
= strncpy_from_user((char *)page
, filename
, len
);
129 return -ENAMETOOLONG
;
135 char * getname(const char __user
* filename
)
139 result
= ERR_PTR(-ENOMEM
);
142 int retval
= do_getname(filename
, tmp
);
147 result
= ERR_PTR(retval
);
150 if (unlikely(current
->audit_context
) && !IS_ERR(result
) && result
)
151 audit_getname(result
);
156 * generic_permission - check for access rights on a Posix-like filesystem
157 * @inode: inode to check access rights for
158 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
159 * @check_acl: optional callback to check for Posix ACLs
161 * Used to check for read/write/execute permissions on a file.
162 * We use "fsuid" for this, letting us set arbitrary permissions
163 * for filesystem access without changing the "normal" uids which
164 * are used for other things..
166 int generic_permission(struct inode
*inode
, int mask
,
167 int (*check_acl
)(struct inode
*inode
, int mask
))
169 umode_t mode
= inode
->i_mode
;
171 if (mask
& MAY_WRITE
) {
173 * Nobody gets write access to a read-only fs.
175 if (IS_RDONLY(inode
) &&
176 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
180 * Nobody gets write access to an immutable file.
182 if (IS_IMMUTABLE(inode
))
186 if (current
->fsuid
== inode
->i_uid
)
189 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
) && check_acl
) {
190 int error
= check_acl(inode
, mask
);
191 if (error
== -EACCES
)
192 goto check_capabilities
;
193 else if (error
!= -EAGAIN
)
197 if (in_group_p(inode
->i_gid
))
202 * If the DACs are ok we don't need any capability check.
204 if (((mode
& mask
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == mask
))
209 * Read/write DACs are always overridable.
210 * Executable DACs are overridable if at least one exec bit is set.
212 if (!(mask
& MAY_EXEC
) ||
213 (inode
->i_mode
& S_IXUGO
) || S_ISDIR(inode
->i_mode
))
214 if (capable(CAP_DAC_OVERRIDE
))
218 * Searching includes executable on directories, else just read.
220 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
221 if (capable(CAP_DAC_READ_SEARCH
))
227 int permission(struct inode
* inode
,int mask
, struct nameidata
*nd
)
232 /* Ordinary permission routines do not understand MAY_APPEND. */
233 submask
= mask
& ~MAY_APPEND
;
235 if (inode
->i_op
&& inode
->i_op
->permission
)
236 retval
= inode
->i_op
->permission(inode
, submask
, nd
);
238 retval
= generic_permission(inode
, submask
, NULL
);
242 return security_inode_permission(inode
, mask
, nd
);
246 * get_write_access() gets write permission for a file.
247 * put_write_access() releases this write permission.
248 * This is used for regular files.
249 * We cannot support write (and maybe mmap read-write shared) accesses and
250 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
251 * can have the following values:
252 * 0: no writers, no VM_DENYWRITE mappings
253 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
254 * > 0: (i_writecount) users are writing to the file.
256 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
257 * except for the cases where we don't hold i_writecount yet. Then we need to
258 * use {get,deny}_write_access() - these functions check the sign and refuse
259 * to do the change if sign is wrong. Exclusion between them is provided by
260 * the inode->i_lock spinlock.
263 int get_write_access(struct inode
* inode
)
265 spin_lock(&inode
->i_lock
);
266 if (atomic_read(&inode
->i_writecount
) < 0) {
267 spin_unlock(&inode
->i_lock
);
270 atomic_inc(&inode
->i_writecount
);
271 spin_unlock(&inode
->i_lock
);
276 int deny_write_access(struct file
* file
)
278 struct inode
*inode
= file
->f_dentry
->d_inode
;
280 spin_lock(&inode
->i_lock
);
281 if (atomic_read(&inode
->i_writecount
) > 0) {
282 spin_unlock(&inode
->i_lock
);
285 atomic_dec(&inode
->i_writecount
);
286 spin_unlock(&inode
->i_lock
);
291 void path_release(struct nameidata
*nd
)
298 * umount() mustn't call path_release()/mntput() as that would clear
301 void path_release_on_umount(struct nameidata
*nd
)
308 * Internal lookup() using the new generic dcache.
311 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
313 struct dentry
* dentry
= __d_lookup(parent
, name
);
315 /* lockess __d_lookup may fail due to concurrent d_move()
316 * in some unrelated directory, so try with d_lookup
319 dentry
= d_lookup(parent
, name
);
321 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
322 if (!dentry
->d_op
->d_revalidate(dentry
, nd
) && !d_invalidate(dentry
)) {
331 * Short-cut version of permission(), for calling by
332 * path_walk(), when dcache lock is held. Combines parts
333 * of permission() and generic_permission(), and tests ONLY for
334 * MAY_EXEC permission.
336 * If appropriate, check DAC only. If not appropriate, or
337 * short-cut DAC fails, then call permission() to do more
338 * complete permission check.
340 static inline int exec_permission_lite(struct inode
*inode
,
341 struct nameidata
*nd
)
343 umode_t mode
= inode
->i_mode
;
345 if (inode
->i_op
&& inode
->i_op
->permission
)
348 if (current
->fsuid
== inode
->i_uid
)
350 else if (in_group_p(inode
->i_gid
))
356 if ((inode
->i_mode
& S_IXUGO
) && capable(CAP_DAC_OVERRIDE
))
359 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_OVERRIDE
))
362 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_READ_SEARCH
))
367 return security_inode_permission(inode
, MAY_EXEC
, nd
);
371 * This is called when everything else fails, and we actually have
372 * to go to the low-level filesystem to find out what we should do..
374 * We get the directory semaphore, and after getting that we also
375 * make sure that nobody added the entry to the dcache in the meantime..
378 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
380 struct dentry
* result
;
381 struct inode
*dir
= parent
->d_inode
;
385 * First re-do the cached lookup just in case it was created
386 * while we waited for the directory semaphore..
388 * FIXME! This could use version numbering or similar to
389 * avoid unnecessary cache lookups.
391 * The "dcache_lock" is purely to protect the RCU list walker
392 * from concurrent renames at this point (we mustn't get false
393 * negatives from the RCU list walk here, unlike the optimistic
396 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
398 result
= d_lookup(parent
, name
);
400 struct dentry
* dentry
= d_alloc(parent
, name
);
401 result
= ERR_PTR(-ENOMEM
);
403 result
= dir
->i_op
->lookup(dir
, dentry
, nd
);
414 * Uhhuh! Nasty case: the cache was re-populated while
415 * we waited on the semaphore. Need to revalidate.
418 if (result
->d_op
&& result
->d_op
->d_revalidate
) {
419 if (!result
->d_op
->d_revalidate(result
, nd
) && !d_invalidate(result
)) {
421 result
= ERR_PTR(-ENOENT
);
427 static int __emul_lookup_dentry(const char *, struct nameidata
*);
431 walk_init_root(const char *name
, struct nameidata
*nd
)
433 read_lock(¤t
->fs
->lock
);
434 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
435 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
436 nd
->dentry
= dget(current
->fs
->altroot
);
437 read_unlock(¤t
->fs
->lock
);
438 if (__emul_lookup_dentry(name
,nd
))
440 read_lock(¤t
->fs
->lock
);
442 nd
->mnt
= mntget(current
->fs
->rootmnt
);
443 nd
->dentry
= dget(current
->fs
->root
);
444 read_unlock(¤t
->fs
->lock
);
448 static inline int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
457 if (!walk_init_root(link
, nd
))
458 /* weird __emul_prefix() stuff did it */
461 res
= link_path_walk(link
, nd
);
463 if (nd
->depth
|| res
|| nd
->last_type
!=LAST_NORM
)
466 * If it is an iterative symlinks resolution in open_namei() we
467 * have to copy the last component. And all that crap because of
468 * bloody create() on broken symlinks. Furrfu...
471 if (unlikely(!name
)) {
475 strcpy(name
, nd
->last
.name
);
476 nd
->last
.name
= name
;
480 return PTR_ERR(link
);
484 * This limits recursive symlink follows to 8, while
485 * limiting consecutive symlinks to 40.
487 * Without that kind of total limit, nasty chains of consecutive
488 * symlinks can cause almost arbitrarily long lookups.
490 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
493 if (current
->link_count
>= MAX_NESTED_LINKS
)
495 if (current
->total_link_count
>= 40)
497 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
499 err
= security_inode_follow_link(dentry
, nd
);
502 current
->link_count
++;
503 current
->total_link_count
++;
505 touch_atime(nd
->mnt
, dentry
);
506 nd_set_link(nd
, NULL
);
507 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
509 char *s
= nd_get_link(nd
);
511 err
= __vfs_follow_link(nd
, s
);
512 if (dentry
->d_inode
->i_op
->put_link
)
513 dentry
->d_inode
->i_op
->put_link(dentry
, nd
);
515 current
->link_count
--;
523 int follow_up(struct vfsmount
**mnt
, struct dentry
**dentry
)
525 struct vfsmount
*parent
;
526 struct dentry
*mountpoint
;
527 spin_lock(&vfsmount_lock
);
528 parent
=(*mnt
)->mnt_parent
;
529 if (parent
== *mnt
) {
530 spin_unlock(&vfsmount_lock
);
534 mountpoint
=dget((*mnt
)->mnt_mountpoint
);
535 spin_unlock(&vfsmount_lock
);
537 *dentry
= mountpoint
;
543 /* no need for dcache_lock, as serialization is taken care in
546 static int follow_mount(struct vfsmount
**mnt
, struct dentry
**dentry
)
549 while (d_mountpoint(*dentry
)) {
550 struct vfsmount
*mounted
= lookup_mnt(*mnt
, *dentry
);
556 *dentry
= dget(mounted
->mnt_root
);
562 /* no need for dcache_lock, as serialization is taken care in
565 static inline int __follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
567 struct vfsmount
*mounted
;
569 mounted
= lookup_mnt(*mnt
, *dentry
);
574 *dentry
= dget(mounted
->mnt_root
);
580 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
582 return __follow_down(mnt
,dentry
);
585 static inline void follow_dotdot(struct vfsmount
**mnt
, struct dentry
**dentry
)
588 struct vfsmount
*parent
;
589 struct dentry
*old
= *dentry
;
591 read_lock(¤t
->fs
->lock
);
592 if (*dentry
== current
->fs
->root
&&
593 *mnt
== current
->fs
->rootmnt
) {
594 read_unlock(¤t
->fs
->lock
);
597 read_unlock(¤t
->fs
->lock
);
598 spin_lock(&dcache_lock
);
599 if (*dentry
!= (*mnt
)->mnt_root
) {
600 *dentry
= dget((*dentry
)->d_parent
);
601 spin_unlock(&dcache_lock
);
605 spin_unlock(&dcache_lock
);
606 spin_lock(&vfsmount_lock
);
607 parent
= (*mnt
)->mnt_parent
;
608 if (parent
== *mnt
) {
609 spin_unlock(&vfsmount_lock
);
613 *dentry
= dget((*mnt
)->mnt_mountpoint
);
614 spin_unlock(&vfsmount_lock
);
619 follow_mount(mnt
, dentry
);
623 struct vfsmount
*mnt
;
624 struct dentry
*dentry
;
628 * It's more convoluted than I'd like it to be, but... it's still fairly
629 * small and for now I'd prefer to have fast path as straight as possible.
630 * It _is_ time-critical.
632 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
635 struct vfsmount
*mnt
= nd
->mnt
;
636 struct dentry
*dentry
= __d_lookup(nd
->dentry
, name
);
640 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
)
641 goto need_revalidate
;
644 path
->dentry
= dentry
;
648 dentry
= real_lookup(nd
->dentry
, name
, nd
);
654 if (dentry
->d_op
->d_revalidate(dentry
, nd
))
656 if (d_invalidate(dentry
))
662 return PTR_ERR(dentry
);
668 * This is the basic name resolution function, turning a pathname
669 * into the final dentry.
671 * We expect 'base' to be positive and a directory.
673 int fastcall
link_path_walk(const char * name
, struct nameidata
*nd
)
678 unsigned int lookup_flags
= nd
->flags
;
685 inode
= nd
->dentry
->d_inode
;
687 lookup_flags
= LOOKUP_FOLLOW
;
689 /* At this point we know we have a real path component. */
695 err
= exec_permission_lite(inode
, nd
);
696 if (err
== -EAGAIN
) {
697 err
= permission(inode
, MAY_EXEC
, nd
);
703 c
= *(const unsigned char *)name
;
705 hash
= init_name_hash();
708 hash
= partial_name_hash(c
, hash
);
709 c
= *(const unsigned char *)name
;
710 } while (c
&& (c
!= '/'));
711 this.len
= name
- (const char *) this.name
;
712 this.hash
= end_name_hash(hash
);
714 /* remove trailing slashes? */
717 while (*++name
== '/');
719 goto last_with_slashes
;
722 * "." and ".." are special - ".." especially so because it has
723 * to be able to know about the current root directory and
724 * parent relationships.
726 if (this.name
[0] == '.') switch (this.len
) {
730 if (this.name
[1] != '.')
732 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
733 inode
= nd
->dentry
->d_inode
;
739 * See if the low-level filesystem might want
740 * to use its own hash..
742 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
743 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
747 nd
->flags
|= LOOKUP_CONTINUE
;
748 /* This does the actual lookups.. */
749 err
= do_lookup(nd
, &this, &next
);
752 /* Check mountpoints.. */
753 follow_mount(&next
.mnt
, &next
.dentry
);
756 inode
= next
.dentry
->d_inode
;
763 if (inode
->i_op
->follow_link
) {
765 err
= do_follow_link(next
.dentry
, nd
);
771 inode
= nd
->dentry
->d_inode
;
780 nd
->dentry
= next
.dentry
;
783 if (!inode
->i_op
->lookup
)
786 /* here ends the main loop */
789 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
791 nd
->flags
&= ~LOOKUP_CONTINUE
;
792 if (lookup_flags
& LOOKUP_PARENT
)
794 if (this.name
[0] == '.') switch (this.len
) {
798 if (this.name
[1] != '.')
800 follow_dotdot(&nd
->mnt
, &nd
->dentry
);
801 inode
= nd
->dentry
->d_inode
;
806 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
807 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
811 err
= do_lookup(nd
, &this, &next
);
814 follow_mount(&next
.mnt
, &next
.dentry
);
815 inode
= next
.dentry
->d_inode
;
816 if ((lookup_flags
& LOOKUP_FOLLOW
)
817 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
819 err
= do_follow_link(next
.dentry
, nd
);
824 inode
= nd
->dentry
->d_inode
;
828 nd
->dentry
= next
.dentry
;
833 if (lookup_flags
& LOOKUP_DIRECTORY
) {
835 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
841 nd
->last_type
= LAST_NORM
;
842 if (this.name
[0] != '.')
845 nd
->last_type
= LAST_DOT
;
846 else if (this.len
== 2 && this.name
[1] == '.')
847 nd
->last_type
= LAST_DOTDOT
;
852 * We bypassed the ordinary revalidation routines.
853 * We may need to check the cached dentry for staleness.
855 if (nd
->dentry
&& nd
->dentry
->d_sb
&&
856 (nd
->dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)) {
858 /* Note: we do not d_invalidate() */
859 if (!nd
->dentry
->d_op
->d_revalidate(nd
->dentry
, nd
))
873 int fastcall
path_walk(const char * name
, struct nameidata
*nd
)
875 current
->total_link_count
= 0;
876 return link_path_walk(name
, nd
);
880 /* returns 1 if everything is done */
881 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
883 if (path_walk(name
, nd
))
884 return 0; /* something went wrong... */
886 if (!nd
->dentry
->d_inode
|| S_ISDIR(nd
->dentry
->d_inode
->i_mode
)) {
887 struct dentry
*old_dentry
= nd
->dentry
;
888 struct vfsmount
*old_mnt
= nd
->mnt
;
889 struct qstr last
= nd
->last
;
890 int last_type
= nd
->last_type
;
892 * NAME was not found in alternate root or it's a directory. Try to find
893 * it in the normal root:
895 nd
->last_type
= LAST_ROOT
;
896 read_lock(¤t
->fs
->lock
);
897 nd
->mnt
= mntget(current
->fs
->rootmnt
);
898 nd
->dentry
= dget(current
->fs
->root
);
899 read_unlock(¤t
->fs
->lock
);
900 if (path_walk(name
, nd
) == 0) {
901 if (nd
->dentry
->d_inode
) {
908 nd
->dentry
= old_dentry
;
911 nd
->last_type
= last_type
;
916 void set_fs_altroot(void)
918 char *emul
= __emul_prefix();
920 struct vfsmount
*mnt
= NULL
, *oldmnt
;
921 struct dentry
*dentry
= NULL
, *olddentry
;
926 err
= path_lookup(emul
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_NOALT
, &nd
);
932 write_lock(¤t
->fs
->lock
);
933 oldmnt
= current
->fs
->altrootmnt
;
934 olddentry
= current
->fs
->altroot
;
935 current
->fs
->altrootmnt
= mnt
;
936 current
->fs
->altroot
= dentry
;
937 write_unlock(¤t
->fs
->lock
);
944 int fastcall
path_lookup(const char *name
, unsigned int flags
, struct nameidata
*nd
)
948 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
952 read_lock(¤t
->fs
->lock
);
954 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
955 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
956 nd
->dentry
= dget(current
->fs
->altroot
);
957 read_unlock(¤t
->fs
->lock
);
958 if (__emul_lookup_dentry(name
,nd
))
960 read_lock(¤t
->fs
->lock
);
962 nd
->mnt
= mntget(current
->fs
->rootmnt
);
963 nd
->dentry
= dget(current
->fs
->root
);
965 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
966 nd
->dentry
= dget(current
->fs
->pwd
);
968 read_unlock(¤t
->fs
->lock
);
969 current
->total_link_count
= 0;
970 retval
= link_path_walk(name
, nd
);
971 if (unlikely(current
->audit_context
972 && nd
&& nd
->dentry
&& nd
->dentry
->d_inode
))
974 nd
->dentry
->d_inode
->i_ino
,
975 nd
->dentry
->d_inode
->i_rdev
);
980 * Restricted form of lookup. Doesn't follow links, single-component only,
981 * needs parent already locked. Doesn't follow mounts.
984 static struct dentry
* __lookup_hash(struct qstr
*name
, struct dentry
* base
, struct nameidata
*nd
)
986 struct dentry
* dentry
;
990 inode
= base
->d_inode
;
991 err
= permission(inode
, MAY_EXEC
, nd
);
992 dentry
= ERR_PTR(err
);
997 * See if the low-level filesystem might want
998 * to use its own hash..
1000 if (base
->d_op
&& base
->d_op
->d_hash
) {
1001 err
= base
->d_op
->d_hash(base
, name
);
1002 dentry
= ERR_PTR(err
);
1007 dentry
= cached_lookup(base
, name
, nd
);
1009 struct dentry
*new = d_alloc(base
, name
);
1010 dentry
= ERR_PTR(-ENOMEM
);
1013 dentry
= inode
->i_op
->lookup(inode
, new, nd
);
1023 struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
1025 return __lookup_hash(name
, base
, NULL
);
1029 struct dentry
* lookup_one_len(const char * name
, struct dentry
* base
, int len
)
1040 hash
= init_name_hash();
1042 c
= *(const unsigned char *)name
++;
1043 if (c
== '/' || c
== '\0')
1045 hash
= partial_name_hash(c
, hash
);
1047 this.hash
= end_name_hash(hash
);
1049 return lookup_hash(&this, base
);
1051 return ERR_PTR(-EACCES
);
1057 * is used by most simple commands to get the inode of a specified name.
1058 * Open, link etc use their own routines, but this is enough for things
1061 * namei exists in two versions: namei/lnamei. The only difference is
1062 * that namei follows links, while lnamei does not.
1065 int fastcall
__user_walk(const char __user
*name
, unsigned flags
, struct nameidata
*nd
)
1067 char *tmp
= getname(name
);
1068 int err
= PTR_ERR(tmp
);
1071 err
= path_lookup(tmp
, flags
, nd
);
1078 * It's inline, so penalty for filesystems that don't use sticky bit is
1081 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1083 if (!(dir
->i_mode
& S_ISVTX
))
1085 if (inode
->i_uid
== current
->fsuid
)
1087 if (dir
->i_uid
== current
->fsuid
)
1089 return !capable(CAP_FOWNER
);
1093 * Check whether we can remove a link victim from directory dir, check
1094 * whether the type of victim is right.
1095 * 1. We can't do it if dir is read-only (done in permission())
1096 * 2. We should have write and exec permissions on dir
1097 * 3. We can't remove anything from append-only dir
1098 * 4. We can't do anything with immutable dir (done in permission())
1099 * 5. If the sticky bit on dir is set we should either
1100 * a. be owner of dir, or
1101 * b. be owner of victim, or
1102 * c. have CAP_FOWNER capability
1103 * 6. If the victim is append-only or immutable we can't do antyhing with
1104 * links pointing to it.
1105 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1106 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1107 * 9. We can't remove a root or mountpoint.
1108 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1109 * nfs_async_unlink().
1111 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1115 if (!victim
->d_inode
)
1118 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1120 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
, NULL
);
1125 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1126 IS_IMMUTABLE(victim
->d_inode
))
1129 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1131 if (IS_ROOT(victim
))
1133 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1135 if (IS_DEADDIR(dir
))
1137 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1142 /* Check whether we can create an object with dentry child in directory
1144 * 1. We can't do it if child already exists (open has special treatment for
1145 * this case, but since we are inlined it's OK)
1146 * 2. We can't do it if dir is read-only (done in permission())
1147 * 3. We should have write and exec permissions on dir
1148 * 4. We can't do it if dir is immutable (done in permission())
1150 static inline int may_create(struct inode
*dir
, struct dentry
*child
,
1151 struct nameidata
*nd
)
1155 if (IS_DEADDIR(dir
))
1157 return permission(dir
,MAY_WRITE
| MAY_EXEC
, nd
);
1161 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1164 * O_DIRECTORY translates into forcing a directory lookup.
1166 static inline int lookup_flags(unsigned int f
)
1168 unsigned long retval
= LOOKUP_FOLLOW
;
1171 retval
&= ~LOOKUP_FOLLOW
;
1173 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
1174 retval
&= ~LOOKUP_FOLLOW
;
1176 if (f
& O_DIRECTORY
)
1177 retval
|= LOOKUP_DIRECTORY
;
1183 * p1 and p2 should be directories on the same fs.
1185 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1190 down(&p1
->d_inode
->i_sem
);
1194 down(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1196 for (p
= p1
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1197 if (p
->d_parent
== p2
) {
1198 down(&p2
->d_inode
->i_sem
);
1199 down(&p1
->d_inode
->i_sem
);
1204 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1205 if (p
->d_parent
== p1
) {
1206 down(&p1
->d_inode
->i_sem
);
1207 down(&p2
->d_inode
->i_sem
);
1212 down(&p1
->d_inode
->i_sem
);
1213 down(&p2
->d_inode
->i_sem
);
1217 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1219 up(&p1
->d_inode
->i_sem
);
1221 up(&p2
->d_inode
->i_sem
);
1222 up(&p1
->d_inode
->i_sb
->s_vfs_rename_sem
);
1226 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1227 struct nameidata
*nd
)
1229 int error
= may_create(dir
, dentry
, nd
);
1234 if (!dir
->i_op
|| !dir
->i_op
->create
)
1235 return -EACCES
; /* shouldn't it be ENOSYS? */
1238 error
= security_inode_create(dir
, dentry
, mode
);
1242 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1244 inode_dir_notify(dir
, DN_CREATE
);
1245 security_inode_post_create(dir
, dentry
, mode
);
1250 int may_open(struct nameidata
*nd
, int acc_mode
, int flag
)
1252 struct dentry
*dentry
= nd
->dentry
;
1253 struct inode
*inode
= dentry
->d_inode
;
1259 if (S_ISLNK(inode
->i_mode
))
1262 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
1265 error
= permission(inode
, acc_mode
, nd
);
1270 * FIFO's, sockets and device files are special: they don't
1271 * actually live on the filesystem itself, and as such you
1272 * can write to them even if the filesystem is read-only.
1274 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
1276 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
1277 if (nd
->mnt
->mnt_flags
& MNT_NODEV
)
1281 } else if (IS_RDONLY(inode
) && (flag
& FMODE_WRITE
))
1284 * An append-only file must be opened in append mode for writing.
1286 if (IS_APPEND(inode
)) {
1287 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
1293 /* O_NOATIME can only be set by the owner or superuser */
1294 if (flag
& O_NOATIME
)
1295 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_FOWNER
))
1299 * Ensure there are no outstanding leases on the file.
1301 error
= break_lease(inode
, flag
);
1305 if (flag
& O_TRUNC
) {
1306 error
= get_write_access(inode
);
1311 * Refuse to truncate files with mandatory locks held on them.
1313 error
= locks_verify_locked(inode
);
1317 error
= do_truncate(dentry
, 0);
1319 put_write_access(inode
);
1323 if (flag
& FMODE_WRITE
)
1332 * namei for open - this is in fact almost the whole open-routine.
1334 * Note that the low bits of "flag" aren't the same as in the open
1335 * system call - they are 00 - no permissions needed
1336 * 01 - read permission needed
1337 * 10 - write permission needed
1338 * 11 - read/write permissions needed
1339 * which is a lot more logical, and also allows the "no perm" needed
1340 * for symlinks (where the permissions are checked later).
1343 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
1345 int acc_mode
, error
= 0;
1346 struct dentry
*dentry
;
1350 acc_mode
= ACC_MODE(flag
);
1352 /* Allow the LSM permission hook to distinguish append
1353 access from general write access. */
1354 if (flag
& O_APPEND
)
1355 acc_mode
|= MAY_APPEND
;
1357 /* Fill in the open() intent data */
1358 nd
->intent
.open
.flags
= flag
;
1359 nd
->intent
.open
.create_mode
= mode
;
1362 * The simplest case - just a plain lookup.
1364 if (!(flag
& O_CREAT
)) {
1365 error
= path_lookup(pathname
, lookup_flags(flag
)|LOOKUP_OPEN
, nd
);
1372 * Create - we need to know the parent.
1374 error
= path_lookup(pathname
, LOOKUP_PARENT
|LOOKUP_OPEN
|LOOKUP_CREATE
, nd
);
1379 * We have the parent and last component. First of all, check
1380 * that we are not asked to creat(2) an obvious directory - that
1384 if (nd
->last_type
!= LAST_NORM
|| nd
->last
.name
[nd
->last
.len
])
1388 nd
->flags
&= ~LOOKUP_PARENT
;
1389 down(&dir
->d_inode
->i_sem
);
1390 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1393 error
= PTR_ERR(dentry
);
1394 if (IS_ERR(dentry
)) {
1395 up(&dir
->d_inode
->i_sem
);
1399 /* Negative dentry, just create the file */
1400 if (!dentry
->d_inode
) {
1401 if (!IS_POSIXACL(dir
->d_inode
))
1402 mode
&= ~current
->fs
->umask
;
1403 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
1404 up(&dir
->d_inode
->i_sem
);
1406 nd
->dentry
= dentry
;
1409 /* Don't check for write permission, don't truncate */
1416 * It already exists.
1418 up(&dir
->d_inode
->i_sem
);
1424 if (d_mountpoint(dentry
)) {
1426 if (flag
& O_NOFOLLOW
)
1428 while (__follow_down(&nd
->mnt
,&dentry
) && d_mountpoint(dentry
));
1431 if (!dentry
->d_inode
)
1433 if (dentry
->d_inode
->i_op
&& dentry
->d_inode
->i_op
->follow_link
)
1437 nd
->dentry
= dentry
;
1439 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
1442 error
= may_open(nd
, acc_mode
, flag
);
1455 if (flag
& O_NOFOLLOW
)
1458 * This is subtle. Instead of calling do_follow_link() we do the
1459 * thing by hands. The reason is that this way we have zero link_count
1460 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1461 * After that we have the parent and last component, i.e.
1462 * we are in the same situation as after the first path_walk().
1463 * Well, almost - if the last component is normal we get its copy
1464 * stored in nd->last.name and we will have to putname() it when we
1465 * are done. Procfs-like symlinks just set LAST_BIND.
1467 nd
->flags
|= LOOKUP_PARENT
;
1468 error
= security_inode_follow_link(dentry
, nd
);
1471 touch_atime(nd
->mnt
, dentry
);
1472 nd_set_link(nd
, NULL
);
1473 error
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
1475 char *s
= nd_get_link(nd
);
1477 error
= __vfs_follow_link(nd
, s
);
1478 if (dentry
->d_inode
->i_op
->put_link
)
1479 dentry
->d_inode
->i_op
->put_link(dentry
, nd
);
1484 nd
->flags
&= ~LOOKUP_PARENT
;
1485 if (nd
->last_type
== LAST_BIND
) {
1486 dentry
= nd
->dentry
;
1490 if (nd
->last_type
!= LAST_NORM
)
1492 if (nd
->last
.name
[nd
->last
.len
]) {
1493 putname(nd
->last
.name
);
1498 putname(nd
->last
.name
);
1502 down(&dir
->d_inode
->i_sem
);
1503 dentry
= __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
1504 putname(nd
->last
.name
);
1509 * lookup_create - lookup a dentry, creating it if it doesn't exist
1510 * @nd: nameidata info
1511 * @is_dir: directory flag
1513 * Simple function to lookup and return a dentry and create it
1514 * if it doesn't exist. Is SMP-safe.
1516 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
1518 struct dentry
*dentry
;
1520 down(&nd
->dentry
->d_inode
->i_sem
);
1521 dentry
= ERR_PTR(-EEXIST
);
1522 if (nd
->last_type
!= LAST_NORM
)
1524 nd
->flags
&= ~LOOKUP_PARENT
;
1525 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1528 if (!is_dir
&& nd
->last
.name
[nd
->last
.len
] && !dentry
->d_inode
)
1533 dentry
= ERR_PTR(-ENOENT
);
1538 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
1540 int error
= may_create(dir
, dentry
, NULL
);
1545 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
1548 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
1551 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
1556 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1558 inode_dir_notify(dir
, DN_CREATE
);
1559 security_inode_post_mknod(dir
, dentry
, mode
, dev
);
1564 asmlinkage
long sys_mknod(const char __user
* filename
, int mode
, unsigned dev
)
1568 struct dentry
* dentry
;
1569 struct nameidata nd
;
1573 tmp
= getname(filename
);
1575 return PTR_ERR(tmp
);
1577 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1580 dentry
= lookup_create(&nd
, 0);
1581 error
= PTR_ERR(dentry
);
1583 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1584 mode
&= ~current
->fs
->umask
;
1585 if (!IS_ERR(dentry
)) {
1586 switch (mode
& S_IFMT
) {
1587 case 0: case S_IFREG
:
1588 error
= vfs_create(nd
.dentry
->d_inode
,dentry
,mode
,&nd
);
1590 case S_IFCHR
: case S_IFBLK
:
1591 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,
1592 new_decode_dev(dev
));
1594 case S_IFIFO
: case S_IFSOCK
:
1595 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,0);
1605 up(&nd
.dentry
->d_inode
->i_sem
);
1613 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1615 int error
= may_create(dir
, dentry
, NULL
);
1620 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1623 mode
&= (S_IRWXUGO
|S_ISVTX
);
1624 error
= security_inode_mkdir(dir
, dentry
, mode
);
1629 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1631 inode_dir_notify(dir
, DN_CREATE
);
1632 security_inode_post_mkdir(dir
,dentry
, mode
);
1637 asmlinkage
long sys_mkdir(const char __user
* pathname
, int mode
)
1642 tmp
= getname(pathname
);
1643 error
= PTR_ERR(tmp
);
1645 struct dentry
*dentry
;
1646 struct nameidata nd
;
1648 error
= path_lookup(tmp
, LOOKUP_PARENT
, &nd
);
1651 dentry
= lookup_create(&nd
, 1);
1652 error
= PTR_ERR(dentry
);
1653 if (!IS_ERR(dentry
)) {
1654 if (!IS_POSIXACL(nd
.dentry
->d_inode
))
1655 mode
&= ~current
->fs
->umask
;
1656 error
= vfs_mkdir(nd
.dentry
->d_inode
, dentry
, mode
);
1659 up(&nd
.dentry
->d_inode
->i_sem
);
1669 * We try to drop the dentry early: we should have
1670 * a usage count of 2 if we're the only user of this
1671 * dentry, and if that is true (possibly after pruning
1672 * the dcache), then we drop the dentry now.
1674 * A low-level filesystem can, if it choses, legally
1677 * if (!d_unhashed(dentry))
1680 * if it cannot handle the case of removing a directory
1681 * that is still in use by something else..
1683 void dentry_unhash(struct dentry
*dentry
)
1686 spin_lock(&dcache_lock
);
1687 switch (atomic_read(&dentry
->d_count
)) {
1689 spin_unlock(&dcache_lock
);
1690 shrink_dcache_parent(dentry
);
1691 spin_lock(&dcache_lock
);
1692 if (atomic_read(&dentry
->d_count
) != 2)
1697 spin_unlock(&dcache_lock
);
1700 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1702 int error
= may_delete(dir
, dentry
, 1);
1707 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1712 down(&dentry
->d_inode
->i_sem
);
1713 dentry_unhash(dentry
);
1714 if (d_mountpoint(dentry
))
1717 error
= security_inode_rmdir(dir
, dentry
);
1719 error
= dir
->i_op
->rmdir(dir
, dentry
);
1721 dentry
->d_inode
->i_flags
|= S_DEAD
;
1724 up(&dentry
->d_inode
->i_sem
);
1726 inode_dir_notify(dir
, DN_DELETE
);
1734 asmlinkage
long sys_rmdir(const char __user
* pathname
)
1738 struct dentry
*dentry
;
1739 struct nameidata nd
;
1741 name
= getname(pathname
);
1743 return PTR_ERR(name
);
1745 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1749 switch(nd
.last_type
) {
1760 down(&nd
.dentry
->d_inode
->i_sem
);
1761 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1762 error
= PTR_ERR(dentry
);
1763 if (!IS_ERR(dentry
)) {
1764 error
= vfs_rmdir(nd
.dentry
->d_inode
, dentry
);
1767 up(&nd
.dentry
->d_inode
->i_sem
);
1775 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1777 int error
= may_delete(dir
, dentry
, 0);
1782 if (!dir
->i_op
|| !dir
->i_op
->unlink
)
1787 down(&dentry
->d_inode
->i_sem
);
1788 if (d_mountpoint(dentry
))
1791 error
= security_inode_unlink(dir
, dentry
);
1793 error
= dir
->i_op
->unlink(dir
, dentry
);
1795 up(&dentry
->d_inode
->i_sem
);
1797 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1798 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
1800 inode_dir_notify(dir
, DN_DELETE
);
1806 * Make sure that the actual truncation of the file will occur outside its
1807 * directory's i_sem. Truncate can take a long time if there is a lot of
1808 * writeout happening, and we don't want to prevent access to the directory
1809 * while waiting on the I/O.
1811 asmlinkage
long sys_unlink(const char __user
* pathname
)
1815 struct dentry
*dentry
;
1816 struct nameidata nd
;
1817 struct inode
*inode
= NULL
;
1819 name
= getname(pathname
);
1821 return PTR_ERR(name
);
1823 error
= path_lookup(name
, LOOKUP_PARENT
, &nd
);
1827 if (nd
.last_type
!= LAST_NORM
)
1829 down(&nd
.dentry
->d_inode
->i_sem
);
1830 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1831 error
= PTR_ERR(dentry
);
1832 if (!IS_ERR(dentry
)) {
1833 /* Why not before? Because we want correct error value */
1834 if (nd
.last
.name
[nd
.last
.len
])
1836 inode
= dentry
->d_inode
;
1838 atomic_inc(&inode
->i_count
);
1839 error
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
1843 up(&nd
.dentry
->d_inode
->i_sem
);
1845 iput(inode
); /* truncate the inode here */
1853 error
= !dentry
->d_inode
? -ENOENT
:
1854 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
1858 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
, int mode
)
1860 int error
= may_create(dir
, dentry
, NULL
);
1865 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1868 error
= security_inode_symlink(dir
, dentry
, oldname
);
1873 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1875 inode_dir_notify(dir
, DN_CREATE
);
1876 security_inode_post_symlink(dir
, dentry
, oldname
);
1881 asmlinkage
long sys_symlink(const char __user
* oldname
, const char __user
* newname
)
1887 from
= getname(oldname
);
1889 return PTR_ERR(from
);
1890 to
= getname(newname
);
1891 error
= PTR_ERR(to
);
1893 struct dentry
*dentry
;
1894 struct nameidata nd
;
1896 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1899 dentry
= lookup_create(&nd
, 0);
1900 error
= PTR_ERR(dentry
);
1901 if (!IS_ERR(dentry
)) {
1902 error
= vfs_symlink(nd
.dentry
->d_inode
, dentry
, from
, S_IALLUGO
);
1905 up(&nd
.dentry
->d_inode
->i_sem
);
1914 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1916 struct inode
*inode
= old_dentry
->d_inode
;
1922 error
= may_create(dir
, new_dentry
, NULL
);
1926 if (dir
->i_sb
!= inode
->i_sb
)
1930 * A link to an append-only or immutable file cannot be created.
1932 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1934 if (!dir
->i_op
|| !dir
->i_op
->link
)
1936 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1939 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
1943 down(&old_dentry
->d_inode
->i_sem
);
1945 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1946 up(&old_dentry
->d_inode
->i_sem
);
1948 inode_dir_notify(dir
, DN_CREATE
);
1949 security_inode_post_link(old_dentry
, dir
, new_dentry
);
1955 * Hardlinks are often used in delicate situations. We avoid
1956 * security-related surprises by not following symlinks on the
1959 * We don't follow them on the oldname either to be compatible
1960 * with linux 2.0, and to avoid hard-linking to directories
1961 * and other special files. --ADM
1963 asmlinkage
long sys_link(const char __user
* oldname
, const char __user
* newname
)
1965 struct dentry
*new_dentry
;
1966 struct nameidata nd
, old_nd
;
1970 to
= getname(newname
);
1974 error
= __user_walk(oldname
, 0, &old_nd
);
1977 error
= path_lookup(to
, LOOKUP_PARENT
, &nd
);
1981 if (old_nd
.mnt
!= nd
.mnt
)
1983 new_dentry
= lookup_create(&nd
, 0);
1984 error
= PTR_ERR(new_dentry
);
1985 if (!IS_ERR(new_dentry
)) {
1986 error
= vfs_link(old_nd
.dentry
, nd
.dentry
->d_inode
, new_dentry
);
1989 up(&nd
.dentry
->d_inode
->i_sem
);
1993 path_release(&old_nd
);
2001 * The worst of all namespace operations - renaming directory. "Perverted"
2002 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2004 * a) we can get into loop creation. Check is done in is_subdir().
2005 * b) race potential - two innocent renames can create a loop together.
2006 * That's where 4.4 screws up. Current fix: serialization on
2007 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2009 * c) we have to lock _three_ objects - parents and victim (if it exists).
2010 * And that - after we got ->i_sem on parents (until then we don't know
2011 * whether the target exists). Solution: try to be smart with locking
2012 * order for inodes. We rely on the fact that tree topology may change
2013 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2014 * move will be locked. Thus we can rank directories by the tree
2015 * (ancestors first) and rank all non-directories after them.
2016 * That works since everybody except rename does "lock parent, lookup,
2017 * lock child" and rename is under ->s_vfs_rename_sem.
2018 * HOWEVER, it relies on the assumption that any object with ->lookup()
2019 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2020 * we'd better make sure that there's no link(2) for them.
2021 * d) some filesystems don't support opened-but-unlinked directories,
2022 * either because of layout or because they are not ready to deal with
2023 * all cases correctly. The latter will be fixed (taking this sort of
2024 * stuff into VFS), but the former is not going away. Solution: the same
2025 * trick as in rmdir().
2026 * e) conversion from fhandle to dentry may come in the wrong moment - when
2027 * we are removing the target. Solution: we will have to grab ->i_sem
2028 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2029 * ->i_sem on parents, which works but leads to some truely excessive
2032 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
2033 struct inode
*new_dir
, struct dentry
*new_dentry
)
2036 struct inode
*target
;
2039 * If we are going to change the parent - check write permissions,
2040 * we'll need to flip '..'.
2042 if (new_dir
!= old_dir
) {
2043 error
= permission(old_dentry
->d_inode
, MAY_WRITE
, NULL
);
2048 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2052 target
= new_dentry
->d_inode
;
2054 down(&target
->i_sem
);
2055 dentry_unhash(new_dentry
);
2057 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2060 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2063 target
->i_flags
|= S_DEAD
;
2065 if (d_unhashed(new_dentry
))
2066 d_rehash(new_dentry
);
2070 d_move(old_dentry
,new_dentry
);
2071 security_inode_post_rename(old_dir
, old_dentry
,
2072 new_dir
, new_dentry
);
2077 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
2078 struct inode
*new_dir
, struct dentry
*new_dentry
)
2080 struct inode
*target
;
2083 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2088 target
= new_dentry
->d_inode
;
2090 down(&target
->i_sem
);
2091 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2094 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2096 /* The following d_move() should become unconditional */
2097 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_ODD_RENAME
))
2098 d_move(old_dentry
, new_dentry
);
2099 security_inode_post_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2107 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2108 struct inode
*new_dir
, struct dentry
*new_dentry
)
2111 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
2113 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
2116 error
= may_delete(old_dir
, old_dentry
, is_dir
);
2120 if (!new_dentry
->d_inode
)
2121 error
= may_create(new_dir
, new_dentry
, NULL
);
2123 error
= may_delete(new_dir
, new_dentry
, is_dir
);
2127 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
2130 DQUOT_INIT(old_dir
);
2131 DQUOT_INIT(new_dir
);
2134 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
2136 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
2138 if (old_dir
== new_dir
)
2139 inode_dir_notify(old_dir
, DN_RENAME
);
2141 inode_dir_notify(old_dir
, DN_DELETE
);
2142 inode_dir_notify(new_dir
, DN_CREATE
);
2148 static inline int do_rename(const char * oldname
, const char * newname
)
2151 struct dentry
* old_dir
, * new_dir
;
2152 struct dentry
* old_dentry
, *new_dentry
;
2153 struct dentry
* trap
;
2154 struct nameidata oldnd
, newnd
;
2156 error
= path_lookup(oldname
, LOOKUP_PARENT
, &oldnd
);
2160 error
= path_lookup(newname
, LOOKUP_PARENT
, &newnd
);
2165 if (oldnd
.mnt
!= newnd
.mnt
)
2168 old_dir
= oldnd
.dentry
;
2170 if (oldnd
.last_type
!= LAST_NORM
)
2173 new_dir
= newnd
.dentry
;
2174 if (newnd
.last_type
!= LAST_NORM
)
2177 trap
= lock_rename(new_dir
, old_dir
);
2179 old_dentry
= lookup_hash(&oldnd
.last
, old_dir
);
2180 error
= PTR_ERR(old_dentry
);
2181 if (IS_ERR(old_dentry
))
2183 /* source must exist */
2185 if (!old_dentry
->d_inode
)
2187 /* unless the source is a directory trailing slashes give -ENOTDIR */
2188 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
2190 if (oldnd
.last
.name
[oldnd
.last
.len
])
2192 if (newnd
.last
.name
[newnd
.last
.len
])
2195 /* source should not be ancestor of target */
2197 if (old_dentry
== trap
)
2199 new_dentry
= lookup_hash(&newnd
.last
, new_dir
);
2200 error
= PTR_ERR(new_dentry
);
2201 if (IS_ERR(new_dentry
))
2203 /* target should not be an ancestor of source */
2205 if (new_dentry
== trap
)
2208 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
2209 new_dir
->d_inode
, new_dentry
);
2215 unlock_rename(new_dir
, old_dir
);
2217 path_release(&newnd
);
2219 path_release(&oldnd
);
2224 asmlinkage
long sys_rename(const char __user
* oldname
, const char __user
* newname
)
2230 from
= getname(oldname
);
2232 return PTR_ERR(from
);
2233 to
= getname(newname
);
2234 error
= PTR_ERR(to
);
2236 error
= do_rename(from
,to
);
2243 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
2247 len
= PTR_ERR(link
);
2252 if (len
> (unsigned) buflen
)
2254 if (copy_to_user(buffer
, link
, len
))
2261 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2262 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2263 * using) it for any given inode is up to filesystem.
2265 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2267 struct nameidata nd
;
2270 res
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
2272 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
2273 if (dentry
->d_inode
->i_op
->put_link
)
2274 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
);
2279 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
2281 return __vfs_follow_link(nd
, link
);
2284 /* get the link contents into pagecache */
2285 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
2288 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
2289 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
2293 wait_on_page_locked(page
);
2294 if (!PageUptodate(page
))
2300 page_cache_release(page
);
2301 return ERR_PTR(-EIO
);
2307 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2309 struct page
*page
= NULL
;
2310 char *s
= page_getlink(dentry
, &page
);
2311 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
2314 page_cache_release(page
);
2319 int page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
2322 nd_set_link(nd
, page_getlink(dentry
, &page
));
2326 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
)
2328 if (!IS_ERR(nd_get_link(nd
))) {
2330 page
= find_get_page(dentry
->d_inode
->i_mapping
, 0);
2334 page_cache_release(page
);
2335 page_cache_release(page
);
2339 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2341 struct page
*page
= NULL
;
2342 char *s
= page_getlink(dentry
, &page
);
2343 int res
= __vfs_follow_link(nd
, s
);
2346 page_cache_release(page
);
2351 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
2353 struct address_space
*mapping
= inode
->i_mapping
;
2354 struct page
*page
= grab_cache_page(mapping
, 0);
2360 err
= mapping
->a_ops
->prepare_write(NULL
, page
, 0, len
-1);
2363 kaddr
= kmap_atomic(page
, KM_USER0
);
2364 memcpy(kaddr
, symname
, len
-1);
2365 kunmap_atomic(kaddr
, KM_USER0
);
2366 mapping
->a_ops
->commit_write(NULL
, page
, 0, len
-1);
2368 * Notice that we are _not_ going to block here - end of page is
2369 * unmapped, so this will only try to map the rest of page, see
2370 * that it is unmapped (typically even will not look into inode -
2371 * ->i_size will be enough for everything) and zero it out.
2372 * OTOH it's obviously correct and should make the page up-to-date.
2374 if (!PageUptodate(page
)) {
2375 err
= mapping
->a_ops
->readpage(NULL
, page
);
2376 wait_on_page_locked(page
);
2380 page_cache_release(page
);
2383 mark_inode_dirty(inode
);
2387 page_cache_release(page
);
2392 struct inode_operations page_symlink_inode_operations
= {
2393 .readlink
= generic_readlink
,
2394 .follow_link
= page_follow_link_light
,
2395 .put_link
= page_put_link
,
2398 EXPORT_SYMBOL(__user_walk
);
2399 EXPORT_SYMBOL(follow_down
);
2400 EXPORT_SYMBOL(follow_up
);
2401 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
2402 EXPORT_SYMBOL(getname
);
2403 EXPORT_SYMBOL(lock_rename
);
2404 EXPORT_SYMBOL(lookup_hash
);
2405 EXPORT_SYMBOL(lookup_one_len
);
2406 EXPORT_SYMBOL(page_follow_link
);
2407 EXPORT_SYMBOL(page_follow_link_light
);
2408 EXPORT_SYMBOL(page_put_link
);
2409 EXPORT_SYMBOL(page_readlink
);
2410 EXPORT_SYMBOL(page_symlink
);
2411 EXPORT_SYMBOL(page_symlink_inode_operations
);
2412 EXPORT_SYMBOL(path_lookup
);
2413 EXPORT_SYMBOL(path_release
);
2414 EXPORT_SYMBOL(path_walk
);
2415 EXPORT_SYMBOL(permission
);
2416 EXPORT_SYMBOL(unlock_rename
);
2417 EXPORT_SYMBOL(vfs_create
);
2418 EXPORT_SYMBOL(vfs_follow_link
);
2419 EXPORT_SYMBOL(vfs_link
);
2420 EXPORT_SYMBOL(vfs_mkdir
);
2421 EXPORT_SYMBOL(vfs_mknod
);
2422 EXPORT_SYMBOL(generic_permission
);
2423 EXPORT_SYMBOL(vfs_readlink
);
2424 EXPORT_SYMBOL(vfs_rename
);
2425 EXPORT_SYMBOL(vfs_rmdir
);
2426 EXPORT_SYMBOL(vfs_symlink
);
2427 EXPORT_SYMBOL(vfs_unlink
);
2428 EXPORT_SYMBOL(dentry_unhash
);
2429 EXPORT_SYMBOL(generic_readlink
);