link_path_walk(): sample parent's i_uid and i_mode for the last component
[linux-block.git] / fs / namei.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/fs/namei.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/*
9 * Some corrections by tytso.
10 */
11
12/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
13 * lookup logic.
14 */
15/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
16 */
17
18#include <linux/init.h>
630d9c47 19#include <linux/export.h>
44696908 20#include <linux/kernel.h>
1da177e4
LT
21#include <linux/slab.h>
22#include <linux/fs.h>
23#include <linux/namei.h>
1da177e4 24#include <linux/pagemap.h>
0eeca283 25#include <linux/fsnotify.h>
1da177e4
LT
26#include <linux/personality.h>
27#include <linux/security.h>
6146f0d5 28#include <linux/ima.h>
1da177e4
LT
29#include <linux/syscalls.h>
30#include <linux/mount.h>
31#include <linux/audit.h>
16f7e0fe 32#include <linux/capability.h>
834f2a4a 33#include <linux/file.h>
5590ff0d 34#include <linux/fcntl.h>
08ce5f16 35#include <linux/device_cgroup.h>
5ad4e53b 36#include <linux/fs_struct.h>
e77819e5 37#include <linux/posix_acl.h>
99d263d4 38#include <linux/hash.h>
2a18da7a 39#include <linux/bitops.h>
aeaa4a79 40#include <linux/init_task.h>
7c0f6ba6 41#include <linux/uaccess.h>
1da177e4 42
e81e3f4d 43#include "internal.h"
c7105365 44#include "mount.h"
e81e3f4d 45
1da177e4
LT
46/* [Feb-1997 T. Schoebel-Theuer]
47 * Fundamental changes in the pathname lookup mechanisms (namei)
48 * were necessary because of omirr. The reason is that omirr needs
49 * to know the _real_ pathname, not the user-supplied one, in case
50 * of symlinks (and also when transname replacements occur).
51 *
52 * The new code replaces the old recursive symlink resolution with
53 * an iterative one (in case of non-nested symlink chains). It does
54 * this with calls to <fs>_follow_link().
55 * As a side effect, dir_namei(), _namei() and follow_link() are now
56 * replaced with a single function lookup_dentry() that can handle all
57 * the special cases of the former code.
58 *
59 * With the new dcache, the pathname is stored at each inode, at least as
60 * long as the refcount of the inode is positive. As a side effect, the
61 * size of the dcache depends on the inode cache and thus is dynamic.
62 *
63 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
64 * resolution to correspond with current state of the code.
65 *
66 * Note that the symlink resolution is not *completely* iterative.
67 * There is still a significant amount of tail- and mid- recursion in
68 * the algorithm. Also, note that <fs>_readlink() is not used in
69 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
70 * may return different results than <fs>_follow_link(). Many virtual
71 * filesystems (including /proc) exhibit this behavior.
72 */
73
74/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
75 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
76 * and the name already exists in form of a symlink, try to create the new
77 * name indicated by the symlink. The old code always complained that the
78 * name already exists, due to not following the symlink even if its target
79 * is nonexistent. The new semantics affects also mknod() and link() when
25985edc 80 * the name is a symlink pointing to a non-existent name.
1da177e4
LT
81 *
82 * I don't know which semantics is the right one, since I have no access
83 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
84 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
85 * "old" one. Personally, I think the new semantics is much more logical.
86 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
87 * file does succeed in both HP-UX and SunOs, but not in Solaris
88 * and in the old Linux semantics.
89 */
90
91/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
92 * semantics. See the comments in "open_namei" and "do_link" below.
93 *
94 * [10-Sep-98 Alan Modra] Another symlink change.
95 */
96
97/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
98 * inside the path - always follow.
99 * in the last component in creation/removal/renaming - never follow.
100 * if LOOKUP_FOLLOW passed - follow.
101 * if the pathname has trailing slashes - follow.
102 * otherwise - don't follow.
103 * (applied in that order).
104 *
105 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
106 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
107 * During the 2.4 we need to fix the userland stuff depending on it -
108 * hopefully we will be able to get rid of that wart in 2.5. So far only
109 * XEmacs seems to be relying on it...
110 */
111/*
112 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
a11f3a05 113 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
1da177e4
LT
114 * any extra contention...
115 */
116
117/* In order to reduce some races, while at the same time doing additional
118 * checking and hopefully speeding things up, we copy filenames to the
119 * kernel data space before using them..
120 *
121 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
122 * PATH_MAX includes the nul terminator --RR.
123 */
91a27b2a 124
fd2f7cb5 125#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
7950e385 126
51f39a1f 127struct filename *
91a27b2a
JL
128getname_flags(const char __user *filename, int flags, int *empty)
129{
94b5d262 130 struct filename *result;
7950e385 131 char *kname;
94b5d262 132 int len;
4043cde8 133
7ac86265
JL
134 result = audit_reusename(filename);
135 if (result)
136 return result;
137
7950e385 138 result = __getname();
3f9f0aa6 139 if (unlikely(!result))
4043cde8
EP
140 return ERR_PTR(-ENOMEM);
141
7950e385
JL
142 /*
143 * First, try to embed the struct filename inside the names_cache
144 * allocation
145 */
fd2f7cb5 146 kname = (char *)result->iname;
91a27b2a 147 result->name = kname;
7950e385 148
94b5d262 149 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
91a27b2a 150 if (unlikely(len < 0)) {
94b5d262
AV
151 __putname(result);
152 return ERR_PTR(len);
91a27b2a 153 }
3f9f0aa6 154
7950e385
JL
155 /*
156 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
157 * separate struct filename so we can dedicate the entire
158 * names_cache allocation for the pathname, and re-do the copy from
159 * userland.
160 */
94b5d262 161 if (unlikely(len == EMBEDDED_NAME_MAX)) {
fd2f7cb5 162 const size_t size = offsetof(struct filename, iname[1]);
7950e385
JL
163 kname = (char *)result;
164
fd2f7cb5
AV
165 /*
166 * size is chosen that way we to guarantee that
167 * result->iname[0] is within the same object and that
168 * kname can't be equal to result->iname, no matter what.
169 */
170 result = kzalloc(size, GFP_KERNEL);
94b5d262
AV
171 if (unlikely(!result)) {
172 __putname(kname);
173 return ERR_PTR(-ENOMEM);
7950e385
JL
174 }
175 result->name = kname;
94b5d262
AV
176 len = strncpy_from_user(kname, filename, PATH_MAX);
177 if (unlikely(len < 0)) {
178 __putname(kname);
179 kfree(result);
180 return ERR_PTR(len);
181 }
182 if (unlikely(len == PATH_MAX)) {
183 __putname(kname);
184 kfree(result);
185 return ERR_PTR(-ENAMETOOLONG);
186 }
7950e385
JL
187 }
188
94b5d262 189 result->refcnt = 1;
3f9f0aa6
LT
190 /* The empty path is special. */
191 if (unlikely(!len)) {
192 if (empty)
4043cde8 193 *empty = 1;
94b5d262
AV
194 if (!(flags & LOOKUP_EMPTY)) {
195 putname(result);
196 return ERR_PTR(-ENOENT);
197 }
1da177e4 198 }
3f9f0aa6 199
7950e385 200 result->uptr = filename;
c4ad8f98 201 result->aname = NULL;
7950e385
JL
202 audit_getname(result);
203 return result;
1da177e4
LT
204}
205
91a27b2a
JL
206struct filename *
207getname(const char __user * filename)
f52e0c11 208{
f7493e5d 209 return getname_flags(filename, 0, NULL);
f52e0c11
AV
210}
211
c4ad8f98
LT
212struct filename *
213getname_kernel(const char * filename)
214{
215 struct filename *result;
08518549 216 int len = strlen(filename) + 1;
c4ad8f98
LT
217
218 result = __getname();
219 if (unlikely(!result))
220 return ERR_PTR(-ENOMEM);
221
08518549 222 if (len <= EMBEDDED_NAME_MAX) {
fd2f7cb5 223 result->name = (char *)result->iname;
08518549 224 } else if (len <= PATH_MAX) {
30ce4d19 225 const size_t size = offsetof(struct filename, iname[1]);
08518549
PM
226 struct filename *tmp;
227
30ce4d19 228 tmp = kmalloc(size, GFP_KERNEL);
08518549
PM
229 if (unlikely(!tmp)) {
230 __putname(result);
231 return ERR_PTR(-ENOMEM);
232 }
233 tmp->name = (char *)result;
08518549
PM
234 result = tmp;
235 } else {
236 __putname(result);
237 return ERR_PTR(-ENAMETOOLONG);
238 }
239 memcpy((char *)result->name, filename, len);
c4ad8f98
LT
240 result->uptr = NULL;
241 result->aname = NULL;
55422d0b 242 result->refcnt = 1;
fd3522fd 243 audit_getname(result);
c4ad8f98 244
c4ad8f98
LT
245 return result;
246}
247
91a27b2a 248void putname(struct filename *name)
1da177e4 249{
55422d0b
PM
250 BUG_ON(name->refcnt <= 0);
251
252 if (--name->refcnt > 0)
253 return;
254
fd2f7cb5 255 if (name->name != name->iname) {
55422d0b
PM
256 __putname(name->name);
257 kfree(name);
258 } else
259 __putname(name);
1da177e4 260}
1da177e4 261
e77819e5
LT
262static int check_acl(struct inode *inode, int mask)
263{
84635d68 264#ifdef CONFIG_FS_POSIX_ACL
e77819e5
LT
265 struct posix_acl *acl;
266
e77819e5 267 if (mask & MAY_NOT_BLOCK) {
3567866b
AV
268 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
269 if (!acl)
e77819e5 270 return -EAGAIN;
3567866b 271 /* no ->get_acl() calls in RCU mode... */
b8a7a3a6 272 if (is_uncached_acl(acl))
3567866b 273 return -ECHILD;
206b1d09 274 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
e77819e5
LT
275 }
276
2982baa2
CH
277 acl = get_acl(inode, ACL_TYPE_ACCESS);
278 if (IS_ERR(acl))
279 return PTR_ERR(acl);
e77819e5
LT
280 if (acl) {
281 int error = posix_acl_permission(inode, acl, mask);
282 posix_acl_release(acl);
283 return error;
284 }
84635d68 285#endif
e77819e5
LT
286
287 return -EAGAIN;
288}
289
5909ccaa 290/*
948409c7 291 * This does the basic permission checking
1da177e4 292 */
7e40145e 293static int acl_permission_check(struct inode *inode, int mask)
1da177e4 294{
26cf46be 295 unsigned int mode = inode->i_mode;
1da177e4 296
8e96e3b7 297 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
1da177e4
LT
298 mode >>= 6;
299 else {
e77819e5 300 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
7e40145e 301 int error = check_acl(inode, mask);
b74c79e9
NP
302 if (error != -EAGAIN)
303 return error;
1da177e4
LT
304 }
305
306 if (in_group_p(inode->i_gid))
307 mode >>= 3;
308 }
309
310 /*
311 * If the DACs are ok we don't need any capability check.
312 */
9c2c7039 313 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4 314 return 0;
5909ccaa
LT
315 return -EACCES;
316}
317
318/**
b74c79e9 319 * generic_permission - check for access rights on a Posix-like filesystem
5909ccaa 320 * @inode: inode to check access rights for
8fd90c8d 321 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
5909ccaa
LT
322 *
323 * Used to check for read/write/execute permissions on a file.
324 * We use "fsuid" for this, letting us set arbitrary permissions
325 * for filesystem access without changing the "normal" uids which
b74c79e9
NP
326 * are used for other things.
327 *
328 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
329 * request cannot be satisfied (eg. requires blocking or too much complexity).
330 * It would then be called again in ref-walk mode.
5909ccaa 331 */
2830ba7f 332int generic_permission(struct inode *inode, int mask)
5909ccaa
LT
333{
334 int ret;
335
336 /*
948409c7 337 * Do the basic permission checks.
5909ccaa 338 */
7e40145e 339 ret = acl_permission_check(inode, mask);
5909ccaa
LT
340 if (ret != -EACCES)
341 return ret;
1da177e4 342
d594e7ec
AV
343 if (S_ISDIR(inode->i_mode)) {
344 /* DACs are overridable for directories */
d594e7ec 345 if (!(mask & MAY_WRITE))
23adbe12
AL
346 if (capable_wrt_inode_uidgid(inode,
347 CAP_DAC_READ_SEARCH))
d594e7ec 348 return 0;
23adbe12 349 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
1da177e4 350 return 0;
2a4c2242
SS
351 return -EACCES;
352 }
1da177e4
LT
353
354 /*
355 * Searching includes executable on directories, else just read.
356 */
7ea66001 357 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec 358 if (mask == MAY_READ)
23adbe12 359 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
1da177e4 360 return 0;
2a4c2242
SS
361 /*
362 * Read/write DACs are always overridable.
363 * Executable DACs are overridable when there is
364 * at least one exec bit set.
365 */
366 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
367 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
368 return 0;
1da177e4
LT
369
370 return -EACCES;
371}
4d359507 372EXPORT_SYMBOL(generic_permission);
1da177e4 373
3ddcd056
LT
374/*
375 * We _really_ want to just do "generic_permission()" without
376 * even looking at the inode->i_op values. So we keep a cache
377 * flag in inode->i_opflags, that says "this has not special
378 * permission function, use the fast case".
379 */
380static inline int do_inode_permission(struct inode *inode, int mask)
381{
382 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
383 if (likely(inode->i_op->permission))
384 return inode->i_op->permission(inode, mask);
385
386 /* This gets set once for the inode lifetime */
387 spin_lock(&inode->i_lock);
388 inode->i_opflags |= IOP_FASTPERM;
389 spin_unlock(&inode->i_lock);
390 }
391 return generic_permission(inode, mask);
392}
393
0bdaea90
DH
394/**
395 * sb_permission - Check superblock-level permissions
396 * @sb: Superblock of inode to check permission on
55852635 397 * @inode: Inode to check permission on
0bdaea90
DH
398 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
399 *
400 * Separate out file-system wide checks from inode-specific permission checks.
401 */
402static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
403{
404 if (unlikely(mask & MAY_WRITE)) {
405 umode_t mode = inode->i_mode;
406
407 /* Nobody gets write access to a read-only fs. */
bc98a42c 408 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
0bdaea90
DH
409 return -EROFS;
410 }
411 return 0;
412}
413
414/**
415 * inode_permission - Check for access rights to a given inode
416 * @inode: Inode to check permission on
417 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
418 *
419 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
420 * this, letting us set arbitrary permissions for filesystem access without
421 * changing the "normal" UIDs which are used for other things.
422 *
423 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
424 */
425int inode_permission(struct inode *inode, int mask)
426{
427 int retval;
428
429 retval = sb_permission(inode->i_sb, inode, mask);
430 if (retval)
431 return retval;
4bfd054a
EB
432
433 if (unlikely(mask & MAY_WRITE)) {
434 /*
435 * Nobody gets write access to an immutable file.
436 */
437 if (IS_IMMUTABLE(inode))
438 return -EPERM;
439
440 /*
441 * Updating mtime will likely cause i_uid and i_gid to be
442 * written back improperly if their true value is unknown
443 * to the vfs.
444 */
445 if (HAS_UNMAPPED_ID(inode))
446 return -EACCES;
447 }
448
449 retval = do_inode_permission(inode, mask);
450 if (retval)
451 return retval;
452
453 retval = devcgroup_inode_permission(inode, mask);
454 if (retval)
455 return retval;
456
457 return security_inode_permission(inode, mask);
0bdaea90 458}
4d359507 459EXPORT_SYMBOL(inode_permission);
0bdaea90 460
5dd784d0
JB
461/**
462 * path_get - get a reference to a path
463 * @path: path to get the reference to
464 *
465 * Given a path increment the reference count to the dentry and the vfsmount.
466 */
dcf787f3 467void path_get(const struct path *path)
5dd784d0
JB
468{
469 mntget(path->mnt);
470 dget(path->dentry);
471}
472EXPORT_SYMBOL(path_get);
473
1d957f9b
JB
474/**
475 * path_put - put a reference to a path
476 * @path: path to put the reference to
477 *
478 * Given a path decrement the reference count to the dentry and the vfsmount.
479 */
dcf787f3 480void path_put(const struct path *path)
1da177e4 481{
1d957f9b
JB
482 dput(path->dentry);
483 mntput(path->mnt);
1da177e4 484}
1d957f9b 485EXPORT_SYMBOL(path_put);
1da177e4 486
894bc8c4 487#define EMBEDDED_LEVELS 2
1f55a6ec
AV
488struct nameidata {
489 struct path path;
1cf2665b 490 struct qstr last;
1f55a6ec
AV
491 struct path root;
492 struct inode *inode; /* path.dentry.d_inode */
493 unsigned int flags;
ab87f9a5 494 unsigned seq, m_seq, r_seq;
1f55a6ec
AV
495 int last_type;
496 unsigned depth;
756daf26 497 int total_link_count;
697fc6ca
AV
498 struct saved {
499 struct path link;
fceef393 500 struct delayed_call done;
697fc6ca 501 const char *name;
0450b2d1 502 unsigned seq;
894bc8c4 503 } *stack, internal[EMBEDDED_LEVELS];
9883d185
AV
504 struct filename *name;
505 struct nameidata *saved;
506 unsigned root_seq;
507 int dfd;
0f705953
AV
508 kuid_t dir_uid;
509 umode_t dir_mode;
3859a271 510} __randomize_layout;
1f55a6ec 511
9883d185 512static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
894bc8c4 513{
756daf26
N
514 struct nameidata *old = current->nameidata;
515 p->stack = p->internal;
c8a53ee5
AV
516 p->dfd = dfd;
517 p->name = name;
756daf26 518 p->total_link_count = old ? old->total_link_count : 0;
9883d185 519 p->saved = old;
756daf26 520 current->nameidata = p;
894bc8c4
AV
521}
522
9883d185 523static void restore_nameidata(void)
894bc8c4 524{
9883d185 525 struct nameidata *now = current->nameidata, *old = now->saved;
756daf26
N
526
527 current->nameidata = old;
528 if (old)
529 old->total_link_count = now->total_link_count;
e1a63bbc 530 if (now->stack != now->internal)
756daf26 531 kfree(now->stack);
894bc8c4
AV
532}
533
60ef60c7 534static bool nd_alloc_stack(struct nameidata *nd)
894bc8c4 535{
bc40aee0
AV
536 struct saved *p;
537
60ef60c7
AV
538 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
539 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
540 if (unlikely(!p))
541 return false;
894bc8c4
AV
542 memcpy(p, nd->internal, sizeof(nd->internal));
543 nd->stack = p;
60ef60c7 544 return true;
894bc8c4
AV
545}
546
397d425d 547/**
6b03f7ed 548 * path_connected - Verify that a dentry is below mnt.mnt_root
397d425d
EB
549 *
550 * Rename can sometimes move a file or directory outside of a bind
551 * mount, path_connected allows those cases to be detected.
552 */
6b03f7ed 553static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
397d425d 554{
95dd7758 555 struct super_block *sb = mnt->mnt_sb;
397d425d 556
95dd7758
EB
557 /* Bind mounts and multi-root filesystems can have disconnected paths */
558 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
397d425d
EB
559 return true;
560
6b03f7ed 561 return is_subdir(dentry, mnt->mnt_root);
397d425d
EB
562}
563
7973387a
AV
564static void drop_links(struct nameidata *nd)
565{
566 int i = nd->depth;
567 while (i--) {
568 struct saved *last = nd->stack + i;
fceef393
AV
569 do_delayed_call(&last->done);
570 clear_delayed_call(&last->done);
7973387a
AV
571 }
572}
573
574static void terminate_walk(struct nameidata *nd)
575{
576 drop_links(nd);
577 if (!(nd->flags & LOOKUP_RCU)) {
578 int i;
579 path_put(&nd->path);
580 for (i = 0; i < nd->depth; i++)
581 path_put(&nd->stack[i].link);
84a2bd39 582 if (nd->flags & LOOKUP_ROOT_GRABBED) {
102b8af2 583 path_put(&nd->root);
84a2bd39 584 nd->flags &= ~LOOKUP_ROOT_GRABBED;
102b8af2 585 }
7973387a
AV
586 } else {
587 nd->flags &= ~LOOKUP_RCU;
7973387a
AV
588 rcu_read_unlock();
589 }
590 nd->depth = 0;
591}
592
593/* path_put is needed afterwards regardless of success or failure */
2aa38470 594static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
7973387a 595{
2aa38470 596 int res = __legitimize_mnt(path->mnt, mseq);
7973387a
AV
597 if (unlikely(res)) {
598 if (res > 0)
599 path->mnt = NULL;
600 path->dentry = NULL;
601 return false;
602 }
603 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
604 path->dentry = NULL;
605 return false;
606 }
607 return !read_seqcount_retry(&path->dentry->d_seq, seq);
608}
609
2aa38470
AV
610static inline bool legitimize_path(struct nameidata *nd,
611 struct path *path, unsigned seq)
612{
613 return __legitimize_path(path, nd->m_seq, seq);
614}
615
7973387a
AV
616static bool legitimize_links(struct nameidata *nd)
617{
618 int i;
619 for (i = 0; i < nd->depth; i++) {
620 struct saved *last = nd->stack + i;
621 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
622 drop_links(nd);
623 nd->depth = i + 1;
624 return false;
625 }
626 }
627 return true;
628}
629
ee594bff
AV
630static bool legitimize_root(struct nameidata *nd)
631{
adb21d2b
AS
632 /*
633 * For scoped-lookups (where nd->root has been zeroed), we need to
634 * restart the whole lookup from scratch -- because set_root() is wrong
635 * for these lookups (nd->dfd is the root, not the filesystem root).
636 */
637 if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
638 return false;
639 /* Nothing to do if nd->root is zero or is managed by the VFS user. */
ee594bff
AV
640 if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
641 return true;
84a2bd39 642 nd->flags |= LOOKUP_ROOT_GRABBED;
ee594bff
AV
643 return legitimize_path(nd, &nd->root, nd->root_seq);
644}
645
19660af7 646/*
31e6b01f 647 * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af7
AV
648 * Documentation/filesystems/path-lookup.txt). In situations when we can't
649 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
57e3715c 650 * normal reference counts on dentries and vfsmounts to transition to ref-walk
19660af7
AV
651 * mode. Refcounts are grabbed at the last known good point before rcu-walk
652 * got stuck, so ref-walk may continue from there. If this is not successful
653 * (eg. a seqcount has changed), then failure is returned and it's up to caller
654 * to restart the path walk from the beginning in ref-walk mode.
31e6b01f 655 */
31e6b01f
NP
656
657/**
19660af7
AV
658 * unlazy_walk - try to switch to ref-walk mode.
659 * @nd: nameidata pathwalk data
39191628 660 * Returns: 0 on success, -ECHILD on failure
31e6b01f 661 *
4675ac39
AV
662 * unlazy_walk attempts to legitimize the current nd->path and nd->root
663 * for ref-walk mode.
664 * Must be called from rcu-walk context.
7973387a
AV
665 * Nothing should touch nameidata between unlazy_walk() failure and
666 * terminate_walk().
31e6b01f 667 */
4675ac39 668static int unlazy_walk(struct nameidata *nd)
31e6b01f 669{
31e6b01f
NP
670 struct dentry *parent = nd->path.dentry;
671
672 BUG_ON(!(nd->flags & LOOKUP_RCU));
e5c832d5 673
4675ac39
AV
674 nd->flags &= ~LOOKUP_RCU;
675 if (unlikely(!legitimize_links(nd)))
4675ac39 676 goto out1;
84a2bd39
AV
677 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
678 goto out;
ee594bff
AV
679 if (unlikely(!legitimize_root(nd)))
680 goto out;
4675ac39
AV
681 rcu_read_unlock();
682 BUG_ON(nd->inode != parent->d_inode);
683 return 0;
684
84a2bd39 685out1:
4675ac39
AV
686 nd->path.mnt = NULL;
687 nd->path.dentry = NULL;
4675ac39
AV
688out:
689 rcu_read_unlock();
690 return -ECHILD;
691}
692
693/**
694 * unlazy_child - try to switch to ref-walk mode.
695 * @nd: nameidata pathwalk data
696 * @dentry: child of nd->path.dentry
697 * @seq: seq number to check dentry against
698 * Returns: 0 on success, -ECHILD on failure
699 *
700 * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
701 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
702 * @nd. Must be called from rcu-walk context.
703 * Nothing should touch nameidata between unlazy_child() failure and
704 * terminate_walk().
705 */
706static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
707{
708 BUG_ON(!(nd->flags & LOOKUP_RCU));
709
e5c832d5 710 nd->flags &= ~LOOKUP_RCU;
7973387a
AV
711 if (unlikely(!legitimize_links(nd)))
712 goto out2;
713 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
714 goto out2;
4675ac39 715 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
7973387a 716 goto out1;
48a066e7 717
15570086 718 /*
4675ac39
AV
719 * We need to move both the parent and the dentry from the RCU domain
720 * to be properly refcounted. And the sequence number in the dentry
721 * validates *both* dentry counters, since we checked the sequence
722 * number of the parent after we got the child sequence number. So we
723 * know the parent must still be valid if the child sequence number is
15570086 724 */
4675ac39
AV
725 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
726 goto out;
84a2bd39
AV
727 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
728 goto out_dput;
e5c832d5
LT
729 /*
730 * Sequence counts matched. Now make sure that the root is
731 * still valid and get it if required.
732 */
84a2bd39
AV
733 if (unlikely(!legitimize_root(nd)))
734 goto out_dput;
8b61e74f 735 rcu_read_unlock();
31e6b01f 736 return 0;
19660af7 737
7973387a
AV
738out2:
739 nd->path.mnt = NULL;
740out1:
741 nd->path.dentry = NULL;
e5c832d5 742out:
8b61e74f 743 rcu_read_unlock();
84a2bd39
AV
744 return -ECHILD;
745out_dput:
746 rcu_read_unlock();
747 dput(dentry);
31e6b01f
NP
748 return -ECHILD;
749}
750
4ce16ef3 751static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
34286d66 752{
a89f8337
AV
753 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
754 return dentry->d_op->d_revalidate(dentry, flags);
755 else
756 return 1;
34286d66
NP
757}
758
9f1fafee
AV
759/**
760 * complete_walk - successful completion of path walk
761 * @nd: pointer nameidata
39159de2 762 *
9f1fafee
AV
763 * If we had been in RCU mode, drop out of it and legitimize nd->path.
764 * Revalidate the final result, unless we'd already done that during
765 * the path walk or the filesystem doesn't ask for it. Return 0 on
766 * success, -error on failure. In case of failure caller does not
767 * need to drop nd->path.
39159de2 768 */
9f1fafee 769static int complete_walk(struct nameidata *nd)
39159de2 770{
16c2cd71 771 struct dentry *dentry = nd->path.dentry;
39159de2 772 int status;
39159de2 773
9f1fafee 774 if (nd->flags & LOOKUP_RCU) {
adb21d2b
AS
775 /*
776 * We don't want to zero nd->root for scoped-lookups or
777 * externally-managed nd->root.
778 */
779 if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED)))
9f1fafee 780 nd->root.mnt = NULL;
4675ac39 781 if (unlikely(unlazy_walk(nd)))
9f1fafee 782 return -ECHILD;
9f1fafee
AV
783 }
784
adb21d2b
AS
785 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
786 /*
787 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
788 * ever step outside the root during lookup" and should already
789 * be guaranteed by the rest of namei, we want to avoid a namei
790 * BUG resulting in userspace being given a path that was not
791 * scoped within the root at some point during the lookup.
792 *
793 * So, do a final sanity-check to make sure that in the
794 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
795 * we won't silently return an fd completely outside of the
796 * requested root to userspace.
797 *
798 * Userspace could move the path outside the root after this
799 * check, but as discussed elsewhere this is not a concern (the
800 * resolved file was inside the root at some point).
801 */
802 if (!path_is_under(&nd->path, &nd->root))
803 return -EXDEV;
804 }
805
16c2cd71
AV
806 if (likely(!(nd->flags & LOOKUP_JUMPED)))
807 return 0;
808
ecf3d1f1 809 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
39159de2
JL
810 return 0;
811
ecf3d1f1 812 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
39159de2
JL
813 if (status > 0)
814 return 0;
815
16c2cd71 816 if (!status)
39159de2 817 status = -ESTALE;
16c2cd71 818
39159de2
JL
819 return status;
820}
821
740a1678 822static int set_root(struct nameidata *nd)
31e6b01f 823{
7bd88377 824 struct fs_struct *fs = current->fs;
c28cc364 825
adb21d2b
AS
826 /*
827 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
828 * still have to ensure it doesn't happen because it will cause a breakout
829 * from the dirfd.
830 */
831 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
832 return -ENOTRECOVERABLE;
833
9e6697e2
AV
834 if (nd->flags & LOOKUP_RCU) {
835 unsigned seq;
836
837 do {
838 seq = read_seqcount_begin(&fs->seq);
839 nd->root = fs->root;
840 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
841 } while (read_seqcount_retry(&fs->seq, seq));
842 } else {
843 get_fs_root(fs, &nd->root);
84a2bd39 844 nd->flags |= LOOKUP_ROOT_GRABBED;
9e6697e2 845 }
740a1678 846 return 0;
31e6b01f
NP
847}
848
248fb5b9
AV
849static int nd_jump_root(struct nameidata *nd)
850{
adb21d2b
AS
851 if (unlikely(nd->flags & LOOKUP_BENEATH))
852 return -EXDEV;
72ba2929
AS
853 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
854 /* Absolute path arguments to path_init() are allowed. */
855 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
856 return -EXDEV;
857 }
740a1678
AS
858 if (!nd->root.mnt) {
859 int error = set_root(nd);
860 if (error)
861 return error;
862 }
248fb5b9
AV
863 if (nd->flags & LOOKUP_RCU) {
864 struct dentry *d;
865 nd->path = nd->root;
866 d = nd->path.dentry;
867 nd->inode = d->d_inode;
868 nd->seq = nd->root_seq;
869 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
870 return -ECHILD;
871 } else {
872 path_put(&nd->path);
873 nd->path = nd->root;
874 path_get(&nd->path);
875 nd->inode = nd->path.dentry->d_inode;
876 }
877 nd->flags |= LOOKUP_JUMPED;
878 return 0;
879}
880
b5fb63c1 881/*
6b255391 882 * Helper to directly jump to a known parsed path from ->get_link,
b5fb63c1
CH
883 * caller must have taken a reference to path beforehand.
884 */
1bc82070 885int nd_jump_link(struct path *path)
b5fb63c1 886{
4b99d499 887 int error = -ELOOP;
6e77137b 888 struct nameidata *nd = current->nameidata;
b5fb63c1 889
4b99d499
AS
890 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
891 goto err;
892
72ba2929
AS
893 error = -EXDEV;
894 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
895 if (nd->path.mnt != path->mnt)
896 goto err;
897 }
adb21d2b
AS
898 /* Not currently safe for scoped-lookups. */
899 if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
900 goto err;
72ba2929 901
4b99d499 902 path_put(&nd->path);
b5fb63c1
CH
903 nd->path = *path;
904 nd->inode = nd->path.dentry->d_inode;
905 nd->flags |= LOOKUP_JUMPED;
1bc82070 906 return 0;
4b99d499
AS
907
908err:
909 path_put(path);
910 return error;
b5fb63c1
CH
911}
912
b9ff4429 913static inline void put_link(struct nameidata *nd)
574197e0 914{
21c3003d 915 struct saved *last = nd->stack + --nd->depth;
fceef393 916 do_delayed_call(&last->done);
6548fae2
AV
917 if (!(nd->flags & LOOKUP_RCU))
918 path_put(&last->link);
574197e0
AV
919}
920
561ec64a
LT
921int sysctl_protected_symlinks __read_mostly = 0;
922int sysctl_protected_hardlinks __read_mostly = 0;
30aba665
SM
923int sysctl_protected_fifos __read_mostly;
924int sysctl_protected_regular __read_mostly;
800179c9
KC
925
926/**
927 * may_follow_link - Check symlink following for unsafe situations
55852635 928 * @nd: nameidata pathwalk data
800179c9
KC
929 *
930 * In the case of the sysctl_protected_symlinks sysctl being enabled,
931 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
932 * in a sticky world-writable directory. This is to protect privileged
933 * processes from failing races against path names that may change out
934 * from under them by way of other users creating malicious symlinks.
935 * It will permit symlinks to be followed only when outside a sticky
936 * world-writable directory, or when the uid of the symlink and follower
937 * match, or when the directory owner matches the symlink's owner.
938 *
939 * Returns 0 if following the symlink is allowed, -ve on error.
940 */
ad6cc4c3 941static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
800179c9 942{
800179c9
KC
943 if (!sysctl_protected_symlinks)
944 return 0;
945
946 /* Allowed if owner and follower match. */
81abe27b 947 if (uid_eq(current_cred()->fsuid, inode->i_uid))
800179c9
KC
948 return 0;
949
950 /* Allowed if parent directory not sticky and world-writable. */
0f705953 951 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
800179c9
KC
952 return 0;
953
954 /* Allowed if parent directory and link owner match. */
0f705953 955 if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, inode->i_uid))
800179c9
KC
956 return 0;
957
31956502
AV
958 if (nd->flags & LOOKUP_RCU)
959 return -ECHILD;
960
ea841baf 961 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
245d7369 962 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
800179c9
KC
963 return -EACCES;
964}
965
966/**
967 * safe_hardlink_source - Check for safe hardlink conditions
968 * @inode: the source inode to hardlink from
969 *
970 * Return false if at least one of the following conditions:
971 * - inode is not a regular file
972 * - inode is setuid
973 * - inode is setgid and group-exec
974 * - access failure for read and write
975 *
976 * Otherwise returns true.
977 */
978static bool safe_hardlink_source(struct inode *inode)
979{
980 umode_t mode = inode->i_mode;
981
982 /* Special files should not get pinned to the filesystem. */
983 if (!S_ISREG(mode))
984 return false;
985
986 /* Setuid files should not get pinned to the filesystem. */
987 if (mode & S_ISUID)
988 return false;
989
990 /* Executable setgid files should not get pinned to the filesystem. */
991 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
992 return false;
993
994 /* Hardlinking to unreadable or unwritable sources is dangerous. */
995 if (inode_permission(inode, MAY_READ | MAY_WRITE))
996 return false;
997
998 return true;
999}
1000
1001/**
1002 * may_linkat - Check permissions for creating a hardlink
1003 * @link: the source to hardlink from
1004 *
1005 * Block hardlink when all of:
1006 * - sysctl_protected_hardlinks enabled
1007 * - fsuid does not match inode
1008 * - hardlink source is unsafe (see safe_hardlink_source() above)
f2ca3796 1009 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
800179c9
KC
1010 *
1011 * Returns 0 if successful, -ve on error.
1012 */
1013static int may_linkat(struct path *link)
1014{
593d1ce8
EB
1015 struct inode *inode = link->dentry->d_inode;
1016
1017 /* Inode writeback is not safe when the uid or gid are invalid. */
1018 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
1019 return -EOVERFLOW;
800179c9
KC
1020
1021 if (!sysctl_protected_hardlinks)
1022 return 0;
1023
800179c9
KC
1024 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1025 * otherwise, it must be a safe source.
1026 */
cc658db4 1027 if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
800179c9
KC
1028 return 0;
1029
245d7369 1030 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
800179c9
KC
1031 return -EPERM;
1032}
1033
30aba665
SM
1034/**
1035 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1036 * should be allowed, or not, on files that already
1037 * exist.
d0cb5018
AV
1038 * @dir_mode: mode bits of directory
1039 * @dir_uid: owner of directory
30aba665
SM
1040 * @inode: the inode of the file to open
1041 *
1042 * Block an O_CREAT open of a FIFO (or a regular file) when:
1043 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1044 * - the file already exists
1045 * - we are in a sticky directory
1046 * - we don't own the file
1047 * - the owner of the directory doesn't own the file
1048 * - the directory is world writable
1049 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1050 * the directory doesn't have to be world writable: being group writable will
1051 * be enough.
1052 *
1053 * Returns 0 if the open is allowed, -ve on error.
1054 */
d0cb5018 1055static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid,
30aba665
SM
1056 struct inode * const inode)
1057{
1058 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1059 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
d0cb5018
AV
1060 likely(!(dir_mode & S_ISVTX)) ||
1061 uid_eq(inode->i_uid, dir_uid) ||
30aba665
SM
1062 uid_eq(current_fsuid(), inode->i_uid))
1063 return 0;
1064
d0cb5018
AV
1065 if (likely(dir_mode & 0002) ||
1066 (dir_mode & 0020 &&
30aba665
SM
1067 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1068 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
245d7369
KC
1069 const char *operation = S_ISFIFO(inode->i_mode) ?
1070 "sticky_create_fifo" :
1071 "sticky_create_regular";
1072 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
30aba665
SM
1073 return -EACCES;
1074 }
1075 return 0;
1076}
1077
f015f126
DH
1078/*
1079 * follow_up - Find the mountpoint of path's vfsmount
1080 *
1081 * Given a path, find the mountpoint of its source file system.
1082 * Replace @path with the path of the mountpoint in the parent mount.
1083 * Up is towards /.
1084 *
1085 * Return 1 if we went up a level and 0 if we were already at the
1086 * root.
1087 */
bab77ebf 1088int follow_up(struct path *path)
1da177e4 1089{
0714a533
AV
1090 struct mount *mnt = real_mount(path->mnt);
1091 struct mount *parent;
1da177e4 1092 struct dentry *mountpoint;
99b7db7b 1093
48a066e7 1094 read_seqlock_excl(&mount_lock);
0714a533 1095 parent = mnt->mnt_parent;
3c0a6163 1096 if (parent == mnt) {
48a066e7 1097 read_sequnlock_excl(&mount_lock);
1da177e4
LT
1098 return 0;
1099 }
0714a533 1100 mntget(&parent->mnt);
a73324da 1101 mountpoint = dget(mnt->mnt_mountpoint);
48a066e7 1102 read_sequnlock_excl(&mount_lock);
bab77ebf
AV
1103 dput(path->dentry);
1104 path->dentry = mountpoint;
1105 mntput(path->mnt);
0714a533 1106 path->mnt = &parent->mnt;
1da177e4
LT
1107 return 1;
1108}
4d359507 1109EXPORT_SYMBOL(follow_up);
1da177e4 1110
7ef482fa
AV
1111static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
1112 struct path *path, unsigned *seqp)
1113{
1114 while (mnt_has_parent(m)) {
1115 struct dentry *mountpoint = m->mnt_mountpoint;
1116
1117 m = m->mnt_parent;
1118 if (unlikely(root->dentry == mountpoint &&
1119 root->mnt == &m->mnt))
1120 break;
1121 if (mountpoint != m->mnt.mnt_root) {
1122 path->mnt = &m->mnt;
1123 path->dentry = mountpoint;
1124 *seqp = read_seqcount_begin(&mountpoint->d_seq);
1125 return true;
1126 }
1127 }
1128 return false;
1129}
1130
2aa38470
AV
1131static bool choose_mountpoint(struct mount *m, const struct path *root,
1132 struct path *path)
1133{
1134 bool found;
1135
1136 rcu_read_lock();
1137 while (1) {
1138 unsigned seq, mseq = read_seqbegin(&mount_lock);
1139
1140 found = choose_mountpoint_rcu(m, root, path, &seq);
1141 if (unlikely(!found)) {
1142 if (!read_seqretry(&mount_lock, mseq))
1143 break;
1144 } else {
1145 if (likely(__legitimize_path(path, seq, mseq)))
1146 break;
1147 rcu_read_unlock();
1148 path_put(path);
1149 rcu_read_lock();
1150 }
1151 }
1152 rcu_read_unlock();
1153 return found;
1154}
1155
b5c84bf6 1156/*
9875cf80
DH
1157 * Perform an automount
1158 * - return -EISDIR to tell follow_managed() to stop and return the path we
1159 * were called with.
1da177e4 1160 */
1c9f5e06 1161static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
31e6b01f 1162{
25e195aa 1163 struct dentry *dentry = path->dentry;
9875cf80 1164
0ec26fd0
MS
1165 /* We don't want to mount if someone's just doing a stat -
1166 * unless they're stat'ing a directory and appended a '/' to
1167 * the name.
1168 *
1169 * We do, however, want to mount if someone wants to open or
1170 * create a file of any type under the mountpoint, wants to
1171 * traverse through the mountpoint or wants to open the
1172 * mounted directory. Also, autofs may mark negative dentries
1173 * as being automount points. These will need the attentions
1174 * of the daemon to instantiate them before they can be used.
9875cf80 1175 */
1c9f5e06 1176 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
5d38f049 1177 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
25e195aa 1178 dentry->d_inode)
5d38f049 1179 return -EISDIR;
0ec26fd0 1180
1c9f5e06 1181 if (count && (*count)++ >= MAXSYMLINKS)
9875cf80
DH
1182 return -ELOOP;
1183
25e195aa 1184 return finish_automount(dentry->d_op->d_automount(path), path);
463ffb2e
AV
1185}
1186
9875cf80 1187/*
9deed3eb
AV
1188 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1189 * dentries are pinned but not locked here, so negative dentry can go
1190 * positive right under us. Use of smp_load_acquire() provides a barrier
1191 * sufficient for ->d_inode and ->d_flags consistency.
9875cf80 1192 */
9deed3eb
AV
1193static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
1194 int *count, unsigned lookup_flags)
1da177e4 1195{
9deed3eb 1196 struct vfsmount *mnt = path->mnt;
9875cf80 1197 bool need_mntput = false;
8aef1884 1198 int ret = 0;
9875cf80 1199
9deed3eb 1200 while (flags & DCACHE_MANAGED_DENTRY) {
cc53ce53
DH
1201 /* Allow the filesystem to manage the transit without i_mutex
1202 * being held. */
d41efb52 1203 if (flags & DCACHE_MANAGE_TRANSIT) {
fb5f51c7 1204 ret = path->dentry->d_op->d_manage(path, false);
508c8772 1205 flags = smp_load_acquire(&path->dentry->d_flags);
cc53ce53 1206 if (ret < 0)
8aef1884 1207 break;
cc53ce53
DH
1208 }
1209
9deed3eb 1210 if (flags & DCACHE_MOUNTED) { // something's mounted on it..
9875cf80 1211 struct vfsmount *mounted = lookup_mnt(path);
9deed3eb 1212 if (mounted) { // ... in our namespace
9875cf80
DH
1213 dput(path->dentry);
1214 if (need_mntput)
1215 mntput(path->mnt);
1216 path->mnt = mounted;
1217 path->dentry = dget(mounted->mnt_root);
9deed3eb
AV
1218 // here we know it's positive
1219 flags = path->dentry->d_flags;
9875cf80
DH
1220 need_mntput = true;
1221 continue;
1222 }
9875cf80
DH
1223 }
1224
9deed3eb
AV
1225 if (!(flags & DCACHE_NEED_AUTOMOUNT))
1226 break;
9875cf80 1227
9deed3eb
AV
1228 // uncovered automount point
1229 ret = follow_automount(path, count, lookup_flags);
1230 flags = smp_load_acquire(&path->dentry->d_flags);
1231 if (ret < 0)
1232 break;
1da177e4 1233 }
8aef1884 1234
9deed3eb
AV
1235 if (ret == -EISDIR)
1236 ret = 0;
1237 // possible if you race with several mount --move
1238 if (need_mntput && path->mnt == mnt)
1239 mntput(path->mnt);
1240 if (!ret && unlikely(d_flags_negative(flags)))
d41efb52 1241 ret = -ENOENT;
9deed3eb 1242 *jumped = need_mntput;
8402752e 1243 return ret;
1da177e4
LT
1244}
1245
9deed3eb
AV
1246static inline int traverse_mounts(struct path *path, bool *jumped,
1247 int *count, unsigned lookup_flags)
1248{
1249 unsigned flags = smp_load_acquire(&path->dentry->d_flags);
1250
1251 /* fastpath */
1252 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
1253 *jumped = false;
1254 if (unlikely(d_flags_negative(flags)))
1255 return -ENOENT;
1256 return 0;
1257 }
1258 return __traverse_mounts(path, flags, jumped, count, lookup_flags);
1259}
1260
cc53ce53 1261int follow_down_one(struct path *path)
1da177e4
LT
1262{
1263 struct vfsmount *mounted;
1264
1c755af4 1265 mounted = lookup_mnt(path);
1da177e4 1266 if (mounted) {
9393bd07
AV
1267 dput(path->dentry);
1268 mntput(path->mnt);
1269 path->mnt = mounted;
1270 path->dentry = dget(mounted->mnt_root);
1da177e4
LT
1271 return 1;
1272 }
1273 return 0;
1274}
4d359507 1275EXPORT_SYMBOL(follow_down_one);
1da177e4 1276
9deed3eb
AV
1277/*
1278 * Follow down to the covering mount currently visible to userspace. At each
1279 * point, the filesystem owning that dentry may be queried as to whether the
1280 * caller is permitted to proceed or not.
1281 */
1282int follow_down(struct path *path)
1283{
1284 struct vfsmount *mnt = path->mnt;
1285 bool jumped;
1286 int ret = traverse_mounts(path, &jumped, NULL, 0);
1287
1288 if (path->mnt != mnt)
1289 mntput(mnt);
1290 return ret;
1291}
1292EXPORT_SYMBOL(follow_down);
1293
9875cf80 1294/*
287548e4
AV
1295 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1296 * we meet a managed dentry that would need blocking.
9875cf80
DH
1297 */
1298static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
254cf582 1299 struct inode **inode, unsigned *seqp)
9875cf80 1300{
ea936aeb
AV
1301 struct dentry *dentry = path->dentry;
1302 unsigned int flags = dentry->d_flags;
1303
1304 if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1305 return true;
1306
1307 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1308 return false;
1309
62a7375e 1310 for (;;) {
62a7375e
IK
1311 /*
1312 * Don't forget we might have a non-mountpoint managed dentry
1313 * that wants to block transit.
1314 */
ea936aeb
AV
1315 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1316 int res = dentry->d_op->d_manage(path, true);
1317 if (res)
1318 return res == -EISDIR;
1319 flags = dentry->d_flags;
b8faf035 1320 }
62a7375e 1321
ea936aeb
AV
1322 if (flags & DCACHE_MOUNTED) {
1323 struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1324 if (mounted) {
1325 path->mnt = &mounted->mnt;
1326 dentry = path->dentry = mounted->mnt.mnt_root;
1327 nd->flags |= LOOKUP_JUMPED;
1328 *seqp = read_seqcount_begin(&dentry->d_seq);
1329 *inode = dentry->d_inode;
1330 /*
1331 * We don't need to re-check ->d_seq after this
1332 * ->d_inode read - there will be an RCU delay
1333 * between mount hash removal and ->mnt_root
1334 * becoming unpinned.
1335 */
1336 flags = dentry->d_flags;
1337 continue;
1338 }
1339 if (read_seqretry(&mount_lock, nd->m_seq))
1340 return false;
1341 }
1342 return !(flags & DCACHE_NEED_AUTOMOUNT);
9875cf80 1343 }
287548e4
AV
1344}
1345
db3c9ade
AV
1346static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1347 struct path *path, struct inode **inode,
1348 unsigned int *seqp)
bd7c4b50 1349{
9deed3eb 1350 bool jumped;
db3c9ade 1351 int ret;
bd7c4b50 1352
db3c9ade
AV
1353 path->mnt = nd->path.mnt;
1354 path->dentry = dentry;
c153007b
AV
1355 if (nd->flags & LOOKUP_RCU) {
1356 unsigned int seq = *seqp;
1357 if (unlikely(!*inode))
1358 return -ENOENT;
1359 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
9deed3eb 1360 return 0;
c153007b
AV
1361 if (unlazy_child(nd, dentry, seq))
1362 return -ECHILD;
1363 // *path might've been clobbered by __follow_mount_rcu()
1364 path->mnt = nd->path.mnt;
1365 path->dentry = dentry;
1366 }
9deed3eb
AV
1367 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
1368 if (jumped) {
1369 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1370 ret = -EXDEV;
1371 else
1372 nd->flags |= LOOKUP_JUMPED;
1373 }
1374 if (unlikely(ret)) {
1375 dput(path->dentry);
1376 if (path->mnt != nd->path.mnt)
1377 mntput(path->mnt);
1378 } else {
bd7c4b50
AV
1379 *inode = d_backing_inode(path->dentry);
1380 *seqp = 0; /* out of RCU mode, so the value doesn't matter */
1381 }
1382 return ret;
1383}
1384
baa03890 1385/*
f4fdace9
OD
1386 * This looks up the name in dcache and possibly revalidates the found dentry.
1387 * NULL is returned if the dentry does not exist in the cache.
baa03890 1388 */
e3c13928
AV
1389static struct dentry *lookup_dcache(const struct qstr *name,
1390 struct dentry *dir,
6c51e513 1391 unsigned int flags)
baa03890 1392{
a89f8337 1393 struct dentry *dentry = d_lookup(dir, name);
bad61189 1394 if (dentry) {
a89f8337
AV
1395 int error = d_revalidate(dentry, flags);
1396 if (unlikely(error <= 0)) {
1397 if (!error)
1398 d_invalidate(dentry);
1399 dput(dentry);
1400 return ERR_PTR(error);
bad61189
MS
1401 }
1402 }
baa03890
NP
1403 return dentry;
1404}
1405
44396f4b 1406/*
a03ece5f
AV
1407 * Parent directory has inode locked exclusive. This is one
1408 * and only case when ->lookup() gets called on non in-lookup
1409 * dentries - as the matter of fact, this only gets called
1410 * when directory is guaranteed to have no in-lookup children
1411 * at all.
44396f4b 1412 */
e3c13928 1413static struct dentry *__lookup_hash(const struct qstr *name,
72bd866a 1414 struct dentry *base, unsigned int flags)
a3255546 1415{
6c51e513 1416 struct dentry *dentry = lookup_dcache(name, base, flags);
a03ece5f
AV
1417 struct dentry *old;
1418 struct inode *dir = base->d_inode;
a3255546 1419
6c51e513 1420 if (dentry)
bad61189 1421 return dentry;
a3255546 1422
a03ece5f
AV
1423 /* Don't create child dentry for a dead directory. */
1424 if (unlikely(IS_DEADDIR(dir)))
1425 return ERR_PTR(-ENOENT);
1426
6c51e513
AV
1427 dentry = d_alloc(base, name);
1428 if (unlikely(!dentry))
1429 return ERR_PTR(-ENOMEM);
1430
a03ece5f
AV
1431 old = dir->i_op->lookup(dir, dentry, flags);
1432 if (unlikely(old)) {
1433 dput(dentry);
1434 dentry = old;
1435 }
1436 return dentry;
a3255546
AV
1437}
1438
20e34357
AV
1439static struct dentry *lookup_fast(struct nameidata *nd,
1440 struct inode **inode,
1441 unsigned *seqp)
1da177e4 1442{
31e6b01f 1443 struct dentry *dentry, *parent = nd->path.dentry;
5a18fff2 1444 int status = 1;
9875cf80 1445
b04f784e
NP
1446 /*
1447 * Rename seqlock is not required here because in the off chance
5d0f49c1
AV
1448 * of a false negative due to a concurrent rename, the caller is
1449 * going to fall back to non-racy lookup.
b04f784e 1450 */
31e6b01f
NP
1451 if (nd->flags & LOOKUP_RCU) {
1452 unsigned seq;
da53be12 1453 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
5d0f49c1 1454 if (unlikely(!dentry)) {
4675ac39 1455 if (unlazy_walk(nd))
20e34357
AV
1456 return ERR_PTR(-ECHILD);
1457 return NULL;
5d0f49c1 1458 }
5a18fff2 1459
12f8ad4b
LT
1460 /*
1461 * This sequence count validates that the inode matches
1462 * the dentry name information from lookup.
1463 */
63afdfc7 1464 *inode = d_backing_inode(dentry);
5d0f49c1 1465 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
20e34357 1466 return ERR_PTR(-ECHILD);
12f8ad4b
LT
1467
1468 /*
1469 * This sequence count validates that the parent had no
1470 * changes while we did the lookup of the dentry above.
1471 *
1472 * The memory barrier in read_seqcount_begin of child is
1473 * enough, we can use __read_seqcount_retry here.
1474 */
5d0f49c1 1475 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
20e34357 1476 return ERR_PTR(-ECHILD);
5a18fff2 1477
254cf582 1478 *seqp = seq;
a89f8337 1479 status = d_revalidate(dentry, nd->flags);
c153007b 1480 if (likely(status > 0))
20e34357 1481 return dentry;
4675ac39 1482 if (unlazy_child(nd, dentry, seq))
20e34357 1483 return ERR_PTR(-ECHILD);
209a7fb2
AV
1484 if (unlikely(status == -ECHILD))
1485 /* we'd been told to redo it in non-rcu mode */
1486 status = d_revalidate(dentry, nd->flags);
5a18fff2 1487 } else {
e97cdc87 1488 dentry = __d_lookup(parent, &nd->last);
5d0f49c1 1489 if (unlikely(!dentry))
20e34357 1490 return NULL;
a89f8337 1491 status = d_revalidate(dentry, nd->flags);
9875cf80 1492 }
5a18fff2 1493 if (unlikely(status <= 0)) {
e9742b53 1494 if (!status)
5d0f49c1 1495 d_invalidate(dentry);
5542aa2f 1496 dput(dentry);
20e34357 1497 return ERR_PTR(status);
24643087 1498 }
20e34357 1499 return dentry;
697f514d
MS
1500}
1501
1502/* Fast lookup failed, do it the slow way */
88d8331a
AV
1503static struct dentry *__lookup_slow(const struct qstr *name,
1504 struct dentry *dir,
1505 unsigned int flags)
697f514d 1506{
88d8331a 1507 struct dentry *dentry, *old;
1936386e 1508 struct inode *inode = dir->d_inode;
d9171b93 1509 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1936386e 1510
1936386e 1511 /* Don't go there if it's already dead */
94bdd655 1512 if (unlikely(IS_DEADDIR(inode)))
88d8331a 1513 return ERR_PTR(-ENOENT);
94bdd655 1514again:
d9171b93 1515 dentry = d_alloc_parallel(dir, name, &wq);
94bdd655 1516 if (IS_ERR(dentry))
88d8331a 1517 return dentry;
94bdd655 1518 if (unlikely(!d_in_lookup(dentry))) {
c64cd6e3
AV
1519 int error = d_revalidate(dentry, flags);
1520 if (unlikely(error <= 0)) {
1521 if (!error) {
1522 d_invalidate(dentry);
949a852e 1523 dput(dentry);
c64cd6e3 1524 goto again;
949a852e 1525 }
c64cd6e3
AV
1526 dput(dentry);
1527 dentry = ERR_PTR(error);
949a852e 1528 }
94bdd655
AV
1529 } else {
1530 old = inode->i_op->lookup(inode, dentry, flags);
1531 d_lookup_done(dentry);
1532 if (unlikely(old)) {
1533 dput(dentry);
1534 dentry = old;
949a852e
AV
1535 }
1536 }
e3c13928 1537 return dentry;
1da177e4
LT
1538}
1539
88d8331a
AV
1540static struct dentry *lookup_slow(const struct qstr *name,
1541 struct dentry *dir,
1542 unsigned int flags)
1543{
1544 struct inode *inode = dir->d_inode;
1545 struct dentry *res;
1546 inode_lock_shared(inode);
1547 res = __lookup_slow(name, dir, flags);
1548 inode_unlock_shared(inode);
1549 return res;
1550}
1551
52094c8a
AV
1552static inline int may_lookup(struct nameidata *nd)
1553{
1554 if (nd->flags & LOOKUP_RCU) {
4ad5abb3 1555 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a
AV
1556 if (err != -ECHILD)
1557 return err;
4675ac39 1558 if (unlazy_walk(nd))
52094c8a
AV
1559 return -ECHILD;
1560 }
4ad5abb3 1561 return inode_permission(nd->inode, MAY_EXEC);
52094c8a
AV
1562}
1563
49055906
AV
1564static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1565{
49055906
AV
1566 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
1567 return -ELOOP;
4542576b
AV
1568
1569 if (likely(nd->depth != EMBEDDED_LEVELS))
1570 return 0;
1571 if (likely(nd->stack != nd->internal))
1572 return 0;
60ef60c7 1573 if (likely(nd_alloc_stack(nd)))
49055906 1574 return 0;
60ef60c7
AV
1575
1576 if (nd->flags & LOOKUP_RCU) {
1577 // we need to grab link before we do unlazy. And we can't skip
1578 // unlazy even if we fail to grab the link - cleanup needs it
49055906 1579 bool grabbed_link = legitimize_path(nd, link, seq);
60ef60c7
AV
1580
1581 if (unlazy_walk(nd) != 0 || !grabbed_link)
1582 return -ECHILD;
1583
1584 if (nd_alloc_stack(nd))
1585 return 0;
49055906 1586 }
60ef60c7 1587 return -ENOMEM;
49055906
AV
1588}
1589
b1a81972
AV
1590enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
1591
06708adb 1592static const char *pick_link(struct nameidata *nd, struct path *link,
b1a81972 1593 struct inode *inode, unsigned seq, int flags)
d63ff28f 1594{
1cf2665b 1595 struct saved *last;
ad6cc4c3 1596 const char *res;
49055906 1597 int error = reserve_stack(nd, link, seq);
ad6cc4c3 1598
626de996 1599 if (unlikely(error)) {
49055906 1600 if (!(nd->flags & LOOKUP_RCU))
bc40aee0 1601 path_put(link);
49055906 1602 return ERR_PTR(error);
626de996 1603 }
ab104923 1604 last = nd->stack + nd->depth++;
1cf2665b 1605 last->link = *link;
fceef393 1606 clear_delayed_call(&last->done);
0450b2d1 1607 last->seq = seq;
ad6cc4c3 1608
b1a81972 1609 if (flags & WALK_TRAILING) {
ad6cc4c3
AV
1610 error = may_follow_link(nd, inode);
1611 if (unlikely(error))
1612 return ERR_PTR(error);
1613 }
1614
1615 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS))
1616 return ERR_PTR(-ELOOP);
1617
1618 if (!(nd->flags & LOOKUP_RCU)) {
1619 touch_atime(&last->link);
1620 cond_resched();
1621 } else if (atime_needs_update(&last->link, inode)) {
1622 if (unlikely(unlazy_walk(nd)))
1623 return ERR_PTR(-ECHILD);
1624 touch_atime(&last->link);
1625 }
1626
1627 error = security_inode_follow_link(link->dentry, inode,
1628 nd->flags & LOOKUP_RCU);
1629 if (unlikely(error))
1630 return ERR_PTR(error);
1631
ad6cc4c3
AV
1632 res = READ_ONCE(inode->i_link);
1633 if (!res) {
1634 const char * (*get)(struct dentry *, struct inode *,
1635 struct delayed_call *);
1636 get = inode->i_op->get_link;
1637 if (nd->flags & LOOKUP_RCU) {
1638 res = get(NULL, inode, &last->done);
1639 if (res == ERR_PTR(-ECHILD)) {
1640 if (unlikely(unlazy_walk(nd)))
1641 return ERR_PTR(-ECHILD);
1642 res = get(link->dentry, inode, &last->done);
1643 }
1644 } else {
1645 res = get(link->dentry, inode, &last->done);
1646 }
1647 if (!res)
1648 goto all_done;
1649 if (IS_ERR(res))
1650 return res;
1651 }
1652 if (*res == '/') {
1653 error = nd_jump_root(nd);
1654 if (unlikely(error))
1655 return ERR_PTR(error);
1656 while (unlikely(*++res == '/'))
1657 ;
1658 }
1659 if (*res)
1660 return res;
1661all_done: // pure jump
1662 put_link(nd);
1663 return NULL;
d63ff28f
AV
1664}
1665
3ddcd056
LT
1666/*
1667 * Do we need to follow links? We _really_ want to be able
1668 * to do this check without having to look at inode->i_op,
1669 * so we keep a cache of "no, this doesn't need follow_link"
1670 * for the common case.
1671 */
b0417d2c 1672static const char *step_into(struct nameidata *nd, int flags,
cbae4d12 1673 struct dentry *dentry, struct inode *inode, unsigned seq)
3ddcd056 1674{
cbae4d12
AV
1675 struct path path;
1676 int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1677
1678 if (err < 0)
b0417d2c 1679 return ERR_PTR(err);
cbae4d12 1680 if (likely(!d_is_symlink(path.dentry)) ||
8c4efe22 1681 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
aca2903e 1682 (flags & WALK_NOFOLLOW)) {
8f64fb1c 1683 /* not a symlink or should not follow */
c99687a0
AV
1684 if (!(nd->flags & LOOKUP_RCU)) {
1685 dput(nd->path.dentry);
1686 if (nd->path.mnt != path.mnt)
1687 mntput(nd->path.mnt);
1688 }
1689 nd->path = path;
8f64fb1c
AV
1690 nd->inode = inode;
1691 nd->seq = seq;
b0417d2c 1692 return NULL;
8f64fb1c 1693 }
a7f77542 1694 if (nd->flags & LOOKUP_RCU) {
84f0cd9e 1695 /* make sure that d_is_symlink above matches inode */
cbae4d12 1696 if (read_seqcount_retry(&path.dentry->d_seq, seq))
b0417d2c 1697 return ERR_PTR(-ECHILD);
84f0cd9e
AV
1698 } else {
1699 if (path.mnt == nd->path.mnt)
1700 mntget(path.mnt);
a7f77542 1701 }
b1a81972 1702 return pick_link(nd, &path, inode, seq, flags);
3ddcd056
LT
1703}
1704
c2df1968
AV
1705static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1706 struct inode **inodep,
1707 unsigned *seqp)
957dd41d 1708{
12487f30 1709 struct dentry *parent, *old;
957dd41d 1710
12487f30
AV
1711 if (path_equal(&nd->path, &nd->root))
1712 goto in_root;
1713 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
7ef482fa 1714 struct path path;
efe772d6 1715 unsigned seq;
7ef482fa
AV
1716 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
1717 &nd->root, &path, &seq))
1718 goto in_root;
efe772d6
AV
1719 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1720 return ERR_PTR(-ECHILD);
1721 nd->path = path;
1722 nd->inode = path.dentry->d_inode;
1723 nd->seq = seq;
1724 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1725 return ERR_PTR(-ECHILD);
1726 /* we know that mountpoint was pinned */
957dd41d 1727 }
12487f30
AV
1728 old = nd->path.dentry;
1729 parent = old->d_parent;
1730 *inodep = parent->d_inode;
1731 *seqp = read_seqcount_begin(&parent->d_seq);
1732 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1733 return ERR_PTR(-ECHILD);
1734 if (unlikely(!path_connected(nd->path.mnt, parent)))
1735 return ERR_PTR(-ECHILD);
1736 return parent;
1737in_root:
efe772d6
AV
1738 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1739 return ERR_PTR(-ECHILD);
c2df1968
AV
1740 if (unlikely(nd->flags & LOOKUP_BENEATH))
1741 return ERR_PTR(-ECHILD);
1742 return NULL;
957dd41d
AV
1743}
1744
c2df1968
AV
1745static struct dentry *follow_dotdot(struct nameidata *nd,
1746 struct inode **inodep,
1747 unsigned *seqp)
957dd41d 1748{
12487f30
AV
1749 struct dentry *parent;
1750
1751 if (path_equal(&nd->path, &nd->root))
1752 goto in_root;
1753 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
2aa38470
AV
1754 struct path path;
1755
1756 if (!choose_mountpoint(real_mount(nd->path.mnt),
1757 &nd->root, &path))
1758 goto in_root;
165200d6
AV
1759 path_put(&nd->path);
1760 nd->path = path;
2aa38470 1761 nd->inode = path.dentry->d_inode;
165200d6
AV
1762 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1763 return ERR_PTR(-EXDEV);
957dd41d 1764 }
12487f30
AV
1765 /* rare case of legitimate dget_parent()... */
1766 parent = dget_parent(nd->path.dentry);
1767 if (unlikely(!path_connected(nd->path.mnt, parent))) {
1768 dput(parent);
1769 return ERR_PTR(-ENOENT);
1770 }
1771 *seqp = 0;
1772 *inodep = parent->d_inode;
1773 return parent;
1774
1775in_root:
c2df1968
AV
1776 if (unlikely(nd->flags & LOOKUP_BENEATH))
1777 return ERR_PTR(-EXDEV);
1778 dget(nd->path.dentry);
1779 return NULL;
957dd41d
AV
1780}
1781
7521f22b 1782static const char *handle_dots(struct nameidata *nd, int type)
957dd41d
AV
1783{
1784 if (type == LAST_DOTDOT) {
7521f22b 1785 const char *error = NULL;
c2df1968
AV
1786 struct dentry *parent;
1787 struct inode *inode;
1788 unsigned seq;
957dd41d
AV
1789
1790 if (!nd->root.mnt) {
7521f22b 1791 error = ERR_PTR(set_root(nd));
957dd41d
AV
1792 if (error)
1793 return error;
1794 }
1795 if (nd->flags & LOOKUP_RCU)
c2df1968 1796 parent = follow_dotdot_rcu(nd, &inode, &seq);
957dd41d 1797 else
c2df1968
AV
1798 parent = follow_dotdot(nd, &inode, &seq);
1799 if (IS_ERR(parent))
1800 return ERR_CAST(parent);
1801 if (unlikely(!parent))
1802 error = step_into(nd, WALK_NOFOLLOW,
1803 nd->path.dentry, nd->inode, nd->seq);
1804 else
1805 error = step_into(nd, WALK_NOFOLLOW,
1806 parent, inode, seq);
1807 if (unlikely(error))
957dd41d
AV
1808 return error;
1809
1810 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1811 /*
1812 * If there was a racing rename or mount along our
1813 * path, then we can't be sure that ".." hasn't jumped
1814 * above nd->root (and so userspace should retry or use
1815 * some fallback).
1816 */
1817 smp_rmb();
1818 if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
7521f22b 1819 return ERR_PTR(-EAGAIN);
957dd41d 1820 if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
7521f22b 1821 return ERR_PTR(-EAGAIN);
957dd41d
AV
1822 }
1823 }
7521f22b 1824 return NULL;
957dd41d
AV
1825}
1826
92d27016 1827static const char *walk_component(struct nameidata *nd, int flags)
ce57dfc1 1828{
db3c9ade 1829 struct dentry *dentry;
ce57dfc1 1830 struct inode *inode;
254cf582 1831 unsigned seq;
ce57dfc1
AV
1832 /*
1833 * "." and ".." are special - ".." especially so because it has
1834 * to be able to know about the current root directory and
1835 * parent relationships.
1836 */
4693a547 1837 if (unlikely(nd->last_type != LAST_NORM)) {
1c4ff1a8 1838 if (!(flags & WALK_MORE) && nd->depth)
4693a547 1839 put_link(nd);
7521f22b 1840 return handle_dots(nd, nd->last_type);
4693a547 1841 }
20e34357
AV
1842 dentry = lookup_fast(nd, &inode, &seq);
1843 if (IS_ERR(dentry))
92d27016 1844 return ERR_CAST(dentry);
20e34357 1845 if (unlikely(!dentry)) {
db3c9ade
AV
1846 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1847 if (IS_ERR(dentry))
92d27016 1848 return ERR_CAST(dentry);
ce57dfc1 1849 }
56676ec3
AV
1850 if (!(flags & WALK_MORE) && nd->depth)
1851 put_link(nd);
b0417d2c 1852 return step_into(nd, flags, dentry, inode, seq);
ce57dfc1
AV
1853}
1854
bfcfaa77
LT
1855/*
1856 * We can do the critical dentry name comparison and hashing
1857 * operations one word at a time, but we are limited to:
1858 *
1859 * - Architectures with fast unaligned word accesses. We could
1860 * do a "get_unaligned()" if this helps and is sufficiently
1861 * fast.
1862 *
bfcfaa77
LT
1863 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1864 * do not trap on the (extremely unlikely) case of a page
1865 * crossing operation.
1866 *
1867 * - Furthermore, we need an efficient 64-bit compile for the
1868 * 64-bit case in order to generate the "number of bytes in
1869 * the final mask". Again, that could be replaced with a
1870 * efficient population count instruction or similar.
1871 */
1872#ifdef CONFIG_DCACHE_WORD_ACCESS
1873
f68e556e 1874#include <asm/word-at-a-time.h>
bfcfaa77 1875
468a9428 1876#ifdef HASH_MIX
bfcfaa77 1877
468a9428 1878/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
bfcfaa77 1879
468a9428 1880#elif defined(CONFIG_64BIT)
0fed3ac8 1881/*
2a18da7a
GS
1882 * Register pressure in the mixing function is an issue, particularly
1883 * on 32-bit x86, but almost any function requires one state value and
1884 * one temporary. Instead, use a function designed for two state values
1885 * and no temporaries.
1886 *
1887 * This function cannot create a collision in only two iterations, so
1888 * we have two iterations to achieve avalanche. In those two iterations,
1889 * we have six layers of mixing, which is enough to spread one bit's
1890 * influence out to 2^6 = 64 state bits.
1891 *
1892 * Rotate constants are scored by considering either 64 one-bit input
1893 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1894 * probability of that delta causing a change to each of the 128 output
1895 * bits, using a sample of random initial states.
1896 *
1897 * The Shannon entropy of the computed probabilities is then summed
1898 * to produce a score. Ideally, any input change has a 50% chance of
1899 * toggling any given output bit.
1900 *
1901 * Mixing scores (in bits) for (12,45):
1902 * Input delta: 1-bit 2-bit
1903 * 1 round: 713.3 42542.6
1904 * 2 rounds: 2753.7 140389.8
1905 * 3 rounds: 5954.1 233458.2
1906 * 4 rounds: 7862.6 256672.2
1907 * Perfect: 8192 258048
1908 * (64*128) (64*63/2 * 128)
0fed3ac8 1909 */
2a18da7a
GS
1910#define HASH_MIX(x, y, a) \
1911 ( x ^= (a), \
1912 y ^= x, x = rol64(x,12),\
1913 x += y, y = rol64(y,45),\
1914 y *= 9 )
bfcfaa77 1915
0fed3ac8 1916/*
2a18da7a
GS
1917 * Fold two longs into one 32-bit hash value. This must be fast, but
1918 * latency isn't quite as critical, as there is a fair bit of additional
1919 * work done before the hash value is used.
0fed3ac8 1920 */
2a18da7a 1921static inline unsigned int fold_hash(unsigned long x, unsigned long y)
0fed3ac8 1922{
2a18da7a
GS
1923 y ^= x * GOLDEN_RATIO_64;
1924 y *= GOLDEN_RATIO_64;
1925 return y >> 32;
0fed3ac8
GS
1926}
1927
bfcfaa77
LT
1928#else /* 32-bit case */
1929
2a18da7a
GS
1930/*
1931 * Mixing scores (in bits) for (7,20):
1932 * Input delta: 1-bit 2-bit
1933 * 1 round: 330.3 9201.6
1934 * 2 rounds: 1246.4 25475.4
1935 * 3 rounds: 1907.1 31295.1
1936 * 4 rounds: 2042.3 31718.6
1937 * Perfect: 2048 31744
1938 * (32*64) (32*31/2 * 64)
1939 */
1940#define HASH_MIX(x, y, a) \
1941 ( x ^= (a), \
1942 y ^= x, x = rol32(x, 7),\
1943 x += y, y = rol32(y,20),\
1944 y *= 9 )
bfcfaa77 1945
2a18da7a 1946static inline unsigned int fold_hash(unsigned long x, unsigned long y)
0fed3ac8 1947{
2a18da7a
GS
1948 /* Use arch-optimized multiply if one exists */
1949 return __hash_32(y ^ __hash_32(x));
0fed3ac8
GS
1950}
1951
bfcfaa77
LT
1952#endif
1953
2a18da7a
GS
1954/*
1955 * Return the hash of a string of known length. This is carfully
1956 * designed to match hash_name(), which is the more critical function.
1957 * In particular, we must end by hashing a final word containing 0..7
1958 * payload bytes, to match the way that hash_name() iterates until it
1959 * finds the delimiter after the name.
1960 */
8387ff25 1961unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
bfcfaa77 1962{
8387ff25 1963 unsigned long a, x = 0, y = (unsigned long)salt;
bfcfaa77
LT
1964
1965 for (;;) {
fcfd2fbf
GS
1966 if (!len)
1967 goto done;
e419b4cc 1968 a = load_unaligned_zeropad(name);
bfcfaa77
LT
1969 if (len < sizeof(unsigned long))
1970 break;
2a18da7a 1971 HASH_MIX(x, y, a);
bfcfaa77
LT
1972 name += sizeof(unsigned long);
1973 len -= sizeof(unsigned long);
bfcfaa77 1974 }
2a18da7a 1975 x ^= a & bytemask_from_count(len);
bfcfaa77 1976done:
2a18da7a 1977 return fold_hash(x, y);
bfcfaa77
LT
1978}
1979EXPORT_SYMBOL(full_name_hash);
1980
fcfd2fbf 1981/* Return the "hash_len" (hash and length) of a null-terminated string */
8387ff25 1982u64 hashlen_string(const void *salt, const char *name)
fcfd2fbf 1983{
8387ff25
LT
1984 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1985 unsigned long adata, mask, len;
fcfd2fbf
GS
1986 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1987
8387ff25
LT
1988 len = 0;
1989 goto inside;
1990
fcfd2fbf 1991 do {
2a18da7a 1992 HASH_MIX(x, y, a);
fcfd2fbf 1993 len += sizeof(unsigned long);
8387ff25 1994inside:
fcfd2fbf
GS
1995 a = load_unaligned_zeropad(name+len);
1996 } while (!has_zero(a, &adata, &constants));
1997
1998 adata = prep_zero_mask(a, adata, &constants);
1999 mask = create_zero_mask(adata);
2a18da7a 2000 x ^= a & zero_bytemask(mask);
fcfd2fbf 2001
2a18da7a 2002 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
fcfd2fbf
GS
2003}
2004EXPORT_SYMBOL(hashlen_string);
2005
bfcfaa77
LT
2006/*
2007 * Calculate the length and hash of the path component, and
d6bb3e90 2008 * return the "hash_len" as the result.
bfcfaa77 2009 */
8387ff25 2010static inline u64 hash_name(const void *salt, const char *name)
bfcfaa77 2011{
8387ff25
LT
2012 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
2013 unsigned long adata, bdata, mask, len;
36126f8f 2014 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
bfcfaa77 2015
8387ff25
LT
2016 len = 0;
2017 goto inside;
2018
bfcfaa77 2019 do {
2a18da7a 2020 HASH_MIX(x, y, a);
bfcfaa77 2021 len += sizeof(unsigned long);
8387ff25 2022inside:
e419b4cc 2023 a = load_unaligned_zeropad(name+len);
36126f8f
LT
2024 b = a ^ REPEAT_BYTE('/');
2025 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
2026
2027 adata = prep_zero_mask(a, adata, &constants);
2028 bdata = prep_zero_mask(b, bdata, &constants);
36126f8f 2029 mask = create_zero_mask(adata | bdata);
2a18da7a 2030 x ^= a & zero_bytemask(mask);
36126f8f 2031
2a18da7a 2032 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
bfcfaa77
LT
2033}
2034
2a18da7a 2035#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
bfcfaa77 2036
fcfd2fbf 2037/* Return the hash of a string of known length */
8387ff25 2038unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
0145acc2 2039{
8387ff25 2040 unsigned long hash = init_name_hash(salt);
0145acc2 2041 while (len--)
fcfd2fbf 2042 hash = partial_name_hash((unsigned char)*name++, hash);
0145acc2
LT
2043 return end_name_hash(hash);
2044}
ae942ae7 2045EXPORT_SYMBOL(full_name_hash);
0145acc2 2046
fcfd2fbf 2047/* Return the "hash_len" (hash and length) of a null-terminated string */
8387ff25 2048u64 hashlen_string(const void *salt, const char *name)
fcfd2fbf 2049{
8387ff25 2050 unsigned long hash = init_name_hash(salt);
fcfd2fbf
GS
2051 unsigned long len = 0, c;
2052
2053 c = (unsigned char)*name;
e0ab7af9 2054 while (c) {
fcfd2fbf
GS
2055 len++;
2056 hash = partial_name_hash(c, hash);
2057 c = (unsigned char)name[len];
e0ab7af9 2058 }
fcfd2fbf
GS
2059 return hashlen_create(end_name_hash(hash), len);
2060}
f2a031b6 2061EXPORT_SYMBOL(hashlen_string);
fcfd2fbf 2062
200e9ef7
LT
2063/*
2064 * We know there's a real path component here of at least
2065 * one character.
2066 */
8387ff25 2067static inline u64 hash_name(const void *salt, const char *name)
200e9ef7 2068{
8387ff25 2069 unsigned long hash = init_name_hash(salt);
200e9ef7
LT
2070 unsigned long len = 0, c;
2071
2072 c = (unsigned char)*name;
2073 do {
2074 len++;
2075 hash = partial_name_hash(c, hash);
2076 c = (unsigned char)name[len];
2077 } while (c && c != '/');
d6bb3e90 2078 return hashlen_create(end_name_hash(hash), len);
200e9ef7
LT
2079}
2080
bfcfaa77
LT
2081#endif
2082
1da177e4
LT
2083/*
2084 * Name resolution.
ea3834d9
PM
2085 * This is the basic name resolution function, turning a pathname into
2086 * the final dentry. We expect 'base' to be positive and a directory.
1da177e4 2087 *
ea3834d9
PM
2088 * Returns 0 and nd will have valid dentry and mnt on success.
2089 * Returns error and drops reference to input namei data on failure.
1da177e4 2090 */
6de88d72 2091static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4 2092{
d8d4611a 2093 int depth = 0; // depth <= nd->depth
1da177e4 2094 int err;
32cd7468 2095
b4c03536 2096 nd->last_type = LAST_ROOT;
c108837e 2097 nd->flags |= LOOKUP_PARENT;
9b5858e9
AV
2098 if (IS_ERR(name))
2099 return PTR_ERR(name);
1da177e4
LT
2100 while (*name=='/')
2101 name++;
2102 if (!*name)
9e18f10a 2103 return 0;
1da177e4 2104
1da177e4
LT
2105 /* At this point we know we have a real path component. */
2106 for(;;) {
92d27016 2107 const char *link;
d6bb3e90 2108 u64 hash_len;
fe479a58 2109 int type;
1da177e4 2110
52094c8a 2111 err = may_lookup(nd);
2a18da7a 2112 if (err)
3595e234 2113 return err;
1da177e4 2114
8387ff25 2115 hash_len = hash_name(nd->path.dentry, name);
1da177e4 2116
fe479a58 2117 type = LAST_NORM;
d6bb3e90 2118 if (name[0] == '.') switch (hashlen_len(hash_len)) {
fe479a58 2119 case 2:
200e9ef7 2120 if (name[1] == '.') {
fe479a58 2121 type = LAST_DOTDOT;
16c2cd71
AV
2122 nd->flags |= LOOKUP_JUMPED;
2123 }
fe479a58
AV
2124 break;
2125 case 1:
2126 type = LAST_DOT;
2127 }
5a202bcd
AV
2128 if (likely(type == LAST_NORM)) {
2129 struct dentry *parent = nd->path.dentry;
16c2cd71 2130 nd->flags &= ~LOOKUP_JUMPED;
5a202bcd 2131 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
a060dc50 2132 struct qstr this = { { .hash_len = hash_len }, .name = name };
da53be12 2133 err = parent->d_op->d_hash(parent, &this);
5a202bcd 2134 if (err < 0)
3595e234 2135 return err;
d6bb3e90
LT
2136 hash_len = this.hash_len;
2137 name = this.name;
5a202bcd
AV
2138 }
2139 }
fe479a58 2140
d6bb3e90
LT
2141 nd->last.hash_len = hash_len;
2142 nd->last.name = name;
5f4a6a69
AV
2143 nd->last_type = type;
2144
d6bb3e90
LT
2145 name += hashlen_len(hash_len);
2146 if (!*name)
bdf6cbf1 2147 goto OK;
200e9ef7
LT
2148 /*
2149 * If it wasn't NUL, we know it was '/'. Skip that
2150 * slash, and continue until no more slashes.
2151 */
2152 do {
d6bb3e90
LT
2153 name++;
2154 } while (unlikely(*name == '/'));
8620c238
AV
2155 if (unlikely(!*name)) {
2156OK:
d8d4611a 2157 /* pathname or trailing symlink, done */
c108837e 2158 if (!depth) {
0f705953
AV
2159 nd->dir_uid = nd->inode->i_uid;
2160 nd->dir_mode = nd->inode->i_mode;
c108837e 2161 nd->flags &= ~LOOKUP_PARENT;
8620c238 2162 return 0;
c108837e 2163 }
8620c238 2164 /* last component of nested symlink */
d8d4611a 2165 name = nd->stack[--depth].name;
8c4efe22 2166 link = walk_component(nd, 0);
1c4ff1a8
AV
2167 } else {
2168 /* not the last component */
8c4efe22 2169 link = walk_component(nd, WALK_MORE);
8620c238 2170 }
92d27016
AV
2171 if (unlikely(link)) {
2172 if (IS_ERR(link))
2173 return PTR_ERR(link);
2174 /* a symlink to follow */
d8d4611a 2175 nd->stack[depth++].name = name;
92d27016
AV
2176 name = link;
2177 continue;
31e6b01f 2178 }
97242f99
AV
2179 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2180 if (nd->flags & LOOKUP_RCU) {
4675ac39 2181 if (unlazy_walk(nd))
97242f99
AV
2182 return -ECHILD;
2183 }
3595e234 2184 return -ENOTDIR;
97242f99 2185 }
1da177e4 2186 }
1da177e4
LT
2187}
2188
edc2b1da 2189/* must be paired with terminate_walk() */
c8a53ee5 2190static const char *path_init(struct nameidata *nd, unsigned flags)
31e6b01f 2191{
740a1678 2192 int error;
c8a53ee5 2193 const char *s = nd->name->name;
31e6b01f 2194
c0eb027e
LT
2195 if (!*s)
2196 flags &= ~LOOKUP_RCU;
edc2b1da
AV
2197 if (flags & LOOKUP_RCU)
2198 rcu_read_lock();
c0eb027e 2199
c108837e 2200 nd->flags = flags | LOOKUP_JUMPED;
31e6b01f 2201 nd->depth = 0;
ab87f9a5
AS
2202
2203 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2204 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2205 smp_rmb();
2206
5b6ca027 2207 if (flags & LOOKUP_ROOT) {
b18825a7
DH
2208 struct dentry *root = nd->root.dentry;
2209 struct inode *inode = root->d_inode;
93893862
AV
2210 if (*s && unlikely(!d_can_lookup(root)))
2211 return ERR_PTR(-ENOTDIR);
5b6ca027
AV
2212 nd->path = nd->root;
2213 nd->inode = inode;
2214 if (flags & LOOKUP_RCU) {
ab87f9a5 2215 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
8f47a016 2216 nd->root_seq = nd->seq;
5b6ca027
AV
2217 } else {
2218 path_get(&nd->path);
2219 }
368ee9ba 2220 return s;
5b6ca027
AV
2221 }
2222
31e6b01f 2223 nd->root.mnt = NULL;
248fb5b9
AV
2224 nd->path.mnt = NULL;
2225 nd->path.dentry = NULL;
31e6b01f 2226
8db52c7e
AS
2227 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2228 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
740a1678
AS
2229 error = nd_jump_root(nd);
2230 if (unlikely(error))
2231 return ERR_PTR(error);
2232 return s;
8db52c7e
AS
2233 }
2234
2235 /* Relative pathname -- get the starting-point it is relative to. */
2236 if (nd->dfd == AT_FDCWD) {
e41f7d4e
AV
2237 if (flags & LOOKUP_RCU) {
2238 struct fs_struct *fs = current->fs;
2239 unsigned seq;
31e6b01f 2240
e41f7d4e
AV
2241 do {
2242 seq = read_seqcount_begin(&fs->seq);
2243 nd->path = fs->pwd;
ef55d917 2244 nd->inode = nd->path.dentry->d_inode;
e41f7d4e
AV
2245 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2246 } while (read_seqcount_retry(&fs->seq, seq));
2247 } else {
2248 get_fs_pwd(current->fs, &nd->path);
ef55d917 2249 nd->inode = nd->path.dentry->d_inode;
e41f7d4e 2250 }
31e6b01f 2251 } else {
582aa64a 2252 /* Caller must check execute permissions on the starting path component */
c8a53ee5 2253 struct fd f = fdget_raw(nd->dfd);
31e6b01f
NP
2254 struct dentry *dentry;
2255
2903ff01 2256 if (!f.file)
368ee9ba 2257 return ERR_PTR(-EBADF);
31e6b01f 2258
2903ff01 2259 dentry = f.file->f_path.dentry;
31e6b01f 2260
edc2b1da
AV
2261 if (*s && unlikely(!d_can_lookup(dentry))) {
2262 fdput(f);
2263 return ERR_PTR(-ENOTDIR);
f52e0c11 2264 }
31e6b01f 2265
2903ff01 2266 nd->path = f.file->f_path;
e41f7d4e 2267 if (flags & LOOKUP_RCU) {
34a26b99
AV
2268 nd->inode = nd->path.dentry->d_inode;
2269 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
e41f7d4e 2270 } else {
2903ff01 2271 path_get(&nd->path);
34a26b99 2272 nd->inode = nd->path.dentry->d_inode;
e41f7d4e 2273 }
34a26b99 2274 fdput(f);
31e6b01f 2275 }
8db52c7e 2276
adb21d2b
AS
2277 /* For scoped-lookups we need to set the root to the dirfd as well. */
2278 if (flags & LOOKUP_IS_SCOPED) {
2279 nd->root = nd->path;
2280 if (flags & LOOKUP_RCU) {
2281 nd->root_seq = nd->seq;
2282 } else {
2283 path_get(&nd->root);
2284 nd->flags |= LOOKUP_ROOT_GRABBED;
2285 }
2286 }
2287 return s;
9b4a9b14
AV
2288}
2289
1ccac622 2290static inline const char *lookup_last(struct nameidata *nd)
bd92d7fe
AV
2291{
2292 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2293 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2294
c108837e 2295 return walk_component(nd, WALK_TRAILING);
bd92d7fe
AV
2296}
2297
4f757f3c
AV
2298static int handle_lookup_down(struct nameidata *nd)
2299{
c153007b 2300 if (!(nd->flags & LOOKUP_RCU))
db3c9ade 2301 dget(nd->path.dentry);
b0417d2c
AV
2302 return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2303 nd->path.dentry, nd->inode, nd->seq));
4f757f3c
AV
2304}
2305
9b4a9b14 2306/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5 2307static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
9b4a9b14 2308{
c8a53ee5 2309 const char *s = path_init(nd, flags);
bd92d7fe 2310 int err;
31e6b01f 2311
9b5858e9 2312 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
4f757f3c 2313 err = handle_lookup_down(nd);
5f336e72
AV
2314 if (unlikely(err < 0))
2315 s = ERR_PTR(err);
4f757f3c
AV
2316 }
2317
1ccac622
AV
2318 while (!(err = link_path_walk(s, nd)) &&
2319 (s = lookup_last(nd)) != NULL)
2320 ;
9f1fafee
AV
2321 if (!err)
2322 err = complete_walk(nd);
bd92d7fe 2323
deb106c6
AV
2324 if (!err && nd->flags & LOOKUP_DIRECTORY)
2325 if (!d_can_lookup(nd->path.dentry))
bd23a539 2326 err = -ENOTDIR;
161aff1d
AV
2327 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2328 err = handle_lookup_down(nd);
2329 nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please...
2330 }
625b6d10
AV
2331 if (!err) {
2332 *path = nd->path;
2333 nd->path.mnt = NULL;
2334 nd->path.dentry = NULL;
2335 }
2336 terminate_walk(nd);
bd92d7fe 2337 return err;
ee0827cd 2338}
31e6b01f 2339
31d921c7
DH
2340int filename_lookup(int dfd, struct filename *name, unsigned flags,
2341 struct path *path, struct path *root)
ee0827cd 2342{
894bc8c4 2343 int retval;
9883d185 2344 struct nameidata nd;
abc9f5be
AV
2345 if (IS_ERR(name))
2346 return PTR_ERR(name);
9ad1aaa6
AV
2347 if (unlikely(root)) {
2348 nd.root = *root;
2349 flags |= LOOKUP_ROOT;
2350 }
9883d185 2351 set_nameidata(&nd, dfd, name);
c8a53ee5 2352 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
ee0827cd 2353 if (unlikely(retval == -ECHILD))
c8a53ee5 2354 retval = path_lookupat(&nd, flags, path);
ee0827cd 2355 if (unlikely(retval == -ESTALE))
c8a53ee5 2356 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
31e6b01f 2357
f78570dd 2358 if (likely(!retval))
161aff1d
AV
2359 audit_inode(name, path->dentry,
2360 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
9883d185 2361 restore_nameidata();
e4bd1c1a 2362 putname(name);
170aa3d0 2363 return retval;
1da177e4
LT
2364}
2365
8bcb77fa 2366/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5 2367static int path_parentat(struct nameidata *nd, unsigned flags,
391172c4 2368 struct path *parent)
8bcb77fa 2369{
c8a53ee5 2370 const char *s = path_init(nd, flags);
9b5858e9 2371 int err = link_path_walk(s, nd);
8bcb77fa
AV
2372 if (!err)
2373 err = complete_walk(nd);
391172c4
AV
2374 if (!err) {
2375 *parent = nd->path;
2376 nd->path.mnt = NULL;
2377 nd->path.dentry = NULL;
2378 }
2379 terminate_walk(nd);
8bcb77fa
AV
2380 return err;
2381}
2382
5c31b6ce 2383static struct filename *filename_parentat(int dfd, struct filename *name,
391172c4
AV
2384 unsigned int flags, struct path *parent,
2385 struct qstr *last, int *type)
8bcb77fa
AV
2386{
2387 int retval;
9883d185 2388 struct nameidata nd;
8bcb77fa 2389
5c31b6ce
AV
2390 if (IS_ERR(name))
2391 return name;
9883d185 2392 set_nameidata(&nd, dfd, name);
c8a53ee5 2393 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
8bcb77fa 2394 if (unlikely(retval == -ECHILD))
c8a53ee5 2395 retval = path_parentat(&nd, flags, parent);
8bcb77fa 2396 if (unlikely(retval == -ESTALE))
c8a53ee5 2397 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
391172c4
AV
2398 if (likely(!retval)) {
2399 *last = nd.last;
2400 *type = nd.last_type;
c9b07eab 2401 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
5c31b6ce
AV
2402 } else {
2403 putname(name);
2404 name = ERR_PTR(retval);
391172c4 2405 }
9883d185 2406 restore_nameidata();
5c31b6ce 2407 return name;
8bcb77fa
AV
2408}
2409
79714f72
AV
2410/* does lookup, returns the object with parent locked */
2411struct dentry *kern_path_locked(const char *name, struct path *path)
5590ff0d 2412{
5c31b6ce
AV
2413 struct filename *filename;
2414 struct dentry *d;
391172c4
AV
2415 struct qstr last;
2416 int type;
51689104 2417
5c31b6ce
AV
2418 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2419 &last, &type);
51689104
PM
2420 if (IS_ERR(filename))
2421 return ERR_CAST(filename);
5c31b6ce 2422 if (unlikely(type != LAST_NORM)) {
391172c4 2423 path_put(path);
5c31b6ce
AV
2424 putname(filename);
2425 return ERR_PTR(-EINVAL);
79714f72 2426 }
5955102c 2427 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
391172c4 2428 d = __lookup_hash(&last, path->dentry, 0);
79714f72 2429 if (IS_ERR(d)) {
5955102c 2430 inode_unlock(path->dentry->d_inode);
391172c4 2431 path_put(path);
79714f72 2432 }
51689104 2433 putname(filename);
79714f72 2434 return d;
5590ff0d
UD
2435}
2436
d1811465
AV
2437int kern_path(const char *name, unsigned int flags, struct path *path)
2438{
abc9f5be
AV
2439 return filename_lookup(AT_FDCWD, getname_kernel(name),
2440 flags, path, NULL);
d1811465 2441}
4d359507 2442EXPORT_SYMBOL(kern_path);
d1811465 2443
16f18200
JJS
2444/**
2445 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2446 * @dentry: pointer to dentry of the base directory
2447 * @mnt: pointer to vfs mount of the base directory
2448 * @name: pointer to file name
2449 * @flags: lookup flags
e0a01249 2450 * @path: pointer to struct path to fill
16f18200
JJS
2451 */
2452int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2453 const char *name, unsigned int flags,
e0a01249 2454 struct path *path)
16f18200 2455{
9ad1aaa6 2456 struct path root = {.mnt = mnt, .dentry = dentry};
9ad1aaa6 2457 /* the first argument of filename_lookup() is ignored with root */
abc9f5be
AV
2458 return filename_lookup(AT_FDCWD, getname_kernel(name),
2459 flags , path, &root);
16f18200 2460}
4d359507 2461EXPORT_SYMBOL(vfs_path_lookup);
16f18200 2462
3c95f0dc
AV
2463static int lookup_one_len_common(const char *name, struct dentry *base,
2464 int len, struct qstr *this)
057f6c01 2465{
3c95f0dc
AV
2466 this->name = name;
2467 this->len = len;
2468 this->hash = full_name_hash(base, name, len);
6a96ba54 2469 if (!len)
3c95f0dc 2470 return -EACCES;
6a96ba54 2471
21d8a15a
AV
2472 if (unlikely(name[0] == '.')) {
2473 if (len < 2 || (len == 2 && name[1] == '.'))
3c95f0dc 2474 return -EACCES;
21d8a15a
AV
2475 }
2476
6a96ba54 2477 while (len--) {
3c95f0dc 2478 unsigned int c = *(const unsigned char *)name++;
6a96ba54 2479 if (c == '/' || c == '\0')
3c95f0dc 2480 return -EACCES;
6a96ba54 2481 }
5a202bcd
AV
2482 /*
2483 * See if the low-level filesystem might want
2484 * to use its own hash..
2485 */
2486 if (base->d_flags & DCACHE_OP_HASH) {
3c95f0dc 2487 int err = base->d_op->d_hash(base, this);
5a202bcd 2488 if (err < 0)
3c95f0dc 2489 return err;
5a202bcd 2490 }
eead1911 2491
3c95f0dc
AV
2492 return inode_permission(base->d_inode, MAY_EXEC);
2493}
2494
0da0b7fd
DH
2495/**
2496 * try_lookup_one_len - filesystem helper to lookup single pathname component
2497 * @name: pathname component to lookup
2498 * @base: base directory to lookup from
2499 * @len: maximum length @len should be interpreted to
2500 *
2501 * Look up a dentry by name in the dcache, returning NULL if it does not
2502 * currently exist. The function does not try to create a dentry.
2503 *
2504 * Note that this routine is purely a helper for filesystem usage and should
2505 * not be called by generic code.
2506 *
2507 * The caller must hold base->i_mutex.
2508 */
2509struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2510{
2511 struct qstr this;
2512 int err;
2513
2514 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2515
2516 err = lookup_one_len_common(name, base, len, &this);
2517 if (err)
2518 return ERR_PTR(err);
2519
2520 return lookup_dcache(&this, base, 0);
2521}
2522EXPORT_SYMBOL(try_lookup_one_len);
2523
3c95f0dc
AV
2524/**
2525 * lookup_one_len - filesystem helper to lookup single pathname component
2526 * @name: pathname component to lookup
2527 * @base: base directory to lookup from
2528 * @len: maximum length @len should be interpreted to
2529 *
2530 * Note that this routine is purely a helper for filesystem usage and should
2531 * not be called by generic code.
2532 *
2533 * The caller must hold base->i_mutex.
2534 */
2535struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2536{
8613a209 2537 struct dentry *dentry;
3c95f0dc
AV
2538 struct qstr this;
2539 int err;
2540
2541 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2542
2543 err = lookup_one_len_common(name, base, len, &this);
cda309de
MS
2544 if (err)
2545 return ERR_PTR(err);
2546
8613a209
AV
2547 dentry = lookup_dcache(&this, base, 0);
2548 return dentry ? dentry : __lookup_slow(&this, base, 0);
057f6c01 2549}
4d359507 2550EXPORT_SYMBOL(lookup_one_len);
057f6c01 2551
bbddca8e
N
2552/**
2553 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2554 * @name: pathname component to lookup
2555 * @base: base directory to lookup from
2556 * @len: maximum length @len should be interpreted to
2557 *
2558 * Note that this routine is purely a helper for filesystem usage and should
2559 * not be called by generic code.
2560 *
2561 * Unlike lookup_one_len, it should be called without the parent
2562 * i_mutex held, and will take the i_mutex itself if necessary.
2563 */
2564struct dentry *lookup_one_len_unlocked(const char *name,
2565 struct dentry *base, int len)
2566{
2567 struct qstr this;
bbddca8e 2568 int err;
20d00ee8 2569 struct dentry *ret;
bbddca8e 2570
3c95f0dc 2571 err = lookup_one_len_common(name, base, len, &this);
bbddca8e
N
2572 if (err)
2573 return ERR_PTR(err);
2574
20d00ee8
LT
2575 ret = lookup_dcache(&this, base, 0);
2576 if (!ret)
2577 ret = lookup_slow(&this, base, 0);
2578 return ret;
bbddca8e
N
2579}
2580EXPORT_SYMBOL(lookup_one_len_unlocked);
2581
6c2d4798
AV
2582/*
2583 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2584 * on negatives. Returns known positive or ERR_PTR(); that's what
2585 * most of the users want. Note that pinned negative with unlocked parent
2586 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2587 * need to be very careful; pinned positives have ->d_inode stable, so
2588 * this one avoids such problems.
2589 */
2590struct dentry *lookup_positive_unlocked(const char *name,
2591 struct dentry *base, int len)
2592{
2593 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
2fa6b1e0 2594 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
6c2d4798
AV
2595 dput(ret);
2596 ret = ERR_PTR(-ENOENT);
2597 }
2598 return ret;
2599}
2600EXPORT_SYMBOL(lookup_positive_unlocked);
2601
eedf265a
EB
2602#ifdef CONFIG_UNIX98_PTYS
2603int path_pts(struct path *path)
2604{
2605 /* Find something mounted on "pts" in the same directory as
2606 * the input path.
2607 */
a6a7eb76
AV
2608 struct dentry *parent = dget_parent(path->dentry);
2609 struct dentry *child;
19f6028a 2610 struct qstr this = QSTR_INIT("pts", 3);
eedf265a 2611
a6a7eb76
AV
2612 if (unlikely(!path_connected(path->mnt, parent))) {
2613 dput(parent);
63b27720 2614 return -ENOENT;
a6a7eb76 2615 }
63b27720
AV
2616 dput(path->dentry);
2617 path->dentry = parent;
eedf265a
EB
2618 child = d_hash_and_lookup(parent, &this);
2619 if (!child)
2620 return -ENOENT;
2621
2622 path->dentry = child;
2623 dput(parent);
19f6028a 2624 follow_down(path);
eedf265a
EB
2625 return 0;
2626}
2627#endif
2628
1fa1e7f6
AW
2629int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2630 struct path *path, int *empty)
1da177e4 2631{
abc9f5be
AV
2632 return filename_lookup(dfd, getname_flags(name, flags, empty),
2633 flags, path, NULL);
1da177e4 2634}
b853a161 2635EXPORT_SYMBOL(user_path_at_empty);
1fa1e7f6 2636
cbdf35bc 2637int __check_sticky(struct inode *dir, struct inode *inode)
1da177e4 2638{
8e96e3b7 2639 kuid_t fsuid = current_fsuid();
da9592ed 2640
8e96e3b7 2641 if (uid_eq(inode->i_uid, fsuid))
1da177e4 2642 return 0;
8e96e3b7 2643 if (uid_eq(dir->i_uid, fsuid))
1da177e4 2644 return 0;
23adbe12 2645 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
1da177e4 2646}
cbdf35bc 2647EXPORT_SYMBOL(__check_sticky);
1da177e4
LT
2648
2649/*
2650 * Check whether we can remove a link victim from directory dir, check
2651 * whether the type of victim is right.
2652 * 1. We can't do it if dir is read-only (done in permission())
2653 * 2. We should have write and exec permissions on dir
2654 * 3. We can't remove anything from append-only dir
2655 * 4. We can't do anything with immutable dir (done in permission())
2656 * 5. If the sticky bit on dir is set we should either
2657 * a. be owner of dir, or
2658 * b. be owner of victim, or
2659 * c. have CAP_FOWNER capability
2660 * 6. If the victim is append-only or immutable we can't do antyhing with
2661 * links pointing to it.
0bd23d09
EB
2662 * 7. If the victim has an unknown uid or gid we can't change the inode.
2663 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2664 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2665 * 10. We can't remove a root or mountpoint.
2666 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
1da177e4
LT
2667 * nfs_async_unlink().
2668 */
b18825a7 2669static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
1da177e4 2670{
63afdfc7 2671 struct inode *inode = d_backing_inode(victim);
1da177e4
LT
2672 int error;
2673
b18825a7 2674 if (d_is_negative(victim))
1da177e4 2675 return -ENOENT;
b18825a7 2676 BUG_ON(!inode);
1da177e4
LT
2677
2678 BUG_ON(victim->d_parent->d_inode != dir);
593d1ce8
EB
2679
2680 /* Inode writeback is not safe when the uid or gid are invalid. */
2681 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2682 return -EOVERFLOW;
2683
4fa6b5ec 2684 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4 2685
f419a2e3 2686 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2687 if (error)
2688 return error;
2689 if (IS_APPEND(dir))
2690 return -EPERM;
b18825a7
DH
2691
2692 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
0bd23d09 2693 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
1da177e4
LT
2694 return -EPERM;
2695 if (isdir) {
44b1d530 2696 if (!d_is_dir(victim))
1da177e4
LT
2697 return -ENOTDIR;
2698 if (IS_ROOT(victim))
2699 return -EBUSY;
44b1d530 2700 } else if (d_is_dir(victim))
1da177e4
LT
2701 return -EISDIR;
2702 if (IS_DEADDIR(dir))
2703 return -ENOENT;
2704 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2705 return -EBUSY;
2706 return 0;
2707}
2708
2709/* Check whether we can create an object with dentry child in directory
2710 * dir.
2711 * 1. We can't do it if child already exists (open has special treatment for
2712 * this case, but since we are inlined it's OK)
2713 * 2. We can't do it if dir is read-only (done in permission())
036d5236
EB
2714 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2715 * 4. We should have write and exec permissions on dir
2716 * 5. We can't do it if dir is immutable (done in permission())
1da177e4 2717 */
a95164d9 2718static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4 2719{
036d5236 2720 struct user_namespace *s_user_ns;
14e972b4 2721 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
1da177e4
LT
2722 if (child->d_inode)
2723 return -EEXIST;
2724 if (IS_DEADDIR(dir))
2725 return -ENOENT;
036d5236
EB
2726 s_user_ns = dir->i_sb->s_user_ns;
2727 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2728 !kgid_has_mapping(s_user_ns, current_fsgid()))
2729 return -EOVERFLOW;
f419a2e3 2730 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2731}
2732
1da177e4
LT
2733/*
2734 * p1 and p2 should be directories on the same fs.
2735 */
2736struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2737{
2738 struct dentry *p;
2739
2740 if (p1 == p2) {
5955102c 2741 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
1da177e4
LT
2742 return NULL;
2743 }
2744
fc64005c 2745 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
1da177e4 2746
e2761a11
OH
2747 p = d_ancestor(p2, p1);
2748 if (p) {
5955102c
AV
2749 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2750 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
e2761a11 2751 return p;
1da177e4
LT
2752 }
2753
e2761a11
OH
2754 p = d_ancestor(p1, p2);
2755 if (p) {
5955102c
AV
2756 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2757 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
e2761a11 2758 return p;
1da177e4
LT
2759 }
2760
5955102c
AV
2761 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2762 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
1da177e4
LT
2763 return NULL;
2764}
4d359507 2765EXPORT_SYMBOL(lock_rename);
1da177e4
LT
2766
2767void unlock_rename(struct dentry *p1, struct dentry *p2)
2768{
5955102c 2769 inode_unlock(p1->d_inode);
1da177e4 2770 if (p1 != p2) {
5955102c 2771 inode_unlock(p2->d_inode);
fc64005c 2772 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
1da177e4
LT
2773 }
2774}
4d359507 2775EXPORT_SYMBOL(unlock_rename);
1da177e4 2776
4acdaf27 2777int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
312b63fb 2778 bool want_excl)
1da177e4 2779{
a95164d9 2780 int error = may_create(dir, dentry);
1da177e4
LT
2781 if (error)
2782 return error;
2783
acfa4380 2784 if (!dir->i_op->create)
1da177e4
LT
2785 return -EACCES; /* shouldn't it be ENOSYS? */
2786 mode &= S_IALLUGO;
2787 mode |= S_IFREG;
2788 error = security_inode_create(dir, dentry, mode);
2789 if (error)
2790 return error;
312b63fb 2791 error = dir->i_op->create(dir, dentry, mode, want_excl);
a74574aa 2792 if (!error)
f38aa942 2793 fsnotify_create(dir, dentry);
1da177e4
LT
2794 return error;
2795}
4d359507 2796EXPORT_SYMBOL(vfs_create);
1da177e4 2797
8e6c848e
AV
2798int vfs_mkobj(struct dentry *dentry, umode_t mode,
2799 int (*f)(struct dentry *, umode_t, void *),
2800 void *arg)
2801{
2802 struct inode *dir = dentry->d_parent->d_inode;
2803 int error = may_create(dir, dentry);
2804 if (error)
2805 return error;
2806
2807 mode &= S_IALLUGO;
2808 mode |= S_IFREG;
2809 error = security_inode_create(dir, dentry, mode);
2810 if (error)
2811 return error;
2812 error = f(dentry, mode, arg);
2813 if (!error)
2814 fsnotify_create(dir, dentry);
2815 return error;
2816}
2817EXPORT_SYMBOL(vfs_mkobj);
2818
a2982cc9
EB
2819bool may_open_dev(const struct path *path)
2820{
2821 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2822 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2823}
2824
f0bb5aaf 2825static int may_open(const struct path *path, int acc_mode, int flag)
1da177e4 2826{
3fb64190 2827 struct dentry *dentry = path->dentry;
1da177e4
LT
2828 struct inode *inode = dentry->d_inode;
2829 int error;
2830
2831 if (!inode)
2832 return -ENOENT;
2833
c8fe8f30
CH
2834 switch (inode->i_mode & S_IFMT) {
2835 case S_IFLNK:
1da177e4 2836 return -ELOOP;
c8fe8f30
CH
2837 case S_IFDIR:
2838 if (acc_mode & MAY_WRITE)
2839 return -EISDIR;
2840 break;
2841 case S_IFBLK:
2842 case S_IFCHR:
a2982cc9 2843 if (!may_open_dev(path))
1da177e4 2844 return -EACCES;
c8fe8f30
CH
2845 /*FALLTHRU*/
2846 case S_IFIFO:
2847 case S_IFSOCK:
1da177e4 2848 flag &= ~O_TRUNC;
c8fe8f30 2849 break;
4a3fd211 2850 }
b41572e9 2851
62fb4a15 2852 error = inode_permission(inode, MAY_OPEN | acc_mode);
b41572e9
DH
2853 if (error)
2854 return error;
6146f0d5 2855
1da177e4
LT
2856 /*
2857 * An append-only file must be opened in append mode for writing.
2858 */
2859 if (IS_APPEND(inode)) {
8737c930 2860 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521 2861 return -EPERM;
1da177e4 2862 if (flag & O_TRUNC)
7715b521 2863 return -EPERM;
1da177e4
LT
2864 }
2865
2866 /* O_NOATIME can only be set by the owner or superuser */
2e149670 2867 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b521 2868 return -EPERM;
1da177e4 2869
f3c7691e 2870 return 0;
7715b521 2871}
1da177e4 2872
e1181ee6 2873static int handle_truncate(struct file *filp)
7715b521 2874{
f0bb5aaf 2875 const struct path *path = &filp->f_path;
7715b521
AV
2876 struct inode *inode = path->dentry->d_inode;
2877 int error = get_write_access(inode);
2878 if (error)
2879 return error;
2880 /*
2881 * Refuse to truncate files with mandatory locks held on them.
2882 */
d7a06983 2883 error = locks_verify_locked(filp);
7715b521 2884 if (!error)
ea0d3ab2 2885 error = security_path_truncate(path);
7715b521
AV
2886 if (!error) {
2887 error = do_truncate(path->dentry, 0,
2888 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6 2889 filp);
7715b521
AV
2890 }
2891 put_write_access(inode);
acd0c935 2892 return error;
1da177e4
LT
2893}
2894
d57999e1
DH
2895static inline int open_to_namei_flags(int flag)
2896{
8a5e929d
AV
2897 if ((flag & O_ACCMODE) == 3)
2898 flag--;
d57999e1
DH
2899 return flag;
2900}
2901
d3607752 2902static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
d18e9008 2903{
1328c727 2904 struct user_namespace *s_user_ns;
d18e9008
MS
2905 int error = security_path_mknod(dir, dentry, mode, 0);
2906 if (error)
2907 return error;
2908
1328c727
SF
2909 s_user_ns = dir->dentry->d_sb->s_user_ns;
2910 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2911 !kgid_has_mapping(s_user_ns, current_fsgid()))
2912 return -EOVERFLOW;
2913
d18e9008
MS
2914 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2915 if (error)
2916 return error;
2917
2918 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2919}
2920
1acf0af9
DH
2921/*
2922 * Attempt to atomically look up, create and open a file from a negative
2923 * dentry.
2924 *
2925 * Returns 0 if successful. The file will have been created and attached to
2926 * @file by the filesystem calling finish_open().
2927 *
00a07c15
AV
2928 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
2929 * be set. The caller will need to perform the open themselves. @path will
2930 * have been updated to point to the new dentry. This may be negative.
1acf0af9
DH
2931 *
2932 * Returns an error code otherwise.
2933 */
239eb983
AV
2934static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
2935 struct file *file,
2936 const struct open_flags *op,
2937 int open_flag, umode_t mode)
d18e9008 2938{
384f26e2 2939 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
d18e9008 2940 struct inode *dir = nd->path.dentry->d_inode;
d18e9008 2941 int error;
d18e9008 2942
384f26e2 2943 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
d18e9008 2944 open_flag &= ~O_TRUNC;
d18e9008 2945
d18e9008
MS
2946 if (nd->flags & LOOKUP_DIRECTORY)
2947 open_flag |= O_DIRECTORY;
2948
30d90494
AV
2949 file->f_path.dentry = DENTRY_NOT_SET;
2950 file->f_path.mnt = nd->path.mnt;
0fb1ea09 2951 error = dir->i_op->atomic_open(dir, dentry, file,
44907d79 2952 open_to_namei_flags(open_flag), mode);
6fbd0714 2953 d_lookup_done(dentry);
384f26e2 2954 if (!error) {
64e1ac4d 2955 if (file->f_mode & FMODE_OPENED) {
6fb968cd
AV
2956 if (unlikely(dentry != file->f_path.dentry)) {
2957 dput(dentry);
2958 dentry = dget(file->f_path.dentry);
2959 }
7be219b4 2960 if (file->f_mode & FMODE_CREATED)
64e1ac4d 2961 fsnotify_create(dir, dentry);
64e1ac4d 2962 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb 2963 error = -EIO;
03da633a 2964 } else {
384f26e2
AV
2965 if (file->f_path.dentry) {
2966 dput(dentry);
2967 dentry = file->f_path.dentry;
03da633a 2968 }
73a09dd9 2969 if (file->f_mode & FMODE_CREATED)
384f26e2 2970 fsnotify_create(dir, dentry);
239eb983 2971 if (unlikely(d_is_negative(dentry)))
a01e718f 2972 error = -ENOENT;
62b2ce96 2973 }
d18e9008 2974 }
239eb983
AV
2975 if (error) {
2976 dput(dentry);
2977 dentry = ERR_PTR(error);
2978 }
2979 return dentry;
d18e9008
MS
2980}
2981
d58ffd35 2982/*
1acf0af9 2983 * Look up and maybe create and open the last component.
d58ffd35 2984 *
00a07c15 2985 * Must be called with parent locked (exclusive in O_CREAT case).
1acf0af9 2986 *
00a07c15
AV
2987 * Returns 0 on success, that is, if
2988 * the file was successfully atomically created (if necessary) and opened, or
2989 * the file was not completely opened at this time, though lookups and
2990 * creations were performed.
2991 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
2992 * In the latter case dentry returned in @path might be negative if O_CREAT
2993 * hadn't been specified.
1acf0af9 2994 *
00a07c15 2995 * An error code is returned on failure.
d58ffd35 2996 */
da5ebf5a
AV
2997static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
2998 const struct open_flags *op,
2999 bool got_write)
d58ffd35
MS
3000{
3001 struct dentry *dir = nd->path.dentry;
54ef4872 3002 struct inode *dir_inode = dir->d_inode;
1643b43f 3003 int open_flag = op->open_flag;
d58ffd35 3004 struct dentry *dentry;
1643b43f 3005 int error, create_error = 0;
1643b43f 3006 umode_t mode = op->mode;
6fbd0714 3007 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
d58ffd35 3008
ce8644fc 3009 if (unlikely(IS_DEADDIR(dir_inode)))
da5ebf5a 3010 return ERR_PTR(-ENOENT);
d58ffd35 3011
73a09dd9 3012 file->f_mode &= ~FMODE_CREATED;
6fbd0714
AV
3013 dentry = d_lookup(dir, &nd->last);
3014 for (;;) {
3015 if (!dentry) {
3016 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3017 if (IS_ERR(dentry))
da5ebf5a 3018 return dentry;
6fbd0714
AV
3019 }
3020 if (d_in_lookup(dentry))
3021 break;
d58ffd35 3022
6fbd0714
AV
3023 error = d_revalidate(dentry, nd->flags);
3024 if (likely(error > 0))
3025 break;
3026 if (error)
3027 goto out_dput;
3028 d_invalidate(dentry);
3029 dput(dentry);
3030 dentry = NULL;
3031 }
3032 if (dentry->d_inode) {
6c51e513 3033 /* Cached positive dentry: will open in f_op->open */
da5ebf5a 3034 return dentry;
6c51e513 3035 }
d18e9008 3036
1643b43f
AV
3037 /*
3038 * Checking write permission is tricky, bacuse we don't know if we are
3039 * going to actually need it: O_CREAT opens should work as long as the
3040 * file exists. But checking existence breaks atomicity. The trick is
3041 * to check access and if not granted clear O_CREAT from the flags.
3042 *
3043 * Another problem is returing the "right" error value (e.g. for an
3044 * O_EXCL open we want to return EEXIST not EROFS).
3045 */
3046 if (open_flag & O_CREAT) {
3047 if (!IS_POSIXACL(dir->d_inode))
3048 mode &= ~current_umask();
3049 if (unlikely(!got_write)) {
3050 create_error = -EROFS;
3051 open_flag &= ~O_CREAT;
3052 if (open_flag & (O_EXCL | O_TRUNC))
3053 goto no_open;
3054 /* No side effects, safe to clear O_CREAT */
3055 } else {
3056 create_error = may_o_create(&nd->path, dentry, mode);
3057 if (create_error) {
3058 open_flag &= ~O_CREAT;
3059 if (open_flag & O_EXCL)
3060 goto no_open;
3061 }
3062 }
3063 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3064 unlikely(!got_write)) {
3065 /*
3066 * No O_CREATE -> atomicity not a requirement -> fall
3067 * back to lookup + open
3068 */
3069 goto no_open;
d18e9008
MS
3070 }
3071
6ac08709 3072 if (dir_inode->i_op->atomic_open) {
239eb983 3073 dentry = atomic_open(nd, dentry, file, op, open_flag, mode);
da5ebf5a
AV
3074 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3075 dentry = ERR_PTR(create_error);
3076 return dentry;
d18e9008 3077 }
54ef4872 3078
1643b43f 3079no_open:
6fbd0714 3080 if (d_in_lookup(dentry)) {
12fa5e24
AV
3081 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3082 nd->flags);
6fbd0714 3083 d_lookup_done(dentry);
12fa5e24
AV
3084 if (unlikely(res)) {
3085 if (IS_ERR(res)) {
3086 error = PTR_ERR(res);
3087 goto out_dput;
3088 }
3089 dput(dentry);
3090 dentry = res;
3091 }
54ef4872
MS
3092 }
3093
d58ffd35 3094 /* Negative dentry, just create the file */
1643b43f 3095 if (!dentry->d_inode && (open_flag & O_CREAT)) {
73a09dd9 3096 file->f_mode |= FMODE_CREATED;
ce8644fc 3097 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
ce8644fc
AV
3098 if (!dir_inode->i_op->create) {
3099 error = -EACCES;
d58ffd35 3100 goto out_dput;
ce8644fc
AV
3101 }
3102 error = dir_inode->i_op->create(dir_inode, dentry, mode,
1643b43f 3103 open_flag & O_EXCL);
d58ffd35
MS
3104 if (error)
3105 goto out_dput;
ce8644fc 3106 fsnotify_create(dir_inode, dentry);
d58ffd35 3107 }
1643b43f
AV
3108 if (unlikely(create_error) && !dentry->d_inode) {
3109 error = create_error;
3110 goto out_dput;
d58ffd35 3111 }
da5ebf5a 3112 return dentry;
d58ffd35
MS
3113
3114out_dput:
3115 dput(dentry);
da5ebf5a 3116 return ERR_PTR(error);
d58ffd35
MS
3117}
3118
c981a482 3119static const char *open_last_lookups(struct nameidata *nd,
3ec2eef1 3120 struct file *file, const struct open_flags *op)
fb1cc555 3121{
a1e28038 3122 struct dentry *dir = nd->path.dentry;
ca344a89 3123 int open_flag = op->open_flag;
64894cf8 3124 bool got_write = false;
254cf582 3125 unsigned seq;
a1eb3315 3126 struct inode *inode;
da5ebf5a 3127 struct dentry *dentry;
b0417d2c 3128 const char *res;
16c2cd71 3129 int error;
1f36f774 3130
c3e380b0
AV
3131 nd->flags |= op->intent;
3132
bc77daa7 3133 if (nd->last_type != LAST_NORM) {
56676ec3
AV
3134 if (nd->depth)
3135 put_link(nd);
7521f22b
AV
3136 res = handle_dots(nd, nd->last_type);
3137 if (likely(!res))
3138 res = ERR_PTR(complete_walk(nd));
3139 return res;
1f36f774 3140 }
67ee3ad2 3141
ca344a89 3142 if (!(open_flag & O_CREAT)) {
fe2d35ff
AV
3143 if (nd->last.name[nd->last.len])
3144 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3145 /* we _can_ be in RCU mode here */
20e34357
AV
3146 dentry = lookup_fast(nd, &inode, &seq);
3147 if (IS_ERR(dentry))
1ccac622 3148 return ERR_CAST(dentry);
20e34357 3149 if (likely(dentry))
71574865
MS
3150 goto finish_lookup;
3151
6583fe22 3152 BUG_ON(nd->flags & LOOKUP_RCU);
b6183df7
MS
3153 } else {
3154 /* create side of things */
3155 /*
3156 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3157 * has been cleared when we got to the last component we are
3158 * about to look up
3159 */
3160 error = complete_walk(nd);
c981a482 3161 if (unlikely(error))
1ccac622 3162 return ERR_PTR(error);
fe2d35ff 3163
c9b07eab 3164 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
b6183df7 3165 /* trailing slashes? */
deb106c6 3166 if (unlikely(nd->last.name[nd->last.len]))
1ccac622 3167 return ERR_PTR(-EISDIR);
b6183df7 3168 }
a2c36b45 3169
9cf843e3 3170 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
64894cf8
AV
3171 error = mnt_want_write(nd->path.mnt);
3172 if (!error)
3173 got_write = true;
3174 /*
3175 * do _not_ fail yet - we might not need that or fail with
3176 * a different error; let lookup_open() decide; we'll be
3177 * dropping this one anyway.
3178 */
3179 }
9cf843e3
AV
3180 if (open_flag & O_CREAT)
3181 inode_lock(dir->d_inode);
3182 else
3183 inode_lock_shared(dir->d_inode);
da5ebf5a 3184 dentry = lookup_open(nd, file, op, got_write);
9cf843e3
AV
3185 if (open_flag & O_CREAT)
3186 inode_unlock(dir->d_inode);
3187 else
3188 inode_unlock_shared(dir->d_inode);
a1e28038 3189
c981a482 3190 if (got_write)
59e96e65 3191 mnt_drop_write(nd->path.mnt);
d18e9008 3192
59e96e65
AV
3193 if (IS_ERR(dentry))
3194 return ERR_CAST(dentry);
3195
973d4b73 3196 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
e73cabff
AV
3197 dput(nd->path.dentry);
3198 nd->path.dentry = dentry;
c981a482 3199 return NULL;
fb1cc555
AV
3200 }
3201
20e34357 3202finish_lookup:
56676ec3
AV
3203 if (nd->depth)
3204 put_link(nd);
8c4efe22 3205 res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
b0417d2c 3206 if (unlikely(res)) {
b0417d2c 3207 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
b0417d2c 3208 return res;
1ccac622 3209 }
31d1726d
AV
3210
3211 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3212 audit_inode(nd->name, nd->path.dentry, 0);
1ccac622 3213 return ERR_PTR(-EEXIST);
31d1726d 3214 }
c981a482 3215
8f64fb1c 3216 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
c981a482
AV
3217 return ERR_PTR(complete_walk(nd));
3218}
3219
3220/*
3221 * Handle the last step of open()
3222 */
3223static const char *do_last(struct nameidata *nd,
3224 struct file *file, const struct open_flags *op)
3225{
c981a482
AV
3226 int open_flag = op->open_flag;
3227 bool do_truncate;
3228 int acc_mode;
3229 const char *link;
3230 int error;
3231
3232 link = open_last_lookups(nd, file, op);
3233 if (unlikely(link))
3234 return link;
3235
973d4b73
AV
3236 if (!(file->f_mode & FMODE_CREATED))
3237 audit_inode(nd->name, nd->path.dentry, 0);
30aba665 3238 if (open_flag & O_CREAT) {
30aba665 3239 if (d_is_dir(nd->path.dentry))
59e96e65 3240 return ERR_PTR(-EISDIR);
0f705953 3241 error = may_create_in_sticky(nd->dir_mode, nd->dir_uid,
30aba665
SM
3242 d_backing_inode(nd->path.dentry));
3243 if (unlikely(error))
59e96e65 3244 return ERR_PTR(error);
30aba665 3245 }
44b1d530 3246 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
59e96e65 3247 return ERR_PTR(-ENOTDIR);
6c0d46c4 3248
8795e7d4
AV
3249 do_truncate = false;
3250 acc_mode = op->acc_mode;
5a2d3edd
AV
3251 if (file->f_mode & FMODE_CREATED) {
3252 /* Don't check for write permission, don't truncate */
3253 open_flag &= ~O_TRUNC;
5a2d3edd 3254 acc_mode = 0;
8795e7d4 3255 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
0f9d1a10
AV
3256 error = mnt_want_write(nd->path.mnt);
3257 if (error)
59e96e65 3258 return ERR_PTR(error);
8795e7d4 3259 do_truncate = true;
0f9d1a10 3260 }
6ac08709 3261 error = may_open(&nd->path, acc_mode, open_flag);
8795e7d4 3262 if (!error && !(file->f_mode & FMODE_OPENED))
3ad5615a 3263 error = vfs_open(&nd->path, file);
8795e7d4
AV
3264 if (!error)
3265 error = ima_file_check(file, op->acc_mode);
3266 if (!error && do_truncate)
2675a4eb 3267 error = handle_truncate(file);
c80567c8
AV
3268 if (unlikely(error > 0)) {
3269 WARN_ON(1);
3270 error = -EINVAL;
3271 }
8795e7d4 3272 if (do_truncate)
0f9d1a10 3273 mnt_drop_write(nd->path.mnt);
1ccac622 3274 return ERR_PTR(error);
fb1cc555
AV
3275}
3276
af7bd4dc
AG
3277struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3278{
af7bd4dc
AG
3279 struct dentry *child = NULL;
3280 struct inode *dir = dentry->d_inode;
3281 struct inode *inode;
3282 int error;
3283
3284 /* we want directory to be writable */
3285 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3286 if (error)
3287 goto out_err;
3288 error = -EOPNOTSUPP;
3289 if (!dir->i_op->tmpfile)
3290 goto out_err;
3291 error = -ENOMEM;
cdf01226 3292 child = d_alloc(dentry, &slash_name);
af7bd4dc
AG
3293 if (unlikely(!child))
3294 goto out_err;
3295 error = dir->i_op->tmpfile(dir, child, mode);
3296 if (error)
3297 goto out_err;
3298 error = -ENOENT;
3299 inode = child->d_inode;
3300 if (unlikely(!inode))
3301 goto out_err;
3302 if (!(open_flag & O_EXCL)) {
3303 spin_lock(&inode->i_lock);
3304 inode->i_state |= I_LINKABLE;
3305 spin_unlock(&inode->i_lock);
3306 }
fdb2410f 3307 ima_post_create_tmpfile(inode);
af7bd4dc
AG
3308 return child;
3309
3310out_err:
3311 dput(child);
3312 return ERR_PTR(error);
3313}
3314EXPORT_SYMBOL(vfs_tmpfile);
3315
c8a53ee5 3316static int do_tmpfile(struct nameidata *nd, unsigned flags,
60545d0d 3317 const struct open_flags *op,
3ec2eef1 3318 struct file *file)
60545d0d 3319{
625b6d10 3320 struct dentry *child;
625b6d10 3321 struct path path;
c8a53ee5 3322 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
60545d0d
AV
3323 if (unlikely(error))
3324 return error;
625b6d10 3325 error = mnt_want_write(path.mnt);
60545d0d
AV
3326 if (unlikely(error))
3327 goto out;
af7bd4dc
AG
3328 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3329 error = PTR_ERR(child);
684e73be 3330 if (IS_ERR(child))
60545d0d 3331 goto out2;
625b6d10
AV
3332 dput(path.dentry);
3333 path.dentry = child;
c8a53ee5 3334 audit_inode(nd->name, child, 0);
69a91c23 3335 /* Don't check for other permissions, the inode was just created */
62fb4a15 3336 error = may_open(&path, 0, op->open_flag);
60545d0d
AV
3337 if (error)
3338 goto out2;
625b6d10 3339 file->f_path.mnt = path.mnt;
be12af3e 3340 error = finish_open(file, child, NULL);
60545d0d 3341out2:
625b6d10 3342 mnt_drop_write(path.mnt);
60545d0d 3343out:
625b6d10 3344 path_put(&path);
60545d0d
AV
3345 return error;
3346}
3347
6ac08709
AV
3348static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3349{
3350 struct path path;
3351 int error = path_lookupat(nd, flags, &path);
3352 if (!error) {
3353 audit_inode(nd->name, path.dentry, 0);
ae2bb293 3354 error = vfs_open(&path, file);
6ac08709
AV
3355 path_put(&path);
3356 }
3357 return error;
3358}
3359
c8a53ee5
AV
3360static struct file *path_openat(struct nameidata *nd,
3361 const struct open_flags *op, unsigned flags)
1da177e4 3362{
30d90494 3363 struct file *file;
13aab428 3364 int error;
31e6b01f 3365
ea73ea72 3366 file = alloc_empty_file(op->open_flag, current_cred());
1afc99be
AV
3367 if (IS_ERR(file))
3368 return file;
31e6b01f 3369
bb458c64 3370 if (unlikely(file->f_flags & __O_TMPFILE)) {
3ec2eef1 3371 error = do_tmpfile(nd, flags, op, file);
5f336e72 3372 } else if (unlikely(file->f_flags & O_PATH)) {
6ac08709 3373 error = do_o_path(nd, flags, file);
5f336e72
AV
3374 } else {
3375 const char *s = path_init(nd, flags);
3376 while (!(error = link_path_walk(s, nd)) &&
1ccac622
AV
3377 (s = do_last(nd, file, op)) != NULL)
3378 ;
5f336e72 3379 terminate_walk(nd);
806b681c 3380 }
7c1c01ec 3381 if (likely(!error)) {
aad888f8 3382 if (likely(file->f_mode & FMODE_OPENED))
7c1c01ec
AV
3383 return file;
3384 WARN_ON(1);
3385 error = -EINVAL;
16b1c1cd 3386 }
7c1c01ec
AV
3387 fput(file);
3388 if (error == -EOPENSTALE) {
3389 if (flags & LOOKUP_RCU)
3390 error = -ECHILD;
3391 else
3392 error = -ESTALE;
2675a4eb 3393 }
7c1c01ec 3394 return ERR_PTR(error);
1da177e4
LT
3395}
3396
669abf4e 3397struct file *do_filp_open(int dfd, struct filename *pathname,
f9652e10 3398 const struct open_flags *op)
13aab428 3399{
9883d185 3400 struct nameidata nd;
f9652e10 3401 int flags = op->lookup_flags;
13aab428
AV
3402 struct file *filp;
3403
9883d185 3404 set_nameidata(&nd, dfd, pathname);
c8a53ee5 3405 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
13aab428 3406 if (unlikely(filp == ERR_PTR(-ECHILD)))
c8a53ee5 3407 filp = path_openat(&nd, op, flags);
13aab428 3408 if (unlikely(filp == ERR_PTR(-ESTALE)))
c8a53ee5 3409 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d185 3410 restore_nameidata();
13aab428
AV
3411 return filp;
3412}
3413
73d049a4 3414struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
f9652e10 3415 const char *name, const struct open_flags *op)
73d049a4 3416{
9883d185 3417 struct nameidata nd;
73d049a4 3418 struct file *file;
51689104 3419 struct filename *filename;
f9652e10 3420 int flags = op->lookup_flags | LOOKUP_ROOT;
73d049a4
AV
3421
3422 nd.root.mnt = mnt;
3423 nd.root.dentry = dentry;
3424
b18825a7 3425 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
73d049a4
AV
3426 return ERR_PTR(-ELOOP);
3427
51689104 3428 filename = getname_kernel(name);
a1c83681 3429 if (IS_ERR(filename))
51689104
PM
3430 return ERR_CAST(filename);
3431
9883d185 3432 set_nameidata(&nd, -1, filename);
c8a53ee5 3433 file = path_openat(&nd, op, flags | LOOKUP_RCU);
73d049a4 3434 if (unlikely(file == ERR_PTR(-ECHILD)))
c8a53ee5 3435 file = path_openat(&nd, op, flags);
73d049a4 3436 if (unlikely(file == ERR_PTR(-ESTALE)))
c8a53ee5 3437 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d185 3438 restore_nameidata();
51689104 3439 putname(filename);
73d049a4
AV
3440 return file;
3441}
3442
fa14a0b8 3443static struct dentry *filename_create(int dfd, struct filename *name,
1ac12b4b 3444 struct path *path, unsigned int lookup_flags)
1da177e4 3445{
c663e5d8 3446 struct dentry *dentry = ERR_PTR(-EEXIST);
391172c4
AV
3447 struct qstr last;
3448 int type;
c30dabfe 3449 int err2;
1ac12b4b
JL
3450 int error;
3451 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3452
3453 /*
3454 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3455 * other flags passed in are ignored!
3456 */
3457 lookup_flags &= LOOKUP_REVAL;
3458
5c31b6ce
AV
3459 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3460 if (IS_ERR(name))
3461 return ERR_CAST(name);
1da177e4 3462
c663e5d8
CH
3463 /*
3464 * Yucky last component or no last component at all?
3465 * (foo/., foo/.., /////)
3466 */
5c31b6ce 3467 if (unlikely(type != LAST_NORM))
ed75e95d 3468 goto out;
c663e5d8 3469
c30dabfe 3470 /* don't fail immediately if it's r/o, at least try to report other errors */
391172c4 3471 err2 = mnt_want_write(path->mnt);
c663e5d8
CH
3472 /*
3473 * Do the final lookup.
3474 */
391172c4 3475 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
5955102c 3476 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
391172c4 3477 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
1da177e4 3478 if (IS_ERR(dentry))
a8104a9f 3479 goto unlock;
c663e5d8 3480
a8104a9f 3481 error = -EEXIST;
b18825a7 3482 if (d_is_positive(dentry))
a8104a9f 3483 goto fail;
b18825a7 3484
c663e5d8
CH
3485 /*
3486 * Special case - lookup gave negative, but... we had foo/bar/
3487 * From the vfs_mknod() POV we just have a negative dentry -
3488 * all is fine. Let's be bastards - you had / on the end, you've
3489 * been asking for (non-existent) directory. -ENOENT for you.
3490 */
391172c4 3491 if (unlikely(!is_dir && last.name[last.len])) {
a8104a9f 3492 error = -ENOENT;
ed75e95d 3493 goto fail;
e9baf6e5 3494 }
c30dabfe
JK
3495 if (unlikely(err2)) {
3496 error = err2;
a8104a9f 3497 goto fail;
c30dabfe 3498 }
181c37b6 3499 putname(name);
1da177e4 3500 return dentry;
1da177e4 3501fail:
a8104a9f
AV
3502 dput(dentry);
3503 dentry = ERR_PTR(error);
3504unlock:
5955102c 3505 inode_unlock(path->dentry->d_inode);
c30dabfe 3506 if (!err2)
391172c4 3507 mnt_drop_write(path->mnt);
ed75e95d 3508out:
391172c4 3509 path_put(path);
181c37b6 3510 putname(name);
1da177e4
LT
3511 return dentry;
3512}
fa14a0b8
AV
3513
3514struct dentry *kern_path_create(int dfd, const char *pathname,
3515 struct path *path, unsigned int lookup_flags)
3516{
181c37b6
AV
3517 return filename_create(dfd, getname_kernel(pathname),
3518 path, lookup_flags);
fa14a0b8 3519}
dae6ad8f
AV
3520EXPORT_SYMBOL(kern_path_create);
3521
921a1650
AV
3522void done_path_create(struct path *path, struct dentry *dentry)
3523{
3524 dput(dentry);
5955102c 3525 inode_unlock(path->dentry->d_inode);
a8104a9f 3526 mnt_drop_write(path->mnt);
921a1650
AV
3527 path_put(path);
3528}
3529EXPORT_SYMBOL(done_path_create);
3530
520ae687 3531inline struct dentry *user_path_create(int dfd, const char __user *pathname,
1ac12b4b 3532 struct path *path, unsigned int lookup_flags)
dae6ad8f 3533{
181c37b6 3534 return filename_create(dfd, getname(pathname), path, lookup_flags);
dae6ad8f
AV
3535}
3536EXPORT_SYMBOL(user_path_create);
3537
1a67aafb 3538int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4 3539{
a95164d9 3540 int error = may_create(dir, dentry);
1da177e4
LT
3541
3542 if (error)
3543 return error;
3544
94f82008 3545 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1da177e4
LT
3546 return -EPERM;
3547
acfa4380 3548 if (!dir->i_op->mknod)
1da177e4
LT
3549 return -EPERM;
3550
08ce5f16
SH
3551 error = devcgroup_inode_mknod(mode, dev);
3552 if (error)
3553 return error;
3554
1da177e4
LT
3555 error = security_inode_mknod(dir, dentry, mode, dev);
3556 if (error)
3557 return error;
3558
1da177e4 3559 error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aa 3560 if (!error)
f38aa942 3561 fsnotify_create(dir, dentry);
1da177e4
LT
3562 return error;
3563}
4d359507 3564EXPORT_SYMBOL(vfs_mknod);
1da177e4 3565
f69aac00 3566static int may_mknod(umode_t mode)
463c3197
DH
3567{
3568 switch (mode & S_IFMT) {
3569 case S_IFREG:
3570 case S_IFCHR:
3571 case S_IFBLK:
3572 case S_IFIFO:
3573 case S_IFSOCK:
3574 case 0: /* zero mode translates to S_IFREG */
3575 return 0;
3576 case S_IFDIR:
3577 return -EPERM;
3578 default:
3579 return -EINVAL;
3580 }
3581}
3582
87c4e192
DB
3583long do_mknodat(int dfd, const char __user *filename, umode_t mode,
3584 unsigned int dev)
1da177e4 3585{
2ad94ae6 3586 struct dentry *dentry;
dae6ad8f
AV
3587 struct path path;
3588 int error;
972567f1 3589 unsigned int lookup_flags = 0;
1da177e4 3590
8e4bfca1
AV
3591 error = may_mknod(mode);
3592 if (error)
3593 return error;
972567f1
JL
3594retry:
3595 dentry = user_path_create(dfd, filename, &path, lookup_flags);
dae6ad8f
AV
3596 if (IS_ERR(dentry))
3597 return PTR_ERR(dentry);
2ad94ae6 3598
dae6ad8f 3599 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3600 mode &= ~current_umask();
dae6ad8f 3601 error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56 3602 if (error)
a8104a9f 3603 goto out;
463c3197 3604 switch (mode & S_IFMT) {
1da177e4 3605 case 0: case S_IFREG:
312b63fb 3606 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
05d1a717
MZ
3607 if (!error)
3608 ima_post_path_mknod(dentry);
1da177e4
LT
3609 break;
3610 case S_IFCHR: case S_IFBLK:
dae6ad8f 3611 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4
LT
3612 new_decode_dev(dev));
3613 break;
3614 case S_IFIFO: case S_IFSOCK:
dae6ad8f 3615 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4 3616 break;
1da177e4 3617 }
a8104a9f 3618out:
921a1650 3619 done_path_create(&path, dentry);
972567f1
JL
3620 if (retry_estale(error, lookup_flags)) {
3621 lookup_flags |= LOOKUP_REVAL;
3622 goto retry;
3623 }
1da177e4
LT
3624 return error;
3625}
3626
87c4e192
DB
3627SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3628 unsigned int, dev)
3629{
3630 return do_mknodat(dfd, filename, mode, dev);
3631}
3632
8208a22b 3633SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d 3634{
87c4e192 3635 return do_mknodat(AT_FDCWD, filename, mode, dev);
5590ff0d
UD
3636}
3637
18bb1db3 3638int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4 3639{
a95164d9 3640 int error = may_create(dir, dentry);
8de52778 3641 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
3642
3643 if (error)
3644 return error;
3645
acfa4380 3646 if (!dir->i_op->mkdir)
1da177e4
LT
3647 return -EPERM;
3648
3649 mode &= (S_IRWXUGO|S_ISVTX);
3650 error = security_inode_mkdir(dir, dentry, mode);
3651 if (error)
3652 return error;
3653
8de52778
AV
3654 if (max_links && dir->i_nlink >= max_links)
3655 return -EMLINK;
3656
1da177e4 3657 error = dir->i_op->mkdir(dir, dentry, mode);
a74574aa 3658 if (!error)
f38aa942 3659 fsnotify_mkdir(dir, dentry);
1da177e4
LT
3660 return error;
3661}
4d359507 3662EXPORT_SYMBOL(vfs_mkdir);
1da177e4 3663
0101db7a 3664long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
1da177e4 3665{
6902d925 3666 struct dentry *dentry;
dae6ad8f
AV
3667 struct path path;
3668 int error;
b76d8b82 3669 unsigned int lookup_flags = LOOKUP_DIRECTORY;
1da177e4 3670
b76d8b82
JL
3671retry:
3672 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
6902d925 3673 if (IS_ERR(dentry))
dae6ad8f 3674 return PTR_ERR(dentry);
1da177e4 3675
dae6ad8f 3676 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3677 mode &= ~current_umask();
dae6ad8f 3678 error = security_path_mkdir(&path, dentry, mode);
a8104a9f
AV
3679 if (!error)
3680 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
921a1650 3681 done_path_create(&path, dentry);
b76d8b82
JL
3682 if (retry_estale(error, lookup_flags)) {
3683 lookup_flags |= LOOKUP_REVAL;
3684 goto retry;
3685 }
1da177e4
LT
3686 return error;
3687}
3688
0101db7a
DB
3689SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3690{
3691 return do_mkdirat(dfd, pathname, mode);
3692}
3693
a218d0fd 3694SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d 3695{
0101db7a 3696 return do_mkdirat(AT_FDCWD, pathname, mode);
5590ff0d
UD
3697}
3698
1da177e4
LT
3699int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3700{
3701 int error = may_delete(dir, dentry, 1);
3702
3703 if (error)
3704 return error;
3705
acfa4380 3706 if (!dir->i_op->rmdir)
1da177e4
LT
3707 return -EPERM;
3708
1d2ef590 3709 dget(dentry);
5955102c 3710 inode_lock(dentry->d_inode);
912dbc15
SW
3711
3712 error = -EBUSY;
7af1364f 3713 if (is_local_mountpoint(dentry))
912dbc15
SW
3714 goto out;
3715
3716 error = security_inode_rmdir(dir, dentry);
3717 if (error)
3718 goto out;
3719
3720 error = dir->i_op->rmdir(dir, dentry);
3721 if (error)
3722 goto out;
3723
8767712f 3724 shrink_dcache_parent(dentry);
912dbc15
SW
3725 dentry->d_inode->i_flags |= S_DEAD;
3726 dont_mount(dentry);
8ed936b5 3727 detach_mounts(dentry);
116b9731 3728 fsnotify_rmdir(dir, dentry);
912dbc15
SW
3729
3730out:
5955102c 3731 inode_unlock(dentry->d_inode);
1d2ef590 3732 dput(dentry);
912dbc15 3733 if (!error)
1da177e4 3734 d_delete(dentry);
1da177e4
LT
3735 return error;
3736}
4d359507 3737EXPORT_SYMBOL(vfs_rmdir);
1da177e4 3738
f459dffa 3739long do_rmdir(int dfd, const char __user *pathname)
1da177e4
LT
3740{
3741 int error = 0;
91a27b2a 3742 struct filename *name;
1da177e4 3743 struct dentry *dentry;
f5beed75
AV
3744 struct path path;
3745 struct qstr last;
3746 int type;
c6ee9206
JL
3747 unsigned int lookup_flags = 0;
3748retry:
c1d4dd27
AV
3749 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3750 &path, &last, &type);
91a27b2a
JL
3751 if (IS_ERR(name))
3752 return PTR_ERR(name);
1da177e4 3753
f5beed75 3754 switch (type) {
0612d9fb
OH
3755 case LAST_DOTDOT:
3756 error = -ENOTEMPTY;
3757 goto exit1;
3758 case LAST_DOT:
3759 error = -EINVAL;
3760 goto exit1;
3761 case LAST_ROOT:
3762 error = -EBUSY;
3763 goto exit1;
1da177e4 3764 }
0612d9fb 3765
f5beed75 3766 error = mnt_want_write(path.mnt);
c30dabfe
JK
3767 if (error)
3768 goto exit1;
0612d9fb 3769
5955102c 3770 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
f5beed75 3771 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
1da177e4 3772 error = PTR_ERR(dentry);
6902d925
DH
3773 if (IS_ERR(dentry))
3774 goto exit2;
e6bc45d6
TT
3775 if (!dentry->d_inode) {
3776 error = -ENOENT;
3777 goto exit3;
3778 }
f5beed75 3779 error = security_path_rmdir(&path, dentry);
be6d3e56 3780 if (error)
c30dabfe 3781 goto exit3;
f5beed75 3782 error = vfs_rmdir(path.dentry->d_inode, dentry);
0622753b 3783exit3:
6902d925
DH
3784 dput(dentry);
3785exit2:
5955102c 3786 inode_unlock(path.dentry->d_inode);
f5beed75 3787 mnt_drop_write(path.mnt);
1da177e4 3788exit1:
f5beed75 3789 path_put(&path);
1da177e4 3790 putname(name);
c6ee9206
JL
3791 if (retry_estale(error, lookup_flags)) {
3792 lookup_flags |= LOOKUP_REVAL;
3793 goto retry;
3794 }
1da177e4
LT
3795 return error;
3796}
3797
3cdad428 3798SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d
UD
3799{
3800 return do_rmdir(AT_FDCWD, pathname);
3801}
3802
b21996e3
BF
3803/**
3804 * vfs_unlink - unlink a filesystem object
3805 * @dir: parent directory
3806 * @dentry: victim
3807 * @delegated_inode: returns victim inode, if the inode is delegated.
3808 *
3809 * The caller must hold dir->i_mutex.
3810 *
3811 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3812 * return a reference to the inode in delegated_inode. The caller
3813 * should then break the delegation on that inode and retry. Because
3814 * breaking a delegation may take a long time, the caller should drop
3815 * dir->i_mutex before doing so.
3816 *
3817 * Alternatively, a caller may pass NULL for delegated_inode. This may
3818 * be appropriate for callers that expect the underlying filesystem not
3819 * to be NFS exported.
3820 */
3821int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
1da177e4 3822{
9accbb97 3823 struct inode *target = dentry->d_inode;
1da177e4
LT
3824 int error = may_delete(dir, dentry, 0);
3825
3826 if (error)
3827 return error;
3828
acfa4380 3829 if (!dir->i_op->unlink)
1da177e4
LT
3830 return -EPERM;
3831
5955102c 3832 inode_lock(target);
8ed936b5 3833 if (is_local_mountpoint(dentry))
1da177e4
LT
3834 error = -EBUSY;
3835 else {
3836 error = security_inode_unlink(dir, dentry);
bec1052e 3837 if (!error) {
5a14696c
BF
3838 error = try_break_deleg(target, delegated_inode);
3839 if (error)
b21996e3 3840 goto out;
1da177e4 3841 error = dir->i_op->unlink(dir, dentry);
8ed936b5 3842 if (!error) {
d83c49f3 3843 dont_mount(dentry);
8ed936b5 3844 detach_mounts(dentry);
116b9731 3845 fsnotify_unlink(dir, dentry);
8ed936b5 3846 }
bec1052e 3847 }
1da177e4 3848 }
b21996e3 3849out:
5955102c 3850 inode_unlock(target);
1da177e4
LT
3851
3852 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3853 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
9accbb97 3854 fsnotify_link_count(target);
e234f35c 3855 d_delete(dentry);
1da177e4 3856 }
0eeca283 3857
1da177e4
LT
3858 return error;
3859}
4d359507 3860EXPORT_SYMBOL(vfs_unlink);
1da177e4
LT
3861
3862/*
3863 * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b 3864 * directory's i_mutex. Truncate can take a long time if there is a lot of
1da177e4
LT
3865 * writeout happening, and we don't want to prevent access to the directory
3866 * while waiting on the I/O.
3867 */
da2f1362 3868long do_unlinkat(int dfd, struct filename *name)
1da177e4 3869{
2ad94ae6 3870 int error;
1da177e4 3871 struct dentry *dentry;
f5beed75
AV
3872 struct path path;
3873 struct qstr last;
3874 int type;
1da177e4 3875 struct inode *inode = NULL;
b21996e3 3876 struct inode *delegated_inode = NULL;
5d18f813
JL
3877 unsigned int lookup_flags = 0;
3878retry:
da2f1362 3879 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
91a27b2a
JL
3880 if (IS_ERR(name))
3881 return PTR_ERR(name);
2ad94ae6 3882
1da177e4 3883 error = -EISDIR;
f5beed75 3884 if (type != LAST_NORM)
1da177e4 3885 goto exit1;
0612d9fb 3886
f5beed75 3887 error = mnt_want_write(path.mnt);
c30dabfe
JK
3888 if (error)
3889 goto exit1;
b21996e3 3890retry_deleg:
5955102c 3891 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
f5beed75 3892 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
1da177e4
LT
3893 error = PTR_ERR(dentry);
3894 if (!IS_ERR(dentry)) {
3895 /* Why not before? Because we want correct error value */
f5beed75 3896 if (last.name[last.len])
50338b88 3897 goto slashes;
1da177e4 3898 inode = dentry->d_inode;
b18825a7 3899 if (d_is_negative(dentry))
e6bc45d6
TT
3900 goto slashes;
3901 ihold(inode);
f5beed75 3902 error = security_path_unlink(&path, dentry);
be6d3e56 3903 if (error)
c30dabfe 3904 goto exit2;
f5beed75 3905 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
c30dabfe 3906exit2:
1da177e4
LT
3907 dput(dentry);
3908 }
5955102c 3909 inode_unlock(path.dentry->d_inode);
1da177e4
LT
3910 if (inode)
3911 iput(inode); /* truncate the inode here */
b21996e3
BF
3912 inode = NULL;
3913 if (delegated_inode) {
5a14696c 3914 error = break_deleg_wait(&delegated_inode);
b21996e3
BF
3915 if (!error)
3916 goto retry_deleg;
3917 }
f5beed75 3918 mnt_drop_write(path.mnt);
1da177e4 3919exit1:
f5beed75 3920 path_put(&path);
5d18f813
JL
3921 if (retry_estale(error, lookup_flags)) {
3922 lookup_flags |= LOOKUP_REVAL;
3923 inode = NULL;
3924 goto retry;
3925 }
da2f1362 3926 putname(name);
1da177e4
LT
3927 return error;
3928
3929slashes:
b18825a7
DH
3930 if (d_is_negative(dentry))
3931 error = -ENOENT;
44b1d530 3932 else if (d_is_dir(dentry))
b18825a7
DH
3933 error = -EISDIR;
3934 else
3935 error = -ENOTDIR;
1da177e4
LT
3936 goto exit2;
3937}
3938
2e4d0924 3939SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d
UD
3940{
3941 if ((flag & ~AT_REMOVEDIR) != 0)
3942 return -EINVAL;
3943
3944 if (flag & AT_REMOVEDIR)
3945 return do_rmdir(dfd, pathname);
3946
da2f1362 3947 return do_unlinkat(dfd, getname(pathname));
5590ff0d
UD
3948}
3949
3480b257 3950SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d 3951{
da2f1362 3952 return do_unlinkat(AT_FDCWD, getname(pathname));
5590ff0d
UD
3953}
3954
db2e747b 3955int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4 3956{
a95164d9 3957 int error = may_create(dir, dentry);
1da177e4
LT
3958
3959 if (error)
3960 return error;
3961
acfa4380 3962 if (!dir->i_op->symlink)
1da177e4
LT
3963 return -EPERM;
3964
3965 error = security_inode_symlink(dir, dentry, oldname);
3966 if (error)
3967 return error;
3968
1da177e4 3969 error = dir->i_op->symlink(dir, dentry, oldname);
a74574aa 3970 if (!error)
f38aa942 3971 fsnotify_create(dir, dentry);
1da177e4
LT
3972 return error;
3973}
4d359507 3974EXPORT_SYMBOL(vfs_symlink);
1da177e4 3975
b724e846
DB
3976long do_symlinkat(const char __user *oldname, int newdfd,
3977 const char __user *newname)
1da177e4 3978{
2ad94ae6 3979 int error;
91a27b2a 3980 struct filename *from;
6902d925 3981 struct dentry *dentry;
dae6ad8f 3982 struct path path;
f46d3567 3983 unsigned int lookup_flags = 0;
1da177e4
LT
3984
3985 from = getname(oldname);
2ad94ae6 3986 if (IS_ERR(from))
1da177e4 3987 return PTR_ERR(from);
f46d3567
JL
3988retry:
3989 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
6902d925
DH
3990 error = PTR_ERR(dentry);
3991 if (IS_ERR(dentry))
dae6ad8f 3992 goto out_putname;
6902d925 3993
91a27b2a 3994 error = security_path_symlink(&path, dentry, from->name);
a8104a9f 3995 if (!error)
91a27b2a 3996 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
921a1650 3997 done_path_create(&path, dentry);
f46d3567
JL
3998 if (retry_estale(error, lookup_flags)) {
3999 lookup_flags |= LOOKUP_REVAL;
4000 goto retry;
4001 }
6902d925 4002out_putname:
1da177e4
LT
4003 putname(from);
4004 return error;
4005}
4006
b724e846
DB
4007SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4008 int, newdfd, const char __user *, newname)
4009{
4010 return do_symlinkat(oldname, newdfd, newname);
4011}
4012
3480b257 4013SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d 4014{
b724e846 4015 return do_symlinkat(oldname, AT_FDCWD, newname);
5590ff0d
UD
4016}
4017
146a8595
BF
4018/**
4019 * vfs_link - create a new link
4020 * @old_dentry: object to be linked
4021 * @dir: new parent
4022 * @new_dentry: where to create the new link
4023 * @delegated_inode: returns inode needing a delegation break
4024 *
4025 * The caller must hold dir->i_mutex
4026 *
4027 * If vfs_link discovers a delegation on the to-be-linked file in need
4028 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4029 * inode in delegated_inode. The caller should then break the delegation
4030 * and retry. Because breaking a delegation may take a long time, the
4031 * caller should drop the i_mutex before doing so.
4032 *
4033 * Alternatively, a caller may pass NULL for delegated_inode. This may
4034 * be appropriate for callers that expect the underlying filesystem not
4035 * to be NFS exported.
4036 */
4037int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
1da177e4
LT
4038{
4039 struct inode *inode = old_dentry->d_inode;
8de52778 4040 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
4041 int error;
4042
4043 if (!inode)
4044 return -ENOENT;
4045
a95164d9 4046 error = may_create(dir, new_dentry);
1da177e4
LT
4047 if (error)
4048 return error;
4049
4050 if (dir->i_sb != inode->i_sb)
4051 return -EXDEV;
4052
4053 /*
4054 * A link to an append-only or immutable file cannot be created.
4055 */
4056 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4057 return -EPERM;
0bd23d09
EB
4058 /*
4059 * Updating the link count will likely cause i_uid and i_gid to
4060 * be writen back improperly if their true value is unknown to
4061 * the vfs.
4062 */
4063 if (HAS_UNMAPPED_ID(inode))
4064 return -EPERM;
acfa4380 4065 if (!dir->i_op->link)
1da177e4 4066 return -EPERM;
7e79eedb 4067 if (S_ISDIR(inode->i_mode))
1da177e4
LT
4068 return -EPERM;
4069
4070 error = security_inode_link(old_dentry, dir, new_dentry);
4071 if (error)
4072 return error;
4073
5955102c 4074 inode_lock(inode);
aae8a97d 4075 /* Make sure we don't allow creating hardlink to an unlinked file */
f4e0c30c 4076 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
aae8a97d 4077 error = -ENOENT;
8de52778
AV
4078 else if (max_links && inode->i_nlink >= max_links)
4079 error = -EMLINK;
146a8595
BF
4080 else {
4081 error = try_break_deleg(inode, delegated_inode);
4082 if (!error)
4083 error = dir->i_op->link(old_dentry, dir, new_dentry);
4084 }
f4e0c30c
AV
4085
4086 if (!error && (inode->i_state & I_LINKABLE)) {
4087 spin_lock(&inode->i_lock);
4088 inode->i_state &= ~I_LINKABLE;
4089 spin_unlock(&inode->i_lock);
4090 }
5955102c 4091 inode_unlock(inode);
e31e14ec 4092 if (!error)
7e79eedb 4093 fsnotify_link(dir, inode, new_dentry);
1da177e4
LT
4094 return error;
4095}
4d359507 4096EXPORT_SYMBOL(vfs_link);
1da177e4
LT
4097
4098/*
4099 * Hardlinks are often used in delicate situations. We avoid
4100 * security-related surprises by not following symlinks on the
4101 * newname. --KAB
4102 *
4103 * We don't follow them on the oldname either to be compatible
4104 * with linux 2.0, and to avoid hard-linking to directories
4105 * and other special files. --ADM
4106 */
46ea89eb
DB
4107int do_linkat(int olddfd, const char __user *oldname, int newdfd,
4108 const char __user *newname, int flags)
1da177e4
LT
4109{
4110 struct dentry *new_dentry;
dae6ad8f 4111 struct path old_path, new_path;
146a8595 4112 struct inode *delegated_inode = NULL;
11a7b371 4113 int how = 0;
1da177e4 4114 int error;
1da177e4 4115
11a7b371 4116 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e1 4117 return -EINVAL;
11a7b371 4118 /*
f0cc6ffb
LT
4119 * To use null names we require CAP_DAC_READ_SEARCH
4120 * This ensures that not everyone will be able to create
4121 * handlink using the passed filedescriptor.
11a7b371 4122 */
f0cc6ffb
LT
4123 if (flags & AT_EMPTY_PATH) {
4124 if (!capable(CAP_DAC_READ_SEARCH))
4125 return -ENOENT;
11a7b371 4126 how = LOOKUP_EMPTY;
f0cc6ffb 4127 }
11a7b371
AK
4128
4129 if (flags & AT_SYMLINK_FOLLOW)
4130 how |= LOOKUP_FOLLOW;
442e31ca 4131retry:
11a7b371 4132 error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4 4133 if (error)
2ad94ae6
AV
4134 return error;
4135
442e31ca
JL
4136 new_dentry = user_path_create(newdfd, newname, &new_path,
4137 (how & LOOKUP_REVAL));
1da177e4 4138 error = PTR_ERR(new_dentry);
6902d925 4139 if (IS_ERR(new_dentry))
dae6ad8f
AV
4140 goto out;
4141
4142 error = -EXDEV;
4143 if (old_path.mnt != new_path.mnt)
4144 goto out_dput;
800179c9
KC
4145 error = may_linkat(&old_path);
4146 if (unlikely(error))
4147 goto out_dput;
dae6ad8f 4148 error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56 4149 if (error)
a8104a9f 4150 goto out_dput;
146a8595 4151 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
75c3f29d 4152out_dput:
921a1650 4153 done_path_create(&new_path, new_dentry);
146a8595
BF
4154 if (delegated_inode) {
4155 error = break_deleg_wait(&delegated_inode);
d22e6338
OD
4156 if (!error) {
4157 path_put(&old_path);
146a8595 4158 goto retry;
d22e6338 4159 }
146a8595 4160 }
442e31ca 4161 if (retry_estale(error, how)) {
d22e6338 4162 path_put(&old_path);
442e31ca
JL
4163 how |= LOOKUP_REVAL;
4164 goto retry;
4165 }
1da177e4 4166out:
2d8f3038 4167 path_put(&old_path);
1da177e4
LT
4168
4169 return error;
4170}
4171
46ea89eb
DB
4172SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4173 int, newdfd, const char __user *, newname, int, flags)
4174{
4175 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4176}
4177
3480b257 4178SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d 4179{
46ea89eb 4180 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
4181}
4182
bc27027a
MS
4183/**
4184 * vfs_rename - rename a filesystem object
4185 * @old_dir: parent of source
4186 * @old_dentry: source
4187 * @new_dir: parent of destination
4188 * @new_dentry: destination
4189 * @delegated_inode: returns an inode needing a delegation break
520c8b16 4190 * @flags: rename flags
bc27027a
MS
4191 *
4192 * The caller must hold multiple mutexes--see lock_rename()).
4193 *
4194 * If vfs_rename discovers a delegation in need of breaking at either
4195 * the source or destination, it will return -EWOULDBLOCK and return a
4196 * reference to the inode in delegated_inode. The caller should then
4197 * break the delegation and retry. Because breaking a delegation may
4198 * take a long time, the caller should drop all locks before doing
4199 * so.
4200 *
4201 * Alternatively, a caller may pass NULL for delegated_inode. This may
4202 * be appropriate for callers that expect the underlying filesystem not
4203 * to be NFS exported.
4204 *
1da177e4
LT
4205 * The worst of all namespace operations - renaming directory. "Perverted"
4206 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4207 * Problems:
0117d427 4208 *
d03b29a2 4209 * a) we can get into loop creation.
1da177e4
LT
4210 * b) race potential - two innocent renames can create a loop together.
4211 * That's where 4.4 screws up. Current fix: serialization on
a11f3a05 4212 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4 4213 * story.
6cedba89
BF
4214 * c) we have to lock _four_ objects - parents and victim (if it exists),
4215 * and source (if it is not a directory).
1b1dcc1b 4216 * And that - after we got ->i_mutex on parents (until then we don't know
1da177e4
LT
4217 * whether the target exists). Solution: try to be smart with locking
4218 * order for inodes. We rely on the fact that tree topology may change
a11f3a05 4219 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4
LT
4220 * move will be locked. Thus we can rank directories by the tree
4221 * (ancestors first) and rank all non-directories after them.
4222 * That works since everybody except rename does "lock parent, lookup,
a11f3a05 4223 * lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4
LT
4224 * HOWEVER, it relies on the assumption that any object with ->lookup()
4225 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4226 * we'd better make sure that there's no link(2) for them.
e4eaac06 4227 * d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b 4228 * we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4 4229 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e7 4230 * ->i_mutex on parents, which works but leads to some truly excessive
1da177e4
LT
4231 * locking].
4232 */
bc27027a
MS
4233int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4234 struct inode *new_dir, struct dentry *new_dentry,
520c8b16 4235 struct inode **delegated_inode, unsigned int flags)
1da177e4 4236{
bc27027a
MS
4237 int error;
4238 bool is_dir = d_is_dir(old_dentry);
bc27027a 4239 struct inode *source = old_dentry->d_inode;
9055cba7 4240 struct inode *target = new_dentry->d_inode;
da1ce067
MS
4241 bool new_is_dir = false;
4242 unsigned max_links = new_dir->i_sb->s_max_links;
49d31c2f 4243 struct name_snapshot old_name;
bc27027a 4244
8d3e2936 4245 if (source == target)
bc27027a
MS
4246 return 0;
4247
4248 error = may_delete(old_dir, old_dentry, is_dir);
4249 if (error)
4250 return error;
4251
da1ce067 4252 if (!target) {
bc27027a 4253 error = may_create(new_dir, new_dentry);
da1ce067
MS
4254 } else {
4255 new_is_dir = d_is_dir(new_dentry);
4256
4257 if (!(flags & RENAME_EXCHANGE))
4258 error = may_delete(new_dir, new_dentry, is_dir);
4259 else
4260 error = may_delete(new_dir, new_dentry, new_is_dir);
4261 }
bc27027a
MS
4262 if (error)
4263 return error;
4264
2773bf00 4265 if (!old_dir->i_op->rename)
bc27027a 4266 return -EPERM;
1da177e4
LT
4267
4268 /*
4269 * If we are going to change the parent - check write permissions,
4270 * we'll need to flip '..'.
4271 */
da1ce067
MS
4272 if (new_dir != old_dir) {
4273 if (is_dir) {
4274 error = inode_permission(source, MAY_WRITE);
4275 if (error)
4276 return error;
4277 }
4278 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4279 error = inode_permission(target, MAY_WRITE);
4280 if (error)
4281 return error;
4282 }
1da177e4
LT
4283 }
4284
0b3974eb
MS
4285 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4286 flags);
1da177e4
LT
4287 if (error)
4288 return error;
4289
49d31c2f 4290 take_dentry_name_snapshot(&old_name, old_dentry);
1d2ef590 4291 dget(new_dentry);
da1ce067 4292 if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a
MS
4293 lock_two_nondirectories(source, target);
4294 else if (target)
5955102c 4295 inode_lock(target);
9055cba7
SW
4296
4297 error = -EBUSY;
7af1364f 4298 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
9055cba7
SW
4299 goto out;
4300
da1ce067 4301 if (max_links && new_dir != old_dir) {
bc27027a 4302 error = -EMLINK;
da1ce067 4303 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
bc27027a 4304 goto out;
da1ce067
MS
4305 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4306 old_dir->i_nlink >= max_links)
4307 goto out;
4308 }
da1ce067 4309 if (!is_dir) {
bc27027a 4310 error = try_break_deleg(source, delegated_inode);
8e6d782c
BF
4311 if (error)
4312 goto out;
da1ce067
MS
4313 }
4314 if (target && !new_is_dir) {
4315 error = try_break_deleg(target, delegated_inode);
4316 if (error)
4317 goto out;
8e6d782c 4318 }
2773bf00 4319 error = old_dir->i_op->rename(old_dir, old_dentry,
18fc84da 4320 new_dir, new_dentry, flags);
51892bbb
SW
4321 if (error)
4322 goto out;
4323
da1ce067 4324 if (!(flags & RENAME_EXCHANGE) && target) {
8767712f
AV
4325 if (is_dir) {
4326 shrink_dcache_parent(new_dentry);
bc27027a 4327 target->i_flags |= S_DEAD;
8767712f 4328 }
51892bbb 4329 dont_mount(new_dentry);
8ed936b5 4330 detach_mounts(new_dentry);
bc27027a 4331 }
da1ce067
MS
4332 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4333 if (!(flags & RENAME_EXCHANGE))
4334 d_move(old_dentry, new_dentry);
4335 else
4336 d_exchange(old_dentry, new_dentry);
4337 }
51892bbb 4338out:
da1ce067 4339 if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a
MS
4340 unlock_two_nondirectories(source, target);
4341 else if (target)
5955102c 4342 inode_unlock(target);
1da177e4 4343 dput(new_dentry);
da1ce067 4344 if (!error) {
f4ec3a3d 4345 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
da1ce067
MS
4346 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4347 if (flags & RENAME_EXCHANGE) {
f4ec3a3d 4348 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
da1ce067
MS
4349 new_is_dir, NULL, new_dentry);
4350 }
4351 }
49d31c2f 4352 release_dentry_name_snapshot(&old_name);
0eeca283 4353
1da177e4
LT
4354 return error;
4355}
4d359507 4356EXPORT_SYMBOL(vfs_rename);
1da177e4 4357
ee81feb6
DB
4358static int do_renameat2(int olddfd, const char __user *oldname, int newdfd,
4359 const char __user *newname, unsigned int flags)
1da177e4 4360{
2ad94ae6
AV
4361 struct dentry *old_dentry, *new_dentry;
4362 struct dentry *trap;
f5beed75
AV
4363 struct path old_path, new_path;
4364 struct qstr old_last, new_last;
4365 int old_type, new_type;
8e6d782c 4366 struct inode *delegated_inode = NULL;
91a27b2a
JL
4367 struct filename *from;
4368 struct filename *to;
f5beed75 4369 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
c6a94284 4370 bool should_retry = false;
2ad94ae6 4371 int error;
520c8b16 4372
0d7a8555 4373 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
da1ce067
MS
4374 return -EINVAL;
4375
0d7a8555
MS
4376 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4377 (flags & RENAME_EXCHANGE))
520c8b16
MS
4378 return -EINVAL;
4379
0d7a8555
MS
4380 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4381 return -EPERM;
4382
f5beed75
AV
4383 if (flags & RENAME_EXCHANGE)
4384 target_flags = 0;
4385
c6a94284 4386retry:
c1d4dd27
AV
4387 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4388 &old_path, &old_last, &old_type);
91a27b2a
JL
4389 if (IS_ERR(from)) {
4390 error = PTR_ERR(from);
1da177e4 4391 goto exit;
91a27b2a 4392 }
1da177e4 4393
c1d4dd27
AV
4394 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4395 &new_path, &new_last, &new_type);
91a27b2a
JL
4396 if (IS_ERR(to)) {
4397 error = PTR_ERR(to);
1da177e4 4398 goto exit1;
91a27b2a 4399 }
1da177e4
LT
4400
4401 error = -EXDEV;
f5beed75 4402 if (old_path.mnt != new_path.mnt)
1da177e4
LT
4403 goto exit2;
4404
1da177e4 4405 error = -EBUSY;
f5beed75 4406 if (old_type != LAST_NORM)
1da177e4
LT
4407 goto exit2;
4408
0a7c3937
MS
4409 if (flags & RENAME_NOREPLACE)
4410 error = -EEXIST;
f5beed75 4411 if (new_type != LAST_NORM)
1da177e4
LT
4412 goto exit2;
4413
f5beed75 4414 error = mnt_want_write(old_path.mnt);
c30dabfe
JK
4415 if (error)
4416 goto exit2;
4417
8e6d782c 4418retry_deleg:
f5beed75 4419 trap = lock_rename(new_path.dentry, old_path.dentry);
1da177e4 4420
f5beed75 4421 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
1da177e4
LT
4422 error = PTR_ERR(old_dentry);
4423 if (IS_ERR(old_dentry))
4424 goto exit3;
4425 /* source must exist */
4426 error = -ENOENT;
b18825a7 4427 if (d_is_negative(old_dentry))
1da177e4 4428 goto exit4;
f5beed75 4429 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
0a7c3937
MS
4430 error = PTR_ERR(new_dentry);
4431 if (IS_ERR(new_dentry))
4432 goto exit4;
4433 error = -EEXIST;
4434 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4435 goto exit5;
da1ce067
MS
4436 if (flags & RENAME_EXCHANGE) {
4437 error = -ENOENT;
4438 if (d_is_negative(new_dentry))
4439 goto exit5;
4440
4441 if (!d_is_dir(new_dentry)) {
4442 error = -ENOTDIR;
f5beed75 4443 if (new_last.name[new_last.len])
da1ce067
MS
4444 goto exit5;
4445 }
4446 }
1da177e4 4447 /* unless the source is a directory trailing slashes give -ENOTDIR */
44b1d530 4448 if (!d_is_dir(old_dentry)) {
1da177e4 4449 error = -ENOTDIR;
f5beed75 4450 if (old_last.name[old_last.len])
0a7c3937 4451 goto exit5;
f5beed75 4452 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
0a7c3937 4453 goto exit5;
1da177e4
LT
4454 }
4455 /* source should not be ancestor of target */
4456 error = -EINVAL;
4457 if (old_dentry == trap)
0a7c3937 4458 goto exit5;
1da177e4 4459 /* target should not be an ancestor of source */
da1ce067
MS
4460 if (!(flags & RENAME_EXCHANGE))
4461 error = -ENOTEMPTY;
1da177e4
LT
4462 if (new_dentry == trap)
4463 goto exit5;
4464
f5beed75
AV
4465 error = security_path_rename(&old_path, old_dentry,
4466 &new_path, new_dentry, flags);
be6d3e56 4467 if (error)
c30dabfe 4468 goto exit5;
f5beed75
AV
4469 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4470 new_path.dentry->d_inode, new_dentry,
520c8b16 4471 &delegated_inode, flags);
1da177e4
LT
4472exit5:
4473 dput(new_dentry);
4474exit4:
4475 dput(old_dentry);
4476exit3:
f5beed75 4477 unlock_rename(new_path.dentry, old_path.dentry);
8e6d782c
BF
4478 if (delegated_inode) {
4479 error = break_deleg_wait(&delegated_inode);
4480 if (!error)
4481 goto retry_deleg;
4482 }
f5beed75 4483 mnt_drop_write(old_path.mnt);
1da177e4 4484exit2:
c6a94284
JL
4485 if (retry_estale(error, lookup_flags))
4486 should_retry = true;
f5beed75 4487 path_put(&new_path);
2ad94ae6 4488 putname(to);
1da177e4 4489exit1:
f5beed75 4490 path_put(&old_path);
1da177e4 4491 putname(from);
c6a94284
JL
4492 if (should_retry) {
4493 should_retry = false;
4494 lookup_flags |= LOOKUP_REVAL;
4495 goto retry;
4496 }
2ad94ae6 4497exit:
1da177e4
LT
4498 return error;
4499}
4500
ee81feb6
DB
4501SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4502 int, newdfd, const char __user *, newname, unsigned int, flags)
4503{
4504 return do_renameat2(olddfd, oldname, newdfd, newname, flags);
4505}
4506
520c8b16
MS
4507SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4508 int, newdfd, const char __user *, newname)
4509{
ee81feb6 4510 return do_renameat2(olddfd, oldname, newdfd, newname, 0);
520c8b16
MS
4511}
4512
a26eab24 4513SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d 4514{
ee81feb6 4515 return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
4516}
4517
787fb6bc
MS
4518int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4519{
4520 int error = may_create(dir, dentry);
4521 if (error)
4522 return error;
4523
4524 if (!dir->i_op->mknod)
4525 return -EPERM;
4526
4527 return dir->i_op->mknod(dir, dentry,
4528 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4529}
4530EXPORT_SYMBOL(vfs_whiteout);
4531
5d826c84 4532int readlink_copy(char __user *buffer, int buflen, const char *link)
1da177e4 4533{
5d826c84 4534 int len = PTR_ERR(link);
1da177e4
LT
4535 if (IS_ERR(link))
4536 goto out;
4537
4538 len = strlen(link);
4539 if (len > (unsigned) buflen)
4540 len = buflen;
4541 if (copy_to_user(buffer, link, len))
4542 len = -EFAULT;
4543out:
4544 return len;
4545}
4546
fd4a0edf
MS
4547/**
4548 * vfs_readlink - copy symlink body into userspace buffer
4549 * @dentry: dentry on which to get symbolic link
4550 * @buffer: user memory pointer
4551 * @buflen: size of buffer
4552 *
4553 * Does not touch atime. That's up to the caller if necessary
4554 *
4555 * Does not call security hook.
4556 */
4557int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4558{
4559 struct inode *inode = d_inode(dentry);
f2df5da6
AV
4560 DEFINE_DELAYED_CALL(done);
4561 const char *link;
4562 int res;
fd4a0edf 4563
76fca90e
MS
4564 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4565 if (unlikely(inode->i_op->readlink))
4566 return inode->i_op->readlink(dentry, buffer, buflen);
4567
4568 if (!d_is_symlink(dentry))
4569 return -EINVAL;
4570
4571 spin_lock(&inode->i_lock);
4572 inode->i_opflags |= IOP_DEFAULT_READLINK;
4573 spin_unlock(&inode->i_lock);
4574 }
fd4a0edf 4575
4c4f7c19 4576 link = READ_ONCE(inode->i_link);
f2df5da6
AV
4577 if (!link) {
4578 link = inode->i_op->get_link(dentry, inode, &done);
4579 if (IS_ERR(link))
4580 return PTR_ERR(link);
4581 }
4582 res = readlink_copy(buffer, buflen, link);
4583 do_delayed_call(&done);
4584 return res;
fd4a0edf
MS
4585}
4586EXPORT_SYMBOL(vfs_readlink);
1da177e4 4587
d60874cd
MS
4588/**
4589 * vfs_get_link - get symlink body
4590 * @dentry: dentry on which to get symbolic link
4591 * @done: caller needs to free returned data with this
4592 *
4593 * Calls security hook and i_op->get_link() on the supplied inode.
4594 *
4595 * It does not touch atime. That's up to the caller if necessary.
4596 *
4597 * Does not work on "special" symlinks like /proc/$$/fd/N
4598 */
4599const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4600{
4601 const char *res = ERR_PTR(-EINVAL);
4602 struct inode *inode = d_inode(dentry);
4603
4604 if (d_is_symlink(dentry)) {
4605 res = ERR_PTR(security_inode_readlink(dentry));
4606 if (!res)
4607 res = inode->i_op->get_link(dentry, inode, done);
4608 }
4609 return res;
4610}
4611EXPORT_SYMBOL(vfs_get_link);
4612
1da177e4 4613/* get the link contents into pagecache */
6b255391 4614const char *page_get_link(struct dentry *dentry, struct inode *inode,
fceef393 4615 struct delayed_call *callback)
1da177e4 4616{
ebd09abb
DG
4617 char *kaddr;
4618 struct page *page;
6b255391
AV
4619 struct address_space *mapping = inode->i_mapping;
4620
d3883d4f
AV
4621 if (!dentry) {
4622 page = find_get_page(mapping, 0);
4623 if (!page)
4624 return ERR_PTR(-ECHILD);
4625 if (!PageUptodate(page)) {
4626 put_page(page);
4627 return ERR_PTR(-ECHILD);
4628 }
4629 } else {
4630 page = read_mapping_page(mapping, 0, NULL);
4631 if (IS_ERR(page))
4632 return (char*)page;
4633 }
fceef393 4634 set_delayed_call(callback, page_put_link, page);
21fc61c7
AV
4635 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4636 kaddr = page_address(page);
6b255391 4637 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
ebd09abb 4638 return kaddr;
1da177e4
LT
4639}
4640
6b255391 4641EXPORT_SYMBOL(page_get_link);
1da177e4 4642
fceef393 4643void page_put_link(void *arg)
1da177e4 4644{
fceef393 4645 put_page(arg);
1da177e4 4646}
4d359507 4647EXPORT_SYMBOL(page_put_link);
1da177e4 4648
aa80deab
AV
4649int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4650{
fceef393 4651 DEFINE_DELAYED_CALL(done);
6b255391
AV
4652 int res = readlink_copy(buffer, buflen,
4653 page_get_link(dentry, d_inode(dentry),
fceef393
AV
4654 &done));
4655 do_delayed_call(&done);
aa80deab
AV
4656 return res;
4657}
4658EXPORT_SYMBOL(page_readlink);
4659
54566b2c
NP
4660/*
4661 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4662 */
4663int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4
LT
4664{
4665 struct address_space *mapping = inode->i_mapping;
0adb25d2 4666 struct page *page;
afddba49 4667 void *fsdata;
beb497ab 4668 int err;
c718a975 4669 unsigned int flags = 0;
54566b2c
NP
4670 if (nofs)
4671 flags |= AOP_FLAG_NOFS;
1da177e4 4672
7e53cac4 4673retry:
afddba49 4674 err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c 4675 flags, &page, &fsdata);
1da177e4 4676 if (err)
afddba49
NP
4677 goto fail;
4678
21fc61c7 4679 memcpy(page_address(page), symname, len-1);
afddba49
NP
4680
4681 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4682 page, fsdata);
1da177e4
LT
4683 if (err < 0)
4684 goto fail;
afddba49
NP
4685 if (err < len-1)
4686 goto retry;
4687
1da177e4
LT
4688 mark_inode_dirty(inode);
4689 return 0;
1da177e4
LT
4690fail:
4691 return err;
4692}
4d359507 4693EXPORT_SYMBOL(__page_symlink);
1da177e4 4694
0adb25d2
KK
4695int page_symlink(struct inode *inode, const char *symname, int len)
4696{
4697 return __page_symlink(inode, symname, len,
c62d2555 4698 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
0adb25d2 4699}
4d359507 4700EXPORT_SYMBOL(page_symlink);
0adb25d2 4701
92e1d5be 4702const struct inode_operations page_symlink_inode_operations = {
6b255391 4703 .get_link = page_get_link,
1da177e4 4704};
1da177e4 4705EXPORT_SYMBOL(page_symlink_inode_operations);