rename __filename_parentat() to filename_parentat()
[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{
91ef658f
DK
258 if (IS_ERR_OR_NULL(name))
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
561ec64a
LT
1023int sysctl_protected_symlinks __read_mostly = 0;
1024int sysctl_protected_hardlinks __read_mostly = 0;
30aba665
SM
1025int sysctl_protected_fifos __read_mostly;
1026int sysctl_protected_regular __read_mostly;
800179c9
KC
1027
1028/**
1029 * may_follow_link - Check symlink following for unsafe situations
55852635 1030 * @nd: nameidata pathwalk data
800179c9
KC
1031 *
1032 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1033 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1034 * in a sticky world-writable directory. This is to protect privileged
1035 * processes from failing races against path names that may change out
1036 * from under them by way of other users creating malicious symlinks.
1037 * It will permit symlinks to be followed only when outside a sticky
1038 * world-writable directory, or when the uid of the symlink and follower
1039 * match, or when the directory owner matches the symlink's owner.
1040 *
1041 * Returns 0 if following the symlink is allowed, -ve on error.
1042 */
ad6cc4c3 1043static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
800179c9 1044{
ba73d987
CB
1045 struct user_namespace *mnt_userns;
1046 kuid_t i_uid;
1047
800179c9
KC
1048 if (!sysctl_protected_symlinks)
1049 return 0;
1050
ba73d987
CB
1051 mnt_userns = mnt_user_ns(nd->path.mnt);
1052 i_uid = i_uid_into_mnt(mnt_userns, inode);
800179c9 1053 /* Allowed if owner and follower match. */
ba73d987 1054 if (uid_eq(current_cred()->fsuid, i_uid))
800179c9
KC
1055 return 0;
1056
1057 /* Allowed if parent directory not sticky and world-writable. */
0f705953 1058 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
800179c9
KC
1059 return 0;
1060
1061 /* Allowed if parent directory and link owner match. */
ba73d987 1062 if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, i_uid))
800179c9
KC
1063 return 0;
1064
31956502
AV
1065 if (nd->flags & LOOKUP_RCU)
1066 return -ECHILD;
1067
ea841baf 1068 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
245d7369 1069 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
800179c9
KC
1070 return -EACCES;
1071}
1072
1073/**
1074 * safe_hardlink_source - Check for safe hardlink conditions
ba73d987 1075 * @mnt_userns: user namespace of the mount the inode was found from
800179c9
KC
1076 * @inode: the source inode to hardlink from
1077 *
1078 * Return false if at least one of the following conditions:
1079 * - inode is not a regular file
1080 * - inode is setuid
1081 * - inode is setgid and group-exec
1082 * - access failure for read and write
1083 *
1084 * Otherwise returns true.
1085 */
ba73d987
CB
1086static bool safe_hardlink_source(struct user_namespace *mnt_userns,
1087 struct inode *inode)
800179c9
KC
1088{
1089 umode_t mode = inode->i_mode;
1090
1091 /* Special files should not get pinned to the filesystem. */
1092 if (!S_ISREG(mode))
1093 return false;
1094
1095 /* Setuid files should not get pinned to the filesystem. */
1096 if (mode & S_ISUID)
1097 return false;
1098
1099 /* Executable setgid files should not get pinned to the filesystem. */
1100 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1101 return false;
1102
1103 /* Hardlinking to unreadable or unwritable sources is dangerous. */
ba73d987 1104 if (inode_permission(mnt_userns, inode, MAY_READ | MAY_WRITE))
800179c9
KC
1105 return false;
1106
1107 return true;
1108}
1109
1110/**
1111 * may_linkat - Check permissions for creating a hardlink
ba73d987 1112 * @mnt_userns: user namespace of the mount the inode was found from
800179c9
KC
1113 * @link: the source to hardlink from
1114 *
1115 * Block hardlink when all of:
1116 * - sysctl_protected_hardlinks enabled
1117 * - fsuid does not match inode
1118 * - hardlink source is unsafe (see safe_hardlink_source() above)
f2ca3796 1119 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
800179c9 1120 *
ba73d987
CB
1121 * If the inode has been found through an idmapped mount the user namespace of
1122 * the vfsmount must be passed through @mnt_userns. This function will then take
1123 * care to map the inode according to @mnt_userns before checking permissions.
1124 * On non-idmapped mounts or if permission checking is to be performed on the
1125 * raw inode simply passs init_user_ns.
1126 *
800179c9
KC
1127 * Returns 0 if successful, -ve on error.
1128 */
ba73d987 1129int may_linkat(struct user_namespace *mnt_userns, struct path *link)
800179c9 1130{
593d1ce8
EB
1131 struct inode *inode = link->dentry->d_inode;
1132
1133 /* Inode writeback is not safe when the uid or gid are invalid. */
ba73d987
CB
1134 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
1135 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
593d1ce8 1136 return -EOVERFLOW;
800179c9
KC
1137
1138 if (!sysctl_protected_hardlinks)
1139 return 0;
1140
800179c9
KC
1141 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1142 * otherwise, it must be a safe source.
1143 */
ba73d987
CB
1144 if (safe_hardlink_source(mnt_userns, inode) ||
1145 inode_owner_or_capable(mnt_userns, inode))
800179c9
KC
1146 return 0;
1147
245d7369 1148 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
800179c9
KC
1149 return -EPERM;
1150}
1151
30aba665
SM
1152/**
1153 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1154 * should be allowed, or not, on files that already
1155 * exist.
ba73d987 1156 * @mnt_userns: user namespace of the mount the inode was found from
2111c3c0 1157 * @nd: nameidata pathwalk data
30aba665
SM
1158 * @inode: the inode of the file to open
1159 *
1160 * Block an O_CREAT open of a FIFO (or a regular file) when:
1161 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1162 * - the file already exists
1163 * - we are in a sticky directory
1164 * - we don't own the file
1165 * - the owner of the directory doesn't own the file
1166 * - the directory is world writable
1167 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1168 * the directory doesn't have to be world writable: being group writable will
1169 * be enough.
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 *
30aba665
SM
1177 * Returns 0 if the open is allowed, -ve on error.
1178 */
ba73d987
CB
1179static int may_create_in_sticky(struct user_namespace *mnt_userns,
1180 struct nameidata *nd, struct inode *const inode)
30aba665 1181{
ba73d987
CB
1182 umode_t dir_mode = nd->dir_mode;
1183 kuid_t dir_uid = nd->dir_uid;
1184
30aba665
SM
1185 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1186 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
d0cb5018 1187 likely(!(dir_mode & S_ISVTX)) ||
ba73d987
CB
1188 uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
1189 uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
30aba665
SM
1190 return 0;
1191
d0cb5018
AV
1192 if (likely(dir_mode & 0002) ||
1193 (dir_mode & 0020 &&
30aba665
SM
1194 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1195 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
245d7369
KC
1196 const char *operation = S_ISFIFO(inode->i_mode) ?
1197 "sticky_create_fifo" :
1198 "sticky_create_regular";
1199 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
30aba665
SM
1200 return -EACCES;
1201 }
1202 return 0;
1203}
1204
f015f126
DH
1205/*
1206 * follow_up - Find the mountpoint of path's vfsmount
1207 *
1208 * Given a path, find the mountpoint of its source file system.
1209 * Replace @path with the path of the mountpoint in the parent mount.
1210 * Up is towards /.
1211 *
1212 * Return 1 if we went up a level and 0 if we were already at the
1213 * root.
1214 */
bab77ebf 1215int follow_up(struct path *path)
1da177e4 1216{
0714a533
AV
1217 struct mount *mnt = real_mount(path->mnt);
1218 struct mount *parent;
1da177e4 1219 struct dentry *mountpoint;
99b7db7b 1220
48a066e7 1221 read_seqlock_excl(&mount_lock);
0714a533 1222 parent = mnt->mnt_parent;
3c0a6163 1223 if (parent == mnt) {
48a066e7 1224 read_sequnlock_excl(&mount_lock);
1da177e4
LT
1225 return 0;
1226 }
0714a533 1227 mntget(&parent->mnt);
a73324da 1228 mountpoint = dget(mnt->mnt_mountpoint);
48a066e7 1229 read_sequnlock_excl(&mount_lock);
bab77ebf
AV
1230 dput(path->dentry);
1231 path->dentry = mountpoint;
1232 mntput(path->mnt);
0714a533 1233 path->mnt = &parent->mnt;
1da177e4
LT
1234 return 1;
1235}
4d359507 1236EXPORT_SYMBOL(follow_up);
1da177e4 1237
7ef482fa
AV
1238static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
1239 struct path *path, unsigned *seqp)
1240{
1241 while (mnt_has_parent(m)) {
1242 struct dentry *mountpoint = m->mnt_mountpoint;
1243
1244 m = m->mnt_parent;
1245 if (unlikely(root->dentry == mountpoint &&
1246 root->mnt == &m->mnt))
1247 break;
1248 if (mountpoint != m->mnt.mnt_root) {
1249 path->mnt = &m->mnt;
1250 path->dentry = mountpoint;
1251 *seqp = read_seqcount_begin(&mountpoint->d_seq);
1252 return true;
1253 }
1254 }
1255 return false;
1256}
1257
2aa38470
AV
1258static bool choose_mountpoint(struct mount *m, const struct path *root,
1259 struct path *path)
1260{
1261 bool found;
1262
1263 rcu_read_lock();
1264 while (1) {
1265 unsigned seq, mseq = read_seqbegin(&mount_lock);
1266
1267 found = choose_mountpoint_rcu(m, root, path, &seq);
1268 if (unlikely(!found)) {
1269 if (!read_seqretry(&mount_lock, mseq))
1270 break;
1271 } else {
1272 if (likely(__legitimize_path(path, seq, mseq)))
1273 break;
1274 rcu_read_unlock();
1275 path_put(path);
1276 rcu_read_lock();
1277 }
1278 }
1279 rcu_read_unlock();
1280 return found;
1281}
1282
b5c84bf6 1283/*
9875cf80
DH
1284 * Perform an automount
1285 * - return -EISDIR to tell follow_managed() to stop and return the path we
1286 * were called with.
1da177e4 1287 */
1c9f5e06 1288static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
31e6b01f 1289{
25e195aa 1290 struct dentry *dentry = path->dentry;
9875cf80 1291
0ec26fd0
MS
1292 /* We don't want to mount if someone's just doing a stat -
1293 * unless they're stat'ing a directory and appended a '/' to
1294 * the name.
1295 *
1296 * We do, however, want to mount if someone wants to open or
1297 * create a file of any type under the mountpoint, wants to
1298 * traverse through the mountpoint or wants to open the
1299 * mounted directory. Also, autofs may mark negative dentries
1300 * as being automount points. These will need the attentions
1301 * of the daemon to instantiate them before they can be used.
9875cf80 1302 */
1c9f5e06 1303 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
5d38f049 1304 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
25e195aa 1305 dentry->d_inode)
5d38f049 1306 return -EISDIR;
0ec26fd0 1307
1c9f5e06 1308 if (count && (*count)++ >= MAXSYMLINKS)
9875cf80
DH
1309 return -ELOOP;
1310
25e195aa 1311 return finish_automount(dentry->d_op->d_automount(path), path);
463ffb2e
AV
1312}
1313
9875cf80 1314/*
9deed3eb
AV
1315 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1316 * dentries are pinned but not locked here, so negative dentry can go
1317 * positive right under us. Use of smp_load_acquire() provides a barrier
1318 * sufficient for ->d_inode and ->d_flags consistency.
9875cf80 1319 */
9deed3eb
AV
1320static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
1321 int *count, unsigned lookup_flags)
1da177e4 1322{
9deed3eb 1323 struct vfsmount *mnt = path->mnt;
9875cf80 1324 bool need_mntput = false;
8aef1884 1325 int ret = 0;
9875cf80 1326
9deed3eb 1327 while (flags & DCACHE_MANAGED_DENTRY) {
cc53ce53
DH
1328 /* Allow the filesystem to manage the transit without i_mutex
1329 * being held. */
d41efb52 1330 if (flags & DCACHE_MANAGE_TRANSIT) {
fb5f51c7 1331 ret = path->dentry->d_op->d_manage(path, false);
508c8772 1332 flags = smp_load_acquire(&path->dentry->d_flags);
cc53ce53 1333 if (ret < 0)
8aef1884 1334 break;
cc53ce53
DH
1335 }
1336
9deed3eb 1337 if (flags & DCACHE_MOUNTED) { // something's mounted on it..
9875cf80 1338 struct vfsmount *mounted = lookup_mnt(path);
9deed3eb 1339 if (mounted) { // ... in our namespace
9875cf80
DH
1340 dput(path->dentry);
1341 if (need_mntput)
1342 mntput(path->mnt);
1343 path->mnt = mounted;
1344 path->dentry = dget(mounted->mnt_root);
9deed3eb
AV
1345 // here we know it's positive
1346 flags = path->dentry->d_flags;
9875cf80
DH
1347 need_mntput = true;
1348 continue;
1349 }
9875cf80
DH
1350 }
1351
9deed3eb
AV
1352 if (!(flags & DCACHE_NEED_AUTOMOUNT))
1353 break;
9875cf80 1354
9deed3eb
AV
1355 // uncovered automount point
1356 ret = follow_automount(path, count, lookup_flags);
1357 flags = smp_load_acquire(&path->dentry->d_flags);
1358 if (ret < 0)
1359 break;
1da177e4 1360 }
8aef1884 1361
9deed3eb
AV
1362 if (ret == -EISDIR)
1363 ret = 0;
1364 // possible if you race with several mount --move
1365 if (need_mntput && path->mnt == mnt)
1366 mntput(path->mnt);
1367 if (!ret && unlikely(d_flags_negative(flags)))
d41efb52 1368 ret = -ENOENT;
9deed3eb 1369 *jumped = need_mntput;
8402752e 1370 return ret;
1da177e4
LT
1371}
1372
9deed3eb
AV
1373static inline int traverse_mounts(struct path *path, bool *jumped,
1374 int *count, unsigned lookup_flags)
1375{
1376 unsigned flags = smp_load_acquire(&path->dentry->d_flags);
1377
1378 /* fastpath */
1379 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
1380 *jumped = false;
1381 if (unlikely(d_flags_negative(flags)))
1382 return -ENOENT;
1383 return 0;
1384 }
1385 return __traverse_mounts(path, flags, jumped, count, lookup_flags);
1386}
1387
cc53ce53 1388int follow_down_one(struct path *path)
1da177e4
LT
1389{
1390 struct vfsmount *mounted;
1391
1c755af4 1392 mounted = lookup_mnt(path);
1da177e4 1393 if (mounted) {
9393bd07
AV
1394 dput(path->dentry);
1395 mntput(path->mnt);
1396 path->mnt = mounted;
1397 path->dentry = dget(mounted->mnt_root);
1da177e4
LT
1398 return 1;
1399 }
1400 return 0;
1401}
4d359507 1402EXPORT_SYMBOL(follow_down_one);
1da177e4 1403
9deed3eb
AV
1404/*
1405 * Follow down to the covering mount currently visible to userspace. At each
1406 * point, the filesystem owning that dentry may be queried as to whether the
1407 * caller is permitted to proceed or not.
1408 */
1409int follow_down(struct path *path)
1410{
1411 struct vfsmount *mnt = path->mnt;
1412 bool jumped;
1413 int ret = traverse_mounts(path, &jumped, NULL, 0);
1414
1415 if (path->mnt != mnt)
1416 mntput(mnt);
1417 return ret;
1418}
1419EXPORT_SYMBOL(follow_down);
1420
9875cf80 1421/*
287548e4
AV
1422 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1423 * we meet a managed dentry that would need blocking.
9875cf80
DH
1424 */
1425static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
254cf582 1426 struct inode **inode, unsigned *seqp)
9875cf80 1427{
ea936aeb
AV
1428 struct dentry *dentry = path->dentry;
1429 unsigned int flags = dentry->d_flags;
1430
1431 if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1432 return true;
1433
1434 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1435 return false;
1436
62a7375e 1437 for (;;) {
62a7375e
IK
1438 /*
1439 * Don't forget we might have a non-mountpoint managed dentry
1440 * that wants to block transit.
1441 */
ea936aeb
AV
1442 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1443 int res = dentry->d_op->d_manage(path, true);
1444 if (res)
1445 return res == -EISDIR;
1446 flags = dentry->d_flags;
b8faf035 1447 }
62a7375e 1448
ea936aeb
AV
1449 if (flags & DCACHE_MOUNTED) {
1450 struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1451 if (mounted) {
1452 path->mnt = &mounted->mnt;
1453 dentry = path->dentry = mounted->mnt.mnt_root;
bcba1e7d 1454 nd->state |= ND_JUMPED;
ea936aeb
AV
1455 *seqp = read_seqcount_begin(&dentry->d_seq);
1456 *inode = dentry->d_inode;
1457 /*
1458 * We don't need to re-check ->d_seq after this
1459 * ->d_inode read - there will be an RCU delay
1460 * between mount hash removal and ->mnt_root
1461 * becoming unpinned.
1462 */
1463 flags = dentry->d_flags;
1464 continue;
1465 }
1466 if (read_seqretry(&mount_lock, nd->m_seq))
1467 return false;
1468 }
1469 return !(flags & DCACHE_NEED_AUTOMOUNT);
9875cf80 1470 }
287548e4
AV
1471}
1472
db3c9ade
AV
1473static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1474 struct path *path, struct inode **inode,
1475 unsigned int *seqp)
bd7c4b50 1476{
9deed3eb 1477 bool jumped;
db3c9ade 1478 int ret;
bd7c4b50 1479
db3c9ade
AV
1480 path->mnt = nd->path.mnt;
1481 path->dentry = dentry;
c153007b
AV
1482 if (nd->flags & LOOKUP_RCU) {
1483 unsigned int seq = *seqp;
1484 if (unlikely(!*inode))
1485 return -ENOENT;
1486 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
9deed3eb 1487 return 0;
ae66db45 1488 if (!try_to_unlazy_next(nd, dentry, seq))
c153007b
AV
1489 return -ECHILD;
1490 // *path might've been clobbered by __follow_mount_rcu()
1491 path->mnt = nd->path.mnt;
1492 path->dentry = dentry;
1493 }
9deed3eb
AV
1494 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
1495 if (jumped) {
1496 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1497 ret = -EXDEV;
1498 else
bcba1e7d 1499 nd->state |= ND_JUMPED;
9deed3eb
AV
1500 }
1501 if (unlikely(ret)) {
1502 dput(path->dentry);
1503 if (path->mnt != nd->path.mnt)
1504 mntput(path->mnt);
1505 } else {
bd7c4b50
AV
1506 *inode = d_backing_inode(path->dentry);
1507 *seqp = 0; /* out of RCU mode, so the value doesn't matter */
1508 }
1509 return ret;
1510}
1511
baa03890 1512/*
f4fdace9
OD
1513 * This looks up the name in dcache and possibly revalidates the found dentry.
1514 * NULL is returned if the dentry does not exist in the cache.
baa03890 1515 */
e3c13928
AV
1516static struct dentry *lookup_dcache(const struct qstr *name,
1517 struct dentry *dir,
6c51e513 1518 unsigned int flags)
baa03890 1519{
a89f8337 1520 struct dentry *dentry = d_lookup(dir, name);
bad61189 1521 if (dentry) {
a89f8337
AV
1522 int error = d_revalidate(dentry, flags);
1523 if (unlikely(error <= 0)) {
1524 if (!error)
1525 d_invalidate(dentry);
1526 dput(dentry);
1527 return ERR_PTR(error);
bad61189
MS
1528 }
1529 }
baa03890
NP
1530 return dentry;
1531}
1532
44396f4b 1533/*
a03ece5f
AV
1534 * Parent directory has inode locked exclusive. This is one
1535 * and only case when ->lookup() gets called on non in-lookup
1536 * dentries - as the matter of fact, this only gets called
1537 * when directory is guaranteed to have no in-lookup children
1538 * at all.
44396f4b 1539 */
e3c13928 1540static struct dentry *__lookup_hash(const struct qstr *name,
72bd866a 1541 struct dentry *base, unsigned int flags)
a3255546 1542{
6c51e513 1543 struct dentry *dentry = lookup_dcache(name, base, flags);
a03ece5f
AV
1544 struct dentry *old;
1545 struct inode *dir = base->d_inode;
a3255546 1546
6c51e513 1547 if (dentry)
bad61189 1548 return dentry;
a3255546 1549
a03ece5f
AV
1550 /* Don't create child dentry for a dead directory. */
1551 if (unlikely(IS_DEADDIR(dir)))
1552 return ERR_PTR(-ENOENT);
1553
6c51e513
AV
1554 dentry = d_alloc(base, name);
1555 if (unlikely(!dentry))
1556 return ERR_PTR(-ENOMEM);
1557
a03ece5f
AV
1558 old = dir->i_op->lookup(dir, dentry, flags);
1559 if (unlikely(old)) {
1560 dput(dentry);
1561 dentry = old;
1562 }
1563 return dentry;
a3255546
AV
1564}
1565
20e34357
AV
1566static struct dentry *lookup_fast(struct nameidata *nd,
1567 struct inode **inode,
1568 unsigned *seqp)
1da177e4 1569{
31e6b01f 1570 struct dentry *dentry, *parent = nd->path.dentry;
5a18fff2 1571 int status = 1;
9875cf80 1572
b04f784e
NP
1573 /*
1574 * Rename seqlock is not required here because in the off chance
5d0f49c1
AV
1575 * of a false negative due to a concurrent rename, the caller is
1576 * going to fall back to non-racy lookup.
b04f784e 1577 */
31e6b01f
NP
1578 if (nd->flags & LOOKUP_RCU) {
1579 unsigned seq;
da53be12 1580 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
5d0f49c1 1581 if (unlikely(!dentry)) {
e36cffed 1582 if (!try_to_unlazy(nd))
20e34357
AV
1583 return ERR_PTR(-ECHILD);
1584 return NULL;
5d0f49c1 1585 }
5a18fff2 1586
12f8ad4b
LT
1587 /*
1588 * This sequence count validates that the inode matches
1589 * the dentry name information from lookup.
1590 */
63afdfc7 1591 *inode = d_backing_inode(dentry);
5d0f49c1 1592 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
20e34357 1593 return ERR_PTR(-ECHILD);
12f8ad4b
LT
1594
1595 /*
1596 * This sequence count validates that the parent had no
1597 * changes while we did the lookup of the dentry above.
1598 *
1599 * The memory barrier in read_seqcount_begin of child is
1600 * enough, we can use __read_seqcount_retry here.
1601 */
5d0f49c1 1602 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
20e34357 1603 return ERR_PTR(-ECHILD);
5a18fff2 1604
254cf582 1605 *seqp = seq;
a89f8337 1606 status = d_revalidate(dentry, nd->flags);
c153007b 1607 if (likely(status > 0))
20e34357 1608 return dentry;
ae66db45 1609 if (!try_to_unlazy_next(nd, dentry, seq))
20e34357 1610 return ERR_PTR(-ECHILD);
26ddb45e 1611 if (status == -ECHILD)
209a7fb2
AV
1612 /* we'd been told to redo it in non-rcu mode */
1613 status = d_revalidate(dentry, nd->flags);
5a18fff2 1614 } else {
e97cdc87 1615 dentry = __d_lookup(parent, &nd->last);
5d0f49c1 1616 if (unlikely(!dentry))
20e34357 1617 return NULL;
a89f8337 1618 status = d_revalidate(dentry, nd->flags);
9875cf80 1619 }
5a18fff2 1620 if (unlikely(status <= 0)) {
e9742b53 1621 if (!status)
5d0f49c1 1622 d_invalidate(dentry);
5542aa2f 1623 dput(dentry);
20e34357 1624 return ERR_PTR(status);
24643087 1625 }
20e34357 1626 return dentry;
697f514d
MS
1627}
1628
1629/* Fast lookup failed, do it the slow way */
88d8331a
AV
1630static struct dentry *__lookup_slow(const struct qstr *name,
1631 struct dentry *dir,
1632 unsigned int flags)
697f514d 1633{
88d8331a 1634 struct dentry *dentry, *old;
1936386e 1635 struct inode *inode = dir->d_inode;
d9171b93 1636 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1936386e 1637
1936386e 1638 /* Don't go there if it's already dead */
94bdd655 1639 if (unlikely(IS_DEADDIR(inode)))
88d8331a 1640 return ERR_PTR(-ENOENT);
94bdd655 1641again:
d9171b93 1642 dentry = d_alloc_parallel(dir, name, &wq);
94bdd655 1643 if (IS_ERR(dentry))
88d8331a 1644 return dentry;
94bdd655 1645 if (unlikely(!d_in_lookup(dentry))) {
c64cd6e3
AV
1646 int error = d_revalidate(dentry, flags);
1647 if (unlikely(error <= 0)) {
1648 if (!error) {
1649 d_invalidate(dentry);
949a852e 1650 dput(dentry);
c64cd6e3 1651 goto again;
949a852e 1652 }
c64cd6e3
AV
1653 dput(dentry);
1654 dentry = ERR_PTR(error);
949a852e 1655 }
94bdd655
AV
1656 } else {
1657 old = inode->i_op->lookup(inode, dentry, flags);
1658 d_lookup_done(dentry);
1659 if (unlikely(old)) {
1660 dput(dentry);
1661 dentry = old;
949a852e
AV
1662 }
1663 }
e3c13928 1664 return dentry;
1da177e4
LT
1665}
1666
88d8331a
AV
1667static struct dentry *lookup_slow(const struct qstr *name,
1668 struct dentry *dir,
1669 unsigned int flags)
1670{
1671 struct inode *inode = dir->d_inode;
1672 struct dentry *res;
1673 inode_lock_shared(inode);
1674 res = __lookup_slow(name, dir, flags);
1675 inode_unlock_shared(inode);
1676 return res;
1677}
1678
ba73d987
CB
1679static inline int may_lookup(struct user_namespace *mnt_userns,
1680 struct nameidata *nd)
52094c8a
AV
1681{
1682 if (nd->flags & LOOKUP_RCU) {
7d6beb71 1683 int err = inode_permission(mnt_userns, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
e36cffed 1684 if (err != -ECHILD || !try_to_unlazy(nd))
52094c8a 1685 return err;
52094c8a 1686 }
ba73d987 1687 return inode_permission(mnt_userns, nd->inode, MAY_EXEC);
52094c8a
AV
1688}
1689
49055906
AV
1690static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1691{
49055906
AV
1692 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
1693 return -ELOOP;
4542576b
AV
1694
1695 if (likely(nd->depth != EMBEDDED_LEVELS))
1696 return 0;
1697 if (likely(nd->stack != nd->internal))
1698 return 0;
60ef60c7 1699 if (likely(nd_alloc_stack(nd)))
49055906 1700 return 0;
60ef60c7
AV
1701
1702 if (nd->flags & LOOKUP_RCU) {
1703 // we need to grab link before we do unlazy. And we can't skip
1704 // unlazy even if we fail to grab the link - cleanup needs it
49055906 1705 bool grabbed_link = legitimize_path(nd, link, seq);
60ef60c7 1706
e36cffed 1707 if (!try_to_unlazy(nd) != 0 || !grabbed_link)
60ef60c7
AV
1708 return -ECHILD;
1709
1710 if (nd_alloc_stack(nd))
1711 return 0;
49055906 1712 }
60ef60c7 1713 return -ENOMEM;
49055906
AV
1714}
1715
b1a81972
AV
1716enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
1717
06708adb 1718static const char *pick_link(struct nameidata *nd, struct path *link,
b1a81972 1719 struct inode *inode, unsigned seq, int flags)
d63ff28f 1720{
1cf2665b 1721 struct saved *last;
ad6cc4c3 1722 const char *res;
49055906 1723 int error = reserve_stack(nd, link, seq);
ad6cc4c3 1724
626de996 1725 if (unlikely(error)) {
49055906 1726 if (!(nd->flags & LOOKUP_RCU))
bc40aee0 1727 path_put(link);
49055906 1728 return ERR_PTR(error);
626de996 1729 }
ab104923 1730 last = nd->stack + nd->depth++;
1cf2665b 1731 last->link = *link;
fceef393 1732 clear_delayed_call(&last->done);
0450b2d1 1733 last->seq = seq;
ad6cc4c3 1734
b1a81972 1735 if (flags & WALK_TRAILING) {
ad6cc4c3
AV
1736 error = may_follow_link(nd, inode);
1737 if (unlikely(error))
1738 return ERR_PTR(error);
1739 }
1740
dab741e0
MN
1741 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1742 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
ad6cc4c3
AV
1743 return ERR_PTR(-ELOOP);
1744
1745 if (!(nd->flags & LOOKUP_RCU)) {
1746 touch_atime(&last->link);
1747 cond_resched();
1748 } else if (atime_needs_update(&last->link, inode)) {
e36cffed 1749 if (!try_to_unlazy(nd))
ad6cc4c3
AV
1750 return ERR_PTR(-ECHILD);
1751 touch_atime(&last->link);
1752 }
1753
1754 error = security_inode_follow_link(link->dentry, inode,
1755 nd->flags & LOOKUP_RCU);
1756 if (unlikely(error))
1757 return ERR_PTR(error);
1758
ad6cc4c3
AV
1759 res = READ_ONCE(inode->i_link);
1760 if (!res) {
1761 const char * (*get)(struct dentry *, struct inode *,
1762 struct delayed_call *);
1763 get = inode->i_op->get_link;
1764 if (nd->flags & LOOKUP_RCU) {
1765 res = get(NULL, inode, &last->done);
e36cffed 1766 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
ad6cc4c3 1767 res = get(link->dentry, inode, &last->done);
ad6cc4c3
AV
1768 } else {
1769 res = get(link->dentry, inode, &last->done);
1770 }
1771 if (!res)
1772 goto all_done;
1773 if (IS_ERR(res))
1774 return res;
1775 }
1776 if (*res == '/') {
1777 error = nd_jump_root(nd);
1778 if (unlikely(error))
1779 return ERR_PTR(error);
1780 while (unlikely(*++res == '/'))
1781 ;
1782 }
1783 if (*res)
1784 return res;
1785all_done: // pure jump
1786 put_link(nd);
1787 return NULL;
d63ff28f
AV
1788}
1789
3ddcd056
LT
1790/*
1791 * Do we need to follow links? We _really_ want to be able
1792 * to do this check without having to look at inode->i_op,
1793 * so we keep a cache of "no, this doesn't need follow_link"
1794 * for the common case.
1795 */
b0417d2c 1796static const char *step_into(struct nameidata *nd, int flags,
cbae4d12 1797 struct dentry *dentry, struct inode *inode, unsigned seq)
3ddcd056 1798{
cbae4d12
AV
1799 struct path path;
1800 int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1801
1802 if (err < 0)
b0417d2c 1803 return ERR_PTR(err);
cbae4d12 1804 if (likely(!d_is_symlink(path.dentry)) ||
8c4efe22 1805 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
aca2903e 1806 (flags & WALK_NOFOLLOW)) {
8f64fb1c 1807 /* not a symlink or should not follow */
c99687a0
AV
1808 if (!(nd->flags & LOOKUP_RCU)) {
1809 dput(nd->path.dentry);
1810 if (nd->path.mnt != path.mnt)
1811 mntput(nd->path.mnt);
1812 }
1813 nd->path = path;
8f64fb1c
AV
1814 nd->inode = inode;
1815 nd->seq = seq;
b0417d2c 1816 return NULL;
8f64fb1c 1817 }
a7f77542 1818 if (nd->flags & LOOKUP_RCU) {
84f0cd9e 1819 /* make sure that d_is_symlink above matches inode */
cbae4d12 1820 if (read_seqcount_retry(&path.dentry->d_seq, seq))
b0417d2c 1821 return ERR_PTR(-ECHILD);
84f0cd9e
AV
1822 } else {
1823 if (path.mnt == nd->path.mnt)
1824 mntget(path.mnt);
a7f77542 1825 }
b1a81972 1826 return pick_link(nd, &path, inode, seq, flags);
3ddcd056
LT
1827}
1828
c2df1968
AV
1829static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1830 struct inode **inodep,
1831 unsigned *seqp)
957dd41d 1832{
12487f30 1833 struct dentry *parent, *old;
957dd41d 1834
12487f30
AV
1835 if (path_equal(&nd->path, &nd->root))
1836 goto in_root;
1837 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
7ef482fa 1838 struct path path;
efe772d6 1839 unsigned seq;
7ef482fa
AV
1840 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
1841 &nd->root, &path, &seq))
1842 goto in_root;
efe772d6
AV
1843 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1844 return ERR_PTR(-ECHILD);
1845 nd->path = path;
1846 nd->inode = path.dentry->d_inode;
1847 nd->seq = seq;
1848 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1849 return ERR_PTR(-ECHILD);
1850 /* we know that mountpoint was pinned */
957dd41d 1851 }
12487f30
AV
1852 old = nd->path.dentry;
1853 parent = old->d_parent;
1854 *inodep = parent->d_inode;
1855 *seqp = read_seqcount_begin(&parent->d_seq);
1856 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1857 return ERR_PTR(-ECHILD);
1858 if (unlikely(!path_connected(nd->path.mnt, parent)))
1859 return ERR_PTR(-ECHILD);
1860 return parent;
1861in_root:
efe772d6
AV
1862 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1863 return ERR_PTR(-ECHILD);
c2df1968
AV
1864 if (unlikely(nd->flags & LOOKUP_BENEATH))
1865 return ERR_PTR(-ECHILD);
1866 return NULL;
957dd41d
AV
1867}
1868
c2df1968
AV
1869static struct dentry *follow_dotdot(struct nameidata *nd,
1870 struct inode **inodep,
1871 unsigned *seqp)
957dd41d 1872{
12487f30
AV
1873 struct dentry *parent;
1874
1875 if (path_equal(&nd->path, &nd->root))
1876 goto in_root;
1877 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
2aa38470
AV
1878 struct path path;
1879
1880 if (!choose_mountpoint(real_mount(nd->path.mnt),
1881 &nd->root, &path))
1882 goto in_root;
165200d6
AV
1883 path_put(&nd->path);
1884 nd->path = path;
2aa38470 1885 nd->inode = path.dentry->d_inode;
165200d6
AV
1886 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1887 return ERR_PTR(-EXDEV);
957dd41d 1888 }
12487f30
AV
1889 /* rare case of legitimate dget_parent()... */
1890 parent = dget_parent(nd->path.dentry);
1891 if (unlikely(!path_connected(nd->path.mnt, parent))) {
1892 dput(parent);
1893 return ERR_PTR(-ENOENT);
1894 }
1895 *seqp = 0;
1896 *inodep = parent->d_inode;
1897 return parent;
1898
1899in_root:
c2df1968
AV
1900 if (unlikely(nd->flags & LOOKUP_BENEATH))
1901 return ERR_PTR(-EXDEV);
1902 dget(nd->path.dentry);
1903 return NULL;
957dd41d
AV
1904}
1905
7521f22b 1906static const char *handle_dots(struct nameidata *nd, int type)
957dd41d
AV
1907{
1908 if (type == LAST_DOTDOT) {
7521f22b 1909 const char *error = NULL;
c2df1968
AV
1910 struct dentry *parent;
1911 struct inode *inode;
1912 unsigned seq;
957dd41d
AV
1913
1914 if (!nd->root.mnt) {
7521f22b 1915 error = ERR_PTR(set_root(nd));
957dd41d
AV
1916 if (error)
1917 return error;
1918 }
1919 if (nd->flags & LOOKUP_RCU)
c2df1968 1920 parent = follow_dotdot_rcu(nd, &inode, &seq);
957dd41d 1921 else
c2df1968
AV
1922 parent = follow_dotdot(nd, &inode, &seq);
1923 if (IS_ERR(parent))
1924 return ERR_CAST(parent);
1925 if (unlikely(!parent))
1926 error = step_into(nd, WALK_NOFOLLOW,
1927 nd->path.dentry, nd->inode, nd->seq);
1928 else
1929 error = step_into(nd, WALK_NOFOLLOW,
1930 parent, inode, seq);
1931 if (unlikely(error))
957dd41d
AV
1932 return error;
1933
1934 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1935 /*
1936 * If there was a racing rename or mount along our
1937 * path, then we can't be sure that ".." hasn't jumped
1938 * above nd->root (and so userspace should retry or use
1939 * some fallback).
1940 */
1941 smp_rmb();
1942 if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
7521f22b 1943 return ERR_PTR(-EAGAIN);
957dd41d 1944 if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
7521f22b 1945 return ERR_PTR(-EAGAIN);
957dd41d
AV
1946 }
1947 }
7521f22b 1948 return NULL;
957dd41d
AV
1949}
1950
92d27016 1951static const char *walk_component(struct nameidata *nd, int flags)
ce57dfc1 1952{
db3c9ade 1953 struct dentry *dentry;
ce57dfc1 1954 struct inode *inode;
254cf582 1955 unsigned seq;
ce57dfc1
AV
1956 /*
1957 * "." and ".." are special - ".." especially so because it has
1958 * to be able to know about the current root directory and
1959 * parent relationships.
1960 */
4693a547 1961 if (unlikely(nd->last_type != LAST_NORM)) {
1c4ff1a8 1962 if (!(flags & WALK_MORE) && nd->depth)
4693a547 1963 put_link(nd);
7521f22b 1964 return handle_dots(nd, nd->last_type);
4693a547 1965 }
20e34357
AV
1966 dentry = lookup_fast(nd, &inode, &seq);
1967 if (IS_ERR(dentry))
92d27016 1968 return ERR_CAST(dentry);
20e34357 1969 if (unlikely(!dentry)) {
db3c9ade
AV
1970 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1971 if (IS_ERR(dentry))
92d27016 1972 return ERR_CAST(dentry);
ce57dfc1 1973 }
56676ec3
AV
1974 if (!(flags & WALK_MORE) && nd->depth)
1975 put_link(nd);
b0417d2c 1976 return step_into(nd, flags, dentry, inode, seq);
ce57dfc1
AV
1977}
1978
bfcfaa77
LT
1979/*
1980 * We can do the critical dentry name comparison and hashing
1981 * operations one word at a time, but we are limited to:
1982 *
1983 * - Architectures with fast unaligned word accesses. We could
1984 * do a "get_unaligned()" if this helps and is sufficiently
1985 * fast.
1986 *
bfcfaa77
LT
1987 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1988 * do not trap on the (extremely unlikely) case of a page
1989 * crossing operation.
1990 *
1991 * - Furthermore, we need an efficient 64-bit compile for the
1992 * 64-bit case in order to generate the "number of bytes in
1993 * the final mask". Again, that could be replaced with a
1994 * efficient population count instruction or similar.
1995 */
1996#ifdef CONFIG_DCACHE_WORD_ACCESS
1997
f68e556e 1998#include <asm/word-at-a-time.h>
bfcfaa77 1999
468a9428 2000#ifdef HASH_MIX
bfcfaa77 2001
468a9428 2002/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
bfcfaa77 2003
468a9428 2004#elif defined(CONFIG_64BIT)
0fed3ac8 2005/*
2a18da7a
GS
2006 * Register pressure in the mixing function is an issue, particularly
2007 * on 32-bit x86, but almost any function requires one state value and
2008 * one temporary. Instead, use a function designed for two state values
2009 * and no temporaries.
2010 *
2011 * This function cannot create a collision in only two iterations, so
2012 * we have two iterations to achieve avalanche. In those two iterations,
2013 * we have six layers of mixing, which is enough to spread one bit's
2014 * influence out to 2^6 = 64 state bits.
2015 *
2016 * Rotate constants are scored by considering either 64 one-bit input
2017 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2018 * probability of that delta causing a change to each of the 128 output
2019 * bits, using a sample of random initial states.
2020 *
2021 * The Shannon entropy of the computed probabilities is then summed
2022 * to produce a score. Ideally, any input change has a 50% chance of
2023 * toggling any given output bit.
2024 *
2025 * Mixing scores (in bits) for (12,45):
2026 * Input delta: 1-bit 2-bit
2027 * 1 round: 713.3 42542.6
2028 * 2 rounds: 2753.7 140389.8
2029 * 3 rounds: 5954.1 233458.2
2030 * 4 rounds: 7862.6 256672.2
2031 * Perfect: 8192 258048
2032 * (64*128) (64*63/2 * 128)
0fed3ac8 2033 */
2a18da7a
GS
2034#define HASH_MIX(x, y, a) \
2035 ( x ^= (a), \
2036 y ^= x, x = rol64(x,12),\
2037 x += y, y = rol64(y,45),\
2038 y *= 9 )
bfcfaa77 2039
0fed3ac8 2040/*
2a18da7a
GS
2041 * Fold two longs into one 32-bit hash value. This must be fast, but
2042 * latency isn't quite as critical, as there is a fair bit of additional
2043 * work done before the hash value is used.
0fed3ac8 2044 */
2a18da7a 2045static inline unsigned int fold_hash(unsigned long x, unsigned long y)
0fed3ac8 2046{
2a18da7a
GS
2047 y ^= x * GOLDEN_RATIO_64;
2048 y *= GOLDEN_RATIO_64;
2049 return y >> 32;
0fed3ac8
GS
2050}
2051
bfcfaa77
LT
2052#else /* 32-bit case */
2053
2a18da7a
GS
2054/*
2055 * Mixing scores (in bits) for (7,20):
2056 * Input delta: 1-bit 2-bit
2057 * 1 round: 330.3 9201.6
2058 * 2 rounds: 1246.4 25475.4
2059 * 3 rounds: 1907.1 31295.1
2060 * 4 rounds: 2042.3 31718.6
2061 * Perfect: 2048 31744
2062 * (32*64) (32*31/2 * 64)
2063 */
2064#define HASH_MIX(x, y, a) \
2065 ( x ^= (a), \
2066 y ^= x, x = rol32(x, 7),\
2067 x += y, y = rol32(y,20),\
2068 y *= 9 )
bfcfaa77 2069
2a18da7a 2070static inline unsigned int fold_hash(unsigned long x, unsigned long y)
0fed3ac8 2071{
2a18da7a
GS
2072 /* Use arch-optimized multiply if one exists */
2073 return __hash_32(y ^ __hash_32(x));
0fed3ac8
GS
2074}
2075
bfcfaa77
LT
2076#endif
2077
2a18da7a
GS
2078/*
2079 * Return the hash of a string of known length. This is carfully
2080 * designed to match hash_name(), which is the more critical function.
2081 * In particular, we must end by hashing a final word containing 0..7
2082 * payload bytes, to match the way that hash_name() iterates until it
2083 * finds the delimiter after the name.
2084 */
8387ff25 2085unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
bfcfaa77 2086{
8387ff25 2087 unsigned long a, x = 0, y = (unsigned long)salt;
bfcfaa77
LT
2088
2089 for (;;) {
fcfd2fbf
GS
2090 if (!len)
2091 goto done;
e419b4cc 2092 a = load_unaligned_zeropad(name);
bfcfaa77
LT
2093 if (len < sizeof(unsigned long))
2094 break;
2a18da7a 2095 HASH_MIX(x, y, a);
bfcfaa77
LT
2096 name += sizeof(unsigned long);
2097 len -= sizeof(unsigned long);
bfcfaa77 2098 }
2a18da7a 2099 x ^= a & bytemask_from_count(len);
bfcfaa77 2100done:
2a18da7a 2101 return fold_hash(x, y);
bfcfaa77
LT
2102}
2103EXPORT_SYMBOL(full_name_hash);
2104
fcfd2fbf 2105/* Return the "hash_len" (hash and length) of a null-terminated string */
8387ff25 2106u64 hashlen_string(const void *salt, const char *name)
fcfd2fbf 2107{
8387ff25
LT
2108 unsigned long a = 0, x = 0, y = (unsigned long)salt;
2109 unsigned long adata, mask, len;
fcfd2fbf
GS
2110 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2111
8387ff25
LT
2112 len = 0;
2113 goto inside;
2114
fcfd2fbf 2115 do {
2a18da7a 2116 HASH_MIX(x, y, a);
fcfd2fbf 2117 len += sizeof(unsigned long);
8387ff25 2118inside:
fcfd2fbf
GS
2119 a = load_unaligned_zeropad(name+len);
2120 } while (!has_zero(a, &adata, &constants));
2121
2122 adata = prep_zero_mask(a, adata, &constants);
2123 mask = create_zero_mask(adata);
2a18da7a 2124 x ^= a & zero_bytemask(mask);
fcfd2fbf 2125
2a18da7a 2126 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
fcfd2fbf
GS
2127}
2128EXPORT_SYMBOL(hashlen_string);
2129
bfcfaa77
LT
2130/*
2131 * Calculate the length and hash of the path component, and
d6bb3e90 2132 * return the "hash_len" as the result.
bfcfaa77 2133 */
8387ff25 2134static inline u64 hash_name(const void *salt, const char *name)
bfcfaa77 2135{
8387ff25
LT
2136 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
2137 unsigned long adata, bdata, mask, len;
36126f8f 2138 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
bfcfaa77 2139
8387ff25
LT
2140 len = 0;
2141 goto inside;
2142
bfcfaa77 2143 do {
2a18da7a 2144 HASH_MIX(x, y, a);
bfcfaa77 2145 len += sizeof(unsigned long);
8387ff25 2146inside:
e419b4cc 2147 a = load_unaligned_zeropad(name+len);
36126f8f
LT
2148 b = a ^ REPEAT_BYTE('/');
2149 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
2150
2151 adata = prep_zero_mask(a, adata, &constants);
2152 bdata = prep_zero_mask(b, bdata, &constants);
36126f8f 2153 mask = create_zero_mask(adata | bdata);
2a18da7a 2154 x ^= a & zero_bytemask(mask);
36126f8f 2155
2a18da7a 2156 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
bfcfaa77
LT
2157}
2158
2a18da7a 2159#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
bfcfaa77 2160
fcfd2fbf 2161/* Return the hash of a string of known length */
8387ff25 2162unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
0145acc2 2163{
8387ff25 2164 unsigned long hash = init_name_hash(salt);
0145acc2 2165 while (len--)
fcfd2fbf 2166 hash = partial_name_hash((unsigned char)*name++, hash);
0145acc2
LT
2167 return end_name_hash(hash);
2168}
ae942ae7 2169EXPORT_SYMBOL(full_name_hash);
0145acc2 2170
fcfd2fbf 2171/* Return the "hash_len" (hash and length) of a null-terminated string */
8387ff25 2172u64 hashlen_string(const void *salt, const char *name)
fcfd2fbf 2173{
8387ff25 2174 unsigned long hash = init_name_hash(salt);
fcfd2fbf
GS
2175 unsigned long len = 0, c;
2176
2177 c = (unsigned char)*name;
e0ab7af9 2178 while (c) {
fcfd2fbf
GS
2179 len++;
2180 hash = partial_name_hash(c, hash);
2181 c = (unsigned char)name[len];
e0ab7af9 2182 }
fcfd2fbf
GS
2183 return hashlen_create(end_name_hash(hash), len);
2184}
f2a031b6 2185EXPORT_SYMBOL(hashlen_string);
fcfd2fbf 2186
200e9ef7
LT
2187/*
2188 * We know there's a real path component here of at least
2189 * one character.
2190 */
8387ff25 2191static inline u64 hash_name(const void *salt, const char *name)
200e9ef7 2192{
8387ff25 2193 unsigned long hash = init_name_hash(salt);
200e9ef7
LT
2194 unsigned long len = 0, c;
2195
2196 c = (unsigned char)*name;
2197 do {
2198 len++;
2199 hash = partial_name_hash(c, hash);
2200 c = (unsigned char)name[len];
2201 } while (c && c != '/');
d6bb3e90 2202 return hashlen_create(end_name_hash(hash), len);
200e9ef7
LT
2203}
2204
bfcfaa77
LT
2205#endif
2206
1da177e4
LT
2207/*
2208 * Name resolution.
ea3834d9
PM
2209 * This is the basic name resolution function, turning a pathname into
2210 * the final dentry. We expect 'base' to be positive and a directory.
1da177e4 2211 *
ea3834d9
PM
2212 * Returns 0 and nd will have valid dentry and mnt on success.
2213 * Returns error and drops reference to input namei data on failure.
1da177e4 2214 */
6de88d72 2215static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4 2216{
d8d4611a 2217 int depth = 0; // depth <= nd->depth
1da177e4 2218 int err;
32cd7468 2219
b4c03536 2220 nd->last_type = LAST_ROOT;
c108837e 2221 nd->flags |= LOOKUP_PARENT;
9b5858e9
AV
2222 if (IS_ERR(name))
2223 return PTR_ERR(name);
1da177e4
LT
2224 while (*name=='/')
2225 name++;
1a97d899
AV
2226 if (!*name) {
2227 nd->dir_mode = 0; // short-circuit the 'hardening' idiocy
9e18f10a 2228 return 0;
1a97d899 2229 }
1da177e4 2230
1da177e4
LT
2231 /* At this point we know we have a real path component. */
2232 for(;;) {
549c7297 2233 struct user_namespace *mnt_userns;
92d27016 2234 const char *link;
d6bb3e90 2235 u64 hash_len;
fe479a58 2236 int type;
1da177e4 2237
549c7297
CB
2238 mnt_userns = mnt_user_ns(nd->path.mnt);
2239 err = may_lookup(mnt_userns, nd);
2a18da7a 2240 if (err)
3595e234 2241 return err;
1da177e4 2242
8387ff25 2243 hash_len = hash_name(nd->path.dentry, name);
1da177e4 2244
fe479a58 2245 type = LAST_NORM;
d6bb3e90 2246 if (name[0] == '.') switch (hashlen_len(hash_len)) {
fe479a58 2247 case 2:
200e9ef7 2248 if (name[1] == '.') {
fe479a58 2249 type = LAST_DOTDOT;
bcba1e7d 2250 nd->state |= ND_JUMPED;
16c2cd71 2251 }
fe479a58
AV
2252 break;
2253 case 1:
2254 type = LAST_DOT;
2255 }
5a202bcd
AV
2256 if (likely(type == LAST_NORM)) {
2257 struct dentry *parent = nd->path.dentry;
bcba1e7d 2258 nd->state &= ~ND_JUMPED;
5a202bcd 2259 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
a060dc50 2260 struct qstr this = { { .hash_len = hash_len }, .name = name };
da53be12 2261 err = parent->d_op->d_hash(parent, &this);
5a202bcd 2262 if (err < 0)
3595e234 2263 return err;
d6bb3e90
LT
2264 hash_len = this.hash_len;
2265 name = this.name;
5a202bcd
AV
2266 }
2267 }
fe479a58 2268
d6bb3e90
LT
2269 nd->last.hash_len = hash_len;
2270 nd->last.name = name;
5f4a6a69
AV
2271 nd->last_type = type;
2272
d6bb3e90
LT
2273 name += hashlen_len(hash_len);
2274 if (!*name)
bdf6cbf1 2275 goto OK;
200e9ef7
LT
2276 /*
2277 * If it wasn't NUL, we know it was '/'. Skip that
2278 * slash, and continue until no more slashes.
2279 */
2280 do {
d6bb3e90
LT
2281 name++;
2282 } while (unlikely(*name == '/'));
8620c238
AV
2283 if (unlikely(!*name)) {
2284OK:
d8d4611a 2285 /* pathname or trailing symlink, done */
c108837e 2286 if (!depth) {
549c7297 2287 nd->dir_uid = i_uid_into_mnt(mnt_userns, nd->inode);
0f705953 2288 nd->dir_mode = nd->inode->i_mode;
c108837e 2289 nd->flags &= ~LOOKUP_PARENT;
8620c238 2290 return 0;
c108837e 2291 }
8620c238 2292 /* last component of nested symlink */
d8d4611a 2293 name = nd->stack[--depth].name;
8c4efe22 2294 link = walk_component(nd, 0);
1c4ff1a8
AV
2295 } else {
2296 /* not the last component */
8c4efe22 2297 link = walk_component(nd, WALK_MORE);
8620c238 2298 }
92d27016
AV
2299 if (unlikely(link)) {
2300 if (IS_ERR(link))
2301 return PTR_ERR(link);
2302 /* a symlink to follow */
d8d4611a 2303 nd->stack[depth++].name = name;
92d27016
AV
2304 name = link;
2305 continue;
31e6b01f 2306 }
97242f99
AV
2307 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2308 if (nd->flags & LOOKUP_RCU) {
e36cffed 2309 if (!try_to_unlazy(nd))
97242f99
AV
2310 return -ECHILD;
2311 }
3595e234 2312 return -ENOTDIR;
97242f99 2313 }
1da177e4 2314 }
1da177e4
LT
2315}
2316
edc2b1da 2317/* must be paired with terminate_walk() */
c8a53ee5 2318static const char *path_init(struct nameidata *nd, unsigned flags)
31e6b01f 2319{
740a1678 2320 int error;
c8a53ee5 2321 const char *s = nd->name->name;
31e6b01f 2322
6c6ec2b0
JA
2323 /* LOOKUP_CACHED requires RCU, ask caller to retry */
2324 if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED)
2325 return ERR_PTR(-EAGAIN);
2326
c0eb027e
LT
2327 if (!*s)
2328 flags &= ~LOOKUP_RCU;
edc2b1da
AV
2329 if (flags & LOOKUP_RCU)
2330 rcu_read_lock();
c0eb027e 2331
bcba1e7d
AV
2332 nd->flags = flags;
2333 nd->state |= ND_JUMPED;
ab87f9a5
AS
2334
2335 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2336 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2337 smp_rmb();
2338
bcba1e7d 2339 if (nd->state & ND_ROOT_PRESET) {
b18825a7
DH
2340 struct dentry *root = nd->root.dentry;
2341 struct inode *inode = root->d_inode;
93893862
AV
2342 if (*s && unlikely(!d_can_lookup(root)))
2343 return ERR_PTR(-ENOTDIR);
5b6ca027
AV
2344 nd->path = nd->root;
2345 nd->inode = inode;
2346 if (flags & LOOKUP_RCU) {
ab87f9a5 2347 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
8f47a016 2348 nd->root_seq = nd->seq;
5b6ca027
AV
2349 } else {
2350 path_get(&nd->path);
2351 }
368ee9ba 2352 return s;
5b6ca027
AV
2353 }
2354
31e6b01f 2355 nd->root.mnt = NULL;
31e6b01f 2356
8db52c7e
AS
2357 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2358 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
740a1678
AS
2359 error = nd_jump_root(nd);
2360 if (unlikely(error))
2361 return ERR_PTR(error);
2362 return s;
8db52c7e
AS
2363 }
2364
2365 /* Relative pathname -- get the starting-point it is relative to. */
2366 if (nd->dfd == AT_FDCWD) {
e41f7d4e
AV
2367 if (flags & LOOKUP_RCU) {
2368 struct fs_struct *fs = current->fs;
2369 unsigned seq;
31e6b01f 2370
e41f7d4e
AV
2371 do {
2372 seq = read_seqcount_begin(&fs->seq);
2373 nd->path = fs->pwd;
ef55d917 2374 nd->inode = nd->path.dentry->d_inode;
e41f7d4e
AV
2375 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2376 } while (read_seqcount_retry(&fs->seq, seq));
2377 } else {
2378 get_fs_pwd(current->fs, &nd->path);
ef55d917 2379 nd->inode = nd->path.dentry->d_inode;
e41f7d4e 2380 }
31e6b01f 2381 } else {
582aa64a 2382 /* Caller must check execute permissions on the starting path component */
c8a53ee5 2383 struct fd f = fdget_raw(nd->dfd);
31e6b01f
NP
2384 struct dentry *dentry;
2385
2903ff01 2386 if (!f.file)
368ee9ba 2387 return ERR_PTR(-EBADF);
31e6b01f 2388
2903ff01 2389 dentry = f.file->f_path.dentry;
31e6b01f 2390
edc2b1da
AV
2391 if (*s && unlikely(!d_can_lookup(dentry))) {
2392 fdput(f);
2393 return ERR_PTR(-ENOTDIR);
f52e0c11 2394 }
31e6b01f 2395
2903ff01 2396 nd->path = f.file->f_path;
e41f7d4e 2397 if (flags & LOOKUP_RCU) {
34a26b99
AV
2398 nd->inode = nd->path.dentry->d_inode;
2399 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
e41f7d4e 2400 } else {
2903ff01 2401 path_get(&nd->path);
34a26b99 2402 nd->inode = nd->path.dentry->d_inode;
e41f7d4e 2403 }
34a26b99 2404 fdput(f);
31e6b01f 2405 }
8db52c7e 2406
adb21d2b
AS
2407 /* For scoped-lookups we need to set the root to the dirfd as well. */
2408 if (flags & LOOKUP_IS_SCOPED) {
2409 nd->root = nd->path;
2410 if (flags & LOOKUP_RCU) {
2411 nd->root_seq = nd->seq;
2412 } else {
2413 path_get(&nd->root);
bcba1e7d 2414 nd->state |= ND_ROOT_GRABBED;
adb21d2b
AS
2415 }
2416 }
2417 return s;
9b4a9b14
AV
2418}
2419
1ccac622 2420static inline const char *lookup_last(struct nameidata *nd)
bd92d7fe
AV
2421{
2422 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2423 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2424
c108837e 2425 return walk_component(nd, WALK_TRAILING);
bd92d7fe
AV
2426}
2427
4f757f3c
AV
2428static int handle_lookup_down(struct nameidata *nd)
2429{
c153007b 2430 if (!(nd->flags & LOOKUP_RCU))
db3c9ade 2431 dget(nd->path.dentry);
b0417d2c
AV
2432 return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2433 nd->path.dentry, nd->inode, nd->seq));
4f757f3c
AV
2434}
2435
9b4a9b14 2436/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5 2437static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
9b4a9b14 2438{
c8a53ee5 2439 const char *s = path_init(nd, flags);
bd92d7fe 2440 int err;
31e6b01f 2441
9b5858e9 2442 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
4f757f3c 2443 err = handle_lookup_down(nd);
5f336e72
AV
2444 if (unlikely(err < 0))
2445 s = ERR_PTR(err);
4f757f3c
AV
2446 }
2447
1ccac622
AV
2448 while (!(err = link_path_walk(s, nd)) &&
2449 (s = lookup_last(nd)) != NULL)
2450 ;
4f0ed93f
AV
2451 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2452 err = handle_lookup_down(nd);
bcba1e7d 2453 nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please...
4f0ed93f 2454 }
9f1fafee
AV
2455 if (!err)
2456 err = complete_walk(nd);
bd92d7fe 2457
deb106c6
AV
2458 if (!err && nd->flags & LOOKUP_DIRECTORY)
2459 if (!d_can_lookup(nd->path.dentry))
bd23a539 2460 err = -ENOTDIR;
625b6d10
AV
2461 if (!err) {
2462 *path = nd->path;
2463 nd->path.mnt = NULL;
2464 nd->path.dentry = NULL;
2465 }
2466 terminate_walk(nd);
bd92d7fe 2467 return err;
ee0827cd 2468}
31e6b01f 2469
020250f3 2470static int __filename_lookup(int dfd, struct filename *name, unsigned flags,
31d921c7 2471 struct path *path, struct path *root)
ee0827cd 2472{
894bc8c4 2473 int retval;
9883d185 2474 struct nameidata nd;
abc9f5be
AV
2475 if (IS_ERR(name))
2476 return PTR_ERR(name);
06422964 2477 set_nameidata(&nd, dfd, name, root);
c8a53ee5 2478 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
ee0827cd 2479 if (unlikely(retval == -ECHILD))
c8a53ee5 2480 retval = path_lookupat(&nd, flags, path);
ee0827cd 2481 if (unlikely(retval == -ESTALE))
c8a53ee5 2482 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
31e6b01f 2483
f78570dd 2484 if (likely(!retval))
161aff1d
AV
2485 audit_inode(name, path->dentry,
2486 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
9883d185 2487 restore_nameidata();
020250f3
DK
2488 return retval;
2489}
2490
2491int filename_lookup(int dfd, struct filename *name, unsigned flags,
2492 struct path *path, struct path *root)
2493{
2494 int retval = __filename_lookup(dfd, name, flags, path, root);
2495
e4bd1c1a 2496 putname(name);
170aa3d0 2497 return retval;
1da177e4
LT
2498}
2499
8bcb77fa 2500/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5 2501static int path_parentat(struct nameidata *nd, unsigned flags,
391172c4 2502 struct path *parent)
8bcb77fa 2503{
c8a53ee5 2504 const char *s = path_init(nd, flags);
9b5858e9 2505 int err = link_path_walk(s, nd);
8bcb77fa
AV
2506 if (!err)
2507 err = complete_walk(nd);
391172c4
AV
2508 if (!err) {
2509 *parent = nd->path;
2510 nd->path.mnt = NULL;
2511 nd->path.dentry = NULL;
2512 }
2513 terminate_walk(nd);
8bcb77fa
AV
2514 return err;
2515}
2516
0766ec82 2517/* Note: this does not consume "name" */
c5f563f9 2518static int filename_parentat(int dfd, struct filename *name,
0766ec82
SB
2519 unsigned int flags, struct path *parent,
2520 struct qstr *last, int *type)
8bcb77fa
AV
2521{
2522 int retval;
9883d185 2523 struct nameidata nd;
8bcb77fa 2524
5c31b6ce 2525 if (IS_ERR(name))
0ee50b47 2526 return PTR_ERR(name);
06422964 2527 set_nameidata(&nd, dfd, name, NULL);
c8a53ee5 2528 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
8bcb77fa 2529 if (unlikely(retval == -ECHILD))
c8a53ee5 2530 retval = path_parentat(&nd, flags, parent);
8bcb77fa 2531 if (unlikely(retval == -ESTALE))
c8a53ee5 2532 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
391172c4
AV
2533 if (likely(!retval)) {
2534 *last = nd.last;
2535 *type = nd.last_type;
c9b07eab 2536 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
391172c4 2537 }
9883d185 2538 restore_nameidata();
0ee50b47
DK
2539 return retval;
2540}
2541
79714f72 2542/* does lookup, returns the object with parent locked */
0766ec82 2543static struct dentry *__kern_path_locked(struct filename *name, struct path *path)
5590ff0d 2544{
5c31b6ce 2545 struct dentry *d;
391172c4 2546 struct qstr last;
0ee50b47 2547 int type, error;
51689104 2548
c5f563f9 2549 error = filename_parentat(AT_FDCWD, name, 0, path, &last, &type);
0ee50b47
DK
2550 if (error)
2551 return ERR_PTR(error);
5c31b6ce 2552 if (unlikely(type != LAST_NORM)) {
391172c4 2553 path_put(path);
5c31b6ce 2554 return ERR_PTR(-EINVAL);
79714f72 2555 }
5955102c 2556 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
391172c4 2557 d = __lookup_hash(&last, path->dentry, 0);
79714f72 2558 if (IS_ERR(d)) {
5955102c 2559 inode_unlock(path->dentry->d_inode);
391172c4 2560 path_put(path);
79714f72 2561 }
79714f72 2562 return d;
5590ff0d
UD
2563}
2564
0766ec82
SB
2565struct dentry *kern_path_locked(const char *name, struct path *path)
2566{
2567 struct filename *filename = getname_kernel(name);
2568 struct dentry *res = __kern_path_locked(filename, path);
2569
2570 putname(filename);
2571 return res;
2572}
2573
d1811465
AV
2574int kern_path(const char *name, unsigned int flags, struct path *path)
2575{
abc9f5be
AV
2576 return filename_lookup(AT_FDCWD, getname_kernel(name),
2577 flags, path, NULL);
d1811465 2578}
4d359507 2579EXPORT_SYMBOL(kern_path);
d1811465 2580
16f18200
JJS
2581/**
2582 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2583 * @dentry: pointer to dentry of the base directory
2584 * @mnt: pointer to vfs mount of the base directory
2585 * @name: pointer to file name
2586 * @flags: lookup flags
e0a01249 2587 * @path: pointer to struct path to fill
16f18200
JJS
2588 */
2589int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2590 const char *name, unsigned int flags,
e0a01249 2591 struct path *path)
16f18200 2592{
9ad1aaa6 2593 struct path root = {.mnt = mnt, .dentry = dentry};
9ad1aaa6 2594 /* the first argument of filename_lookup() is ignored with root */
abc9f5be
AV
2595 return filename_lookup(AT_FDCWD, getname_kernel(name),
2596 flags , path, &root);
16f18200 2597}
4d359507 2598EXPORT_SYMBOL(vfs_path_lookup);
16f18200 2599
c2fd68b6
CB
2600static int lookup_one_common(struct user_namespace *mnt_userns,
2601 const char *name, struct dentry *base, int len,
2602 struct qstr *this)
057f6c01 2603{
3c95f0dc
AV
2604 this->name = name;
2605 this->len = len;
2606 this->hash = full_name_hash(base, name, len);
6a96ba54 2607 if (!len)
3c95f0dc 2608 return -EACCES;
6a96ba54 2609
21d8a15a
AV
2610 if (unlikely(name[0] == '.')) {
2611 if (len < 2 || (len == 2 && name[1] == '.'))
3c95f0dc 2612 return -EACCES;
21d8a15a
AV
2613 }
2614
6a96ba54 2615 while (len--) {
3c95f0dc 2616 unsigned int c = *(const unsigned char *)name++;
6a96ba54 2617 if (c == '/' || c == '\0')
3c95f0dc 2618 return -EACCES;
6a96ba54 2619 }
5a202bcd
AV
2620 /*
2621 * See if the low-level filesystem might want
2622 * to use its own hash..
2623 */
2624 if (base->d_flags & DCACHE_OP_HASH) {
3c95f0dc 2625 int err = base->d_op->d_hash(base, this);
5a202bcd 2626 if (err < 0)
3c95f0dc 2627 return err;
5a202bcd 2628 }
eead1911 2629
c2fd68b6 2630 return inode_permission(mnt_userns, base->d_inode, MAY_EXEC);
3c95f0dc
AV
2631}
2632
0da0b7fd
DH
2633/**
2634 * try_lookup_one_len - filesystem helper to lookup single pathname component
2635 * @name: pathname component to lookup
2636 * @base: base directory to lookup from
2637 * @len: maximum length @len should be interpreted to
2638 *
2639 * Look up a dentry by name in the dcache, returning NULL if it does not
2640 * currently exist. The function does not try to create a dentry.
2641 *
2642 * Note that this routine is purely a helper for filesystem usage and should
2643 * not be called by generic code.
2644 *
2645 * The caller must hold base->i_mutex.
2646 */
2647struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2648{
2649 struct qstr this;
2650 int err;
2651
2652 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2653
c2fd68b6 2654 err = lookup_one_common(&init_user_ns, name, base, len, &this);
0da0b7fd
DH
2655 if (err)
2656 return ERR_PTR(err);
2657
2658 return lookup_dcache(&this, base, 0);
2659}
2660EXPORT_SYMBOL(try_lookup_one_len);
2661
3c95f0dc
AV
2662/**
2663 * lookup_one_len - filesystem helper to lookup single pathname component
2664 * @name: pathname component to lookup
2665 * @base: base directory to lookup from
2666 * @len: maximum length @len should be interpreted to
2667 *
2668 * Note that this routine is purely a helper for filesystem usage and should
2669 * not be called by generic code.
2670 *
2671 * The caller must hold base->i_mutex.
2672 */
2673struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2674{
8613a209 2675 struct dentry *dentry;
3c95f0dc
AV
2676 struct qstr this;
2677 int err;
2678
2679 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2680
c2fd68b6 2681 err = lookup_one_common(&init_user_ns, name, base, len, &this);
cda309de
MS
2682 if (err)
2683 return ERR_PTR(err);
2684
8613a209
AV
2685 dentry = lookup_dcache(&this, base, 0);
2686 return dentry ? dentry : __lookup_slow(&this, base, 0);
057f6c01 2687}
4d359507 2688EXPORT_SYMBOL(lookup_one_len);
057f6c01 2689
c2fd68b6
CB
2690/**
2691 * lookup_one - filesystem helper to lookup single pathname component
2692 * @mnt_userns: user namespace of the mount the lookup is performed from
2693 * @name: pathname component to lookup
2694 * @base: base directory to lookup from
2695 * @len: maximum length @len should be interpreted to
2696 *
2697 * Note that this routine is purely a helper for filesystem usage and should
2698 * not be called by generic code.
2699 *
2700 * The caller must hold base->i_mutex.
2701 */
2702struct dentry *lookup_one(struct user_namespace *mnt_userns, const char *name,
2703 struct dentry *base, int len)
2704{
2705 struct dentry *dentry;
2706 struct qstr this;
2707 int err;
2708
2709 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2710
2711 err = lookup_one_common(mnt_userns, name, base, len, &this);
2712 if (err)
2713 return ERR_PTR(err);
2714
2715 dentry = lookup_dcache(&this, base, 0);
2716 return dentry ? dentry : __lookup_slow(&this, base, 0);
2717}
2718EXPORT_SYMBOL(lookup_one);
2719
bbddca8e
N
2720/**
2721 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2722 * @name: pathname component to lookup
2723 * @base: base directory to lookup from
2724 * @len: maximum length @len should be interpreted to
2725 *
2726 * Note that this routine is purely a helper for filesystem usage and should
2727 * not be called by generic code.
2728 *
2729 * Unlike lookup_one_len, it should be called without the parent
2730 * i_mutex held, and will take the i_mutex itself if necessary.
2731 */
2732struct dentry *lookup_one_len_unlocked(const char *name,
2733 struct dentry *base, int len)
2734{
2735 struct qstr this;
bbddca8e 2736 int err;
20d00ee8 2737 struct dentry *ret;
bbddca8e 2738
c2fd68b6 2739 err = lookup_one_common(&init_user_ns, name, base, len, &this);
bbddca8e
N
2740 if (err)
2741 return ERR_PTR(err);
2742
20d00ee8
LT
2743 ret = lookup_dcache(&this, base, 0);
2744 if (!ret)
2745 ret = lookup_slow(&this, base, 0);
2746 return ret;
bbddca8e
N
2747}
2748EXPORT_SYMBOL(lookup_one_len_unlocked);
2749
6c2d4798
AV
2750/*
2751 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2752 * on negatives. Returns known positive or ERR_PTR(); that's what
2753 * most of the users want. Note that pinned negative with unlocked parent
2754 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2755 * need to be very careful; pinned positives have ->d_inode stable, so
2756 * this one avoids such problems.
2757 */
2758struct dentry *lookup_positive_unlocked(const char *name,
2759 struct dentry *base, int len)
2760{
2761 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
2fa6b1e0 2762 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
6c2d4798
AV
2763 dput(ret);
2764 ret = ERR_PTR(-ENOENT);
2765 }
2766 return ret;
2767}
2768EXPORT_SYMBOL(lookup_positive_unlocked);
2769
eedf265a
EB
2770#ifdef CONFIG_UNIX98_PTYS
2771int path_pts(struct path *path)
2772{
2773 /* Find something mounted on "pts" in the same directory as
2774 * the input path.
2775 */
a6a7eb76
AV
2776 struct dentry *parent = dget_parent(path->dentry);
2777 struct dentry *child;
19f6028a 2778 struct qstr this = QSTR_INIT("pts", 3);
eedf265a 2779
a6a7eb76
AV
2780 if (unlikely(!path_connected(path->mnt, parent))) {
2781 dput(parent);
63b27720 2782 return -ENOENT;
a6a7eb76 2783 }
63b27720
AV
2784 dput(path->dentry);
2785 path->dentry = parent;
eedf265a
EB
2786 child = d_hash_and_lookup(parent, &this);
2787 if (!child)
2788 return -ENOENT;
2789
2790 path->dentry = child;
2791 dput(parent);
19f6028a 2792 follow_down(path);
eedf265a
EB
2793 return 0;
2794}
2795#endif
2796
1fa1e7f6
AW
2797int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2798 struct path *path, int *empty)
1da177e4 2799{
abc9f5be
AV
2800 return filename_lookup(dfd, getname_flags(name, flags, empty),
2801 flags, path, NULL);
1da177e4 2802}
b853a161 2803EXPORT_SYMBOL(user_path_at_empty);
1fa1e7f6 2804
ba73d987
CB
2805int __check_sticky(struct user_namespace *mnt_userns, struct inode *dir,
2806 struct inode *inode)
1da177e4 2807{
8e96e3b7 2808 kuid_t fsuid = current_fsuid();
da9592ed 2809
ba73d987 2810 if (uid_eq(i_uid_into_mnt(mnt_userns, inode), fsuid))
1da177e4 2811 return 0;
ba73d987 2812 if (uid_eq(i_uid_into_mnt(mnt_userns, dir), fsuid))
1da177e4 2813 return 0;
ba73d987 2814 return !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FOWNER);
1da177e4 2815}
cbdf35bc 2816EXPORT_SYMBOL(__check_sticky);
1da177e4
LT
2817
2818/*
2819 * Check whether we can remove a link victim from directory dir, check
2820 * whether the type of victim is right.
2821 * 1. We can't do it if dir is read-only (done in permission())
2822 * 2. We should have write and exec permissions on dir
2823 * 3. We can't remove anything from append-only dir
2824 * 4. We can't do anything with immutable dir (done in permission())
2825 * 5. If the sticky bit on dir is set we should either
2826 * a. be owner of dir, or
2827 * b. be owner of victim, or
2828 * c. have CAP_FOWNER capability
2829 * 6. If the victim is append-only or immutable we can't do antyhing with
2830 * links pointing to it.
0bd23d09
EB
2831 * 7. If the victim has an unknown uid or gid we can't change the inode.
2832 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2833 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2834 * 10. We can't remove a root or mountpoint.
2835 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
1da177e4
LT
2836 * nfs_async_unlink().
2837 */
ba73d987
CB
2838static int may_delete(struct user_namespace *mnt_userns, struct inode *dir,
2839 struct dentry *victim, bool isdir)
1da177e4 2840{
63afdfc7 2841 struct inode *inode = d_backing_inode(victim);
1da177e4
LT
2842 int error;
2843
b18825a7 2844 if (d_is_negative(victim))
1da177e4 2845 return -ENOENT;
b18825a7 2846 BUG_ON(!inode);
1da177e4
LT
2847
2848 BUG_ON(victim->d_parent->d_inode != dir);
593d1ce8
EB
2849
2850 /* Inode writeback is not safe when the uid or gid are invalid. */
ba73d987
CB
2851 if (!uid_valid(i_uid_into_mnt(mnt_userns, inode)) ||
2852 !gid_valid(i_gid_into_mnt(mnt_userns, inode)))
593d1ce8
EB
2853 return -EOVERFLOW;
2854
4fa6b5ec 2855 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4 2856
ba73d987 2857 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2858 if (error)
2859 return error;
2860 if (IS_APPEND(dir))
2861 return -EPERM;
b18825a7 2862
ba73d987
CB
2863 if (check_sticky(mnt_userns, dir, inode) || IS_APPEND(inode) ||
2864 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) ||
2865 HAS_UNMAPPED_ID(mnt_userns, inode))
1da177e4
LT
2866 return -EPERM;
2867 if (isdir) {
44b1d530 2868 if (!d_is_dir(victim))
1da177e4
LT
2869 return -ENOTDIR;
2870 if (IS_ROOT(victim))
2871 return -EBUSY;
44b1d530 2872 } else if (d_is_dir(victim))
1da177e4
LT
2873 return -EISDIR;
2874 if (IS_DEADDIR(dir))
2875 return -ENOENT;
2876 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2877 return -EBUSY;
2878 return 0;
2879}
2880
2881/* Check whether we can create an object with dentry child in directory
2882 * dir.
2883 * 1. We can't do it if child already exists (open has special treatment for
2884 * this case, but since we are inlined it's OK)
2885 * 2. We can't do it if dir is read-only (done in permission())
036d5236
EB
2886 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2887 * 4. We should have write and exec permissions on dir
2888 * 5. We can't do it if dir is immutable (done in permission())
1da177e4 2889 */
ba73d987
CB
2890static inline int may_create(struct user_namespace *mnt_userns,
2891 struct inode *dir, struct dentry *child)
1da177e4 2892{
14e972b4 2893 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
1da177e4
LT
2894 if (child->d_inode)
2895 return -EEXIST;
2896 if (IS_DEADDIR(dir))
2897 return -ENOENT;
8e538913 2898 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
036d5236 2899 return -EOVERFLOW;
8e538913 2900
ba73d987 2901 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2902}
2903
1da177e4
LT
2904/*
2905 * p1 and p2 should be directories on the same fs.
2906 */
2907struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2908{
2909 struct dentry *p;
2910
2911 if (p1 == p2) {
5955102c 2912 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
1da177e4
LT
2913 return NULL;
2914 }
2915
fc64005c 2916 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
1da177e4 2917
e2761a11
OH
2918 p = d_ancestor(p2, p1);
2919 if (p) {
5955102c
AV
2920 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2921 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
e2761a11 2922 return p;
1da177e4
LT
2923 }
2924
e2761a11
OH
2925 p = d_ancestor(p1, p2);
2926 if (p) {
5955102c
AV
2927 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2928 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
e2761a11 2929 return p;
1da177e4
LT
2930 }
2931
5955102c
AV
2932 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2933 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
1da177e4
LT
2934 return NULL;
2935}
4d359507 2936EXPORT_SYMBOL(lock_rename);
1da177e4
LT
2937
2938void unlock_rename(struct dentry *p1, struct dentry *p2)
2939{
5955102c 2940 inode_unlock(p1->d_inode);
1da177e4 2941 if (p1 != p2) {
5955102c 2942 inode_unlock(p2->d_inode);
fc64005c 2943 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
1da177e4
LT
2944 }
2945}
4d359507 2946EXPORT_SYMBOL(unlock_rename);
1da177e4 2947
6521f891
CB
2948/**
2949 * vfs_create - create new file
2950 * @mnt_userns: user namespace of the mount the inode was found from
2951 * @dir: inode of @dentry
2952 * @dentry: pointer to dentry of the base directory
2953 * @mode: mode of the new file
2954 * @want_excl: whether the file must not yet exist
2955 *
2956 * Create a new file.
2957 *
2958 * If the inode has been found through an idmapped mount the user namespace of
2959 * the vfsmount must be passed through @mnt_userns. This function will then take
2960 * care to map the inode according to @mnt_userns before checking permissions.
2961 * On non-idmapped mounts or if permission checking is to be performed on the
2962 * raw inode simply passs init_user_ns.
2963 */
2964int vfs_create(struct user_namespace *mnt_userns, struct inode *dir,
2965 struct dentry *dentry, umode_t mode, bool want_excl)
1da177e4 2966{
6521f891 2967 int error = may_create(mnt_userns, dir, dentry);
1da177e4
LT
2968 if (error)
2969 return error;
2970
acfa4380 2971 if (!dir->i_op->create)
1da177e4
LT
2972 return -EACCES; /* shouldn't it be ENOSYS? */
2973 mode &= S_IALLUGO;
2974 mode |= S_IFREG;
2975 error = security_inode_create(dir, dentry, mode);
2976 if (error)
2977 return error;
549c7297 2978 error = dir->i_op->create(mnt_userns, dir, dentry, mode, want_excl);
a74574aa 2979 if (!error)
f38aa942 2980 fsnotify_create(dir, dentry);
1da177e4
LT
2981 return error;
2982}
4d359507 2983EXPORT_SYMBOL(vfs_create);
1da177e4 2984
8e6c848e
AV
2985int vfs_mkobj(struct dentry *dentry, umode_t mode,
2986 int (*f)(struct dentry *, umode_t, void *),
2987 void *arg)
2988{
2989 struct inode *dir = dentry->d_parent->d_inode;
ba73d987 2990 int error = may_create(&init_user_ns, dir, dentry);
8e6c848e
AV
2991 if (error)
2992 return error;
2993
2994 mode &= S_IALLUGO;
2995 mode |= S_IFREG;
2996 error = security_inode_create(dir, dentry, mode);
2997 if (error)
2998 return error;
2999 error = f(dentry, mode, arg);
3000 if (!error)
3001 fsnotify_create(dir, dentry);
3002 return error;
3003}
3004EXPORT_SYMBOL(vfs_mkobj);
3005
a2982cc9
EB
3006bool may_open_dev(const struct path *path)
3007{
3008 return !(path->mnt->mnt_flags & MNT_NODEV) &&
3009 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
3010}
3011
ba73d987
CB
3012static int may_open(struct user_namespace *mnt_userns, const struct path *path,
3013 int acc_mode, int flag)
1da177e4 3014{
3fb64190 3015 struct dentry *dentry = path->dentry;
1da177e4
LT
3016 struct inode *inode = dentry->d_inode;
3017 int error;
3018
3019 if (!inode)
3020 return -ENOENT;
3021
c8fe8f30
CH
3022 switch (inode->i_mode & S_IFMT) {
3023 case S_IFLNK:
1da177e4 3024 return -ELOOP;
c8fe8f30 3025 case S_IFDIR:
fc4177be 3026 if (acc_mode & MAY_WRITE)
c8fe8f30 3027 return -EISDIR;
fc4177be
KC
3028 if (acc_mode & MAY_EXEC)
3029 return -EACCES;
c8fe8f30
CH
3030 break;
3031 case S_IFBLK:
3032 case S_IFCHR:
a2982cc9 3033 if (!may_open_dev(path))
1da177e4 3034 return -EACCES;
633fb6ac 3035 fallthrough;
c8fe8f30
CH
3036 case S_IFIFO:
3037 case S_IFSOCK:
633fb6ac
KC
3038 if (acc_mode & MAY_EXEC)
3039 return -EACCES;
1da177e4 3040 flag &= ~O_TRUNC;
c8fe8f30 3041 break;
0fd338b2
KC
3042 case S_IFREG:
3043 if ((acc_mode & MAY_EXEC) && path_noexec(path))
3044 return -EACCES;
3045 break;
4a3fd211 3046 }
b41572e9 3047
ba73d987 3048 error = inode_permission(mnt_userns, inode, MAY_OPEN | acc_mode);
b41572e9
DH
3049 if (error)
3050 return error;
6146f0d5 3051
1da177e4
LT
3052 /*
3053 * An append-only file must be opened in append mode for writing.
3054 */
3055 if (IS_APPEND(inode)) {
8737c930 3056 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521 3057 return -EPERM;
1da177e4 3058 if (flag & O_TRUNC)
7715b521 3059 return -EPERM;
1da177e4
LT
3060 }
3061
3062 /* O_NOATIME can only be set by the owner or superuser */
ba73d987 3063 if (flag & O_NOATIME && !inode_owner_or_capable(mnt_userns, inode))
7715b521 3064 return -EPERM;
1da177e4 3065
f3c7691e 3066 return 0;
7715b521 3067}
1da177e4 3068
549c7297 3069static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp)
7715b521 3070{
f0bb5aaf 3071 const struct path *path = &filp->f_path;
7715b521
AV
3072 struct inode *inode = path->dentry->d_inode;
3073 int error = get_write_access(inode);
3074 if (error)
3075 return error;
3076 /*
3077 * Refuse to truncate files with mandatory locks held on them.
3078 */
f7e33bdb 3079 error = security_path_truncate(path);
7715b521 3080 if (!error) {
549c7297 3081 error = do_truncate(mnt_userns, path->dentry, 0,
7715b521 3082 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6 3083 filp);
7715b521
AV
3084 }
3085 put_write_access(inode);
acd0c935 3086 return error;
1da177e4
LT
3087}
3088
d57999e1
DH
3089static inline int open_to_namei_flags(int flag)
3090{
8a5e929d
AV
3091 if ((flag & O_ACCMODE) == 3)
3092 flag--;
d57999e1
DH
3093 return flag;
3094}
3095
ba73d987
CB
3096static int may_o_create(struct user_namespace *mnt_userns,
3097 const struct path *dir, struct dentry *dentry,
3098 umode_t mode)
d18e9008
MS
3099{
3100 int error = security_path_mknod(dir, dentry, mode, 0);
3101 if (error)
3102 return error;
3103
8e538913 3104 if (!fsuidgid_has_mapping(dir->dentry->d_sb, mnt_userns))
1328c727
SF
3105 return -EOVERFLOW;
3106
ba73d987 3107 error = inode_permission(mnt_userns, dir->dentry->d_inode,
47291baa 3108 MAY_WRITE | MAY_EXEC);
d18e9008
MS
3109 if (error)
3110 return error;
3111
3112 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3113}
3114
1acf0af9
DH
3115/*
3116 * Attempt to atomically look up, create and open a file from a negative
3117 * dentry.
3118 *
3119 * Returns 0 if successful. The file will have been created and attached to
3120 * @file by the filesystem calling finish_open().
3121 *
00a07c15
AV
3122 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3123 * be set. The caller will need to perform the open themselves. @path will
3124 * have been updated to point to the new dentry. This may be negative.
1acf0af9
DH
3125 *
3126 * Returns an error code otherwise.
3127 */
239eb983
AV
3128static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3129 struct file *file,
239eb983 3130 int open_flag, umode_t mode)
d18e9008 3131{
384f26e2 3132 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
d18e9008 3133 struct inode *dir = nd->path.dentry->d_inode;
d18e9008 3134 int error;
d18e9008 3135
d18e9008
MS
3136 if (nd->flags & LOOKUP_DIRECTORY)
3137 open_flag |= O_DIRECTORY;
3138
30d90494
AV
3139 file->f_path.dentry = DENTRY_NOT_SET;
3140 file->f_path.mnt = nd->path.mnt;
0fb1ea09 3141 error = dir->i_op->atomic_open(dir, dentry, file,
44907d79 3142 open_to_namei_flags(open_flag), mode);
6fbd0714 3143 d_lookup_done(dentry);
384f26e2 3144 if (!error) {
64e1ac4d 3145 if (file->f_mode & FMODE_OPENED) {
6fb968cd
AV
3146 if (unlikely(dentry != file->f_path.dentry)) {
3147 dput(dentry);
3148 dentry = dget(file->f_path.dentry);
3149 }
64e1ac4d 3150 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb 3151 error = -EIO;
03da633a 3152 } else {
384f26e2
AV
3153 if (file->f_path.dentry) {
3154 dput(dentry);
3155 dentry = file->f_path.dentry;
03da633a 3156 }
239eb983 3157 if (unlikely(d_is_negative(dentry)))
a01e718f 3158 error = -ENOENT;
62b2ce96 3159 }
d18e9008 3160 }
239eb983
AV
3161 if (error) {
3162 dput(dentry);
3163 dentry = ERR_PTR(error);
3164 }
3165 return dentry;
d18e9008
MS
3166}
3167
d58ffd35 3168/*
1acf0af9 3169 * Look up and maybe create and open the last component.
d58ffd35 3170 *
00a07c15 3171 * Must be called with parent locked (exclusive in O_CREAT case).
1acf0af9 3172 *
00a07c15
AV
3173 * Returns 0 on success, that is, if
3174 * the file was successfully atomically created (if necessary) and opened, or
3175 * the file was not completely opened at this time, though lookups and
3176 * creations were performed.
3177 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3178 * In the latter case dentry returned in @path might be negative if O_CREAT
3179 * hadn't been specified.
1acf0af9 3180 *
00a07c15 3181 * An error code is returned on failure.
d58ffd35 3182 */
da5ebf5a
AV
3183static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3184 const struct open_flags *op,
3185 bool got_write)
d58ffd35 3186{
549c7297 3187 struct user_namespace *mnt_userns;
d58ffd35 3188 struct dentry *dir = nd->path.dentry;
54ef4872 3189 struct inode *dir_inode = dir->d_inode;
1643b43f 3190 int open_flag = op->open_flag;
d58ffd35 3191 struct dentry *dentry;
1643b43f 3192 int error, create_error = 0;
1643b43f 3193 umode_t mode = op->mode;
6fbd0714 3194 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
d58ffd35 3195
ce8644fc 3196 if (unlikely(IS_DEADDIR(dir_inode)))
da5ebf5a 3197 return ERR_PTR(-ENOENT);
d58ffd35 3198
73a09dd9 3199 file->f_mode &= ~FMODE_CREATED;
6fbd0714
AV
3200 dentry = d_lookup(dir, &nd->last);
3201 for (;;) {
3202 if (!dentry) {
3203 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3204 if (IS_ERR(dentry))
da5ebf5a 3205 return dentry;
6fbd0714
AV
3206 }
3207 if (d_in_lookup(dentry))
3208 break;
d58ffd35 3209
6fbd0714
AV
3210 error = d_revalidate(dentry, nd->flags);
3211 if (likely(error > 0))
3212 break;
3213 if (error)
3214 goto out_dput;
3215 d_invalidate(dentry);
3216 dput(dentry);
3217 dentry = NULL;
3218 }
3219 if (dentry->d_inode) {
6c51e513 3220 /* Cached positive dentry: will open in f_op->open */
da5ebf5a 3221 return dentry;
6c51e513 3222 }
d18e9008 3223
1643b43f
AV
3224 /*
3225 * Checking write permission is tricky, bacuse we don't know if we are
3226 * going to actually need it: O_CREAT opens should work as long as the
3227 * file exists. But checking existence breaks atomicity. The trick is
3228 * to check access and if not granted clear O_CREAT from the flags.
3229 *
3230 * Another problem is returing the "right" error value (e.g. for an
3231 * O_EXCL open we want to return EEXIST not EROFS).
3232 */
99a4a90c
AV
3233 if (unlikely(!got_write))
3234 open_flag &= ~O_TRUNC;
549c7297 3235 mnt_userns = mnt_user_ns(nd->path.mnt);
1643b43f 3236 if (open_flag & O_CREAT) {
99a4a90c
AV
3237 if (open_flag & O_EXCL)
3238 open_flag &= ~O_TRUNC;
1643b43f
AV
3239 if (!IS_POSIXACL(dir->d_inode))
3240 mode &= ~current_umask();
99a4a90c 3241 if (likely(got_write))
549c7297 3242 create_error = may_o_create(mnt_userns, &nd->path,
ba73d987 3243 dentry, mode);
99a4a90c
AV
3244 else
3245 create_error = -EROFS;
d18e9008 3246 }
99a4a90c
AV
3247 if (create_error)
3248 open_flag &= ~O_CREAT;
6ac08709 3249 if (dir_inode->i_op->atomic_open) {
d489cf9a 3250 dentry = atomic_open(nd, dentry, file, open_flag, mode);
da5ebf5a
AV
3251 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3252 dentry = ERR_PTR(create_error);
3253 return dentry;
d18e9008 3254 }
54ef4872 3255
6fbd0714 3256 if (d_in_lookup(dentry)) {
12fa5e24
AV
3257 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3258 nd->flags);
6fbd0714 3259 d_lookup_done(dentry);
12fa5e24
AV
3260 if (unlikely(res)) {
3261 if (IS_ERR(res)) {
3262 error = PTR_ERR(res);
3263 goto out_dput;
3264 }
3265 dput(dentry);
3266 dentry = res;
3267 }
54ef4872
MS
3268 }
3269
d58ffd35 3270 /* Negative dentry, just create the file */
1643b43f 3271 if (!dentry->d_inode && (open_flag & O_CREAT)) {
73a09dd9 3272 file->f_mode |= FMODE_CREATED;
ce8644fc 3273 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
ce8644fc
AV
3274 if (!dir_inode->i_op->create) {
3275 error = -EACCES;
d58ffd35 3276 goto out_dput;
ce8644fc 3277 }
549c7297
CB
3278
3279 error = dir_inode->i_op->create(mnt_userns, dir_inode, dentry,
3280 mode, open_flag & O_EXCL);
d58ffd35
MS
3281 if (error)
3282 goto out_dput;
3283 }
1643b43f
AV
3284 if (unlikely(create_error) && !dentry->d_inode) {
3285 error = create_error;
3286 goto out_dput;
d58ffd35 3287 }
da5ebf5a 3288 return dentry;
d58ffd35
MS
3289
3290out_dput:
3291 dput(dentry);
da5ebf5a 3292 return ERR_PTR(error);
d58ffd35
MS
3293}
3294
c981a482 3295static const char *open_last_lookups(struct nameidata *nd,
3ec2eef1 3296 struct file *file, const struct open_flags *op)
fb1cc555 3297{
a1e28038 3298 struct dentry *dir = nd->path.dentry;
ca344a89 3299 int open_flag = op->open_flag;
64894cf8 3300 bool got_write = false;
254cf582 3301 unsigned seq;
a1eb3315 3302 struct inode *inode;
da5ebf5a 3303 struct dentry *dentry;
b0417d2c 3304 const char *res;
1f36f774 3305
c3e380b0
AV
3306 nd->flags |= op->intent;
3307
bc77daa7 3308 if (nd->last_type != LAST_NORM) {
56676ec3
AV
3309 if (nd->depth)
3310 put_link(nd);
ff326a32 3311 return handle_dots(nd, nd->last_type);
1f36f774 3312 }
67ee3ad2 3313
ca344a89 3314 if (!(open_flag & O_CREAT)) {
fe2d35ff
AV
3315 if (nd->last.name[nd->last.len])
3316 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3317 /* we _can_ be in RCU mode here */
20e34357
AV
3318 dentry = lookup_fast(nd, &inode, &seq);
3319 if (IS_ERR(dentry))
1ccac622 3320 return ERR_CAST(dentry);
20e34357 3321 if (likely(dentry))
71574865
MS
3322 goto finish_lookup;
3323
6583fe22 3324 BUG_ON(nd->flags & LOOKUP_RCU);
b6183df7
MS
3325 } else {
3326 /* create side of things */
72287417 3327 if (nd->flags & LOOKUP_RCU) {
e36cffed
JA
3328 if (!try_to_unlazy(nd))
3329 return ERR_PTR(-ECHILD);
72287417 3330 }
c9b07eab 3331 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
b6183df7 3332 /* trailing slashes? */
deb106c6 3333 if (unlikely(nd->last.name[nd->last.len]))
1ccac622 3334 return ERR_PTR(-EISDIR);
b6183df7 3335 }
a2c36b45 3336
9cf843e3 3337 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
e36cffed 3338 got_write = !mnt_want_write(nd->path.mnt);
64894cf8
AV
3339 /*
3340 * do _not_ fail yet - we might not need that or fail with
3341 * a different error; let lookup_open() decide; we'll be
3342 * dropping this one anyway.
3343 */
3344 }
9cf843e3
AV
3345 if (open_flag & O_CREAT)
3346 inode_lock(dir->d_inode);
3347 else
3348 inode_lock_shared(dir->d_inode);
da5ebf5a 3349 dentry = lookup_open(nd, file, op, got_write);
f7bb959d
AV
3350 if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3351 fsnotify_create(dir->d_inode, dentry);
9cf843e3
AV
3352 if (open_flag & O_CREAT)
3353 inode_unlock(dir->d_inode);
3354 else
3355 inode_unlock_shared(dir->d_inode);
a1e28038 3356
c981a482 3357 if (got_write)
59e96e65 3358 mnt_drop_write(nd->path.mnt);
d18e9008 3359
59e96e65
AV
3360 if (IS_ERR(dentry))
3361 return ERR_CAST(dentry);
3362
973d4b73 3363 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
e73cabff
AV
3364 dput(nd->path.dentry);
3365 nd->path.dentry = dentry;
c981a482 3366 return NULL;
fb1cc555
AV
3367 }
3368
20e34357 3369finish_lookup:
56676ec3
AV
3370 if (nd->depth)
3371 put_link(nd);
8c4efe22 3372 res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
ff326a32 3373 if (unlikely(res))
b0417d2c 3374 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
ff326a32 3375 return res;
c981a482
AV
3376}
3377
3378/*
3379 * Handle the last step of open()
3380 */
c5971b8c 3381static int do_open(struct nameidata *nd,
c981a482
AV
3382 struct file *file, const struct open_flags *op)
3383{
549c7297 3384 struct user_namespace *mnt_userns;
c981a482
AV
3385 int open_flag = op->open_flag;
3386 bool do_truncate;
3387 int acc_mode;
c981a482
AV
3388 int error;
3389
ff326a32
AV
3390 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3391 error = complete_walk(nd);
3392 if (error)
3393 return error;
3394 }
973d4b73
AV
3395 if (!(file->f_mode & FMODE_CREATED))
3396 audit_inode(nd->name, nd->path.dentry, 0);
549c7297 3397 mnt_userns = mnt_user_ns(nd->path.mnt);
30aba665 3398 if (open_flag & O_CREAT) {
b94e0b32
AV
3399 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3400 return -EEXIST;
30aba665 3401 if (d_is_dir(nd->path.dentry))
c5971b8c 3402 return -EISDIR;
549c7297 3403 error = may_create_in_sticky(mnt_userns, nd,
30aba665
SM
3404 d_backing_inode(nd->path.dentry));
3405 if (unlikely(error))
c5971b8c 3406 return error;
30aba665 3407 }
44b1d530 3408 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
c5971b8c 3409 return -ENOTDIR;
6c0d46c4 3410
8795e7d4
AV
3411 do_truncate = false;
3412 acc_mode = op->acc_mode;
5a2d3edd
AV
3413 if (file->f_mode & FMODE_CREATED) {
3414 /* Don't check for write permission, don't truncate */
3415 open_flag &= ~O_TRUNC;
5a2d3edd 3416 acc_mode = 0;
8795e7d4 3417 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
0f9d1a10
AV
3418 error = mnt_want_write(nd->path.mnt);
3419 if (error)
c5971b8c 3420 return error;
8795e7d4 3421 do_truncate = true;
0f9d1a10 3422 }
549c7297 3423 error = may_open(mnt_userns, &nd->path, acc_mode, open_flag);
8795e7d4 3424 if (!error && !(file->f_mode & FMODE_OPENED))
3ad5615a 3425 error = vfs_open(&nd->path, file);
8795e7d4
AV
3426 if (!error)
3427 error = ima_file_check(file, op->acc_mode);
3428 if (!error && do_truncate)
549c7297 3429 error = handle_truncate(mnt_userns, file);
c80567c8
AV
3430 if (unlikely(error > 0)) {
3431 WARN_ON(1);
3432 error = -EINVAL;
3433 }
8795e7d4 3434 if (do_truncate)
0f9d1a10 3435 mnt_drop_write(nd->path.mnt);
c5971b8c 3436 return error;
fb1cc555
AV
3437}
3438
6521f891
CB
3439/**
3440 * vfs_tmpfile - create tmpfile
3441 * @mnt_userns: user namespace of the mount the inode was found from
3442 * @dentry: pointer to dentry of the base directory
3443 * @mode: mode of the new tmpfile
2111c3c0 3444 * @open_flag: flags
6521f891
CB
3445 *
3446 * Create a temporary file.
3447 *
3448 * If the inode has been found through an idmapped mount the user namespace of
3449 * the vfsmount must be passed through @mnt_userns. This function will then take
3450 * care to map the inode according to @mnt_userns before checking permissions.
3451 * On non-idmapped mounts or if permission checking is to be performed on the
3452 * raw inode simply passs init_user_ns.
3453 */
3454struct dentry *vfs_tmpfile(struct user_namespace *mnt_userns,
3455 struct dentry *dentry, umode_t mode, int open_flag)
af7bd4dc 3456{
af7bd4dc
AG
3457 struct dentry *child = NULL;
3458 struct inode *dir = dentry->d_inode;
3459 struct inode *inode;
3460 int error;
3461
3462 /* we want directory to be writable */
6521f891 3463 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
af7bd4dc
AG
3464 if (error)
3465 goto out_err;
3466 error = -EOPNOTSUPP;
3467 if (!dir->i_op->tmpfile)
3468 goto out_err;
3469 error = -ENOMEM;
cdf01226 3470 child = d_alloc(dentry, &slash_name);
af7bd4dc
AG
3471 if (unlikely(!child))
3472 goto out_err;
549c7297 3473 error = dir->i_op->tmpfile(mnt_userns, dir, child, mode);
af7bd4dc
AG
3474 if (error)
3475 goto out_err;
3476 error = -ENOENT;
3477 inode = child->d_inode;
3478 if (unlikely(!inode))
3479 goto out_err;
3480 if (!(open_flag & O_EXCL)) {
3481 spin_lock(&inode->i_lock);
3482 inode->i_state |= I_LINKABLE;
3483 spin_unlock(&inode->i_lock);
3484 }
a2d2329e 3485 ima_post_create_tmpfile(mnt_userns, inode);
af7bd4dc
AG
3486 return child;
3487
3488out_err:
3489 dput(child);
3490 return ERR_PTR(error);
3491}
3492EXPORT_SYMBOL(vfs_tmpfile);
3493
c8a53ee5 3494static int do_tmpfile(struct nameidata *nd, unsigned flags,
60545d0d 3495 const struct open_flags *op,
3ec2eef1 3496 struct file *file)
60545d0d 3497{
6521f891 3498 struct user_namespace *mnt_userns;
625b6d10 3499 struct dentry *child;
625b6d10 3500 struct path path;
c8a53ee5 3501 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
60545d0d
AV
3502 if (unlikely(error))
3503 return error;
625b6d10 3504 error = mnt_want_write(path.mnt);
60545d0d
AV
3505 if (unlikely(error))
3506 goto out;
6521f891
CB
3507 mnt_userns = mnt_user_ns(path.mnt);
3508 child = vfs_tmpfile(mnt_userns, path.dentry, op->mode, op->open_flag);
af7bd4dc 3509 error = PTR_ERR(child);
684e73be 3510 if (IS_ERR(child))
60545d0d 3511 goto out2;
625b6d10
AV
3512 dput(path.dentry);
3513 path.dentry = child;
c8a53ee5 3514 audit_inode(nd->name, child, 0);
69a91c23 3515 /* Don't check for other permissions, the inode was just created */
549c7297 3516 error = may_open(mnt_userns, &path, 0, op->open_flag);
1e8f44f1
AV
3517 if (!error)
3518 error = vfs_open(&path, file);
60545d0d 3519out2:
625b6d10 3520 mnt_drop_write(path.mnt);
60545d0d 3521out:
625b6d10 3522 path_put(&path);
60545d0d
AV
3523 return error;
3524}
3525
6ac08709
AV
3526static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3527{
3528 struct path path;
3529 int error = path_lookupat(nd, flags, &path);
3530 if (!error) {
3531 audit_inode(nd->name, path.dentry, 0);
ae2bb293 3532 error = vfs_open(&path, file);
6ac08709
AV
3533 path_put(&path);
3534 }
3535 return error;
3536}
3537
c8a53ee5
AV
3538static struct file *path_openat(struct nameidata *nd,
3539 const struct open_flags *op, unsigned flags)
1da177e4 3540{
30d90494 3541 struct file *file;
13aab428 3542 int error;
31e6b01f 3543
ea73ea72 3544 file = alloc_empty_file(op->open_flag, current_cred());
1afc99be
AV
3545 if (IS_ERR(file))
3546 return file;
31e6b01f 3547
bb458c64 3548 if (unlikely(file->f_flags & __O_TMPFILE)) {
3ec2eef1 3549 error = do_tmpfile(nd, flags, op, file);
5f336e72 3550 } else if (unlikely(file->f_flags & O_PATH)) {
6ac08709 3551 error = do_o_path(nd, flags, file);
5f336e72
AV
3552 } else {
3553 const char *s = path_init(nd, flags);
3554 while (!(error = link_path_walk(s, nd)) &&
c5971b8c 3555 (s = open_last_lookups(nd, file, op)) != NULL)
1ccac622 3556 ;
c5971b8c
AV
3557 if (!error)
3558 error = do_open(nd, file, op);
5f336e72 3559 terminate_walk(nd);
806b681c 3560 }
7c1c01ec 3561 if (likely(!error)) {
aad888f8 3562 if (likely(file->f_mode & FMODE_OPENED))
7c1c01ec
AV
3563 return file;
3564 WARN_ON(1);
3565 error = -EINVAL;
16b1c1cd 3566 }
7c1c01ec
AV
3567 fput(file);
3568 if (error == -EOPENSTALE) {
3569 if (flags & LOOKUP_RCU)
3570 error = -ECHILD;
3571 else
3572 error = -ESTALE;
2675a4eb 3573 }
7c1c01ec 3574 return ERR_PTR(error);
1da177e4
LT
3575}
3576
669abf4e 3577struct file *do_filp_open(int dfd, struct filename *pathname,
f9652e10 3578 const struct open_flags *op)
13aab428 3579{
9883d185 3580 struct nameidata nd;
f9652e10 3581 int flags = op->lookup_flags;
13aab428
AV
3582 struct file *filp;
3583
06422964 3584 set_nameidata(&nd, dfd, pathname, NULL);
c8a53ee5 3585 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
13aab428 3586 if (unlikely(filp == ERR_PTR(-ECHILD)))
c8a53ee5 3587 filp = path_openat(&nd, op, flags);
13aab428 3588 if (unlikely(filp == ERR_PTR(-ESTALE)))
c8a53ee5 3589 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d185 3590 restore_nameidata();
13aab428
AV
3591 return filp;
3592}
3593
ffb37ca3 3594struct file *do_file_open_root(const struct path *root,
f9652e10 3595 const char *name, const struct open_flags *op)
73d049a4 3596{
9883d185 3597 struct nameidata nd;
73d049a4 3598 struct file *file;
51689104 3599 struct filename *filename;
bcba1e7d 3600 int flags = op->lookup_flags;
73d049a4 3601
ffb37ca3 3602 if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN)
73d049a4
AV
3603 return ERR_PTR(-ELOOP);
3604
51689104 3605 filename = getname_kernel(name);
a1c83681 3606 if (IS_ERR(filename))
51689104
PM
3607 return ERR_CAST(filename);
3608
06422964 3609 set_nameidata(&nd, -1, filename, root);
c8a53ee5 3610 file = path_openat(&nd, op, flags | LOOKUP_RCU);
73d049a4 3611 if (unlikely(file == ERR_PTR(-ECHILD)))
c8a53ee5 3612 file = path_openat(&nd, op, flags);
73d049a4 3613 if (unlikely(file == ERR_PTR(-ESTALE)))
c8a53ee5 3614 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d185 3615 restore_nameidata();
51689104 3616 putname(filename);
73d049a4
AV
3617 return file;
3618}
3619
584d3226 3620static struct dentry *__filename_create(int dfd, struct filename *name,
1ac12b4b 3621 struct path *path, unsigned int lookup_flags)
1da177e4 3622{
c663e5d8 3623 struct dentry *dentry = ERR_PTR(-EEXIST);
391172c4
AV
3624 struct qstr last;
3625 int type;
c30dabfe 3626 int err2;
1ac12b4b
JL
3627 int error;
3628 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3629
3630 /*
3631 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3632 * other flags passed in are ignored!
3633 */
3634 lookup_flags &= LOOKUP_REVAL;
3635
c5f563f9 3636 error = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
0ee50b47
DK
3637 if (error)
3638 return ERR_PTR(error);
1da177e4 3639
c663e5d8
CH
3640 /*
3641 * Yucky last component or no last component at all?
3642 * (foo/., foo/.., /////)
3643 */
5c31b6ce 3644 if (unlikely(type != LAST_NORM))
ed75e95d 3645 goto out;
c663e5d8 3646
c30dabfe 3647 /* don't fail immediately if it's r/o, at least try to report other errors */
391172c4 3648 err2 = mnt_want_write(path->mnt);
c663e5d8
CH
3649 /*
3650 * Do the final lookup.
3651 */
391172c4 3652 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
5955102c 3653 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
391172c4 3654 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
1da177e4 3655 if (IS_ERR(dentry))
a8104a9f 3656 goto unlock;
c663e5d8 3657
a8104a9f 3658 error = -EEXIST;
b18825a7 3659 if (d_is_positive(dentry))
a8104a9f 3660 goto fail;
b18825a7 3661
c663e5d8
CH
3662 /*
3663 * Special case - lookup gave negative, but... we had foo/bar/
3664 * From the vfs_mknod() POV we just have a negative dentry -
3665 * all is fine. Let's be bastards - you had / on the end, you've
3666 * been asking for (non-existent) directory. -ENOENT for you.
3667 */
391172c4 3668 if (unlikely(!is_dir && last.name[last.len])) {
a8104a9f 3669 error = -ENOENT;
ed75e95d 3670 goto fail;
e9baf6e5 3671 }
c30dabfe
JK
3672 if (unlikely(err2)) {
3673 error = err2;
a8104a9f 3674 goto fail;
c30dabfe 3675 }
1da177e4 3676 return dentry;
1da177e4 3677fail:
a8104a9f
AV
3678 dput(dentry);
3679 dentry = ERR_PTR(error);
3680unlock:
5955102c 3681 inode_unlock(path->dentry->d_inode);
c30dabfe 3682 if (!err2)
391172c4 3683 mnt_drop_write(path->mnt);
ed75e95d 3684out:
391172c4 3685 path_put(path);
1da177e4
LT
3686 return dentry;
3687}
fa14a0b8 3688
584d3226
DK
3689static inline struct dentry *filename_create(int dfd, struct filename *name,
3690 struct path *path, unsigned int lookup_flags)
3691{
3692 struct dentry *res = __filename_create(dfd, name, path, lookup_flags);
3693
3694 putname(name);
3695 return res;
3696}
3697
fa14a0b8
AV
3698struct dentry *kern_path_create(int dfd, const char *pathname,
3699 struct path *path, unsigned int lookup_flags)
3700{
181c37b6
AV
3701 return filename_create(dfd, getname_kernel(pathname),
3702 path, lookup_flags);
fa14a0b8 3703}
dae6ad8f
AV
3704EXPORT_SYMBOL(kern_path_create);
3705
921a1650
AV
3706void done_path_create(struct path *path, struct dentry *dentry)
3707{
3708 dput(dentry);
5955102c 3709 inode_unlock(path->dentry->d_inode);
a8104a9f 3710 mnt_drop_write(path->mnt);
921a1650
AV
3711 path_put(path);
3712}
3713EXPORT_SYMBOL(done_path_create);
3714
520ae687 3715inline struct dentry *user_path_create(int dfd, const char __user *pathname,
1ac12b4b 3716 struct path *path, unsigned int lookup_flags)
dae6ad8f 3717{
181c37b6 3718 return filename_create(dfd, getname(pathname), path, lookup_flags);
dae6ad8f
AV
3719}
3720EXPORT_SYMBOL(user_path_create);
3721
6521f891
CB
3722/**
3723 * vfs_mknod - create device node or file
3724 * @mnt_userns: user namespace of the mount the inode was found from
3725 * @dir: inode of @dentry
3726 * @dentry: pointer to dentry of the base directory
3727 * @mode: mode of the new device node or file
3728 * @dev: device number of device to create
3729 *
3730 * Create a device node or file.
3731 *
3732 * If the inode has been found through an idmapped mount the user namespace of
3733 * the vfsmount must be passed through @mnt_userns. This function will then take
3734 * care to map the inode according to @mnt_userns before checking permissions.
3735 * On non-idmapped mounts or if permission checking is to be performed on the
3736 * raw inode simply passs init_user_ns.
3737 */
3738int vfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
3739 struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4 3740{
a3c751a5 3741 bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
6521f891 3742 int error = may_create(mnt_userns, dir, dentry);
1da177e4
LT
3743
3744 if (error)
3745 return error;
3746
a3c751a5
MS
3747 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3748 !capable(CAP_MKNOD))
1da177e4
LT
3749 return -EPERM;
3750
acfa4380 3751 if (!dir->i_op->mknod)
1da177e4
LT
3752 return -EPERM;
3753
08ce5f16
SH
3754 error = devcgroup_inode_mknod(mode, dev);
3755 if (error)
3756 return error;
3757
1da177e4
LT
3758 error = security_inode_mknod(dir, dentry, mode, dev);
3759 if (error)
3760 return error;
3761
549c7297 3762 error = dir->i_op->mknod(mnt_userns, dir, dentry, mode, dev);
a74574aa 3763 if (!error)
f38aa942 3764 fsnotify_create(dir, dentry);
1da177e4
LT
3765 return error;
3766}
4d359507 3767EXPORT_SYMBOL(vfs_mknod);
1da177e4 3768
f69aac00 3769static int may_mknod(umode_t mode)
463c3197
DH
3770{
3771 switch (mode & S_IFMT) {
3772 case S_IFREG:
3773 case S_IFCHR:
3774 case S_IFBLK:
3775 case S_IFIFO:
3776 case S_IFSOCK:
3777 case 0: /* zero mode translates to S_IFREG */
3778 return 0;
3779 case S_IFDIR:
3780 return -EPERM;
3781 default:
3782 return -EINVAL;
3783 }
3784}
3785
45f30dab 3786static int do_mknodat(int dfd, struct filename *name, umode_t mode,
87c4e192 3787 unsigned int dev)
1da177e4 3788{
6521f891 3789 struct user_namespace *mnt_userns;
2ad94ae6 3790 struct dentry *dentry;
dae6ad8f
AV
3791 struct path path;
3792 int error;
972567f1 3793 unsigned int lookup_flags = 0;
1da177e4 3794
8e4bfca1
AV
3795 error = may_mknod(mode);
3796 if (error)
7797251b 3797 goto out1;
972567f1 3798retry:
7797251b
DK
3799 dentry = __filename_create(dfd, name, &path, lookup_flags);
3800 error = PTR_ERR(dentry);
dae6ad8f 3801 if (IS_ERR(dentry))
7797251b 3802 goto out1;
2ad94ae6 3803
dae6ad8f 3804 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3805 mode &= ~current_umask();
dae6ad8f 3806 error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56 3807 if (error)
7797251b 3808 goto out2;
6521f891
CB
3809
3810 mnt_userns = mnt_user_ns(path.mnt);
463c3197 3811 switch (mode & S_IFMT) {
1da177e4 3812 case 0: case S_IFREG:
6521f891
CB
3813 error = vfs_create(mnt_userns, path.dentry->d_inode,
3814 dentry, mode, true);
05d1a717 3815 if (!error)
a2d2329e 3816 ima_post_path_mknod(mnt_userns, dentry);
1da177e4
LT
3817 break;
3818 case S_IFCHR: case S_IFBLK:
6521f891
CB
3819 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3820 dentry, mode, new_decode_dev(dev));
1da177e4
LT
3821 break;
3822 case S_IFIFO: case S_IFSOCK:
6521f891
CB
3823 error = vfs_mknod(mnt_userns, path.dentry->d_inode,
3824 dentry, mode, 0);
1da177e4 3825 break;
1da177e4 3826 }
7797251b 3827out2:
921a1650 3828 done_path_create(&path, dentry);
972567f1
JL
3829 if (retry_estale(error, lookup_flags)) {
3830 lookup_flags |= LOOKUP_REVAL;
3831 goto retry;
3832 }
7797251b
DK
3833out1:
3834 putname(name);
1da177e4
LT
3835 return error;
3836}
3837
87c4e192
DB
3838SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3839 unsigned int, dev)
3840{
7797251b 3841 return do_mknodat(dfd, getname(filename), mode, dev);
87c4e192
DB
3842}
3843
8208a22b 3844SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d 3845{
7797251b 3846 return do_mknodat(AT_FDCWD, getname(filename), mode, dev);
5590ff0d
UD
3847}
3848
6521f891
CB
3849/**
3850 * vfs_mkdir - create directory
3851 * @mnt_userns: user namespace of the mount the inode was found from
3852 * @dir: inode of @dentry
3853 * @dentry: pointer to dentry of the base directory
3854 * @mode: mode of the new directory
3855 *
3856 * Create a directory.
3857 *
3858 * If the inode has been found through an idmapped mount the user namespace of
3859 * the vfsmount must be passed through @mnt_userns. This function will then take
3860 * care to map the inode according to @mnt_userns before checking permissions.
3861 * On non-idmapped mounts or if permission checking is to be performed on the
3862 * raw inode simply passs init_user_ns.
3863 */
3864int vfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
3865 struct dentry *dentry, umode_t mode)
1da177e4 3866{
6521f891 3867 int error = may_create(mnt_userns, dir, dentry);
8de52778 3868 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
3869
3870 if (error)
3871 return error;
3872
acfa4380 3873 if (!dir->i_op->mkdir)
1da177e4
LT
3874 return -EPERM;
3875
3876 mode &= (S_IRWXUGO|S_ISVTX);
3877 error = security_inode_mkdir(dir, dentry, mode);
3878 if (error)
3879 return error;
3880
8de52778
AV
3881 if (max_links && dir->i_nlink >= max_links)
3882 return -EMLINK;
3883
549c7297 3884 error = dir->i_op->mkdir(mnt_userns, dir, dentry, mode);
a74574aa 3885 if (!error)
f38aa942 3886 fsnotify_mkdir(dir, dentry);
1da177e4
LT
3887 return error;
3888}
4d359507 3889EXPORT_SYMBOL(vfs_mkdir);
1da177e4 3890
45f30dab 3891int do_mkdirat(int dfd, struct filename *name, umode_t mode)
1da177e4 3892{
6902d925 3893 struct dentry *dentry;
dae6ad8f
AV
3894 struct path path;
3895 int error;
b76d8b82 3896 unsigned int lookup_flags = LOOKUP_DIRECTORY;
1da177e4 3897
b76d8b82 3898retry:
584d3226
DK
3899 dentry = __filename_create(dfd, name, &path, lookup_flags);
3900 error = PTR_ERR(dentry);
6902d925 3901 if (IS_ERR(dentry))
584d3226 3902 goto out_putname;
1da177e4 3903
dae6ad8f 3904 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3905 mode &= ~current_umask();
dae6ad8f 3906 error = security_path_mkdir(&path, dentry, mode);
6521f891
CB
3907 if (!error) {
3908 struct user_namespace *mnt_userns;
3909 mnt_userns = mnt_user_ns(path.mnt);
549c7297
CB
3910 error = vfs_mkdir(mnt_userns, path.dentry->d_inode, dentry,
3911 mode);
6521f891 3912 }
921a1650 3913 done_path_create(&path, dentry);
b76d8b82
JL
3914 if (retry_estale(error, lookup_flags)) {
3915 lookup_flags |= LOOKUP_REVAL;
3916 goto retry;
3917 }
584d3226
DK
3918out_putname:
3919 putname(name);
1da177e4
LT
3920 return error;
3921}
3922
0101db7a
DB
3923SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3924{
584d3226 3925 return do_mkdirat(dfd, getname(pathname), mode);
0101db7a
DB
3926}
3927
a218d0fd 3928SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d 3929{
584d3226 3930 return do_mkdirat(AT_FDCWD, getname(pathname), mode);
5590ff0d
UD
3931}
3932
6521f891
CB
3933/**
3934 * vfs_rmdir - remove directory
3935 * @mnt_userns: user namespace of the mount the inode was found from
3936 * @dir: inode of @dentry
3937 * @dentry: pointer to dentry of the base directory
3938 *
3939 * Remove a directory.
3940 *
3941 * If the inode has been found through an idmapped mount the user namespace of
3942 * the vfsmount must be passed through @mnt_userns. This function will then take
3943 * care to map the inode according to @mnt_userns before checking permissions.
3944 * On non-idmapped mounts or if permission checking is to be performed on the
3945 * raw inode simply passs init_user_ns.
3946 */
3947int vfs_rmdir(struct user_namespace *mnt_userns, struct inode *dir,
3948 struct dentry *dentry)
1da177e4 3949{
6521f891 3950 int error = may_delete(mnt_userns, dir, dentry, 1);
1da177e4
LT
3951
3952 if (error)
3953 return error;
3954
acfa4380 3955 if (!dir->i_op->rmdir)
1da177e4
LT
3956 return -EPERM;
3957
1d2ef590 3958 dget(dentry);
5955102c 3959 inode_lock(dentry->d_inode);
912dbc15
SW
3960
3961 error = -EBUSY;
7af1364f 3962 if (is_local_mountpoint(dentry))
912dbc15
SW
3963 goto out;
3964
3965 error = security_inode_rmdir(dir, dentry);
3966 if (error)
3967 goto out;
3968
3969 error = dir->i_op->rmdir(dir, dentry);
3970 if (error)
3971 goto out;
3972
8767712f 3973 shrink_dcache_parent(dentry);
912dbc15
SW
3974 dentry->d_inode->i_flags |= S_DEAD;
3975 dont_mount(dentry);
8ed936b5 3976 detach_mounts(dentry);
116b9731 3977 fsnotify_rmdir(dir, dentry);
912dbc15
SW
3978
3979out:
5955102c 3980 inode_unlock(dentry->d_inode);
1d2ef590 3981 dput(dentry);
912dbc15 3982 if (!error)
1da177e4 3983 d_delete(dentry);
1da177e4
LT
3984 return error;
3985}
4d359507 3986EXPORT_SYMBOL(vfs_rmdir);
1da177e4 3987
45f30dab 3988int do_rmdir(int dfd, struct filename *name)
1da177e4 3989{
6521f891 3990 struct user_namespace *mnt_userns;
0ee50b47 3991 int error;
1da177e4 3992 struct dentry *dentry;
f5beed75
AV
3993 struct path path;
3994 struct qstr last;
3995 int type;
c6ee9206
JL
3996 unsigned int lookup_flags = 0;
3997retry:
c5f563f9 3998 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
0ee50b47
DK
3999 if (error)
4000 goto exit1;
1da177e4 4001
f5beed75 4002 switch (type) {
0612d9fb
OH
4003 case LAST_DOTDOT:
4004 error = -ENOTEMPTY;
0ee50b47 4005 goto exit2;
0612d9fb
OH
4006 case LAST_DOT:
4007 error = -EINVAL;
0ee50b47 4008 goto exit2;
0612d9fb
OH
4009 case LAST_ROOT:
4010 error = -EBUSY;
0ee50b47 4011 goto exit2;
1da177e4 4012 }
0612d9fb 4013
f5beed75 4014 error = mnt_want_write(path.mnt);
c30dabfe 4015 if (error)
0ee50b47 4016 goto exit2;
0612d9fb 4017
5955102c 4018 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
f5beed75 4019 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
1da177e4 4020 error = PTR_ERR(dentry);
6902d925 4021 if (IS_ERR(dentry))
0ee50b47 4022 goto exit3;
e6bc45d6
TT
4023 if (!dentry->d_inode) {
4024 error = -ENOENT;
0ee50b47 4025 goto exit4;
e6bc45d6 4026 }
f5beed75 4027 error = security_path_rmdir(&path, dentry);
be6d3e56 4028 if (error)
0ee50b47 4029 goto exit4;
6521f891
CB
4030 mnt_userns = mnt_user_ns(path.mnt);
4031 error = vfs_rmdir(mnt_userns, path.dentry->d_inode, dentry);
0ee50b47 4032exit4:
6902d925 4033 dput(dentry);
0ee50b47 4034exit3:
5955102c 4035 inode_unlock(path.dentry->d_inode);
f5beed75 4036 mnt_drop_write(path.mnt);
0ee50b47 4037exit2:
f5beed75 4038 path_put(&path);
c6ee9206
JL
4039 if (retry_estale(error, lookup_flags)) {
4040 lookup_flags |= LOOKUP_REVAL;
4041 goto retry;
4042 }
0ee50b47 4043exit1:
24fb33d4 4044 putname(name);
1da177e4
LT
4045 return error;
4046}
4047
3cdad428 4048SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d 4049{
e24ab0ef 4050 return do_rmdir(AT_FDCWD, getname(pathname));
5590ff0d
UD
4051}
4052
b21996e3
BF
4053/**
4054 * vfs_unlink - unlink a filesystem object
6521f891 4055 * @mnt_userns: user namespace of the mount the inode was found from
b21996e3
BF
4056 * @dir: parent directory
4057 * @dentry: victim
4058 * @delegated_inode: returns victim inode, if the inode is delegated.
4059 *
4060 * The caller must hold dir->i_mutex.
4061 *
4062 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4063 * return a reference to the inode in delegated_inode. The caller
4064 * should then break the delegation on that inode and retry. Because
4065 * breaking a delegation may take a long time, the caller should drop
4066 * dir->i_mutex before doing so.
4067 *
4068 * Alternatively, a caller may pass NULL for delegated_inode. This may
4069 * be appropriate for callers that expect the underlying filesystem not
4070 * to be NFS exported.
6521f891
CB
4071 *
4072 * If the inode has been found through an idmapped mount the user namespace of
4073 * the vfsmount must be passed through @mnt_userns. This function will then take
4074 * care to map the inode according to @mnt_userns before checking permissions.
4075 * On non-idmapped mounts or if permission checking is to be performed on the
4076 * raw inode simply passs init_user_ns.
b21996e3 4077 */
6521f891
CB
4078int vfs_unlink(struct user_namespace *mnt_userns, struct inode *dir,
4079 struct dentry *dentry, struct inode **delegated_inode)
1da177e4 4080{
9accbb97 4081 struct inode *target = dentry->d_inode;
6521f891 4082 int error = may_delete(mnt_userns, dir, dentry, 0);
1da177e4
LT
4083
4084 if (error)
4085 return error;
4086
acfa4380 4087 if (!dir->i_op->unlink)
1da177e4
LT
4088 return -EPERM;
4089
5955102c 4090 inode_lock(target);
51cc3a66
HD
4091 if (IS_SWAPFILE(target))
4092 error = -EPERM;
4093 else if (is_local_mountpoint(dentry))
1da177e4
LT
4094 error = -EBUSY;
4095 else {
4096 error = security_inode_unlink(dir, dentry);
bec1052e 4097 if (!error) {
5a14696c
BF
4098 error = try_break_deleg(target, delegated_inode);
4099 if (error)
b21996e3 4100 goto out;
1da177e4 4101 error = dir->i_op->unlink(dir, dentry);
8ed936b5 4102 if (!error) {
d83c49f3 4103 dont_mount(dentry);
8ed936b5 4104 detach_mounts(dentry);
116b9731 4105 fsnotify_unlink(dir, dentry);
8ed936b5 4106 }
bec1052e 4107 }
1da177e4 4108 }
b21996e3 4109out:
5955102c 4110 inode_unlock(target);
1da177e4
LT
4111
4112 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4113 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
9accbb97 4114 fsnotify_link_count(target);
e234f35c 4115 d_delete(dentry);
1da177e4 4116 }
0eeca283 4117
1da177e4
LT
4118 return error;
4119}
4d359507 4120EXPORT_SYMBOL(vfs_unlink);
1da177e4
LT
4121
4122/*
4123 * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b 4124 * directory's i_mutex. Truncate can take a long time if there is a lot of
1da177e4
LT
4125 * writeout happening, and we don't want to prevent access to the directory
4126 * while waiting on the I/O.
4127 */
45f30dab 4128int do_unlinkat(int dfd, struct filename *name)
1da177e4 4129{
2ad94ae6 4130 int error;
1da177e4 4131 struct dentry *dentry;
f5beed75
AV
4132 struct path path;
4133 struct qstr last;
4134 int type;
1da177e4 4135 struct inode *inode = NULL;
b21996e3 4136 struct inode *delegated_inode = NULL;
5d18f813
JL
4137 unsigned int lookup_flags = 0;
4138retry:
c5f563f9 4139 error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
0ee50b47
DK
4140 if (error)
4141 goto exit1;
2ad94ae6 4142
1da177e4 4143 error = -EISDIR;
f5beed75 4144 if (type != LAST_NORM)
0ee50b47 4145 goto exit2;
0612d9fb 4146
f5beed75 4147 error = mnt_want_write(path.mnt);
c30dabfe 4148 if (error)
0ee50b47 4149 goto exit2;
b21996e3 4150retry_deleg:
5955102c 4151 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
f5beed75 4152 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
1da177e4
LT
4153 error = PTR_ERR(dentry);
4154 if (!IS_ERR(dentry)) {
6521f891
CB
4155 struct user_namespace *mnt_userns;
4156
1da177e4 4157 /* Why not before? Because we want correct error value */
f5beed75 4158 if (last.name[last.len])
50338b88 4159 goto slashes;
1da177e4 4160 inode = dentry->d_inode;
b18825a7 4161 if (d_is_negative(dentry))
e6bc45d6
TT
4162 goto slashes;
4163 ihold(inode);
f5beed75 4164 error = security_path_unlink(&path, dentry);
be6d3e56 4165 if (error)
0ee50b47 4166 goto exit3;
6521f891 4167 mnt_userns = mnt_user_ns(path.mnt);
549c7297
CB
4168 error = vfs_unlink(mnt_userns, path.dentry->d_inode, dentry,
4169 &delegated_inode);
0ee50b47 4170exit3:
1da177e4
LT
4171 dput(dentry);
4172 }
5955102c 4173 inode_unlock(path.dentry->d_inode);
1da177e4
LT
4174 if (inode)
4175 iput(inode); /* truncate the inode here */
b21996e3
BF
4176 inode = NULL;
4177 if (delegated_inode) {
5a14696c 4178 error = break_deleg_wait(&delegated_inode);
b21996e3
BF
4179 if (!error)
4180 goto retry_deleg;
4181 }
f5beed75 4182 mnt_drop_write(path.mnt);
0ee50b47 4183exit2:
f5beed75 4184 path_put(&path);
5d18f813
JL
4185 if (retry_estale(error, lookup_flags)) {
4186 lookup_flags |= LOOKUP_REVAL;
4187 inode = NULL;
4188 goto retry;
4189 }
0ee50b47 4190exit1:
da2f1362 4191 putname(name);
1da177e4
LT
4192 return error;
4193
4194slashes:
b18825a7
DH
4195 if (d_is_negative(dentry))
4196 error = -ENOENT;
44b1d530 4197 else if (d_is_dir(dentry))
b18825a7
DH
4198 error = -EISDIR;
4199 else
4200 error = -ENOTDIR;
0ee50b47 4201 goto exit3;
1da177e4
LT
4202}
4203
2e4d0924 4204SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d
UD
4205{
4206 if ((flag & ~AT_REMOVEDIR) != 0)
4207 return -EINVAL;
4208
4209 if (flag & AT_REMOVEDIR)
e24ab0ef 4210 return do_rmdir(dfd, getname(pathname));
da2f1362 4211 return do_unlinkat(dfd, getname(pathname));
5590ff0d
UD
4212}
4213
3480b257 4214SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d 4215{
da2f1362 4216 return do_unlinkat(AT_FDCWD, getname(pathname));
5590ff0d
UD
4217}
4218
6521f891
CB
4219/**
4220 * vfs_symlink - create symlink
4221 * @mnt_userns: user namespace of the mount the inode was found from
4222 * @dir: inode of @dentry
4223 * @dentry: pointer to dentry of the base directory
4224 * @oldname: name of the file to link to
4225 *
4226 * Create a symlink.
4227 *
4228 * If the inode has been found through an idmapped mount the user namespace of
4229 * the vfsmount must be passed through @mnt_userns. This function will then take
4230 * care to map the inode according to @mnt_userns before checking permissions.
4231 * On non-idmapped mounts or if permission checking is to be performed on the
4232 * raw inode simply passs init_user_ns.
4233 */
4234int vfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
4235 struct dentry *dentry, const char *oldname)
1da177e4 4236{
6521f891 4237 int error = may_create(mnt_userns, dir, dentry);
1da177e4
LT
4238
4239 if (error)
4240 return error;
4241
acfa4380 4242 if (!dir->i_op->symlink)
1da177e4
LT
4243 return -EPERM;
4244
4245 error = security_inode_symlink(dir, dentry, oldname);
4246 if (error)
4247 return error;
4248
549c7297 4249 error = dir->i_op->symlink(mnt_userns, dir, dentry, oldname);
a74574aa 4250 if (!error)
f38aa942 4251 fsnotify_create(dir, dentry);
1da177e4
LT
4252 return error;
4253}
4d359507 4254EXPORT_SYMBOL(vfs_symlink);
1da177e4 4255
7a8721f8 4256int do_symlinkat(struct filename *from, int newdfd, struct filename *to)
1da177e4 4257{
2ad94ae6 4258 int error;
6902d925 4259 struct dentry *dentry;
dae6ad8f 4260 struct path path;
f46d3567 4261 unsigned int lookup_flags = 0;
1da177e4 4262
da2d0ced
DK
4263 if (IS_ERR(from)) {
4264 error = PTR_ERR(from);
4265 goto out_putnames;
4266 }
f46d3567 4267retry:
da2d0ced 4268 dentry = __filename_create(newdfd, to, &path, lookup_flags);
6902d925
DH
4269 error = PTR_ERR(dentry);
4270 if (IS_ERR(dentry))
da2d0ced 4271 goto out_putnames;
6902d925 4272
91a27b2a 4273 error = security_path_symlink(&path, dentry, from->name);
6521f891
CB
4274 if (!error) {
4275 struct user_namespace *mnt_userns;
4276
4277 mnt_userns = mnt_user_ns(path.mnt);
4278 error = vfs_symlink(mnt_userns, path.dentry->d_inode, dentry,
4279 from->name);
4280 }
921a1650 4281 done_path_create(&path, dentry);
f46d3567
JL
4282 if (retry_estale(error, lookup_flags)) {
4283 lookup_flags |= LOOKUP_REVAL;
4284 goto retry;
4285 }
da2d0ced
DK
4286out_putnames:
4287 putname(to);
1da177e4
LT
4288 putname(from);
4289 return error;
4290}
4291
b724e846
DB
4292SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4293 int, newdfd, const char __user *, newname)
4294{
da2d0ced 4295 return do_symlinkat(getname(oldname), newdfd, getname(newname));
b724e846
DB
4296}
4297
3480b257 4298SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d 4299{
da2d0ced 4300 return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname));
5590ff0d
UD
4301}
4302
146a8595
BF
4303/**
4304 * vfs_link - create a new link
4305 * @old_dentry: object to be linked
6521f891 4306 * @mnt_userns: the user namespace of the mount
146a8595
BF
4307 * @dir: new parent
4308 * @new_dentry: where to create the new link
4309 * @delegated_inode: returns inode needing a delegation break
4310 *
4311 * The caller must hold dir->i_mutex
4312 *
4313 * If vfs_link discovers a delegation on the to-be-linked file in need
4314 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4315 * inode in delegated_inode. The caller should then break the delegation
4316 * and retry. Because breaking a delegation may take a long time, the
4317 * caller should drop the i_mutex before doing so.
4318 *
4319 * Alternatively, a caller may pass NULL for delegated_inode. This may
4320 * be appropriate for callers that expect the underlying filesystem not
4321 * to be NFS exported.
6521f891
CB
4322 *
4323 * If the inode has been found through an idmapped mount the user namespace of
4324 * the vfsmount must be passed through @mnt_userns. This function will then take
4325 * care to map the inode according to @mnt_userns before checking permissions.
4326 * On non-idmapped mounts or if permission checking is to be performed on the
4327 * raw inode simply passs init_user_ns.
146a8595 4328 */
6521f891
CB
4329int vfs_link(struct dentry *old_dentry, struct user_namespace *mnt_userns,
4330 struct inode *dir, struct dentry *new_dentry,
4331 struct inode **delegated_inode)
1da177e4
LT
4332{
4333 struct inode *inode = old_dentry->d_inode;
8de52778 4334 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
4335 int error;
4336
4337 if (!inode)
4338 return -ENOENT;
4339
6521f891 4340 error = may_create(mnt_userns, dir, new_dentry);
1da177e4
LT
4341 if (error)
4342 return error;
4343
4344 if (dir->i_sb != inode->i_sb)
4345 return -EXDEV;
4346
4347 /*
4348 * A link to an append-only or immutable file cannot be created.
4349 */
4350 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4351 return -EPERM;
0bd23d09
EB
4352 /*
4353 * Updating the link count will likely cause i_uid and i_gid to
4354 * be writen back improperly if their true value is unknown to
4355 * the vfs.
4356 */
6521f891 4357 if (HAS_UNMAPPED_ID(mnt_userns, inode))
0bd23d09 4358 return -EPERM;
acfa4380 4359 if (!dir->i_op->link)
1da177e4 4360 return -EPERM;
7e79eedb 4361 if (S_ISDIR(inode->i_mode))
1da177e4
LT
4362 return -EPERM;
4363
4364 error = security_inode_link(old_dentry, dir, new_dentry);
4365 if (error)
4366 return error;
4367
5955102c 4368 inode_lock(inode);
aae8a97d 4369 /* Make sure we don't allow creating hardlink to an unlinked file */
f4e0c30c 4370 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
aae8a97d 4371 error = -ENOENT;
8de52778
AV
4372 else if (max_links && inode->i_nlink >= max_links)
4373 error = -EMLINK;
146a8595
BF
4374 else {
4375 error = try_break_deleg(inode, delegated_inode);
4376 if (!error)
4377 error = dir->i_op->link(old_dentry, dir, new_dentry);
4378 }
f4e0c30c
AV
4379
4380 if (!error && (inode->i_state & I_LINKABLE)) {
4381 spin_lock(&inode->i_lock);
4382 inode->i_state &= ~I_LINKABLE;
4383 spin_unlock(&inode->i_lock);
4384 }
5955102c 4385 inode_unlock(inode);
e31e14ec 4386 if (!error)
7e79eedb 4387 fsnotify_link(dir, inode, new_dentry);
1da177e4
LT
4388 return error;
4389}
4d359507 4390EXPORT_SYMBOL(vfs_link);
1da177e4
LT
4391
4392/*
4393 * Hardlinks are often used in delicate situations. We avoid
4394 * security-related surprises by not following symlinks on the
4395 * newname. --KAB
4396 *
4397 * We don't follow them on the oldname either to be compatible
4398 * with linux 2.0, and to avoid hard-linking to directories
4399 * and other special files. --ADM
4400 */
cf30da90 4401int do_linkat(int olddfd, struct filename *old, int newdfd,
020250f3 4402 struct filename *new, int flags)
1da177e4 4403{
6521f891 4404 struct user_namespace *mnt_userns;
1da177e4 4405 struct dentry *new_dentry;
dae6ad8f 4406 struct path old_path, new_path;
146a8595 4407 struct inode *delegated_inode = NULL;
11a7b371 4408 int how = 0;
1da177e4 4409 int error;
1da177e4 4410
020250f3
DK
4411 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) {
4412 error = -EINVAL;
4413 goto out_putnames;
4414 }
11a7b371 4415 /*
f0cc6ffb
LT
4416 * To use null names we require CAP_DAC_READ_SEARCH
4417 * This ensures that not everyone will be able to create
4418 * handlink using the passed filedescriptor.
11a7b371 4419 */
020250f3
DK
4420 if (flags & AT_EMPTY_PATH && !capable(CAP_DAC_READ_SEARCH)) {
4421 error = -ENOENT;
4422 goto out_putnames;
f0cc6ffb 4423 }
11a7b371
AK
4424
4425 if (flags & AT_SYMLINK_FOLLOW)
4426 how |= LOOKUP_FOLLOW;
442e31ca 4427retry:
020250f3 4428 error = __filename_lookup(olddfd, old, how, &old_path, NULL);
1da177e4 4429 if (error)
020250f3 4430 goto out_putnames;
2ad94ae6 4431
020250f3 4432 new_dentry = __filename_create(newdfd, new, &new_path,
442e31ca 4433 (how & LOOKUP_REVAL));
1da177e4 4434 error = PTR_ERR(new_dentry);
6902d925 4435 if (IS_ERR(new_dentry))
020250f3 4436 goto out_putpath;
dae6ad8f
AV
4437
4438 error = -EXDEV;
4439 if (old_path.mnt != new_path.mnt)
4440 goto out_dput;
549c7297
CB
4441 mnt_userns = mnt_user_ns(new_path.mnt);
4442 error = may_linkat(mnt_userns, &old_path);
800179c9
KC
4443 if (unlikely(error))
4444 goto out_dput;
dae6ad8f 4445 error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56 4446 if (error)
a8104a9f 4447 goto out_dput;
6521f891
CB
4448 error = vfs_link(old_path.dentry, mnt_userns, new_path.dentry->d_inode,
4449 new_dentry, &delegated_inode);
75c3f29d 4450out_dput:
921a1650 4451 done_path_create(&new_path, new_dentry);
146a8595
BF
4452 if (delegated_inode) {
4453 error = break_deleg_wait(&delegated_inode);
d22e6338
OD
4454 if (!error) {
4455 path_put(&old_path);
146a8595 4456 goto retry;
d22e6338 4457 }
146a8595 4458 }
442e31ca 4459 if (retry_estale(error, how)) {
d22e6338 4460 path_put(&old_path);
442e31ca
JL
4461 how |= LOOKUP_REVAL;
4462 goto retry;
4463 }
020250f3 4464out_putpath:
2d8f3038 4465 path_put(&old_path);
020250f3
DK
4466out_putnames:
4467 putname(old);
4468 putname(new);
1da177e4
LT
4469
4470 return error;
4471}
4472
46ea89eb
DB
4473SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4474 int, newdfd, const char __user *, newname, int, flags)
4475{
020250f3
DK
4476 return do_linkat(olddfd, getname_uflags(oldname, flags),
4477 newdfd, getname(newname), flags);
46ea89eb
DB
4478}
4479
3480b257 4480SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d 4481{
020250f3 4482 return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0);
5590ff0d
UD
4483}
4484
bc27027a
MS
4485/**
4486 * vfs_rename - rename a filesystem object
2111c3c0 4487 * @rd: pointer to &struct renamedata info
bc27027a
MS
4488 *
4489 * The caller must hold multiple mutexes--see lock_rename()).
4490 *
4491 * If vfs_rename discovers a delegation in need of breaking at either
4492 * the source or destination, it will return -EWOULDBLOCK and return a
4493 * reference to the inode in delegated_inode. The caller should then
4494 * break the delegation and retry. Because breaking a delegation may
4495 * take a long time, the caller should drop all locks before doing
4496 * so.
4497 *
4498 * Alternatively, a caller may pass NULL for delegated_inode. This may
4499 * be appropriate for callers that expect the underlying filesystem not
4500 * to be NFS exported.
4501 *
1da177e4
LT
4502 * The worst of all namespace operations - renaming directory. "Perverted"
4503 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4504 * Problems:
0117d427 4505 *
d03b29a2 4506 * a) we can get into loop creation.
1da177e4
LT
4507 * b) race potential - two innocent renames can create a loop together.
4508 * That's where 4.4 screws up. Current fix: serialization on
a11f3a05 4509 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4 4510 * story.
6cedba89
BF
4511 * c) we have to lock _four_ objects - parents and victim (if it exists),
4512 * and source (if it is not a directory).
1b1dcc1b 4513 * And that - after we got ->i_mutex on parents (until then we don't know
1da177e4
LT
4514 * whether the target exists). Solution: try to be smart with locking
4515 * order for inodes. We rely on the fact that tree topology may change
a11f3a05 4516 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4
LT
4517 * move will be locked. Thus we can rank directories by the tree
4518 * (ancestors first) and rank all non-directories after them.
4519 * That works since everybody except rename does "lock parent, lookup,
a11f3a05 4520 * lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4
LT
4521 * HOWEVER, it relies on the assumption that any object with ->lookup()
4522 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4523 * we'd better make sure that there's no link(2) for them.
e4eaac06 4524 * d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b 4525 * we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4 4526 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e7 4527 * ->i_mutex on parents, which works but leads to some truly excessive
1da177e4
LT
4528 * locking].
4529 */
9fe61450 4530int vfs_rename(struct renamedata *rd)
1da177e4 4531{
bc27027a 4532 int error;
9fe61450
CB
4533 struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir;
4534 struct dentry *old_dentry = rd->old_dentry;
4535 struct dentry *new_dentry = rd->new_dentry;
4536 struct inode **delegated_inode = rd->delegated_inode;
4537 unsigned int flags = rd->flags;
bc27027a 4538 bool is_dir = d_is_dir(old_dentry);
bc27027a 4539 struct inode *source = old_dentry->d_inode;
9055cba7 4540 struct inode *target = new_dentry->d_inode;
da1ce067
MS
4541 bool new_is_dir = false;
4542 unsigned max_links = new_dir->i_sb->s_max_links;
49d31c2f 4543 struct name_snapshot old_name;
bc27027a 4544
8d3e2936 4545 if (source == target)
bc27027a
MS
4546 return 0;
4547
6521f891 4548 error = may_delete(rd->old_mnt_userns, old_dir, old_dentry, is_dir);
bc27027a
MS
4549 if (error)
4550 return error;
4551
da1ce067 4552 if (!target) {
6521f891 4553 error = may_create(rd->new_mnt_userns, new_dir, new_dentry);
da1ce067
MS
4554 } else {
4555 new_is_dir = d_is_dir(new_dentry);
4556
4557 if (!(flags & RENAME_EXCHANGE))
6521f891
CB
4558 error = may_delete(rd->new_mnt_userns, new_dir,
4559 new_dentry, is_dir);
da1ce067 4560 else
6521f891
CB
4561 error = may_delete(rd->new_mnt_userns, new_dir,
4562 new_dentry, new_is_dir);
da1ce067 4563 }
bc27027a
MS
4564 if (error)
4565 return error;
4566
2773bf00 4567 if (!old_dir->i_op->rename)
bc27027a 4568 return -EPERM;
1da177e4
LT
4569
4570 /*
4571 * If we are going to change the parent - check write permissions,
4572 * we'll need to flip '..'.
4573 */
da1ce067
MS
4574 if (new_dir != old_dir) {
4575 if (is_dir) {
6521f891 4576 error = inode_permission(rd->old_mnt_userns, source,
47291baa 4577 MAY_WRITE);
da1ce067
MS
4578 if (error)
4579 return error;
4580 }
4581 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
6521f891 4582 error = inode_permission(rd->new_mnt_userns, target,
47291baa 4583 MAY_WRITE);
da1ce067
MS
4584 if (error)
4585 return error;
4586 }
1da177e4
LT
4587 }
4588
0b3974eb
MS
4589 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4590 flags);
1da177e4
LT
4591 if (error)
4592 return error;
4593
49d31c2f 4594 take_dentry_name_snapshot(&old_name, old_dentry);
1d2ef590 4595 dget(new_dentry);
da1ce067 4596 if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a
MS
4597 lock_two_nondirectories(source, target);
4598 else if (target)
5955102c 4599 inode_lock(target);
9055cba7 4600
51cc3a66
HD
4601 error = -EPERM;
4602 if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target)))
4603 goto out;
4604
9055cba7 4605 error = -EBUSY;
7af1364f 4606 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
9055cba7
SW
4607 goto out;
4608
da1ce067 4609 if (max_links && new_dir != old_dir) {
bc27027a 4610 error = -EMLINK;
da1ce067 4611 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
bc27027a 4612 goto out;
da1ce067
MS
4613 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4614 old_dir->i_nlink >= max_links)
4615 goto out;
4616 }
da1ce067 4617 if (!is_dir) {
bc27027a 4618 error = try_break_deleg(source, delegated_inode);
8e6d782c
BF
4619 if (error)
4620 goto out;
da1ce067
MS
4621 }
4622 if (target && !new_is_dir) {
4623 error = try_break_deleg(target, delegated_inode);
4624 if (error)
4625 goto out;
8e6d782c 4626 }
549c7297
CB
4627 error = old_dir->i_op->rename(rd->new_mnt_userns, old_dir, old_dentry,
4628 new_dir, new_dentry, flags);
51892bbb
SW
4629 if (error)
4630 goto out;
4631
da1ce067 4632 if (!(flags & RENAME_EXCHANGE) && target) {
8767712f
AV
4633 if (is_dir) {
4634 shrink_dcache_parent(new_dentry);
bc27027a 4635 target->i_flags |= S_DEAD;
8767712f 4636 }
51892bbb 4637 dont_mount(new_dentry);
8ed936b5 4638 detach_mounts(new_dentry);
bc27027a 4639 }
da1ce067
MS
4640 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4641 if (!(flags & RENAME_EXCHANGE))
4642 d_move(old_dentry, new_dentry);
4643 else
4644 d_exchange(old_dentry, new_dentry);
4645 }
51892bbb 4646out:
da1ce067 4647 if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a
MS
4648 unlock_two_nondirectories(source, target);
4649 else if (target)
5955102c 4650 inode_unlock(target);
1da177e4 4651 dput(new_dentry);
da1ce067 4652 if (!error) {
f4ec3a3d 4653 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
da1ce067
MS
4654 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4655 if (flags & RENAME_EXCHANGE) {
f4ec3a3d 4656 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
da1ce067
MS
4657 new_is_dir, NULL, new_dentry);
4658 }
4659 }
49d31c2f 4660 release_dentry_name_snapshot(&old_name);
0eeca283 4661
1da177e4
LT
4662 return error;
4663}
4d359507 4664EXPORT_SYMBOL(vfs_rename);
1da177e4 4665
e886663c
JA
4666int do_renameat2(int olddfd, struct filename *from, int newdfd,
4667 struct filename *to, unsigned int flags)
1da177e4 4668{
9fe61450 4669 struct renamedata rd;
2ad94ae6
AV
4670 struct dentry *old_dentry, *new_dentry;
4671 struct dentry *trap;
f5beed75
AV
4672 struct path old_path, new_path;
4673 struct qstr old_last, new_last;
4674 int old_type, new_type;
8e6d782c 4675 struct inode *delegated_inode = NULL;
f5beed75 4676 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
c6a94284 4677 bool should_retry = false;
e886663c 4678 int error = -EINVAL;
520c8b16 4679
0d7a8555 4680 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
0ee50b47 4681 goto put_names;
da1ce067 4682
0d7a8555
MS
4683 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4684 (flags & RENAME_EXCHANGE))
0ee50b47 4685 goto put_names;
520c8b16 4686
f5beed75
AV
4687 if (flags & RENAME_EXCHANGE)
4688 target_flags = 0;
4689
c6a94284 4690retry:
c5f563f9
AV
4691 error = filename_parentat(olddfd, from, lookup_flags, &old_path,
4692 &old_last, &old_type);
0ee50b47
DK
4693 if (error)
4694 goto put_names;
1da177e4 4695
c5f563f9
AV
4696 error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last,
4697 &new_type);
0ee50b47 4698 if (error)
1da177e4
LT
4699 goto exit1;
4700
4701 error = -EXDEV;
f5beed75 4702 if (old_path.mnt != new_path.mnt)
1da177e4
LT
4703 goto exit2;
4704
1da177e4 4705 error = -EBUSY;
f5beed75 4706 if (old_type != LAST_NORM)
1da177e4
LT
4707 goto exit2;
4708
0a7c3937
MS
4709 if (flags & RENAME_NOREPLACE)
4710 error = -EEXIST;
f5beed75 4711 if (new_type != LAST_NORM)
1da177e4
LT
4712 goto exit2;
4713
f5beed75 4714 error = mnt_want_write(old_path.mnt);
c30dabfe
JK
4715 if (error)
4716 goto exit2;
4717
8e6d782c 4718retry_deleg:
f5beed75 4719 trap = lock_rename(new_path.dentry, old_path.dentry);
1da177e4 4720
f5beed75 4721 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
1da177e4
LT
4722 error = PTR_ERR(old_dentry);
4723 if (IS_ERR(old_dentry))
4724 goto exit3;
4725 /* source must exist */
4726 error = -ENOENT;
b18825a7 4727 if (d_is_negative(old_dentry))
1da177e4 4728 goto exit4;
f5beed75 4729 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
0a7c3937
MS
4730 error = PTR_ERR(new_dentry);
4731 if (IS_ERR(new_dentry))
4732 goto exit4;
4733 error = -EEXIST;
4734 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4735 goto exit5;
da1ce067
MS
4736 if (flags & RENAME_EXCHANGE) {
4737 error = -ENOENT;
4738 if (d_is_negative(new_dentry))
4739 goto exit5;
4740
4741 if (!d_is_dir(new_dentry)) {
4742 error = -ENOTDIR;
f5beed75 4743 if (new_last.name[new_last.len])
da1ce067
MS
4744 goto exit5;
4745 }
4746 }
1da177e4 4747 /* unless the source is a directory trailing slashes give -ENOTDIR */
44b1d530 4748 if (!d_is_dir(old_dentry)) {
1da177e4 4749 error = -ENOTDIR;
f5beed75 4750 if (old_last.name[old_last.len])
0a7c3937 4751 goto exit5;
f5beed75 4752 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
0a7c3937 4753 goto exit5;
1da177e4
LT
4754 }
4755 /* source should not be ancestor of target */
4756 error = -EINVAL;
4757 if (old_dentry == trap)
0a7c3937 4758 goto exit5;
1da177e4 4759 /* target should not be an ancestor of source */
da1ce067
MS
4760 if (!(flags & RENAME_EXCHANGE))
4761 error = -ENOTEMPTY;
1da177e4
LT
4762 if (new_dentry == trap)
4763 goto exit5;
4764
f5beed75
AV
4765 error = security_path_rename(&old_path, old_dentry,
4766 &new_path, new_dentry, flags);
be6d3e56 4767 if (error)
c30dabfe 4768 goto exit5;
9fe61450
CB
4769
4770 rd.old_dir = old_path.dentry->d_inode;
4771 rd.old_dentry = old_dentry;
6521f891 4772 rd.old_mnt_userns = mnt_user_ns(old_path.mnt);
9fe61450
CB
4773 rd.new_dir = new_path.dentry->d_inode;
4774 rd.new_dentry = new_dentry;
6521f891 4775 rd.new_mnt_userns = mnt_user_ns(new_path.mnt);
9fe61450
CB
4776 rd.delegated_inode = &delegated_inode;
4777 rd.flags = flags;
4778 error = vfs_rename(&rd);
1da177e4
LT
4779exit5:
4780 dput(new_dentry);
4781exit4:
4782 dput(old_dentry);
4783exit3:
f5beed75 4784 unlock_rename(new_path.dentry, old_path.dentry);
8e6d782c
BF
4785 if (delegated_inode) {
4786 error = break_deleg_wait(&delegated_inode);
4787 if (!error)
4788 goto retry_deleg;
4789 }
f5beed75 4790 mnt_drop_write(old_path.mnt);
1da177e4 4791exit2:
c6a94284
JL
4792 if (retry_estale(error, lookup_flags))
4793 should_retry = true;
f5beed75 4794 path_put(&new_path);
1da177e4 4795exit1:
f5beed75 4796 path_put(&old_path);
c6a94284
JL
4797 if (should_retry) {
4798 should_retry = false;
4799 lookup_flags |= LOOKUP_REVAL;
4800 goto retry;
4801 }
0ee50b47 4802put_names:
91ef658f 4803 putname(from);
91ef658f 4804 putname(to);
1da177e4
LT
4805 return error;
4806}
4807
ee81feb6
DB
4808SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4809 int, newdfd, const char __user *, newname, unsigned int, flags)
4810{
e886663c
JA
4811 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4812 flags);
ee81feb6
DB
4813}
4814
520c8b16
MS
4815SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4816 int, newdfd, const char __user *, newname)
4817{
e886663c
JA
4818 return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname),
4819 0);
520c8b16
MS
4820}
4821
a26eab24 4822SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d 4823{
e886663c
JA
4824 return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD,
4825 getname(newname), 0);
5590ff0d
UD
4826}
4827
5d826c84 4828int readlink_copy(char __user *buffer, int buflen, const char *link)
1da177e4 4829{
5d826c84 4830 int len = PTR_ERR(link);
1da177e4
LT
4831 if (IS_ERR(link))
4832 goto out;
4833
4834 len = strlen(link);
4835 if (len > (unsigned) buflen)
4836 len = buflen;
4837 if (copy_to_user(buffer, link, len))
4838 len = -EFAULT;
4839out:
4840 return len;
4841}
4842
fd4a0edf
MS
4843/**
4844 * vfs_readlink - copy symlink body into userspace buffer
4845 * @dentry: dentry on which to get symbolic link
4846 * @buffer: user memory pointer
4847 * @buflen: size of buffer
4848 *
4849 * Does not touch atime. That's up to the caller if necessary
4850 *
4851 * Does not call security hook.
4852 */
4853int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4854{
4855 struct inode *inode = d_inode(dentry);
f2df5da6
AV
4856 DEFINE_DELAYED_CALL(done);
4857 const char *link;
4858 int res;
fd4a0edf 4859
76fca90e
MS
4860 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4861 if (unlikely(inode->i_op->readlink))
4862 return inode->i_op->readlink(dentry, buffer, buflen);
4863
4864 if (!d_is_symlink(dentry))
4865 return -EINVAL;
4866
4867 spin_lock(&inode->i_lock);
4868 inode->i_opflags |= IOP_DEFAULT_READLINK;
4869 spin_unlock(&inode->i_lock);
4870 }
fd4a0edf 4871
4c4f7c19 4872 link = READ_ONCE(inode->i_link);
f2df5da6
AV
4873 if (!link) {
4874 link = inode->i_op->get_link(dentry, inode, &done);
4875 if (IS_ERR(link))
4876 return PTR_ERR(link);
4877 }
4878 res = readlink_copy(buffer, buflen, link);
4879 do_delayed_call(&done);
4880 return res;
fd4a0edf
MS
4881}
4882EXPORT_SYMBOL(vfs_readlink);
1da177e4 4883
d60874cd
MS
4884/**
4885 * vfs_get_link - get symlink body
4886 * @dentry: dentry on which to get symbolic link
4887 * @done: caller needs to free returned data with this
4888 *
4889 * Calls security hook and i_op->get_link() on the supplied inode.
4890 *
4891 * It does not touch atime. That's up to the caller if necessary.
4892 *
4893 * Does not work on "special" symlinks like /proc/$$/fd/N
4894 */
4895const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4896{
4897 const char *res = ERR_PTR(-EINVAL);
4898 struct inode *inode = d_inode(dentry);
4899
4900 if (d_is_symlink(dentry)) {
4901 res = ERR_PTR(security_inode_readlink(dentry));
4902 if (!res)
4903 res = inode->i_op->get_link(dentry, inode, done);
4904 }
4905 return res;
4906}
4907EXPORT_SYMBOL(vfs_get_link);
4908
1da177e4 4909/* get the link contents into pagecache */
6b255391 4910const char *page_get_link(struct dentry *dentry, struct inode *inode,
fceef393 4911 struct delayed_call *callback)
1da177e4 4912{
ebd09abb
DG
4913 char *kaddr;
4914 struct page *page;
6b255391
AV
4915 struct address_space *mapping = inode->i_mapping;
4916
d3883d4f
AV
4917 if (!dentry) {
4918 page = find_get_page(mapping, 0);
4919 if (!page)
4920 return ERR_PTR(-ECHILD);
4921 if (!PageUptodate(page)) {
4922 put_page(page);
4923 return ERR_PTR(-ECHILD);
4924 }
4925 } else {
4926 page = read_mapping_page(mapping, 0, NULL);
4927 if (IS_ERR(page))
4928 return (char*)page;
4929 }
fceef393 4930 set_delayed_call(callback, page_put_link, page);
21fc61c7
AV
4931 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4932 kaddr = page_address(page);
6b255391 4933 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
ebd09abb 4934 return kaddr;
1da177e4
LT
4935}
4936
6b255391 4937EXPORT_SYMBOL(page_get_link);
1da177e4 4938
fceef393 4939void page_put_link(void *arg)
1da177e4 4940{
fceef393 4941 put_page(arg);
1da177e4 4942}
4d359507 4943EXPORT_SYMBOL(page_put_link);
1da177e4 4944
aa80deab
AV
4945int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4946{
fceef393 4947 DEFINE_DELAYED_CALL(done);
6b255391
AV
4948 int res = readlink_copy(buffer, buflen,
4949 page_get_link(dentry, d_inode(dentry),
fceef393
AV
4950 &done));
4951 do_delayed_call(&done);
aa80deab
AV
4952 return res;
4953}
4954EXPORT_SYMBOL(page_readlink);
4955
54566b2c
NP
4956/*
4957 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4958 */
4959int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4
LT
4960{
4961 struct address_space *mapping = inode->i_mapping;
0adb25d2 4962 struct page *page;
afddba49 4963 void *fsdata;
beb497ab 4964 int err;
c718a975 4965 unsigned int flags = 0;
54566b2c
NP
4966 if (nofs)
4967 flags |= AOP_FLAG_NOFS;
1da177e4 4968
7e53cac4 4969retry:
afddba49 4970 err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c 4971 flags, &page, &fsdata);
1da177e4 4972 if (err)
afddba49
NP
4973 goto fail;
4974
21fc61c7 4975 memcpy(page_address(page), symname, len-1);
afddba49
NP
4976
4977 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4978 page, fsdata);
1da177e4
LT
4979 if (err < 0)
4980 goto fail;
afddba49
NP
4981 if (err < len-1)
4982 goto retry;
4983
1da177e4
LT
4984 mark_inode_dirty(inode);
4985 return 0;
1da177e4
LT
4986fail:
4987 return err;
4988}
4d359507 4989EXPORT_SYMBOL(__page_symlink);
1da177e4 4990
0adb25d2
KK
4991int page_symlink(struct inode *inode, const char *symname, int len)
4992{
4993 return __page_symlink(inode, symname, len,
c62d2555 4994 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
0adb25d2 4995}
4d359507 4996EXPORT_SYMBOL(page_symlink);
0adb25d2 4997
92e1d5be 4998const struct inode_operations page_symlink_inode_operations = {
6b255391 4999 .get_link = page_get_link,
1da177e4 5000};
1da177e4 5001EXPORT_SYMBOL(page_symlink_inode_operations);