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