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