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