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