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.
18 #include <linux/proc_fs.h>
19 #include <linux/smp_lock.h>
20 #include <linux/quotaops.h>
21 #include <linux/pagemap.h>
22 #include <linux/dcache.h>
24 #include <asm/uaccess.h>
25 #include <asm/unaligned.h>
26 #include <asm/semaphore.h>
28 #include <asm/pgtable.h>
30 #include <asm/namei.h>
32 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
34 /* [Feb-1997 T. Schoebel-Theuer]
35 * Fundamental changes in the pathname lookup mechanisms (namei)
36 * were necessary because of omirr. The reason is that omirr needs
37 * to know the _real_ pathname, not the user-supplied one, in case
38 * of symlinks (and also when transname replacements occur).
40 * The new code replaces the old recursive symlink resolution with
41 * an iterative one (in case of non-nested symlink chains). It does
42 * this with calls to <fs>_follow_link().
43 * As a side effect, dir_namei(), _namei() and follow_link() are now
44 * replaced with a single function lookup_dentry() that can handle all
45 * the special cases of the former code.
47 * With the new dcache, the pathname is stored at each inode, at least as
48 * long as the refcount of the inode is positive. As a side effect, the
49 * size of the dcache depends on the inode cache and thus is dynamic.
51 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
52 * resolution to correspond with current state of the code.
54 * Note that the symlink resolution is not *completely* iterative.
55 * There is still a significant amount of tail- and mid- recursion in
56 * the algorithm. Also, note that <fs>_readlink() is not used in
57 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
58 * may return different results than <fs>_follow_link(). Many virtual
59 * filesystems (including /proc) exhibit this behavior.
62 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
63 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
64 * and the name already exists in form of a symlink, try to create the new
65 * name indicated by the symlink. The old code always complained that the
66 * name already exists, due to not following the symlink even if its target
67 * is nonexistent. The new semantics affects also mknod() and link() when
68 * the name is a symlink pointing to a non-existant name.
70 * I don't know which semantics is the right one, since I have no access
71 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
72 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
73 * "old" one. Personally, I think the new semantics is much more logical.
74 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
75 * file does succeed in both HP-UX and SunOs, but not in Solaris
76 * and in the old Linux semantics.
79 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
80 * semantics. See the comments in "open_namei" and "do_link" below.
82 * [10-Sep-98 Alan Modra] Another symlink change.
85 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
86 * inside the path - always follow.
87 * in the last component in creation/removal/renaming - never follow.
88 * if LOOKUP_FOLLOW passed - follow.
89 * if the pathname has trailing slashes - follow.
90 * otherwise - don't follow.
91 * (applied in that order).
93 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
94 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
95 * During the 2.4 we need to fix the userland stuff depending on it -
96 * hopefully we will be able to get rid of that wart in 2.5. So far only
97 * XEmacs seems to be relying on it...
100 /* In order to reduce some races, while at the same time doing additional
101 * checking and hopefully speeding things up, we copy filenames to the
102 * kernel data space before using them..
104 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
106 static inline int do_getname(const char *filename
, char *page
)
109 unsigned long len
= PAGE_SIZE
;
111 if ((unsigned long) filename
>= TASK_SIZE
) {
112 if (!segment_eq(get_fs(), KERNEL_DS
))
114 } else if (TASK_SIZE
- (unsigned long) filename
< PAGE_SIZE
)
115 len
= TASK_SIZE
- (unsigned long) filename
;
117 retval
= strncpy_from_user((char *)page
, filename
, len
);
121 return -ENAMETOOLONG
;
127 char * getname(const char * filename
)
131 result
= ERR_PTR(-ENOMEM
);
134 int retval
= do_getname(filename
, tmp
);
139 result
= ERR_PTR(retval
);
148 * is used to check for read/write/execute permissions on a file.
149 * We use "fsuid" for this, letting us set arbitrary permissions
150 * for filesystem access without changing the "normal" uids which
151 * are used for other things..
153 int permission(struct inode
* inode
,int mask
)
155 int mode
= inode
->i_mode
;
157 if (inode
->i_op
&& inode
->i_op
->permission
) {
160 retval
= inode
->i_op
->permission(inode
, mask
);
165 if ((mask
& S_IWOTH
) && IS_RDONLY(inode
) &&
166 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
167 return -EROFS
; /* Nobody gets write access to a read-only fs */
169 if ((mask
& S_IWOTH
) && IS_IMMUTABLE(inode
))
170 return -EACCES
; /* Nobody gets write access to an immutable file */
172 if (current
->fsuid
== inode
->i_uid
)
174 else if (in_group_p(inode
->i_gid
))
177 if (((mode
& mask
& S_IRWXO
) == mask
) || capable(CAP_DAC_OVERRIDE
))
180 /* read and search access */
181 if ((mask
== S_IROTH
) ||
182 (S_ISDIR(mode
) && !(mask
& ~(S_IROTH
| S_IXOTH
))))
183 if (capable(CAP_DAC_READ_SEARCH
))
190 * get_write_access() gets write permission for a file.
191 * put_write_access() releases this write permission.
192 * This is used for regular files.
193 * We cannot support write (and maybe mmap read-write shared) accesses and
194 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
195 * can have the following values:
196 * 0: no writers, no VM_DENYWRITE mappings
197 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
198 * > 0: (i_writecount) users are writing to the file.
200 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
201 * except for the cases where we don't hold i_writecount yet. Then we need to
202 * use {get,deny}_write_access() - these functions check the sign and refuse
203 * to do the change if sign is wrong. Exclusion between them is provided by
204 * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
205 * who will try to move it in struct inode - just leave it here.
207 static spinlock_t arbitration_lock
= SPIN_LOCK_UNLOCKED
;
208 int get_write_access(struct inode
* inode
)
210 spin_lock(&arbitration_lock
);
211 if (atomic_read(&inode
->i_writecount
) < 0) {
212 spin_unlock(&arbitration_lock
);
215 atomic_inc(&inode
->i_writecount
);
216 spin_unlock(&arbitration_lock
);
219 int deny_write_access(struct file
* file
)
221 spin_lock(&arbitration_lock
);
222 if (atomic_read(&file
->f_dentry
->d_inode
->i_writecount
) > 0) {
223 spin_unlock(&arbitration_lock
);
226 atomic_dec(&file
->f_dentry
->d_inode
->i_writecount
);
227 spin_unlock(&arbitration_lock
);
231 void path_release(struct nameidata
*nd
)
240 * 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..
262 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
264 struct dentry
* result
;
265 struct inode
*dir
= parent
->d_inode
;
269 * First re-do the cached lookup just in case it was created
270 * while we waited for the directory semaphore..
272 * FIXME! This could use version numbering or similar to
273 * avoid unnecessary cache lookups.
275 result
= d_lookup(parent
, name
);
277 struct dentry
* dentry
= d_alloc(parent
, name
);
278 result
= ERR_PTR(-ENOMEM
);
280 result
= dir
->i_op
->lookup(dir
, dentry
);
291 * Uhhuh! Nasty case: the cache was re-populated while
292 * we waited on the semaphore. Need to revalidate, but
293 * we're going to return this entry regardless (same
294 * as if it was busy).
297 if (result
->d_op
&& result
->d_op
->d_revalidate
)
298 result
->d_op
->d_revalidate(result
, flags
);
302 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
305 if (current
->link_count
>= 32)
307 current
->link_count
++;
308 UPDATE_ATIME(dentry
->d_inode
);
309 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
310 current
->link_count
--;
317 static inline int __follow_up(struct vfsmount
**mnt
, struct dentry
**base
)
319 struct vfsmount
*parent
=(*mnt
)->mnt_parent
;
320 struct dentry
*dentry
;
324 dentry
=dget((*mnt
)->mnt_mountpoint
);
332 int follow_up(struct vfsmount
**mnt
, struct dentry
**dentry
)
334 return __follow_up(mnt
, dentry
);
337 static inline int __follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
339 struct list_head
*p
= (*dentry
)->d_vfsmnt
.next
;
340 while (p
!= &(*dentry
)->d_vfsmnt
) {
341 struct vfsmount
*tmp
;
342 tmp
= list_entry(p
, struct vfsmount
, mnt_clash
);
343 if (tmp
->mnt_parent
== *mnt
) {
345 mntput(tmp
->mnt_parent
);
346 /* tmp holds the mountpoint, so... */
348 *dentry
= dget(tmp
->mnt_root
);
356 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
358 return __follow_down(mnt
,dentry
);
361 static inline void follow_dotdot(struct nameidata
*nd
)
364 struct vfsmount
*parent
;
365 struct dentry
*dentry
;
366 if (nd
->dentry
== current
->fs
->root
&&
367 nd
->mnt
== current
->fs
->rootmnt
) {
370 if (nd
->dentry
!= nd
->mnt
->mnt_root
) {
371 dentry
= dget(nd
->dentry
->d_parent
);
376 parent
=nd
->mnt
->mnt_parent
;
377 if (parent
== nd
->mnt
) {
381 dentry
=dget(nd
->mnt
->mnt_mountpoint
);
391 * This is the basic name resolution function, turning a pathname
392 * into the final dentry.
394 * We expect 'base' to be positive and a directory.
396 int path_walk(const char * name
, struct nameidata
*nd
)
398 struct dentry
*dentry
;
401 unsigned int lookup_flags
= nd
->flags
;
408 inode
= nd
->dentry
->d_inode
;
409 if (current
->link_count
)
410 lookup_flags
= LOOKUP_FOLLOW
;
412 /* At this point we know we have a real path component. */
418 err
= permission(inode
, MAY_EXEC
);
419 dentry
= ERR_PTR(err
);
424 c
= *(const unsigned char *)name
;
426 hash
= init_name_hash();
429 hash
= partial_name_hash(c
, hash
);
430 c
= *(const unsigned char *)name
;
431 } while (c
&& (c
!= '/'));
432 this.len
= name
- (const char *) this.name
;
433 this.hash
= end_name_hash(hash
);
435 /* remove trailing slashes? */
438 while (*++name
== '/');
440 goto last_with_slashes
;
443 * "." and ".." are special - ".." especially so because it has
444 * to be able to know about the current root directory and
445 * parent relationships.
447 if (this.name
[0] == '.') switch (this.len
) {
451 if (this.name
[1] != '.')
454 inode
= nd
->dentry
->d_inode
;
460 * See if the low-level filesystem might want
461 * to use its own hash..
463 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
464 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
468 /* This does the actual lookups.. */
469 dentry
= cached_lookup(nd
->dentry
, &this, LOOKUP_CONTINUE
);
471 dentry
= real_lookup(nd
->dentry
, &this, LOOKUP_CONTINUE
);
472 err
= PTR_ERR(dentry
);
476 /* Check mountpoints.. */
477 while (d_mountpoint(dentry
) && __follow_down(&nd
->mnt
, &dentry
))
481 inode
= dentry
->d_inode
;
488 if (inode
->i_op
->follow_link
) {
489 err
= do_follow_link(dentry
, nd
);
494 inode
= nd
->dentry
->d_inode
;
505 if (!inode
->i_op
->lookup
)
508 /* here ends the main loop */
511 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
513 if (lookup_flags
& LOOKUP_PARENT
)
515 if (this.name
[0] == '.') switch (this.len
) {
519 if (this.name
[1] != '.')
522 inode
= nd
->dentry
->d_inode
;
527 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
528 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
532 dentry
= cached_lookup(nd
->dentry
, &this, 0);
534 dentry
= real_lookup(nd
->dentry
, &this, 0);
535 err
= PTR_ERR(dentry
);
539 while (d_mountpoint(dentry
) && __follow_down(&nd
->mnt
, &dentry
))
541 inode
= dentry
->d_inode
;
542 if ((lookup_flags
& LOOKUP_FOLLOW
)
543 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
544 err
= do_follow_link(dentry
, nd
);
548 inode
= nd
->dentry
->d_inode
;
556 if (lookup_flags
& LOOKUP_DIRECTORY
) {
558 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
564 if (lookup_flags
& (LOOKUP_POSITIVE
|LOOKUP_DIRECTORY
))
569 nd
->last_type
= LAST_NORM
;
570 if (this.name
[0] != '.')
573 nd
->last_type
= LAST_DOT
;
574 else if (this.len
== 2 && this.name
[1] == '.')
575 nd
->last_type
= LAST_DOTDOT
;
587 /* returns 1 if everything is done */
588 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
590 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
591 nd
->dentry
= dget(current
->fs
->altroot
);
592 if (path_walk(name
, nd
))
595 if (!nd
->dentry
->d_inode
) {
596 struct nameidata nd_root
;
597 nd_root
.last_type
= LAST_ROOT
;
598 nd_root
.flags
= nd
->flags
;
599 nd_root
.mnt
= mntget(current
->fs
->rootmnt
);
600 nd_root
.dentry
= dget(current
->fs
->root
);
601 if (path_walk(name
, &nd_root
))
603 if (nd_root
.dentry
->d_inode
) {
605 nd
->dentry
= nd_root
.dentry
;
606 nd
->mnt
= nd_root
.mnt
;
607 nd
->last
= nd_root
.last
;
610 path_release(&nd_root
);
615 void set_fs_altroot(void)
617 char *emul
= __emul_prefix();
619 struct vfsmount
*mnt
= NULL
, *oldmnt
;
620 struct dentry
*dentry
= NULL
, *olddentry
;
622 nd
.mnt
= mntget(current
->fs
->rootmnt
);
623 nd
.dentry
= dget(current
->fs
->root
);
624 nd
.flags
= LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_POSITIVE
;
625 if (path_walk(emul
,&nd
) == 0) {
630 oldmnt
= current
->fs
->altrootmnt
;
631 olddentry
= current
->fs
->altroot
;
632 current
->fs
->altrootmnt
= mnt
;
633 current
->fs
->altroot
= dentry
;
641 walk_init_root(const char *name
, struct nameidata
*nd
)
643 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
))
644 if (__emul_lookup_dentry(name
,nd
))
646 nd
->mnt
= mntget(current
->fs
->rootmnt
);
647 nd
->dentry
= dget(current
->fs
->root
);
651 int path_init(const char *name
,unsigned int flags
,struct nameidata
*nd
)
653 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
656 return walk_init_root(name
,nd
);
657 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
658 nd
->dentry
= dget(current
->fs
->pwd
);
663 * Restricted form of lookup. Doesn't follow links, single-component only,
664 * needs parent already locked. Doesn't follow mounts.
666 struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
668 struct dentry
* dentry
;
672 inode
= base
->d_inode
;
673 err
= permission(inode
, MAY_EXEC
);
674 dentry
= ERR_PTR(err
);
679 * See if the low-level filesystem might want
680 * to use its own hash..
682 if (base
->d_op
&& base
->d_op
->d_hash
) {
683 err
= base
->d_op
->d_hash(base
, name
);
684 dentry
= ERR_PTR(err
);
689 dentry
= cached_lookup(base
, name
, 0);
691 struct dentry
*new = d_alloc(base
, name
);
692 dentry
= ERR_PTR(-ENOMEM
);
695 dentry
= inode
->i_op
->lookup(inode
, new);
705 struct dentry
* lookup_one(const char * name
, struct dentry
* base
)
712 c
= *(const unsigned char *)name
;
716 hash
= init_name_hash();
721 hash
= partial_name_hash(c
, hash
);
722 c
= *(const unsigned char *)name
;
724 this.len
= name
- (const char *) this.name
;
725 this.hash
= end_name_hash(hash
);
727 return lookup_hash(&this, base
);
729 return ERR_PTR(-EACCES
);
735 * is used by most simple commands to get the inode of a specified name.
736 * Open, link etc use their own routines, but this is enough for things
739 * namei exists in two versions: namei/lnamei. The only difference is
740 * that namei follows links, while lnamei does not.
742 int __user_walk(const char *name
, unsigned flags
, struct nameidata
*nd
)
752 if (path_init(tmp
, flags
, nd
))
753 err
= path_walk(tmp
, nd
);
761 * It's inline, so penalty for filesystems that don't use sticky bit is
764 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
766 if (!(dir
->i_mode
& S_ISVTX
))
768 if (inode
->i_uid
== current
->fsuid
)
770 if (dir
->i_uid
== current
->fsuid
)
772 return !capable(CAP_FOWNER
);
776 * Check whether we can remove a link victim from directory dir, check
777 * whether the type of victim is right.
778 * 1. We can't do it if dir is read-only (done in permission())
779 * 2. We should have write and exec permissions on dir
780 * 3. We can't remove anything from append-only dir
781 * 4. We can't do anything with immutable dir (done in permission())
782 * 5. If the sticky bit on dir is set we should either
783 * a. be owner of dir, or
784 * b. be owner of victim, or
785 * c. have CAP_FOWNER capability
786 * 6. If the victim is append-only or immutable we can't do antyhing with
787 * links pointing to it.
788 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
789 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
790 * 9. We can't remove a root or mountpoint.
792 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
, int isdir
)
795 if (!victim
->d_inode
|| victim
->d_parent
->d_inode
!= dir
)
797 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
);
802 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
803 IS_IMMUTABLE(victim
->d_inode
))
806 if (!S_ISDIR(victim
->d_inode
->i_mode
))
810 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
815 /* Check whether we can create an object with dentry child in directory
817 * 1. We can't do it if child already exists (open has special treatment for
818 * this case, but since we are inlined it's OK)
819 * 2. We can't do it if dir is read-only (done in permission())
820 * 3. We should have write and exec permissions on dir
821 * 4. We can't do it if dir is immutable (done in permission())
823 static inline int may_create(struct inode
*dir
, struct dentry
*child
) {
828 return permission(dir
,MAY_WRITE
| MAY_EXEC
);
832 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
835 * O_DIRECTORY translates into forcing a directory lookup.
837 static inline int lookup_flags(unsigned int f
)
839 unsigned long retval
= LOOKUP_FOLLOW
;
842 retval
&= ~LOOKUP_FOLLOW
;
844 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
845 retval
&= ~LOOKUP_FOLLOW
;
848 retval
|= LOOKUP_DIRECTORY
;
853 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
857 mode
&= S_IALLUGO
& ~current
->fs
->umask
;
860 down(&dir
->i_zombie
);
861 error
= may_create(dir
, dentry
);
865 error
= -EACCES
; /* shouldn't it be ENOSYS? */
866 if (!dir
->i_op
|| !dir
->i_op
->create
)
870 error
= dir
->i_op
->create(dir
, dentry
, mode
);
879 * namei for open - this is in fact almost the whole open-routine.
881 * Note that the low bits of "flag" aren't the same as in the open
882 * system call - they are 00 - no permissions needed
883 * 01 - read permission needed
884 * 10 - write permission needed
885 * 11 - read/write permissions needed
886 * which is a lot more logical, and also allows the "no perm" needed
887 * for symlinks (where the permissions are checked later).
889 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
891 int acc_mode
, error
= 0;
893 struct dentry
*dentry
;
897 acc_mode
= ACC_MODE(flag
);
900 * The simplest case - just a plain lookup.
902 if (!(flag
& O_CREAT
)) {
903 if (path_init(pathname
, lookup_flags(flag
), nd
))
904 error
= path_walk(pathname
, nd
);
912 * Create - we need to know the parent.
914 if (path_init(pathname
, LOOKUP_PARENT
, nd
))
915 error
= path_walk(pathname
, nd
);
920 * We have the parent and last component. First of all, check
921 * that we are not asked to creat(2) an obvious directory - that
925 if (nd
->last_type
!= LAST_NORM
|| nd
->last
.name
[nd
->last
.len
])
929 down(&dir
->d_inode
->i_sem
);
930 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
933 error
= PTR_ERR(dentry
);
934 if (IS_ERR(dentry
)) {
935 up(&dir
->d_inode
->i_sem
);
939 /* Negative dentry, just create the file */
940 if (!dentry
->d_inode
) {
941 error
= vfs_create(dir
->d_inode
, dentry
, mode
);
942 up(&dir
->d_inode
->i_sem
);
947 /* Don't check for write permission, don't truncate */
956 up(&dir
->d_inode
->i_sem
);
962 if (d_mountpoint(dentry
)) {
964 if (flag
& O_NOFOLLOW
)
966 do __follow_down(&nd
->mnt
,&dentry
); while(d_mountpoint(dentry
));
969 if (!dentry
->d_inode
)
971 if (dentry
->d_inode
->i_op
&& dentry
->d_inode
->i_op
->follow_link
)
977 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
981 inode
= dentry
->d_inode
;
986 if (S_ISLNK(inode
->i_mode
))
990 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
993 error
= permission(inode
,acc_mode
);
998 * FIFO's, sockets and device files are special: they don't
999 * actually live on the filesystem itself, and as such you
1000 * can write to them even if the filesystem is read-only.
1002 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
1004 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
1006 if (IS_NODEV(inode
))
1012 if (IS_RDONLY(inode
) && (flag
& 2))
1016 * An append-only file must be opened in append mode for writing.
1019 if (IS_APPEND(inode
)) {
1020 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
1026 if (flag
& O_TRUNC
) {
1027 error
= get_write_access(inode
);
1032 * Refuse to truncate files with mandatory locks held on them.
1034 error
= locks_verify_locked(inode
);
1038 error
= do_truncate(dentry
, 0);
1040 put_write_access(inode
);
1044 if (flag
& FMODE_WRITE
)
1057 if (flag
& O_NOFOLLOW
)
1060 * This is subtle. Instead of calling do_follow_link() we do the
1061 * thing by hands. The reason is that this way we have zero link_count
1062 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1063 * After that we have the parent and last component, i.e.
1064 * we are in the same situation as after the first path_walk().
1065 * Well, almost - if the last component is normal we get its copy
1066 * stored in nd->last.name and we will have to putname() it when we
1067 * are done. Procfs-like symlinks just set LAST_BIND.
1069 UPDATE_ATIME(dentry
->d_inode
);
1070 error
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
1074 if (nd
->last_type
== LAST_BIND
) {
1075 dentry
= nd
->dentry
;
1079 if (nd
->last_type
!= LAST_NORM
)
1081 if (nd
->last
.name
[nd
->last
.len
]) {
1082 putname(nd
->last
.name
);
1086 dentry
= nd
->dentry
;
1087 putname(nd
->last
.name
);
1091 down(&dir
->d_inode
->i_sem
);
1092 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1093 putname(nd
->last
.name
);
1097 static struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
1099 struct dentry
*dentry
;
1101 down(&nd
->dentry
->d_inode
->i_sem
);
1102 dentry
= ERR_PTR(-EEXIST
);
1103 if (nd
->last_type
!= LAST_NORM
)
1105 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
1108 if (!is_dir
&& nd
->last
.name
[nd
->last
.len
] && !dentry
->d_inode
)
1113 dentry
= ERR_PTR(-ENOENT
);
1118 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
1122 mode
&= ~current
->fs
->umask
;
1124 down(&dir
->i_zombie
);
1125 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
1128 error
= may_create(dir
, dentry
);
1133 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
1137 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1143 asmlinkage
long sys_mknod(const char * filename
, int mode
, dev_t dev
)
1147 struct dentry
* dentry
;
1148 struct nameidata nd
;
1152 tmp
= getname(filename
);
1154 return PTR_ERR(tmp
);
1157 if (path_init(tmp
, LOOKUP_PARENT
, &nd
))
1158 error
= path_walk(tmp
, &nd
);
1161 dentry
= lookup_create(&nd
, 0);
1162 error
= PTR_ERR(dentry
);
1163 if (!IS_ERR(dentry
)) {
1164 switch (mode
& S_IFMT
) {
1165 case 0: case S_IFREG
:
1166 error
= vfs_create(nd
.dentry
->d_inode
,dentry
,mode
);
1168 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
1169 error
= vfs_mknod(nd
.dentry
->d_inode
,dentry
,mode
,dev
);
1179 up(&nd
.dentry
->d_inode
->i_sem
);
1188 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1192 down(&dir
->i_zombie
);
1193 error
= may_create(dir
, dentry
);
1198 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1202 mode
&= (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
;
1203 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1210 asmlinkage
long sys_mkdir(const char * pathname
, int mode
)
1215 tmp
= getname(pathname
);
1216 error
= PTR_ERR(tmp
);
1218 struct dentry
*dentry
;
1219 struct nameidata nd
;
1222 if (path_init(tmp
, LOOKUP_PARENT
, &nd
))
1223 error
= path_walk(tmp
, &nd
);
1226 dentry
= lookup_create(&nd
, 1);
1227 error
= PTR_ERR(dentry
);
1228 if (!IS_ERR(dentry
)) {
1229 error
= vfs_mkdir(nd
.dentry
->d_inode
, dentry
, mode
);
1232 up(&nd
.dentry
->d_inode
->i_sem
);
1243 * We try to drop the dentry early: we should have
1244 * a usage count of 2 if we're the only user of this
1245 * dentry, and if that is true (possibly after pruning
1246 * the dcache), then we drop the dentry now.
1248 * A low-level filesystem can, if it choses, legally
1251 * if (!d_unhashed(dentry))
1254 * if it cannot handle the case of removing a directory
1255 * that is still in use by something else..
1257 static void d_unhash(struct dentry
*dentry
)
1260 switch (dentry
->d_count
) {
1262 shrink_dcache_parent(dentry
);
1263 if (dentry
->d_count
!= 2)
1270 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1274 error
= may_delete(dir
, dentry
, 1);
1278 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1283 double_down(&dir
->i_zombie
, &dentry
->d_inode
->i_zombie
);
1285 if (IS_DEADDIR(dir
))
1287 else if (d_mountpoint(dentry
))
1290 error
= dir
->i_op
->rmdir(dir
, dentry
);
1292 dentry
->d_inode
->i_flags
|= S_DEAD
;
1294 double_up(&dir
->i_zombie
, &dentry
->d_inode
->i_zombie
);
1302 asmlinkage
long sys_rmdir(const char * pathname
)
1306 struct dentry
*dentry
;
1307 struct nameidata nd
;
1309 name
= getname(pathname
);
1311 return PTR_ERR(name
);
1314 if (path_init(name
, LOOKUP_PARENT
, &nd
))
1315 error
= path_walk(name
, &nd
);
1319 switch(nd
.last_type
) {
1323 case LAST_ROOT
: case LAST_DOT
:
1327 down(&nd
.dentry
->d_inode
->i_sem
);
1328 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1329 error
= PTR_ERR(dentry
);
1330 if (!IS_ERR(dentry
)) {
1331 error
= vfs_rmdir(nd
.dentry
->d_inode
, dentry
);
1334 up(&nd
.dentry
->d_inode
->i_sem
);
1343 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1347 down(&dir
->i_zombie
);
1348 error
= may_delete(dir
, dentry
, 0);
1351 if (dir
->i_op
&& dir
->i_op
->unlink
) {
1353 if (d_mountpoint(dentry
))
1356 error
= dir
->i_op
->unlink(dir
, dentry
);
1366 asmlinkage
long sys_unlink(const char * pathname
)
1370 struct dentry
*dentry
;
1371 struct nameidata nd
;
1373 name
= getname(pathname
);
1375 return PTR_ERR(name
);
1378 if (path_init(name
, LOOKUP_PARENT
, &nd
))
1379 error
= path_walk(name
, &nd
);
1383 if (nd
.last_type
!= LAST_NORM
)
1385 down(&nd
.dentry
->d_inode
->i_sem
);
1386 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1387 error
= PTR_ERR(dentry
);
1388 if (!IS_ERR(dentry
)) {
1389 /* Why not before? Because we want correct error value */
1390 if (nd
.last
.name
[nd
.last
.len
])
1392 error
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
1396 up(&nd
.dentry
->d_inode
->i_sem
);
1406 error
= !dentry
->d_inode
? -ENOENT
:
1407 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
1411 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
1415 down(&dir
->i_zombie
);
1416 error
= may_create(dir
, dentry
);
1421 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1425 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1432 asmlinkage
long sys_symlink(const char * oldname
, const char * newname
)
1438 from
= getname(oldname
);
1440 return PTR_ERR(from
);
1441 to
= getname(newname
);
1442 error
= PTR_ERR(to
);
1444 struct dentry
*dentry
;
1445 struct nameidata nd
;
1448 if (path_init(to
, LOOKUP_PARENT
, &nd
))
1449 error
= path_walk(to
, &nd
);
1452 dentry
= lookup_create(&nd
, 0);
1453 error
= PTR_ERR(dentry
);
1454 if (!IS_ERR(dentry
)) {
1455 error
= vfs_symlink(nd
.dentry
->d_inode
, dentry
, from
);
1458 up(&nd
.dentry
->d_inode
->i_sem
);
1468 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1470 struct inode
*inode
;
1473 down(&dir
->i_zombie
);
1475 inode
= old_dentry
->d_inode
;
1479 error
= may_create(dir
, new_dentry
);
1484 if (dir
->i_dev
!= inode
->i_dev
)
1488 * A link to an append-only or immutable file cannot be created.
1491 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1493 if (!dir
->i_op
|| !dir
->i_op
->link
)
1497 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1505 * Hardlinks are often used in delicate situations. We avoid
1506 * security-related surprises by not following symlinks on the
1509 * We don't follow them on the oldname either to be compatible
1510 * with linux 2.0, and to avoid hard-linking to directories
1511 * and other special files. --ADM
1513 asmlinkage
long sys_link(const char * oldname
, const char * newname
)
1519 from
= getname(oldname
);
1521 return PTR_ERR(from
);
1522 to
= getname(newname
);
1523 error
= PTR_ERR(to
);
1525 struct dentry
*new_dentry
;
1526 struct nameidata nd
, old_nd
;
1530 if (path_init(from
, LOOKUP_POSITIVE
, &old_nd
))
1531 error
= path_walk(from
, &old_nd
);
1534 if (path_init(to
, LOOKUP_PARENT
, &nd
))
1535 error
= path_walk(to
, &nd
);
1539 if (old_nd
.mnt
!= nd
.mnt
)
1541 new_dentry
= lookup_create(&nd
, 0);
1542 error
= PTR_ERR(new_dentry
);
1543 if (!IS_ERR(new_dentry
)) {
1544 error
= vfs_link(old_nd
.dentry
, nd
.dentry
->d_inode
, new_dentry
);
1547 up(&nd
.dentry
->d_inode
->i_sem
);
1550 path_release(&old_nd
);
1561 * The worst of all namespace operations - renaming directory. "Perverted"
1562 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1564 * a) we can get into loop creation. Check is done in is_subdir().
1565 * b) race potential - two innocent renames can create a loop together.
1566 * That's where 4.4 screws up. Current fix: serialization on
1567 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1569 * c) we have to lock _three_ objects - parents and victim (if it exists).
1570 * And that - after we got ->i_sem on parents (until then we don't know
1571 * whether the target exists at all, let alone whether it is a directory
1572 * or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1573 * on link creation/removal of any kind. And taken (without ->i_sem) on
1574 * directory that will be removed (both in rmdir() and here).
1575 * d) some filesystems don't support opened-but-unlinked directories,
1576 * either because of layout or because they are not ready to deal with
1577 * all cases correctly. The latter will be fixed (taking this sort of
1578 * stuff into VFS), but the former is not going away. Solution: the same
1579 * trick as in rmdir().
1580 * e) conversion from fhandle to dentry may come in the wrong moment - when
1581 * we are removing the target. Solution: we will have to grab ->i_zombie
1582 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1583 * ->i_sem on parents, which works but leads to some truely excessive
1586 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
1587 struct inode
*new_dir
, struct dentry
*new_dentry
)
1590 struct inode
*target
;
1592 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1595 error
= may_delete(old_dir
, old_dentry
, 1);
1599 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1602 if (!new_dentry
->d_inode
)
1603 error
= may_create(new_dir
, new_dentry
);
1605 error
= may_delete(new_dir
, new_dentry
, 1);
1609 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1613 * If we are going to change the parent - check write permissions,
1614 * we'll need to flip '..'.
1616 if (new_dir
!= old_dir
) {
1617 error
= permission(old_dentry
->d_inode
, MAY_WRITE
);
1622 DQUOT_INIT(old_dir
);
1623 DQUOT_INIT(new_dir
);
1624 down(&old_dir
->i_sb
->s_vfs_rename_sem
);
1626 if (is_subdir(new_dentry
, old_dentry
))
1628 target
= new_dentry
->d_inode
;
1629 if (target
) { /* Hastur! Hastur! Hastur! */
1630 triple_down(&old_dir
->i_zombie
,
1633 d_unhash(new_dentry
);
1635 double_down(&old_dir
->i_zombie
,
1636 &new_dir
->i_zombie
);
1637 if (IS_DEADDIR(old_dir
)||IS_DEADDIR(new_dir
))
1639 else if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
1642 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1645 target
->i_flags
|= S_DEAD
;
1646 triple_up(&old_dir
->i_zombie
,
1649 d_rehash(new_dentry
);
1652 double_up(&old_dir
->i_zombie
,
1653 &new_dir
->i_zombie
);
1656 d_move(old_dentry
,new_dentry
);
1658 up(&old_dir
->i_sb
->s_vfs_rename_sem
);
1662 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
1663 struct inode
*new_dir
, struct dentry
*new_dentry
)
1667 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1670 error
= may_delete(old_dir
, old_dentry
, 0);
1674 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1677 if (!new_dentry
->d_inode
)
1678 error
= may_create(new_dir
, new_dentry
);
1680 error
= may_delete(new_dir
, new_dentry
, 0);
1684 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1687 DQUOT_INIT(old_dir
);
1688 DQUOT_INIT(new_dir
);
1689 double_down(&old_dir
->i_zombie
, &new_dir
->i_zombie
);
1690 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
1693 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1694 double_up(&old_dir
->i_zombie
, &new_dir
->i_zombie
);
1697 /* The following d_move() should become unconditional */
1698 if (!(old_dir
->i_sb
->s_flags
& MS_ODD_RENAME
)) {
1699 d_move(old_dentry
, new_dentry
);
1704 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1705 struct inode
*new_dir
, struct dentry
*new_dentry
)
1707 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1708 return vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
1710 return vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
1713 static inline int do_rename(const char * oldname
, const char * newname
)
1716 struct dentry
* old_dir
, * new_dir
;
1717 struct dentry
* old_dentry
, *new_dentry
;
1718 struct nameidata oldnd
, newnd
;
1720 if (path_init(oldname
, LOOKUP_PARENT
, &oldnd
))
1721 error
= path_walk(oldname
, &oldnd
);
1726 if (path_init(newname
, LOOKUP_PARENT
, &newnd
))
1727 error
= path_walk(newname
, &newnd
);
1732 if (oldnd
.mnt
!= newnd
.mnt
)
1735 old_dir
= oldnd
.dentry
;
1737 if (oldnd
.last_type
!= LAST_NORM
)
1740 new_dir
= newnd
.dentry
;
1741 if (newnd
.last_type
!= LAST_NORM
)
1744 double_lock(new_dir
, old_dir
);
1746 old_dentry
= lookup_hash(&oldnd
.last
, old_dir
);
1747 error
= PTR_ERR(old_dentry
);
1748 if (IS_ERR(old_dentry
))
1750 /* source must exist */
1752 if (!old_dentry
->d_inode
)
1754 /* unless the source is a directory trailing slashes give -ENOTDIR */
1755 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
1757 if (oldnd
.last
.name
[oldnd
.last
.len
])
1759 if (newnd
.last
.name
[newnd
.last
.len
])
1762 new_dentry
= lookup_hash(&newnd
.last
, new_dir
);
1763 error
= PTR_ERR(new_dentry
);
1764 if (IS_ERR(new_dentry
))
1767 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
1768 new_dir
->d_inode
, new_dentry
);
1774 double_up(&new_dir
->d_inode
->i_sem
, &old_dir
->d_inode
->i_sem
);
1776 path_release(&newnd
);
1778 path_release(&oldnd
);
1783 asmlinkage
long sys_rename(const char * oldname
, const char * newname
)
1789 from
= getname(oldname
);
1791 return PTR_ERR(from
);
1792 to
= getname(newname
);
1793 error
= PTR_ERR(to
);
1796 error
= do_rename(from
,to
);
1804 int vfs_readlink(struct dentry
*dentry
, char *buffer
, int buflen
, const char *link
)
1808 len
= PTR_ERR(link
);
1813 if (len
> (unsigned) buflen
)
1815 if (copy_to_user(buffer
, link
, len
))
1822 __vfs_follow_link(struct nameidata
*nd
, const char *link
)
1831 if (!walk_init_root(link
, nd
))
1832 /* weird __emul_prefix() stuff did it */
1835 res
= path_walk(link
, nd
);
1837 if (current
->link_count
|| res
|| nd
->last_type
!=LAST_NORM
)
1840 * If it is an iterative symlinks resolution in open_namei() we
1841 * have to copy the last component. And all that crap because of
1842 * bloody create() on broken symlinks. Furrfu...
1847 strcpy(name
, nd
->last
.name
);
1848 nd
->last
.name
= name
;
1854 return PTR_ERR(link
);
1857 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
1859 return __vfs_follow_link(nd
, link
);
1862 /* get the link contents into pagecache */
1863 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
1866 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
1867 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
1872 if (!Page_Uptodate(page
))
1875 return (char*) kmap(page
);
1878 page_cache_release(page
);
1879 return ERR_PTR(-EIO
);
1885 int page_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
1887 struct page
*page
= NULL
;
1888 char *s
= page_getlink(dentry
, &page
);
1889 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
1892 page_cache_release(page
);
1897 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1899 struct page
*page
= NULL
;
1900 char *s
= page_getlink(dentry
, &page
);
1901 int res
= __vfs_follow_link(nd
, s
);
1904 page_cache_release(page
);
1909 struct inode_operations page_symlink_inode_operations
= {
1910 readlink
: page_readlink
,
1911 follow_link
: page_follow_link
,