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