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