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