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