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).
94 /* In order to reduce some races, while at the same time doing additional
95 * checking and hopefully speeding things up, we copy filenames to the
96 * kernel data space before using them..
98 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
100 static inline int do_getname(const char *filename
, char *page
)
103 unsigned long len
= PAGE_SIZE
;
105 if ((unsigned long) filename
>= TASK_SIZE
) {
106 if (!segment_eq(get_fs(), KERNEL_DS
))
108 } else if (TASK_SIZE
- (unsigned long) filename
< PAGE_SIZE
)
109 len
= TASK_SIZE
- (unsigned long) filename
;
111 retval
= strncpy_from_user((char *)page
, filename
, len
);
115 return -ENAMETOOLONG
;
121 char * getname(const char * filename
)
125 result
= ERR_PTR(-ENOMEM
);
128 int retval
= do_getname(filename
, tmp
);
133 result
= ERR_PTR(retval
);
142 * is used to check for read/write/execute permissions on a file.
143 * We use "fsuid" for this, letting us set arbitrary permissions
144 * for filesystem access without changing the "normal" uids which
145 * are used for other things..
147 int permission(struct inode
* inode
,int mask
)
149 int mode
= inode
->i_mode
;
151 if (inode
->i_op
&& inode
->i_op
->permission
)
152 return inode
->i_op
->permission(inode
, mask
);
153 else if ((mask
& S_IWOTH
) && IS_RDONLY(inode
) &&
154 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
155 return -EROFS
; /* Nobody gets write access to a read-only fs */
156 else if ((mask
& S_IWOTH
) && IS_IMMUTABLE(inode
))
157 return -EACCES
; /* Nobody gets write access to an immutable file */
158 else if (current
->fsuid
== inode
->i_uid
)
160 else if (in_group_p(inode
->i_gid
))
162 if (((mode
& mask
& S_IRWXO
) == mask
) || capable(CAP_DAC_OVERRIDE
))
164 /* read and search access */
165 if ((mask
== S_IROTH
) ||
166 (S_ISDIR(mode
) && !(mask
& ~(S_IROTH
| S_IXOTH
))))
167 if (capable(CAP_DAC_READ_SEARCH
))
173 * get_write_access() gets write permission for a file.
174 * put_write_access() releases this write permission.
175 * This is used for regular files.
176 * We cannot support write (and maybe mmap read-write shared) accesses and
177 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
178 * can have the following values:
179 * 0: no writers, no VM_DENYWRITE mappings
180 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
181 * > 0: (i_writecount) users are writing to the file.
183 * WARNING: as soon as we will move get_write_access(), do_mmap() or
184 * prepare_binfmt() out of the big lock we will need a spinlock protecting
185 * the checks in all 3. For the time being it is not needed.
187 int get_write_access(struct inode
* inode
)
189 if (atomic_read(&inode
->i_writecount
) < 0)
191 atomic_inc(&inode
->i_writecount
);
195 void put_write_access(struct inode
* inode
)
197 atomic_dec(&inode
->i_writecount
);
201 * Internal lookup() using the new generic dcache.
203 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
205 struct dentry
* dentry
= d_lookup(parent
, name
);
207 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
208 if (!dentry
->d_op
->d_revalidate(dentry
, flags
) && !d_invalidate(dentry
)) {
217 * This is called when everything else fails, and we actually have
218 * to go to the low-level filesystem to find out what we should do..
220 * We get the directory semaphore, and after getting that we also
221 * make sure that nobody added the entry to the dcache in the meantime..
223 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, int flags
)
225 struct dentry
* result
;
226 struct inode
*dir
= parent
->d_inode
;
230 * First re-do the cached lookup just in case it was created
231 * while we waited for the directory semaphore..
233 * FIXME! This could use version numbering or similar to
234 * avoid unnecessary cache lookups.
236 result
= d_lookup(parent
, name
);
238 struct dentry
* dentry
= d_alloc(parent
, name
);
239 result
= ERR_PTR(-ENOMEM
);
241 result
= dir
->i_op
->lookup(dir
, dentry
);
252 * Uhhuh! Nasty case: the cache was re-populated while
253 * we waited on the semaphore. Need to revalidate, but
254 * we're going to return this entry regardless (same
255 * as if it was busy).
258 if (result
->d_op
&& result
->d_op
->d_revalidate
)
259 result
->d_op
->d_revalidate(result
, flags
);
263 static inline int do_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
266 if (current
->link_count
>= 32)
268 current
->link_count
++;
269 UPDATE_ATIME(dentry
->d_inode
);
270 err
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
271 current
->link_count
--;
279 static inline int follow_up(struct vfsmount
**mnt
, struct dentry
**base
)
281 struct vfsmount
*parent
=(*mnt
)->mnt_parent
;
282 struct dentry
*dentry
;
285 dentry
=dget((*mnt
)->mnt_mountpoint
);
294 static inline int __follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
296 struct list_head
*p
= (*dentry
)->d_vfsmnt
.next
;
297 while (p
!= &(*dentry
)->d_vfsmnt
) {
298 struct vfsmount
*tmp
;
299 tmp
= list_entry(p
, struct vfsmount
, mnt_clash
);
300 if (tmp
->mnt_parent
== *mnt
) {
302 mntput(tmp
->mnt_parent
);
303 /* tmp holds the mountpoint, so... */
305 *dentry
= dget(tmp
->mnt_root
);
313 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
315 return __follow_down(mnt
,dentry
);
321 * This is the basic name resolution function, turning a pathname
322 * into the final dentry.
324 * We expect 'base' to be positive and a directory.
326 int walk_name(const char * name
, struct nameidata
*nd
)
328 struct dentry
*dentry
;
331 unsigned int lookup_flags
= nd
->flags
;
338 inode
= nd
->dentry
->d_inode
;
339 if (current
->link_count
)
340 lookup_flags
= LOOKUP_FOLLOW
;
342 /* At this point we know we have a real path component. */
348 err
= permission(inode
, MAY_EXEC
);
349 dentry
= ERR_PTR(err
);
354 c
= *(const unsigned char *)name
;
356 hash
= init_name_hash();
359 hash
= partial_name_hash(c
, hash
);
360 c
= *(const unsigned char *)name
;
361 } while (c
&& (c
!= '/'));
362 this.len
= name
- (const char *) this.name
;
363 this.hash
= end_name_hash(hash
);
365 /* remove trailing slashes? */
368 while (*++name
== '/');
370 goto last_with_slashes
;
373 * "." and ".." are special - ".." especially so because it has
374 * to be able to know about the current root directory and
375 * parent relationships.
377 if (this.name
[0] == '.') switch (this.len
) {
381 if (this.name
[1] != '.')
384 if (nd
->dentry
== current
->fs
->root
&&
385 nd
->mnt
== current
->fs
->rootmnt
)
387 if (nd
->dentry
!= nd
->mnt
->mnt_root
) {
388 dentry
= dget(nd
->dentry
->d_parent
);
393 if (!follow_up(&nd
->mnt
, &nd
->dentry
))
396 inode
= nd
->dentry
->d_inode
;
402 * See if the low-level filesystem might want
403 * to use its own hash..
405 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
406 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
410 /* This does the actual lookups.. */
411 dentry
= cached_lookup(nd
->dentry
, &this, LOOKUP_CONTINUE
);
413 dentry
= real_lookup(nd
->dentry
, &this, LOOKUP_CONTINUE
);
414 err
= PTR_ERR(dentry
);
418 /* Check mountpoints.. */
419 while (d_mountpoint(dentry
) && __follow_down(&nd
->mnt
, &dentry
))
423 inode
= dentry
->d_inode
;
430 if (inode
->i_op
->follow_link
) {
431 err
= do_follow_link(dentry
, nd
);
436 inode
= nd
->dentry
->d_inode
;
447 if (!inode
->i_op
->lookup
)
450 /* here ends the main loop */
453 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
455 if (lookup_flags
& LOOKUP_PARENT
)
457 if (this.name
[0] == '.') switch (this.len
) {
461 if (this.name
[1] != '.')
464 if (nd
->dentry
== current
->fs
->root
&&
465 nd
->mnt
== current
->fs
->rootmnt
)
467 if (nd
->dentry
!= nd
->mnt
->mnt_root
) {
468 dentry
= dget(nd
->dentry
->d_parent
);
473 if (!follow_up(&nd
->mnt
, &nd
->dentry
))
476 inode
= nd
->dentry
->d_inode
;
481 if (nd
->dentry
->d_op
&& nd
->dentry
->d_op
->d_hash
) {
482 err
= nd
->dentry
->d_op
->d_hash(nd
->dentry
, &this);
486 dentry
= cached_lookup(nd
->dentry
, &this, 0);
488 dentry
= real_lookup(nd
->dentry
, &this, 0);
489 err
= PTR_ERR(dentry
);
493 while (d_mountpoint(dentry
) && __follow_down(&nd
->mnt
, &dentry
))
495 inode
= dentry
->d_inode
;
496 if ((lookup_flags
& LOOKUP_FOLLOW
)
497 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
498 err
= do_follow_link(dentry
, nd
);
502 inode
= nd
->dentry
->d_inode
;
510 if (lookup_flags
& LOOKUP_DIRECTORY
) {
512 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
518 if (lookup_flags
& (LOOKUP_POSITIVE
|LOOKUP_DIRECTORY
))
523 nd
->last_type
= LAST_NORM
;
524 if (this.name
[0] != '.')
527 nd
->last_type
= LAST_DOT
;
528 else if (this.len
== 2 && this.name
[1] == '.')
529 nd
->last_type
= LAST_DOTDOT
;
542 /* returns 1 if everything is done */
543 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
545 nd
->mnt
= mntget(current
->fs
->altrootmnt
);
546 nd
->dentry
= dget(current
->fs
->altroot
);
547 if (walk_name(name
, nd
))
550 if (!nd
->dentry
->d_inode
) {
551 struct nameidata nd_root
;
552 nd_root
.last_type
= LAST_ROOT
;
553 nd_root
.flags
= nd
->flags
;
554 nd_root
.mnt
= mntget(current
->fs
->rootmnt
);
555 nd_root
.dentry
= dget(current
->fs
->root
);
556 if (walk_name(name
, &nd_root
))
558 if (nd_root
.dentry
->d_inode
) {
561 nd
->dentry
= nd_root
.dentry
;
562 nd
->mnt
= nd_root
.mnt
;
563 nd
->last
= nd_root
.last
;
566 dput(nd_root
.dentry
);
572 void set_fs_altroot(void)
574 char *emul
= __emul_prefix();
576 struct vfsmount
*mnt
= NULL
, *oldmnt
;
577 struct dentry
*dentry
= NULL
, *olddentry
;
579 nd
.mnt
= mntget(current
->fs
->rootmnt
);
580 nd
.dentry
= dget(current
->fs
->root
);
581 nd
.flags
= LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_POSITIVE
;
582 if (walk_name(emul
,&nd
) == 0) {
587 oldmnt
= current
->fs
->altrootmnt
;
588 olddentry
= current
->fs
->altroot
;
589 current
->fs
->altrootmnt
= mnt
;
590 current
->fs
->altroot
= dentry
;
598 walk_init_root(const char *name
, struct nameidata
*nd
)
600 if (current
->fs
->altroot
&& !(nd
->flags
& LOOKUP_NOALT
))
601 if (__emul_lookup_dentry(name
,nd
))
603 nd
->mnt
= mntget(current
->fs
->rootmnt
);
604 nd
->dentry
= dget(current
->fs
->root
);
608 int walk_init(const char *name
,unsigned int flags
,struct nameidata
*nd
)
610 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
613 return walk_init_root(name
,nd
);
614 nd
->mnt
= mntget(current
->fs
->pwdmnt
);
615 nd
->dentry
= dget(current
->fs
->pwd
);
619 struct dentry
* lookup_dentry(const char * name
, unsigned int lookup_flags
)
624 if (walk_init(name
, lookup_flags
, &nd
))
625 err
= walk_name(name
, &nd
);
634 * Restricted form of lookup. Doesn't follow links, single-component only,
635 * needs parent already locked. Doesn't follow mounts.
637 static inline struct dentry
* lookup_hash(struct qstr
*name
, struct dentry
* base
)
639 struct dentry
* dentry
;
643 inode
= base
->d_inode
;
644 err
= permission(inode
, MAY_EXEC
);
645 dentry
= ERR_PTR(err
);
650 * See if the low-level filesystem might want
651 * to use its own hash..
653 if (base
->d_op
&& base
->d_op
->d_hash
) {
654 err
= base
->d_op
->d_hash(base
, name
);
655 dentry
= ERR_PTR(err
);
660 dentry
= cached_lookup(base
, name
, 0);
662 struct dentry
*new = d_alloc(base
, name
);
663 dentry
= ERR_PTR(-ENOMEM
);
666 dentry
= inode
->i_op
->lookup(inode
, new);
676 struct dentry
* lookup_one(const char * name
, struct dentry
* base
)
683 c
= *(const unsigned char *)name
;
687 hash
= init_name_hash();
692 hash
= partial_name_hash(c
, hash
);
693 c
= *(const unsigned char *)name
;
695 this.len
= name
- (const char *) this.name
;
696 this.hash
= end_name_hash(hash
);
698 return lookup_hash(&this, base
);
700 return ERR_PTR(-EACCES
);
706 * is used by most simple commands to get the inode of a specified name.
707 * Open, link etc use their own routines, but this is enough for things
710 * namei exists in two versions: namei/lnamei. The only difference is
711 * that namei follows links, while lnamei does not.
713 struct dentry
* __namei(const char *pathname
, unsigned int lookup_flags
)
716 struct dentry
*dentry
;
718 name
= getname(pathname
);
719 dentry
= (struct dentry
*) name
;
721 dentry
= lookup_dentry(name
,lookup_flags
|LOOKUP_POSITIVE
);
728 * It's inline, so penalty for filesystems that don't use sticky bit is
731 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
733 if (!(dir
->i_mode
& S_ISVTX
))
735 if (inode
->i_uid
== current
->fsuid
)
737 if (dir
->i_uid
== current
->fsuid
)
739 return !capable(CAP_FOWNER
);
743 * Check whether we can remove a link victim from directory dir, check
744 * whether the type of victim is right.
745 * 1. We can't do it if dir is read-only (done in permission())
746 * 2. We should have write and exec permissions on dir
747 * 3. We can't remove anything from append-only dir
748 * 4. We can't do anything with immutable dir (done in permission())
749 * 5. If the sticky bit on dir is set we should either
750 * a. be owner of dir, or
751 * b. be owner of victim, or
752 * c. have CAP_FOWNER capability
753 * 6. If the victim is append-only or immutable we can't do antyhing with
754 * links pointing to it.
755 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
756 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
757 * 9. We can't remove a root or mountpoint.
759 static inline int may_delete(struct inode
*dir
,struct dentry
*victim
, int isdir
)
762 if (!victim
->d_inode
|| victim
->d_parent
->d_inode
!= dir
)
766 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
);
771 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
772 IS_IMMUTABLE(victim
->d_inode
))
775 if (!S_ISDIR(victim
->d_inode
->i_mode
))
779 if (d_mountpoint(victim
))
781 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
786 /* Check whether we can create an object with dentry child in directory
788 * 1. We can't do it if child already exists (open has special treatment for
789 * this case, but since we are inlined it's OK)
790 * 2. We can't do it if dir is read-only (done in permission())
791 * 3. We should have write and exec permissions on dir
792 * 4. We can't do it if dir is immutable (done in permission())
794 static inline int may_create(struct inode
*dir
, struct dentry
*child
) {
799 return permission(dir
,MAY_WRITE
| MAY_EXEC
);
803 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
806 * O_DIRECTORY translates into forcing a directory lookup.
808 static inline int lookup_flags(unsigned int f
)
810 unsigned long retval
= LOOKUP_FOLLOW
;
813 retval
&= ~LOOKUP_FOLLOW
;
815 if ((f
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
))
816 retval
&= ~LOOKUP_FOLLOW
;
819 retval
|= LOOKUP_DIRECTORY
;
824 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
828 mode
&= S_IALLUGO
& ~current
->fs
->umask
;
831 down(&dir
->i_zombie
);
832 error
= may_create(dir
, dentry
);
836 error
= -EACCES
; /* shouldn't it be ENOSYS? */
837 if (!dir
->i_op
|| !dir
->i_op
->create
)
841 error
= dir
->i_op
->create(dir
, dentry
, mode
);
850 * namei for open - this is in fact almost the whole open-routine.
852 * Note that the low bits of "flag" aren't the same as in the open
853 * system call - they are 00 - no permissions needed
854 * 01 - read permission needed
855 * 10 - write permission needed
856 * 11 - read/write permissions needed
857 * which is a lot more logical, and also allows the "no perm" needed
858 * for symlinks (where the permissions are checked later).
860 int open_namei(const char * pathname
, int flag
, int mode
, struct nameidata
*nd
)
862 int acc_mode
, error
= 0;
864 struct dentry
*dentry
;
866 acc_mode
= ACC_MODE(flag
);
867 if (!(flag
& O_CREAT
)) {
868 if (walk_init(pathname
, lookup_flags(flag
), nd
))
869 error
= walk_name(pathname
, nd
);
877 if (walk_init(pathname
, LOOKUP_PARENT
, nd
))
878 error
= walk_name(pathname
, nd
);
882 * It's not obvious that open(".", O_CREAT, foo) should
883 * fail, but it's even less obvious that it should succeed.
884 * Since O_CREAT means an intention to create the thing and
885 * open(2) had never created directories, count it as caller's
886 * luserdom and let him sod off - -EISDIR it is.
889 if (nd
->last_type
!= LAST_NORM
)
892 if (nd
->last
.name
[nd
->last
.len
])
895 dir
= dget(nd
->dentry
);
896 down(&dir
->d_inode
->i_sem
);
898 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
899 error
= PTR_ERR(dentry
);
900 if (IS_ERR(dentry
)) {
901 up(&dir
->d_inode
->i_sem
);
906 if (dentry
->d_inode
) {
907 up(&dir
->d_inode
->i_sem
);
912 if (dentry
->d_inode
->i_op
&&
913 dentry
->d_inode
->i_op
->follow_link
) {
915 * With O_EXCL it would be -EEXIST.
916 * If symlink is a dangling one it's -ENOENT.
917 * Otherwise we open the object it points to.
919 error
= do_follow_link(dentry
, nd
);
929 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
))
932 error
= vfs_create(dir
->d_inode
, dentry
, mode
);
933 /* Don't check for write permission, don't truncate */
945 inode
= dentry
->d_inode
;
950 if (S_ISLNK(inode
->i_mode
))
954 if (S_ISDIR(inode
->i_mode
) && (flag
& FMODE_WRITE
))
957 error
= permission(inode
,acc_mode
);
962 * FIFO's, sockets and device files are special: they don't
963 * actually live on the filesystem itself, and as such you
964 * can write to them even if the filesystem is read-only.
966 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
968 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
976 if (IS_RDONLY(inode
) && (flag
& 2))
980 * An append-only file must be opened in append mode for writing.
983 if (IS_APPEND(inode
)) {
984 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
990 if (flag
& O_TRUNC
) {
991 error
= get_write_access(inode
);
996 * Refuse to truncate files with mandatory locks held on them.
998 error
= locks_verify_locked(inode
);
1002 error
= do_truncate(dentry
, 0);
1004 put_write_access(inode
);
1008 if (flag
& FMODE_WRITE
)
1021 static struct dentry
*lookup_create(const char *name
, int is_dir
)
1023 struct nameidata nd
;
1024 struct dentry
*dentry
;
1026 if (walk_init(name
, LOOKUP_PARENT
, &nd
))
1027 err
= walk_name(name
, &nd
);
1028 dentry
= ERR_PTR(err
);
1031 down(&nd
.dentry
->d_inode
->i_sem
);
1032 dentry
= ERR_PTR(-EEXIST
);
1033 if (nd
.last_type
!= LAST_NORM
)
1035 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1038 if (!is_dir
&& nd
.last
.name
[nd
.last
.len
] && !dentry
->d_inode
)
1047 dentry
= ERR_PTR(-ENOENT
);
1049 up(&nd
.dentry
->d_inode
->i_sem
);
1053 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
1057 mode
&= ~current
->fs
->umask
;
1059 down(&dir
->i_zombie
);
1060 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
1063 error
= may_create(dir
, dentry
);
1068 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
1072 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
1078 struct dentry
* do_mknod(const char * filename
, int mode
, dev_t dev
)
1082 struct dentry
*dentry
, *retval
;
1084 dentry
= lookup_create(filename
, 0);
1088 dir
= dget(dentry
->d_parent
);
1090 error
= vfs_mknod(dir
->d_inode
, dentry
, mode
, dev
);
1092 retval
= ERR_PTR(error
);
1094 retval
= dget(dentry
);
1100 asmlinkage
long sys_mknod(const char * filename
, int mode
, dev_t dev
)
1104 struct dentry
* dentry
, *dir
;
1108 tmp
= getname(filename
);
1110 return PTR_ERR(tmp
);
1113 dentry
= lookup_create(tmp
, 0);
1114 error
= PTR_ERR(dentry
);
1117 dir
= dget(dentry
->d_parent
);
1118 switch (mode
& S_IFMT
) {
1119 case 0: case S_IFREG
:
1120 error
= vfs_create(dir
->d_inode
, dentry
, mode
);
1122 case S_IFCHR
: case S_IFBLK
: case S_IFIFO
: case S_IFSOCK
:
1123 error
= vfs_mknod(dir
->d_inode
, dentry
, mode
, dev
);
1140 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1144 down(&dir
->i_zombie
);
1145 error
= may_create(dir
, dentry
);
1150 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
1154 mode
&= (S_IRWXUGO
|S_ISVTX
) & ~current
->fs
->umask
;
1155 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
1162 asmlinkage
long sys_mkdir(const char * pathname
, int mode
)
1167 tmp
= getname(pathname
);
1168 error
= PTR_ERR(tmp
);
1171 struct dentry
*dentry
;
1174 dentry
= lookup_create(tmp
, 1);
1175 error
= PTR_ERR(dentry
);
1176 if (!IS_ERR(dentry
)) {
1177 dir
= dget(dentry
->d_parent
);
1178 error
= vfs_mkdir(dir
->d_inode
, dentry
, mode
);
1190 * We try to drop the dentry early: we should have
1191 * a usage count of 2 if we're the only user of this
1192 * dentry, and if that is true (possibly after pruning
1193 * the dcache), then we drop the dentry now.
1195 * A low-level filesystem can, if it choses, legally
1198 * if (!d_unhashed(dentry))
1201 * if it cannot handle the case of removing a directory
1202 * that is still in use by something else..
1204 static void d_unhash(struct dentry
*dentry
)
1207 switch (dentry
->d_count
) {
1209 shrink_dcache_parent(dentry
);
1210 if (dentry
->d_count
!= 2)
1217 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1221 error
= may_delete(dir
, dentry
, 1);
1225 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
1230 double_down(&dir
->i_zombie
, &dentry
->d_inode
->i_zombie
);
1232 error
= dir
->i_op
->rmdir(dir
, dentry
);
1234 dentry
->d_inode
->i_flags
|= S_DEAD
;
1235 double_up(&dir
->i_zombie
, &dentry
->d_inode
->i_zombie
);
1241 asmlinkage
long sys_rmdir(const char * pathname
)
1245 struct dentry
*dentry
;
1246 struct nameidata nd
;
1248 name
= getname(pathname
);
1250 return PTR_ERR(name
);
1253 if (walk_init(name
, LOOKUP_PARENT
, &nd
))
1254 error
= walk_name(name
, &nd
);
1258 switch(nd
.last_type
) {
1262 case LAST_ROOT
: case LAST_DOT
:
1266 down(&nd
.dentry
->d_inode
->i_sem
);
1267 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1268 error
= PTR_ERR(dentry
);
1269 if (!IS_ERR(dentry
)) {
1270 error
= vfs_rmdir(nd
.dentry
->d_inode
, dentry
);
1273 up(&nd
.dentry
->d_inode
->i_sem
);
1283 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1287 down(&dir
->i_zombie
);
1288 error
= may_delete(dir
, dentry
, 0);
1291 if (dir
->i_op
&& dir
->i_op
->unlink
) {
1293 error
= dir
->i_op
->unlink(dir
, dentry
);
1300 asmlinkage
long sys_unlink(const char * pathname
)
1304 struct dentry
*dentry
;
1305 struct nameidata nd
;
1307 name
= getname(pathname
);
1309 return PTR_ERR(name
);
1312 if (walk_init(name
, LOOKUP_PARENT
, &nd
))
1313 error
= walk_name(name
, &nd
);
1317 if (nd
.last_type
!= LAST_NORM
)
1319 down(&nd
.dentry
->d_inode
->i_sem
);
1320 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
1321 error
= PTR_ERR(dentry
);
1322 if (!IS_ERR(dentry
)) {
1323 /* Why not before? Because we want correct error value */
1324 if (nd
.last
.name
[nd
.last
.len
])
1326 error
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
1330 up(&nd
.dentry
->d_inode
->i_sem
);
1341 error
= !dentry
->d_inode
? -ENOENT
:
1342 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
1346 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
1350 down(&dir
->i_zombie
);
1351 error
= may_create(dir
, dentry
);
1356 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
1360 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
1367 asmlinkage
long sys_symlink(const char * oldname
, const char * newname
)
1373 from
= getname(oldname
);
1375 return PTR_ERR(from
);
1376 to
= getname(newname
);
1377 error
= PTR_ERR(to
);
1380 struct dentry
*dentry
;
1383 dentry
= lookup_create(to
, 0);
1384 error
= PTR_ERR(dentry
);
1385 if (!IS_ERR(dentry
)) {
1386 dir
= dget(dentry
->d_parent
);
1387 error
= vfs_symlink(dir
->d_inode
, dentry
, from
);
1398 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
1400 struct inode
*inode
;
1403 down(&dir
->i_zombie
);
1405 inode
= old_dentry
->d_inode
;
1409 error
= may_create(dir
, new_dentry
);
1414 if (dir
->i_dev
!= inode
->i_dev
)
1418 * A link to an append-only or immutable file cannot be created.
1421 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1423 if (!dir
->i_op
|| !dir
->i_op
->link
)
1427 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
1435 * Hardlinks are often used in delicate situations. We avoid
1436 * security-related surprises by not following symlinks on the
1439 * We don't follow them on the oldname either to be compatible
1440 * with linux 2.0, and to avoid hard-linking to directories
1441 * and other special files. --ADM
1443 asmlinkage
long sys_link(const char * oldname
, const char * newname
)
1449 from
= getname(oldname
);
1451 return PTR_ERR(from
);
1452 to
= getname(newname
);
1453 error
= PTR_ERR(to
);
1455 struct dentry
*old_dentry
, *new_dentry
, *dir
;
1458 old_dentry
= lookup_dentry(from
, LOOKUP_POSITIVE
);
1459 error
= PTR_ERR(old_dentry
);
1460 if (IS_ERR(old_dentry
))
1463 new_dentry
= lookup_create(to
, 0);
1464 error
= PTR_ERR(new_dentry
);
1465 if (!IS_ERR(new_dentry
)) {
1466 dir
= dget(new_dentry
->d_parent
);
1467 error
= vfs_link(old_dentry
, dir
->d_inode
, new_dentry
);
1482 * The worst of all namespace operations - renaming directory. "Perverted"
1483 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1485 * a) we can get into loop creation. Check is done in is_subdir().
1486 * b) race potential - two innocent renames can create a loop together.
1487 * That's where 4.4 screws up. Current fix: serialization on
1488 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
1490 * c) we have to lock _three_ objects - parents and victim (if it exists).
1491 * And that - after we got ->i_sem on parents (until then we don't know
1492 * whether the target exists at all, let alone whether it is a directory
1493 * or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1494 * on link creation/removal of any kind. And taken (without ->i_sem) on
1495 * directory that will be removed (both in rmdir() and here).
1496 * d) some filesystems don't support opened-but-unlinked directories,
1497 * either because of layout or because they are not ready to deal with
1498 * all cases correctly. The latter will be fixed (taking this sort of
1499 * stuff into VFS), but the former is not going away. Solution: the same
1500 * trick as in rmdir().
1501 * e) conversion from fhandle to dentry may come in the wrong moment - when
1502 * we are removing the target. Solution: we will have to grab ->i_zombie
1503 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1504 * ->i_sem on parents, which works but leads to some truely excessive
1507 int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
1508 struct inode
*new_dir
, struct dentry
*new_dentry
)
1511 struct inode
*target
;
1513 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1516 error
= may_delete(old_dir
, old_dentry
, 1);
1520 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1523 if (!new_dentry
->d_inode
)
1524 error
= may_create(new_dir
, new_dentry
);
1526 error
= may_delete(new_dir
, new_dentry
, 1);
1530 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1534 * If we are going to change the parent - check write permissions,
1535 * we'll need to flip '..'.
1537 if (new_dir
!= old_dir
) {
1538 error
= permission(old_dentry
->d_inode
, MAY_WRITE
);
1543 DQUOT_INIT(old_dir
);
1544 DQUOT_INIT(new_dir
);
1545 down(&old_dir
->i_sb
->s_vfs_rename_sem
);
1547 if (is_subdir(new_dentry
, old_dentry
))
1549 target
= new_dentry
->d_inode
;
1550 if (target
) { /* Hastur! Hastur! Hastur! */
1551 triple_down(&old_dir
->i_zombie
,
1554 d_unhash(new_dentry
);
1556 double_down(&old_dir
->i_zombie
,
1557 &new_dir
->i_zombie
);
1558 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1561 target
->i_flags
|= S_DEAD
;
1562 triple_up(&old_dir
->i_zombie
,
1565 d_rehash(new_dentry
);
1568 double_up(&old_dir
->i_zombie
,
1569 &new_dir
->i_zombie
);
1572 d_move(old_dentry
,new_dentry
);
1574 up(&old_dir
->i_sb
->s_vfs_rename_sem
);
1578 int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
1579 struct inode
*new_dir
, struct dentry
*new_dentry
)
1583 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
1586 error
= may_delete(old_dir
, old_dentry
, 0);
1590 if (new_dir
->i_dev
!= old_dir
->i_dev
)
1593 if (!new_dentry
->d_inode
)
1594 error
= may_create(new_dir
, new_dentry
);
1596 error
= may_delete(new_dir
, new_dentry
, 0);
1600 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
1603 DQUOT_INIT(old_dir
);
1604 DQUOT_INIT(new_dir
);
1605 double_down(&old_dir
->i_zombie
, &new_dir
->i_zombie
);
1606 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1607 double_up(&old_dir
->i_zombie
, &new_dir
->i_zombie
);
1610 /* The following d_move() should become unconditional */
1611 if (!(old_dir
->i_sb
->s_flags
& MS_ODD_RENAME
)) {
1612 d_move(old_dentry
, new_dentry
);
1617 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1618 struct inode
*new_dir
, struct dentry
*new_dentry
)
1620 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
1621 return vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
1623 return vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
1626 static inline int do_rename(const char * oldname
, const char * newname
)
1629 struct dentry
* old_dir
, * new_dir
;
1630 struct dentry
* old_dentry
, *new_dentry
;
1631 struct nameidata oldnd
, newnd
;
1633 if (walk_init(oldname
, LOOKUP_PARENT
, &oldnd
))
1634 error
= walk_name(oldname
, &oldnd
);
1639 if (walk_init(newname
, LOOKUP_PARENT
, &newnd
))
1640 error
= walk_name(newname
, &newnd
);
1645 if (oldnd
.mnt
!= newnd
.mnt
)
1648 old_dir
= oldnd
.dentry
;
1650 if (oldnd
.last_type
!= LAST_NORM
)
1653 new_dir
= newnd
.dentry
;
1654 if (newnd
.last_type
!= LAST_NORM
)
1657 double_lock(new_dir
, old_dir
);
1659 old_dentry
= lookup_hash(&oldnd
.last
, old_dir
);
1660 error
= PTR_ERR(old_dentry
);
1661 if (IS_ERR(old_dentry
))
1663 /* source must exist */
1665 if (!old_dentry
->d_inode
)
1667 /* unless the source is a directory trailing slashes give -ENOTDIR */
1668 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
1670 if (oldnd
.last
.name
[oldnd
.last
.len
])
1672 if (newnd
.last
.name
[newnd
.last
.len
])
1675 new_dentry
= lookup_hash(&newnd
.last
, new_dir
);
1676 error
= PTR_ERR(new_dentry
);
1677 if (IS_ERR(new_dentry
))
1680 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
1681 new_dir
->d_inode
, new_dentry
);
1687 double_up(&new_dir
->d_inode
->i_sem
, &old_dir
->d_inode
->i_sem
);
1698 asmlinkage
long sys_rename(const char * oldname
, const char * newname
)
1704 from
= getname(oldname
);
1706 return PTR_ERR(from
);
1707 to
= getname(newname
);
1708 error
= PTR_ERR(to
);
1711 error
= do_rename(from
,to
);
1719 int vfs_readlink(struct dentry
*dentry
, char *buffer
, int buflen
, const char *link
)
1723 len
= PTR_ERR(link
);
1728 if (len
> (unsigned) buflen
)
1730 if (copy_to_user(buffer
, link
, len
))
1737 __vfs_follow_link(struct nameidata
*nd
, const char *link
)
1745 if (!walk_init_root(link
, nd
))
1746 /* weird __emul_prefix() stuff did it */
1749 return walk_name(link
, nd
);
1754 return PTR_ERR(link
);
1757 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
1759 return __vfs_follow_link(nd
, link
);
1762 /* get the link contents into pagecache */
1763 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
1766 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
1767 page
= read_cache_page(mapping
, 0, (filler_t
*)mapping
->a_ops
->readpage
,
1772 if (!Page_Uptodate(page
))
1775 return (char*) kmap(page
);
1778 page_cache_release(page
);
1779 return ERR_PTR(-EIO
);
1785 int page_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
1787 struct page
*page
= NULL
;
1788 char *s
= page_getlink(dentry
, &page
);
1789 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
1792 page_cache_release(page
);
1797 int page_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1799 struct page
*page
= NULL
;
1800 char *s
= page_getlink(dentry
, &page
);
1801 int res
= __vfs_follow_link(nd
, s
);
1804 page_cache_release(page
);
1809 struct inode_operations page_symlink_inode_operations
= {
1810 readlink
: page_readlink
,
1811 follow_link
: page_follow_link
,