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