]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/namei.c
UBUNTU: Ubuntu-snapdragon-4.4.0-1082.87
[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 2014
f803e0c1
LT
2015 if (!*s)
2016 flags &= ~LOOKUP_RCU;
2017
31e6b01f 2018 nd->last_type = LAST_ROOT; /* if there are only slashes... */
980f3ea2 2019 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
31e6b01f 2020 nd->depth = 0;
5b6ca027 2021 if (flags & LOOKUP_ROOT) {
b18825a7
DH
2022 struct dentry *root = nd->root.dentry;
2023 struct inode *inode = root->d_inode;
fd2f7cb5 2024 if (*s) {
44b1d530 2025 if (!d_can_lookup(root))
368ee9ba 2026 return ERR_PTR(-ENOTDIR);
73d049a4
AV
2027 retval = inode_permission(inode, MAY_EXEC);
2028 if (retval)
368ee9ba 2029 return ERR_PTR(retval);
73d049a4 2030 }
5b6ca027
AV
2031 nd->path = nd->root;
2032 nd->inode = inode;
2033 if (flags & LOOKUP_RCU) {
8b61e74f 2034 rcu_read_lock();
5b6ca027 2035 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
8f47a016 2036 nd->root_seq = nd->seq;
48a066e7 2037 nd->m_seq = read_seqbegin(&mount_lock);
5b6ca027
AV
2038 } else {
2039 path_get(&nd->path);
2040 }
368ee9ba 2041 return s;
5b6ca027
AV
2042 }
2043
31e6b01f 2044 nd->root.mnt = NULL;
31e6b01f 2045
48a066e7 2046 nd->m_seq = read_seqbegin(&mount_lock);
fd2f7cb5 2047 if (*s == '/') {
e41f7d4e 2048 if (flags & LOOKUP_RCU) {
8b61e74f 2049 rcu_read_lock();
06d7137e
AV
2050 set_root_rcu(nd);
2051 nd->seq = nd->root_seq;
e41f7d4e
AV
2052 } else {
2053 set_root(nd);
2054 path_get(&nd->root);
2055 }
2056 nd->path = nd->root;
c8a53ee5 2057 } else if (nd->dfd == AT_FDCWD) {
e41f7d4e
AV
2058 if (flags & LOOKUP_RCU) {
2059 struct fs_struct *fs = current->fs;
2060 unsigned seq;
31e6b01f 2061
8b61e74f 2062 rcu_read_lock();
c28cc364 2063
e41f7d4e
AV
2064 do {
2065 seq = read_seqcount_begin(&fs->seq);
2066 nd->path = fs->pwd;
2067 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2068 } while (read_seqcount_retry(&fs->seq, seq));
2069 } else {
2070 get_fs_pwd(current->fs, &nd->path);
2071 }
31e6b01f 2072 } else {
582aa64a 2073 /* Caller must check execute permissions on the starting path component */
c8a53ee5 2074 struct fd f = fdget_raw(nd->dfd);
31e6b01f
NP
2075 struct dentry *dentry;
2076
2903ff01 2077 if (!f.file)
368ee9ba 2078 return ERR_PTR(-EBADF);
31e6b01f 2079
2903ff01 2080 dentry = f.file->f_path.dentry;
31e6b01f 2081
fd2f7cb5 2082 if (*s) {
44b1d530 2083 if (!d_can_lookup(dentry)) {
2903ff01 2084 fdput(f);
368ee9ba 2085 return ERR_PTR(-ENOTDIR);
2903ff01 2086 }
f52e0c11 2087 }
31e6b01f 2088
2903ff01 2089 nd->path = f.file->f_path;
e41f7d4e 2090 if (flags & LOOKUP_RCU) {
8b61e74f 2091 rcu_read_lock();
34a26b99
AV
2092 nd->inode = nd->path.dentry->d_inode;
2093 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
e41f7d4e 2094 } else {
2903ff01 2095 path_get(&nd->path);
34a26b99 2096 nd->inode = nd->path.dentry->d_inode;
e41f7d4e 2097 }
34a26b99 2098 fdput(f);
368ee9ba 2099 return s;
31e6b01f 2100 }
31e6b01f 2101
31e6b01f 2102 nd->inode = nd->path.dentry->d_inode;
4023bfc9 2103 if (!(flags & LOOKUP_RCU))
368ee9ba 2104 return s;
4023bfc9 2105 if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
368ee9ba 2106 return s;
4023bfc9
AV
2107 if (!(nd->flags & LOOKUP_ROOT))
2108 nd->root.mnt = NULL;
2109 rcu_read_unlock();
368ee9ba 2110 return ERR_PTR(-ECHILD);
9b4a9b14
AV
2111}
2112
3bdba28b 2113static const char *trailing_symlink(struct nameidata *nd)
95fa25d9
AV
2114{
2115 const char *s;
fec2fa24 2116 int error = may_follow_link(nd);
deb106c6 2117 if (unlikely(error))
3bdba28b 2118 return ERR_PTR(error);
95fa25d9 2119 nd->flags |= LOOKUP_PARENT;
fab51e8a 2120 nd->stack[0].name = NULL;
3b2e7f75 2121 s = get_link(nd);
deb106c6 2122 return s ? s : "";
95fa25d9
AV
2123}
2124
caa85634 2125static inline int lookup_last(struct nameidata *nd)
bd92d7fe
AV
2126{
2127 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2128 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2129
2130 nd->flags &= ~LOOKUP_PARENT;
deb106c6 2131 return walk_component(nd,
4693a547
AV
2132 nd->flags & LOOKUP_FOLLOW
2133 ? nd->depth
2134 ? WALK_PUT | WALK_GET
2135 : WALK_GET
2136 : 0);
bd92d7fe
AV
2137}
2138
9b4a9b14 2139/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5 2140static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
9b4a9b14 2141{
c8a53ee5 2142 const char *s = path_init(nd, flags);
bd92d7fe 2143 int err;
31e6b01f 2144
368ee9ba
AV
2145 if (IS_ERR(s))
2146 return PTR_ERR(s);
3bdba28b
AV
2147 while (!(err = link_path_walk(s, nd))
2148 && ((err = lookup_last(nd)) > 0)) {
2149 s = trailing_symlink(nd);
2150 if (IS_ERR(s)) {
2151 err = PTR_ERR(s);
2152 break;
bd92d7fe
AV
2153 }
2154 }
9f1fafee
AV
2155 if (!err)
2156 err = complete_walk(nd);
bd92d7fe 2157
deb106c6
AV
2158 if (!err && nd->flags & LOOKUP_DIRECTORY)
2159 if (!d_can_lookup(nd->path.dentry))
bd23a539 2160 err = -ENOTDIR;
625b6d10
AV
2161 if (!err) {
2162 *path = nd->path;
2163 nd->path.mnt = NULL;
2164 nd->path.dentry = NULL;
2165 }
2166 terminate_walk(nd);
bd92d7fe 2167 return err;
ee0827cd 2168}
31e6b01f 2169
625b6d10 2170static int filename_lookup(int dfd, struct filename *name, unsigned flags,
9ad1aaa6 2171 struct path *path, struct path *root)
ee0827cd 2172{
894bc8c4 2173 int retval;
9883d185 2174 struct nameidata nd;
abc9f5be
AV
2175 if (IS_ERR(name))
2176 return PTR_ERR(name);
9ad1aaa6
AV
2177 if (unlikely(root)) {
2178 nd.root = *root;
2179 flags |= LOOKUP_ROOT;
2180 }
9883d185 2181 set_nameidata(&nd, dfd, name);
c8a53ee5 2182 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
ee0827cd 2183 if (unlikely(retval == -ECHILD))
c8a53ee5 2184 retval = path_lookupat(&nd, flags, path);
ee0827cd 2185 if (unlikely(retval == -ESTALE))
c8a53ee5 2186 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
31e6b01f 2187
f78570dd 2188 if (likely(!retval))
625b6d10 2189 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
9883d185 2190 restore_nameidata();
e4bd1c1a 2191 putname(name);
170aa3d0 2192 return retval;
1da177e4
LT
2193}
2194
8bcb77fa 2195/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
c8a53ee5 2196static int path_parentat(struct nameidata *nd, unsigned flags,
391172c4 2197 struct path *parent)
8bcb77fa 2198{
c8a53ee5 2199 const char *s = path_init(nd, flags);
368ee9ba
AV
2200 int err;
2201 if (IS_ERR(s))
2202 return PTR_ERR(s);
2203 err = link_path_walk(s, nd);
8bcb77fa
AV
2204 if (!err)
2205 err = complete_walk(nd);
391172c4
AV
2206 if (!err) {
2207 *parent = nd->path;
2208 nd->path.mnt = NULL;
2209 nd->path.dentry = NULL;
2210 }
2211 terminate_walk(nd);
8bcb77fa
AV
2212 return err;
2213}
2214
5c31b6ce 2215static struct filename *filename_parentat(int dfd, struct filename *name,
391172c4
AV
2216 unsigned int flags, struct path *parent,
2217 struct qstr *last, int *type)
8bcb77fa
AV
2218{
2219 int retval;
9883d185 2220 struct nameidata nd;
8bcb77fa 2221
5c31b6ce
AV
2222 if (IS_ERR(name))
2223 return name;
9883d185 2224 set_nameidata(&nd, dfd, name);
c8a53ee5 2225 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
8bcb77fa 2226 if (unlikely(retval == -ECHILD))
c8a53ee5 2227 retval = path_parentat(&nd, flags, parent);
8bcb77fa 2228 if (unlikely(retval == -ESTALE))
c8a53ee5 2229 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
391172c4
AV
2230 if (likely(!retval)) {
2231 *last = nd.last;
2232 *type = nd.last_type;
2233 audit_inode(name, parent->dentry, LOOKUP_PARENT);
5c31b6ce
AV
2234 } else {
2235 putname(name);
2236 name = ERR_PTR(retval);
391172c4 2237 }
9883d185 2238 restore_nameidata();
5c31b6ce 2239 return name;
8bcb77fa
AV
2240}
2241
79714f72
AV
2242/* does lookup, returns the object with parent locked */
2243struct dentry *kern_path_locked(const char *name, struct path *path)
5590ff0d 2244{
5c31b6ce
AV
2245 struct filename *filename;
2246 struct dentry *d;
391172c4
AV
2247 struct qstr last;
2248 int type;
51689104 2249
5c31b6ce
AV
2250 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2251 &last, &type);
51689104
PM
2252 if (IS_ERR(filename))
2253 return ERR_CAST(filename);
5c31b6ce 2254 if (unlikely(type != LAST_NORM)) {
391172c4 2255 path_put(path);
5c31b6ce
AV
2256 putname(filename);
2257 return ERR_PTR(-EINVAL);
79714f72 2258 }
391172c4
AV
2259 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2260 d = __lookup_hash(&last, path->dentry, 0);
79714f72 2261 if (IS_ERR(d)) {
391172c4
AV
2262 mutex_unlock(&path->dentry->d_inode->i_mutex);
2263 path_put(path);
79714f72 2264 }
51689104 2265 putname(filename);
79714f72 2266 return d;
5590ff0d
UD
2267}
2268
d1811465
AV
2269int kern_path(const char *name, unsigned int flags, struct path *path)
2270{
abc9f5be
AV
2271 return filename_lookup(AT_FDCWD, getname_kernel(name),
2272 flags, path, NULL);
d1811465 2273}
4d359507 2274EXPORT_SYMBOL(kern_path);
d1811465 2275
16f18200
JJS
2276/**
2277 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2278 * @dentry: pointer to dentry of the base directory
2279 * @mnt: pointer to vfs mount of the base directory
2280 * @name: pointer to file name
2281 * @flags: lookup flags
e0a01249 2282 * @path: pointer to struct path to fill
16f18200
JJS
2283 */
2284int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2285 const char *name, unsigned int flags,
e0a01249 2286 struct path *path)
16f18200 2287{
9ad1aaa6 2288 struct path root = {.mnt = mnt, .dentry = dentry};
9ad1aaa6 2289 /* the first argument of filename_lookup() is ignored with root */
abc9f5be
AV
2290 return filename_lookup(AT_FDCWD, getname_kernel(name),
2291 flags , path, &root);
16f18200 2292}
4d359507 2293EXPORT_SYMBOL(vfs_path_lookup);
16f18200 2294
eead1911 2295/**
a6b91919 2296 * lookup_one_len - filesystem helper to lookup single pathname component
eead1911
CH
2297 * @name: pathname component to lookup
2298 * @base: base directory to lookup from
2299 * @len: maximum length @len should be interpreted to
2300 *
a6b91919 2301 * Note that this routine is purely a helper for filesystem usage and should
9e7543e9 2302 * not be called by generic code.
eead1911 2303 */
057f6c01
JM
2304struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2305{
057f6c01 2306 struct qstr this;
6a96ba54 2307 unsigned int c;
cda309de 2308 int err;
057f6c01 2309
2f9092e1
DW
2310 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2311
6a96ba54
AV
2312 this.name = name;
2313 this.len = len;
0145acc2 2314 this.hash = full_name_hash(name, len);
6a96ba54
AV
2315 if (!len)
2316 return ERR_PTR(-EACCES);
2317
21d8a15a
AV
2318 if (unlikely(name[0] == '.')) {
2319 if (len < 2 || (len == 2 && name[1] == '.'))
2320 return ERR_PTR(-EACCES);
2321 }
2322
6a96ba54
AV
2323 while (len--) {
2324 c = *(const unsigned char *)name++;
2325 if (c == '/' || c == '\0')
2326 return ERR_PTR(-EACCES);
6a96ba54 2327 }
5a202bcd
AV
2328 /*
2329 * See if the low-level filesystem might want
2330 * to use its own hash..
2331 */
2332 if (base->d_flags & DCACHE_OP_HASH) {
da53be12 2333 int err = base->d_op->d_hash(base, &this);
5a202bcd
AV
2334 if (err < 0)
2335 return ERR_PTR(err);
2336 }
eead1911 2337
cda309de
MS
2338 err = inode_permission(base->d_inode, MAY_EXEC);
2339 if (err)
2340 return ERR_PTR(err);
2341
72bd866a 2342 return __lookup_hash(&this, base, 0);
057f6c01 2343}
4d359507 2344EXPORT_SYMBOL(lookup_one_len);
057f6c01 2345
1fa1e7f6
AW
2346int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2347 struct path *path, int *empty)
1da177e4 2348{
abc9f5be
AV
2349 return filename_lookup(dfd, getname_flags(name, flags, empty),
2350 flags, path, NULL);
1da177e4 2351}
b853a161 2352EXPORT_SYMBOL(user_path_at_empty);
1fa1e7f6 2353
873f1eed
JL
2354/*
2355 * NB: most callers don't do anything directly with the reference to the
2356 * to struct filename, but the nd->last pointer points into the name string
2357 * allocated by getname. So we must hold the reference to it until all
2358 * path-walking is complete.
2359 */
a2ec4a2d 2360static inline struct filename *
f5beed75
AV
2361user_path_parent(int dfd, const char __user *path,
2362 struct path *parent,
2363 struct qstr *last,
2364 int *type,
9e790bd6 2365 unsigned int flags)
2ad94ae6 2366{
9e790bd6 2367 /* only LOOKUP_REVAL is allowed in extra flags */
5c31b6ce
AV
2368 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2369 parent, last, type);
2ad94ae6
AV
2370}
2371
8033426e 2372/**
197df04c 2373 * mountpoint_last - look up last component for umount
8033426e
JL
2374 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2375 * @path: pointer to container for result
2376 *
2377 * This is a special lookup_last function just for umount. In this case, we
2378 * need to resolve the path without doing any revalidation.
2379 *
2380 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2381 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2382 * in almost all cases, this lookup will be served out of the dcache. The only
2383 * cases where it won't are if nd->last refers to a symlink or the path is
2384 * bogus and it doesn't exist.
2385 *
2386 * Returns:
2387 * -error: if there was an error during lookup. This includes -ENOENT if the
2388 * lookup found a negative dentry. The nd->path reference will also be
2389 * put in this case.
2390 *
2391 * 0: if we successfully resolved nd->path and found it to not to be a
2392 * symlink that needs to be followed. "path" will also be populated.
2393 * The nd->path reference will also be put.
2394 *
2395 * 1: if we successfully resolved nd->last and found it to be a symlink
2396 * that needs to be followed. "path" will be populated with the path
2397 * to the link, and nd->path will *not* be put.
2398 */
2399static int
197df04c 2400mountpoint_last(struct nameidata *nd, struct path *path)
8033426e
JL
2401{
2402 int error = 0;
2403 struct dentry *dentry;
2404 struct dentry *dir = nd->path.dentry;
2405
35759521
AV
2406 /* If we're in rcuwalk, drop out of it to handle last component */
2407 if (nd->flags & LOOKUP_RCU) {
6e9918b7 2408 if (unlazy_walk(nd, NULL, 0))
deb106c6 2409 return -ECHILD;
8033426e
JL
2410 }
2411
2412 nd->flags &= ~LOOKUP_PARENT;
2413
2414 if (unlikely(nd->last_type != LAST_NORM)) {
2415 error = handle_dots(nd, nd->last_type);
35759521 2416 if (error)
deb106c6 2417 return error;
35759521
AV
2418 dentry = dget(nd->path.dentry);
2419 goto done;
8033426e
JL
2420 }
2421
2422 mutex_lock(&dir->d_inode->i_mutex);
2423 dentry = d_lookup(dir, &nd->last);
2424 if (!dentry) {
2425 /*
2426 * No cached dentry. Mounted dentries are pinned in the cache,
2427 * so that means that this dentry is probably a symlink or the
2428 * path doesn't actually point to a mounted dentry.
2429 */
2430 dentry = d_alloc(dir, &nd->last);
2431 if (!dentry) {
bcceeeba 2432 mutex_unlock(&dir->d_inode->i_mutex);
deb106c6 2433 return -ENOMEM;
8033426e 2434 }
35759521 2435 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
bcceeeba
DJ
2436 if (IS_ERR(dentry)) {
2437 mutex_unlock(&dir->d_inode->i_mutex);
deb106c6 2438 return PTR_ERR(dentry);
bcceeeba 2439 }
8033426e
JL
2440 }
2441 mutex_unlock(&dir->d_inode->i_mutex);
2442
35759521 2443done:
698934df 2444 if (d_is_negative(dentry)) {
35759521 2445 dput(dentry);
deb106c6 2446 return -ENOENT;
8033426e 2447 }
191d7f73
AV
2448 if (nd->depth)
2449 put_link(nd);
35759521 2450 path->dentry = dentry;
295dc39d 2451 path->mnt = nd->path.mnt;
181548c0
AV
2452 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2453 d_backing_inode(dentry), 0);
deb106c6 2454 if (unlikely(error))
d63ff28f 2455 return error;
295dc39d 2456 mntget(path->mnt);
35759521 2457 follow_mount(path);
deb106c6 2458 return 0;
8033426e
JL
2459}
2460
2461/**
197df04c 2462 * path_mountpoint - look up a path to be umounted
2a78b857 2463 * @nd: lookup context
8033426e 2464 * @flags: lookup flags
c8a53ee5 2465 * @path: pointer to container for result
8033426e
JL
2466 *
2467 * Look up the given name, but don't attempt to revalidate the last component.
606d6fe3 2468 * Returns 0 and "path" will be valid on success; Returns error otherwise.
8033426e
JL
2469 */
2470static int
c8a53ee5 2471path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
8033426e 2472{
c8a53ee5 2473 const char *s = path_init(nd, flags);
368ee9ba
AV
2474 int err;
2475 if (IS_ERR(s))
2476 return PTR_ERR(s);
3bdba28b
AV
2477 while (!(err = link_path_walk(s, nd)) &&
2478 (err = mountpoint_last(nd, path)) > 0) {
2479 s = trailing_symlink(nd);
2480 if (IS_ERR(s)) {
2481 err = PTR_ERR(s);
8033426e 2482 break;
3bdba28b 2483 }
8033426e 2484 }
deb106c6 2485 terminate_walk(nd);
8033426e
JL
2486 return err;
2487}
2488
2d864651 2489static int
668696dc 2490filename_mountpoint(int dfd, struct filename *name, struct path *path,
2d864651
AV
2491 unsigned int flags)
2492{
9883d185 2493 struct nameidata nd;
cbaab2db 2494 int error;
668696dc
AV
2495 if (IS_ERR(name))
2496 return PTR_ERR(name);
9883d185 2497 set_nameidata(&nd, dfd, name);
c8a53ee5 2498 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2d864651 2499 if (unlikely(error == -ECHILD))
c8a53ee5 2500 error = path_mountpoint(&nd, flags, path);
2d864651 2501 if (unlikely(error == -ESTALE))
c8a53ee5 2502 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2d864651 2503 if (likely(!error))
668696dc 2504 audit_inode(name, path->dentry, 0);
9883d185 2505 restore_nameidata();
668696dc 2506 putname(name);
2d864651
AV
2507 return error;
2508}
2509
8033426e 2510/**
197df04c 2511 * user_path_mountpoint_at - lookup a path from userland in order to umount it
8033426e
JL
2512 * @dfd: directory file descriptor
2513 * @name: pathname from userland
2514 * @flags: lookup flags
2515 * @path: pointer to container to hold result
2516 *
2517 * A umount is a special case for path walking. We're not actually interested
2518 * in the inode in this situation, and ESTALE errors can be a problem. We
2519 * simply want track down the dentry and vfsmount attached at the mountpoint
2520 * and avoid revalidating the last component.
2521 *
2522 * Returns 0 and populates "path" on success.
2523 */
2524int
197df04c 2525user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
8033426e
JL
2526 struct path *path)
2527{
cbaab2db 2528 return filename_mountpoint(dfd, getname(name), path, flags);
8033426e
JL
2529}
2530
2d864651
AV
2531int
2532kern_path_mountpoint(int dfd, const char *name, struct path *path,
2533 unsigned int flags)
2534{
cbaab2db 2535 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2d864651
AV
2536}
2537EXPORT_SYMBOL(kern_path_mountpoint);
2538
cbdf35bc 2539int __check_sticky(struct inode *dir, struct inode *inode)
1da177e4 2540{
8e96e3b7 2541 kuid_t fsuid = current_fsuid();
da9592ed 2542
8e96e3b7 2543 if (uid_eq(inode->i_uid, fsuid))
1da177e4 2544 return 0;
8e96e3b7 2545 if (uid_eq(dir->i_uid, fsuid))
1da177e4 2546 return 0;
23adbe12 2547 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
1da177e4 2548}
cbdf35bc 2549EXPORT_SYMBOL(__check_sticky);
1da177e4
LT
2550
2551/*
2552 * Check whether we can remove a link victim from directory dir, check
2553 * whether the type of victim is right.
2554 * 1. We can't do it if dir is read-only (done in permission())
2555 * 2. We should have write and exec permissions on dir
2556 * 3. We can't remove anything from append-only dir
2557 * 4. We can't do anything with immutable dir (done in permission())
2558 * 5. If the sticky bit on dir is set we should either
2559 * a. be owner of dir, or
2560 * b. be owner of victim, or
2561 * c. have CAP_FOWNER capability
2562 * 6. If the victim is append-only or immutable we can't do antyhing with
2563 * links pointing to it.
be494dcc
EB
2564 * 7. If the victim has an unknown uid or gid we can't change the inode.
2565 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2566 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2567 * 10. We can't remove a root or mountpoint.
2568 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
1da177e4
LT
2569 * nfs_async_unlink().
2570 */
b18825a7 2571static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
1da177e4 2572{
63afdfc7 2573 struct inode *inode = d_backing_inode(victim);
1da177e4
LT
2574 int error;
2575
b18825a7 2576 if (d_is_negative(victim))
1da177e4 2577 return -ENOENT;
b18825a7 2578 BUG_ON(!inode);
1da177e4
LT
2579
2580 BUG_ON(victim->d_parent->d_inode != dir);
4fa6b5ec 2581 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4 2582
f419a2e3 2583 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2584 if (error)
2585 return error;
2586 if (IS_APPEND(dir))
2587 return -EPERM;
b18825a7
DH
2588
2589 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
be494dcc 2590 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
1da177e4
LT
2591 return -EPERM;
2592 if (isdir) {
44b1d530 2593 if (!d_is_dir(victim))
1da177e4
LT
2594 return -ENOTDIR;
2595 if (IS_ROOT(victim))
2596 return -EBUSY;
44b1d530 2597 } else if (d_is_dir(victim))
1da177e4
LT
2598 return -EISDIR;
2599 if (IS_DEADDIR(dir))
2600 return -ENOENT;
2601 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2602 return -EBUSY;
2603 return 0;
2604}
2605
2606/* Check whether we can create an object with dentry child in directory
2607 * dir.
2608 * 1. We can't do it if child already exists (open has special treatment for
2609 * this case, but since we are inlined it's OK)
2610 * 2. We can't do it if dir is read-only (done in permission())
b95f0b70
EB
2611 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2612 * 4. We should have write and exec permissions on dir
2613 * 5. We can't do it if dir is immutable (done in permission())
1da177e4 2614 */
a95164d9 2615static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4 2616{
b95f0b70 2617 struct user_namespace *s_user_ns;
14e972b4 2618 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
1da177e4
LT
2619 if (child->d_inode)
2620 return -EEXIST;
2621 if (IS_DEADDIR(dir))
2622 return -ENOENT;
b95f0b70
EB
2623 s_user_ns = dir->i_sb->s_user_ns;
2624 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2625 !kgid_has_mapping(s_user_ns, current_fsgid()))
2626 return -EOVERFLOW;
f419a2e3 2627 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2628}
2629
1da177e4
LT
2630/*
2631 * p1 and p2 should be directories on the same fs.
2632 */
2633struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2634{
2635 struct dentry *p;
2636
2637 if (p1 == p2) {
f2eace23 2638 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4
LT
2639 return NULL;
2640 }
2641
a11f3a05 2642 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4 2643
e2761a11
OH
2644 p = d_ancestor(p2, p1);
2645 if (p) {
2646 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2647 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2648 return p;
1da177e4
LT
2649 }
2650
e2761a11
OH
2651 p = d_ancestor(p1, p2);
2652 if (p) {
2653 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2654 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2655 return p;
1da177e4
LT
2656 }
2657
f2eace23 2658 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
d1b72cc6 2659 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
1da177e4
LT
2660 return NULL;
2661}
4d359507 2662EXPORT_SYMBOL(lock_rename);
1da177e4
LT
2663
2664void unlock_rename(struct dentry *p1, struct dentry *p2)
2665{
1b1dcc1b 2666 mutex_unlock(&p1->d_inode->i_mutex);
1da177e4 2667 if (p1 != p2) {
1b1dcc1b 2668 mutex_unlock(&p2->d_inode->i_mutex);
a11f3a05 2669 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4
LT
2670 }
2671}
4d359507 2672EXPORT_SYMBOL(unlock_rename);
1da177e4 2673
4acdaf27 2674int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
312b63fb 2675 bool want_excl)
1da177e4 2676{
a95164d9 2677 int error = may_create(dir, dentry);
1da177e4
LT
2678 if (error)
2679 return error;
2680
acfa4380 2681 if (!dir->i_op->create)
1da177e4
LT
2682 return -EACCES; /* shouldn't it be ENOSYS? */
2683 mode &= S_IALLUGO;
2684 mode |= S_IFREG;
2685 error = security_inode_create(dir, dentry, mode);
2686 if (error)
2687 return error;
312b63fb 2688 error = dir->i_op->create(dir, dentry, mode, want_excl);
a74574aa 2689 if (!error)
f38aa942 2690 fsnotify_create(dir, dentry);
1da177e4
LT
2691 return error;
2692}
4d359507 2693EXPORT_SYMBOL(vfs_create);
1da177e4 2694
73d049a4 2695static int may_open(struct path *path, int acc_mode, int flag)
1da177e4 2696{
3fb64190 2697 struct dentry *dentry = path->dentry;
1da177e4
LT
2698 struct inode *inode = dentry->d_inode;
2699 int error;
2700
bcda7652
AV
2701 /* O_PATH? */
2702 if (!acc_mode)
2703 return 0;
2704
1da177e4
LT
2705 if (!inode)
2706 return -ENOENT;
2707
c8fe8f30
CH
2708 switch (inode->i_mode & S_IFMT) {
2709 case S_IFLNK:
1da177e4 2710 return -ELOOP;
c8fe8f30
CH
2711 case S_IFDIR:
2712 if (acc_mode & MAY_WRITE)
2713 return -EISDIR;
2714 break;
2715 case S_IFBLK:
2716 case S_IFCHR:
3fb64190 2717 if (path->mnt->mnt_flags & MNT_NODEV)
1da177e4 2718 return -EACCES;
c8fe8f30
CH
2719 /*FALLTHRU*/
2720 case S_IFIFO:
2721 case S_IFSOCK:
1da177e4 2722 flag &= ~O_TRUNC;
c8fe8f30 2723 break;
4a3fd211 2724 }
b41572e9 2725
3fb64190 2726 error = inode_permission(inode, acc_mode);
b41572e9
DH
2727 if (error)
2728 return error;
6146f0d5 2729
1da177e4
LT
2730 /*
2731 * An append-only file must be opened in append mode for writing.
2732 */
2733 if (IS_APPEND(inode)) {
8737c930 2734 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521 2735 return -EPERM;
1da177e4 2736 if (flag & O_TRUNC)
7715b521 2737 return -EPERM;
1da177e4
LT
2738 }
2739
2740 /* O_NOATIME can only be set by the owner or superuser */
2e149670 2741 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b521 2742 return -EPERM;
1da177e4 2743
f3c7691e 2744 return 0;
7715b521 2745}
1da177e4 2746
e1181ee6 2747static int handle_truncate(struct file *filp)
7715b521 2748{
e1181ee6 2749 struct path *path = &filp->f_path;
7715b521
AV
2750 struct inode *inode = path->dentry->d_inode;
2751 int error = get_write_access(inode);
2752 if (error)
2753 return error;
2754 /*
2755 * Refuse to truncate files with mandatory locks held on them.
2756 */
d7a06983 2757 error = locks_verify_locked(filp);
7715b521 2758 if (!error)
ea0d3ab2 2759 error = security_path_truncate(path);
7715b521
AV
2760 if (!error) {
2761 error = do_truncate(path->dentry, 0,
2762 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6 2763 filp);
7715b521
AV
2764 }
2765 put_write_access(inode);
acd0c935 2766 return error;
1da177e4
LT
2767}
2768
d57999e1
DH
2769static inline int open_to_namei_flags(int flag)
2770{
8a5e929d
AV
2771 if ((flag & O_ACCMODE) == 3)
2772 flag--;
d57999e1
DH
2773 return flag;
2774}
2775
d18e9008
MS
2776static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2777{
2778 int error = security_path_mknod(dir, dentry, mode, 0);
2779 if (error)
2780 return error;
2781
2782 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2783 if (error)
2784 return error;
2785
2786 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2787}
2788
1acf0af9
DH
2789/*
2790 * Attempt to atomically look up, create and open a file from a negative
2791 * dentry.
2792 *
2793 * Returns 0 if successful. The file will have been created and attached to
2794 * @file by the filesystem calling finish_open().
2795 *
2796 * Returns 1 if the file was looked up only or didn't need creating. The
2797 * caller will need to perform the open themselves. @path will have been
2798 * updated to point to the new dentry. This may be negative.
2799 *
2800 * Returns an error code otherwise.
2801 */
2675a4eb
AV
2802static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2803 struct path *path, struct file *file,
2804 const struct open_flags *op,
64894cf8 2805 bool got_write, bool need_lookup,
2675a4eb 2806 int *opened)
d18e9008
MS
2807{
2808 struct inode *dir = nd->path.dentry->d_inode;
2809 unsigned open_flag = open_to_namei_flags(op->open_flag);
2810 umode_t mode;
2811 int error;
2812 int acc_mode;
d18e9008
MS
2813 int create_error = 0;
2814 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
116cc022 2815 bool excl;
d18e9008
MS
2816
2817 BUG_ON(dentry->d_inode);
2818
2819 /* Don't create child dentry for a dead directory. */
2820 if (unlikely(IS_DEADDIR(dir))) {
2675a4eb 2821 error = -ENOENT;
d18e9008
MS
2822 goto out;
2823 }
2824
62b259d8 2825 mode = op->mode;
d18e9008
MS
2826 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2827 mode &= ~current_umask();
2828
116cc022
MS
2829 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2830 if (excl)
d18e9008 2831 open_flag &= ~O_TRUNC;
d18e9008
MS
2832
2833 /*
2834 * Checking write permission is tricky, bacuse we don't know if we are
2835 * going to actually need it: O_CREAT opens should work as long as the
2836 * file exists. But checking existence breaks atomicity. The trick is
2837 * to check access and if not granted clear O_CREAT from the flags.
2838 *
2839 * Another problem is returing the "right" error value (e.g. for an
2840 * O_EXCL open we want to return EEXIST not EROFS).
2841 */
64894cf8
AV
2842 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2843 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2844 if (!(open_flag & O_CREAT)) {
d18e9008
MS
2845 /*
2846 * No O_CREATE -> atomicity not a requirement -> fall
2847 * back to lookup + open
2848 */
2849 goto no_open;
2850 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2851 /* Fall back and fail with the right error */
64894cf8 2852 create_error = -EROFS;
d18e9008
MS
2853 goto no_open;
2854 } else {
2855 /* No side effects, safe to clear O_CREAT */
64894cf8 2856 create_error = -EROFS;
d18e9008
MS
2857 open_flag &= ~O_CREAT;
2858 }
2859 }
2860
2861 if (open_flag & O_CREAT) {
38227f78 2862 error = may_o_create(&nd->path, dentry, mode);
d18e9008
MS
2863 if (error) {
2864 create_error = error;
2865 if (open_flag & O_EXCL)
2866 goto no_open;
2867 open_flag &= ~O_CREAT;
2868 }
2869 }
2870
2871 if (nd->flags & LOOKUP_DIRECTORY)
2872 open_flag |= O_DIRECTORY;
2873
30d90494
AV
2874 file->f_path.dentry = DENTRY_NOT_SET;
2875 file->f_path.mnt = nd->path.mnt;
2876 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
47237687 2877 opened);
d9585277 2878 if (error < 0) {
d9585277
AV
2879 if (create_error && error == -ENOENT)
2880 error = create_error;
d18e9008
MS
2881 goto out;
2882 }
2883
d9585277 2884 if (error) { /* returned 1, that is */
30d90494 2885 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb 2886 error = -EIO;
d18e9008
MS
2887 goto out;
2888 }
30d90494 2889 if (file->f_path.dentry) {
d18e9008 2890 dput(dentry);
30d90494 2891 dentry = file->f_path.dentry;
d18e9008 2892 }
03da633a
AV
2893 if (*opened & FILE_CREATED)
2894 fsnotify_create(dir, dentry);
2895 if (!dentry->d_inode) {
2896 WARN_ON(*opened & FILE_CREATED);
2897 if (create_error) {
2898 error = create_error;
2899 goto out;
2900 }
2901 } else {
2902 if (excl && !(*opened & FILE_CREATED)) {
2903 error = -EEXIST;
2904 goto out;
2905 }
62b2ce96 2906 }
d18e9008
MS
2907 goto looked_up;
2908 }
2909
2910 /*
2911 * We didn't have the inode before the open, so check open permission
2912 * here.
2913 */
03da633a
AV
2914 acc_mode = op->acc_mode;
2915 if (*opened & FILE_CREATED) {
2916 WARN_ON(!(open_flag & O_CREAT));
2917 fsnotify_create(dir, dentry);
2918 acc_mode = MAY_OPEN;
2919 }
2675a4eb
AV
2920 error = may_open(&file->f_path, acc_mode, open_flag);
2921 if (error)
2922 fput(file);
d18e9008
MS
2923
2924out:
2925 dput(dentry);
2675a4eb 2926 return error;
d18e9008 2927
d18e9008
MS
2928no_open:
2929 if (need_lookup) {
72bd866a 2930 dentry = lookup_real(dir, dentry, nd->flags);
d18e9008 2931 if (IS_ERR(dentry))
2675a4eb 2932 return PTR_ERR(dentry);
75a6552a
AV
2933 }
2934 if (create_error && !dentry->d_inode) {
2935 error = create_error;
2936 goto out;
d18e9008
MS
2937 }
2938looked_up:
2939 path->dentry = dentry;
2940 path->mnt = nd->path.mnt;
2675a4eb 2941 return 1;
d18e9008
MS
2942}
2943
d58ffd35 2944/*
1acf0af9 2945 * Look up and maybe create and open the last component.
d58ffd35
MS
2946 *
2947 * Must be called with i_mutex held on parent.
2948 *
1acf0af9
DH
2949 * Returns 0 if the file was successfully atomically created (if necessary) and
2950 * opened. In this case the file will be returned attached to @file.
2951 *
2952 * Returns 1 if the file was not completely opened at this time, though lookups
2953 * and creations will have been performed and the dentry returned in @path will
2954 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2955 * specified then a negative dentry may be returned.
2956 *
2957 * An error code is returned otherwise.
2958 *
2959 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2960 * cleared otherwise prior to returning.
d58ffd35 2961 */
2675a4eb
AV
2962static int lookup_open(struct nameidata *nd, struct path *path,
2963 struct file *file,
2964 const struct open_flags *op,
64894cf8 2965 bool got_write, int *opened)
d58ffd35
MS
2966{
2967 struct dentry *dir = nd->path.dentry;
54ef4872 2968 struct inode *dir_inode = dir->d_inode;
d58ffd35
MS
2969 struct dentry *dentry;
2970 int error;
54ef4872 2971 bool need_lookup;
d58ffd35 2972
47237687 2973 *opened &= ~FILE_CREATED;
201f956e 2974 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
d58ffd35 2975 if (IS_ERR(dentry))
2675a4eb 2976 return PTR_ERR(dentry);
d58ffd35 2977
d18e9008
MS
2978 /* Cached positive dentry: will open in f_op->open */
2979 if (!need_lookup && dentry->d_inode)
2980 goto out_no_open;
2981
2982 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
64894cf8 2983 return atomic_open(nd, dentry, path, file, op, got_write,
47237687 2984 need_lookup, opened);
d18e9008
MS
2985 }
2986
54ef4872
MS
2987 if (need_lookup) {
2988 BUG_ON(dentry->d_inode);
2989
72bd866a 2990 dentry = lookup_real(dir_inode, dentry, nd->flags);
54ef4872 2991 if (IS_ERR(dentry))
2675a4eb 2992 return PTR_ERR(dentry);
54ef4872
MS
2993 }
2994
d58ffd35
MS
2995 /* Negative dentry, just create the file */
2996 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2997 umode_t mode = op->mode;
2998 if (!IS_POSIXACL(dir->d_inode))
2999 mode &= ~current_umask();
3000 /*
3001 * This write is needed to ensure that a
3002 * rw->ro transition does not occur between
3003 * the time when the file is created and when
3004 * a permanent write count is taken through
015c3bbc 3005 * the 'struct file' in finish_open().
d58ffd35 3006 */
64894cf8
AV
3007 if (!got_write) {
3008 error = -EROFS;
d58ffd35 3009 goto out_dput;
64894cf8 3010 }
47237687 3011 *opened |= FILE_CREATED;
d58ffd35
MS
3012 error = security_path_mknod(&nd->path, dentry, mode, 0);
3013 if (error)
3014 goto out_dput;
312b63fb
AV
3015 error = vfs_create(dir->d_inode, dentry, mode,
3016 nd->flags & LOOKUP_EXCL);
d58ffd35
MS
3017 if (error)
3018 goto out_dput;
3019 }
d18e9008 3020out_no_open:
d58ffd35
MS
3021 path->dentry = dentry;
3022 path->mnt = nd->path.mnt;
2675a4eb 3023 return 1;
d58ffd35
MS
3024
3025out_dput:
3026 dput(dentry);
2675a4eb 3027 return error;
d58ffd35
MS
3028}
3029
31e6b01f 3030/*
fe2d35ff 3031 * Handle the last step of open()
31e6b01f 3032 */
896475d5 3033static int do_last(struct nameidata *nd,
2675a4eb 3034 struct file *file, const struct open_flags *op,
76ae2a5a 3035 int *opened)
fb1cc555 3036{
a1e28038 3037 struct dentry *dir = nd->path.dentry;
ca344a89 3038 int open_flag = op->open_flag;
77d660a8 3039 bool will_truncate = (open_flag & O_TRUNC) != 0;
64894cf8 3040 bool got_write = false;
bcda7652 3041 int acc_mode = op->acc_mode;
254cf582 3042 unsigned seq;
a1eb3315 3043 struct inode *inode;
16b1c1cd 3044 struct path save_parent = { .dentry = NULL, .mnt = NULL };
896475d5 3045 struct path path;
16b1c1cd 3046 bool retried = false;
16c2cd71 3047 int error;
1f36f774 3048
c3e380b0
AV
3049 nd->flags &= ~LOOKUP_PARENT;
3050 nd->flags |= op->intent;
3051
bc77daa7 3052 if (nd->last_type != LAST_NORM) {
fe2d35ff 3053 error = handle_dots(nd, nd->last_type);
deb106c6 3054 if (unlikely(error))
2675a4eb 3055 return error;
e83db167 3056 goto finish_open;
1f36f774 3057 }
67ee3ad2 3058
ca344a89 3059 if (!(open_flag & O_CREAT)) {
fe2d35ff
AV
3060 if (nd->last.name[nd->last.len])
3061 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3062 /* we _can_ be in RCU mode here */
254cf582 3063 error = lookup_fast(nd, &path, &inode, &seq);
71574865
MS
3064 if (likely(!error))
3065 goto finish_lookup;
3066
3067 if (error < 0)
deb106c6 3068 return error;
71574865
MS
3069
3070 BUG_ON(nd->inode != dir->d_inode);
b6183df7
MS
3071 } else {
3072 /* create side of things */
3073 /*
3074 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3075 * has been cleared when we got to the last component we are
3076 * about to look up
3077 */
3078 error = complete_walk(nd);
e8bb73df 3079 if (error)
2675a4eb 3080 return error;
fe2d35ff 3081
76ae2a5a 3082 audit_inode(nd->name, dir, LOOKUP_PARENT);
b6183df7 3083 /* trailing slashes? */
deb106c6
AV
3084 if (unlikely(nd->last.name[nd->last.len]))
3085 return -EISDIR;
b6183df7 3086 }
a2c36b45 3087
16b1c1cd 3088retry_lookup:
64894cf8
AV
3089 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3090 error = mnt_want_write(nd->path.mnt);
3091 if (!error)
3092 got_write = true;
3093 /*
3094 * do _not_ fail yet - we might not need that or fail with
3095 * a different error; let lookup_open() decide; we'll be
3096 * dropping this one anyway.
3097 */
3098 }
a1e28038 3099 mutex_lock(&dir->d_inode->i_mutex);
896475d5 3100 error = lookup_open(nd, &path, file, op, got_write, opened);
d58ffd35 3101 mutex_unlock(&dir->d_inode->i_mutex);
a1e28038 3102
2675a4eb
AV
3103 if (error <= 0) {
3104 if (error)
d18e9008
MS
3105 goto out;
3106
47237687 3107 if ((*opened & FILE_CREATED) ||
496ad9aa 3108 !S_ISREG(file_inode(file)->i_mode))
77d660a8 3109 will_truncate = false;
d18e9008 3110
76ae2a5a 3111 audit_inode(nd->name, file->f_path.dentry, 0);
d18e9008
MS
3112 goto opened;
3113 }
fb1cc555 3114
47237687 3115 if (*opened & FILE_CREATED) {
9b44f1b3 3116 /* Don't check for write permission, don't truncate */
ca344a89 3117 open_flag &= ~O_TRUNC;
77d660a8 3118 will_truncate = false;
bcda7652 3119 acc_mode = MAY_OPEN;
896475d5 3120 path_to_nameidata(&path, nd);
e83db167 3121 goto finish_open_created;
fb1cc555
AV
3122 }
3123
3124 /*
3134f37e 3125 * create/update audit record if it already exists.
fb1cc555 3126 */
896475d5 3127 if (d_is_positive(path.dentry))
76ae2a5a 3128 audit_inode(nd->name, path.dentry, 0);
fb1cc555 3129
d18e9008
MS
3130 /*
3131 * If atomic_open() acquired write access it is dropped now due to
3132 * possible mount and symlink following (this might be optimized away if
3133 * necessary...)
3134 */
64894cf8 3135 if (got_write) {
d18e9008 3136 mnt_drop_write(nd->path.mnt);
64894cf8 3137 got_write = false;
d18e9008
MS
3138 }
3139
deb106c6
AV
3140 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3141 path_to_nameidata(&path, nd);
3142 return -EEXIST;
3143 }
fb1cc555 3144
756daf26 3145 error = follow_managed(&path, nd);
deb106c6
AV
3146 if (unlikely(error < 0))
3147 return error;
a3fbbde7 3148
decf3400 3149 BUG_ON(nd->flags & LOOKUP_RCU);
254cf582 3150 seq = 0; /* out of RCU mode, so the value doesn't matter */
deb106c6 3151 if (unlikely(d_is_negative(path.dentry))) {
896475d5 3152 path_to_nameidata(&path, nd);
deb106c6 3153 return -ENOENT;
54c33e7f 3154 }
32b9b145 3155 inode = d_backing_inode(path.dentry);
766c4cbf 3156finish_lookup:
d63ff28f
AV
3157 if (nd->depth)
3158 put_link(nd);
181548c0
AV
3159 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3160 inode, seq);
deb106c6 3161 if (unlikely(error))
d63ff28f 3162 return error;
fb1cc555 3163
896475d5
AV
3164 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path.mnt) {
3165 path_to_nameidata(&path, nd);
16b1c1cd
MS
3166 } else {
3167 save_parent.dentry = nd->path.dentry;
896475d5
AV
3168 save_parent.mnt = mntget(path.mnt);
3169 nd->path.dentry = path.dentry;
16b1c1cd
MS
3170
3171 }
decf3400 3172 nd->inode = inode;
254cf582 3173 nd->seq = seq;
a3fbbde7 3174 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
bc77daa7 3175finish_open:
a3fbbde7 3176 error = complete_walk(nd);
16b1c1cd
MS
3177 if (error) {
3178 path_put(&save_parent);
2675a4eb 3179 return error;
16b1c1cd 3180 }
76ae2a5a 3181 audit_inode(nd->name, nd->path.dentry, 0);
6a2721a3
AV
3182 if (unlikely(d_is_symlink(nd->path.dentry)) && !(open_flag & O_PATH)) {
3183 error = -ELOOP;
3184 goto out;
3185 }
fb1cc555 3186 error = -EISDIR;
44b1d530 3187 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
2675a4eb 3188 goto out;
af2f5542 3189 error = -ENOTDIR;
44b1d530 3190 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
2675a4eb 3191 goto out;
4bbcbd3b 3192 if (!d_is_reg(nd->path.dentry))
77d660a8 3193 will_truncate = false;
6c0d46c4 3194
0f9d1a10
AV
3195 if (will_truncate) {
3196 error = mnt_want_write(nd->path.mnt);
3197 if (error)
2675a4eb 3198 goto out;
64894cf8 3199 got_write = true;
0f9d1a10 3200 }
e83db167 3201finish_open_created:
bcda7652 3202 error = may_open(&nd->path, acc_mode, open_flag);
ca344a89 3203 if (error)
2675a4eb 3204 goto out;
4aa7c634
MS
3205
3206 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3207 error = vfs_open(&nd->path, file, current_cred());
3208 if (!error) {
3209 *opened |= FILE_OPENED;
3210 } else {
30d90494 3211 if (error == -EOPENSTALE)
f60dc3db 3212 goto stale_open;
015c3bbc 3213 goto out;
f60dc3db 3214 }
a8277b9b 3215opened:
2675a4eb 3216 error = open_check_o_direct(file);
015c3bbc
MS
3217 if (error)
3218 goto exit_fput;
3034a146 3219 error = ima_file_check(file, op->acc_mode, *opened);
aa4caadb
MS
3220 if (error)
3221 goto exit_fput;
3222
3223 if (will_truncate) {
2675a4eb 3224 error = handle_truncate(file);
aa4caadb
MS
3225 if (error)
3226 goto exit_fput;
0f9d1a10 3227 }
ca344a89 3228out:
09c56bd9
AV
3229 if (unlikely(error > 0)) {
3230 WARN_ON(1);
3231 error = -EINVAL;
3232 }
64894cf8 3233 if (got_write)
0f9d1a10 3234 mnt_drop_write(nd->path.mnt);
16b1c1cd 3235 path_put(&save_parent);
2675a4eb 3236 return error;
fb1cc555 3237
015c3bbc 3238exit_fput:
2675a4eb
AV
3239 fput(file);
3240 goto out;
015c3bbc 3241
f60dc3db
MS
3242stale_open:
3243 /* If no saved parent or already retried then can't retry */
3244 if (!save_parent.dentry || retried)
3245 goto out;
3246
3247 BUG_ON(save_parent.dentry != dir);
3248 path_put(&nd->path);
3249 nd->path = save_parent;
3250 nd->inode = dir->d_inode;
3251 save_parent.mnt = NULL;
3252 save_parent.dentry = NULL;
64894cf8 3253 if (got_write) {
f60dc3db 3254 mnt_drop_write(nd->path.mnt);
64894cf8 3255 got_write = false;
f60dc3db
MS
3256 }
3257 retried = true;
3258 goto retry_lookup;
fb1cc555
AV
3259}
3260
c8a53ee5 3261static int do_tmpfile(struct nameidata *nd, unsigned flags,
60545d0d
AV
3262 const struct open_flags *op,
3263 struct file *file, int *opened)
3264{
3265 static const struct qstr name = QSTR_INIT("/", 1);
625b6d10 3266 struct dentry *child;
60545d0d 3267 struct inode *dir;
625b6d10 3268 struct path path;
c8a53ee5 3269 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
60545d0d
AV
3270 if (unlikely(error))
3271 return error;
625b6d10 3272 error = mnt_want_write(path.mnt);
60545d0d
AV
3273 if (unlikely(error))
3274 goto out;
625b6d10 3275 dir = path.dentry->d_inode;
60545d0d 3276 /* we want directory to be writable */
625b6d10 3277 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
60545d0d
AV
3278 if (error)
3279 goto out2;
60545d0d
AV
3280 if (!dir->i_op->tmpfile) {
3281 error = -EOPNOTSUPP;
3282 goto out2;
3283 }
625b6d10 3284 child = d_alloc(path.dentry, &name);
60545d0d
AV
3285 if (unlikely(!child)) {
3286 error = -ENOMEM;
3287 goto out2;
3288 }
625b6d10
AV
3289 dput(path.dentry);
3290 path.dentry = child;
3291 error = dir->i_op->tmpfile(dir, child, op->mode);
60545d0d
AV
3292 if (error)
3293 goto out2;
c8a53ee5 3294 audit_inode(nd->name, child, 0);
69a91c23 3295 /* Don't check for other permissions, the inode was just created */
625b6d10 3296 error = may_open(&path, MAY_OPEN, op->open_flag);
60545d0d
AV
3297 if (error)
3298 goto out2;
625b6d10
AV
3299 file->f_path.mnt = path.mnt;
3300 error = finish_open(file, child, NULL, opened);
60545d0d
AV
3301 if (error)
3302 goto out2;
3303 error = open_check_o_direct(file);
f4e0c30c 3304 if (error) {
60545d0d 3305 fput(file);
f4e0c30c
AV
3306 } else if (!(op->open_flag & O_EXCL)) {
3307 struct inode *inode = file_inode(file);
3308 spin_lock(&inode->i_lock);
3309 inode->i_state |= I_LINKABLE;
3310 spin_unlock(&inode->i_lock);
3311 }
60545d0d 3312out2:
625b6d10 3313 mnt_drop_write(path.mnt);
60545d0d 3314out:
625b6d10 3315 path_put(&path);
60545d0d
AV
3316 return error;
3317}
3318
c8a53ee5
AV
3319static struct file *path_openat(struct nameidata *nd,
3320 const struct open_flags *op, unsigned flags)
1da177e4 3321{
368ee9ba 3322 const char *s;
30d90494 3323 struct file *file;
47237687 3324 int opened = 0;
13aab428 3325 int error;
31e6b01f 3326
30d90494 3327 file = get_empty_filp();
1afc99be
AV
3328 if (IS_ERR(file))
3329 return file;
31e6b01f 3330
30d90494 3331 file->f_flags = op->open_flag;
31e6b01f 3332
bb458c64 3333 if (unlikely(file->f_flags & __O_TMPFILE)) {
c8a53ee5 3334 error = do_tmpfile(nd, flags, op, file, &opened);
f15133df 3335 goto out2;
60545d0d
AV
3336 }
3337
c8a53ee5 3338 s = path_init(nd, flags);
368ee9ba
AV
3339 if (IS_ERR(s)) {
3340 put_filp(file);
3341 return ERR_CAST(s);
3342 }
3bdba28b 3343 while (!(error = link_path_walk(s, nd)) &&
76ae2a5a 3344 (error = do_last(nd, file, op, &opened)) > 0) {
73d049a4 3345 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3bdba28b
AV
3346 s = trailing_symlink(nd);
3347 if (IS_ERR(s)) {
3348 error = PTR_ERR(s);
2675a4eb 3349 break;
3bdba28b 3350 }
806b681c 3351 }
deb106c6 3352 terminate_walk(nd);
f15133df 3353out2:
2675a4eb
AV
3354 if (!(opened & FILE_OPENED)) {
3355 BUG_ON(!error);
30d90494 3356 put_filp(file);
16b1c1cd 3357 }
2675a4eb
AV
3358 if (unlikely(error)) {
3359 if (error == -EOPENSTALE) {
3360 if (flags & LOOKUP_RCU)
3361 error = -ECHILD;
3362 else
3363 error = -ESTALE;
3364 }
3365 file = ERR_PTR(error);
3366 }
3367 return file;
1da177e4
LT
3368}
3369
669abf4e 3370struct file *do_filp_open(int dfd, struct filename *pathname,
f9652e10 3371 const struct open_flags *op)
13aab428 3372{
9883d185 3373 struct nameidata nd;
f9652e10 3374 int flags = op->lookup_flags;
13aab428
AV
3375 struct file *filp;
3376
9883d185 3377 set_nameidata(&nd, dfd, pathname);
c8a53ee5 3378 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
13aab428 3379 if (unlikely(filp == ERR_PTR(-ECHILD)))
c8a53ee5 3380 filp = path_openat(&nd, op, flags);
13aab428 3381 if (unlikely(filp == ERR_PTR(-ESTALE)))
c8a53ee5 3382 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d185 3383 restore_nameidata();
13aab428
AV
3384 return filp;
3385}
3386
73d049a4 3387struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
f9652e10 3388 const char *name, const struct open_flags *op)
73d049a4 3389{
9883d185 3390 struct nameidata nd;
73d049a4 3391 struct file *file;
51689104 3392 struct filename *filename;
f9652e10 3393 int flags = op->lookup_flags | LOOKUP_ROOT;
73d049a4
AV
3394
3395 nd.root.mnt = mnt;
3396 nd.root.dentry = dentry;
3397
b18825a7 3398 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
73d049a4
AV
3399 return ERR_PTR(-ELOOP);
3400
51689104 3401 filename = getname_kernel(name);
a1c83681 3402 if (IS_ERR(filename))
51689104
PM
3403 return ERR_CAST(filename);
3404
9883d185 3405 set_nameidata(&nd, -1, filename);
c8a53ee5 3406 file = path_openat(&nd, op, flags | LOOKUP_RCU);
73d049a4 3407 if (unlikely(file == ERR_PTR(-ECHILD)))
c8a53ee5 3408 file = path_openat(&nd, op, flags);
73d049a4 3409 if (unlikely(file == ERR_PTR(-ESTALE)))
c8a53ee5 3410 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
9883d185 3411 restore_nameidata();
51689104 3412 putname(filename);
73d049a4
AV
3413 return file;
3414}
3415
fa14a0b8 3416static struct dentry *filename_create(int dfd, struct filename *name,
1ac12b4b 3417 struct path *path, unsigned int lookup_flags)
1da177e4 3418{
c663e5d8 3419 struct dentry *dentry = ERR_PTR(-EEXIST);
391172c4
AV
3420 struct qstr last;
3421 int type;
c30dabfe 3422 int err2;
1ac12b4b
JL
3423 int error;
3424 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3425
3426 /*
3427 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3428 * other flags passed in are ignored!
3429 */
3430 lookup_flags &= LOOKUP_REVAL;
3431
5c31b6ce
AV
3432 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3433 if (IS_ERR(name))
3434 return ERR_CAST(name);
1da177e4 3435
c663e5d8
CH
3436 /*
3437 * Yucky last component or no last component at all?
3438 * (foo/., foo/.., /////)
3439 */
5c31b6ce 3440 if (unlikely(type != LAST_NORM))
ed75e95d 3441 goto out;
c663e5d8 3442
c30dabfe 3443 /* don't fail immediately if it's r/o, at least try to report other errors */
391172c4 3444 err2 = mnt_want_write(path->mnt);
c663e5d8
CH
3445 /*
3446 * Do the final lookup.
3447 */
391172c4
AV
3448 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3449 mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3450 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
1da177e4 3451 if (IS_ERR(dentry))
a8104a9f 3452 goto unlock;
c663e5d8 3453
a8104a9f 3454 error = -EEXIST;
b18825a7 3455 if (d_is_positive(dentry))
a8104a9f 3456 goto fail;
b18825a7 3457
c663e5d8
CH
3458 /*
3459 * Special case - lookup gave negative, but... we had foo/bar/
3460 * From the vfs_mknod() POV we just have a negative dentry -
3461 * all is fine. Let's be bastards - you had / on the end, you've
3462 * been asking for (non-existent) directory. -ENOENT for you.
3463 */
391172c4 3464 if (unlikely(!is_dir && last.name[last.len])) {
a8104a9f 3465 error = -ENOENT;
ed75e95d 3466 goto fail;
e9baf6e5 3467 }
c30dabfe
JK
3468 if (unlikely(err2)) {
3469 error = err2;
a8104a9f 3470 goto fail;
c30dabfe 3471 }
181c37b6 3472 putname(name);
1da177e4 3473 return dentry;
1da177e4 3474fail:
a8104a9f
AV
3475 dput(dentry);
3476 dentry = ERR_PTR(error);
3477unlock:
391172c4 3478 mutex_unlock(&path->dentry->d_inode->i_mutex);
c30dabfe 3479 if (!err2)
391172c4 3480 mnt_drop_write(path->mnt);
ed75e95d 3481out:
391172c4 3482 path_put(path);
181c37b6 3483 putname(name);
1da177e4
LT
3484 return dentry;
3485}
fa14a0b8
AV
3486
3487struct dentry *kern_path_create(int dfd, const char *pathname,
3488 struct path *path, unsigned int lookup_flags)
3489{
181c37b6
AV
3490 return filename_create(dfd, getname_kernel(pathname),
3491 path, lookup_flags);
fa14a0b8 3492}
dae6ad8f
AV
3493EXPORT_SYMBOL(kern_path_create);
3494
921a1650
AV
3495void done_path_create(struct path *path, struct dentry *dentry)
3496{
3497 dput(dentry);
3498 mutex_unlock(&path->dentry->d_inode->i_mutex);
a8104a9f 3499 mnt_drop_write(path->mnt);
921a1650
AV
3500 path_put(path);
3501}
3502EXPORT_SYMBOL(done_path_create);
3503
520ae687 3504inline struct dentry *user_path_create(int dfd, const char __user *pathname,
1ac12b4b 3505 struct path *path, unsigned int lookup_flags)
dae6ad8f 3506{
181c37b6 3507 return filename_create(dfd, getname(pathname), path, lookup_flags);
dae6ad8f
AV
3508}
3509EXPORT_SYMBOL(user_path_create);
3510
1a67aafb 3511int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4 3512{
a95164d9 3513 int error = may_create(dir, dentry);
1da177e4
LT
3514
3515 if (error)
3516 return error;
3517
975d6b39 3518 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1da177e4
LT
3519 return -EPERM;
3520
acfa4380 3521 if (!dir->i_op->mknod)
1da177e4
LT
3522 return -EPERM;
3523
08ce5f16
SH
3524 error = devcgroup_inode_mknod(mode, dev);
3525 if (error)
3526 return error;
3527
1da177e4
LT
3528 error = security_inode_mknod(dir, dentry, mode, dev);
3529 if (error)
3530 return error;
3531
1da177e4 3532 error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aa 3533 if (!error)
f38aa942 3534 fsnotify_create(dir, dentry);
1da177e4
LT
3535 return error;
3536}
4d359507 3537EXPORT_SYMBOL(vfs_mknod);
1da177e4 3538
f69aac00 3539static int may_mknod(umode_t mode)
463c3197
DH
3540{
3541 switch (mode & S_IFMT) {
3542 case S_IFREG:
3543 case S_IFCHR:
3544 case S_IFBLK:
3545 case S_IFIFO:
3546 case S_IFSOCK:
3547 case 0: /* zero mode translates to S_IFREG */
3548 return 0;
3549 case S_IFDIR:
3550 return -EPERM;
3551 default:
3552 return -EINVAL;
3553 }
3554}
3555
8208a22b 3556SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2e4d0924 3557 unsigned, dev)
1da177e4 3558{
2ad94ae6 3559 struct dentry *dentry;
dae6ad8f
AV
3560 struct path path;
3561 int error;
972567f1 3562 unsigned int lookup_flags = 0;
1da177e4 3563
8e4bfca1
AV
3564 error = may_mknod(mode);
3565 if (error)
3566 return error;
972567f1
JL
3567retry:
3568 dentry = user_path_create(dfd, filename, &path, lookup_flags);
dae6ad8f
AV
3569 if (IS_ERR(dentry))
3570 return PTR_ERR(dentry);
2ad94ae6 3571
dae6ad8f 3572 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3573 mode &= ~current_umask();
dae6ad8f 3574 error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56 3575 if (error)
a8104a9f 3576 goto out;
463c3197 3577 switch (mode & S_IFMT) {
1da177e4 3578 case 0: case S_IFREG:
312b63fb 3579 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
1da177e4
LT
3580 break;
3581 case S_IFCHR: case S_IFBLK:
dae6ad8f 3582 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4
LT
3583 new_decode_dev(dev));
3584 break;
3585 case S_IFIFO: case S_IFSOCK:
dae6ad8f 3586 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4 3587 break;
1da177e4 3588 }
a8104a9f 3589out:
921a1650 3590 done_path_create(&path, dentry);
972567f1
JL
3591 if (retry_estale(error, lookup_flags)) {
3592 lookup_flags |= LOOKUP_REVAL;
3593 goto retry;
3594 }
1da177e4
LT
3595 return error;
3596}
3597
8208a22b 3598SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d
UD
3599{
3600 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3601}
3602
18bb1db3 3603int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4 3604{
a95164d9 3605 int error = may_create(dir, dentry);
8de52778 3606 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
3607
3608 if (error)
3609 return error;
3610
acfa4380 3611 if (!dir->i_op->mkdir)
1da177e4
LT
3612 return -EPERM;
3613
3614 mode &= (S_IRWXUGO|S_ISVTX);
3615 error = security_inode_mkdir(dir, dentry, mode);
3616 if (error)
3617 return error;
3618
8de52778
AV
3619 if (max_links && dir->i_nlink >= max_links)
3620 return -EMLINK;
3621
1da177e4 3622 error = dir->i_op->mkdir(dir, dentry, mode);
a74574aa 3623 if (!error)
f38aa942 3624 fsnotify_mkdir(dir, dentry);
1da177e4
LT
3625 return error;
3626}
4d359507 3627EXPORT_SYMBOL(vfs_mkdir);
1da177e4 3628
a218d0fd 3629SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
1da177e4 3630{
6902d925 3631 struct dentry *dentry;
dae6ad8f
AV
3632 struct path path;
3633 int error;
b76d8b82 3634 unsigned int lookup_flags = LOOKUP_DIRECTORY;
1da177e4 3635
b76d8b82
JL
3636retry:
3637 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
6902d925 3638 if (IS_ERR(dentry))
dae6ad8f 3639 return PTR_ERR(dentry);
1da177e4 3640
dae6ad8f 3641 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3642 mode &= ~current_umask();
dae6ad8f 3643 error = security_path_mkdir(&path, dentry, mode);
a8104a9f
AV
3644 if (!error)
3645 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
921a1650 3646 done_path_create(&path, dentry);
b76d8b82
JL
3647 if (retry_estale(error, lookup_flags)) {
3648 lookup_flags |= LOOKUP_REVAL;
3649 goto retry;
3650 }
1da177e4
LT
3651 return error;
3652}
3653
a218d0fd 3654SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d
UD
3655{
3656 return sys_mkdirat(AT_FDCWD, pathname, mode);
3657}
3658
1da177e4 3659/*
a71905f0 3660 * The dentry_unhash() helper will try to drop the dentry early: we
c0d02594 3661 * should have a usage count of 1 if we're the only user of this
a71905f0
SW
3662 * dentry, and if that is true (possibly after pruning the dcache),
3663 * then we drop the dentry now.
1da177e4
LT
3664 *
3665 * A low-level filesystem can, if it choses, legally
3666 * do a
3667 *
3668 * if (!d_unhashed(dentry))
3669 * return -EBUSY;
3670 *
3671 * if it cannot handle the case of removing a directory
3672 * that is still in use by something else..
3673 */
3674void dentry_unhash(struct dentry *dentry)
3675{
dc168427 3676 shrink_dcache_parent(dentry);
1da177e4 3677 spin_lock(&dentry->d_lock);
98474236 3678 if (dentry->d_lockref.count == 1)
1da177e4
LT
3679 __d_drop(dentry);
3680 spin_unlock(&dentry->d_lock);
1da177e4 3681}
4d359507 3682EXPORT_SYMBOL(dentry_unhash);
1da177e4
LT
3683
3684int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3685{
3686 int error = may_delete(dir, dentry, 1);
3687
3688 if (error)
3689 return error;
3690
acfa4380 3691 if (!dir->i_op->rmdir)
1da177e4
LT
3692 return -EPERM;
3693
1d2ef590 3694 dget(dentry);
1b1dcc1b 3695 mutex_lock(&dentry->d_inode->i_mutex);
912dbc15
SW
3696
3697 error = -EBUSY;
7af1364f 3698 if (is_local_mountpoint(dentry))
912dbc15
SW
3699 goto out;
3700
3701 error = security_inode_rmdir(dir, dentry);
3702 if (error)
3703 goto out;
3704
3cebde24 3705 shrink_dcache_parent(dentry);
912dbc15
SW
3706 error = dir->i_op->rmdir(dir, dentry);
3707 if (error)
3708 goto out;
3709
3710 dentry->d_inode->i_flags |= S_DEAD;
3711 dont_mount(dentry);
8ed936b5 3712 detach_mounts(dentry);
912dbc15
SW
3713
3714out:
1b1dcc1b 3715 mutex_unlock(&dentry->d_inode->i_mutex);
1d2ef590 3716 dput(dentry);
912dbc15 3717 if (!error)
1da177e4 3718 d_delete(dentry);
1da177e4
LT
3719 return error;
3720}
4d359507 3721EXPORT_SYMBOL(vfs_rmdir);
1da177e4 3722
5590ff0d 3723static long do_rmdir(int dfd, const char __user *pathname)
1da177e4
LT
3724{
3725 int error = 0;
91a27b2a 3726 struct filename *name;
1da177e4 3727 struct dentry *dentry;
f5beed75
AV
3728 struct path path;
3729 struct qstr last;
3730 int type;
c6ee9206
JL
3731 unsigned int lookup_flags = 0;
3732retry:
f5beed75
AV
3733 name = user_path_parent(dfd, pathname,
3734 &path, &last, &type, lookup_flags);
91a27b2a
JL
3735 if (IS_ERR(name))
3736 return PTR_ERR(name);
1da177e4 3737
f5beed75 3738 switch (type) {
0612d9fb
OH
3739 case LAST_DOTDOT:
3740 error = -ENOTEMPTY;
3741 goto exit1;
3742 case LAST_DOT:
3743 error = -EINVAL;
3744 goto exit1;
3745 case LAST_ROOT:
3746 error = -EBUSY;
3747 goto exit1;
1da177e4 3748 }
0612d9fb 3749
f5beed75 3750 error = mnt_want_write(path.mnt);
c30dabfe
JK
3751 if (error)
3752 goto exit1;
0612d9fb 3753
f5beed75
AV
3754 mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3755 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
1da177e4 3756 error = PTR_ERR(dentry);
6902d925
DH
3757 if (IS_ERR(dentry))
3758 goto exit2;
e6bc45d6
TT
3759 if (!dentry->d_inode) {
3760 error = -ENOENT;
3761 goto exit3;
3762 }
f5beed75 3763 error = security_path_rmdir(&path, dentry);
be6d3e56 3764 if (error)
c30dabfe 3765 goto exit3;
f5beed75 3766 error = vfs_rmdir(path.dentry->d_inode, dentry);
0622753b 3767exit3:
6902d925
DH
3768 dput(dentry);
3769exit2:
f5beed75
AV
3770 mutex_unlock(&path.dentry->d_inode->i_mutex);
3771 mnt_drop_write(path.mnt);
1da177e4 3772exit1:
f5beed75 3773 path_put(&path);
1da177e4 3774 putname(name);
c6ee9206
JL
3775 if (retry_estale(error, lookup_flags)) {
3776 lookup_flags |= LOOKUP_REVAL;
3777 goto retry;
3778 }
1da177e4
LT
3779 return error;
3780}
3781
3cdad428 3782SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d
UD
3783{
3784 return do_rmdir(AT_FDCWD, pathname);
3785}
3786
b21996e3
BF
3787/**
3788 * vfs_unlink - unlink a filesystem object
3789 * @dir: parent directory
3790 * @dentry: victim
3791 * @delegated_inode: returns victim inode, if the inode is delegated.
3792 *
3793 * The caller must hold dir->i_mutex.
3794 *
3795 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3796 * return a reference to the inode in delegated_inode. The caller
3797 * should then break the delegation on that inode and retry. Because
3798 * breaking a delegation may take a long time, the caller should drop
3799 * dir->i_mutex before doing so.
3800 *
3801 * Alternatively, a caller may pass NULL for delegated_inode. This may
3802 * be appropriate for callers that expect the underlying filesystem not
3803 * to be NFS exported.
3804 */
3805int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
1da177e4 3806{
9accbb97 3807 struct inode *target = dentry->d_inode;
1da177e4
LT
3808 int error = may_delete(dir, dentry, 0);
3809
3810 if (error)
3811 return error;
3812
acfa4380 3813 if (!dir->i_op->unlink)
1da177e4
LT
3814 return -EPERM;
3815
9accbb97 3816 mutex_lock(&target->i_mutex);
8ed936b5 3817 if (is_local_mountpoint(dentry))
1da177e4
LT
3818 error = -EBUSY;
3819 else {
3820 error = security_inode_unlink(dir, dentry);
bec1052e 3821 if (!error) {
5a14696c
BF
3822 error = try_break_deleg(target, delegated_inode);
3823 if (error)
b21996e3 3824 goto out;
1da177e4 3825 error = dir->i_op->unlink(dir, dentry);
8ed936b5 3826 if (!error) {
d83c49f3 3827 dont_mount(dentry);
8ed936b5
EB
3828 detach_mounts(dentry);
3829 }
bec1052e 3830 }
1da177e4 3831 }
b21996e3 3832out:
9accbb97 3833 mutex_unlock(&target->i_mutex);
1da177e4
LT
3834
3835 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3836 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
9accbb97 3837 fsnotify_link_count(target);
e234f35c 3838 d_delete(dentry);
1da177e4 3839 }
0eeca283 3840
1da177e4
LT
3841 return error;
3842}
4d359507 3843EXPORT_SYMBOL(vfs_unlink);
1da177e4
LT
3844
3845/*
3846 * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b 3847 * directory's i_mutex. Truncate can take a long time if there is a lot of
1da177e4
LT
3848 * writeout happening, and we don't want to prevent access to the directory
3849 * while waiting on the I/O.
3850 */
5590ff0d 3851static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4 3852{
2ad94ae6 3853 int error;
91a27b2a 3854 struct filename *name;
1da177e4 3855 struct dentry *dentry;
f5beed75
AV
3856 struct path path;
3857 struct qstr last;
3858 int type;
1da177e4 3859 struct inode *inode = NULL;
b21996e3 3860 struct inode *delegated_inode = NULL;
5d18f813
JL
3861 unsigned int lookup_flags = 0;
3862retry:
f5beed75
AV
3863 name = user_path_parent(dfd, pathname,
3864 &path, &last, &type, lookup_flags);
91a27b2a
JL
3865 if (IS_ERR(name))
3866 return PTR_ERR(name);
2ad94ae6 3867
1da177e4 3868 error = -EISDIR;
f5beed75 3869 if (type != LAST_NORM)
1da177e4 3870 goto exit1;
0612d9fb 3871
f5beed75 3872 error = mnt_want_write(path.mnt);
c30dabfe
JK
3873 if (error)
3874 goto exit1;
b21996e3 3875retry_deleg:
f5beed75
AV
3876 mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3877 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
1da177e4
LT
3878 error = PTR_ERR(dentry);
3879 if (!IS_ERR(dentry)) {
3880 /* Why not before? Because we want correct error value */
f5beed75 3881 if (last.name[last.len])
50338b88 3882 goto slashes;
1da177e4 3883 inode = dentry->d_inode;
b18825a7 3884 if (d_is_negative(dentry))
e6bc45d6
TT
3885 goto slashes;
3886 ihold(inode);
f5beed75 3887 error = security_path_unlink(&path, dentry);
be6d3e56 3888 if (error)
c30dabfe 3889 goto exit2;
f5beed75 3890 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
c30dabfe 3891exit2:
1da177e4
LT
3892 dput(dentry);
3893 }
f5beed75 3894 mutex_unlock(&path.dentry->d_inode->i_mutex);
1da177e4
LT
3895 if (inode)
3896 iput(inode); /* truncate the inode here */
b21996e3
BF
3897 inode = NULL;
3898 if (delegated_inode) {
5a14696c 3899 error = break_deleg_wait(&delegated_inode);
b21996e3
BF
3900 if (!error)
3901 goto retry_deleg;
3902 }
f5beed75 3903 mnt_drop_write(path.mnt);
1da177e4 3904exit1:
f5beed75 3905 path_put(&path);
1da177e4 3906 putname(name);
5d18f813
JL
3907 if (retry_estale(error, lookup_flags)) {
3908 lookup_flags |= LOOKUP_REVAL;
3909 inode = NULL;
3910 goto retry;
3911 }
1da177e4
LT
3912 return error;
3913
3914slashes:
b18825a7
DH
3915 if (d_is_negative(dentry))
3916 error = -ENOENT;
44b1d530 3917 else if (d_is_dir(dentry))
b18825a7
DH
3918 error = -EISDIR;
3919 else
3920 error = -ENOTDIR;
1da177e4
LT
3921 goto exit2;
3922}
3923
2e4d0924 3924SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d
UD
3925{
3926 if ((flag & ~AT_REMOVEDIR) != 0)
3927 return -EINVAL;
3928
3929 if (flag & AT_REMOVEDIR)
3930 return do_rmdir(dfd, pathname);
3931
3932 return do_unlinkat(dfd, pathname);
3933}
3934
3480b257 3935SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d
UD
3936{
3937 return do_unlinkat(AT_FDCWD, pathname);
3938}
3939
db2e747b 3940int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4 3941{
a95164d9 3942 int error = may_create(dir, dentry);
1da177e4
LT
3943
3944 if (error)
3945 return error;
3946
acfa4380 3947 if (!dir->i_op->symlink)
1da177e4
LT
3948 return -EPERM;
3949
3950 error = security_inode_symlink(dir, dentry, oldname);
3951 if (error)
3952 return error;
3953
1da177e4 3954 error = dir->i_op->symlink(dir, dentry, oldname);
a74574aa 3955 if (!error)
f38aa942 3956 fsnotify_create(dir, dentry);
1da177e4
LT
3957 return error;
3958}
4d359507 3959EXPORT_SYMBOL(vfs_symlink);
1da177e4 3960
2e4d0924
HC
3961SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3962 int, newdfd, const char __user *, newname)
1da177e4 3963{
2ad94ae6 3964 int error;
91a27b2a 3965 struct filename *from;
6902d925 3966 struct dentry *dentry;
dae6ad8f 3967 struct path path;
f46d3567 3968 unsigned int lookup_flags = 0;
1da177e4
LT
3969
3970 from = getname(oldname);
2ad94ae6 3971 if (IS_ERR(from))
1da177e4 3972 return PTR_ERR(from);
f46d3567
JL
3973retry:
3974 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
6902d925
DH
3975 error = PTR_ERR(dentry);
3976 if (IS_ERR(dentry))
dae6ad8f 3977 goto out_putname;
6902d925 3978
91a27b2a 3979 error = security_path_symlink(&path, dentry, from->name);
a8104a9f 3980 if (!error)
91a27b2a 3981 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
921a1650 3982 done_path_create(&path, dentry);
f46d3567
JL
3983 if (retry_estale(error, lookup_flags)) {
3984 lookup_flags |= LOOKUP_REVAL;
3985 goto retry;
3986 }
6902d925 3987out_putname:
1da177e4
LT
3988 putname(from);
3989 return error;
3990}
3991
3480b257 3992SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d
UD
3993{
3994 return sys_symlinkat(oldname, AT_FDCWD, newname);
3995}
3996
146a8595
BF
3997/**
3998 * vfs_link - create a new link
3999 * @old_dentry: object to be linked
4000 * @dir: new parent
4001 * @new_dentry: where to create the new link
4002 * @delegated_inode: returns inode needing a delegation break
4003 *
4004 * The caller must hold dir->i_mutex
4005 *
4006 * If vfs_link discovers a delegation on the to-be-linked file in need
4007 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4008 * inode in delegated_inode. The caller should then break the delegation
4009 * and retry. Because breaking a delegation may take a long time, the
4010 * caller should drop the i_mutex before doing so.
4011 *
4012 * Alternatively, a caller may pass NULL for delegated_inode. This may
4013 * be appropriate for callers that expect the underlying filesystem not
4014 * to be NFS exported.
4015 */
4016int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
1da177e4
LT
4017{
4018 struct inode *inode = old_dentry->d_inode;
8de52778 4019 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
4020 int error;
4021
4022 if (!inode)
4023 return -ENOENT;
4024
a95164d9 4025 error = may_create(dir, new_dentry);
1da177e4
LT
4026 if (error)
4027 return error;
4028
4029 if (dir->i_sb != inode->i_sb)
4030 return -EXDEV;
4031
4032 /*
4033 * A link to an append-only or immutable file cannot be created.
4034 */
4035 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4036 return -EPERM;
be494dcc
EB
4037 /*
4038 * Updating the link count will likely cause i_uid and i_gid to
4039 * be writen back improperly if their true value is unknown to
4040 * the vfs.
4041 */
4042 if (HAS_UNMAPPED_ID(inode))
4043 return -EPERM;
acfa4380 4044 if (!dir->i_op->link)
1da177e4 4045 return -EPERM;
7e79eedb 4046 if (S_ISDIR(inode->i_mode))
1da177e4
LT
4047 return -EPERM;
4048
4049 error = security_inode_link(old_dentry, dir, new_dentry);
4050 if (error)
4051 return error;
4052
7e79eedb 4053 mutex_lock(&inode->i_mutex);
aae8a97d 4054 /* Make sure we don't allow creating hardlink to an unlinked file */
f4e0c30c 4055 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
aae8a97d 4056 error = -ENOENT;
8de52778
AV
4057 else if (max_links && inode->i_nlink >= max_links)
4058 error = -EMLINK;
146a8595
BF
4059 else {
4060 error = try_break_deleg(inode, delegated_inode);
4061 if (!error)
4062 error = dir->i_op->link(old_dentry, dir, new_dentry);
4063 }
f4e0c30c
AV
4064
4065 if (!error && (inode->i_state & I_LINKABLE)) {
4066 spin_lock(&inode->i_lock);
4067 inode->i_state &= ~I_LINKABLE;
4068 spin_unlock(&inode->i_lock);
4069 }
7e79eedb 4070 mutex_unlock(&inode->i_mutex);
e31e14ec 4071 if (!error)
7e79eedb 4072 fsnotify_link(dir, inode, new_dentry);
1da177e4
LT
4073 return error;
4074}
4d359507 4075EXPORT_SYMBOL(vfs_link);
1da177e4
LT
4076
4077/*
4078 * Hardlinks are often used in delicate situations. We avoid
4079 * security-related surprises by not following symlinks on the
4080 * newname. --KAB
4081 *
4082 * We don't follow them on the oldname either to be compatible
4083 * with linux 2.0, and to avoid hard-linking to directories
4084 * and other special files. --ADM
4085 */
2e4d0924
HC
4086SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4087 int, newdfd, const char __user *, newname, int, flags)
1da177e4
LT
4088{
4089 struct dentry *new_dentry;
dae6ad8f 4090 struct path old_path, new_path;
146a8595 4091 struct inode *delegated_inode = NULL;
11a7b371 4092 int how = 0;
1da177e4 4093 int error;
1da177e4 4094
11a7b371 4095 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e1 4096 return -EINVAL;
11a7b371 4097 /*
f0cc6ffb
LT
4098 * To use null names we require CAP_DAC_READ_SEARCH
4099 * This ensures that not everyone will be able to create
4100 * handlink using the passed filedescriptor.
11a7b371 4101 */
f0cc6ffb
LT
4102 if (flags & AT_EMPTY_PATH) {
4103 if (!capable(CAP_DAC_READ_SEARCH))
4104 return -ENOENT;
11a7b371 4105 how = LOOKUP_EMPTY;
f0cc6ffb 4106 }
11a7b371
AK
4107
4108 if (flags & AT_SYMLINK_FOLLOW)
4109 how |= LOOKUP_FOLLOW;
442e31ca 4110retry:
11a7b371 4111 error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4 4112 if (error)
2ad94ae6
AV
4113 return error;
4114
442e31ca
JL
4115 new_dentry = user_path_create(newdfd, newname, &new_path,
4116 (how & LOOKUP_REVAL));
1da177e4 4117 error = PTR_ERR(new_dentry);
6902d925 4118 if (IS_ERR(new_dentry))
dae6ad8f
AV
4119 goto out;
4120
4121 error = -EXDEV;
4122 if (old_path.mnt != new_path.mnt)
4123 goto out_dput;
800179c9
KC
4124 error = may_linkat(&old_path);
4125 if (unlikely(error))
4126 goto out_dput;
dae6ad8f 4127 error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56 4128 if (error)
a8104a9f 4129 goto out_dput;
146a8595 4130 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
75c3f29d 4131out_dput:
921a1650 4132 done_path_create(&new_path, new_dentry);
146a8595
BF
4133 if (delegated_inode) {
4134 error = break_deleg_wait(&delegated_inode);
d22e6338
OD
4135 if (!error) {
4136 path_put(&old_path);
146a8595 4137 goto retry;
d22e6338 4138 }
146a8595 4139 }
442e31ca 4140 if (retry_estale(error, how)) {
d22e6338 4141 path_put(&old_path);
442e31ca
JL
4142 how |= LOOKUP_REVAL;
4143 goto retry;
4144 }
1da177e4 4145out:
2d8f3038 4146 path_put(&old_path);
1da177e4
LT
4147
4148 return error;
4149}
4150
3480b257 4151SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d 4152{
c04030e1 4153 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
4154}
4155
bc27027a
MS
4156/**
4157 * vfs_rename - rename a filesystem object
4158 * @old_dir: parent of source
4159 * @old_dentry: source
4160 * @new_dir: parent of destination
4161 * @new_dentry: destination
4162 * @delegated_inode: returns an inode needing a delegation break
520c8b16 4163 * @flags: rename flags
bc27027a
MS
4164 *
4165 * The caller must hold multiple mutexes--see lock_rename()).
4166 *
4167 * If vfs_rename discovers a delegation in need of breaking at either
4168 * the source or destination, it will return -EWOULDBLOCK and return a
4169 * reference to the inode in delegated_inode. The caller should then
4170 * break the delegation and retry. Because breaking a delegation may
4171 * take a long time, the caller should drop all locks before doing
4172 * so.
4173 *
4174 * Alternatively, a caller may pass NULL for delegated_inode. This may
4175 * be appropriate for callers that expect the underlying filesystem not
4176 * to be NFS exported.
4177 *
1da177e4
LT
4178 * The worst of all namespace operations - renaming directory. "Perverted"
4179 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4180 * Problems:
d03b29a2 4181 * a) we can get into loop creation.
1da177e4
LT
4182 * b) race potential - two innocent renames can create a loop together.
4183 * That's where 4.4 screws up. Current fix: serialization on
a11f3a05 4184 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4 4185 * story.
6cedba89
BF
4186 * c) we have to lock _four_ objects - parents and victim (if it exists),
4187 * and source (if it is not a directory).
1b1dcc1b 4188 * And that - after we got ->i_mutex on parents (until then we don't know
1da177e4
LT
4189 * whether the target exists). Solution: try to be smart with locking
4190 * order for inodes. We rely on the fact that tree topology may change
a11f3a05 4191 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4
LT
4192 * move will be locked. Thus we can rank directories by the tree
4193 * (ancestors first) and rank all non-directories after them.
4194 * That works since everybody except rename does "lock parent, lookup,
a11f3a05 4195 * lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4
LT
4196 * HOWEVER, it relies on the assumption that any object with ->lookup()
4197 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4198 * we'd better make sure that there's no link(2) for them.
e4eaac06 4199 * d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b 4200 * we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4 4201 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e7 4202 * ->i_mutex on parents, which works but leads to some truly excessive
1da177e4
LT
4203 * locking].
4204 */
bc27027a
MS
4205int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4206 struct inode *new_dir, struct dentry *new_dentry,
520c8b16 4207 struct inode **delegated_inode, unsigned int flags)
1da177e4 4208{
bc27027a
MS
4209 int error;
4210 bool is_dir = d_is_dir(old_dentry);
bc27027a 4211 struct inode *source = old_dentry->d_inode;
9055cba7 4212 struct inode *target = new_dentry->d_inode;
da1ce067
MS
4213 bool new_is_dir = false;
4214 unsigned max_links = new_dir->i_sb->s_max_links;
626f7c23 4215 struct name_snapshot old_name;
bc27027a 4216
1f17780e
MS
4217 /*
4218 * Check source == target.
4219 * On overlayfs need to look at underlying inodes.
4220 */
4221 if (vfs_select_inode(old_dentry, 0) == vfs_select_inode(new_dentry, 0))
bc27027a
MS
4222 return 0;
4223
4224 error = may_delete(old_dir, old_dentry, is_dir);
4225 if (error)
4226 return error;
4227
da1ce067 4228 if (!target) {
bc27027a 4229 error = may_create(new_dir, new_dentry);
da1ce067
MS
4230 } else {
4231 new_is_dir = d_is_dir(new_dentry);
4232
4233 if (!(flags & RENAME_EXCHANGE))
4234 error = may_delete(new_dir, new_dentry, is_dir);
4235 else
4236 error = may_delete(new_dir, new_dentry, new_is_dir);
4237 }
bc27027a
MS
4238 if (error)
4239 return error;
4240
7177a9c4 4241 if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
bc27027a 4242 return -EPERM;
1da177e4 4243
520c8b16
MS
4244 if (flags && !old_dir->i_op->rename2)
4245 return -EINVAL;
4246
1da177e4
LT
4247 /*
4248 * If we are going to change the parent - check write permissions,
4249 * we'll need to flip '..'.
4250 */
da1ce067
MS
4251 if (new_dir != old_dir) {
4252 if (is_dir) {
4253 error = inode_permission(source, MAY_WRITE);
4254 if (error)
4255 return error;
4256 }
4257 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4258 error = inode_permission(target, MAY_WRITE);
4259 if (error)
4260 return error;
4261 }
1da177e4
LT
4262 }
4263
0b3974eb
MS
4264 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4265 flags);
1da177e4
LT
4266 if (error)
4267 return error;
4268
626f7c23 4269 take_dentry_name_snapshot(&old_name, old_dentry);
1d2ef590 4270 dget(new_dentry);
da1ce067 4271 if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a
MS
4272 lock_two_nondirectories(source, target);
4273 else if (target)
1b1dcc1b 4274 mutex_lock(&target->i_mutex);
9055cba7
SW
4275
4276 error = -EBUSY;
7af1364f 4277 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
9055cba7
SW
4278 goto out;
4279
da1ce067 4280 if (max_links && new_dir != old_dir) {
bc27027a 4281 error = -EMLINK;
da1ce067 4282 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
bc27027a 4283 goto out;
da1ce067
MS
4284 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4285 old_dir->i_nlink >= max_links)
4286 goto out;
4287 }
4288 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4289 shrink_dcache_parent(new_dentry);
4290 if (!is_dir) {
bc27027a 4291 error = try_break_deleg(source, delegated_inode);
8e6d782c
BF
4292 if (error)
4293 goto out;
da1ce067
MS
4294 }
4295 if (target && !new_is_dir) {
4296 error = try_break_deleg(target, delegated_inode);
4297 if (error)
4298 goto out;
8e6d782c 4299 }
7177a9c4 4300 if (!old_dir->i_op->rename2) {
520c8b16
MS
4301 error = old_dir->i_op->rename(old_dir, old_dentry,
4302 new_dir, new_dentry);
4303 } else {
7177a9c4 4304 WARN_ON(old_dir->i_op->rename != NULL);
520c8b16
MS
4305 error = old_dir->i_op->rename2(old_dir, old_dentry,
4306 new_dir, new_dentry, flags);
4307 }
51892bbb
SW
4308 if (error)
4309 goto out;
4310
da1ce067 4311 if (!(flags & RENAME_EXCHANGE) && target) {
bc27027a
MS
4312 if (is_dir)
4313 target->i_flags |= S_DEAD;
51892bbb 4314 dont_mount(new_dentry);
8ed936b5 4315 detach_mounts(new_dentry);
bc27027a 4316 }
da1ce067
MS
4317 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4318 if (!(flags & RENAME_EXCHANGE))
4319 d_move(old_dentry, new_dentry);
4320 else
4321 d_exchange(old_dentry, new_dentry);
4322 }
51892bbb 4323out:
da1ce067 4324 if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a
MS
4325 unlock_two_nondirectories(source, target);
4326 else if (target)
4327 mutex_unlock(&target->i_mutex);
1da177e4 4328 dput(new_dentry);
da1ce067 4329 if (!error) {
626f7c23 4330 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
da1ce067
MS
4331 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4332 if (flags & RENAME_EXCHANGE) {
4333 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4334 new_is_dir, NULL, new_dentry);
4335 }
4336 }
626f7c23 4337 release_dentry_name_snapshot(&old_name);
0eeca283 4338
1da177e4
LT
4339 return error;
4340}
4d359507 4341EXPORT_SYMBOL(vfs_rename);
1da177e4 4342
520c8b16
MS
4343SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4344 int, newdfd, const char __user *, newname, unsigned int, flags)
1da177e4 4345{
2ad94ae6
AV
4346 struct dentry *old_dentry, *new_dentry;
4347 struct dentry *trap;
f5beed75
AV
4348 struct path old_path, new_path;
4349 struct qstr old_last, new_last;
4350 int old_type, new_type;
8e6d782c 4351 struct inode *delegated_inode = NULL;
91a27b2a
JL
4352 struct filename *from;
4353 struct filename *to;
f5beed75 4354 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
c6a94284 4355 bool should_retry = false;
2ad94ae6 4356 int error;
520c8b16 4357
0d7a8555 4358 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
da1ce067
MS
4359 return -EINVAL;
4360
0d7a8555
MS
4361 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4362 (flags & RENAME_EXCHANGE))
520c8b16
MS
4363 return -EINVAL;
4364
0d7a8555
MS
4365 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4366 return -EPERM;
4367
f5beed75
AV
4368 if (flags & RENAME_EXCHANGE)
4369 target_flags = 0;
4370
c6a94284 4371retry:
f5beed75
AV
4372 from = user_path_parent(olddfd, oldname,
4373 &old_path, &old_last, &old_type, lookup_flags);
91a27b2a
JL
4374 if (IS_ERR(from)) {
4375 error = PTR_ERR(from);
1da177e4 4376 goto exit;
91a27b2a 4377 }
1da177e4 4378
f5beed75
AV
4379 to = user_path_parent(newdfd, newname,
4380 &new_path, &new_last, &new_type, lookup_flags);
91a27b2a
JL
4381 if (IS_ERR(to)) {
4382 error = PTR_ERR(to);
1da177e4 4383 goto exit1;
91a27b2a 4384 }
1da177e4
LT
4385
4386 error = -EXDEV;
f5beed75 4387 if (old_path.mnt != new_path.mnt)
1da177e4
LT
4388 goto exit2;
4389
1da177e4 4390 error = -EBUSY;
f5beed75 4391 if (old_type != LAST_NORM)
1da177e4
LT
4392 goto exit2;
4393
0a7c3937
MS
4394 if (flags & RENAME_NOREPLACE)
4395 error = -EEXIST;
f5beed75 4396 if (new_type != LAST_NORM)
1da177e4
LT
4397 goto exit2;
4398
f5beed75 4399 error = mnt_want_write(old_path.mnt);
c30dabfe
JK
4400 if (error)
4401 goto exit2;
4402
8e6d782c 4403retry_deleg:
f5beed75 4404 trap = lock_rename(new_path.dentry, old_path.dentry);
1da177e4 4405
f5beed75 4406 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
1da177e4
LT
4407 error = PTR_ERR(old_dentry);
4408 if (IS_ERR(old_dentry))
4409 goto exit3;
4410 /* source must exist */
4411 error = -ENOENT;
b18825a7 4412 if (d_is_negative(old_dentry))
1da177e4 4413 goto exit4;
f5beed75 4414 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
0a7c3937
MS
4415 error = PTR_ERR(new_dentry);
4416 if (IS_ERR(new_dentry))
4417 goto exit4;
4418 error = -EEXIST;
4419 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4420 goto exit5;
da1ce067
MS
4421 if (flags & RENAME_EXCHANGE) {
4422 error = -ENOENT;
4423 if (d_is_negative(new_dentry))
4424 goto exit5;
4425
4426 if (!d_is_dir(new_dentry)) {
4427 error = -ENOTDIR;
f5beed75 4428 if (new_last.name[new_last.len])
da1ce067
MS
4429 goto exit5;
4430 }
4431 }
1da177e4 4432 /* unless the source is a directory trailing slashes give -ENOTDIR */
44b1d530 4433 if (!d_is_dir(old_dentry)) {
1da177e4 4434 error = -ENOTDIR;
f5beed75 4435 if (old_last.name[old_last.len])
0a7c3937 4436 goto exit5;
f5beed75 4437 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
0a7c3937 4438 goto exit5;
1da177e4
LT
4439 }
4440 /* source should not be ancestor of target */
4441 error = -EINVAL;
4442 if (old_dentry == trap)
0a7c3937 4443 goto exit5;
1da177e4 4444 /* target should not be an ancestor of source */
da1ce067
MS
4445 if (!(flags & RENAME_EXCHANGE))
4446 error = -ENOTEMPTY;
1da177e4
LT
4447 if (new_dentry == trap)
4448 goto exit5;
4449
f5beed75
AV
4450 error = security_path_rename(&old_path, old_dentry,
4451 &new_path, new_dentry, flags);
be6d3e56 4452 if (error)
c30dabfe 4453 goto exit5;
f5beed75
AV
4454 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4455 new_path.dentry->d_inode, new_dentry,
520c8b16 4456 &delegated_inode, flags);
1da177e4
LT
4457exit5:
4458 dput(new_dentry);
4459exit4:
4460 dput(old_dentry);
4461exit3:
f5beed75 4462 unlock_rename(new_path.dentry, old_path.dentry);
8e6d782c
BF
4463 if (delegated_inode) {
4464 error = break_deleg_wait(&delegated_inode);
4465 if (!error)
4466 goto retry_deleg;
4467 }
f5beed75 4468 mnt_drop_write(old_path.mnt);
1da177e4 4469exit2:
c6a94284
JL
4470 if (retry_estale(error, lookup_flags))
4471 should_retry = true;
f5beed75 4472 path_put(&new_path);
2ad94ae6 4473 putname(to);
1da177e4 4474exit1:
f5beed75 4475 path_put(&old_path);
1da177e4 4476 putname(from);
c6a94284
JL
4477 if (should_retry) {
4478 should_retry = false;
4479 lookup_flags |= LOOKUP_REVAL;
4480 goto retry;
4481 }
2ad94ae6 4482exit:
1da177e4
LT
4483 return error;
4484}
4485
520c8b16
MS
4486SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4487 int, newdfd, const char __user *, newname)
4488{
4489 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4490}
4491
a26eab24 4492SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d 4493{
520c8b16 4494 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
4495}
4496
787fb6bc
MS
4497int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4498{
4499 int error = may_create(dir, dentry);
4500 if (error)
4501 return error;
4502
4503 if (!dir->i_op->mknod)
4504 return -EPERM;
4505
4506 return dir->i_op->mknod(dir, dentry,
4507 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4508}
4509EXPORT_SYMBOL(vfs_whiteout);
4510
5d826c84 4511int readlink_copy(char __user *buffer, int buflen, const char *link)
1da177e4 4512{
5d826c84 4513 int len = PTR_ERR(link);
1da177e4
LT
4514 if (IS_ERR(link))
4515 goto out;
4516
4517 len = strlen(link);
4518 if (len > (unsigned) buflen)
4519 len = buflen;
4520 if (copy_to_user(buffer, link, len))
4521 len = -EFAULT;
4522out:
4523 return len;
4524}
5d826c84 4525EXPORT_SYMBOL(readlink_copy);
1da177e4
LT
4526
4527/*
4528 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4529 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4530 * using) it for any given inode is up to filesystem.
4531 */
4532int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4533{
cc314eef 4534 void *cookie;
5f2c4179
AV
4535 struct inode *inode = d_inode(dentry);
4536 const char *link = inode->i_link;
694a1764 4537 int res;
cc314eef 4538
d4dee48b 4539 if (!link) {
5f2c4179 4540 link = inode->i_op->follow_link(dentry, &cookie);
d4dee48b
AV
4541 if (IS_ERR(link))
4542 return PTR_ERR(link);
4543 }
680baacb 4544 res = readlink_copy(buffer, buflen, link);
5f2c4179
AV
4545 if (inode->i_op->put_link)
4546 inode->i_op->put_link(inode, cookie);
694a1764 4547 return res;
1da177e4 4548}
4d359507 4549EXPORT_SYMBOL(generic_readlink);
1da177e4 4550
1da177e4
LT
4551/* get the link contents into pagecache */
4552static char *page_getlink(struct dentry * dentry, struct page **ppage)
4553{
ebd09abb
DG
4554 char *kaddr;
4555 struct page *page;
1da177e4 4556 struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b18 4557 page = read_mapping_page(mapping, 0, NULL);
1da177e4 4558 if (IS_ERR(page))
6fe6900e 4559 return (char*)page;
1da177e4 4560 *ppage = page;
ebd09abb
DG
4561 kaddr = kmap(page);
4562 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4563 return kaddr;
1da177e4
LT
4564}
4565
4566int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4567{
4568 struct page *page = NULL;
5d826c84 4569 int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
1da177e4
LT
4570 if (page) {
4571 kunmap(page);
4572 page_cache_release(page);
4573 }
4574 return res;
4575}
4d359507 4576EXPORT_SYMBOL(page_readlink);
1da177e4 4577
6e77137b 4578const char *page_follow_link_light(struct dentry *dentry, void **cookie)
1da177e4 4579{
cc314eef 4580 struct page *page = NULL;
680baacb
AV
4581 char *res = page_getlink(dentry, &page);
4582 if (!IS_ERR(res))
4583 *cookie = page;
4584 return res;
1da177e4 4585}
4d359507 4586EXPORT_SYMBOL(page_follow_link_light);
1da177e4 4587
5f2c4179 4588void page_put_link(struct inode *unused, void *cookie)
1da177e4 4589{
cc314eef 4590 struct page *page = cookie;
680baacb
AV
4591 kunmap(page);
4592 page_cache_release(page);
1da177e4 4593}
4d359507 4594EXPORT_SYMBOL(page_put_link);
1da177e4 4595
54566b2c
NP
4596/*
4597 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4598 */
4599int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4
LT
4600{
4601 struct address_space *mapping = inode->i_mapping;
0adb25d2 4602 struct page *page;
afddba49 4603 void *fsdata;
beb497ab 4604 int err;
1da177e4 4605 char *kaddr;
54566b2c
NP
4606 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4607 if (nofs)
4608 flags |= AOP_FLAG_NOFS;
1da177e4 4609
7e53cac4 4610retry:
afddba49 4611 err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c 4612 flags, &page, &fsdata);
1da177e4 4613 if (err)
afddba49
NP
4614 goto fail;
4615
e8e3c3d6 4616 kaddr = kmap_atomic(page);
1da177e4 4617 memcpy(kaddr, symname, len-1);
e8e3c3d6 4618 kunmap_atomic(kaddr);
afddba49
NP
4619
4620 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4621 page, fsdata);
1da177e4
LT
4622 if (err < 0)
4623 goto fail;
afddba49
NP
4624 if (err < len-1)
4625 goto retry;
4626
1da177e4
LT
4627 mark_inode_dirty(inode);
4628 return 0;
1da177e4
LT
4629fail:
4630 return err;
4631}
4d359507 4632EXPORT_SYMBOL(__page_symlink);
1da177e4 4633
0adb25d2
KK
4634int page_symlink(struct inode *inode, const char *symname, int len)
4635{
4636 return __page_symlink(inode, symname, len,
c62d2555 4637 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
0adb25d2 4638}
4d359507 4639EXPORT_SYMBOL(page_symlink);
0adb25d2 4640
92e1d5be 4641const struct inode_operations page_symlink_inode_operations = {
1da177e4
LT
4642 .readlink = generic_readlink,
4643 .follow_link = page_follow_link_light,
4644 .put_link = page_put_link,
4645};
1da177e4 4646EXPORT_SYMBOL(page_symlink_inode_operations);