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