4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/quotaops.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <asm/namei.h>
35 #include <asm/uaccess.h>
37 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
39 /* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
67 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existant name.
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
84 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
87 * [10-Sep-98 Alan Modra] Another symlink change.
90 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
107 * any extra contention...
110 static int __link_path_walk(const char *name
, struct nameidata
*nd
);
112 /* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
119 static int do_getname(const char __user
*filename
, char *page
)
122 unsigned long len
= PATH_MAX
;
124 if (!segment_eq(get_fs(), KERNEL_DS
)) {
125 if ((unsigned long) filename
>= TASK_SIZE
)
127 if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
128 len
= TASK_SIZE
- (unsigned long) filename
;
131 retval
= strncpy_from_user(page
, filename
, len
);
135 return -ENAMETOOLONG
;
141 char * getname(const char __user
* filename
)
145 result
= ERR_PTR(-ENOMEM
);
148 int retval
= do_getname(filename
, tmp
);
153 result
= ERR_PTR(retval
);
156 audit_getname(result
);
160 #ifdef CONFIG_AUDITSYSCALL
161 void putname(const char *name
)
163 if (unlikely(!audit_dummy_context()))
168 EXPORT_SYMBOL(putname
);
173 * generic_permission - check for access rights on a Posix-like filesystem
174 * @inode: inode to check access rights for
175 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
176 * @check_acl: optional callback to check for Posix ACLs
178 * Used to check for read/write/execute permissions on a file.
179 * We use "fsuid" for this, letting us set arbitrary permissions
180 * for filesystem access without changing the "normal" uids which
181 * are used for other things..
183 int generic_permission(struct inode
*inode
, int mask
,
184 int (*check_acl
)(struct inode
*inode
, int mask
))
186 umode_t mode
= inode
->i_mode
;
188 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
190 if (current
->fsuid
== inode
->i_uid
)
193 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
) && check_acl
) {
194 int error
= check_acl(inode
, mask
);
195 if (error
== -EACCES
)
196 goto check_capabilities
;
197 else if (error
!= -EAGAIN
)
201 if (in_group_p(inode
->i_gid
))
206 * If the DACs are ok we don't need any capability check.
208 if ((mask
& ~mode
) == 0)
213 * Read/write DACs are always overridable.
214 * Executable DACs are overridable if at least one exec bit is set.
216 if (!(mask
& MAY_EXEC
) ||
217 (inode
->i_mode
& S_IXUGO
) || S_ISDIR(inode
->i_mode
))
218 if (capable(CAP_DAC_OVERRIDE
))
222 * Searching includes executable on directories, else just read.
224 if (mask
== MAY_READ
|| (S_ISDIR(inode
->i_mode
) && !(mask
& MAY_WRITE
)))
225 if (capable(CAP_DAC_READ_SEARCH
))
231 int permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
234 struct vfsmount
*mnt
= NULL
;
239 if (mask
& MAY_WRITE
) {
240 umode_t mode
= inode
->i_mode
;
243 * Nobody gets write access to a read-only fs.
245 if (IS_RDONLY(inode
) &&
246 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
250 * Nobody gets write access to an immutable file.
252 if (IS_IMMUTABLE(inode
))
256 if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
)) {
258 * MAY_EXEC on regular files is denied if the fs is mounted
259 * with the "noexec" flag.
261 if (mnt
&& (mnt
->mnt_flags
& MNT_NOEXEC
))
265 /* Ordinary permission routines do not understand MAY_APPEND. */
266 if (inode
->i_op
&& inode
->i_op
->permission
) {
269 if (nd
->flags
& LOOKUP_ACCESS
)
271 if (nd
->flags
& LOOKUP_CHDIR
)
273 if (nd
->flags
& LOOKUP_OPEN
)
276 retval
= inode
->i_op
->permission(inode
, mask
| extra
);
279 * Exec permission on a regular file is denied if none
280 * of the execute bits are set.
282 * This check should be done by the ->permission()
285 if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
) &&
286 !(inode
->i_mode
& S_IXUGO
))
290 retval
= generic_permission(inode
, mask
, NULL
);
295 retval
= devcgroup_inode_permission(inode
, mask
);
299 return security_inode_permission(inode
,
300 mask
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
), nd
);
304 * vfs_permission - check for access rights to a given path
305 * @nd: lookup result that describes the path
306 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
308 * Used to check for read/write/execute permissions on a path.
309 * We use "fsuid" for this, letting us set arbitrary permissions
310 * for filesystem access without changing the "normal" uids which
311 * are used for other things.
313 int vfs_permission(struct nameidata
*nd
, int mask
)
315 return permission(nd
->path
.dentry
->d_inode
, mask
, nd
);
319 * file_permission - check for additional access rights to a given file
320 * @file: file to check access rights for
321 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
323 * Used to check for read/write/execute permissions on an already opened
327 * Do not use this function in new code. All access checks should
328 * be done using vfs_permission().
330 int file_permission(struct file
*file
, int mask
)
332 return permission(file
->f_path
.dentry
->d_inode
, mask
, NULL
);
336 * get_write_access() gets write permission for a file.
337 * put_write_access() releases this write permission.
338 * This is used for regular files.
339 * We cannot support write (and maybe mmap read-write shared) accesses and
340 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
341 * can have the following values:
342 * 0: no writers, no VM_DENYWRITE mappings
343 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
344 * > 0: (i_writecount) users are writing to the file.
346 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
347 * except for the cases where we don't hold i_writecount yet. Then we need to
348 * use {get,deny}_write_access() - these functions check the sign and refuse
349 * to do the change if sign is wrong. Exclusion between them is provided by
350 * the inode->i_lock spinlock.
353 int get_write_access(struct inode
* inode
)
355 spin_lock(&inode
->i_lock
);
356 if (atomic_read(&inode
->i_writecount
) < 0) {
357 spin_unlock(&inode
->i_lock
);
360 atomic_inc(&inode
->i_writecount
);
361 spin_unlock(&inode
->i_lock
);
366 int deny_write_access(struct file
* file
)
368 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
370 spin_lock(&inode
->i_lock
);
371 if (atomic_read(&inode
->i_writecount
) > 0) {
372 spin_unlock(&inode
->i_lock
);
375 atomic_dec(&inode
->i_writecount
);
376 spin_unlock(&inode
->i_lock
);
382 * path_get - get a reference to a path
383 * @path: path to get the reference to
385 * Given a path increment the reference count to the dentry and the vfsmount.
387 void path_get(struct path
*path
)
392 EXPORT_SYMBOL(path_get
);
395 * path_put - put a reference to a path
396 * @path: path to put the reference to
398 * Given a path decrement the reference count to the dentry and the vfsmount.
400 void path_put(struct path
*path
)
405 EXPORT_SYMBOL(path_put
);
408 * release_open_intent - free up open intent resources
409 * @nd: pointer to nameidata
411 void release_open_intent(struct nameidata
*nd
)
413 if (nd
->intent
.open
.file
->f_path
.dentry
== NULL
)
414 put_filp(nd
->intent
.open
.file
);
416 fput(nd
->intent
.open
.file
);
419 static inline struct dentry
*
420 do_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
422 int status
= dentry
->d_op
->d_revalidate(dentry
, nd
);
423 if (unlikely(status
<= 0)) {
425 * The dentry failed validation.
426 * If d_revalidate returned 0 attempt to invalidate
427 * the dentry otherwise d_revalidate is asking us
428 * to return a fail status.
431 if (!d_invalidate(dentry
)) {
437 dentry
= ERR_PTR(status
);
444 * Internal lookup() using the new generic dcache.
447 static struct dentry
* cached_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
449 struct dentry
* dentry
= __d_lookup(parent
, name
);
451 /* lockess __d_lookup may fail due to concurrent d_move()
452 * in some unrelated directory, so try with d_lookup
455 dentry
= d_lookup(parent
, name
);
457 if (dentry
&& dentry
->d_op
&& dentry
->d_op
->d_revalidate
)
458 dentry
= do_revalidate(dentry
, nd
);
464 * Short-cut version of permission(), for calling by
465 * path_walk(), when dcache lock is held. Combines parts
466 * of permission() and generic_permission(), and tests ONLY for
467 * MAY_EXEC permission.
469 * If appropriate, check DAC only. If not appropriate, or
470 * short-cut DAC fails, then call permission() to do more
471 * complete permission check.
473 static int exec_permission_lite(struct inode
*inode
,
474 struct nameidata
*nd
)
476 umode_t mode
= inode
->i_mode
;
478 if (inode
->i_op
&& inode
->i_op
->permission
)
481 if (current
->fsuid
== inode
->i_uid
)
483 else if (in_group_p(inode
->i_gid
))
489 if ((inode
->i_mode
& S_IXUGO
) && capable(CAP_DAC_OVERRIDE
))
492 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_OVERRIDE
))
495 if (S_ISDIR(inode
->i_mode
) && capable(CAP_DAC_READ_SEARCH
))
500 return security_inode_permission(inode
, MAY_EXEC
, nd
);
504 * This is called when everything else fails, and we actually have
505 * to go to the low-level filesystem to find out what we should do..
507 * We get the directory semaphore, and after getting that we also
508 * make sure that nobody added the entry to the dcache in the meantime..
511 static struct dentry
* real_lookup(struct dentry
* parent
, struct qstr
* name
, struct nameidata
*nd
)
513 struct dentry
* result
;
514 struct inode
*dir
= parent
->d_inode
;
516 mutex_lock(&dir
->i_mutex
);
518 * First re-do the cached lookup just in case it was created
519 * while we waited for the directory semaphore..
521 * FIXME! This could use version numbering or similar to
522 * avoid unnecessary cache lookups.
524 * The "dcache_lock" is purely to protect the RCU list walker
525 * from concurrent renames at this point (we mustn't get false
526 * negatives from the RCU list walk here, unlike the optimistic
529 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
531 result
= d_lookup(parent
, name
);
533 struct dentry
*dentry
;
535 /* Don't create child dentry for a dead directory. */
536 result
= ERR_PTR(-ENOENT
);
540 dentry
= d_alloc(parent
, name
);
541 result
= ERR_PTR(-ENOMEM
);
543 result
= dir
->i_op
->lookup(dir
, dentry
, nd
);
550 mutex_unlock(&dir
->i_mutex
);
555 * Uhhuh! Nasty case: the cache was re-populated while
556 * we waited on the semaphore. Need to revalidate.
558 mutex_unlock(&dir
->i_mutex
);
559 if (result
->d_op
&& result
->d_op
->d_revalidate
) {
560 result
= do_revalidate(result
, nd
);
562 result
= ERR_PTR(-ENOENT
);
567 static int __emul_lookup_dentry(const char *, struct nameidata
*);
570 static __always_inline
int
571 walk_init_root(const char *name
, struct nameidata
*nd
)
573 struct fs_struct
*fs
= current
->fs
;
575 read_lock(&fs
->lock
);
576 if (fs
->altroot
.dentry
&& !(nd
->flags
& LOOKUP_NOALT
)) {
577 nd
->path
= fs
->altroot
;
578 path_get(&fs
->altroot
);
579 read_unlock(&fs
->lock
);
580 if (__emul_lookup_dentry(name
,nd
))
582 read_lock(&fs
->lock
);
586 read_unlock(&fs
->lock
);
591 * Wrapper to retry pathname resolution whenever the underlying
592 * file system returns an ESTALE.
594 * Retry the whole path once, forcing real lookup requests
595 * instead of relying on the dcache.
597 static __always_inline
int link_path_walk(const char *name
, struct nameidata
*nd
)
599 struct path save
= nd
->path
;
602 /* make sure the stuff we saved doesn't go away */
605 result
= __link_path_walk(name
, nd
);
606 if (result
== -ESTALE
) {
607 /* nd->path had been dropped */
610 nd
->flags
|= LOOKUP_REVAL
;
611 result
= __link_path_walk(name
, nd
);
619 static __always_inline
int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
628 if (!walk_init_root(link
, nd
))
629 /* weird __emul_prefix() stuff did it */
632 res
= link_path_walk(link
, nd
);
634 if (nd
->depth
|| res
|| nd
->last_type
!=LAST_NORM
)
637 * If it is an iterative symlinks resolution in open_namei() we
638 * have to copy the last component. And all that crap because of
639 * bloody create() on broken symlinks. Furrfu...
642 if (unlikely(!name
)) {
646 strcpy(name
, nd
->last
.name
);
647 nd
->last
.name
= name
;
651 return PTR_ERR(link
);
654 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
657 if (path
->mnt
!= nd
->path
.mnt
)
661 static inline void path_to_nameidata(struct path
*path
, struct nameidata
*nd
)
663 dput(nd
->path
.dentry
);
664 if (nd
->path
.mnt
!= path
->mnt
)
665 mntput(nd
->path
.mnt
);
666 nd
->path
.mnt
= path
->mnt
;
667 nd
->path
.dentry
= path
->dentry
;
670 static __always_inline
int __do_follow_link(struct path
*path
, struct nameidata
*nd
)
674 struct dentry
*dentry
= path
->dentry
;
676 touch_atime(path
->mnt
, dentry
);
677 nd_set_link(nd
, NULL
);
679 if (path
->mnt
!= nd
->path
.mnt
) {
680 path_to_nameidata(path
, nd
);
684 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
685 error
= PTR_ERR(cookie
);
686 if (!IS_ERR(cookie
)) {
687 char *s
= nd_get_link(nd
);
690 error
= __vfs_follow_link(nd
, s
);
691 if (dentry
->d_inode
->i_op
->put_link
)
692 dentry
->d_inode
->i_op
->put_link(dentry
, nd
, cookie
);
700 * This limits recursive symlink follows to 8, while
701 * limiting consecutive symlinks to 40.
703 * Without that kind of total limit, nasty chains of consecutive
704 * symlinks can cause almost arbitrarily long lookups.
706 static inline int do_follow_link(struct path
*path
, struct nameidata
*nd
)
709 if (current
->link_count
>= MAX_NESTED_LINKS
)
711 if (current
->total_link_count
>= 40)
713 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
715 err
= security_inode_follow_link(path
->dentry
, nd
);
718 current
->link_count
++;
719 current
->total_link_count
++;
721 err
= __do_follow_link(path
, nd
);
722 current
->link_count
--;
726 path_put_conditional(path
, nd
);
731 int follow_up(struct vfsmount
**mnt
, struct dentry
**dentry
)
733 struct vfsmount
*parent
;
734 struct dentry
*mountpoint
;
735 spin_lock(&vfsmount_lock
);
736 parent
=(*mnt
)->mnt_parent
;
737 if (parent
== *mnt
) {
738 spin_unlock(&vfsmount_lock
);
742 mountpoint
=dget((*mnt
)->mnt_mountpoint
);
743 spin_unlock(&vfsmount_lock
);
745 *dentry
= mountpoint
;
751 /* no need for dcache_lock, as serialization is taken care in
754 static int __follow_mount(struct path
*path
)
757 while (d_mountpoint(path
->dentry
)) {
758 struct vfsmount
*mounted
= lookup_mnt(path
->mnt
, path
->dentry
);
765 path
->dentry
= dget(mounted
->mnt_root
);
771 static void follow_mount(struct vfsmount
**mnt
, struct dentry
**dentry
)
773 while (d_mountpoint(*dentry
)) {
774 struct vfsmount
*mounted
= lookup_mnt(*mnt
, *dentry
);
780 *dentry
= dget(mounted
->mnt_root
);
784 /* no need for dcache_lock, as serialization is taken care in
787 int follow_down(struct vfsmount
**mnt
, struct dentry
**dentry
)
789 struct vfsmount
*mounted
;
791 mounted
= lookup_mnt(*mnt
, *dentry
);
796 *dentry
= dget(mounted
->mnt_root
);
802 static __always_inline
void follow_dotdot(struct nameidata
*nd
)
804 struct fs_struct
*fs
= current
->fs
;
807 struct vfsmount
*parent
;
808 struct dentry
*old
= nd
->path
.dentry
;
810 read_lock(&fs
->lock
);
811 if (nd
->path
.dentry
== fs
->root
.dentry
&&
812 nd
->path
.mnt
== fs
->root
.mnt
) {
813 read_unlock(&fs
->lock
);
816 read_unlock(&fs
->lock
);
817 spin_lock(&dcache_lock
);
818 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
819 nd
->path
.dentry
= dget(nd
->path
.dentry
->d_parent
);
820 spin_unlock(&dcache_lock
);
824 spin_unlock(&dcache_lock
);
825 spin_lock(&vfsmount_lock
);
826 parent
= nd
->path
.mnt
->mnt_parent
;
827 if (parent
== nd
->path
.mnt
) {
828 spin_unlock(&vfsmount_lock
);
832 nd
->path
.dentry
= dget(nd
->path
.mnt
->mnt_mountpoint
);
833 spin_unlock(&vfsmount_lock
);
835 mntput(nd
->path
.mnt
);
836 nd
->path
.mnt
= parent
;
838 follow_mount(&nd
->path
.mnt
, &nd
->path
.dentry
);
842 * It's more convoluted than I'd like it to be, but... it's still fairly
843 * small and for now I'd prefer to have fast path as straight as possible.
844 * It _is_ time-critical.
846 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
849 struct vfsmount
*mnt
= nd
->path
.mnt
;
850 struct dentry
*dentry
= __d_lookup(nd
->path
.dentry
, name
);
854 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
)
855 goto need_revalidate
;
858 path
->dentry
= dentry
;
859 __follow_mount(path
);
863 dentry
= real_lookup(nd
->path
.dentry
, name
, nd
);
869 dentry
= do_revalidate(dentry
, nd
);
877 return PTR_ERR(dentry
);
882 * This is the basic name resolution function, turning a pathname into
883 * the final dentry. We expect 'base' to be positive and a directory.
885 * Returns 0 and nd will have valid dentry and mnt on success.
886 * Returns error and drops reference to input namei data on failure.
888 static int __link_path_walk(const char *name
, struct nameidata
*nd
)
893 unsigned int lookup_flags
= nd
->flags
;
900 inode
= nd
->path
.dentry
->d_inode
;
902 lookup_flags
= LOOKUP_FOLLOW
| (nd
->flags
& LOOKUP_CONTINUE
);
904 /* At this point we know we have a real path component. */
910 nd
->flags
|= LOOKUP_CONTINUE
;
911 err
= exec_permission_lite(inode
, nd
);
913 err
= vfs_permission(nd
, MAY_EXEC
);
918 c
= *(const unsigned char *)name
;
920 hash
= init_name_hash();
923 hash
= partial_name_hash(c
, hash
);
924 c
= *(const unsigned char *)name
;
925 } while (c
&& (c
!= '/'));
926 this.len
= name
- (const char *) this.name
;
927 this.hash
= end_name_hash(hash
);
929 /* remove trailing slashes? */
932 while (*++name
== '/');
934 goto last_with_slashes
;
937 * "." and ".." are special - ".." especially so because it has
938 * to be able to know about the current root directory and
939 * parent relationships.
941 if (this.name
[0] == '.') switch (this.len
) {
945 if (this.name
[1] != '.')
948 inode
= nd
->path
.dentry
->d_inode
;
954 * See if the low-level filesystem might want
955 * to use its own hash..
957 if (nd
->path
.dentry
->d_op
&& nd
->path
.dentry
->d_op
->d_hash
) {
958 err
= nd
->path
.dentry
->d_op
->d_hash(nd
->path
.dentry
,
963 /* This does the actual lookups.. */
964 err
= do_lookup(nd
, &this, &next
);
969 inode
= next
.dentry
->d_inode
;
976 if (inode
->i_op
->follow_link
) {
977 err
= do_follow_link(&next
, nd
);
981 inode
= nd
->path
.dentry
->d_inode
;
988 path_to_nameidata(&next
, nd
);
990 if (!inode
->i_op
->lookup
)
993 /* here ends the main loop */
996 lookup_flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
998 /* Clear LOOKUP_CONTINUE iff it was previously unset */
999 nd
->flags
&= lookup_flags
| ~LOOKUP_CONTINUE
;
1000 if (lookup_flags
& LOOKUP_PARENT
)
1002 if (this.name
[0] == '.') switch (this.len
) {
1006 if (this.name
[1] != '.')
1009 inode
= nd
->path
.dentry
->d_inode
;
1014 if (nd
->path
.dentry
->d_op
&& nd
->path
.dentry
->d_op
->d_hash
) {
1015 err
= nd
->path
.dentry
->d_op
->d_hash(nd
->path
.dentry
,
1020 err
= do_lookup(nd
, &this, &next
);
1023 inode
= next
.dentry
->d_inode
;
1024 if ((lookup_flags
& LOOKUP_FOLLOW
)
1025 && inode
&& inode
->i_op
&& inode
->i_op
->follow_link
) {
1026 err
= do_follow_link(&next
, nd
);
1029 inode
= nd
->path
.dentry
->d_inode
;
1031 path_to_nameidata(&next
, nd
);
1035 if (lookup_flags
& LOOKUP_DIRECTORY
) {
1037 if (!inode
->i_op
|| !inode
->i_op
->lookup
)
1043 nd
->last_type
= LAST_NORM
;
1044 if (this.name
[0] != '.')
1047 nd
->last_type
= LAST_DOT
;
1048 else if (this.len
== 2 && this.name
[1] == '.')
1049 nd
->last_type
= LAST_DOTDOT
;
1054 * We bypassed the ordinary revalidation routines.
1055 * We may need to check the cached dentry for staleness.
1057 if (nd
->path
.dentry
&& nd
->path
.dentry
->d_sb
&&
1058 (nd
->path
.dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)) {
1060 /* Note: we do not d_invalidate() */
1061 if (!nd
->path
.dentry
->d_op
->d_revalidate(
1062 nd
->path
.dentry
, nd
))
1068 path_put_conditional(&next
, nd
);
1071 path_put(&nd
->path
);
1076 static int path_walk(const char *name
, struct nameidata
*nd
)
1078 current
->total_link_count
= 0;
1079 return link_path_walk(name
, nd
);
1083 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1084 * everything is done. Returns 0 and drops input nd, if lookup failed;
1086 static int __emul_lookup_dentry(const char *name
, struct nameidata
*nd
)
1088 if (path_walk(name
, nd
))
1089 return 0; /* something went wrong... */
1091 if (!nd
->path
.dentry
->d_inode
||
1092 S_ISDIR(nd
->path
.dentry
->d_inode
->i_mode
)) {
1093 struct path old_path
= nd
->path
;
1094 struct qstr last
= nd
->last
;
1095 int last_type
= nd
->last_type
;
1096 struct fs_struct
*fs
= current
->fs
;
1099 * NAME was not found in alternate root or it's a directory.
1100 * Try to find it in the normal root:
1102 nd
->last_type
= LAST_ROOT
;
1103 read_lock(&fs
->lock
);
1104 nd
->path
= fs
->root
;
1105 path_get(&fs
->root
);
1106 read_unlock(&fs
->lock
);
1107 if (path_walk(name
, nd
) == 0) {
1108 if (nd
->path
.dentry
->d_inode
) {
1109 path_put(&old_path
);
1112 path_put(&nd
->path
);
1114 nd
->path
= old_path
;
1116 nd
->last_type
= last_type
;
1121 void set_fs_altroot(void)
1123 char *emul
= __emul_prefix();
1124 struct nameidata nd
;
1125 struct path path
= {}, old_path
;
1127 struct fs_struct
*fs
= current
->fs
;
1131 err
= path_lookup(emul
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_NOALT
, &nd
);
1135 write_lock(&fs
->lock
);
1136 old_path
= fs
->altroot
;
1138 write_unlock(&fs
->lock
);
1139 if (old_path
.dentry
)
1140 path_put(&old_path
);
1143 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1144 static int do_path_lookup(int dfd
, const char *name
,
1145 unsigned int flags
, struct nameidata
*nd
)
1150 struct fs_struct
*fs
= current
->fs
;
1152 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1157 read_lock(&fs
->lock
);
1158 if (fs
->altroot
.dentry
&& !(nd
->flags
& LOOKUP_NOALT
)) {
1159 nd
->path
= fs
->altroot
;
1160 path_get(&fs
->altroot
);
1161 read_unlock(&fs
->lock
);
1162 if (__emul_lookup_dentry(name
,nd
))
1163 goto out
; /* found in altroot */
1164 read_lock(&fs
->lock
);
1166 nd
->path
= fs
->root
;
1167 path_get(&fs
->root
);
1168 read_unlock(&fs
->lock
);
1169 } else if (dfd
== AT_FDCWD
) {
1170 read_lock(&fs
->lock
);
1173 read_unlock(&fs
->lock
);
1175 struct dentry
*dentry
;
1177 file
= fget_light(dfd
, &fput_needed
);
1182 dentry
= file
->f_path
.dentry
;
1185 if (!S_ISDIR(dentry
->d_inode
->i_mode
))
1188 retval
= file_permission(file
, MAY_EXEC
);
1192 nd
->path
= file
->f_path
;
1193 path_get(&file
->f_path
);
1195 fput_light(file
, fput_needed
);
1198 retval
= path_walk(name
, nd
);
1200 if (unlikely(!retval
&& !audit_dummy_context() && nd
->path
.dentry
&&
1201 nd
->path
.dentry
->d_inode
))
1202 audit_inode(name
, nd
->path
.dentry
);
1207 fput_light(file
, fput_needed
);
1211 int path_lookup(const char *name
, unsigned int flags
,
1212 struct nameidata
*nd
)
1214 return do_path_lookup(AT_FDCWD
, name
, flags
, nd
);
1218 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1219 * @dentry: pointer to dentry of the base directory
1220 * @mnt: pointer to vfs mount of the base directory
1221 * @name: pointer to file name
1222 * @flags: lookup flags
1223 * @nd: pointer to nameidata
1225 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
1226 const char *name
, unsigned int flags
,
1227 struct nameidata
*nd
)
1231 /* same as do_path_lookup */
1232 nd
->last_type
= LAST_ROOT
;
1236 nd
->path
.dentry
= dentry
;
1238 path_get(&nd
->path
);
1240 retval
= path_walk(name
, nd
);
1241 if (unlikely(!retval
&& !audit_dummy_context() && nd
->path
.dentry
&&
1242 nd
->path
.dentry
->d_inode
))
1243 audit_inode(name
, nd
->path
.dentry
);
1249 static int __path_lookup_intent_open(int dfd
, const char *name
,
1250 unsigned int lookup_flags
, struct nameidata
*nd
,
1251 int open_flags
, int create_mode
)
1253 struct file
*filp
= get_empty_filp();
1258 nd
->intent
.open
.file
= filp
;
1259 nd
->intent
.open
.flags
= open_flags
;
1260 nd
->intent
.open
.create_mode
= create_mode
;
1261 err
= do_path_lookup(dfd
, name
, lookup_flags
|LOOKUP_OPEN
, nd
);
1262 if (IS_ERR(nd
->intent
.open
.file
)) {
1264 err
= PTR_ERR(nd
->intent
.open
.file
);
1265 path_put(&nd
->path
);
1267 } else if (err
!= 0)
1268 release_open_intent(nd
);
1273 * path_lookup_open - lookup a file path with open intent
1274 * @dfd: the directory to use as base, or AT_FDCWD
1275 * @name: pointer to file name
1276 * @lookup_flags: lookup intent flags
1277 * @nd: pointer to nameidata
1278 * @open_flags: open intent flags
1280 int path_lookup_open(int dfd
, const char *name
, unsigned int lookup_flags
,
1281 struct nameidata
*nd
, int open_flags
)
1283 return __path_lookup_intent_open(dfd
, name
, lookup_flags
, nd
,
1288 * path_lookup_create - lookup a file path with open + create intent
1289 * @dfd: the directory to use as base, or AT_FDCWD
1290 * @name: pointer to file name
1291 * @lookup_flags: lookup intent flags
1292 * @nd: pointer to nameidata
1293 * @open_flags: open intent flags
1294 * @create_mode: create intent flags
1296 static int path_lookup_create(int dfd
, const char *name
,
1297 unsigned int lookup_flags
, struct nameidata
*nd
,
1298 int open_flags
, int create_mode
)
1300 return __path_lookup_intent_open(dfd
, name
, lookup_flags
|LOOKUP_CREATE
,
1301 nd
, open_flags
, create_mode
);
1304 int __user_path_lookup_open(const char __user
*name
, unsigned int lookup_flags
,
1305 struct nameidata
*nd
, int open_flags
)
1307 char *tmp
= getname(name
);
1308 int err
= PTR_ERR(tmp
);
1311 err
= __path_lookup_intent_open(AT_FDCWD
, tmp
, lookup_flags
, nd
, open_flags
, 0);
1317 static struct dentry
*__lookup_hash(struct qstr
*name
,
1318 struct dentry
*base
, struct nameidata
*nd
)
1320 struct dentry
*dentry
;
1321 struct inode
*inode
;
1324 inode
= base
->d_inode
;
1327 * See if the low-level filesystem might want
1328 * to use its own hash..
1330 if (base
->d_op
&& base
->d_op
->d_hash
) {
1331 err
= base
->d_op
->d_hash(base
, name
);
1332 dentry
= ERR_PTR(err
);
1337 dentry
= cached_lookup(base
, name
, nd
);
1341 /* Don't create child dentry for a dead directory. */
1342 dentry
= ERR_PTR(-ENOENT
);
1343 if (IS_DEADDIR(inode
))
1346 new = d_alloc(base
, name
);
1347 dentry
= ERR_PTR(-ENOMEM
);
1350 dentry
= inode
->i_op
->lookup(inode
, new, nd
);
1361 * Restricted form of lookup. Doesn't follow links, single-component only,
1362 * needs parent already locked. Doesn't follow mounts.
1365 static struct dentry
*lookup_hash(struct nameidata
*nd
)
1369 err
= permission(nd
->path
.dentry
->d_inode
, MAY_EXEC
, nd
);
1371 return ERR_PTR(err
);
1372 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
);
1375 static int __lookup_one_len(const char *name
, struct qstr
*this,
1376 struct dentry
*base
, int len
)
1386 hash
= init_name_hash();
1388 c
= *(const unsigned char *)name
++;
1389 if (c
== '/' || c
== '\0')
1391 hash
= partial_name_hash(c
, hash
);
1393 this->hash
= end_name_hash(hash
);
1398 * lookup_one_len - filesystem helper to lookup single pathname component
1399 * @name: pathname component to lookup
1400 * @base: base directory to lookup from
1401 * @len: maximum length @len should be interpreted to
1403 * Note that this routine is purely a helper for filesystem usage and should
1404 * not be called by generic code. Also note that by using this function the
1405 * nameidata argument is passed to the filesystem methods and a filesystem
1406 * using this helper needs to be prepared for that.
1408 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
1413 err
= __lookup_one_len(name
, &this, base
, len
);
1415 return ERR_PTR(err
);
1417 err
= permission(base
->d_inode
, MAY_EXEC
, NULL
);
1419 return ERR_PTR(err
);
1420 return __lookup_hash(&this, base
, NULL
);
1424 * lookup_one_noperm - bad hack for sysfs
1425 * @name: pathname component to lookup
1426 * @base: base directory to lookup from
1428 * This is a variant of lookup_one_len that doesn't perform any permission
1429 * checks. It's a horrible hack to work around the braindead sysfs
1430 * architecture and should not be used anywhere else.
1432 * DON'T USE THIS FUNCTION EVER, thanks.
1434 struct dentry
*lookup_one_noperm(const char *name
, struct dentry
*base
)
1439 err
= __lookup_one_len(name
, &this, base
, strlen(name
));
1441 return ERR_PTR(err
);
1442 return __lookup_hash(&this, base
, NULL
);
1445 int __user_walk_fd(int dfd
, const char __user
*name
, unsigned flags
,
1446 struct nameidata
*nd
)
1448 char *tmp
= getname(name
);
1449 int err
= PTR_ERR(tmp
);
1452 err
= do_path_lookup(dfd
, tmp
, flags
, nd
);
1458 int __user_walk(const char __user
*name
, unsigned flags
, struct nameidata
*nd
)
1460 return __user_walk_fd(AT_FDCWD
, name
, flags
, nd
);
1464 * It's inline, so penalty for filesystems that don't use sticky bit is
1467 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1469 if (!(dir
->i_mode
& S_ISVTX
))
1471 if (inode
->i_uid
== current
->fsuid
)
1473 if (dir
->i_uid
== current
->fsuid
)
1475 return !capable(CAP_FOWNER
);
1479 * Check whether we can remove a link victim from directory dir, check
1480 * whether the type of victim is right.
1481 * 1. We can't do it if dir is read-only (done in permission())
1482 * 2. We should have write and exec permissions on dir
1483 * 3. We can't remove anything from append-only dir
1484 * 4. We can't do anything with immutable dir (done in permission())
1485 * 5. If the sticky bit on dir is set we should either
1486 * a. be owner of dir, or
1487 * b. be owner of victim, or
1488 * c. have CAP_FOWNER capability
1489 * 6. If the victim is append-only or immutable we can't do antyhing with
1490 * links pointing to it.
1491 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1492 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1493 * 9. We can't remove a root or mountpoint.
1494 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1495 * nfs_async_unlink().
1497 static int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1501 if (!victim
->d_inode
)
1504 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1505 audit_inode_child(victim
->d_name
.name
, victim
, dir
);
1507 error
= permission(dir
,MAY_WRITE
| MAY_EXEC
, NULL
);
1512 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1513 IS_IMMUTABLE(victim
->d_inode
))
1516 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1518 if (IS_ROOT(victim
))
1520 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1522 if (IS_DEADDIR(dir
))
1524 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1529 /* Check whether we can create an object with dentry child in directory
1531 * 1. We can't do it if child already exists (open has special treatment for
1532 * this case, but since we are inlined it's OK)
1533 * 2. We can't do it if dir is read-only (done in permission())
1534 * 3. We should have write and exec permissions on dir
1535 * 4. We can't do it if dir is immutable (done in permission())
1537 static inline int may_create(struct inode
*dir
, struct dentry
*child
,
1538 struct nameidata
*nd
)
1542 if (IS_DEADDIR(dir
))
1544 return permission(dir
,MAY_WRITE
| MAY_EXEC
, nd
);
1548 * O_DIRECTORY translates into forcing a directory lookup.
1550 static inline int lookup_flags(unsigned int f
)
1552 unsigned long retval
= LOOKUP_FOLLOW
;
1555 retval
&= ~LOOKUP_FOLLOW
;
1557 if (f
& O_DIRECTORY
)
1558 retval
|= LOOKUP_DIRECTORY
;
1564 * p1 and p2 should be directories on the same fs.
1566 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1571 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1575 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1577 for (p
= p1
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1578 if (p
->d_parent
== p2
) {
1579 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1580 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1585 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1586 if (p
->d_parent
== p1
) {
1587 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1588 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1593 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1594 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1598 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1600 mutex_unlock(&p1
->d_inode
->i_mutex
);
1602 mutex_unlock(&p2
->d_inode
->i_mutex
);
1603 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1607 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1608 struct nameidata
*nd
)
1610 int error
= may_create(dir
, dentry
, nd
);
1615 if (!dir
->i_op
|| !dir
->i_op
->create
)
1616 return -EACCES
; /* shouldn't it be ENOSYS? */
1619 error
= security_inode_create(dir
, dentry
, mode
);
1623 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1625 fsnotify_create(dir
, dentry
);
1629 int may_open(struct nameidata
*nd
, int acc_mode
, int flag
)
1631 struct dentry
*dentry
= nd
->path
.dentry
;
1632 struct inode
*inode
= dentry
->d_inode
;
1638 if (S_ISLNK(inode
->i_mode
))
1641 if (S_ISDIR(inode
->i_mode
) && (acc_mode
& MAY_WRITE
))
1645 * FIFO's, sockets and device files are special: they don't
1646 * actually live on the filesystem itself, and as such you
1647 * can write to them even if the filesystem is read-only.
1649 if (S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
1651 } else if (S_ISBLK(inode
->i_mode
) || S_ISCHR(inode
->i_mode
)) {
1652 if (nd
->path
.mnt
->mnt_flags
& MNT_NODEV
)
1658 error
= vfs_permission(nd
, acc_mode
);
1662 * An append-only file must be opened in append mode for writing.
1664 if (IS_APPEND(inode
)) {
1665 if ((flag
& FMODE_WRITE
) && !(flag
& O_APPEND
))
1671 /* O_NOATIME can only be set by the owner or superuser */
1672 if (flag
& O_NOATIME
)
1673 if (!is_owner_or_cap(inode
))
1677 * Ensure there are no outstanding leases on the file.
1679 error
= break_lease(inode
, flag
);
1683 if (flag
& O_TRUNC
) {
1684 error
= get_write_access(inode
);
1689 * Refuse to truncate files with mandatory locks held on them.
1691 error
= locks_verify_locked(inode
);
1695 error
= do_truncate(dentry
, 0,
1696 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
1699 put_write_access(inode
);
1703 if (flag
& FMODE_WRITE
)
1710 * Be careful about ever adding any more callers of this
1711 * function. Its flags must be in the namei format, not
1712 * what get passed to sys_open().
1714 static int __open_namei_create(struct nameidata
*nd
, struct path
*path
,
1718 struct dentry
*dir
= nd
->path
.dentry
;
1720 if (!IS_POSIXACL(dir
->d_inode
))
1721 mode
&= ~current
->fs
->umask
;
1722 error
= vfs_create(dir
->d_inode
, path
->dentry
, mode
, nd
);
1723 mutex_unlock(&dir
->d_inode
->i_mutex
);
1724 dput(nd
->path
.dentry
);
1725 nd
->path
.dentry
= path
->dentry
;
1728 /* Don't check for write permission, don't truncate */
1729 return may_open(nd
, 0, flag
& ~O_TRUNC
);
1733 * Note that while the flag value (low two bits) for sys_open means:
1738 * it is changed into
1739 * 00 - no permissions needed
1740 * 01 - read-permission
1741 * 10 - write-permission
1743 * for the internal routines (ie open_namei()/follow_link() etc)
1744 * This is more logical, and also allows the 00 "no perm needed"
1745 * to be used for symlinks (where the permissions are checked
1749 static inline int open_to_namei_flags(int flag
)
1751 if ((flag
+1) & O_ACCMODE
)
1756 static int open_will_write_to_fs(int flag
, struct inode
*inode
)
1759 * We'll never write to the fs underlying
1762 if (special_file(inode
->i_mode
))
1764 return (flag
& O_TRUNC
);
1768 * Note that the low bits of the passed in "open_flag"
1769 * are not the same as in the local variable "flag". See
1770 * open_to_namei_flags() for more details.
1772 struct file
*do_filp_open(int dfd
, const char *pathname
,
1773 int open_flag
, int mode
)
1776 struct nameidata nd
;
1777 int acc_mode
, error
;
1782 int flag
= open_to_namei_flags(open_flag
);
1784 acc_mode
= ACC_MODE(flag
);
1786 /* O_TRUNC implies we need access checks for write permissions */
1788 acc_mode
|= MAY_WRITE
;
1790 /* Allow the LSM permission hook to distinguish append
1791 access from general write access. */
1792 if (flag
& O_APPEND
)
1793 acc_mode
|= MAY_APPEND
;
1796 * The simplest case - just a plain lookup.
1798 if (!(flag
& O_CREAT
)) {
1799 error
= path_lookup_open(dfd
, pathname
, lookup_flags(flag
),
1802 return ERR_PTR(error
);
1807 * Create - we need to know the parent.
1809 error
= path_lookup_create(dfd
, pathname
, LOOKUP_PARENT
,
1812 return ERR_PTR(error
);
1815 * We have the parent and last component. First of all, check
1816 * that we are not asked to creat(2) an obvious directory - that
1820 if (nd
.last_type
!= LAST_NORM
|| nd
.last
.name
[nd
.last
.len
])
1823 dir
= nd
.path
.dentry
;
1824 nd
.flags
&= ~LOOKUP_PARENT
;
1825 mutex_lock(&dir
->d_inode
->i_mutex
);
1826 path
.dentry
= lookup_hash(&nd
);
1827 path
.mnt
= nd
.path
.mnt
;
1830 error
= PTR_ERR(path
.dentry
);
1831 if (IS_ERR(path
.dentry
)) {
1832 mutex_unlock(&dir
->d_inode
->i_mutex
);
1836 if (IS_ERR(nd
.intent
.open
.file
)) {
1837 error
= PTR_ERR(nd
.intent
.open
.file
);
1838 goto exit_mutex_unlock
;
1841 /* Negative dentry, just create the file */
1842 if (!path
.dentry
->d_inode
) {
1844 * This write is needed to ensure that a
1845 * ro->rw transition does not occur between
1846 * the time when the file is created and when
1847 * a permanent write count is taken through
1848 * the 'struct file' in nameidata_to_filp().
1850 error
= mnt_want_write(nd
.path
.mnt
);
1852 goto exit_mutex_unlock
;
1853 error
= __open_namei_create(&nd
, &path
, flag
, mode
);
1855 mnt_drop_write(nd
.path
.mnt
);
1858 filp
= nameidata_to_filp(&nd
, open_flag
);
1859 mnt_drop_write(nd
.path
.mnt
);
1864 * It already exists.
1866 mutex_unlock(&dir
->d_inode
->i_mutex
);
1867 audit_inode(pathname
, path
.dentry
);
1873 if (__follow_mount(&path
)) {
1875 if (flag
& O_NOFOLLOW
)
1880 if (!path
.dentry
->d_inode
)
1882 if (path
.dentry
->d_inode
->i_op
&& path
.dentry
->d_inode
->i_op
->follow_link
)
1885 path_to_nameidata(&path
, &nd
);
1887 if (path
.dentry
->d_inode
&& S_ISDIR(path
.dentry
->d_inode
->i_mode
))
1892 * 1. may_open() truncates a file
1893 * 2. a rw->ro mount transition occurs
1894 * 3. nameidata_to_filp() fails due to
1896 * That would be inconsistent, and should
1897 * be avoided. Taking this mnt write here
1898 * ensures that (2) can not occur.
1900 will_write
= open_will_write_to_fs(flag
, nd
.path
.dentry
->d_inode
);
1902 error
= mnt_want_write(nd
.path
.mnt
);
1906 error
= may_open(&nd
, acc_mode
, flag
);
1909 mnt_drop_write(nd
.path
.mnt
);
1912 filp
= nameidata_to_filp(&nd
, open_flag
);
1914 * It is now safe to drop the mnt write
1915 * because the filp has had a write taken
1919 mnt_drop_write(nd
.path
.mnt
);
1923 mutex_unlock(&dir
->d_inode
->i_mutex
);
1925 path_put_conditional(&path
, &nd
);
1927 if (!IS_ERR(nd
.intent
.open
.file
))
1928 release_open_intent(&nd
);
1930 return ERR_PTR(error
);
1934 if (flag
& O_NOFOLLOW
)
1937 * This is subtle. Instead of calling do_follow_link() we do the
1938 * thing by hands. The reason is that this way we have zero link_count
1939 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1940 * After that we have the parent and last component, i.e.
1941 * we are in the same situation as after the first path_walk().
1942 * Well, almost - if the last component is normal we get its copy
1943 * stored in nd->last.name and we will have to putname() it when we
1944 * are done. Procfs-like symlinks just set LAST_BIND.
1946 nd
.flags
|= LOOKUP_PARENT
;
1947 error
= security_inode_follow_link(path
.dentry
, &nd
);
1950 error
= __do_follow_link(&path
, &nd
);
1952 /* Does someone understand code flow here? Or it is only
1953 * me so stupid? Anathema to whoever designed this non-sense
1954 * with "intent.open".
1956 release_open_intent(&nd
);
1957 return ERR_PTR(error
);
1959 nd
.flags
&= ~LOOKUP_PARENT
;
1960 if (nd
.last_type
== LAST_BIND
)
1963 if (nd
.last_type
!= LAST_NORM
)
1965 if (nd
.last
.name
[nd
.last
.len
]) {
1966 __putname(nd
.last
.name
);
1971 __putname(nd
.last
.name
);
1974 dir
= nd
.path
.dentry
;
1975 mutex_lock(&dir
->d_inode
->i_mutex
);
1976 path
.dentry
= lookup_hash(&nd
);
1977 path
.mnt
= nd
.path
.mnt
;
1978 __putname(nd
.last
.name
);
1983 * filp_open - open file and return file pointer
1985 * @filename: path to open
1986 * @flags: open flags as per the open(2) second argument
1987 * @mode: mode for the new file if O_CREAT is set, else ignored
1989 * This is the helper to open a file from kernelspace if you really
1990 * have to. But in generally you should not do this, so please move
1991 * along, nothing to see here..
1993 struct file
*filp_open(const char *filename
, int flags
, int mode
)
1995 return do_filp_open(AT_FDCWD
, filename
, flags
, mode
);
1997 EXPORT_SYMBOL(filp_open
);
2000 * lookup_create - lookup a dentry, creating it if it doesn't exist
2001 * @nd: nameidata info
2002 * @is_dir: directory flag
2004 * Simple function to lookup and return a dentry and create it
2005 * if it doesn't exist. Is SMP-safe.
2007 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2009 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
2011 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
2013 mutex_lock_nested(&nd
->path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2015 * Yucky last component or no last component at all?
2016 * (foo/., foo/.., /////)
2018 if (nd
->last_type
!= LAST_NORM
)
2020 nd
->flags
&= ~LOOKUP_PARENT
;
2021 nd
->flags
|= LOOKUP_CREATE
;
2022 nd
->intent
.open
.flags
= O_EXCL
;
2025 * Do the final lookup.
2027 dentry
= lookup_hash(nd
);
2031 if (dentry
->d_inode
)
2034 * Special case - lookup gave negative, but... we had foo/bar/
2035 * From the vfs_mknod() POV we just have a negative dentry -
2036 * all is fine. Let's be bastards - you had / on the end, you've
2037 * been asking for (non-existent) directory. -ENOENT for you.
2039 if (unlikely(!is_dir
&& nd
->last
.name
[nd
->last
.len
])) {
2041 dentry
= ERR_PTR(-ENOENT
);
2046 dentry
= ERR_PTR(-EEXIST
);
2050 EXPORT_SYMBOL_GPL(lookup_create
);
2052 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
2054 int error
= may_create(dir
, dentry
, NULL
);
2059 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
2062 if (!dir
->i_op
|| !dir
->i_op
->mknod
)
2065 error
= devcgroup_inode_mknod(mode
, dev
);
2069 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
2074 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
2076 fsnotify_create(dir
, dentry
);
2080 static int may_mknod(mode_t mode
)
2082 switch (mode
& S_IFMT
) {
2088 case 0: /* zero mode translates to S_IFREG */
2097 asmlinkage
long sys_mknodat(int dfd
, const char __user
*filename
, int mode
,
2102 struct dentry
* dentry
;
2103 struct nameidata nd
;
2107 tmp
= getname(filename
);
2109 return PTR_ERR(tmp
);
2111 error
= do_path_lookup(dfd
, tmp
, LOOKUP_PARENT
, &nd
);
2114 dentry
= lookup_create(&nd
, 0);
2115 if (IS_ERR(dentry
)) {
2116 error
= PTR_ERR(dentry
);
2119 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2120 mode
&= ~current
->fs
->umask
;
2121 error
= may_mknod(mode
);
2124 error
= mnt_want_write(nd
.path
.mnt
);
2127 switch (mode
& S_IFMT
) {
2128 case 0: case S_IFREG
:
2129 error
= vfs_create(nd
.path
.dentry
->d_inode
,dentry
,mode
,&nd
);
2131 case S_IFCHR
: case S_IFBLK
:
2132 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,
2133 new_decode_dev(dev
));
2135 case S_IFIFO
: case S_IFSOCK
:
2136 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,0);
2139 mnt_drop_write(nd
.path
.mnt
);
2143 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2151 asmlinkage
long sys_mknod(const char __user
*filename
, int mode
, unsigned dev
)
2153 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
2156 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2158 int error
= may_create(dir
, dentry
, NULL
);
2163 if (!dir
->i_op
|| !dir
->i_op
->mkdir
)
2166 mode
&= (S_IRWXUGO
|S_ISVTX
);
2167 error
= security_inode_mkdir(dir
, dentry
, mode
);
2172 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
2174 fsnotify_mkdir(dir
, dentry
);
2178 asmlinkage
long sys_mkdirat(int dfd
, const char __user
*pathname
, int mode
)
2182 struct dentry
*dentry
;
2183 struct nameidata nd
;
2185 tmp
= getname(pathname
);
2186 error
= PTR_ERR(tmp
);
2190 error
= do_path_lookup(dfd
, tmp
, LOOKUP_PARENT
, &nd
);
2193 dentry
= lookup_create(&nd
, 1);
2194 error
= PTR_ERR(dentry
);
2198 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2199 mode
&= ~current
->fs
->umask
;
2200 error
= mnt_want_write(nd
.path
.mnt
);
2203 error
= vfs_mkdir(nd
.path
.dentry
->d_inode
, dentry
, mode
);
2204 mnt_drop_write(nd
.path
.mnt
);
2208 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2216 asmlinkage
long sys_mkdir(const char __user
*pathname
, int mode
)
2218 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
2222 * We try to drop the dentry early: we should have
2223 * a usage count of 2 if we're the only user of this
2224 * dentry, and if that is true (possibly after pruning
2225 * the dcache), then we drop the dentry now.
2227 * A low-level filesystem can, if it choses, legally
2230 * if (!d_unhashed(dentry))
2233 * if it cannot handle the case of removing a directory
2234 * that is still in use by something else..
2236 void dentry_unhash(struct dentry
*dentry
)
2239 shrink_dcache_parent(dentry
);
2240 spin_lock(&dcache_lock
);
2241 spin_lock(&dentry
->d_lock
);
2242 if (atomic_read(&dentry
->d_count
) == 2)
2244 spin_unlock(&dentry
->d_lock
);
2245 spin_unlock(&dcache_lock
);
2248 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2250 int error
= may_delete(dir
, dentry
, 1);
2255 if (!dir
->i_op
|| !dir
->i_op
->rmdir
)
2260 mutex_lock(&dentry
->d_inode
->i_mutex
);
2261 dentry_unhash(dentry
);
2262 if (d_mountpoint(dentry
))
2265 error
= security_inode_rmdir(dir
, dentry
);
2267 error
= dir
->i_op
->rmdir(dir
, dentry
);
2269 dentry
->d_inode
->i_flags
|= S_DEAD
;
2272 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2281 static long do_rmdir(int dfd
, const char __user
*pathname
)
2285 struct dentry
*dentry
;
2286 struct nameidata nd
;
2288 name
= getname(pathname
);
2290 return PTR_ERR(name
);
2292 error
= do_path_lookup(dfd
, name
, LOOKUP_PARENT
, &nd
);
2296 switch(nd
.last_type
) {
2307 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2308 dentry
= lookup_hash(&nd
);
2309 error
= PTR_ERR(dentry
);
2312 error
= mnt_want_write(nd
.path
.mnt
);
2315 error
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
2316 mnt_drop_write(nd
.path
.mnt
);
2320 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2328 asmlinkage
long sys_rmdir(const char __user
*pathname
)
2330 return do_rmdir(AT_FDCWD
, pathname
);
2333 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2335 int error
= may_delete(dir
, dentry
, 0);
2340 if (!dir
->i_op
|| !dir
->i_op
->unlink
)
2345 mutex_lock(&dentry
->d_inode
->i_mutex
);
2346 if (d_mountpoint(dentry
))
2349 error
= security_inode_unlink(dir
, dentry
);
2351 error
= dir
->i_op
->unlink(dir
, dentry
);
2353 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2355 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2356 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
2357 fsnotify_link_count(dentry
->d_inode
);
2365 * Make sure that the actual truncation of the file will occur outside its
2366 * directory's i_mutex. Truncate can take a long time if there is a lot of
2367 * writeout happening, and we don't want to prevent access to the directory
2368 * while waiting on the I/O.
2370 static long do_unlinkat(int dfd
, const char __user
*pathname
)
2374 struct dentry
*dentry
;
2375 struct nameidata nd
;
2376 struct inode
*inode
= NULL
;
2378 name
= getname(pathname
);
2380 return PTR_ERR(name
);
2382 error
= do_path_lookup(dfd
, name
, LOOKUP_PARENT
, &nd
);
2386 if (nd
.last_type
!= LAST_NORM
)
2388 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2389 dentry
= lookup_hash(&nd
);
2390 error
= PTR_ERR(dentry
);
2391 if (!IS_ERR(dentry
)) {
2392 /* Why not before? Because we want correct error value */
2393 if (nd
.last
.name
[nd
.last
.len
])
2395 inode
= dentry
->d_inode
;
2397 atomic_inc(&inode
->i_count
);
2398 error
= mnt_want_write(nd
.path
.mnt
);
2401 error
= vfs_unlink(nd
.path
.dentry
->d_inode
, dentry
);
2402 mnt_drop_write(nd
.path
.mnt
);
2406 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2408 iput(inode
); /* truncate the inode here */
2416 error
= !dentry
->d_inode
? -ENOENT
:
2417 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
2421 asmlinkage
long sys_unlinkat(int dfd
, const char __user
*pathname
, int flag
)
2423 if ((flag
& ~AT_REMOVEDIR
) != 0)
2426 if (flag
& AT_REMOVEDIR
)
2427 return do_rmdir(dfd
, pathname
);
2429 return do_unlinkat(dfd
, pathname
);
2432 asmlinkage
long sys_unlink(const char __user
*pathname
)
2434 return do_unlinkat(AT_FDCWD
, pathname
);
2437 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
, int mode
)
2439 int error
= may_create(dir
, dentry
, NULL
);
2444 if (!dir
->i_op
|| !dir
->i_op
->symlink
)
2447 error
= security_inode_symlink(dir
, dentry
, oldname
);
2452 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
2454 fsnotify_create(dir
, dentry
);
2458 asmlinkage
long sys_symlinkat(const char __user
*oldname
,
2459 int newdfd
, const char __user
*newname
)
2464 struct dentry
*dentry
;
2465 struct nameidata nd
;
2467 from
= getname(oldname
);
2469 return PTR_ERR(from
);
2470 to
= getname(newname
);
2471 error
= PTR_ERR(to
);
2475 error
= do_path_lookup(newdfd
, to
, LOOKUP_PARENT
, &nd
);
2478 dentry
= lookup_create(&nd
, 0);
2479 error
= PTR_ERR(dentry
);
2483 error
= mnt_want_write(nd
.path
.mnt
);
2486 error
= vfs_symlink(nd
.path
.dentry
->d_inode
, dentry
, from
, S_IALLUGO
);
2487 mnt_drop_write(nd
.path
.mnt
);
2491 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2500 asmlinkage
long sys_symlink(const char __user
*oldname
, const char __user
*newname
)
2502 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
2505 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2507 struct inode
*inode
= old_dentry
->d_inode
;
2513 error
= may_create(dir
, new_dentry
, NULL
);
2517 if (dir
->i_sb
!= inode
->i_sb
)
2521 * A link to an append-only or immutable file cannot be created.
2523 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2525 if (!dir
->i_op
|| !dir
->i_op
->link
)
2527 if (S_ISDIR(old_dentry
->d_inode
->i_mode
))
2530 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
2534 mutex_lock(&old_dentry
->d_inode
->i_mutex
);
2536 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
2537 mutex_unlock(&old_dentry
->d_inode
->i_mutex
);
2539 fsnotify_link(dir
, old_dentry
->d_inode
, new_dentry
);
2544 * Hardlinks are often used in delicate situations. We avoid
2545 * security-related surprises by not following symlinks on the
2548 * We don't follow them on the oldname either to be compatible
2549 * with linux 2.0, and to avoid hard-linking to directories
2550 * and other special files. --ADM
2552 asmlinkage
long sys_linkat(int olddfd
, const char __user
*oldname
,
2553 int newdfd
, const char __user
*newname
,
2556 struct dentry
*new_dentry
;
2557 struct nameidata nd
, old_nd
;
2561 if ((flags
& ~AT_SYMLINK_FOLLOW
) != 0)
2564 to
= getname(newname
);
2568 error
= __user_walk_fd(olddfd
, oldname
,
2569 flags
& AT_SYMLINK_FOLLOW
? LOOKUP_FOLLOW
: 0,
2573 error
= do_path_lookup(newdfd
, to
, LOOKUP_PARENT
, &nd
);
2577 if (old_nd
.path
.mnt
!= nd
.path
.mnt
)
2579 new_dentry
= lookup_create(&nd
, 0);
2580 error
= PTR_ERR(new_dentry
);
2581 if (IS_ERR(new_dentry
))
2583 error
= mnt_want_write(nd
.path
.mnt
);
2586 error
= vfs_link(old_nd
.path
.dentry
, nd
.path
.dentry
->d_inode
, new_dentry
);
2587 mnt_drop_write(nd
.path
.mnt
);
2591 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2595 path_put(&old_nd
.path
);
2602 asmlinkage
long sys_link(const char __user
*oldname
, const char __user
*newname
)
2604 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
2608 * The worst of all namespace operations - renaming directory. "Perverted"
2609 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2611 * a) we can get into loop creation. Check is done in is_subdir().
2612 * b) race potential - two innocent renames can create a loop together.
2613 * That's where 4.4 screws up. Current fix: serialization on
2614 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2616 * c) we have to lock _three_ objects - parents and victim (if it exists).
2617 * And that - after we got ->i_mutex on parents (until then we don't know
2618 * whether the target exists). Solution: try to be smart with locking
2619 * order for inodes. We rely on the fact that tree topology may change
2620 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2621 * move will be locked. Thus we can rank directories by the tree
2622 * (ancestors first) and rank all non-directories after them.
2623 * That works since everybody except rename does "lock parent, lookup,
2624 * lock child" and rename is under ->s_vfs_rename_mutex.
2625 * HOWEVER, it relies on the assumption that any object with ->lookup()
2626 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2627 * we'd better make sure that there's no link(2) for them.
2628 * d) some filesystems don't support opened-but-unlinked directories,
2629 * either because of layout or because they are not ready to deal with
2630 * all cases correctly. The latter will be fixed (taking this sort of
2631 * stuff into VFS), but the former is not going away. Solution: the same
2632 * trick as in rmdir().
2633 * e) conversion from fhandle to dentry may come in the wrong moment - when
2634 * we are removing the target. Solution: we will have to grab ->i_mutex
2635 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2636 * ->i_mutex on parents, which works but leads to some truely excessive
2639 static int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
2640 struct inode
*new_dir
, struct dentry
*new_dentry
)
2643 struct inode
*target
;
2646 * If we are going to change the parent - check write permissions,
2647 * we'll need to flip '..'.
2649 if (new_dir
!= old_dir
) {
2650 error
= permission(old_dentry
->d_inode
, MAY_WRITE
, NULL
);
2655 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2659 target
= new_dentry
->d_inode
;
2661 mutex_lock(&target
->i_mutex
);
2662 dentry_unhash(new_dentry
);
2664 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2667 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2670 target
->i_flags
|= S_DEAD
;
2671 mutex_unlock(&target
->i_mutex
);
2672 if (d_unhashed(new_dentry
))
2673 d_rehash(new_dentry
);
2677 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
2678 d_move(old_dentry
,new_dentry
);
2682 static int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
2683 struct inode
*new_dir
, struct dentry
*new_dentry
)
2685 struct inode
*target
;
2688 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2693 target
= new_dentry
->d_inode
;
2695 mutex_lock(&target
->i_mutex
);
2696 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2699 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2701 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
2702 d_move(old_dentry
, new_dentry
);
2705 mutex_unlock(&target
->i_mutex
);
2710 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2711 struct inode
*new_dir
, struct dentry
*new_dentry
)
2714 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
2715 const char *old_name
;
2717 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
2720 error
= may_delete(old_dir
, old_dentry
, is_dir
);
2724 if (!new_dentry
->d_inode
)
2725 error
= may_create(new_dir
, new_dentry
, NULL
);
2727 error
= may_delete(new_dir
, new_dentry
, is_dir
);
2731 if (!old_dir
->i_op
|| !old_dir
->i_op
->rename
)
2734 DQUOT_INIT(old_dir
);
2735 DQUOT_INIT(new_dir
);
2737 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
2740 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
2742 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
2744 const char *new_name
= old_dentry
->d_name
.name
;
2745 fsnotify_move(old_dir
, new_dir
, old_name
, new_name
, is_dir
,
2746 new_dentry
->d_inode
, old_dentry
);
2748 fsnotify_oldname_free(old_name
);
2753 static int do_rename(int olddfd
, const char *oldname
,
2754 int newdfd
, const char *newname
)
2757 struct dentry
* old_dir
, * new_dir
;
2758 struct dentry
* old_dentry
, *new_dentry
;
2759 struct dentry
* trap
;
2760 struct nameidata oldnd
, newnd
;
2762 error
= do_path_lookup(olddfd
, oldname
, LOOKUP_PARENT
, &oldnd
);
2766 error
= do_path_lookup(newdfd
, newname
, LOOKUP_PARENT
, &newnd
);
2771 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
2774 old_dir
= oldnd
.path
.dentry
;
2776 if (oldnd
.last_type
!= LAST_NORM
)
2779 new_dir
= newnd
.path
.dentry
;
2780 if (newnd
.last_type
!= LAST_NORM
)
2783 trap
= lock_rename(new_dir
, old_dir
);
2785 old_dentry
= lookup_hash(&oldnd
);
2786 error
= PTR_ERR(old_dentry
);
2787 if (IS_ERR(old_dentry
))
2789 /* source must exist */
2791 if (!old_dentry
->d_inode
)
2793 /* unless the source is a directory trailing slashes give -ENOTDIR */
2794 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
2796 if (oldnd
.last
.name
[oldnd
.last
.len
])
2798 if (newnd
.last
.name
[newnd
.last
.len
])
2801 /* source should not be ancestor of target */
2803 if (old_dentry
== trap
)
2805 new_dentry
= lookup_hash(&newnd
);
2806 error
= PTR_ERR(new_dentry
);
2807 if (IS_ERR(new_dentry
))
2809 /* target should not be an ancestor of source */
2811 if (new_dentry
== trap
)
2814 error
= mnt_want_write(oldnd
.path
.mnt
);
2817 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
2818 new_dir
->d_inode
, new_dentry
);
2819 mnt_drop_write(oldnd
.path
.mnt
);
2825 unlock_rename(new_dir
, old_dir
);
2827 path_put(&newnd
.path
);
2829 path_put(&oldnd
.path
);
2834 asmlinkage
long sys_renameat(int olddfd
, const char __user
*oldname
,
2835 int newdfd
, const char __user
*newname
)
2841 from
= getname(oldname
);
2843 return PTR_ERR(from
);
2844 to
= getname(newname
);
2845 error
= PTR_ERR(to
);
2847 error
= do_rename(olddfd
, from
, newdfd
, to
);
2854 asmlinkage
long sys_rename(const char __user
*oldname
, const char __user
*newname
)
2856 return sys_renameat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
);
2859 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
2863 len
= PTR_ERR(link
);
2868 if (len
> (unsigned) buflen
)
2870 if (copy_to_user(buffer
, link
, len
))
2877 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2878 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2879 * using) it for any given inode is up to filesystem.
2881 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2883 struct nameidata nd
;
2888 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
2890 return PTR_ERR(cookie
);
2892 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
2893 if (dentry
->d_inode
->i_op
->put_link
)
2894 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
2898 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
2900 return __vfs_follow_link(nd
, link
);
2903 /* get the link contents into pagecache */
2904 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
2907 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
2908 page
= read_mapping_page(mapping
, 0, NULL
);
2915 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
2917 struct page
*page
= NULL
;
2918 char *s
= page_getlink(dentry
, &page
);
2919 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
2922 page_cache_release(page
);
2927 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
2929 struct page
*page
= NULL
;
2930 nd_set_link(nd
, page_getlink(dentry
, &page
));
2934 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
2936 struct page
*page
= cookie
;
2940 page_cache_release(page
);
2944 int __page_symlink(struct inode
*inode
, const char *symname
, int len
,
2947 struct address_space
*mapping
= inode
->i_mapping
;
2954 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
2955 AOP_FLAG_UNINTERRUPTIBLE
, &page
, &fsdata
);
2959 kaddr
= kmap_atomic(page
, KM_USER0
);
2960 memcpy(kaddr
, symname
, len
-1);
2961 kunmap_atomic(kaddr
, KM_USER0
);
2963 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
2970 mark_inode_dirty(inode
);
2976 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
2978 return __page_symlink(inode
, symname
, len
,
2979 mapping_gfp_mask(inode
->i_mapping
));
2982 const struct inode_operations page_symlink_inode_operations
= {
2983 .readlink
= generic_readlink
,
2984 .follow_link
= page_follow_link_light
,
2985 .put_link
= page_put_link
,
2988 EXPORT_SYMBOL(__user_walk
);
2989 EXPORT_SYMBOL(__user_walk_fd
);
2990 EXPORT_SYMBOL(follow_down
);
2991 EXPORT_SYMBOL(follow_up
);
2992 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
2993 EXPORT_SYMBOL(getname
);
2994 EXPORT_SYMBOL(lock_rename
);
2995 EXPORT_SYMBOL(lookup_one_len
);
2996 EXPORT_SYMBOL(page_follow_link_light
);
2997 EXPORT_SYMBOL(page_put_link
);
2998 EXPORT_SYMBOL(page_readlink
);
2999 EXPORT_SYMBOL(__page_symlink
);
3000 EXPORT_SYMBOL(page_symlink
);
3001 EXPORT_SYMBOL(page_symlink_inode_operations
);
3002 EXPORT_SYMBOL(path_lookup
);
3003 EXPORT_SYMBOL(vfs_path_lookup
);
3004 EXPORT_SYMBOL(permission
);
3005 EXPORT_SYMBOL(vfs_permission
);
3006 EXPORT_SYMBOL(file_permission
);
3007 EXPORT_SYMBOL(unlock_rename
);
3008 EXPORT_SYMBOL(vfs_create
);
3009 EXPORT_SYMBOL(vfs_follow_link
);
3010 EXPORT_SYMBOL(vfs_link
);
3011 EXPORT_SYMBOL(vfs_mkdir
);
3012 EXPORT_SYMBOL(vfs_mknod
);
3013 EXPORT_SYMBOL(generic_permission
);
3014 EXPORT_SYMBOL(vfs_readlink
);
3015 EXPORT_SYMBOL(vfs_rename
);
3016 EXPORT_SYMBOL(vfs_rmdir
);
3017 EXPORT_SYMBOL(vfs_symlink
);
3018 EXPORT_SYMBOL(vfs_unlink
);
3019 EXPORT_SYMBOL(dentry_unhash
);
3020 EXPORT_SYMBOL(generic_readlink
);