link_path_walk(): simplify stack handling
[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
fb5f51c7 1272static inline int managed_dentry_rcu(const struct path *path)
62a7375e 1273{
fb5f51c7
IK
1274 return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1275 path->dentry->d_op->d_manage(path, true) : 0;
62a7375e
IK
1276}
1277
9875cf80 1278/*
287548e4
AV
1279 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1280 * we meet a managed dentry that would need blocking.
9875cf80
DH
1281 */
1282static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
254cf582 1283 struct inode **inode, unsigned *seqp)
9875cf80 1284{
62a7375e 1285 for (;;) {
c7105365 1286 struct mount *mounted;
62a7375e
IK
1287 /*
1288 * Don't forget we might have a non-mountpoint managed dentry
1289 * that wants to block transit.
1290 */
fb5f51c7 1291 switch (managed_dentry_rcu(path)) {
b8faf035
N
1292 case -ECHILD:
1293 default:
ab90911f 1294 return false;
b8faf035
N
1295 case -EISDIR:
1296 return true;
1297 case 0:
1298 break;
1299 }
62a7375e
IK
1300
1301 if (!d_mountpoint(path->dentry))
b8faf035 1302 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
62a7375e 1303
474279dc 1304 mounted = __lookup_mnt(path->mnt, path->dentry);
9875cf80
DH
1305 if (!mounted)
1306 break;
72ba2929
AS
1307 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1308 return false;
c7105365
AV
1309 path->mnt = &mounted->mnt;
1310 path->dentry = mounted->mnt.mnt_root;
a3fbbde7 1311 nd->flags |= LOOKUP_JUMPED;
254cf582 1312 *seqp = read_seqcount_begin(&path->dentry->d_seq);
59430262
LT
1313 /*
1314 * Update the inode too. We don't need to re-check the
1315 * dentry sequence number here after this d_inode read,
1316 * because a mount-point is always pinned.
1317 */
1318 *inode = path->dentry->d_inode;
9875cf80 1319 }
f5be3e29 1320 return !read_seqretry(&mount_lock, nd->m_seq) &&
b8faf035 1321 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
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;
9b5858e9
AV
2127 if (IS_ERR(name))
2128 return PTR_ERR(name);
1da177e4
LT
2129 while (*name=='/')
2130 name++;
2131 if (!*name)
9e18f10a 2132 return 0;
1da177e4 2133
1da177e4
LT
2134 /* At this point we know we have a real path component. */
2135 for(;;) {
92d27016 2136 const char *link;
d6bb3e90 2137 u64 hash_len;
fe479a58 2138 int type;
1da177e4 2139
52094c8a 2140 err = may_lookup(nd);
2a18da7a 2141 if (err)
3595e234 2142 return err;
1da177e4 2143
8387ff25 2144 hash_len = hash_name(nd->path.dentry, name);
1da177e4 2145
fe479a58 2146 type = LAST_NORM;
d6bb3e90 2147 if (name[0] == '.') switch (hashlen_len(hash_len)) {
fe479a58 2148 case 2:
200e9ef7 2149 if (name[1] == '.') {
fe479a58 2150 type = LAST_DOTDOT;
16c2cd71
AV
2151 nd->flags |= LOOKUP_JUMPED;
2152 }
fe479a58
AV
2153 break;
2154 case 1:
2155 type = LAST_DOT;
2156 }
5a202bcd
AV
2157 if (likely(type == LAST_NORM)) {
2158 struct dentry *parent = nd->path.dentry;
16c2cd71 2159 nd->flags &= ~LOOKUP_JUMPED;
5a202bcd 2160 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
a060dc50 2161 struct qstr this = { { .hash_len = hash_len }, .name = name };
da53be12 2162 err = parent->d_op->d_hash(parent, &this);
5a202bcd 2163 if (err < 0)
3595e234 2164 return err;
d6bb3e90
LT
2165 hash_len = this.hash_len;
2166 name = this.name;
5a202bcd
AV
2167 }
2168 }
fe479a58 2169
d6bb3e90
LT
2170 nd->last.hash_len = hash_len;
2171 nd->last.name = name;
5f4a6a69
AV
2172 nd->last_type = type;
2173
d6bb3e90
LT
2174 name += hashlen_len(hash_len);
2175 if (!*name)
bdf6cbf1 2176 goto OK;
200e9ef7
LT
2177 /*
2178 * If it wasn't NUL, we know it was '/'. Skip that
2179 * slash, and continue until no more slashes.
2180 */
2181 do {
d6bb3e90
LT
2182 name++;
2183 } while (unlikely(*name == '/'));
8620c238
AV
2184 if (unlikely(!*name)) {
2185OK:
d8d4611a
AV
2186 /* pathname or trailing symlink, done */
2187 if (!depth)
8620c238
AV
2188 return 0;
2189 /* last component of nested symlink */
d8d4611a 2190 name = nd->stack[--depth].name;
8c4efe22 2191 link = walk_component(nd, 0);
1c4ff1a8
AV
2192 } else {
2193 /* not the last component */
8c4efe22 2194 link = walk_component(nd, WALK_MORE);
8620c238 2195 }
92d27016
AV
2196 if (unlikely(link)) {
2197 if (IS_ERR(link))
2198 return PTR_ERR(link);
2199 /* a symlink to follow */
d8d4611a 2200 nd->stack[depth++].name = name;
92d27016
AV
2201 name = link;
2202 continue;
31e6b01f 2203 }
97242f99
AV
2204 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2205 if (nd->flags & LOOKUP_RCU) {
4675ac39 2206 if (unlazy_walk(nd))
97242f99
AV
2207 return -ECHILD;
2208 }
3595e234 2209 return -ENOTDIR;
97242f99 2210 }
1da177e4 2211 }
1da177e4
LT
2212}
2213
edc2b1da 2214/* must be paired with terminate_walk() */
c8a53ee5 2215static const char *path_init(struct nameidata *nd, unsigned flags)
31e6b01f 2216{
740a1678 2217 int error;
c8a53ee5 2218 const char *s = nd->name->name;
31e6b01f 2219
c0eb027e
LT
2220 if (!*s)
2221 flags &= ~LOOKUP_RCU;
edc2b1da
AV
2222 if (flags & LOOKUP_RCU)
2223 rcu_read_lock();
c0eb027e 2224
980f3ea2 2225 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
31e6b01f 2226 nd->depth = 0;
ab87f9a5
AS
2227
2228 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2229 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2230 smp_rmb();
2231
5b6ca027 2232 if (flags & LOOKUP_ROOT) {
b18825a7
DH
2233 struct dentry *root = nd->root.dentry;
2234 struct inode *inode = root->d_inode;
93893862
AV
2235 if (*s && unlikely(!d_can_lookup(root)))
2236 return ERR_PTR(-ENOTDIR);
5b6ca027
AV
2237 nd->path = nd->root;
2238 nd->inode = inode;
2239 if (flags & LOOKUP_RCU) {
ab87f9a5 2240 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
8f47a016 2241 nd->root_seq = nd->seq;
5b6ca027
AV
2242 } else {
2243 path_get(&nd->path);
2244 }
368ee9ba 2245 return s;
5b6ca027
AV
2246 }
2247
31e6b01f 2248 nd->root.mnt = NULL;
248fb5b9
AV
2249 nd->path.mnt = NULL;
2250 nd->path.dentry = NULL;
31e6b01f 2251
8db52c7e
AS
2252 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2253 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
740a1678
AS
2254 error = nd_jump_root(nd);
2255 if (unlikely(error))
2256 return ERR_PTR(error);
2257 return s;
8db52c7e
AS
2258 }
2259
2260 /* Relative pathname -- get the starting-point it is relative to. */
2261 if (nd->dfd == AT_FDCWD) {
e41f7d4e
AV
2262 if (flags & LOOKUP_RCU) {
2263 struct fs_struct *fs = current->fs;
2264 unsigned seq;
31e6b01f 2265
e41f7d4e
AV
2266 do {
2267 seq = read_seqcount_begin(&fs->seq);
2268 nd->path = fs->pwd;
ef55d917 2269 nd->inode = nd->path.dentry->d_inode;
e41f7d4e
AV
2270 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2271 } while (read_seqcount_retry(&fs->seq, seq));
2272 } else {
2273 get_fs_pwd(current->fs, &nd->path);
ef55d917 2274 nd->inode = nd->path.dentry->d_inode;
e41f7d4e 2275 }
31e6b01f 2276 } else {
582aa64a 2277 /* Caller must check execute permissions on the starting path component */
c8a53ee5 2278 struct fd f = fdget_raw(nd->dfd);
31e6b01f
NP
2279 struct dentry *dentry;
2280
2903ff01 2281 if (!f.file)
368ee9ba 2282 return ERR_PTR(-EBADF);
31e6b01f 2283
2903ff01 2284 dentry = f.file->f_path.dentry;
31e6b01f 2285
edc2b1da
AV
2286 if (*s && unlikely(!d_can_lookup(dentry))) {
2287 fdput(f);
2288 return ERR_PTR(-ENOTDIR);
f52e0c11 2289 }
31e6b01f 2290
2903ff01 2291 nd->path = f.file->f_path;
e41f7d4e 2292 if (flags & LOOKUP_RCU) {
34a26b99
AV
2293 nd->inode = nd->path.dentry->d_inode;
2294 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
e41f7d4e 2295 } else {
2903ff01 2296 path_get(&nd->path);
34a26b99 2297 nd->inode = nd->path.dentry->d_inode;
e41f7d4e 2298 }
34a26b99 2299 fdput(f);
31e6b01f 2300 }
8db52c7e 2301
adb21d2b
AS
2302 /* For scoped-lookups we need to set the root to the dirfd as well. */
2303 if (flags & LOOKUP_IS_SCOPED) {
2304 nd->root = nd->path;
2305 if (flags & LOOKUP_RCU) {
2306 nd->root_seq = nd->seq;
2307 } else {
2308 path_get(&nd->root);
2309 nd->flags |= LOOKUP_ROOT_GRABBED;
2310 }
2311 }
2312 return s;
9b4a9b14
AV
2313}
2314
1ccac622 2315static inline const char *lookup_last(struct nameidata *nd)
bd92d7fe 2316{
92d27016 2317 const char *link;
bd92d7fe
AV
2318 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2319 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2320
2321 nd->flags &= ~LOOKUP_PARENT;
8c4efe22 2322 link = walk_component(nd, WALK_TRAILING);
92d27016
AV
2323 if (link) {
2324 nd->flags |= LOOKUP_PARENT;
1ccac622 2325 }
92d27016 2326 return link;
bd92d7fe
AV
2327}
2328
4f757f3c
AV
2329static int handle_lookup_down(struct nameidata *nd)
2330{
c153007b 2331 if (!(nd->flags & LOOKUP_RCU))
db3c9ade 2332 dget(nd->path.dentry);
b0417d2c
AV
2333 return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2334 nd->path.dentry, nd->inode, nd->seq));
4f757f3c
AV
2335}
2336
9b4a9b14 2337/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5 2338static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
9b4a9b14 2339{
c8a53ee5 2340 const char *s = path_init(nd, flags);
bd92d7fe 2341 int err;
31e6b01f 2342
9b5858e9 2343 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
4f757f3c 2344 err = handle_lookup_down(nd);
5f336e72
AV
2345 if (unlikely(err < 0))
2346 s = ERR_PTR(err);
4f757f3c
AV
2347 }
2348
1ccac622
AV
2349 while (!(err = link_path_walk(s, nd)) &&
2350 (s = lookup_last(nd)) != NULL)
2351 ;
9f1fafee
AV
2352 if (!err)
2353 err = complete_walk(nd);
bd92d7fe 2354
deb106c6
AV
2355 if (!err && nd->flags & LOOKUP_DIRECTORY)
2356 if (!d_can_lookup(nd->path.dentry))
bd23a539 2357 err = -ENOTDIR;
161aff1d
AV
2358 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2359 err = handle_lookup_down(nd);
2360 nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please...
2361 }
625b6d10
AV
2362 if (!err) {
2363 *path = nd->path;
2364 nd->path.mnt = NULL;
2365 nd->path.dentry = NULL;
2366 }
2367 terminate_walk(nd);
bd92d7fe 2368 return err;
ee0827cd 2369}
31e6b01f 2370
31d921c7
DH
2371int filename_lookup(int dfd, struct filename *name, unsigned flags,
2372 struct path *path, struct path *root)
ee0827cd 2373{
894bc8c4 2374 int retval;
9883d185 2375 struct nameidata nd;
abc9f5be
AV
2376 if (IS_ERR(name))
2377 return PTR_ERR(name);
9ad1aaa6
AV
2378 if (unlikely(root)) {
2379 nd.root = *root;
2380 flags |= LOOKUP_ROOT;
2381 }
9883d185 2382 set_nameidata(&nd, dfd, name);
c8a53ee5 2383 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
ee0827cd 2384 if (unlikely(retval == -ECHILD))
c8a53ee5 2385 retval = path_lookupat(&nd, flags, path);
ee0827cd 2386 if (unlikely(retval == -ESTALE))
c8a53ee5 2387 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
31e6b01f 2388
f78570dd 2389 if (likely(!retval))
161aff1d
AV
2390 audit_inode(name, path->dentry,
2391 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
9883d185 2392 restore_nameidata();
e4bd1c1a 2393 putname(name);
170aa3d0 2394 return retval;
1da177e4
LT
2395}
2396
8bcb77fa 2397/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5 2398static int path_parentat(struct nameidata *nd, unsigned flags,
391172c4 2399 struct path *parent)
8bcb77fa 2400{
c8a53ee5 2401 const char *s = path_init(nd, flags);
9b5858e9 2402 int err = link_path_walk(s, nd);
8bcb77fa
AV
2403 if (!err)
2404 err = complete_walk(nd);
391172c4
AV
2405 if (!err) {
2406 *parent = nd->path;
2407 nd->path.mnt = NULL;
2408 nd->path.dentry = NULL;
2409 }
2410 terminate_walk(nd);
8bcb77fa
AV
2411 return err;
2412}
2413
5c31b6ce 2414static struct filename *filename_parentat(int dfd, struct filename *name,
391172c4
AV
2415 unsigned int flags, struct path *parent,
2416 struct qstr *last, int *type)
8bcb77fa
AV
2417{
2418 int retval;
9883d185 2419 struct nameidata nd;
8bcb77fa 2420
5c31b6ce
AV
2421 if (IS_ERR(name))
2422 return name;
9883d185 2423 set_nameidata(&nd, dfd, name);
c8a53ee5 2424 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
8bcb77fa 2425 if (unlikely(retval == -ECHILD))
c8a53ee5 2426 retval = path_parentat(&nd, flags, parent);
8bcb77fa 2427 if (unlikely(retval == -ESTALE))
c8a53ee5 2428 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
391172c4
AV
2429 if (likely(!retval)) {
2430 *last = nd.last;
2431 *type = nd.last_type;
c9b07eab 2432 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
5c31b6ce
AV
2433 } else {
2434 putname(name);
2435 name = ERR_PTR(retval);
391172c4 2436 }
9883d185 2437 restore_nameidata();
5c31b6ce 2438 return name;
8bcb77fa
AV
2439}
2440
79714f72
AV
2441/* does lookup, returns the object with parent locked */
2442struct dentry *kern_path_locked(const char *name, struct path *path)
5590ff0d 2443{
5c31b6ce
AV
2444 struct filename *filename;
2445 struct dentry *d;
391172c4
AV
2446 struct qstr last;
2447 int type;
51689104 2448
5c31b6ce
AV
2449 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2450 &last, &type);
51689104
PM
2451 if (IS_ERR(filename))
2452 return ERR_CAST(filename);
5c31b6ce 2453 if (unlikely(type != LAST_NORM)) {
391172c4 2454 path_put(path);
5c31b6ce
AV
2455 putname(filename);
2456 return ERR_PTR(-EINVAL);
79714f72 2457 }
5955102c 2458 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
391172c4 2459 d = __lookup_hash(&last, path->dentry, 0);
79714f72 2460 if (IS_ERR(d)) {
5955102c 2461 inode_unlock(path->dentry->d_inode);
391172c4 2462 path_put(path);
79714f72 2463 }
51689104 2464 putname(filename);
79714f72 2465 return d;
5590ff0d
UD
2466}
2467
d1811465
AV
2468int kern_path(const char *name, unsigned int flags, struct path *path)
2469{
abc9f5be
AV
2470 return filename_lookup(AT_FDCWD, getname_kernel(name),
2471 flags, path, NULL);
d1811465 2472}
4d359507 2473EXPORT_SYMBOL(kern_path);
d1811465 2474
16f18200
JJS
2475/**
2476 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2477 * @dentry: pointer to dentry of the base directory
2478 * @mnt: pointer to vfs mount of the base directory
2479 * @name: pointer to file name
2480 * @flags: lookup flags
e0a01249 2481 * @path: pointer to struct path to fill
16f18200
JJS
2482 */
2483int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2484 const char *name, unsigned int flags,
e0a01249 2485 struct path *path)
16f18200 2486{
9ad1aaa6 2487 struct path root = {.mnt = mnt, .dentry = dentry};
9ad1aaa6 2488 /* the first argument of filename_lookup() is ignored with root */
abc9f5be
AV
2489 return filename_lookup(AT_FDCWD, getname_kernel(name),
2490 flags , path, &root);
16f18200 2491}
4d359507 2492EXPORT_SYMBOL(vfs_path_lookup);
16f18200 2493
3c95f0dc
AV
2494static int lookup_one_len_common(const char *name, struct dentry *base,
2495 int len, struct qstr *this)
057f6c01 2496{
3c95f0dc
AV
2497 this->name = name;
2498 this->len = len;
2499 this->hash = full_name_hash(base, name, len);
6a96ba54 2500 if (!len)
3c95f0dc 2501 return -EACCES;
6a96ba54 2502
21d8a15a
AV
2503 if (unlikely(name[0] == '.')) {
2504 if (len < 2 || (len == 2 && name[1] == '.'))
3c95f0dc 2505 return -EACCES;
21d8a15a
AV
2506 }
2507
6a96ba54 2508 while (len--) {
3c95f0dc 2509 unsigned int c = *(const unsigned char *)name++;
6a96ba54 2510 if (c == '/' || c == '\0')
3c95f0dc 2511 return -EACCES;
6a96ba54 2512 }
5a202bcd
AV
2513 /*
2514 * See if the low-level filesystem might want
2515 * to use its own hash..
2516 */
2517 if (base->d_flags & DCACHE_OP_HASH) {
3c95f0dc 2518 int err = base->d_op->d_hash(base, this);
5a202bcd 2519 if (err < 0)
3c95f0dc 2520 return err;
5a202bcd 2521 }
eead1911 2522
3c95f0dc
AV
2523 return inode_permission(base->d_inode, MAY_EXEC);
2524}
2525
0da0b7fd
DH
2526/**
2527 * try_lookup_one_len - filesystem helper to lookup single pathname component
2528 * @name: pathname component to lookup
2529 * @base: base directory to lookup from
2530 * @len: maximum length @len should be interpreted to
2531 *
2532 * Look up a dentry by name in the dcache, returning NULL if it does not
2533 * currently exist. The function does not try to create a dentry.
2534 *
2535 * Note that this routine is purely a helper for filesystem usage and should
2536 * not be called by generic code.
2537 *
2538 * The caller must hold base->i_mutex.
2539 */
2540struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2541{
2542 struct qstr this;
2543 int err;
2544
2545 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2546
2547 err = lookup_one_len_common(name, base, len, &this);
2548 if (err)
2549 return ERR_PTR(err);
2550
2551 return lookup_dcache(&this, base, 0);
2552}
2553EXPORT_SYMBOL(try_lookup_one_len);
2554
3c95f0dc
AV
2555/**
2556 * lookup_one_len - filesystem helper to lookup single pathname component
2557 * @name: pathname component to lookup
2558 * @base: base directory to lookup from
2559 * @len: maximum length @len should be interpreted to
2560 *
2561 * Note that this routine is purely a helper for filesystem usage and should
2562 * not be called by generic code.
2563 *
2564 * The caller must hold base->i_mutex.
2565 */
2566struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2567{
8613a209 2568 struct dentry *dentry;
3c95f0dc
AV
2569 struct qstr this;
2570 int err;
2571
2572 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2573
2574 err = lookup_one_len_common(name, base, len, &this);
cda309de
MS
2575 if (err)
2576 return ERR_PTR(err);
2577
8613a209
AV
2578 dentry = lookup_dcache(&this, base, 0);
2579 return dentry ? dentry : __lookup_slow(&this, base, 0);
057f6c01 2580}
4d359507 2581EXPORT_SYMBOL(lookup_one_len);
057f6c01 2582
bbddca8e
N
2583/**
2584 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2585 * @name: pathname component to lookup
2586 * @base: base directory to lookup from
2587 * @len: maximum length @len should be interpreted to
2588 *
2589 * Note that this routine is purely a helper for filesystem usage and should
2590 * not be called by generic code.
2591 *
2592 * Unlike lookup_one_len, it should be called without the parent
2593 * i_mutex held, and will take the i_mutex itself if necessary.
2594 */
2595struct dentry *lookup_one_len_unlocked(const char *name,
2596 struct dentry *base, int len)
2597{
2598 struct qstr this;
bbddca8e 2599 int err;
20d00ee8 2600 struct dentry *ret;
bbddca8e 2601
3c95f0dc 2602 err = lookup_one_len_common(name, base, len, &this);
bbddca8e
N
2603 if (err)
2604 return ERR_PTR(err);
2605
20d00ee8
LT
2606 ret = lookup_dcache(&this, base, 0);
2607 if (!ret)
2608 ret = lookup_slow(&this, base, 0);
2609 return ret;
bbddca8e
N
2610}
2611EXPORT_SYMBOL(lookup_one_len_unlocked);
2612
6c2d4798
AV
2613/*
2614 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2615 * on negatives. Returns known positive or ERR_PTR(); that's what
2616 * most of the users want. Note that pinned negative with unlocked parent
2617 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2618 * need to be very careful; pinned positives have ->d_inode stable, so
2619 * this one avoids such problems.
2620 */
2621struct dentry *lookup_positive_unlocked(const char *name,
2622 struct dentry *base, int len)
2623{
2624 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
2fa6b1e0 2625 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
6c2d4798
AV
2626 dput(ret);
2627 ret = ERR_PTR(-ENOENT);
2628 }
2629 return ret;
2630}
2631EXPORT_SYMBOL(lookup_positive_unlocked);
2632
eedf265a
EB
2633#ifdef CONFIG_UNIX98_PTYS
2634int path_pts(struct path *path)
2635{
2636 /* Find something mounted on "pts" in the same directory as
2637 * the input path.
2638 */
2639 struct dentry *child, *parent;
2640 struct qstr this;
2641 int ret;
2642
2643 ret = path_parent_directory(path);
2644 if (ret)
2645 return ret;
2646
2647 parent = path->dentry;
2648 this.name = "pts";
2649 this.len = 3;
2650 child = d_hash_and_lookup(parent, &this);
2651 if (!child)
2652 return -ENOENT;
2653
2654 path->dentry = child;
2655 dput(parent);
2656 follow_mount(path);
2657 return 0;
2658}
2659#endif
2660
1fa1e7f6
AW
2661int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2662 struct path *path, int *empty)
1da177e4 2663{
abc9f5be
AV
2664 return filename_lookup(dfd, getname_flags(name, flags, empty),
2665 flags, path, NULL);
1da177e4 2666}
b853a161 2667EXPORT_SYMBOL(user_path_at_empty);
1fa1e7f6 2668
cbdf35bc 2669int __check_sticky(struct inode *dir, struct inode *inode)
1da177e4 2670{
8e96e3b7 2671 kuid_t fsuid = current_fsuid();
da9592ed 2672
8e96e3b7 2673 if (uid_eq(inode->i_uid, fsuid))
1da177e4 2674 return 0;
8e96e3b7 2675 if (uid_eq(dir->i_uid, fsuid))
1da177e4 2676 return 0;
23adbe12 2677 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
1da177e4 2678}
cbdf35bc 2679EXPORT_SYMBOL(__check_sticky);
1da177e4
LT
2680
2681/*
2682 * Check whether we can remove a link victim from directory dir, check
2683 * whether the type of victim is right.
2684 * 1. We can't do it if dir is read-only (done in permission())
2685 * 2. We should have write and exec permissions on dir
2686 * 3. We can't remove anything from append-only dir
2687 * 4. We can't do anything with immutable dir (done in permission())
2688 * 5. If the sticky bit on dir is set we should either
2689 * a. be owner of dir, or
2690 * b. be owner of victim, or
2691 * c. have CAP_FOWNER capability
2692 * 6. If the victim is append-only or immutable we can't do antyhing with
2693 * links pointing to it.
0bd23d09
EB
2694 * 7. If the victim has an unknown uid or gid we can't change the inode.
2695 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2696 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2697 * 10. We can't remove a root or mountpoint.
2698 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
1da177e4
LT
2699 * nfs_async_unlink().
2700 */
b18825a7 2701static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
1da177e4 2702{
63afdfc7 2703 struct inode *inode = d_backing_inode(victim);
1da177e4
LT
2704 int error;
2705
b18825a7 2706 if (d_is_negative(victim))
1da177e4 2707 return -ENOENT;
b18825a7 2708 BUG_ON(!inode);
1da177e4
LT
2709
2710 BUG_ON(victim->d_parent->d_inode != dir);
593d1ce8
EB
2711
2712 /* Inode writeback is not safe when the uid or gid are invalid. */
2713 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2714 return -EOVERFLOW;
2715
4fa6b5ec 2716 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4 2717
f419a2e3 2718 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2719 if (error)
2720 return error;
2721 if (IS_APPEND(dir))
2722 return -EPERM;
b18825a7
DH
2723
2724 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
0bd23d09 2725 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
1da177e4
LT
2726 return -EPERM;
2727 if (isdir) {
44b1d530 2728 if (!d_is_dir(victim))
1da177e4
LT
2729 return -ENOTDIR;
2730 if (IS_ROOT(victim))
2731 return -EBUSY;
44b1d530 2732 } else if (d_is_dir(victim))
1da177e4
LT
2733 return -EISDIR;
2734 if (IS_DEADDIR(dir))
2735 return -ENOENT;
2736 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2737 return -EBUSY;
2738 return 0;
2739}
2740
2741/* Check whether we can create an object with dentry child in directory
2742 * dir.
2743 * 1. We can't do it if child already exists (open has special treatment for
2744 * this case, but since we are inlined it's OK)
2745 * 2. We can't do it if dir is read-only (done in permission())
036d5236
EB
2746 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2747 * 4. We should have write and exec permissions on dir
2748 * 5. We can't do it if dir is immutable (done in permission())
1da177e4 2749 */
a95164d9 2750static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4 2751{
036d5236 2752 struct user_namespace *s_user_ns;
14e972b4 2753 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
1da177e4
LT
2754 if (child->d_inode)
2755 return -EEXIST;
2756 if (IS_DEADDIR(dir))
2757 return -ENOENT;
036d5236
EB
2758 s_user_ns = dir->i_sb->s_user_ns;
2759 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2760 !kgid_has_mapping(s_user_ns, current_fsgid()))
2761 return -EOVERFLOW;
f419a2e3 2762 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2763}
2764
1da177e4
LT
2765/*
2766 * p1 and p2 should be directories on the same fs.
2767 */
2768struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2769{
2770 struct dentry *p;
2771
2772 if (p1 == p2) {
5955102c 2773 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
1da177e4
LT
2774 return NULL;
2775 }
2776
fc64005c 2777 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
1da177e4 2778
e2761a11
OH
2779 p = d_ancestor(p2, p1);
2780 if (p) {
5955102c
AV
2781 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2782 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
e2761a11 2783 return p;
1da177e4
LT
2784 }
2785
e2761a11
OH
2786 p = d_ancestor(p1, p2);
2787 if (p) {
5955102c
AV
2788 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2789 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
e2761a11 2790 return p;
1da177e4
LT
2791 }
2792
5955102c
AV
2793 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2794 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
1da177e4
LT
2795 return NULL;
2796}
4d359507 2797EXPORT_SYMBOL(lock_rename);
1da177e4
LT
2798
2799void unlock_rename(struct dentry *p1, struct dentry *p2)
2800{
5955102c 2801 inode_unlock(p1->d_inode);
1da177e4 2802 if (p1 != p2) {
5955102c 2803 inode_unlock(p2->d_inode);
fc64005c 2804 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
1da177e4
LT
2805 }
2806}
4d359507 2807EXPORT_SYMBOL(unlock_rename);
1da177e4 2808
4acdaf27 2809int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
312b63fb 2810 bool want_excl)
1da177e4 2811{
a95164d9 2812 int error = may_create(dir, dentry);
1da177e4
LT
2813 if (error)
2814 return error;
2815
acfa4380 2816 if (!dir->i_op->create)
1da177e4
LT
2817 return -EACCES; /* shouldn't it be ENOSYS? */
2818 mode &= S_IALLUGO;
2819 mode |= S_IFREG;
2820 error = security_inode_create(dir, dentry, mode);
2821 if (error)
2822 return error;
312b63fb 2823 error = dir->i_op->create(dir, dentry, mode, want_excl);
a74574aa 2824 if (!error)
f38aa942 2825 fsnotify_create(dir, dentry);
1da177e4
LT
2826 return error;
2827}
4d359507 2828EXPORT_SYMBOL(vfs_create);
1da177e4 2829
8e6c848e
AV
2830int vfs_mkobj(struct dentry *dentry, umode_t mode,
2831 int (*f)(struct dentry *, umode_t, void *),
2832 void *arg)
2833{
2834 struct inode *dir = dentry->d_parent->d_inode;
2835 int error = may_create(dir, dentry);
2836 if (error)
2837 return error;
2838
2839 mode &= S_IALLUGO;
2840 mode |= S_IFREG;
2841 error = security_inode_create(dir, dentry, mode);
2842 if (error)
2843 return error;
2844 error = f(dentry, mode, arg);
2845 if (!error)
2846 fsnotify_create(dir, dentry);
2847 return error;
2848}
2849EXPORT_SYMBOL(vfs_mkobj);
2850
a2982cc9
EB
2851bool may_open_dev(const struct path *path)
2852{
2853 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2854 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2855}
2856
f0bb5aaf 2857static int may_open(const struct path *path, int acc_mode, int flag)
1da177e4 2858{
3fb64190 2859 struct dentry *dentry = path->dentry;
1da177e4
LT
2860 struct inode *inode = dentry->d_inode;
2861 int error;
2862
2863 if (!inode)
2864 return -ENOENT;
2865
c8fe8f30
CH
2866 switch (inode->i_mode & S_IFMT) {
2867 case S_IFLNK:
1da177e4 2868 return -ELOOP;
c8fe8f30
CH
2869 case S_IFDIR:
2870 if (acc_mode & MAY_WRITE)
2871 return -EISDIR;
2872 break;
2873 case S_IFBLK:
2874 case S_IFCHR:
a2982cc9 2875 if (!may_open_dev(path))
1da177e4 2876 return -EACCES;
c8fe8f30
CH
2877 /*FALLTHRU*/
2878 case S_IFIFO:
2879 case S_IFSOCK:
1da177e4 2880 flag &= ~O_TRUNC;
c8fe8f30 2881 break;
4a3fd211 2882 }
b41572e9 2883
62fb4a15 2884 error = inode_permission(inode, MAY_OPEN | acc_mode);
b41572e9
DH
2885 if (error)
2886 return error;
6146f0d5 2887
1da177e4
LT
2888 /*
2889 * An append-only file must be opened in append mode for writing.
2890 */
2891 if (IS_APPEND(inode)) {
8737c930 2892 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521 2893 return -EPERM;
1da177e4 2894 if (flag & O_TRUNC)
7715b521 2895 return -EPERM;
1da177e4
LT
2896 }
2897
2898 /* O_NOATIME can only be set by the owner or superuser */
2e149670 2899 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b521 2900 return -EPERM;
1da177e4 2901
f3c7691e 2902 return 0;
7715b521 2903}
1da177e4 2904
e1181ee6 2905static int handle_truncate(struct file *filp)
7715b521 2906{
f0bb5aaf 2907 const struct path *path = &filp->f_path;
7715b521
AV
2908 struct inode *inode = path->dentry->d_inode;
2909 int error = get_write_access(inode);
2910 if (error)
2911 return error;
2912 /*
2913 * Refuse to truncate files with mandatory locks held on them.
2914 */
d7a06983 2915 error = locks_verify_locked(filp);
7715b521 2916 if (!error)
ea0d3ab2 2917 error = security_path_truncate(path);
7715b521
AV
2918 if (!error) {
2919 error = do_truncate(path->dentry, 0,
2920 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6 2921 filp);
7715b521
AV
2922 }
2923 put_write_access(inode);
acd0c935 2924 return error;
1da177e4
LT
2925}
2926
d57999e1
DH
2927static inline int open_to_namei_flags(int flag)
2928{
8a5e929d
AV
2929 if ((flag & O_ACCMODE) == 3)
2930 flag--;
d57999e1
DH
2931 return flag;
2932}
2933
d3607752 2934static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
d18e9008 2935{
1328c727 2936 struct user_namespace *s_user_ns;
d18e9008
MS
2937 int error = security_path_mknod(dir, dentry, mode, 0);
2938 if (error)
2939 return error;
2940
1328c727
SF
2941 s_user_ns = dir->dentry->d_sb->s_user_ns;
2942 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2943 !kgid_has_mapping(s_user_ns, current_fsgid()))
2944 return -EOVERFLOW;
2945
d18e9008
MS
2946 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2947 if (error)
2948 return error;
2949
2950 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2951}
2952
1acf0af9
DH
2953/*
2954 * Attempt to atomically look up, create and open a file from a negative
2955 * dentry.
2956 *
2957 * Returns 0 if successful. The file will have been created and attached to
2958 * @file by the filesystem calling finish_open().
2959 *
00a07c15
AV
2960 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
2961 * be set. The caller will need to perform the open themselves. @path will
2962 * have been updated to point to the new dentry. This may be negative.
1acf0af9
DH
2963 *
2964 * Returns an error code otherwise.
2965 */
239eb983
AV
2966static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
2967 struct file *file,
2968 const struct open_flags *op,
2969 int open_flag, umode_t mode)
d18e9008 2970{
384f26e2 2971 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
d18e9008 2972 struct inode *dir = nd->path.dentry->d_inode;
d18e9008 2973 int error;
d18e9008 2974
384f26e2 2975 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
d18e9008 2976 open_flag &= ~O_TRUNC;
d18e9008 2977
d18e9008
MS
2978 if (nd->flags & LOOKUP_DIRECTORY)
2979 open_flag |= O_DIRECTORY;
2980
30d90494
AV
2981 file->f_path.dentry = DENTRY_NOT_SET;
2982 file->f_path.mnt = nd->path.mnt;
0fb1ea09 2983 error = dir->i_op->atomic_open(dir, dentry, file,
44907d79 2984 open_to_namei_flags(open_flag), mode);
6fbd0714 2985 d_lookup_done(dentry);
384f26e2 2986 if (!error) {
64e1ac4d
AV
2987 if (file->f_mode & FMODE_OPENED) {
2988 /*
2989 * We didn't have the inode before the open, so check open
2990 * permission here.
2991 */
2992 int acc_mode = op->acc_mode;
2993 if (file->f_mode & FMODE_CREATED) {
2994 WARN_ON(!(open_flag & O_CREAT));
2995 fsnotify_create(dir, dentry);
2996 acc_mode = 0;
2997 }
2998 error = may_open(&file->f_path, acc_mode, open_flag);
2999 if (WARN_ON(error > 0))
3000 error = -EINVAL;
3001 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb 3002 error = -EIO;
03da633a 3003 } else {
384f26e2
AV
3004 if (file->f_path.dentry) {
3005 dput(dentry);
3006 dentry = file->f_path.dentry;
03da633a 3007 }
73a09dd9 3008 if (file->f_mode & FMODE_CREATED)
384f26e2 3009 fsnotify_create(dir, dentry);
239eb983 3010 if (unlikely(d_is_negative(dentry)))
a01e718f 3011 error = -ENOENT;
62b2ce96 3012 }
d18e9008 3013 }
239eb983
AV
3014 if (error) {
3015 dput(dentry);
3016 dentry = ERR_PTR(error);
3017 }
3018 return dentry;
d18e9008
MS
3019}
3020
d58ffd35 3021/*
1acf0af9 3022 * Look up and maybe create and open the last component.
d58ffd35 3023 *
00a07c15 3024 * Must be called with parent locked (exclusive in O_CREAT case).
1acf0af9 3025 *
00a07c15
AV
3026 * Returns 0 on success, that is, if
3027 * the file was successfully atomically created (if necessary) and opened, or
3028 * the file was not completely opened at this time, though lookups and
3029 * creations were performed.
3030 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3031 * In the latter case dentry returned in @path might be negative if O_CREAT
3032 * hadn't been specified.
1acf0af9 3033 *
00a07c15 3034 * An error code is returned on failure.
d58ffd35 3035 */
da5ebf5a
AV
3036static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3037 const struct open_flags *op,
3038 bool got_write)
d58ffd35
MS
3039{
3040 struct dentry *dir = nd->path.dentry;
54ef4872 3041 struct inode *dir_inode = dir->d_inode;
1643b43f 3042 int open_flag = op->open_flag;
d58ffd35 3043 struct dentry *dentry;
1643b43f 3044 int error, create_error = 0;
1643b43f 3045 umode_t mode = op->mode;
6fbd0714 3046 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
d58ffd35 3047
ce8644fc 3048 if (unlikely(IS_DEADDIR(dir_inode)))
da5ebf5a 3049 return ERR_PTR(-ENOENT);
d58ffd35 3050
73a09dd9 3051 file->f_mode &= ~FMODE_CREATED;
6fbd0714
AV
3052 dentry = d_lookup(dir, &nd->last);
3053 for (;;) {
3054 if (!dentry) {
3055 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3056 if (IS_ERR(dentry))
da5ebf5a 3057 return dentry;
6fbd0714
AV
3058 }
3059 if (d_in_lookup(dentry))
3060 break;
d58ffd35 3061
6fbd0714
AV
3062 error = d_revalidate(dentry, nd->flags);
3063 if (likely(error > 0))
3064 break;
3065 if (error)
3066 goto out_dput;
3067 d_invalidate(dentry);
3068 dput(dentry);
3069 dentry = NULL;
3070 }
3071 if (dentry->d_inode) {
6c51e513 3072 /* Cached positive dentry: will open in f_op->open */
da5ebf5a 3073 return dentry;
6c51e513 3074 }
d18e9008 3075
1643b43f
AV
3076 /*
3077 * Checking write permission is tricky, bacuse we don't know if we are
3078 * going to actually need it: O_CREAT opens should work as long as the
3079 * file exists. But checking existence breaks atomicity. The trick is
3080 * to check access and if not granted clear O_CREAT from the flags.
3081 *
3082 * Another problem is returing the "right" error value (e.g. for an
3083 * O_EXCL open we want to return EEXIST not EROFS).
3084 */
3085 if (open_flag & O_CREAT) {
3086 if (!IS_POSIXACL(dir->d_inode))
3087 mode &= ~current_umask();
3088 if (unlikely(!got_write)) {
3089 create_error = -EROFS;
3090 open_flag &= ~O_CREAT;
3091 if (open_flag & (O_EXCL | O_TRUNC))
3092 goto no_open;
3093 /* No side effects, safe to clear O_CREAT */
3094 } else {
3095 create_error = may_o_create(&nd->path, dentry, mode);
3096 if (create_error) {
3097 open_flag &= ~O_CREAT;
3098 if (open_flag & O_EXCL)
3099 goto no_open;
3100 }
3101 }
3102 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3103 unlikely(!got_write)) {
3104 /*
3105 * No O_CREATE -> atomicity not a requirement -> fall
3106 * back to lookup + open
3107 */
3108 goto no_open;
d18e9008
MS
3109 }
3110
6ac08709 3111 if (dir_inode->i_op->atomic_open) {
239eb983 3112 dentry = atomic_open(nd, dentry, file, op, open_flag, mode);
da5ebf5a
AV
3113 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3114 dentry = ERR_PTR(create_error);
3115 return dentry;
d18e9008 3116 }
54ef4872 3117
1643b43f 3118no_open:
6fbd0714 3119 if (d_in_lookup(dentry)) {
12fa5e24
AV
3120 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3121 nd->flags);
6fbd0714 3122 d_lookup_done(dentry);
12fa5e24
AV
3123 if (unlikely(res)) {
3124 if (IS_ERR(res)) {
3125 error = PTR_ERR(res);
3126 goto out_dput;
3127 }
3128 dput(dentry);
3129 dentry = res;
3130 }
54ef4872
MS
3131 }
3132
d58ffd35 3133 /* Negative dentry, just create the file */
1643b43f 3134 if (!dentry->d_inode && (open_flag & O_CREAT)) {
73a09dd9 3135 file->f_mode |= FMODE_CREATED;
ce8644fc 3136 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
ce8644fc
AV
3137 if (!dir_inode->i_op->create) {
3138 error = -EACCES;
d58ffd35 3139 goto out_dput;
ce8644fc
AV
3140 }
3141 error = dir_inode->i_op->create(dir_inode, dentry, mode,
1643b43f 3142 open_flag & O_EXCL);
d58ffd35
MS
3143 if (error)
3144 goto out_dput;
ce8644fc 3145 fsnotify_create(dir_inode, dentry);
d58ffd35 3146 }
1643b43f
AV
3147 if (unlikely(create_error) && !dentry->d_inode) {
3148 error = create_error;
3149 goto out_dput;
d58ffd35 3150 }
da5ebf5a 3151 return dentry;
d58ffd35
MS
3152
3153out_dput:
3154 dput(dentry);
da5ebf5a 3155 return ERR_PTR(error);
d58ffd35
MS
3156}
3157
31e6b01f 3158/*
fe2d35ff 3159 * Handle the last step of open()
31e6b01f 3160 */
1ccac622 3161static const char *do_last(struct nameidata *nd,
3ec2eef1 3162 struct file *file, const struct open_flags *op)
fb1cc555 3163{
a1e28038 3164 struct dentry *dir = nd->path.dentry;
6404674a
AV
3165 kuid_t dir_uid = nd->inode->i_uid;
3166 umode_t dir_mode = nd->inode->i_mode;
ca344a89 3167 int open_flag = op->open_flag;
77d660a8 3168 bool will_truncate = (open_flag & O_TRUNC) != 0;
64894cf8 3169 bool got_write = false;
bcda7652 3170 int acc_mode = op->acc_mode;
254cf582 3171 unsigned seq;
a1eb3315 3172 struct inode *inode;
da5ebf5a 3173 struct dentry *dentry;
b0417d2c 3174 const char *res;
16c2cd71 3175 int error;
1f36f774 3176
c3e380b0
AV
3177 nd->flags &= ~LOOKUP_PARENT;
3178 nd->flags |= op->intent;
3179
bc77daa7 3180 if (nd->last_type != LAST_NORM) {
56676ec3
AV
3181 if (nd->depth)
3182 put_link(nd);
fe2d35ff 3183 error = handle_dots(nd, nd->last_type);
deb106c6 3184 if (unlikely(error))
1ccac622 3185 return ERR_PTR(error);
e83db167 3186 goto finish_open;
1f36f774 3187 }
67ee3ad2 3188
ca344a89 3189 if (!(open_flag & O_CREAT)) {
fe2d35ff
AV
3190 if (nd->last.name[nd->last.len])
3191 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3192 /* we _can_ be in RCU mode here */
20e34357
AV
3193 dentry = lookup_fast(nd, &inode, &seq);
3194 if (IS_ERR(dentry))
1ccac622 3195 return ERR_CAST(dentry);
20e34357 3196 if (likely(dentry))
71574865
MS
3197 goto finish_lookup;
3198
71574865 3199 BUG_ON(nd->inode != dir->d_inode);
6583fe22 3200 BUG_ON(nd->flags & LOOKUP_RCU);
b6183df7
MS
3201 } else {
3202 /* create side of things */
3203 /*
3204 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3205 * has been cleared when we got to the last component we are
3206 * about to look up
3207 */
3208 error = complete_walk(nd);
e8bb73df 3209 if (error)
1ccac622 3210 return ERR_PTR(error);
fe2d35ff 3211
c9b07eab 3212 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
b6183df7 3213 /* trailing slashes? */
deb106c6 3214 if (unlikely(nd->last.name[nd->last.len]))
1ccac622 3215 return ERR_PTR(-EISDIR);
b6183df7 3216 }
a2c36b45 3217
9cf843e3 3218 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
64894cf8
AV
3219 error = mnt_want_write(nd->path.mnt);
3220 if (!error)
3221 got_write = true;
3222 /*
3223 * do _not_ fail yet - we might not need that or fail with
3224 * a different error; let lookup_open() decide; we'll be
3225 * dropping this one anyway.
3226 */
3227 }
9cf843e3
AV
3228 if (open_flag & O_CREAT)
3229 inode_lock(dir->d_inode);
3230 else
3231 inode_lock_shared(dir->d_inode);
da5ebf5a 3232 dentry = lookup_open(nd, file, op, got_write);
9cf843e3
AV
3233 if (open_flag & O_CREAT)
3234 inode_unlock(dir->d_inode);
3235 else
3236 inode_unlock_shared(dir->d_inode);
a1e28038 3237
da5ebf5a
AV
3238 if (IS_ERR(dentry)) {
3239 error = PTR_ERR(dentry);
00a07c15 3240 goto out;
da5ebf5a 3241 }
d18e9008 3242
00a07c15 3243 if (file->f_mode & FMODE_OPENED) {
73a09dd9 3244 if ((file->f_mode & FMODE_CREATED) ||
496ad9aa 3245 !S_ISREG(file_inode(file)->i_mode))
77d660a8 3246 will_truncate = false;
d18e9008 3247
76ae2a5a 3248 audit_inode(nd->name, file->f_path.dentry, 0);
da5ebf5a 3249 dput(dentry);
d18e9008
MS
3250 goto opened;
3251 }
fb1cc555 3252
73a09dd9 3253 if (file->f_mode & FMODE_CREATED) {
9b44f1b3 3254 /* Don't check for write permission, don't truncate */
ca344a89 3255 open_flag &= ~O_TRUNC;
77d660a8 3256 will_truncate = false;
62fb4a15 3257 acc_mode = 0;
e73cabff
AV
3258 dput(nd->path.dentry);
3259 nd->path.dentry = dentry;
e83db167 3260 goto finish_open_created;
fb1cc555
AV
3261 }
3262
d18e9008
MS
3263 /*
3264 * If atomic_open() acquired write access it is dropped now due to
3265 * possible mount and symlink following (this might be optimized away if
3266 * necessary...)
3267 */
64894cf8 3268 if (got_write) {
d18e9008 3269 mnt_drop_write(nd->path.mnt);
64894cf8 3270 got_write = false;
d18e9008
MS
3271 }
3272
20e34357 3273finish_lookup:
56676ec3
AV
3274 if (nd->depth)
3275 put_link(nd);
8c4efe22 3276 res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
b0417d2c
AV
3277 if (unlikely(res)) {
3278 nd->flags |= LOOKUP_PARENT;
3279 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
b0417d2c 3280 return res;
1ccac622 3281 }
31d1726d
AV
3282
3283 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3284 audit_inode(nd->name, nd->path.dentry, 0);
1ccac622 3285 return ERR_PTR(-EEXIST);
31d1726d 3286 }
bc77daa7 3287finish_open:
8f64fb1c 3288 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
a3fbbde7 3289 error = complete_walk(nd);
fac7d191 3290 if (error)
1ccac622 3291 return ERR_PTR(error);
76ae2a5a 3292 audit_inode(nd->name, nd->path.dentry, 0);
30aba665
SM
3293 if (open_flag & O_CREAT) {
3294 error = -EISDIR;
3295 if (d_is_dir(nd->path.dentry))
3296 goto out;
d0cb5018 3297 error = may_create_in_sticky(dir_mode, dir_uid,
30aba665
SM
3298 d_backing_inode(nd->path.dentry));
3299 if (unlikely(error))
3300 goto out;
3301 }
af2f5542 3302 error = -ENOTDIR;
44b1d530 3303 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
2675a4eb 3304 goto out;
4bbcbd3b 3305 if (!d_is_reg(nd->path.dentry))
77d660a8 3306 will_truncate = false;
6c0d46c4 3307
0f9d1a10
AV
3308 if (will_truncate) {
3309 error = mnt_want_write(nd->path.mnt);
3310 if (error)
2675a4eb 3311 goto out;
64894cf8 3312 got_write = true;
0f9d1a10 3313 }
e83db167 3314finish_open_created:
6ac08709
AV
3315 error = may_open(&nd->path, acc_mode, open_flag);
3316 if (error)
3317 goto out;
aad888f8 3318 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
ae2bb293 3319 error = vfs_open(&nd->path, file);
fac7d191 3320 if (error)
015c3bbc 3321 goto out;
a8277b9b 3322opened:
6035a27b 3323 error = ima_file_check(file, op->acc_mode);
fe9ec829 3324 if (!error && will_truncate)
2675a4eb 3325 error = handle_truncate(file);
ca344a89 3326out:
c80567c8
AV
3327 if (unlikely(error > 0)) {
3328 WARN_ON(1);
3329 error = -EINVAL;
3330 }
64894cf8 3331 if (got_write)
0f9d1a10 3332 mnt_drop_write(nd->path.mnt);
1ccac622 3333 return ERR_PTR(error);
fb1cc555
AV
3334}
3335
af7bd4dc
AG
3336struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3337{
af7bd4dc
AG
3338 struct dentry *child = NULL;
3339 struct inode *dir = dentry->d_inode;
3340 struct inode *inode;
3341 int error;
3342
3343 /* we want directory to be writable */
3344 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3345 if (error)
3346 goto out_err;
3347 error = -EOPNOTSUPP;
3348 if (!dir->i_op->tmpfile)
3349 goto out_err;
3350 error = -ENOMEM;
cdf01226 3351 child = d_alloc(dentry, &slash_name);
af7bd4dc
AG
3352 if (unlikely(!child))
3353 goto out_err;
3354 error = dir->i_op->tmpfile(dir, child, mode);
3355 if (error)
3356 goto out_err;
3357 error = -ENOENT;
3358 inode = child->d_inode;
3359 if (unlikely(!inode))
3360 goto out_err;
3361 if (!(open_flag & O_EXCL)) {
3362 spin_lock(&inode->i_lock);
3363 inode->i_state |= I_LINKABLE;
3364 spin_unlock(&inode->i_lock);
3365 }
fdb2410f 3366 ima_post_create_tmpfile(inode);
af7bd4dc
AG
3367 return child;
3368
3369out_err:
3370 dput(child);
3371 return ERR_PTR(error);
3372}
3373EXPORT_SYMBOL(vfs_tmpfile);
3374
c8a53ee5 3375static int do_tmpfile(struct nameidata *nd, unsigned flags,
60545d0d 3376 const struct open_flags *op,
3ec2eef1 3377 struct file *file)
60545d0d 3378{
625b6d10 3379 struct dentry *child;
625b6d10 3380 struct path path;
c8a53ee5 3381 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
60545d0d
AV
3382 if (unlikely(error))
3383 return error;
625b6d10 3384 error = mnt_want_write(path.mnt);
60545d0d
AV
3385 if (unlikely(error))
3386 goto out;
af7bd4dc
AG
3387 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3388 error = PTR_ERR(child);
684e73be 3389 if (IS_ERR(child))
60545d0d 3390 goto out2;
625b6d10
AV
3391 dput(path.dentry);
3392 path.dentry = child;
c8a53ee5 3393 audit_inode(nd->name, child, 0);
69a91c23 3394 /* Don't check for other permissions, the inode was just created */
62fb4a15 3395 error = may_open(&path, 0, op->open_flag);
60545d0d
AV
3396 if (error)
3397 goto out2;
625b6d10 3398 file->f_path.mnt = path.mnt;
be12af3e 3399 error = finish_open(file, child, NULL);
60545d0d 3400out2:
625b6d10 3401 mnt_drop_write(path.mnt);
60545d0d 3402out:
625b6d10 3403 path_put(&path);
60545d0d
AV
3404 return error;
3405}
3406
6ac08709
AV
3407static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3408{
3409 struct path path;
3410 int error = path_lookupat(nd, flags, &path);
3411 if (!error) {
3412 audit_inode(nd->name, path.dentry, 0);
ae2bb293 3413 error = vfs_open(&path, file);
6ac08709
AV
3414 path_put(&path);
3415 }
3416 return error;
3417}
3418
c8a53ee5
AV
3419static struct file *path_openat(struct nameidata *nd,
3420 const struct open_flags *op, unsigned flags)
1da177e4 3421{
30d90494 3422 struct file *file;
13aab428 3423 int error;
31e6b01f 3424
ea73ea72 3425 file = alloc_empty_file(op->open_flag, current_cred());
1afc99be
AV
3426 if (IS_ERR(file))
3427 return file;
31e6b01f 3428
bb458c64 3429 if (unlikely(file->f_flags & __O_TMPFILE)) {
3ec2eef1 3430 error = do_tmpfile(nd, flags, op, file);
5f336e72 3431 } else if (unlikely(file->f_flags & O_PATH)) {
6ac08709 3432 error = do_o_path(nd, flags, file);
5f336e72
AV
3433 } else {
3434 const char *s = path_init(nd, flags);
3435 while (!(error = link_path_walk(s, nd)) &&
1ccac622
AV
3436 (s = do_last(nd, file, op)) != NULL)
3437 ;
5f336e72 3438 terminate_walk(nd);
806b681c 3439 }
7c1c01ec 3440 if (likely(!error)) {
aad888f8 3441 if (likely(file->f_mode & FMODE_OPENED))
7c1c01ec
AV
3442 return file;
3443 WARN_ON(1);
3444 error = -EINVAL;
16b1c1cd 3445 }
7c1c01ec
AV
3446 fput(file);
3447 if (error == -EOPENSTALE) {
3448 if (flags & LOOKUP_RCU)
3449 error = -ECHILD;
3450 else
3451 error = -ESTALE;
2675a4eb 3452 }
7c1c01ec 3453 return ERR_PTR(error);
1da177e4
LT
3454}
3455
669abf4e 3456struct file *do_filp_open(int dfd, struct filename *pathname,
f9652e10 3457 const struct open_flags *op)
13aab428 3458{
9883d185 3459 struct nameidata nd;
f9652e10 3460 int flags = op->lookup_flags;
13aab428
AV
3461 struct file *filp;
3462
9883d185 3463 set_nameidata(&nd, dfd, pathname);
c8a53ee5 3464 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
13aab428 3465 if (unlikely(filp == ERR_PTR(-ECHILD)))
c8a53ee5 3466 filp = path_openat(&nd, op, flags);
13aab428 3467 if (unlikely(filp == ERR_PTR(-ESTALE)))
c8a53ee5 3468 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d185 3469 restore_nameidata();
13aab428
AV
3470 return filp;
3471}
3472
73d049a4 3473struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
f9652e10 3474 const char *name, const struct open_flags *op)
73d049a4 3475{
9883d185 3476 struct nameidata nd;
73d049a4 3477 struct file *file;
51689104 3478 struct filename *filename;
f9652e10 3479 int flags = op->lookup_flags | LOOKUP_ROOT;
73d049a4
AV
3480
3481 nd.root.mnt = mnt;
3482 nd.root.dentry = dentry;
3483
b18825a7 3484 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
73d049a4
AV
3485 return ERR_PTR(-ELOOP);
3486
51689104 3487 filename = getname_kernel(name);
a1c83681 3488 if (IS_ERR(filename))
51689104
PM
3489 return ERR_CAST(filename);
3490
9883d185 3491 set_nameidata(&nd, -1, filename);
c8a53ee5 3492 file = path_openat(&nd, op, flags | LOOKUP_RCU);
73d049a4 3493 if (unlikely(file == ERR_PTR(-ECHILD)))
c8a53ee5 3494 file = path_openat(&nd, op, flags);
73d049a4 3495 if (unlikely(file == ERR_PTR(-ESTALE)))
c8a53ee5 3496 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d185 3497 restore_nameidata();
51689104 3498 putname(filename);
73d049a4
AV
3499 return file;
3500}
3501
fa14a0b8 3502static struct dentry *filename_create(int dfd, struct filename *name,
1ac12b4b 3503 struct path *path, unsigned int lookup_flags)
1da177e4 3504{
c663e5d8 3505 struct dentry *dentry = ERR_PTR(-EEXIST);
391172c4
AV
3506 struct qstr last;
3507 int type;
c30dabfe 3508 int err2;
1ac12b4b
JL
3509 int error;
3510 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3511
3512 /*
3513 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3514 * other flags passed in are ignored!
3515 */
3516 lookup_flags &= LOOKUP_REVAL;
3517
5c31b6ce
AV
3518 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3519 if (IS_ERR(name))
3520 return ERR_CAST(name);
1da177e4 3521
c663e5d8
CH
3522 /*
3523 * Yucky last component or no last component at all?
3524 * (foo/., foo/.., /////)
3525 */
5c31b6ce 3526 if (unlikely(type != LAST_NORM))
ed75e95d 3527 goto out;
c663e5d8 3528
c30dabfe 3529 /* don't fail immediately if it's r/o, at least try to report other errors */
391172c4 3530 err2 = mnt_want_write(path->mnt);
c663e5d8
CH
3531 /*
3532 * Do the final lookup.
3533 */
391172c4 3534 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
5955102c 3535 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
391172c4 3536 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
1da177e4 3537 if (IS_ERR(dentry))
a8104a9f 3538 goto unlock;
c663e5d8 3539
a8104a9f 3540 error = -EEXIST;
b18825a7 3541 if (d_is_positive(dentry))
a8104a9f 3542 goto fail;
b18825a7 3543
c663e5d8
CH
3544 /*
3545 * Special case - lookup gave negative, but... we had foo/bar/
3546 * From the vfs_mknod() POV we just have a negative dentry -
3547 * all is fine. Let's be bastards - you had / on the end, you've
3548 * been asking for (non-existent) directory. -ENOENT for you.
3549 */
391172c4 3550 if (unlikely(!is_dir && last.name[last.len])) {
a8104a9f 3551 error = -ENOENT;
ed75e95d 3552 goto fail;
e9baf6e5 3553 }
c30dabfe
JK
3554 if (unlikely(err2)) {
3555 error = err2;
a8104a9f 3556 goto fail;
c30dabfe 3557 }
181c37b6 3558 putname(name);
1da177e4 3559 return dentry;
1da177e4 3560fail:
a8104a9f
AV
3561 dput(dentry);
3562 dentry = ERR_PTR(error);
3563unlock:
5955102c 3564 inode_unlock(path->dentry->d_inode);
c30dabfe 3565 if (!err2)
391172c4 3566 mnt_drop_write(path->mnt);
ed75e95d 3567out:
391172c4 3568 path_put(path);
181c37b6 3569 putname(name);
1da177e4
LT
3570 return dentry;
3571}
fa14a0b8
AV
3572
3573struct dentry *kern_path_create(int dfd, const char *pathname,
3574 struct path *path, unsigned int lookup_flags)
3575{
181c37b6
AV
3576 return filename_create(dfd, getname_kernel(pathname),
3577 path, lookup_flags);
fa14a0b8 3578}
dae6ad8f
AV
3579EXPORT_SYMBOL(kern_path_create);
3580
921a1650
AV
3581void done_path_create(struct path *path, struct dentry *dentry)
3582{
3583 dput(dentry);
5955102c 3584 inode_unlock(path->dentry->d_inode);
a8104a9f 3585 mnt_drop_write(path->mnt);
921a1650
AV
3586 path_put(path);
3587}
3588EXPORT_SYMBOL(done_path_create);
3589
520ae687 3590inline struct dentry *user_path_create(int dfd, const char __user *pathname,
1ac12b4b 3591 struct path *path, unsigned int lookup_flags)
dae6ad8f 3592{
181c37b6 3593 return filename_create(dfd, getname(pathname), path, lookup_flags);
dae6ad8f
AV
3594}
3595EXPORT_SYMBOL(user_path_create);
3596
1a67aafb 3597int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4 3598{
a95164d9 3599 int error = may_create(dir, dentry);
1da177e4
LT
3600
3601 if (error)
3602 return error;
3603
94f82008 3604 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1da177e4
LT
3605 return -EPERM;
3606
acfa4380 3607 if (!dir->i_op->mknod)
1da177e4
LT
3608 return -EPERM;
3609
08ce5f16
SH
3610 error = devcgroup_inode_mknod(mode, dev);
3611 if (error)
3612 return error;
3613
1da177e4
LT
3614 error = security_inode_mknod(dir, dentry, mode, dev);
3615 if (error)
3616 return error;
3617
1da177e4 3618 error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aa 3619 if (!error)
f38aa942 3620 fsnotify_create(dir, dentry);
1da177e4
LT
3621 return error;
3622}
4d359507 3623EXPORT_SYMBOL(vfs_mknod);
1da177e4 3624
f69aac00 3625static int may_mknod(umode_t mode)
463c3197
DH
3626{
3627 switch (mode & S_IFMT) {
3628 case S_IFREG:
3629 case S_IFCHR:
3630 case S_IFBLK:
3631 case S_IFIFO:
3632 case S_IFSOCK:
3633 case 0: /* zero mode translates to S_IFREG */
3634 return 0;
3635 case S_IFDIR:
3636 return -EPERM;
3637 default:
3638 return -EINVAL;
3639 }
3640}
3641
87c4e192
DB
3642long do_mknodat(int dfd, const char __user *filename, umode_t mode,
3643 unsigned int dev)
1da177e4 3644{
2ad94ae6 3645 struct dentry *dentry;
dae6ad8f
AV
3646 struct path path;
3647 int error;
972567f1 3648 unsigned int lookup_flags = 0;
1da177e4 3649
8e4bfca1
AV
3650 error = may_mknod(mode);
3651 if (error)
3652 return error;
972567f1
JL
3653retry:
3654 dentry = user_path_create(dfd, filename, &path, lookup_flags);
dae6ad8f
AV
3655 if (IS_ERR(dentry))
3656 return PTR_ERR(dentry);
2ad94ae6 3657
dae6ad8f 3658 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3659 mode &= ~current_umask();
dae6ad8f 3660 error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56 3661 if (error)
a8104a9f 3662 goto out;
463c3197 3663 switch (mode & S_IFMT) {
1da177e4 3664 case 0: case S_IFREG:
312b63fb 3665 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
05d1a717
MZ
3666 if (!error)
3667 ima_post_path_mknod(dentry);
1da177e4
LT
3668 break;
3669 case S_IFCHR: case S_IFBLK:
dae6ad8f 3670 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4
LT
3671 new_decode_dev(dev));
3672 break;
3673 case S_IFIFO: case S_IFSOCK:
dae6ad8f 3674 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4 3675 break;
1da177e4 3676 }
a8104a9f 3677out:
921a1650 3678 done_path_create(&path, dentry);
972567f1
JL
3679 if (retry_estale(error, lookup_flags)) {
3680 lookup_flags |= LOOKUP_REVAL;
3681 goto retry;
3682 }
1da177e4
LT
3683 return error;
3684}
3685
87c4e192
DB
3686SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3687 unsigned int, dev)
3688{
3689 return do_mknodat(dfd, filename, mode, dev);
3690}
3691
8208a22b 3692SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d 3693{
87c4e192 3694 return do_mknodat(AT_FDCWD, filename, mode, dev);
5590ff0d
UD
3695}
3696
18bb1db3 3697int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4 3698{
a95164d9 3699 int error = may_create(dir, dentry);
8de52778 3700 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
3701
3702 if (error)
3703 return error;
3704
acfa4380 3705 if (!dir->i_op->mkdir)
1da177e4
LT
3706 return -EPERM;
3707
3708 mode &= (S_IRWXUGO|S_ISVTX);
3709 error = security_inode_mkdir(dir, dentry, mode);
3710 if (error)
3711 return error;
3712
8de52778
AV
3713 if (max_links && dir->i_nlink >= max_links)
3714 return -EMLINK;
3715
1da177e4 3716 error = dir->i_op->mkdir(dir, dentry, mode);
a74574aa 3717 if (!error)
f38aa942 3718 fsnotify_mkdir(dir, dentry);
1da177e4
LT
3719 return error;
3720}
4d359507 3721EXPORT_SYMBOL(vfs_mkdir);
1da177e4 3722
0101db7a 3723long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
1da177e4 3724{
6902d925 3725 struct dentry *dentry;
dae6ad8f
AV
3726 struct path path;
3727 int error;
b76d8b82 3728 unsigned int lookup_flags = LOOKUP_DIRECTORY;
1da177e4 3729
b76d8b82
JL
3730retry:
3731 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
6902d925 3732 if (IS_ERR(dentry))
dae6ad8f 3733 return PTR_ERR(dentry);
1da177e4 3734
dae6ad8f 3735 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3736 mode &= ~current_umask();
dae6ad8f 3737 error = security_path_mkdir(&path, dentry, mode);
a8104a9f
AV
3738 if (!error)
3739 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
921a1650 3740 done_path_create(&path, dentry);
b76d8b82
JL
3741 if (retry_estale(error, lookup_flags)) {
3742 lookup_flags |= LOOKUP_REVAL;
3743 goto retry;
3744 }
1da177e4
LT
3745 return error;
3746}
3747
0101db7a
DB
3748SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3749{
3750 return do_mkdirat(dfd, pathname, mode);
3751}
3752
a218d0fd 3753SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d 3754{
0101db7a 3755 return do_mkdirat(AT_FDCWD, pathname, mode);
5590ff0d
UD
3756}
3757
1da177e4
LT
3758int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3759{
3760 int error = may_delete(dir, dentry, 1);
3761
3762 if (error)
3763 return error;
3764
acfa4380 3765 if (!dir->i_op->rmdir)
1da177e4
LT
3766 return -EPERM;
3767
1d2ef590 3768 dget(dentry);
5955102c 3769 inode_lock(dentry->d_inode);
912dbc15
SW
3770
3771 error = -EBUSY;
7af1364f 3772 if (is_local_mountpoint(dentry))
912dbc15
SW
3773 goto out;
3774
3775 error = security_inode_rmdir(dir, dentry);
3776 if (error)
3777 goto out;
3778
3779 error = dir->i_op->rmdir(dir, dentry);
3780 if (error)
3781 goto out;
3782
8767712f 3783 shrink_dcache_parent(dentry);
912dbc15
SW
3784 dentry->d_inode->i_flags |= S_DEAD;
3785 dont_mount(dentry);
8ed936b5 3786 detach_mounts(dentry);
116b9731 3787 fsnotify_rmdir(dir, dentry);
912dbc15
SW
3788
3789out:
5955102c 3790 inode_unlock(dentry->d_inode);
1d2ef590 3791 dput(dentry);
912dbc15 3792 if (!error)
1da177e4 3793 d_delete(dentry);
1da177e4
LT
3794 return error;
3795}
4d359507 3796EXPORT_SYMBOL(vfs_rmdir);
1da177e4 3797
f459dffa 3798long do_rmdir(int dfd, const char __user *pathname)
1da177e4
LT
3799{
3800 int error = 0;
91a27b2a 3801 struct filename *name;
1da177e4 3802 struct dentry *dentry;
f5beed75
AV
3803 struct path path;
3804 struct qstr last;
3805 int type;
c6ee9206
JL
3806 unsigned int lookup_flags = 0;
3807retry:
c1d4dd27
AV
3808 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3809 &path, &last, &type);
91a27b2a
JL
3810 if (IS_ERR(name))
3811 return PTR_ERR(name);
1da177e4 3812
f5beed75 3813 switch (type) {
0612d9fb
OH
3814 case LAST_DOTDOT:
3815 error = -ENOTEMPTY;
3816 goto exit1;
3817 case LAST_DOT:
3818 error = -EINVAL;
3819 goto exit1;
3820 case LAST_ROOT:
3821 error = -EBUSY;
3822 goto exit1;
1da177e4 3823 }
0612d9fb 3824
f5beed75 3825 error = mnt_want_write(path.mnt);
c30dabfe
JK
3826 if (error)
3827 goto exit1;
0612d9fb 3828
5955102c 3829 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
f5beed75 3830 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
1da177e4 3831 error = PTR_ERR(dentry);
6902d925
DH
3832 if (IS_ERR(dentry))
3833 goto exit2;
e6bc45d6
TT
3834 if (!dentry->d_inode) {
3835 error = -ENOENT;
3836 goto exit3;
3837 }
f5beed75 3838 error = security_path_rmdir(&path, dentry);
be6d3e56 3839 if (error)
c30dabfe 3840 goto exit3;
f5beed75 3841 error = vfs_rmdir(path.dentry->d_inode, dentry);
0622753b 3842exit3:
6902d925
DH
3843 dput(dentry);
3844exit2:
5955102c 3845 inode_unlock(path.dentry->d_inode);
f5beed75 3846 mnt_drop_write(path.mnt);
1da177e4 3847exit1:
f5beed75 3848 path_put(&path);
1da177e4 3849 putname(name);
c6ee9206
JL
3850 if (retry_estale(error, lookup_flags)) {
3851 lookup_flags |= LOOKUP_REVAL;
3852 goto retry;
3853 }
1da177e4
LT
3854 return error;
3855}
3856
3cdad428 3857SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d
UD
3858{
3859 return do_rmdir(AT_FDCWD, pathname);
3860}
3861
b21996e3
BF
3862/**
3863 * vfs_unlink - unlink a filesystem object
3864 * @dir: parent directory
3865 * @dentry: victim
3866 * @delegated_inode: returns victim inode, if the inode is delegated.
3867 *
3868 * The caller must hold dir->i_mutex.
3869 *
3870 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3871 * return a reference to the inode in delegated_inode. The caller
3872 * should then break the delegation on that inode and retry. Because
3873 * breaking a delegation may take a long time, the caller should drop
3874 * dir->i_mutex before doing so.
3875 *
3876 * Alternatively, a caller may pass NULL for delegated_inode. This may
3877 * be appropriate for callers that expect the underlying filesystem not
3878 * to be NFS exported.
3879 */
3880int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
1da177e4 3881{
9accbb97 3882 struct inode *target = dentry->d_inode;
1da177e4
LT
3883 int error = may_delete(dir, dentry, 0);
3884
3885 if (error)
3886 return error;
3887
acfa4380 3888 if (!dir->i_op->unlink)
1da177e4
LT
3889 return -EPERM;
3890
5955102c 3891 inode_lock(target);
8ed936b5 3892 if (is_local_mountpoint(dentry))
1da177e4
LT
3893 error = -EBUSY;
3894 else {
3895 error = security_inode_unlink(dir, dentry);
bec1052e 3896 if (!error) {
5a14696c
BF
3897 error = try_break_deleg(target, delegated_inode);
3898 if (error)
b21996e3 3899 goto out;
1da177e4 3900 error = dir->i_op->unlink(dir, dentry);
8ed936b5 3901 if (!error) {
d83c49f3 3902 dont_mount(dentry);
8ed936b5 3903 detach_mounts(dentry);
116b9731 3904 fsnotify_unlink(dir, dentry);
8ed936b5 3905 }
bec1052e 3906 }
1da177e4 3907 }
b21996e3 3908out:
5955102c 3909 inode_unlock(target);
1da177e4
LT
3910
3911 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3912 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
9accbb97 3913 fsnotify_link_count(target);
e234f35c 3914 d_delete(dentry);
1da177e4 3915 }
0eeca283 3916
1da177e4
LT
3917 return error;
3918}
4d359507 3919EXPORT_SYMBOL(vfs_unlink);
1da177e4
LT
3920
3921/*
3922 * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b 3923 * directory's i_mutex. Truncate can take a long time if there is a lot of
1da177e4
LT
3924 * writeout happening, and we don't want to prevent access to the directory
3925 * while waiting on the I/O.
3926 */
da2f1362 3927long do_unlinkat(int dfd, struct filename *name)
1da177e4 3928{
2ad94ae6 3929 int error;
1da177e4 3930 struct dentry *dentry;
f5beed75
AV
3931 struct path path;
3932 struct qstr last;
3933 int type;
1da177e4 3934 struct inode *inode = NULL;
b21996e3 3935 struct inode *delegated_inode = NULL;
5d18f813
JL
3936 unsigned int lookup_flags = 0;
3937retry:
da2f1362 3938 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
91a27b2a
JL
3939 if (IS_ERR(name))
3940 return PTR_ERR(name);
2ad94ae6 3941
1da177e4 3942 error = -EISDIR;
f5beed75 3943 if (type != LAST_NORM)
1da177e4 3944 goto exit1;
0612d9fb 3945
f5beed75 3946 error = mnt_want_write(path.mnt);
c30dabfe
JK
3947 if (error)
3948 goto exit1;
b21996e3 3949retry_deleg:
5955102c 3950 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
f5beed75 3951 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
1da177e4
LT
3952 error = PTR_ERR(dentry);
3953 if (!IS_ERR(dentry)) {
3954 /* Why not before? Because we want correct error value */
f5beed75 3955 if (last.name[last.len])
50338b88 3956 goto slashes;
1da177e4 3957 inode = dentry->d_inode;
b18825a7 3958 if (d_is_negative(dentry))
e6bc45d6
TT
3959 goto slashes;
3960 ihold(inode);
f5beed75 3961 error = security_path_unlink(&path, dentry);
be6d3e56 3962 if (error)
c30dabfe 3963 goto exit2;
f5beed75 3964 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
c30dabfe 3965exit2:
1da177e4
LT
3966 dput(dentry);
3967 }
5955102c 3968 inode_unlock(path.dentry->d_inode);
1da177e4
LT
3969 if (inode)
3970 iput(inode); /* truncate the inode here */
b21996e3
BF
3971 inode = NULL;
3972 if (delegated_inode) {
5a14696c 3973 error = break_deleg_wait(&delegated_inode);
b21996e3
BF
3974 if (!error)
3975 goto retry_deleg;
3976 }
f5beed75 3977 mnt_drop_write(path.mnt);
1da177e4 3978exit1:
f5beed75 3979 path_put(&path);
5d18f813
JL
3980 if (retry_estale(error, lookup_flags)) {
3981 lookup_flags |= LOOKUP_REVAL;
3982 inode = NULL;
3983 goto retry;
3984 }
da2f1362 3985 putname(name);
1da177e4
LT
3986 return error;
3987
3988slashes:
b18825a7
DH
3989 if (d_is_negative(dentry))
3990 error = -ENOENT;
44b1d530 3991 else if (d_is_dir(dentry))
b18825a7
DH
3992 error = -EISDIR;
3993 else
3994 error = -ENOTDIR;
1da177e4
LT
3995 goto exit2;
3996}
3997
2e4d0924 3998SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d
UD
3999{
4000 if ((flag & ~AT_REMOVEDIR) != 0)
4001 return -EINVAL;
4002
4003 if (flag & AT_REMOVEDIR)
4004 return do_rmdir(dfd, pathname);
4005
da2f1362 4006 return do_unlinkat(dfd, getname(pathname));
5590ff0d
UD
4007}
4008
3480b257 4009SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d 4010{
da2f1362 4011 return do_unlinkat(AT_FDCWD, getname(pathname));
5590ff0d
UD
4012}
4013
db2e747b 4014int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4 4015{
a95164d9 4016 int error = may_create(dir, dentry);
1da177e4
LT
4017
4018 if (error)
4019 return error;
4020
acfa4380 4021 if (!dir->i_op->symlink)
1da177e4
LT
4022 return -EPERM;
4023
4024 error = security_inode_symlink(dir, dentry, oldname);
4025 if (error)
4026 return error;
4027
1da177e4 4028 error = dir->i_op->symlink(dir, dentry, oldname);
a74574aa 4029 if (!error)
f38aa942 4030 fsnotify_create(dir, dentry);
1da177e4
LT
4031 return error;
4032}
4d359507 4033EXPORT_SYMBOL(vfs_symlink);
1da177e4 4034
b724e846
DB
4035long do_symlinkat(const char __user *oldname, int newdfd,
4036 const char __user *newname)
1da177e4 4037{
2ad94ae6 4038 int error;
91a27b2a 4039 struct filename *from;
6902d925 4040 struct dentry *dentry;
dae6ad8f 4041 struct path path;
f46d3567 4042 unsigned int lookup_flags = 0;
1da177e4
LT
4043
4044 from = getname(oldname);
2ad94ae6 4045 if (IS_ERR(from))
1da177e4 4046 return PTR_ERR(from);
f46d3567
JL
4047retry:
4048 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
6902d925
DH
4049 error = PTR_ERR(dentry);
4050 if (IS_ERR(dentry))
dae6ad8f 4051 goto out_putname;
6902d925 4052
91a27b2a 4053 error = security_path_symlink(&path, dentry, from->name);
a8104a9f 4054 if (!error)
91a27b2a 4055 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
921a1650 4056 done_path_create(&path, dentry);
f46d3567
JL
4057 if (retry_estale(error, lookup_flags)) {
4058 lookup_flags |= LOOKUP_REVAL;
4059 goto retry;
4060 }
6902d925 4061out_putname:
1da177e4
LT
4062 putname(from);
4063 return error;
4064}
4065
b724e846
DB
4066SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4067 int, newdfd, const char __user *, newname)
4068{
4069 return do_symlinkat(oldname, newdfd, newname);
4070}
4071
3480b257 4072SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d 4073{
b724e846 4074 return do_symlinkat(oldname, AT_FDCWD, newname);
5590ff0d
UD
4075}
4076
146a8595
BF
4077/**
4078 * vfs_link - create a new link
4079 * @old_dentry: object to be linked
4080 * @dir: new parent
4081 * @new_dentry: where to create the new link
4082 * @delegated_inode: returns inode needing a delegation break
4083 *
4084 * The caller must hold dir->i_mutex
4085 *
4086 * If vfs_link discovers a delegation on the to-be-linked file in need
4087 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4088 * inode in delegated_inode. The caller should then break the delegation
4089 * and retry. Because breaking a delegation may take a long time, the
4090 * caller should drop the i_mutex before doing so.
4091 *
4092 * Alternatively, a caller may pass NULL for delegated_inode. This may
4093 * be appropriate for callers that expect the underlying filesystem not
4094 * to be NFS exported.
4095 */
4096int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
1da177e4
LT
4097{
4098 struct inode *inode = old_dentry->d_inode;
8de52778 4099 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
4100 int error;
4101
4102 if (!inode)
4103 return -ENOENT;
4104
a95164d9 4105 error = may_create(dir, new_dentry);
1da177e4
LT
4106 if (error)
4107 return error;
4108
4109 if (dir->i_sb != inode->i_sb)
4110 return -EXDEV;
4111
4112 /*
4113 * A link to an append-only or immutable file cannot be created.
4114 */
4115 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4116 return -EPERM;
0bd23d09
EB
4117 /*
4118 * Updating the link count will likely cause i_uid and i_gid to
4119 * be writen back improperly if their true value is unknown to
4120 * the vfs.
4121 */
4122 if (HAS_UNMAPPED_ID(inode))
4123 return -EPERM;
acfa4380 4124 if (!dir->i_op->link)
1da177e4 4125 return -EPERM;
7e79eedb 4126 if (S_ISDIR(inode->i_mode))
1da177e4
LT
4127 return -EPERM;
4128
4129 error = security_inode_link(old_dentry, dir, new_dentry);
4130 if (error)
4131 return error;
4132
5955102c 4133 inode_lock(inode);
aae8a97d 4134 /* Make sure we don't allow creating hardlink to an unlinked file */
f4e0c30c 4135 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
aae8a97d 4136 error = -ENOENT;
8de52778
AV
4137 else if (max_links && inode->i_nlink >= max_links)
4138 error = -EMLINK;
146a8595
BF
4139 else {
4140 error = try_break_deleg(inode, delegated_inode);
4141 if (!error)
4142 error = dir->i_op->link(old_dentry, dir, new_dentry);
4143 }
f4e0c30c
AV
4144
4145 if (!error && (inode->i_state & I_LINKABLE)) {
4146 spin_lock(&inode->i_lock);
4147 inode->i_state &= ~I_LINKABLE;
4148 spin_unlock(&inode->i_lock);
4149 }
5955102c 4150 inode_unlock(inode);
e31e14ec 4151 if (!error)
7e79eedb 4152 fsnotify_link(dir, inode, new_dentry);
1da177e4
LT
4153 return error;
4154}
4d359507 4155EXPORT_SYMBOL(vfs_link);
1da177e4
LT
4156
4157/*
4158 * Hardlinks are often used in delicate situations. We avoid
4159 * security-related surprises by not following symlinks on the
4160 * newname. --KAB
4161 *
4162 * We don't follow them on the oldname either to be compatible
4163 * with linux 2.0, and to avoid hard-linking to directories
4164 * and other special files. --ADM
4165 */
46ea89eb
DB
4166int do_linkat(int olddfd, const char __user *oldname, int newdfd,
4167 const char __user *newname, int flags)
1da177e4
LT
4168{
4169 struct dentry *new_dentry;
dae6ad8f 4170 struct path old_path, new_path;
146a8595 4171 struct inode *delegated_inode = NULL;
11a7b371 4172 int how = 0;
1da177e4 4173 int error;
1da177e4 4174
11a7b371 4175 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e1 4176 return -EINVAL;
11a7b371 4177 /*
f0cc6ffb
LT
4178 * To use null names we require CAP_DAC_READ_SEARCH
4179 * This ensures that not everyone will be able to create
4180 * handlink using the passed filedescriptor.
11a7b371 4181 */
f0cc6ffb
LT
4182 if (flags & AT_EMPTY_PATH) {
4183 if (!capable(CAP_DAC_READ_SEARCH))
4184 return -ENOENT;
11a7b371 4185 how = LOOKUP_EMPTY;
f0cc6ffb 4186 }
11a7b371
AK
4187
4188 if (flags & AT_SYMLINK_FOLLOW)
4189 how |= LOOKUP_FOLLOW;
442e31ca 4190retry:
11a7b371 4191 error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4 4192 if (error)
2ad94ae6
AV
4193 return error;
4194
442e31ca
JL
4195 new_dentry = user_path_create(newdfd, newname, &new_path,
4196 (how & LOOKUP_REVAL));
1da177e4 4197 error = PTR_ERR(new_dentry);
6902d925 4198 if (IS_ERR(new_dentry))
dae6ad8f
AV
4199 goto out;
4200
4201 error = -EXDEV;
4202 if (old_path.mnt != new_path.mnt)
4203 goto out_dput;
800179c9
KC
4204 error = may_linkat(&old_path);
4205 if (unlikely(error))
4206 goto out_dput;
dae6ad8f 4207 error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56 4208 if (error)
a8104a9f 4209 goto out_dput;
146a8595 4210 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
75c3f29d 4211out_dput:
921a1650 4212 done_path_create(&new_path, new_dentry);
146a8595
BF
4213 if (delegated_inode) {
4214 error = break_deleg_wait(&delegated_inode);
d22e6338
OD
4215 if (!error) {
4216 path_put(&old_path);
146a8595 4217 goto retry;
d22e6338 4218 }
146a8595 4219 }
442e31ca 4220 if (retry_estale(error, how)) {
d22e6338 4221 path_put(&old_path);
442e31ca
JL
4222 how |= LOOKUP_REVAL;
4223 goto retry;
4224 }
1da177e4 4225out:
2d8f3038 4226 path_put(&old_path);
1da177e4
LT
4227
4228 return error;
4229}
4230
46ea89eb
DB
4231SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4232 int, newdfd, const char __user *, newname, int, flags)
4233{
4234 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4235}
4236
3480b257 4237SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d 4238{
46ea89eb 4239 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
4240}
4241
bc27027a
MS
4242/**
4243 * vfs_rename - rename a filesystem object
4244 * @old_dir: parent of source
4245 * @old_dentry: source
4246 * @new_dir: parent of destination
4247 * @new_dentry: destination
4248 * @delegated_inode: returns an inode needing a delegation break
520c8b16 4249 * @flags: rename flags
bc27027a
MS
4250 *
4251 * The caller must hold multiple mutexes--see lock_rename()).
4252 *
4253 * If vfs_rename discovers a delegation in need of breaking at either
4254 * the source or destination, it will return -EWOULDBLOCK and return a
4255 * reference to the inode in delegated_inode. The caller should then
4256 * break the delegation and retry. Because breaking a delegation may
4257 * take a long time, the caller should drop all locks before doing
4258 * so.
4259 *
4260 * Alternatively, a caller may pass NULL for delegated_inode. This may
4261 * be appropriate for callers that expect the underlying filesystem not
4262 * to be NFS exported.
4263 *
1da177e4
LT
4264 * The worst of all namespace operations - renaming directory. "Perverted"
4265 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4266 * Problems:
0117d427 4267 *
d03b29a2 4268 * a) we can get into loop creation.
1da177e4
LT
4269 * b) race potential - two innocent renames can create a loop together.
4270 * That's where 4.4 screws up. Current fix: serialization on
a11f3a05 4271 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4 4272 * story.
6cedba89
BF
4273 * c) we have to lock _four_ objects - parents and victim (if it exists),
4274 * and source (if it is not a directory).
1b1dcc1b 4275 * And that - after we got ->i_mutex on parents (until then we don't know
1da177e4
LT
4276 * whether the target exists). Solution: try to be smart with locking
4277 * order for inodes. We rely on the fact that tree topology may change
a11f3a05 4278 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4
LT
4279 * move will be locked. Thus we can rank directories by the tree
4280 * (ancestors first) and rank all non-directories after them.
4281 * That works since everybody except rename does "lock parent, lookup,
a11f3a05 4282 * lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4
LT
4283 * HOWEVER, it relies on the assumption that any object with ->lookup()
4284 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4285 * we'd better make sure that there's no link(2) for them.
e4eaac06 4286 * d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b 4287 * we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4 4288 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e7 4289 * ->i_mutex on parents, which works but leads to some truly excessive
1da177e4
LT
4290 * locking].
4291 */
bc27027a
MS
4292int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4293 struct inode *new_dir, struct dentry *new_dentry,
520c8b16 4294 struct inode **delegated_inode, unsigned int flags)
1da177e4 4295{
bc27027a
MS
4296 int error;
4297 bool is_dir = d_is_dir(old_dentry);
bc27027a 4298 struct inode *source = old_dentry->d_inode;
9055cba7 4299 struct inode *target = new_dentry->d_inode;
da1ce067
MS
4300 bool new_is_dir = false;
4301 unsigned max_links = new_dir->i_sb->s_max_links;
49d31c2f 4302 struct name_snapshot old_name;
bc27027a 4303
8d3e2936 4304 if (source == target)
bc27027a
MS
4305 return 0;
4306
4307 error = may_delete(old_dir, old_dentry, is_dir);
4308 if (error)
4309 return error;
4310
da1ce067 4311 if (!target) {
bc27027a 4312 error = may_create(new_dir, new_dentry);
da1ce067
MS
4313 } else {
4314 new_is_dir = d_is_dir(new_dentry);
4315
4316 if (!(flags & RENAME_EXCHANGE))
4317 error = may_delete(new_dir, new_dentry, is_dir);
4318 else
4319 error = may_delete(new_dir, new_dentry, new_is_dir);
4320 }
bc27027a
MS
4321 if (error)
4322 return error;
4323
2773bf00 4324 if (!old_dir->i_op->rename)
bc27027a 4325 return -EPERM;
1da177e4
LT
4326
4327 /*
4328 * If we are going to change the parent - check write permissions,
4329 * we'll need to flip '..'.
4330 */
da1ce067
MS
4331 if (new_dir != old_dir) {
4332 if (is_dir) {
4333 error = inode_permission(source, MAY_WRITE);
4334 if (error)
4335 return error;
4336 }
4337 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4338 error = inode_permission(target, MAY_WRITE);
4339 if (error)
4340 return error;
4341 }
1da177e4
LT
4342 }
4343
0b3974eb
MS
4344 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4345 flags);
1da177e4
LT
4346 if (error)
4347 return error;
4348
49d31c2f 4349 take_dentry_name_snapshot(&old_name, old_dentry);
1d2ef590 4350 dget(new_dentry);
da1ce067 4351 if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a
MS
4352 lock_two_nondirectories(source, target);
4353 else if (target)
5955102c 4354 inode_lock(target);
9055cba7
SW
4355
4356 error = -EBUSY;
7af1364f 4357 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
9055cba7
SW
4358 goto out;
4359
da1ce067 4360 if (max_links && new_dir != old_dir) {
bc27027a 4361 error = -EMLINK;
da1ce067 4362 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
bc27027a 4363 goto out;
da1ce067
MS
4364 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4365 old_dir->i_nlink >= max_links)
4366 goto out;
4367 }
da1ce067 4368 if (!is_dir) {
bc27027a 4369 error = try_break_deleg(source, delegated_inode);
8e6d782c
BF
4370 if (error)
4371 goto out;
da1ce067
MS
4372 }
4373 if (target && !new_is_dir) {
4374 error = try_break_deleg(target, delegated_inode);
4375 if (error)
4376 goto out;
8e6d782c 4377 }
2773bf00 4378 error = old_dir->i_op->rename(old_dir, old_dentry,
18fc84da 4379 new_dir, new_dentry, flags);
51892bbb
SW
4380 if (error)
4381 goto out;
4382
da1ce067 4383 if (!(flags & RENAME_EXCHANGE) && target) {
8767712f
AV
4384 if (is_dir) {
4385 shrink_dcache_parent(new_dentry);
bc27027a 4386 target->i_flags |= S_DEAD;
8767712f 4387 }
51892bbb 4388 dont_mount(new_dentry);
8ed936b5 4389 detach_mounts(new_dentry);
bc27027a 4390 }
da1ce067
MS
4391 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4392 if (!(flags & RENAME_EXCHANGE))
4393 d_move(old_dentry, new_dentry);
4394 else
4395 d_exchange(old_dentry, new_dentry);
4396 }
51892bbb 4397out:
da1ce067 4398 if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a
MS
4399 unlock_two_nondirectories(source, target);
4400 else if (target)
5955102c 4401 inode_unlock(target);
1da177e4 4402 dput(new_dentry);
da1ce067 4403 if (!error) {
f4ec3a3d 4404 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
da1ce067
MS
4405 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4406 if (flags & RENAME_EXCHANGE) {
f4ec3a3d 4407 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
da1ce067
MS
4408 new_is_dir, NULL, new_dentry);
4409 }
4410 }
49d31c2f 4411 release_dentry_name_snapshot(&old_name);
0eeca283 4412
1da177e4
LT
4413 return error;
4414}
4d359507 4415EXPORT_SYMBOL(vfs_rename);
1da177e4 4416
ee81feb6
DB
4417static int do_renameat2(int olddfd, const char __user *oldname, int newdfd,
4418 const char __user *newname, unsigned int flags)
1da177e4 4419{
2ad94ae6
AV
4420 struct dentry *old_dentry, *new_dentry;
4421 struct dentry *trap;
f5beed75
AV
4422 struct path old_path, new_path;
4423 struct qstr old_last, new_last;
4424 int old_type, new_type;
8e6d782c 4425 struct inode *delegated_inode = NULL;
91a27b2a
JL
4426 struct filename *from;
4427 struct filename *to;
f5beed75 4428 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
c6a94284 4429 bool should_retry = false;
2ad94ae6 4430 int error;
520c8b16 4431
0d7a8555 4432 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
da1ce067
MS
4433 return -EINVAL;
4434
0d7a8555
MS
4435 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4436 (flags & RENAME_EXCHANGE))
520c8b16
MS
4437 return -EINVAL;
4438
0d7a8555
MS
4439 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4440 return -EPERM;
4441
f5beed75
AV
4442 if (flags & RENAME_EXCHANGE)
4443 target_flags = 0;
4444
c6a94284 4445retry:
c1d4dd27
AV
4446 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4447 &old_path, &old_last, &old_type);
91a27b2a
JL
4448 if (IS_ERR(from)) {
4449 error = PTR_ERR(from);
1da177e4 4450 goto exit;
91a27b2a 4451 }
1da177e4 4452
c1d4dd27
AV
4453 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4454 &new_path, &new_last, &new_type);
91a27b2a
JL
4455 if (IS_ERR(to)) {
4456 error = PTR_ERR(to);
1da177e4 4457 goto exit1;
91a27b2a 4458 }
1da177e4
LT
4459
4460 error = -EXDEV;
f5beed75 4461 if (old_path.mnt != new_path.mnt)
1da177e4
LT
4462 goto exit2;
4463
1da177e4 4464 error = -EBUSY;
f5beed75 4465 if (old_type != LAST_NORM)
1da177e4
LT
4466 goto exit2;
4467
0a7c3937
MS
4468 if (flags & RENAME_NOREPLACE)
4469 error = -EEXIST;
f5beed75 4470 if (new_type != LAST_NORM)
1da177e4
LT
4471 goto exit2;
4472
f5beed75 4473 error = mnt_want_write(old_path.mnt);
c30dabfe
JK
4474 if (error)
4475 goto exit2;
4476
8e6d782c 4477retry_deleg:
f5beed75 4478 trap = lock_rename(new_path.dentry, old_path.dentry);
1da177e4 4479
f5beed75 4480 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
1da177e4
LT
4481 error = PTR_ERR(old_dentry);
4482 if (IS_ERR(old_dentry))
4483 goto exit3;
4484 /* source must exist */
4485 error = -ENOENT;
b18825a7 4486 if (d_is_negative(old_dentry))
1da177e4 4487 goto exit4;
f5beed75 4488 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
0a7c3937
MS
4489 error = PTR_ERR(new_dentry);
4490 if (IS_ERR(new_dentry))
4491 goto exit4;
4492 error = -EEXIST;
4493 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4494 goto exit5;
da1ce067
MS
4495 if (flags & RENAME_EXCHANGE) {
4496 error = -ENOENT;
4497 if (d_is_negative(new_dentry))
4498 goto exit5;
4499
4500 if (!d_is_dir(new_dentry)) {
4501 error = -ENOTDIR;
f5beed75 4502 if (new_last.name[new_last.len])
da1ce067
MS
4503 goto exit5;
4504 }
4505 }
1da177e4 4506 /* unless the source is a directory trailing slashes give -ENOTDIR */
44b1d530 4507 if (!d_is_dir(old_dentry)) {
1da177e4 4508 error = -ENOTDIR;
f5beed75 4509 if (old_last.name[old_last.len])
0a7c3937 4510 goto exit5;
f5beed75 4511 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
0a7c3937 4512 goto exit5;
1da177e4
LT
4513 }
4514 /* source should not be ancestor of target */
4515 error = -EINVAL;
4516 if (old_dentry == trap)
0a7c3937 4517 goto exit5;
1da177e4 4518 /* target should not be an ancestor of source */
da1ce067
MS
4519 if (!(flags & RENAME_EXCHANGE))
4520 error = -ENOTEMPTY;
1da177e4
LT
4521 if (new_dentry == trap)
4522 goto exit5;
4523
f5beed75
AV
4524 error = security_path_rename(&old_path, old_dentry,
4525 &new_path, new_dentry, flags);
be6d3e56 4526 if (error)
c30dabfe 4527 goto exit5;
f5beed75
AV
4528 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4529 new_path.dentry->d_inode, new_dentry,
520c8b16 4530 &delegated_inode, flags);
1da177e4
LT
4531exit5:
4532 dput(new_dentry);
4533exit4:
4534 dput(old_dentry);
4535exit3:
f5beed75 4536 unlock_rename(new_path.dentry, old_path.dentry);
8e6d782c
BF
4537 if (delegated_inode) {
4538 error = break_deleg_wait(&delegated_inode);
4539 if (!error)
4540 goto retry_deleg;
4541 }
f5beed75 4542 mnt_drop_write(old_path.mnt);
1da177e4 4543exit2:
c6a94284
JL
4544 if (retry_estale(error, lookup_flags))
4545 should_retry = true;
f5beed75 4546 path_put(&new_path);
2ad94ae6 4547 putname(to);
1da177e4 4548exit1:
f5beed75 4549 path_put(&old_path);
1da177e4 4550 putname(from);
c6a94284
JL
4551 if (should_retry) {
4552 should_retry = false;
4553 lookup_flags |= LOOKUP_REVAL;
4554 goto retry;
4555 }
2ad94ae6 4556exit:
1da177e4
LT
4557 return error;
4558}
4559
ee81feb6
DB
4560SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4561 int, newdfd, const char __user *, newname, unsigned int, flags)
4562{
4563 return do_renameat2(olddfd, oldname, newdfd, newname, flags);
4564}
4565
520c8b16
MS
4566SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4567 int, newdfd, const char __user *, newname)
4568{
ee81feb6 4569 return do_renameat2(olddfd, oldname, newdfd, newname, 0);
520c8b16
MS
4570}
4571
a26eab24 4572SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d 4573{
ee81feb6 4574 return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
4575}
4576
787fb6bc
MS
4577int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4578{
4579 int error = may_create(dir, dentry);
4580 if (error)
4581 return error;
4582
4583 if (!dir->i_op->mknod)
4584 return -EPERM;
4585
4586 return dir->i_op->mknod(dir, dentry,
4587 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4588}
4589EXPORT_SYMBOL(vfs_whiteout);
4590
5d826c84 4591int readlink_copy(char __user *buffer, int buflen, const char *link)
1da177e4 4592{
5d826c84 4593 int len = PTR_ERR(link);
1da177e4
LT
4594 if (IS_ERR(link))
4595 goto out;
4596
4597 len = strlen(link);
4598 if (len > (unsigned) buflen)
4599 len = buflen;
4600 if (copy_to_user(buffer, link, len))
4601 len = -EFAULT;
4602out:
4603 return len;
4604}
4605
fd4a0edf
MS
4606/**
4607 * vfs_readlink - copy symlink body into userspace buffer
4608 * @dentry: dentry on which to get symbolic link
4609 * @buffer: user memory pointer
4610 * @buflen: size of buffer
4611 *
4612 * Does not touch atime. That's up to the caller if necessary
4613 *
4614 * Does not call security hook.
4615 */
4616int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4617{
4618 struct inode *inode = d_inode(dentry);
f2df5da6
AV
4619 DEFINE_DELAYED_CALL(done);
4620 const char *link;
4621 int res;
fd4a0edf 4622
76fca90e
MS
4623 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4624 if (unlikely(inode->i_op->readlink))
4625 return inode->i_op->readlink(dentry, buffer, buflen);
4626
4627 if (!d_is_symlink(dentry))
4628 return -EINVAL;
4629
4630 spin_lock(&inode->i_lock);
4631 inode->i_opflags |= IOP_DEFAULT_READLINK;
4632 spin_unlock(&inode->i_lock);
4633 }
fd4a0edf 4634
4c4f7c19 4635 link = READ_ONCE(inode->i_link);
f2df5da6
AV
4636 if (!link) {
4637 link = inode->i_op->get_link(dentry, inode, &done);
4638 if (IS_ERR(link))
4639 return PTR_ERR(link);
4640 }
4641 res = readlink_copy(buffer, buflen, link);
4642 do_delayed_call(&done);
4643 return res;
fd4a0edf
MS
4644}
4645EXPORT_SYMBOL(vfs_readlink);
1da177e4 4646
d60874cd
MS
4647/**
4648 * vfs_get_link - get symlink body
4649 * @dentry: dentry on which to get symbolic link
4650 * @done: caller needs to free returned data with this
4651 *
4652 * Calls security hook and i_op->get_link() on the supplied inode.
4653 *
4654 * It does not touch atime. That's up to the caller if necessary.
4655 *
4656 * Does not work on "special" symlinks like /proc/$$/fd/N
4657 */
4658const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4659{
4660 const char *res = ERR_PTR(-EINVAL);
4661 struct inode *inode = d_inode(dentry);
4662
4663 if (d_is_symlink(dentry)) {
4664 res = ERR_PTR(security_inode_readlink(dentry));
4665 if (!res)
4666 res = inode->i_op->get_link(dentry, inode, done);
4667 }
4668 return res;
4669}
4670EXPORT_SYMBOL(vfs_get_link);
4671
1da177e4 4672/* get the link contents into pagecache */
6b255391 4673const char *page_get_link(struct dentry *dentry, struct inode *inode,
fceef393 4674 struct delayed_call *callback)
1da177e4 4675{
ebd09abb
DG
4676 char *kaddr;
4677 struct page *page;
6b255391
AV
4678 struct address_space *mapping = inode->i_mapping;
4679
d3883d4f
AV
4680 if (!dentry) {
4681 page = find_get_page(mapping, 0);
4682 if (!page)
4683 return ERR_PTR(-ECHILD);
4684 if (!PageUptodate(page)) {
4685 put_page(page);
4686 return ERR_PTR(-ECHILD);
4687 }
4688 } else {
4689 page = read_mapping_page(mapping, 0, NULL);
4690 if (IS_ERR(page))
4691 return (char*)page;
4692 }
fceef393 4693 set_delayed_call(callback, page_put_link, page);
21fc61c7
AV
4694 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4695 kaddr = page_address(page);
6b255391 4696 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
ebd09abb 4697 return kaddr;
1da177e4
LT
4698}
4699
6b255391 4700EXPORT_SYMBOL(page_get_link);
1da177e4 4701
fceef393 4702void page_put_link(void *arg)
1da177e4 4703{
fceef393 4704 put_page(arg);
1da177e4 4705}
4d359507 4706EXPORT_SYMBOL(page_put_link);
1da177e4 4707
aa80deab
AV
4708int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4709{
fceef393 4710 DEFINE_DELAYED_CALL(done);
6b255391
AV
4711 int res = readlink_copy(buffer, buflen,
4712 page_get_link(dentry, d_inode(dentry),
fceef393
AV
4713 &done));
4714 do_delayed_call(&done);
aa80deab
AV
4715 return res;
4716}
4717EXPORT_SYMBOL(page_readlink);
4718
54566b2c
NP
4719/*
4720 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4721 */
4722int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4
LT
4723{
4724 struct address_space *mapping = inode->i_mapping;
0adb25d2 4725 struct page *page;
afddba49 4726 void *fsdata;
beb497ab 4727 int err;
c718a975 4728 unsigned int flags = 0;
54566b2c
NP
4729 if (nofs)
4730 flags |= AOP_FLAG_NOFS;
1da177e4 4731
7e53cac4 4732retry:
afddba49 4733 err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c 4734 flags, &page, &fsdata);
1da177e4 4735 if (err)
afddba49
NP
4736 goto fail;
4737
21fc61c7 4738 memcpy(page_address(page), symname, len-1);
afddba49
NP
4739
4740 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4741 page, fsdata);
1da177e4
LT
4742 if (err < 0)
4743 goto fail;
afddba49
NP
4744 if (err < len-1)
4745 goto retry;
4746
1da177e4
LT
4747 mark_inode_dirty(inode);
4748 return 0;
1da177e4
LT
4749fail:
4750 return err;
4751}
4d359507 4752EXPORT_SYMBOL(__page_symlink);
1da177e4 4753
0adb25d2
KK
4754int page_symlink(struct inode *inode, const char *symname, int len)
4755{
4756 return __page_symlink(inode, symname, len,
c62d2555 4757 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
0adb25d2 4758}
4d359507 4759EXPORT_SYMBOL(page_symlink);
0adb25d2 4760
92e1d5be 4761const struct inode_operations page_symlink_inode_operations = {
6b255391 4762 .get_link = page_get_link,
1da177e4 4763};
1da177e4 4764EXPORT_SYMBOL(page_symlink_inode_operations);