18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
25 #include <linux/personality.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
33 #include <linux/fcntl.h>
37 #include <asm/uaccess.h>
130 #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
140 result = audit_reusename(filename);
152 kname = (
char *)result +
sizeof(*result);
153 result->
name = kname;
171 kname = (
char *)result;
173 result = kzalloc(
sizeof(*result),
GFP_KERNEL);
176 result = (
struct filename *)kname;
179 result->
name = kname;
199 audit_getname(result);
210 return getname_flags(filename, 0,
NULL);
214 #ifdef CONFIG_AUDITSYSCALL
217 if (
unlikely(!audit_dummy_context()))
225 #ifdef CONFIG_FS_POSIX_ACL
249 if (inode->
i_op->get_acl) {
261 posix_acl_release(acl);
272 static int acl_permission_check(
struct inode *inode,
int mask)
280 int error = check_acl(inode, mask);
318 ret = acl_permission_check(inode, mask);
357 static inline int do_inode_permission(
struct inode *inode,
int mask)
361 return inode->
i_op->permission(inode, mask);
364 spin_lock(&inode->
i_lock);
366 spin_unlock(&inode->
i_lock);
395 retval = do_inode_permission(inode, mask);
399 retval = devcgroup_inode_permission(inode, mask);
414 static int sb_permission(
struct super_block *
sb,
struct inode *inode,
int mask)
442 retval = sb_permission(inode->
i_sb, inode, mask);
485 static inline void lock_rcu_walk(
void)
491 static inline void unlock_rcu_walk(
void)
516 spin_lock(&fs->
lock);
517 if (nd->
root.mnt != fs->root.mnt ||
518 nd->
root.dentry != fs->root.dentry)
521 spin_lock(&parent->
d_lock);
523 if (!__d_rcu_to_refcount(parent, nd->
seq))
530 if (!__d_rcu_to_refcount(dentry, nd->
seq))
541 spin_unlock(&dentry->
d_lock);
543 spin_unlock(&parent->
d_lock);
546 spin_unlock(&fs->
lock);
555 spin_unlock(&dentry->
d_lock);
557 spin_unlock(&parent->
d_lock);
560 spin_unlock(&fs->
lock);
564 static inline int d_revalidate(
struct dentry *dentry,
unsigned int flags)
566 return dentry->
d_op->d_revalidate(dentry, flags);
579 static int complete_walk(
struct nameidata *nd)
581 struct dentry *dentry = nd->
path.dentry;
588 spin_lock(&dentry->
d_lock);
589 if (
unlikely(!__d_rcu_to_refcount(dentry, nd->
seq))) {
590 spin_unlock(&dentry->
d_lock);
595 spin_unlock(&dentry->
d_lock);
610 status = d_revalidate(dentry, nd->
flags);
627 static int link_path_walk(
const char *,
struct nameidata *);
636 seq = read_seqcount_begin(&fs->
seq);
638 nd->
seq = __read_seqcount_begin(&nd->
root.dentry->d_seq);
639 }
while (read_seqcount_retry(&fs->
seq, seq));
659 ret = link_path_walk(link, nd);
663 return PTR_ERR(link);
673 static inline void path_to_nameidata(
const struct path *path,
700 static inline void put_link(
struct nameidata *nd,
struct path *link,
void *
cookie)
702 struct inode *inode = link->
dentry->d_inode;
703 if (inode->
i_op->put_link)
704 inode->
i_op->put_link(link->
dentry, nd, cookie);
727 static inline int may_follow_link(
struct path *link,
struct nameidata *nd)
729 const struct inode *
inode;
730 const struct inode *parent;
736 inode = link->
dentry->d_inode;
741 parent = nd->
path.dentry->d_inode;
750 path_put_conditional(link, nd);
767 static bool safe_hardlink_source(
struct inode *inode)
802 static int may_linkat(
struct path *link)
807 if (!sysctl_protected_hardlinks)
811 inode = link->
dentry->d_inode;
816 if (uid_eq(cred->
fsuid, inode->
i_uid) || safe_hardlink_source(inode) ||
825 follow_link(
struct path *link,
struct nameidata *nd,
void **
p)
827 struct dentry *dentry = link->
dentry;
838 goto out_put_nd_path;
844 nd_set_link(nd,
NULL);
848 goto out_put_nd_path;
851 *p = dentry->
d_inode->i_op->follow_link(dentry, nd);
854 goto out_put_nd_path;
859 error = __vfs_follow_link(nd, s);
861 put_link(nd, link, *p);
873 static int follow_up_rcu(
struct path *path)
876 struct mount *parent;
877 struct dentry *mountpoint;
880 if (&parent->
mnt == path->
mnt)
883 path->
dentry = mountpoint;
900 struct mount *mnt = real_mount(path->
mnt);
901 struct mount *parent;
902 struct dentry *mountpoint;
914 path->
dentry = mountpoint;
925 static int follow_automount(
struct path *path,
unsigned flags,
931 if (!path->
dentry->d_op || !path->
dentry->d_op->d_automount)
951 if (
current->total_link_count >= 40)
954 mnt = path->
dentry->d_op->d_automount(path);
1005 static int follow_managed(
struct path *path,
unsigned flags)
1009 bool need_mntput =
false;
1023 ret = path->
dentry->d_op->d_manage(path->
dentry,
false);
1035 path->
mnt = mounted;
1049 ret = follow_automount(path, flags, &need_mntput);
1059 if (need_mntput && path->
mnt == mnt)
1063 return ret < 0 ? ret : need_mntput;
1074 path->
mnt = mounted;
1081 static inline bool managed_dentry_might_block(
struct dentry *dentry)
1083 return (dentry->
d_flags & DCACHE_MANAGE_TRANSIT &&
1084 dentry->
d_op->d_manage(dentry,
true) < 0);
1091 static bool __follow_mount_rcu(
struct nameidata *nd,
struct path *path,
1092 struct inode **inode)
1095 struct mount *mounted;
1103 if (!d_mountpoint(path->
dentry))
1109 path->
mnt = &mounted->
mnt;
1112 nd->
seq = read_seqcount_begin(&path->
dentry->d_seq);
1118 *inode = path->
dentry->d_inode;
1123 static void follow_mount_rcu(
struct nameidata *nd)
1125 while (d_mountpoint(nd->
path.dentry)) {
1126 struct mount *mounted;
1131 nd->
path.dentry = mounted->
mnt.mnt_root;
1132 nd->
seq = read_seqcount_begin(&nd->
path.dentry->d_seq);
1136 static int follow_dotdot_rcu(
struct nameidata *nd)
1141 if (nd->
path.dentry == nd->
root.dentry &&
1145 if (nd->
path.dentry != nd->
path.mnt->mnt_root) {
1146 struct dentry *old = nd->
path.dentry;
1147 struct dentry *parent = old->
d_parent;
1150 seq = read_seqcount_begin(&parent->
d_seq);
1151 if (read_seqcount_retry(&old->
d_seq, nd->
seq))
1153 nd->
path.dentry = parent;
1157 if (!follow_up_rcu(&nd->
path))
1159 nd->
seq = read_seqcount_begin(&nd->
path.dentry->d_seq);
1161 follow_mount_rcu(nd);
1195 if (managed & DCACHE_MANAGE_TRANSIT) {
1198 ret = path->
dentry->d_op->d_manage(
1205 if (managed & DCACHE_MOUNTED) {
1211 path->
mnt = mounted;
1225 static void follow_mount(
struct path *path)
1227 while (d_mountpoint(path->
dentry)) {
1233 path->
mnt = mounted;
1238 static void follow_dotdot(
struct nameidata *nd)
1243 struct dentry *old = nd->
path.dentry;
1245 if (nd->
path.dentry == nd->
root.dentry &&
1249 if (nd->
path.dentry != nd->
path.mnt->mnt_root) {
1258 follow_mount(&nd->
path);
1269 static struct dentry *lookup_dcache(
struct qstr *name,
struct dentry *dir,
1270 unsigned int flags,
bool *need_lookup)
1272 struct dentry *dentry;
1275 *need_lookup =
false;
1278 if (d_need_lookup(dentry)) {
1279 *need_lookup =
true;
1281 error = d_revalidate(dentry, flags);
1285 return ERR_PTR(error);
1299 *need_lookup =
true;
1310 static struct dentry *lookup_real(
struct inode *dir,
struct dentry *dentry,
1321 old = dir->
i_op->lookup(dir, dentry, flags);
1329 static struct dentry *__lookup_hash(
struct qstr *name,
1330 struct dentry *base,
unsigned int flags)
1333 struct dentry *dentry;
1335 dentry = lookup_dcache(name, base, flags, &need_lookup);
1339 return lookup_real(base->
d_inode, dentry, flags);
1347 static int lookup_fast(
struct nameidata *nd,
struct qstr *name,
1348 struct path *path,
struct inode **inode)
1351 struct dentry *dentry, *parent = nd->
path.dentry;
1372 if (read_seqcount_retry(&dentry->
d_seq, seq))
1382 if (__read_seqcount_retry(&parent->
d_seq, nd->
seq))
1386 if (
unlikely(d_need_lookup(dentry)))
1389 status = d_revalidate(dentry, nd->
flags);
1398 if (
unlikely(!__follow_mount_rcu(nd, path, inode)))
1404 if (unlazy_walk(nd, dentry))
1413 if (
unlikely(d_need_lookup(dentry))) {
1419 status = d_revalidate(dentry, nd->
flags);
1433 err = follow_managed(path, nd->
flags);
1435 path_put_conditional(path, nd);
1440 *inode = path->
dentry->d_inode;
1448 static int lookup_slow(
struct nameidata *nd,
struct qstr *name,
1451 struct dentry *dentry, *parent;
1454 parent = nd->
path.dentry;
1458 dentry = __lookup_hash(name, parent, nd->
flags);
1461 return PTR_ERR(dentry);
1464 err = follow_managed(path, nd->
flags);
1466 path_put_conditional(path, nd);
1474 static inline int may_lookup(
struct nameidata *nd)
1480 if (unlazy_walk(nd,
NULL))
1486 static inline int handle_dots(
struct nameidata *nd,
int type)
1490 if (follow_dotdot_rcu(nd))
1498 static void terminate_walk(
struct nameidata *nd)
1516 static inline int should_follow_link(
struct inode *inode,
int follow)
1523 spin_lock(&inode->
i_lock);
1525 spin_unlock(&inode->
i_lock);
1530 static inline int walk_component(
struct nameidata *nd,
struct path *path,
1531 struct qstr *name,
int type,
int follow)
1533 struct inode *
inode;
1541 return handle_dots(nd, type);
1542 err = lookup_fast(nd, name, path, &inode);
1547 err = lookup_slow(nd, name, path);
1551 inode = path->
dentry->d_inode;
1557 if (should_follow_link(inode, follow)) {
1567 path_to_nameidata(path, nd);
1572 path_to_nameidata(path, nd);
1585 static inline int nested_symlink(
struct path *path,
struct nameidata *nd)
1590 path_put_conditional(path, nd);
1600 struct path link = *
path;
1603 res = follow_link(&link, nd, &cookie);
1606 res = walk_component(nd, path, &nd->
last,
1608 put_link(nd, &link, cookie);
1622 static inline int can_lookup(
struct inode *inode)
1630 spin_lock(&inode->
i_lock);
1632 spin_unlock(&inode->
i_lock);
1658 #ifdef CONFIG_DCACHE_WORD_ACCESS
1660 #include <asm/word-at-a-time.h>
1664 static inline unsigned int fold_hash(
unsigned long hash)
1666 hash += hash >> (8*
sizeof(
int));
1672 #define fold_hash(x) (x)
1676 unsigned int full_name_hash(
const unsigned char *name,
unsigned int len)
1678 unsigned long a,
mask;
1679 unsigned long hash = 0;
1682 a = load_unaligned_zeropad(name);
1683 if (len <
sizeof(
unsigned long))
1687 name +=
sizeof(
unsigned long);
1688 len -=
sizeof(
unsigned long);
1692 mask = ~(~0ul << len*8);
1695 return fold_hash(hash);
1703 static inline unsigned long hash_name(
const char *name,
unsigned int *hashp)
1705 unsigned long a,
b, adata, bdata,
mask,
hash, len;
1709 len = -
sizeof(
unsigned long);
1711 hash = (hash +
a) * 9;
1712 len +=
sizeof(
unsigned long);
1713 a = load_unaligned_zeropad(name+len);
1715 }
while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1723 *hashp = fold_hash(hash);
1725 return len + find_zero(mask);
1734 hash = partial_name_hash(*name++, hash);
1735 return end_name_hash(hash);
1743 static inline unsigned long hash_name(
const char *name,
unsigned int *hashp)
1746 unsigned long len = 0,
c;
1748 c = (
unsigned char)*name;
1751 hash = partial_name_hash(
c, hash);
1752 c = (
unsigned char)name[len];
1753 }
while (
c &&
c !=
'/');
1754 *hashp = end_name_hash(hash);
1768 static int link_path_walk(
const char *name,
struct nameidata *nd)
1784 err = may_lookup(nd);
1788 len = hash_name(name, &this.hash);
1793 if (name[0] ==
'.')
switch (len) {
1795 if (name[1] ==
'.') {
1804 struct dentry *parent = nd->
path.dentry;
1807 err = parent->
d_op->d_hash(parent, nd->
inode,
1815 goto last_component;
1822 }
while (
unlikely(name[len] ==
'/'));
1824 goto last_component;
1832 err = nested_symlink(&
next, nd);
1836 if (can_lookup(nd->
inode))
1851 static int path_init(
int dfd,
const char *name,
unsigned int flags,
1860 struct inode *inode = nd->
root.dentry->d_inode;
1862 if (!inode->
i_op->lookup)
1872 nd->
seq = __read_seqcount_begin(&nd->
path.dentry->d_seq);
1898 seq = read_seqcount_begin(&fs->
seq);
1900 nd->
seq = __read_seqcount_begin(&nd->
path.dentry->d_seq);
1901 }
while (read_seqcount_retry(&fs->
seq, seq));
1906 struct fd f = fdget_raw(dfd);
1907 struct dentry *dentry;
1912 dentry = f.
file->f_path.dentry;
1928 if (flags & LOOKUP_RCU) {
1931 nd->
seq = __read_seqcount_begin(&nd->
path.dentry->d_seq);
1943 static inline int lookup_last(
struct nameidata *nd,
struct path *path)
1954 static int path_lookupat(
int dfd,
const char *name,
1955 unsigned int flags,
struct nameidata *nd)
1975 err = path_init(dfd, name, flags |
LOOKUP_PARENT, nd, &base);
1980 current->total_link_count = 0;
1981 err = link_path_walk(name, nd);
1984 err = lookup_last(nd, &path);
1987 struct path link =
path;
1988 err = may_follow_link(&link, nd);
1992 err = follow_link(&link, nd, &cookie);
1995 err = lookup_last(nd, &path);
1996 put_link(nd, &link, cookie);
2001 err = complete_walk(nd);
2004 if (!nd->
inode->i_op->lookup) {
2013 if (nd->
root.mnt && !(nd->
flags & LOOKUP_ROOT)) {
2020 static int filename_lookup(
int dfd,
struct filename *name,
2021 unsigned int flags,
struct nameidata *nd)
2023 int retval = path_lookupat(dfd, name->
name, flags | LOOKUP_RCU, nd);
2025 retval = path_lookupat(dfd, name->
name, flags, nd);
2027 retval = path_lookupat(dfd, name->
name,
2031 audit_inode(name, nd->
path.dentry, flags & LOOKUP_PARENT);
2035 static int do_path_lookup(
int dfd,
const char *name,
2036 unsigned int flags,
struct nameidata *nd)
2040 return filename_lookup(dfd, &filename, flags, nd);
2048 int err = do_path_lookup(
AT_FDCWD, name, LOOKUP_PARENT, &nd);
2050 return ERR_PTR(err);
2056 d = __lookup_hash(&nd.
last, nd.
path.dentry, 0);
2066 int kern_path(
const char *name,
unsigned int flags,
struct path *path)
2069 int res = do_path_lookup(
AT_FDCWD, name, flags, &nd);
2084 const char *name,
unsigned int flags,
2089 nd.
root.dentry = dentry;
2091 BUG_ON(flags & LOOKUP_PARENT);
2093 err = do_path_lookup(
AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2104 static struct dentry *lookup_hash(
struct nameidata *nd)
2106 return __lookup_hash(&nd->
last, nd->
path.dentry, nd->
flags);
2135 if (len < 2 || (len == 2 && name[1] ==
'.'))
2140 c = *(
const unsigned char *)name++;
2141 if (c ==
'/' || c ==
'\0')
2149 int err = base->
d_op->d_hash(base, base->
d_inode, &
this);
2151 return ERR_PTR(err);
2156 return ERR_PTR(err);
2158 return __lookup_hash(&
this, base, 0);
2162 struct path *path,
int *
empty)
2165 struct filename *
tmp = getname_flags(name, flags, empty);
2166 int err = PTR_ERR(tmp);
2169 BUG_ON(flags & LOOKUP_PARENT);
2171 err = filename_lookup(dfd, tmp, flags, &nd);
2192 user_path_parent(
int dfd,
const char __user *path,
struct nameidata *nd)
2200 error = filename_lookup(dfd, s, LOOKUP_PARENT, nd);
2203 return ERR_PTR(error);
2213 static inline int check_sticky(
struct inode *dir,
struct inode *inode)
2219 if (uid_eq(inode->
i_uid, fsuid))
2221 if (uid_eq(dir->
i_uid, fsuid))
2245 static int may_delete(
struct inode *dir,
struct dentry *victim,
int isdir)
2285 static inline int may_create(
struct inode *dir,
struct dentry *
child)
2339 int error = may_create(dir, dentry);
2343 if (!dir->
i_op->create)
2350 error = dir->
i_op->create(dir, dentry, mode, want_excl);
2352 fsnotify_create(dir, dentry);
2356 static int may_open(
struct path *path,
int acc_mode,
int flag)
2358 struct dentry *dentry = path->
dentry;
2359 struct inode *inode = dentry->
d_inode;
2373 if (acc_mode & MAY_WRITE)
2408 static int handle_truncate(
struct file *filp)
2410 struct path *path = &filp->
f_path;
2411 struct inode *inode = path->
dentry->d_inode;
2412 int error = get_write_access(inode);
2418 error = locks_verify_locked(inode);
2420 error = security_path_truncate(path);
2426 put_write_access(inode);
2430 static inline int open_to_namei_flags(
int flag)
2432 if ((flag & O_ACCMODE) == 3)
2437 static int may_o_create(
struct path *dir,
struct dentry *dentry,
umode_t mode)
2439 int error = security_path_mknod(dir, dentry, mode, 0);
2463 static int atomic_open(
struct nameidata *nd,
struct dentry *dentry,
2464 struct path *path,
struct file *
file,
2466 bool got_write,
bool need_lookup,
2469 struct inode *dir = nd->
path.dentry->d_inode;
2470 unsigned open_flag = open_to_namei_flags(op->
open_flag);
2474 int create_error = 0;
2475 struct dentry *
const DENTRY_NOT_SET = (
void *) -1
UL;
2489 if ((open_flag & (
O_EXCL | O_CREAT)) == (
O_EXCL | O_CREAT)) {
2503 if (((open_flag & (O_CREAT |
O_TRUNC)) ||
2505 if (!(open_flag & O_CREAT)) {
2513 create_error = -
EROFS;
2517 create_error = -
EROFS;
2518 open_flag &= ~O_CREAT;
2522 if (open_flag & O_CREAT) {
2523 error = may_o_create(&nd->
path, dentry, mode);
2525 create_error =
error;
2528 open_flag &= ~O_CREAT;
2535 file->
f_path.dentry = DENTRY_NOT_SET;
2537 error = dir->
i_op->atomic_open(dir, dentry, file, open_flag, mode,
2540 if (create_error && error == -
ENOENT)
2541 error = create_error;
2547 fsnotify_create(dir, dentry);
2556 if (file->
f_path.dentry) {
2558 dentry = file->
f_path.dentry;
2561 error = create_error;
2571 error = may_open(&file->
f_path, acc_mode, open_flag);
2581 dentry = lookup_real(dir, dentry, nd->
flags);
2583 return PTR_ERR(dentry);
2588 error = create_error;
2589 if ((open_flag &
O_EXCL)) {
2592 }
else if (!dentry->
d_inode) {
2594 }
else if ((open_flag &
O_TRUNC) &&
2625 static int lookup_open(
struct nameidata *nd,
struct path *path,
2628 bool got_write,
int *opened)
2630 struct dentry *dir = nd->
path.dentry;
2631 struct inode *dir_inode = dir->
d_inode;
2632 struct dentry *dentry;
2636 *opened &= ~FILE_CREATED;
2637 dentry = lookup_dcache(&nd->
last, dir, nd->
flags, &need_lookup);
2639 return PTR_ERR(dentry);
2642 if (!need_lookup && dentry->
d_inode)
2646 return atomic_open(nd, dentry, path, file, op, got_write,
2647 need_lookup, opened);
2653 dentry = lookup_real(dir_inode, dentry, nd->
flags);
2655 return PTR_ERR(dentry);
2675 error = security_path_mknod(&nd->
path, dentry, mode, 0);
2696 static int do_last(
struct nameidata *nd,
struct path *path,
2697 struct file *file,
const struct open_flags *op,
2698 int *opened,
struct filename *name)
2700 struct dentry *dir = nd->
path.dentry;
2702 bool will_truncate = (open_flag &
O_TRUNC) != 0;
2703 bool got_write =
false;
2705 struct inode *
inode;
2706 bool symlink_ok =
false;
2708 bool retried =
false;
2711 nd->
flags &= ~LOOKUP_PARENT;
2722 error = complete_walk(nd);
2725 audit_inode(name, nd->
path.dentry, 0);
2726 if (open_flag & O_CREAT) {
2732 error = complete_walk(nd);
2735 audit_inode(name, dir, 0);
2739 if (!(open_flag & O_CREAT)) {
2745 error = lookup_fast(nd, &nd->
last, path, &inode);
2760 error = complete_walk(nd);
2764 audit_inode(name, dir, 0);
2783 error = lookup_open(nd, path, file, op, got_write, opened);
2790 if ((*opened & FILE_CREATED) ||
2792 will_truncate =
false;
2794 audit_inode(name, file->
f_path.dentry, 0);
2798 if (*opened & FILE_CREATED) {
2801 will_truncate =
false;
2803 path_to_nameidata(path, nd);
2804 goto finish_open_created;
2810 if (path->
dentry->d_inode)
2811 audit_inode(name, path->
dentry, 0);
2824 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2827 error = follow_managed(path, nd->
flags);
2835 inode = path->
dentry->d_inode;
2840 path_to_nameidata(path, nd);
2844 if (should_follow_link(inode, !symlink_ok)) {
2845 if (nd->
flags & LOOKUP_RCU) {
2855 if ((nd->
flags & LOOKUP_RCU) || nd->
path.mnt != path->
mnt) {
2856 path_to_nameidata(path, nd);
2865 error = complete_walk(nd);
2871 if ((open_flag & O_CREAT) &&
S_ISDIR(nd->
inode->i_mode))
2876 audit_inode(name, nd->
path.dentry, 0);
2879 will_truncate =
false;
2881 if (will_truncate) {
2887 finish_open_created:
2888 error = may_open(&nd->
path, acc_mode, open_flag);
2906 if (will_truncate) {
2907 error = handle_truncate(file);
2919 path_put_conditional(path, nd);
2927 if (!save_parent.
dentry || retried)
2932 nd->
path = save_parent;
2947 struct file *base =
NULL;
2959 error = path_init(dfd, pathname->
name, flags | LOOKUP_PARENT, nd, &base);
2963 current->total_link_count = 0;
2964 error = link_path_walk(pathname->
name, nd);
2968 error = do_last(nd, &path, file, op, &opened, pathname);
2970 struct path link =
path;
2973 path_put_conditional(&path, nd);
2978 error = may_follow_link(&link, nd);
2983 error = follow_link(&link, nd, &cookie);
2986 error = do_last(nd, &path, file, op, &opened, pathname);
2987 put_link(nd, &link, cookie);
2990 if (nd->
root.mnt && !(nd->
flags & LOOKUP_ROOT))
3000 if (flags & LOOKUP_RCU)
3005 file = ERR_PTR(error);
3016 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
3018 filp = path_openat(dfd, pathname, &nd, op, flags);
3020 filp = path_openat(dfd, pathname, &nd, op, flags |
LOOKUP_REVAL);
3025 const char *name,
const struct open_flags *op,
int flags)
3032 nd.
root.dentry = dentry;
3037 return ERR_PTR(-
ELOOP);
3039 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
3041 file = path_openat(-1, &filename, &nd, op, flags);
3043 file = path_openat(-1, &filename, &nd, op, flags |
LOOKUP_REVAL);
3049 struct dentry *dentry = ERR_PTR(-
EEXIST);
3052 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
3054 return ERR_PTR(error);
3062 nd.
flags &= ~LOOKUP_PARENT;
3071 dentry = lookup_hash(&nd);
3096 dentry = ERR_PTR(error);
3116 struct dentry *
user_path_create(
int dfd,
const char __user *pathname,
struct path *path,
int is_dir)
3121 return ERR_CAST(tmp);
3130 int error = may_create(dir, dentry);
3138 if (!dir->
i_op->mknod)
3149 error = dir->
i_op->mknod(dir, dentry, mode, dev);
3151 fsnotify_create(dir, dentry);
3155 static int may_mknod(
umode_t mode)
3175 struct dentry *dentry;
3179 error = may_mknod(mode);
3185 return PTR_ERR(dentry);
3189 error = security_path_mknod(&path, dentry, mode,
dev);
3198 new_decode_dev(
dev));
3216 int error = may_create(dir, dentry);
3217 unsigned max_links = dir->
i_sb->s_max_links;
3222 if (!dir->
i_op->mkdir)
3230 if (max_links && dir->
i_nlink >= max_links)
3233 error = dir->
i_op->mkdir(dir, dentry, mode);
3235 fsnotify_mkdir(dir, dentry);
3241 struct dentry *dentry;
3247 return PTR_ERR(dentry);
3251 error = security_path_mkdir(&path, dentry, mode);
3281 spin_lock(&dentry->
d_lock);
3284 spin_unlock(&dentry->
d_lock);
3289 int error = may_delete(dir, dentry, 1);
3294 if (!dir->
i_op->rmdir)
3301 if (d_mountpoint(dentry))
3309 error = dir->
i_op->rmdir(dir, dentry);
3324 static long do_rmdir(
int dfd,
const char __user *pathname)
3328 struct dentry *dentry;
3331 name = user_path_parent(dfd, pathname, &nd);
3333 return PTR_ERR(name);
3347 nd.
flags &= ~LOOKUP_PARENT;
3353 dentry = lookup_hash(&nd);
3354 error = PTR_ERR(dentry);
3361 error = security_path_rmdir(&nd.
path, dentry);
3383 int error = may_delete(dir, dentry, 0);
3388 if (!dir->
i_op->unlink)
3392 if (d_mountpoint(dentry))
3397 error = dir->
i_op->unlink(dir, dentry);
3406 fsnotify_link_count(dentry->
d_inode);
3419 static long do_unlinkat(
int dfd,
const char __user *pathname)
3423 struct dentry *dentry;
3425 struct inode *inode =
NULL;
3427 name = user_path_parent(dfd, pathname, &nd);
3429 return PTR_ERR(name);
3435 nd.
flags &= ~LOOKUP_PARENT;
3441 dentry = lookup_hash(&nd);
3442 error = PTR_ERR(dentry);
3443 if (!IS_ERR(dentry)) {
3451 error = security_path_unlink(&nd.
path, dentry);
3481 return do_unlinkat(dfd, pathname);
3486 return do_unlinkat(
AT_FDCWD, pathname);
3489 int vfs_symlink(
struct inode *dir,
struct dentry *dentry,
const char *oldname)
3491 int error = may_create(dir, dentry);
3496 if (!dir->
i_op->symlink)
3503 error = dir->
i_op->symlink(dir, dentry, oldname);
3505 fsnotify_create(dir, dentry);
3510 int, newdfd,
const char __user *, newname)
3514 struct dentry *dentry;
3519 return PTR_ERR(from);
3522 error = PTR_ERR(dentry);
3526 error = security_path_symlink(&path, dentry, from->name);
3540 int vfs_link(
struct dentry *old_dentry,
struct inode *dir,
struct dentry *new_dentry)
3542 struct inode *inode = old_dentry->
d_inode;
3543 unsigned max_links = dir->
i_sb->s_max_links;
3549 error = may_create(dir, new_dentry);
3561 if (!dir->
i_op->link)
3574 else if (max_links && inode->
i_nlink >= max_links)
3577 error = dir->
i_op->link(old_dentry, dir, new_dentry);
3580 fsnotify_link(dir, inode, new_dentry);
3594 int, newdfd,
const char __user *, newname,
int, flags)
3596 struct dentry *new_dentry;
3597 struct path old_path, new_path;
3622 error = PTR_ERR(new_dentry);
3623 if (IS_ERR(new_dentry))
3627 if (old_path.
mnt != new_path.
mnt)
3629 error = may_linkat(&old_path);
3632 error = security_path_link(old_path.
dentry, &new_path, new_dentry);
3676 static int vfs_rename_dir(
struct inode *old_dir,
struct dentry *old_dentry,
3677 struct inode *new_dir,
struct dentry *new_dentry)
3681 unsigned max_links = new_dir->
i_sb->s_max_links;
3687 if (new_dir != old_dir) {
3702 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3706 if (max_links && !target && new_dir != old_dir &&
3707 new_dir->
i_nlink >= max_links)
3712 error = old_dir->
i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3718 dont_mount(new_dentry);
3726 d_move(old_dentry,new_dentry);
3730 static int vfs_rename_other(
struct inode *old_dir,
struct dentry *old_dentry,
3731 struct inode *new_dir,
struct dentry *new_dentry)
3733 struct inode *target = new_dentry->
d_inode;
3745 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3748 error = old_dir->
i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3753 dont_mount(new_dentry);
3755 d_move(old_dentry, new_dentry);
3764 struct inode *new_dir,
struct dentry *new_dentry)
3768 const unsigned char *old_name;
3773 error = may_delete(old_dir, old_dentry, is_dir);
3778 error = may_create(new_dir, new_dentry);
3780 error = may_delete(new_dir, new_dentry, is_dir);
3784 if (!old_dir->
i_op->rename)
3787 old_name = fsnotify_oldname_init(old_dentry->
d_name.name);
3790 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3792 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3794 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3795 new_dentry->
d_inode, old_dentry);
3796 fsnotify_oldname_free(old_name);
3802 int, newdfd,
const char __user *, newname)
3804 struct dentry *old_dir, *new_dir;
3805 struct dentry *old_dentry, *new_dentry;
3806 struct dentry *
trap;
3812 from = user_path_parent(olddfd, oldname, &oldnd);
3814 error = PTR_ERR(from);
3818 to = user_path_parent(newdfd, newname, &newnd);
3820 error = PTR_ERR(to);
3825 if (oldnd.
path.mnt != newnd.
path.mnt)
3828 old_dir = oldnd.
path.dentry;
3833 new_dir = newnd.
path.dentry;
3841 oldnd.
flags &= ~LOOKUP_PARENT;
3842 newnd.
flags &= ~LOOKUP_PARENT;
3847 old_dentry = lookup_hash(&oldnd);
3848 error = PTR_ERR(old_dentry);
3849 if (IS_ERR(old_dentry))
3858 if (oldnd.
last.name[oldnd.
last.len])
3860 if (newnd.
last.name[newnd.
last.len])
3865 if (old_dentry == trap)
3867 new_dentry = lookup_hash(&newnd);
3868 error = PTR_ERR(new_dentry);
3869 if (IS_ERR(new_dentry))
3873 if (new_dentry == trap)
3876 error = security_path_rename(&oldnd.
path, old_dentry,
3877 &newnd.
path, new_dentry);
3881 new_dir->
d_inode, new_dentry);
3908 len = PTR_ERR(link);
3913 if (len > (
unsigned) buflen)
3933 cookie = dentry->
d_inode->i_op->follow_link(dentry, &nd);
3935 return PTR_ERR(cookie);
3937 res =
vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3938 if (dentry->
d_inode->i_op->put_link)
3939 dentry->
d_inode->i_op->put_link(dentry, &nd, cookie);
3945 return __vfs_follow_link(nd, link);
3949 static char *page_getlink(
struct dentry * dentry,
struct page **ppage)
3954 page = read_mapping_page(mapping, 0,
NULL);
3965 struct page *page =
NULL;
3966 char *s = page_getlink(dentry, &page);
3977 struct page *page =
NULL;
3978 nd_set_link(nd, page_getlink(dentry, &page));
3984 struct page *page =
cookie;
4008 flags, &page, &fsdata);
4013 memcpy(kaddr, symname, len-1);
4023 mark_inode_dirty(inode);