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