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>
19 #include <linux/proc_fs.h>
20 #include <linux/smp_lock.h>
21 #include <linux/quotaops.h>
22 #include <linux/pagemap.h>
23 #include <linux/dcache.h>
25 #include <asm/uaccess.h>
26 #include <asm/unaligned.h>
27 #include <asm/semaphore.h>
29 #include <asm/pgtable.h>
31 #include <asm/namei.h>
33 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
35 /* [Feb-1997 T. Schoebel-Theuer]
36 * Fundamental changes in the pathname lookup mechanisms (namei)
37 * were necessary because of omirr. The reason is that omirr needs
38 * to know the _real_ pathname, not the user-supplied one, in case
39 * of symlinks (and also when transname replacements occur).
41 * The new code replaces the old recursive symlink resolution with
42 * an iterative one (in case of non-nested symlink chains). It does
43 * this with calls to <fs>_follow_link().
44 * As a side effect, dir_namei(), _namei() and follow_link() are now
45 * replaced with a single function lookup_dentry() that can handle all
46 * the special cases of the former code.
48 * With the new dcache, the pathname is stored at each inode, at least as
49 * long as the refcount of the inode is positive. As a side effect, the
50 * size of the dcache depends on the inode cache and thus is dynamic.
52 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
53 * resolution to correspond with current state of the code.
55 * Note that the symlink resolution is not *completely* iterative.
56 * There is still a significant amount of tail- and mid- recursion in
57 * the algorithm. Also, note that <fs>_readlink() is not used in
58 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
59 * may return different results than <fs>_follow_link(). Many virtual
60 * filesystems (including /proc) exhibit this behavior.
63 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
64 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
65 * and the name already exists in form of a symlink, try to create the new
66 * name indicated by the symlink. The old code always complained that the
67 * name already exists, due to not following the symlink even if its target
68 * is nonexistent. The new semantics affects also mknod() and link() when
69 * the name is a symlink pointing to a non-existant name.
71 * I don't know which semantics is the right one, since I have no access
72 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
73 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
74 * "old" one. Personally, I think the new semantics is much more logical.
75 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
76 * file does succeed in both HP-UX and SunOs, but not in Solaris
77 * and in the old Linux semantics.
80 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
81 * semantics. See the comments in "open_namei" and "do_link" below.
83 * [10-Sep-98 Alan Modra] Another symlink change.
86 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
87 * inside the path - always follow.
88 * in the last component in creation/removal/renaming - never follow.
89 * if LOOKUP_FOLLOW passed - follow.
90 * if the pathname has trailing slashes - follow.
91 * otherwise - don't follow.
92 * (applied in that order).
94 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
95 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
96 * During the 2.4 we need to fix the userland stuff depending on it -
97 * hopefully we will be able to get rid of that wart in 2.5. So far only
98 * XEmacs seems to be relying on it...
101 /* In order to reduce some races, while at the same time doing additional
102 * checking and hopefully speeding things up, we copy filenames to the
103 * kernel data space before using them..
105 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
107 static inline int do_getname(const char *filename
, char *page
)
110 unsigned long len
= PAGE_SIZE
;
112 if ((unsigned long) filename
>= TASK_SIZE
) {
113 if (!segment_eq(get_fs(), KERNEL_DS
))
115 } else if (TASK_SIZE
- (unsigned long) filename
< PAGE_SIZE
)
116 len
= TASK_SIZE
- (unsigned long) filename
;
118 retval
= strncpy_from_user((char *)page
, filename
, len
);
122 return -ENAMETOOLONG
;
128 char * getname(const char * filename
)
132 result
= ERR_PTR(-ENOMEM
);
135 int retval
= do_getname(filename
, tmp
);
140 result
= ERR_PTR(retval
);
149 * is used to check for read/write/execute permissions on a file.
150 * We use "fsuid" for this, letting us set arbitrary permissions
151 * for filesystem access without changing the "normal" uids which
152 * are used for other things..
154 int permission(struct inode
* inode
,int mask
)
156 int mode
= inode
->i_mode
;
158 if (inode
->i_op
&& inode
->i_op
->permission
) {
161 retval
= inode
->i_op
->permission(inode
, mask
);
166 if ((mask
& S_IWOTH
) && IS_RDONLY(inode
) &&
167 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
168 return -EROFS
; /* Nobody gets write access to a read-only fs */
170 if ((mask
& S_IWOTH
) && IS_IMMUTABLE(inode
))
171 return -EACCES
; /* Nobody gets write access to an immutable file */
173 if (current
->fsuid
== inode
->i_uid
)
175 else if (in_group_p(inode
->i_gid
))
178 if (((mode
& mask
& S_IRWXO
) == mask
) || capable(CAP_DAC_OVERRIDE
))
181 /* read and search access */
182 if ((mask
== S_IROTH
) ||
183 (S_ISDIR(mode
) && !(mask
& ~(S_IROTH
| S_IXOTH
))))
184 if (capable(CAP_DAC_READ_SEARCH
))
191 * get_write_access() gets write permission for a file.
192 * put_write_access() releases this write permission.
193 * This is used for regular files.
194 * We cannot support write (and maybe mmap read-write shared) accesses and
195 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
196 * can have the following values:
197 * 0: no writers, no VM_DENYWRITE mappings
198 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
199 * > 0: (i_writecount) users are writing to the file.
201 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
202 * except for the cases where we don't hold i_writecount yet. Then we need to
203 * use {get,deny}_write_access() - these functions check the sign and refuse
204 * to do the change if sign is wrong. Exclusion between them is provided by
205 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
206 * who will try to move it in struct inode - just leave it here.
208 static spinlock_t arbitration_lock
= SPIN_LOCK_UNLOCKED
;
209 int get_write_access(struct inode
* inode
)
211 spin_lock(&arbitration_lock
);
212 if (atomic_read(&inode
->i_writecount
) < 0) {
213 spin_unlock(&arbitration_lock
);
216 atomic_inc(&inode
->i_writecount
);
217 spin_unlock(&arbitration_lock
);
220 int deny_write_access(struct file
* file
)
222 spin_lock(&arbitration_lock
);
223 if (atomic_read(&file
->f_dentry
->d_inode
->i_writecount
) > 0) {
224 spin_unlock(&arbitration_lock
);
227 atomic_dec(&file
->f_dentry
->d_inode
->i_writecount
);
228 spin_unlock(&arbitration_lock
);
232 void path_release(struct nameidata
*nd
)
239 * Internal lookup() using the new generic dcache.
242 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
244 struct dentry
* dentry
= d_lookup(parent
, name
);
246 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
247 if (!dentry
->d_op
->d_revalidate(dentry
, flags
) && !d_invalidate(dentry
)) {
256 * This is called when everything else fails, and we actually have
257 * to go to the low-level filesystem to find out what we should do..
259 * We get the directory semaphore, and after getting that we also
260 * make sure that nobody added the entry to the dcache in the meantime..
263 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
265 struct dentry
* result
;
266 struct inode
*dir
= parent
->d_inode
;
270 * First re-do the cached lookup just in case it was created
271 * while we waited for the directory semaphore..
273 * FIXME! This could use version numbering or similar to
274 * avoid unnecessary cache lookups.
276 result
= d_lookup(parent
, name
);
278 struct dentry
* dentry
= d_alloc(parent
, name
);
279 result
= ERR_PTR(-ENOMEM
);
282 result
= dir
->i_op
->lookup(dir
, dentry
);
294 * Uhhuh! Nasty case: the cache was re-populated while
295 * we waited on the semaphore. Need to revalidate.
298 if (result
->d_op
&& result
->d_op
->d_revalidate
) {
299 if (!result
->d_op
->d_revalidate(result
, flags
) && !d_invalidate(result
)) {
301 result
= ERR_PTR(-ENOENT
);
307 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
310 if (current
->link_count
>= 8)
312 current
->link_count
++;
313 UPDATE_ATIME(dentry
->d_inode
);
314 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
315 current
->link_count
--;
322 static inline int __follow_up(struct vfsmount
**mnt
, struct dentry
**base
)
324 struct vfsmount
*parent
;
325 struct dentry
*dentry
;
326 spin_lock(&dcache_lock
);
327 parent
=(*mnt
)->mnt_parent
;
328 if (parent
== *mnt
) {
329 spin_unlock(&dcache_lock
);
333 dentry
=dget((*mnt
)->mnt_mountpoint
);
334 spin_unlock(&dcache_lock
);
342 int follow_up(struct vfsmount
**mnt
, struct dentry
**dentry
)
344 return __follow_up(mnt
, dentry
);
347 static inline int __follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
350 spin_lock(&dcache_lock
);
351 p
= (*dentry
)->d_vfsmnt
.next
;
352 while (p
!= &(*dentry
)->d_vfsmnt
) {
353 struct vfsmount
*tmp
;
354 tmp
= list_entry(p
, struct vfsmount
, mnt_clash
);
355 if (tmp
->mnt_parent
== *mnt
) {
357 spin_unlock(&dcache_lock
);
358 mntput(tmp
->mnt_parent
);
359 /* tmp holds the mountpoint, so... */
361 *dentry
= dget(tmp
->mnt_root
);
366 spin_unlock(&dcache_lock
);
370 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
372 return __follow_down(mnt
,dentry
);
375 static inline void follow_dotdot(struct nameidata
*nd
)
378 struct vfsmount
*parent
;
379 struct dentry
*dentry
;
380 read_lock(¤t
->fs
->lock
);
381 if (nd
->dentry
== current
->fs
->root
&&
382 nd
->mnt
== current
->fs
->rootmnt
) {
383 read_unlock(¤t
->fs
->lock
);
386 read_unlock(¤t
->fs
->lock
);
387 spin_lock(&dcache_lock
);
388 if (nd
->dentry
!= nd
->mnt
->mnt_root
) {
389 dentry
= dget(nd
->dentry
->d_parent
);
390 spin_unlock(&dcache_lock
);
395 parent
=nd
->mnt
->mnt_parent
;
396 if (parent
== nd
->mnt
) {
397 spin_unlock(&dcache_lock
);
401 dentry
=dget(nd
->mnt
->mnt_mountpoint
);
402 spin_unlock(&dcache_lock
);
412 * This is the basic name resolution function, turning a pathname
413 * into the final dentry.
415 * We expect 'base' to be positive and a directory.
417 int path_walk(const char * name
, struct nameidata
*nd
)
419 struct dentry
*dentry
;
422 unsigned int lookup_flags
= nd
->flags
;
429 inode
= nd
->dentry
->d_inode
;
430 if (current
->link_count
)
431 lookup_flags
= LOOKUP_FOLLOW
;
433 /* At this point we know we have a real path component. */
439 err
= permission(inode
, MAY_EXEC
);
440 dentry
= ERR_PTR(err
);
445 c
= *(const unsigned char *)name
;
447 hash
= init_name_hash();
450 hash
= partial_name_hash(c
, hash
);
451 c
= *(const unsigned char *)name
;
452 } while (c
&& (c
!= '/'));
453 this.len
= name
- (const char *) this.name
;
454 this.hash
= end_name_hash(hash
);
456 /* remove trailing slashes? */
459 while (*++name
== '/');
461 goto last_with_slashes
;
464 * "." and ".." are special - ".." especially so because it has
465 * to be able to know about the current root directory and
466 * parent relationships.
468 if (this.name
[0] == '.') switch (this.len
) {
472 if (this.name
[1] != '.')
475 inode
= nd
->dentry
->d_inode
;
481 * See if the low-level filesystem might want
482 * to use its own hash..
484 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
485 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
489 /* This does the actual lookups.. */
490 dentry
= cached_lookup(nd
->dentry
, &this, LOOKUP_CONTINUE
);
492 dentry
= real_lookup(nd
->dentry
, &this, LOOKUP_CONTINUE
);
493 err
= PTR_ERR(dentry
);
497 /* Check mountpoints.. */
498 while (d_mountpoint(dentry
) && __follow_down(&nd
->mnt
, &dentry
))
502 inode
= dentry
->d_inode
;
509 if (inode
->i_op
->follow_link
) {
510 err
= do_follow_link(dentry
, nd
);
515 inode
= nd
->dentry
->d_inode
;
526 if (!inode
->i_op
->lookup
)
529 /* here ends the main loop */
532 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
534 if (lookup_flags
& LOOKUP_PARENT
)
536 if (this.name
[0] == '.') switch (this.len
) {
540 if (this.name
[1] != '.')
543 inode
= nd
->dentry
->d_inode
;
548 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
549 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
553 dentry
= cached_lookup(nd
->dentry
, &this, 0);
555 dentry
= real_lookup(nd
->dentry
, &this, 0);
556 err
= PTR_ERR(dentry
);
560 while (d_mountpoint(dentry
) && __follow_down(&nd
->mnt
, &dentry
))
562 inode
= dentry
->d_inode
;
563 if ((lookup_flags
& LOOKUP_FOLLOW
)
564 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
565 err
= do_follow_link(dentry
, nd
);
569 inode
= nd
->dentry
->d_inode
;
577 if (lookup_flags
& LOOKUP_DIRECTORY
) {
579 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
585 if (lookup_flags
& (LOOKUP_POSITIVE
|LOOKUP_DIRECTORY
))
590 nd
->last_type
= LAST_NORM
;
591 if (this.name
[0] != '.')
594 nd
->last_type
= LAST_DOT
;
595 else if (this.len
== 2 && this.name
[1] == '.')
596 nd
->last_type
= LAST_DOTDOT
;
609 /* returns 1 if everything is done */
610 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
612 if (path_walk(name
, nd
))
615 if (!nd
->dentry
->d_inode
) {
616 struct nameidata nd_root
;
617 nd_root
.last_type
= LAST_ROOT
;
618 nd_root
.flags
= nd
->flags
;
619 read_lock(¤t
->fs
->lock
);
620 nd_root
.mnt
= mntget(current
->fs
->rootmnt
);
621 nd_root
.dentry
= dget(current
->fs
->root
);
622 read_unlock(¤t
->fs
->lock
);
623 if (path_walk(name
, &nd_root
))
625 if (nd_root
.dentry
->d_inode
) {
627 nd
->dentry
= nd_root
.dentry
;
628 nd
->mnt
= nd_root
.mnt
;
629 nd
->last
= nd_root
.last
;
632 path_release(&nd_root
);
637 void set_fs_altroot(void)
639 char *emul
= __emul_prefix();
641 struct vfsmount
*mnt
= NULL
, *oldmnt
;
642 struct dentry
*dentry
= NULL
, *olddentry
;
644 read_lock(¤t
->fs
->lock
);
645 nd
.mnt
= mntget(current
->fs
->rootmnt
);
646 nd
.dentry
= dget(current
->fs
->root
);
647 read_unlock(¤t
->fs
->lock
);
648 nd
.flags
= LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_POSITIVE
;
649 if (path_walk(emul
,&nd
) == 0) {
654 write_lock(¤t
->fs
->lock
);
655 oldmnt
= current
->fs
->altrootmnt
;
656 olddentry
= current
->fs
->altroot
;
657 current
->fs
->altrootmnt
= mnt
;
658 current
->fs
->altroot
= dentry
;
659 write_unlock(¤t
->fs
->lock
);
668 walk_init_root(const char *name
, struct nameidata
*nd
)
670 read_lock(¤t
->fs
->lock
);
671 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
)) {
672 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
673 nd
->dentry
= dget(current
->fs
->altroot
);
674 read_unlock(¤t
->fs
->lock
);
675 if (__emul_lookup_dentry(name
,nd
))
677 read_lock(¤t
->fs
->lock
);
679 nd
->mnt
= mntget(current
->fs
->rootmnt
);
680 nd
->dentry
= dget(current
->fs
->root
);
681 read_unlock(¤t
->fs
->lock
);
686 int path_init(const char *name
,unsigned int flags
,struct nameidata
*nd
)
688 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
691 return walk_init_root(name
,nd
);
692 read_lock(¤t
->fs
->lock
);
693 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
694 nd
->dentry
= dget(current
->fs
->pwd
);
695 read_unlock(¤t
->fs
->lock
);
700 * Restricted form of lookup. Doesn't follow links, single-component only,
701 * needs parent already locked. Doesn't follow mounts.
704 struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
706 struct dentry
* dentry
;
710 inode
= base
->d_inode
;
711 err
= permission(inode
, MAY_EXEC
);
712 dentry
= ERR_PTR(err
);
717 * See if the low-level filesystem might want
718 * to use its own hash..
720 if (base
->d_op
&& base
->d_op
->d_hash
) {
721 err
= base
->d_op
->d_hash(base
, name
);
722 dentry
= ERR_PTR(err
);
727 dentry
= cached_lookup(base
, name
, 0);
729 struct dentry
*new = d_alloc(base
, name
);
730 dentry
= ERR_PTR(-ENOMEM
);
734 dentry
= inode
->i_op
->lookup(inode
, new);
746 struct dentry
* lookup_one(const char * name
, struct dentry
* base
)
753 c
= *(const unsigned char *)name
;
757 hash
= init_name_hash();
762 hash
= partial_name_hash(c
, hash
);
763 c
= *(const unsigned char *)name
;
765 this.len
= name
- (const char *) this.name
;
766 this.hash
= end_name_hash(hash
);
768 return lookup_hash(&this, base
);
770 return ERR_PTR(-EACCES
);
776 * is used by most simple commands to get the inode of a specified name.
777 * Open, link etc use their own routines, but this is enough for things
780 * namei exists in two versions: namei/lnamei. The only difference is
781 * that namei follows links, while lnamei does not.
784 int __user_walk(const char *name
, unsigned flags
, struct nameidata
*nd
)
793 if (path_init(tmp
, flags
, nd
))
794 err
= path_walk(tmp
, nd
);
801 * It's inline, so penalty for filesystems that don't use sticky bit is
804 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
806 if (!(dir
->i_mode
& S_ISVTX
))
808 if (inode
->i_uid
== current
->fsuid
)
810 if (dir
->i_uid
== current
->fsuid
)
812 return !capable(CAP_FOWNER
);
816 * Check whether we can remove a link victim from directory dir, check
817 * whether the type of victim is right.
818 * 1. We can't do it if dir is read-only (done in permission())
819 * 2. We should have write and exec permissions on dir
820 * 3. We can't remove anything from append-only dir
821 * 4. We can't do anything with immutable dir (done in permission())
822 * 5. If the sticky bit on dir is set we should either
823 * a. be owner of dir, or
824 * b. be owner of victim, or
825 * c. have CAP_FOWNER capability
826 * 6. If the victim is append-only or immutable we can't do antyhing with
827 * links pointing to it.
828 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
829 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
830 * 9. We can't remove a root or mountpoint.
832 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
, int isdir
)
835 if (!victim
->d_inode
|| victim
->d_parent
->d_inode
!= dir
)
837 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
);
842 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
843 IS_IMMUTABLE(victim
->d_inode
))
846 if (!S_ISDIR(victim
->d_inode
->i_mode
))
850 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
855 /* Check whether we can create an object with dentry child in directory
857 * 1. We can't do it if child already exists (open has special treatment for
858 * this case, but since we are inlined it's OK)
859 * 2. We can't do it if dir is read-only (done in permission())
860 * 3. We should have write and exec permissions on dir
861 * 4. We can't do it if dir is immutable (done in permission())
863 static inline int may_create(struct inode
*dir
, struct dentry
*child
) {
868 return permission(dir
,MAY_WRITE
| MAY_EXEC
);
872 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
875 * O_DIRECTORY translates into forcing a directory lookup.
877 static inline int lookup_flags(unsigned int f
)
879 unsigned long retval
= LOOKUP_FOLLOW
;
882 retval
&= ~LOOKUP_FOLLOW
;
884 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
885 retval
&= ~LOOKUP_FOLLOW
;
888 retval
|= LOOKUP_DIRECTORY
;
893 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
897 mode
&= S_IALLUGO
& ~current
->fs
->umask
;
900 down(&dir
->i_zombie
);
901 error
= may_create(dir
, dentry
);
905 error
= -EACCES
; /* shouldn't it be ENOSYS? */
906 if (!dir
->i_op
|| !dir
->i_op
->create
)
911 error
= dir
->i_op
->create(dir
, dentry
, mode
);
921 * namei for open - this is in fact almost the whole open-routine.
923 * Note that the low bits of "flag" aren't the same as in the open
924 * system call - they are 00 - no permissions needed
925 * 01 - read permission needed
926 * 10 - write permission needed
927 * 11 - read/write permissions needed
928 * which is a lot more logical, and also allows the "no perm" needed
929 * for symlinks (where the permissions are checked later).
932 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
934 int acc_mode
, error
= 0;
936 struct dentry
*dentry
;
940 acc_mode
= ACC_MODE(flag
);
943 * The simplest case - just a plain lookup.
945 if (!(flag
& O_CREAT
)) {
946 if (path_init(pathname
, lookup_flags(flag
), nd
))
947 error
= path_walk(pathname
, nd
);
955 * Create - we need to know the parent.
957 if (path_init(pathname
, LOOKUP_PARENT
, nd
))
958 error
= path_walk(pathname
, nd
);
963 * We have the parent and last component. First of all, check
964 * that we are not asked to creat(2) an obvious directory - that
968 if (nd
->last_type
!= LAST_NORM
|| nd
->last
.name
[nd
->last
.len
])
972 down(&dir
->d_inode
->i_sem
);
973 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
976 error
= PTR_ERR(dentry
);
977 if (IS_ERR(dentry
)) {
978 up(&dir
->d_inode
->i_sem
);
982 /* Negative dentry, just create the file */
983 if (!dentry
->d_inode
) {
984 error
= vfs_create(dir
->d_inode
, dentry
, mode
);
985 up(&dir
->d_inode
->i_sem
);
990 /* Don't check for write permission, don't truncate */
999 up(&dir
->d_inode
->i_sem
);
1005 if (d_mountpoint(dentry
)) {
1007 if (flag
& O_NOFOLLOW
)
1009 do __follow_down(&nd
->mnt
,&dentry
); while(d_mountpoint(dentry
));
1012 if (!dentry
->d_inode
)
1014 if (dentry
->d_inode
->i_op
&& dentry
->d_inode
->i_op
->follow_link
)
1018 nd
->dentry
= dentry
;
1020 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
1024 inode
= dentry
->d_inode
;
1029 if (S_ISLNK(inode
->i_mode
))
1033 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
1036 error
= permission(inode
,acc_mode
);
1041 * FIFO's, sockets and device files are special: they don't
1042 * actually live on the filesystem itself, and as such you
1043 * can write to them even if the filesystem is read-only.
1045 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
1047 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
1049 if (IS_NODEV(inode
))
1055 if (IS_RDONLY(inode
) && (flag
& 2))
1059 * An append-only file must be opened in append mode for writing.
1062 if (IS_APPEND(inode
)) {
1063 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
1069 if (flag
& O_TRUNC
) {
1070 error
= get_write_access(inode
);
1075 * Refuse to truncate files with mandatory locks held on them.
1077 error
= locks_verify_locked(inode
);
1081 error
= do_truncate(dentry
, 0);
1083 put_write_access(inode
);
1087 if (flag
& FMODE_WRITE
)
1100 if (flag
& O_NOFOLLOW
)
1103 * This is subtle. Instead of calling do_follow_link() we do the
1104 * thing by hands. The reason is that this way we have zero link_count
1105 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1106 * After that we have the parent and last component, i.e.
1107 * we are in the same situation as after the first path_walk().
1108 * Well, almost - if the last component is normal we get its copy
1109 * stored in nd->last.name and we will have to putname() it when we
1110 * are done. Procfs-like symlinks just set LAST_BIND.
1112 UPDATE_ATIME(dentry
->d_inode
);
1113 error
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
1117 if (nd
->last_type
== LAST_BIND
) {
1118 dentry
= nd
->dentry
;
1122 if (nd
->last_type
!= LAST_NORM
)
1124 if (nd
->last
.name
[nd
->last
.len
]) {
1125 putname(nd
->last
.name
);
1129 dentry
= nd
->dentry
;
1130 putname(nd
->last
.name
);
1134 down(&dir
->d_inode
->i_sem
);
1135 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1136 putname(nd
->last
.name
);
1141 static struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
1143 struct dentry
*dentry
;
1145 down(&nd
->dentry
->d_inode
->i_sem
);
1146 dentry
= ERR_PTR(-EEXIST
);
1147 if (nd
->last_type
!= LAST_NORM
)
1149 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1152 if (!is_dir
&& nd
->last
.name
[nd
->last
.len
] && !dentry
->d_inode
)
1157 dentry
= ERR_PTR(-ENOENT
);
1162 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
1166 mode
&= ~current
->fs
->umask
;
1168 down(&dir
->i_zombie
);
1169 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
1172 error
= may_create(dir
, dentry
);
1177 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
1182 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1189 asmlinkage
long sys_mknod(const char * filename
, int mode
, dev_t dev
)
1193 struct dentry
* dentry
;
1194 struct nameidata nd
;
1198 tmp
= getname(filename
);
1200 return PTR_ERR(tmp
);
1202 if (path_init(tmp
, LOOKUP_PARENT
, &nd
))
1203 error
= path_walk(tmp
, &nd
);
1206 dentry
= lookup_create(&nd
, 0);
1207 error
= PTR_ERR(dentry
);
1208 if (!IS_ERR(dentry
)) {
1209 switch (mode
& S_IFMT
) {
1210 case 0: case S_IFREG
:
1211 error
= vfs_create(nd
.dentry
->d_inode
,dentry
,mode
);
1213 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
1214 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,dev
);
1224 up(&nd
.dentry
->d_inode
->i_sem
);
1232 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1236 down(&dir
->i_zombie
);
1237 error
= may_create(dir
, dentry
);
1242 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1246 mode
&= (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
;
1248 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1256 asmlinkage
long sys_mkdir(const char * pathname
, int mode
)
1261 tmp
= getname(pathname
);
1262 error
= PTR_ERR(tmp
);
1264 struct dentry
*dentry
;
1265 struct nameidata nd
;
1267 if (path_init(tmp
, LOOKUP_PARENT
, &nd
))
1268 error
= path_walk(tmp
, &nd
);
1271 dentry
= lookup_create(&nd
, 1);
1272 error
= PTR_ERR(dentry
);
1273 if (!IS_ERR(dentry
)) {
1274 error
= vfs_mkdir(nd
.dentry
->d_inode
, dentry
, mode
);
1277 up(&nd
.dentry
->d_inode
->i_sem
);
1287 * We try to drop the dentry early: we should have
1288 * a usage count of 2 if we're the only user of this
1289 * dentry, and if that is true (possibly after pruning
1290 * the dcache), then we drop the dentry now.
1292 * A low-level filesystem can, if it choses, legally
1295 * if (!d_unhashed(dentry))
1298 * if it cannot handle the case of removing a directory
1299 * that is still in use by something else..
1301 static void d_unhash(struct dentry
*dentry
)
1304 switch (atomic_read(&dentry
->d_count
)) {
1306 shrink_dcache_parent(dentry
);
1307 if (atomic_read(&dentry
->d_count
) != 2)
1314 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1318 error
= may_delete(dir
, dentry
, 1);
1322 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1327 double_down(&dir
->i_zombie
, &dentry
->d_inode
->i_zombie
);
1329 if (IS_DEADDIR(dir
))
1331 else if (d_mountpoint(dentry
))
1335 error
= dir
->i_op
->rmdir(dir
, dentry
);
1338 dentry
->d_inode
->i_flags
|= S_DEAD
;
1340 double_up(&dir
->i_zombie
, &dentry
->d_inode
->i_zombie
);
1348 asmlinkage
long sys_rmdir(const char * pathname
)
1352 struct dentry
*dentry
;
1353 struct nameidata nd
;
1355 name
= getname(pathname
);
1357 return PTR_ERR(name
);
1359 if (path_init(name
, LOOKUP_PARENT
, &nd
))
1360 error
= path_walk(name
, &nd
);
1364 switch(nd
.last_type
) {
1368 case LAST_ROOT
: case LAST_DOT
:
1372 down(&nd
.dentry
->d_inode
->i_sem
);
1373 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1374 error
= PTR_ERR(dentry
);
1375 if (!IS_ERR(dentry
)) {
1376 error
= vfs_rmdir(nd
.dentry
->d_inode
, dentry
);
1379 up(&nd
.dentry
->d_inode
->i_sem
);
1387 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1391 down(&dir
->i_zombie
);
1392 error
= may_delete(dir
, dentry
, 0);
1395 if (dir
->i_op
&& dir
->i_op
->unlink
) {
1397 if (d_mountpoint(dentry
))
1401 error
= dir
->i_op
->unlink(dir
, dentry
);
1412 asmlinkage
long sys_unlink(const char * pathname
)
1416 struct dentry
*dentry
;
1417 struct nameidata nd
;
1419 name
= getname(pathname
);
1421 return PTR_ERR(name
);
1423 if (path_init(name
, LOOKUP_PARENT
, &nd
))
1424 error
= path_walk(name
, &nd
);
1428 if (nd
.last_type
!= LAST_NORM
)
1430 down(&nd
.dentry
->d_inode
->i_sem
);
1431 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1432 error
= PTR_ERR(dentry
);
1433 if (!IS_ERR(dentry
)) {
1434 /* Why not before? Because we want correct error value */
1435 if (nd
.last
.name
[nd
.last
.len
])
1437 error
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
1441 up(&nd
.dentry
->d_inode
->i_sem
);
1450 error
= !dentry
->d_inode
? -ENOENT
:
1451 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
1455 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
1459 down(&dir
->i_zombie
);
1460 error
= may_create(dir
, dentry
);
1465 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1470 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1478 asmlinkage
long sys_symlink(const char * oldname
, const char * newname
)
1484 from
= getname(oldname
);
1486 return PTR_ERR(from
);
1487 to
= getname(newname
);
1488 error
= PTR_ERR(to
);
1490 struct dentry
*dentry
;
1491 struct nameidata nd
;
1493 if (path_init(to
, LOOKUP_PARENT
, &nd
))
1494 error
= path_walk(to
, &nd
);
1497 dentry
= lookup_create(&nd
, 0);
1498 error
= PTR_ERR(dentry
);
1499 if (!IS_ERR(dentry
)) {
1500 error
= vfs_symlink(nd
.dentry
->d_inode
, dentry
, from
);
1503 up(&nd
.dentry
->d_inode
->i_sem
);
1512 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1514 struct inode
*inode
;
1517 down(&dir
->i_zombie
);
1519 inode
= old_dentry
->d_inode
;
1523 error
= may_create(dir
, new_dentry
);
1528 if (dir
->i_dev
!= inode
->i_dev
)
1532 * A link to an append-only or immutable file cannot be created.
1535 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1537 if (!dir
->i_op
|| !dir
->i_op
->link
)
1542 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1551 * Hardlinks are often used in delicate situations. We avoid
1552 * security-related surprises by not following symlinks on the
1555 * We don't follow them on the oldname either to be compatible
1556 * with linux 2.0, and to avoid hard-linking to directories
1557 * and other special files. --ADM
1559 asmlinkage
long sys_link(const char * oldname
, const char * newname
)
1565 from
= getname(oldname
);
1567 return PTR_ERR(from
);
1568 to
= getname(newname
);
1569 error
= PTR_ERR(to
);
1571 struct dentry
*new_dentry
;
1572 struct nameidata nd
, old_nd
;
1575 if (path_init(from
, LOOKUP_POSITIVE
, &old_nd
))
1576 error
= path_walk(from
, &old_nd
);
1579 if (path_init(to
, LOOKUP_PARENT
, &nd
))
1580 error
= path_walk(to
, &nd
);
1584 if (old_nd
.mnt
!= nd
.mnt
)
1586 new_dentry
= lookup_create(&nd
, 0);
1587 error
= PTR_ERR(new_dentry
);
1588 if (!IS_ERR(new_dentry
)) {
1589 error
= vfs_link(old_nd
.dentry
, nd
.dentry
->d_inode
, new_dentry
);
1592 up(&nd
.dentry
->d_inode
->i_sem
);
1595 path_release(&old_nd
);
1605 * The worst of all namespace operations - renaming directory. "Perverted"
1606 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1608 * a) we can get into loop creation. Check is done in is_subdir().
1609 * b) race potential - two innocent renames can create a loop together.
1610 * That's where 4.4 screws up. Current fix: serialization on
1611 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1613 * c) we have to lock _three_ objects - parents and victim (if it exists).
1614 * And that - after we got ->i_sem on parents (until then we don't know
1615 * whether the target exists at all, let alone whether it is a directory
1616 * or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1617 * on link creation/removal of any kind. And taken (without ->i_sem) on
1618 * directory that will be removed (both in rmdir() and here).
1619 * d) some filesystems don't support opened-but-unlinked directories,
1620 * either because of layout or because they are not ready to deal with
1621 * all cases correctly. The latter will be fixed (taking this sort of
1622 * stuff into VFS), but the former is not going away. Solution: the same
1623 * trick as in rmdir().
1624 * e) conversion from fhandle to dentry may come in the wrong moment - when
1625 * we are removing the target. Solution: we will have to grab ->i_zombie
1626 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1627 * ->i_sem on parents, which works but leads to some truely excessive
1630 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
1631 struct inode
*new_dir
, struct dentry
*new_dentry
)
1634 struct inode
*target
;
1636 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1639 error
= may_delete(old_dir
, old_dentry
, 1);
1643 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1646 if (!new_dentry
->d_inode
)
1647 error
= may_create(new_dir
, new_dentry
);
1649 error
= may_delete(new_dir
, new_dentry
, 1);
1653 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1657 * If we are going to change the parent - check write permissions,
1658 * we'll need to flip '..'.
1660 if (new_dir
!= old_dir
) {
1661 error
= permission(old_dentry
->d_inode
, MAY_WRITE
);
1666 DQUOT_INIT(old_dir
);
1667 DQUOT_INIT(new_dir
);
1668 down(&old_dir
->i_sb
->s_vfs_rename_sem
);
1670 if (is_subdir(new_dentry
, old_dentry
))
1672 target
= new_dentry
->d_inode
;
1673 if (target
) { /* Hastur! Hastur! Hastur! */
1674 triple_down(&old_dir
->i_zombie
,
1677 d_unhash(new_dentry
);
1679 double_down(&old_dir
->i_zombie
,
1680 &new_dir
->i_zombie
);
1681 if (IS_DEADDIR(old_dir
)||IS_DEADDIR(new_dir
))
1683 else if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
1686 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1689 target
->i_flags
|= S_DEAD
;
1690 triple_up(&old_dir
->i_zombie
,
1693 d_rehash(new_dentry
);
1696 double_up(&old_dir
->i_zombie
,
1697 &new_dir
->i_zombie
);
1700 d_move(old_dentry
,new_dentry
);
1702 up(&old_dir
->i_sb
->s_vfs_rename_sem
);
1706 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
1707 struct inode
*new_dir
, struct dentry
*new_dentry
)
1711 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1714 error
= may_delete(old_dir
, old_dentry
, 0);
1718 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1721 if (!new_dentry
->d_inode
)
1722 error
= may_create(new_dir
, new_dentry
);
1724 error
= may_delete(new_dir
, new_dentry
, 0);
1728 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1731 DQUOT_INIT(old_dir
);
1732 DQUOT_INIT(new_dir
);
1733 double_down(&old_dir
->i_zombie
, &new_dir
->i_zombie
);
1734 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
1737 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1738 double_up(&old_dir
->i_zombie
, &new_dir
->i_zombie
);
1741 /* The following d_move() should become unconditional */
1742 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_ODD_RENAME
)) {
1743 d_move(old_dentry
, new_dentry
);
1748 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1749 struct inode
*new_dir
, struct dentry
*new_dentry
)
1751 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1752 return vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
1754 return vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
1757 static inline int do_rename(const char * oldname
, const char * newname
)
1760 struct dentry
* old_dir
, * new_dir
;
1761 struct dentry
* old_dentry
, *new_dentry
;
1762 struct nameidata oldnd
, newnd
;
1764 if (path_init(oldname
, LOOKUP_PARENT
, &oldnd
))
1765 error
= path_walk(oldname
, &oldnd
);
1770 if (path_init(newname
, LOOKUP_PARENT
, &newnd
))
1771 error
= path_walk(newname
, &newnd
);
1776 if (oldnd
.mnt
!= newnd
.mnt
)
1779 old_dir
= oldnd
.dentry
;
1781 if (oldnd
.last_type
!= LAST_NORM
)
1784 new_dir
= newnd
.dentry
;
1785 if (newnd
.last_type
!= LAST_NORM
)
1788 double_lock(new_dir
, old_dir
);
1790 old_dentry
= lookup_hash(&oldnd
.last
, old_dir
);
1791 error
= PTR_ERR(old_dentry
);
1792 if (IS_ERR(old_dentry
))
1794 /* source must exist */
1796 if (!old_dentry
->d_inode
)
1798 /* unless the source is a directory trailing slashes give -ENOTDIR */
1799 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
1801 if (oldnd
.last
.name
[oldnd
.last
.len
])
1803 if (newnd
.last
.name
[newnd
.last
.len
])
1806 new_dentry
= lookup_hash(&newnd
.last
, new_dir
);
1807 error
= PTR_ERR(new_dentry
);
1808 if (IS_ERR(new_dentry
))
1812 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
1813 new_dir
->d_inode
, new_dentry
);
1820 double_up(&new_dir
->d_inode
->i_sem
, &old_dir
->d_inode
->i_sem
);
1822 path_release(&newnd
);
1824 path_release(&oldnd
);
1829 asmlinkage
long sys_rename(const char * oldname
, const char * newname
)
1835 from
= getname(oldname
);
1837 return PTR_ERR(from
);
1838 to
= getname(newname
);
1839 error
= PTR_ERR(to
);
1841 error
= do_rename(from
,to
);
1848 int vfs_readlink(struct dentry
*dentry
, char *buffer
, int buflen
, const char *link
)
1852 len
= PTR_ERR(link
);
1857 if (len
> (unsigned) buflen
)
1859 if (copy_to_user(buffer
, link
, len
))
1866 __vfs_follow_link(struct nameidata
*nd
, const char *link
)
1875 if (!walk_init_root(link
, nd
))
1876 /* weird __emul_prefix() stuff did it */
1879 res
= path_walk(link
, nd
);
1881 if (current
->link_count
|| res
|| nd
->last_type
!=LAST_NORM
)
1884 * If it is an iterative symlinks resolution in open_namei() we
1885 * have to copy the last component. And all that crap because of
1886 * bloody create() on broken symlinks. Furrfu...
1891 strcpy(name
, nd
->last
.name
);
1892 nd
->last
.name
= name
;
1898 return PTR_ERR(link
);
1901 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
1903 return __vfs_follow_link(nd
, link
);
1906 /* get the link contents into pagecache */
1907 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
1910 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
1911 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
1916 if (!Page_Uptodate(page
))
1919 return (char*) kmap(page
);
1922 page_cache_release(page
);
1923 return ERR_PTR(-EIO
);
1929 int page_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
1931 struct page
*page
= NULL
;
1932 char *s
= page_getlink(dentry
, &page
);
1933 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
1936 page_cache_release(page
);
1941 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1943 struct page
*page
= NULL
;
1944 char *s
= page_getlink(dentry
, &page
);
1945 int res
= __vfs_follow_link(nd
, s
);
1948 page_cache_release(page
);
1953 struct inode_operations page_symlink_inode_operations
= {
1954 readlink
: page_readlink
,
1955 follow_link
: page_follow_link
,