vfs: pass all mask flags check_acl and posix_acl_permission
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
1da177e4 22#include <linux/pagemap.h>
0eeca283 23#include <linux/fsnotify.h>
1da177e4
LT
24#include <linux/personality.h>
25#include <linux/security.h>
6146f0d5 26#include <linux/ima.h>
1da177e4
LT
27#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
16f7e0fe 30#include <linux/capability.h>
834f2a4a 31#include <linux/file.h>
5590ff0d 32#include <linux/fcntl.h>
08ce5f16 33#include <linux/device_cgroup.h>
5ad4e53b 34#include <linux/fs_struct.h>
e77819e5 35#include <linux/posix_acl.h>
1da177e4
LT
36#include <asm/uaccess.h>
37
e81e3f4d
EP
38#include "internal.h"
39
1da177e4
LT
40/* [Feb-1997 T. Schoebel-Theuer]
41 * Fundamental changes in the pathname lookup mechanisms (namei)
42 * were necessary because of omirr. The reason is that omirr needs
43 * to know the _real_ pathname, not the user-supplied one, in case
44 * of symlinks (and also when transname replacements occur).
45 *
46 * The new code replaces the old recursive symlink resolution with
47 * an iterative one (in case of non-nested symlink chains). It does
48 * this with calls to <fs>_follow_link().
49 * As a side effect, dir_namei(), _namei() and follow_link() are now
50 * replaced with a single function lookup_dentry() that can handle all
51 * the special cases of the former code.
52 *
53 * With the new dcache, the pathname is stored at each inode, at least as
54 * long as the refcount of the inode is positive. As a side effect, the
55 * size of the dcache depends on the inode cache and thus is dynamic.
56 *
57 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
58 * resolution to correspond with current state of the code.
59 *
60 * Note that the symlink resolution is not *completely* iterative.
61 * There is still a significant amount of tail- and mid- recursion in
62 * the algorithm. Also, note that <fs>_readlink() is not used in
63 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
64 * may return different results than <fs>_follow_link(). Many virtual
65 * filesystems (including /proc) exhibit this behavior.
66 */
67
68/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
69 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
70 * and the name already exists in form of a symlink, try to create the new
71 * name indicated by the symlink. The old code always complained that the
72 * name already exists, due to not following the symlink even if its target
73 * is nonexistent. The new semantics affects also mknod() and link() when
25985edc 74 * the name is a symlink pointing to a non-existent name.
1da177e4
LT
75 *
76 * I don't know which semantics is the right one, since I have no access
77 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
78 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
79 * "old" one. Personally, I think the new semantics is much more logical.
80 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
81 * file does succeed in both HP-UX and SunOs, but not in Solaris
82 * and in the old Linux semantics.
83 */
84
85/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
86 * semantics. See the comments in "open_namei" and "do_link" below.
87 *
88 * [10-Sep-98 Alan Modra] Another symlink change.
89 */
90
91/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
92 * inside the path - always follow.
93 * in the last component in creation/removal/renaming - never follow.
94 * if LOOKUP_FOLLOW passed - follow.
95 * if the pathname has trailing slashes - follow.
96 * otherwise - don't follow.
97 * (applied in that order).
98 *
99 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
100 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
101 * During the 2.4 we need to fix the userland stuff depending on it -
102 * hopefully we will be able to get rid of that wart in 2.5. So far only
103 * XEmacs seems to be relying on it...
104 */
105/*
106 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
a11f3a05 107 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
1da177e4
LT
108 * any extra contention...
109 */
110
111/* In order to reduce some races, while at the same time doing additional
112 * checking and hopefully speeding things up, we copy filenames to the
113 * kernel data space before using them..
114 *
115 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116 * PATH_MAX includes the nul terminator --RR.
117 */
858119e1 118static int do_getname(const char __user *filename, char *page)
1da177e4
LT
119{
120 int retval;
121 unsigned long len = PATH_MAX;
122
123 if (!segment_eq(get_fs(), KERNEL_DS)) {
124 if ((unsigned long) filename >= TASK_SIZE)
125 return -EFAULT;
126 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
128 }
129
130 retval = strncpy_from_user(page, filename, len);
131 if (retval > 0) {
132 if (retval < len)
133 return 0;
134 return -ENAMETOOLONG;
135 } else if (!retval)
136 retval = -ENOENT;
137 return retval;
138}
139
f52e0c11 140static char *getname_flags(const char __user * filename, int flags)
1da177e4
LT
141{
142 char *tmp, *result;
143
144 result = ERR_PTR(-ENOMEM);
145 tmp = __getname();
146 if (tmp) {
147 int retval = do_getname(filename, tmp);
148
149 result = tmp;
150 if (retval < 0) {
f52e0c11
AV
151 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
152 __putname(tmp);
153 result = ERR_PTR(retval);
154 }
1da177e4
LT
155 }
156 }
157 audit_getname(result);
158 return result;
159}
160
f52e0c11
AV
161char *getname(const char __user * filename)
162{
163 return getname_flags(filename, 0);
164}
165
1da177e4
LT
166#ifdef CONFIG_AUDITSYSCALL
167void putname(const char *name)
168{
5ac3a9c2 169 if (unlikely(!audit_dummy_context()))
1da177e4
LT
170 audit_putname(name);
171 else
172 __putname(name);
173}
174EXPORT_SYMBOL(putname);
175#endif
176
e77819e5
LT
177static int check_acl(struct inode *inode, int mask)
178{
84635d68 179#ifdef CONFIG_FS_POSIX_ACL
e77819e5
LT
180 struct posix_acl *acl;
181
e77819e5 182 if (mask & MAY_NOT_BLOCK) {
3567866b
AV
183 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
184 if (!acl)
e77819e5 185 return -EAGAIN;
3567866b
AV
186 /* no ->get_acl() calls in RCU mode... */
187 if (acl == ACL_NOT_CACHED)
188 return -ECHILD;
206b1d09 189 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
e77819e5
LT
190 }
191
192 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
193
194 /*
4e34e719
CH
195 * A filesystem can force a ACL callback by just never filling the
196 * ACL cache. But normally you'd fill the cache either at inode
197 * instantiation time, or on the first ->get_acl call.
e77819e5 198 *
4e34e719
CH
199 * If the filesystem doesn't have a get_acl() function at all, we'll
200 * just create the negative cache entry.
e77819e5
LT
201 */
202 if (acl == ACL_NOT_CACHED) {
4e34e719
CH
203 if (inode->i_op->get_acl) {
204 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
205 if (IS_ERR(acl))
206 return PTR_ERR(acl);
207 } else {
208 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
209 return -EAGAIN;
210 }
e77819e5
LT
211 }
212
213 if (acl) {
214 int error = posix_acl_permission(inode, acl, mask);
215 posix_acl_release(acl);
216 return error;
217 }
84635d68 218#endif
e77819e5
LT
219
220 return -EAGAIN;
221}
222
5909ccaa
LT
223/*
224 * This does basic POSIX ACL permission checking
1da177e4 225 */
7e40145e 226static int acl_permission_check(struct inode *inode, int mask)
1da177e4 227{
26cf46be 228 unsigned int mode = inode->i_mode;
1da177e4 229
e795b717
SH
230 if (current_user_ns() != inode_userns(inode))
231 goto other_perms;
232
14067ff5 233 if (likely(current_fsuid() == inode->i_uid))
1da177e4
LT
234 mode >>= 6;
235 else {
e77819e5 236 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
7e40145e 237 int error = check_acl(inode, mask);
b74c79e9
NP
238 if (error != -EAGAIN)
239 return error;
1da177e4
LT
240 }
241
242 if (in_group_p(inode->i_gid))
243 mode >>= 3;
244 }
245
e795b717 246other_perms:
1da177e4
LT
247 /*
248 * If the DACs are ok we don't need any capability check.
249 */
9c2c7039 250 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4 251 return 0;
5909ccaa
LT
252 return -EACCES;
253}
254
255/**
b74c79e9 256 * generic_permission - check for access rights on a Posix-like filesystem
5909ccaa 257 * @inode: inode to check access rights for
8fd90c8d 258 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
5909ccaa
LT
259 *
260 * Used to check for read/write/execute permissions on a file.
261 * We use "fsuid" for this, letting us set arbitrary permissions
262 * for filesystem access without changing the "normal" uids which
b74c79e9
NP
263 * are used for other things.
264 *
265 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
266 * request cannot be satisfied (eg. requires blocking or too much complexity).
267 * It would then be called again in ref-walk mode.
5909ccaa 268 */
2830ba7f 269int generic_permission(struct inode *inode, int mask)
5909ccaa
LT
270{
271 int ret;
272
273 /*
274 * Do the basic POSIX ACL permission checks.
275 */
7e40145e 276 ret = acl_permission_check(inode, mask);
5909ccaa
LT
277 if (ret != -EACCES)
278 return ret;
1da177e4 279
d594e7ec
AV
280 if (S_ISDIR(inode->i_mode)) {
281 /* DACs are overridable for directories */
282 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
283 return 0;
284 if (!(mask & MAY_WRITE))
285 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
286 return 0;
287 return -EACCES;
288 }
1da177e4
LT
289 /*
290 * Read/write DACs are always overridable.
d594e7ec
AV
291 * Executable DACs are overridable when there is
292 * at least one exec bit set.
1da177e4 293 */
d594e7ec 294 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
e795b717 295 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
1da177e4
LT
296 return 0;
297
298 /*
299 * Searching includes executable on directories, else just read.
300 */
7ea66001 301 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec 302 if (mask == MAY_READ)
e795b717 303 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
1da177e4
LT
304 return 0;
305
306 return -EACCES;
307}
308
3ddcd056
LT
309/*
310 * We _really_ want to just do "generic_permission()" without
311 * even looking at the inode->i_op values. So we keep a cache
312 * flag in inode->i_opflags, that says "this has not special
313 * permission function, use the fast case".
314 */
315static inline int do_inode_permission(struct inode *inode, int mask)
316{
317 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
318 if (likely(inode->i_op->permission))
319 return inode->i_op->permission(inode, mask);
320
321 /* This gets set once for the inode lifetime */
322 spin_lock(&inode->i_lock);
323 inode->i_opflags |= IOP_FASTPERM;
324 spin_unlock(&inode->i_lock);
325 }
326 return generic_permission(inode, mask);
327}
328
cb23beb5
CH
329/**
330 * inode_permission - check for access rights to a given inode
331 * @inode: inode to check permission on
8fd90c8d 332 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
cb23beb5
CH
333 *
334 * Used to check for read/write/execute permissions on an inode.
335 * We use "fsuid" for this, letting us set arbitrary permissions
336 * for filesystem access without changing the "normal" uids which
337 * are used for other things.
338 */
f419a2e3 339int inode_permission(struct inode *inode, int mask)
1da177e4 340{
e6305c43 341 int retval;
1da177e4 342
3ddcd056 343 if (unlikely(mask & MAY_WRITE)) {
22590e41 344 umode_t mode = inode->i_mode;
1da177e4
LT
345
346 /*
347 * Nobody gets write access to a read-only fs.
348 */
349 if (IS_RDONLY(inode) &&
350 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
351 return -EROFS;
352
353 /*
354 * Nobody gets write access to an immutable file.
355 */
356 if (IS_IMMUTABLE(inode))
357 return -EACCES;
358 }
359
3ddcd056 360 retval = do_inode_permission(inode, mask);
1da177e4
LT
361 if (retval)
362 return retval;
363
08ce5f16
SH
364 retval = devcgroup_inode_permission(inode, mask);
365 if (retval)
366 return retval;
367
d09ca739 368 return security_inode_permission(inode, mask);
1da177e4
LT
369}
370
5dd784d0
JB
371/**
372 * path_get - get a reference to a path
373 * @path: path to get the reference to
374 *
375 * Given a path increment the reference count to the dentry and the vfsmount.
376 */
377void path_get(struct path *path)
378{
379 mntget(path->mnt);
380 dget(path->dentry);
381}
382EXPORT_SYMBOL(path_get);
383
1d957f9b
JB
384/**
385 * path_put - put a reference to a path
386 * @path: path to put the reference to
387 *
388 * Given a path decrement the reference count to the dentry and the vfsmount.
389 */
390void path_put(struct path *path)
1da177e4 391{
1d957f9b
JB
392 dput(path->dentry);
393 mntput(path->mnt);
1da177e4 394}
1d957f9b 395EXPORT_SYMBOL(path_put);
1da177e4 396
19660af7 397/*
31e6b01f 398 * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af7
AV
399 * Documentation/filesystems/path-lookup.txt). In situations when we can't
400 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
401 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
402 * mode. Refcounts are grabbed at the last known good point before rcu-walk
403 * got stuck, so ref-walk may continue from there. If this is not successful
404 * (eg. a seqcount has changed), then failure is returned and it's up to caller
405 * to restart the path walk from the beginning in ref-walk mode.
31e6b01f 406 */
31e6b01f
NP
407
408/**
19660af7
AV
409 * unlazy_walk - try to switch to ref-walk mode.
410 * @nd: nameidata pathwalk data
411 * @dentry: child of nd->path.dentry or NULL
39191628 412 * Returns: 0 on success, -ECHILD on failure
31e6b01f 413 *
19660af7
AV
414 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
415 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
416 * @nd or NULL. Must be called from rcu-walk context.
31e6b01f 417 */
19660af7 418static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
31e6b01f
NP
419{
420 struct fs_struct *fs = current->fs;
421 struct dentry *parent = nd->path.dentry;
5b6ca027 422 int want_root = 0;
31e6b01f
NP
423
424 BUG_ON(!(nd->flags & LOOKUP_RCU));
5b6ca027
AV
425 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
426 want_root = 1;
31e6b01f
NP
427 spin_lock(&fs->lock);
428 if (nd->root.mnt != fs->root.mnt ||
429 nd->root.dentry != fs->root.dentry)
430 goto err_root;
431 }
432 spin_lock(&parent->d_lock);
19660af7
AV
433 if (!dentry) {
434 if (!__d_rcu_to_refcount(parent, nd->seq))
435 goto err_parent;
436 BUG_ON(nd->inode != parent->d_inode);
437 } else {
94c0d4ec
AV
438 if (dentry->d_parent != parent)
439 goto err_parent;
19660af7
AV
440 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
441 if (!__d_rcu_to_refcount(dentry, nd->seq))
442 goto err_child;
443 /*
444 * If the sequence check on the child dentry passed, then
445 * the child has not been removed from its parent. This
446 * means the parent dentry must be valid and able to take
447 * a reference at this point.
448 */
449 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
450 BUG_ON(!parent->d_count);
451 parent->d_count++;
452 spin_unlock(&dentry->d_lock);
453 }
31e6b01f 454 spin_unlock(&parent->d_lock);
5b6ca027 455 if (want_root) {
31e6b01f
NP
456 path_get(&nd->root);
457 spin_unlock(&fs->lock);
458 }
459 mntget(nd->path.mnt);
460
461 rcu_read_unlock();
462 br_read_unlock(vfsmount_lock);
463 nd->flags &= ~LOOKUP_RCU;
464 return 0;
19660af7
AV
465
466err_child:
31e6b01f 467 spin_unlock(&dentry->d_lock);
19660af7 468err_parent:
31e6b01f
NP
469 spin_unlock(&parent->d_lock);
470err_root:
5b6ca027 471 if (want_root)
31e6b01f
NP
472 spin_unlock(&fs->lock);
473 return -ECHILD;
474}
475
834f2a4a
TM
476/**
477 * release_open_intent - free up open intent resources
478 * @nd: pointer to nameidata
479 */
480void release_open_intent(struct nameidata *nd)
481{
2dab5974
LT
482 struct file *file = nd->intent.open.file;
483
484 if (file && !IS_ERR(file)) {
485 if (file->f_path.dentry == NULL)
486 put_filp(file);
487 else
488 fput(file);
489 }
834f2a4a
TM
490}
491
f60aef7e 492static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
34286d66 493{
f60aef7e 494 return dentry->d_op->d_revalidate(dentry, nd);
34286d66
NP
495}
496
9f1fafee
AV
497/**
498 * complete_walk - successful completion of path walk
499 * @nd: pointer nameidata
39159de2 500 *
9f1fafee
AV
501 * If we had been in RCU mode, drop out of it and legitimize nd->path.
502 * Revalidate the final result, unless we'd already done that during
503 * the path walk or the filesystem doesn't ask for it. Return 0 on
504 * success, -error on failure. In case of failure caller does not
505 * need to drop nd->path.
39159de2 506 */
9f1fafee 507static int complete_walk(struct nameidata *nd)
39159de2 508{
16c2cd71 509 struct dentry *dentry = nd->path.dentry;
39159de2 510 int status;
39159de2 511
9f1fafee
AV
512 if (nd->flags & LOOKUP_RCU) {
513 nd->flags &= ~LOOKUP_RCU;
514 if (!(nd->flags & LOOKUP_ROOT))
515 nd->root.mnt = NULL;
516 spin_lock(&dentry->d_lock);
517 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
518 spin_unlock(&dentry->d_lock);
519 rcu_read_unlock();
520 br_read_unlock(vfsmount_lock);
521 return -ECHILD;
522 }
523 BUG_ON(nd->inode != dentry->d_inode);
524 spin_unlock(&dentry->d_lock);
525 mntget(nd->path.mnt);
526 rcu_read_unlock();
527 br_read_unlock(vfsmount_lock);
528 }
529
16c2cd71
AV
530 if (likely(!(nd->flags & LOOKUP_JUMPED)))
531 return 0;
532
533 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
39159de2
JL
534 return 0;
535
16c2cd71
AV
536 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
537 return 0;
538
539 /* Note: we do not d_invalidate() */
34286d66 540 status = d_revalidate(dentry, nd);
39159de2
JL
541 if (status > 0)
542 return 0;
543
16c2cd71 544 if (!status)
39159de2 545 status = -ESTALE;
16c2cd71 546
9f1fafee 547 path_put(&nd->path);
39159de2
JL
548 return status;
549}
550
2a737871
AV
551static __always_inline void set_root(struct nameidata *nd)
552{
f7ad3c6b
MS
553 if (!nd->root.mnt)
554 get_fs_root(current->fs, &nd->root);
2a737871
AV
555}
556
6de88d72
AV
557static int link_path_walk(const char *, struct nameidata *);
558
31e6b01f
NP
559static __always_inline void set_root_rcu(struct nameidata *nd)
560{
561 if (!nd->root.mnt) {
562 struct fs_struct *fs = current->fs;
c28cc364
NP
563 unsigned seq;
564
565 do {
566 seq = read_seqcount_begin(&fs->seq);
567 nd->root = fs->root;
c1530019 568 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
c28cc364 569 } while (read_seqcount_retry(&fs->seq, seq));
31e6b01f
NP
570 }
571}
572
f1662356 573static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
1da177e4 574{
31e6b01f
NP
575 int ret;
576
1da177e4
LT
577 if (IS_ERR(link))
578 goto fail;
579
580 if (*link == '/') {
2a737871 581 set_root(nd);
1d957f9b 582 path_put(&nd->path);
2a737871
AV
583 nd->path = nd->root;
584 path_get(&nd->root);
16c2cd71 585 nd->flags |= LOOKUP_JUMPED;
1da177e4 586 }
31e6b01f 587 nd->inode = nd->path.dentry->d_inode;
b4091d5f 588
31e6b01f
NP
589 ret = link_path_walk(link, nd);
590 return ret;
1da177e4 591fail:
1d957f9b 592 path_put(&nd->path);
1da177e4
LT
593 return PTR_ERR(link);
594}
595
1d957f9b 596static void path_put_conditional(struct path *path, struct nameidata *nd)
051d3812
IK
597{
598 dput(path->dentry);
4ac91378 599 if (path->mnt != nd->path.mnt)
051d3812
IK
600 mntput(path->mnt);
601}
602
7b9337aa
NP
603static inline void path_to_nameidata(const struct path *path,
604 struct nameidata *nd)
051d3812 605{
31e6b01f
NP
606 if (!(nd->flags & LOOKUP_RCU)) {
607 dput(nd->path.dentry);
608 if (nd->path.mnt != path->mnt)
609 mntput(nd->path.mnt);
9a229683 610 }
31e6b01f 611 nd->path.mnt = path->mnt;
4ac91378 612 nd->path.dentry = path->dentry;
051d3812
IK
613}
614
574197e0
AV
615static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
616{
617 struct inode *inode = link->dentry->d_inode;
618 if (!IS_ERR(cookie) && inode->i_op->put_link)
619 inode->i_op->put_link(link->dentry, nd, cookie);
620 path_put(link);
621}
622
def4af30 623static __always_inline int
574197e0 624follow_link(struct path *link, struct nameidata *nd, void **p)
1da177e4
LT
625{
626 int error;
7b9337aa 627 struct dentry *dentry = link->dentry;
1da177e4 628
844a3917
AV
629 BUG_ON(nd->flags & LOOKUP_RCU);
630
0e794589
AV
631 if (link->mnt == nd->path.mnt)
632 mntget(link->mnt);
633
574197e0
AV
634 if (unlikely(current->total_link_count >= 40)) {
635 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
574197e0
AV
636 path_put(&nd->path);
637 return -ELOOP;
638 }
639 cond_resched();
640 current->total_link_count++;
641
7b9337aa 642 touch_atime(link->mnt, dentry);
1da177e4 643 nd_set_link(nd, NULL);
cd4e91d3 644
36f3b4f6
AV
645 error = security_inode_follow_link(link->dentry, nd);
646 if (error) {
647 *p = ERR_PTR(error); /* no ->put_link(), please */
648 path_put(&nd->path);
649 return error;
650 }
651
86acdca1 652 nd->last_type = LAST_BIND;
def4af30
AV
653 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
654 error = PTR_ERR(*p);
655 if (!IS_ERR(*p)) {
1da177e4 656 char *s = nd_get_link(nd);
cc314eef 657 error = 0;
1da177e4
LT
658 if (s)
659 error = __vfs_follow_link(nd, s);
bcda7652 660 else if (nd->last_type == LAST_BIND) {
16c2cd71 661 nd->flags |= LOOKUP_JUMPED;
b21041d0
AV
662 nd->inode = nd->path.dentry->d_inode;
663 if (nd->inode->i_op->follow_link) {
bcda7652
AV
664 /* stepped on a _really_ weird one */
665 path_put(&nd->path);
666 error = -ELOOP;
667 }
668 }
1da177e4 669 }
1da177e4
LT
670 return error;
671}
672
31e6b01f
NP
673static int follow_up_rcu(struct path *path)
674{
675 struct vfsmount *parent;
676 struct dentry *mountpoint;
677
678 parent = path->mnt->mnt_parent;
679 if (parent == path->mnt)
680 return 0;
681 mountpoint = path->mnt->mnt_mountpoint;
682 path->dentry = mountpoint;
683 path->mnt = parent;
684 return 1;
685}
686
bab77ebf 687int follow_up(struct path *path)
1da177e4
LT
688{
689 struct vfsmount *parent;
690 struct dentry *mountpoint;
99b7db7b
NP
691
692 br_read_lock(vfsmount_lock);
bab77ebf
AV
693 parent = path->mnt->mnt_parent;
694 if (parent == path->mnt) {
99b7db7b 695 br_read_unlock(vfsmount_lock);
1da177e4
LT
696 return 0;
697 }
698 mntget(parent);
bab77ebf 699 mountpoint = dget(path->mnt->mnt_mountpoint);
99b7db7b 700 br_read_unlock(vfsmount_lock);
bab77ebf
AV
701 dput(path->dentry);
702 path->dentry = mountpoint;
703 mntput(path->mnt);
704 path->mnt = parent;
1da177e4
LT
705 return 1;
706}
707
b5c84bf6 708/*
9875cf80
DH
709 * Perform an automount
710 * - return -EISDIR to tell follow_managed() to stop and return the path we
711 * were called with.
1da177e4 712 */
9875cf80
DH
713static int follow_automount(struct path *path, unsigned flags,
714 bool *need_mntput)
31e6b01f 715{
9875cf80 716 struct vfsmount *mnt;
ea5b778a 717 int err;
9875cf80
DH
718
719 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
720 return -EREMOTE;
721
0ec26fd0
MS
722 /* We don't want to mount if someone's just doing a stat -
723 * unless they're stat'ing a directory and appended a '/' to
724 * the name.
725 *
726 * We do, however, want to mount if someone wants to open or
727 * create a file of any type under the mountpoint, wants to
728 * traverse through the mountpoint or wants to open the
729 * mounted directory. Also, autofs may mark negative dentries
730 * as being automount points. These will need the attentions
731 * of the daemon to instantiate them before they can be used.
9875cf80 732 */
0ec26fd0 733 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
d94c177b 734 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
0ec26fd0
MS
735 path->dentry->d_inode)
736 return -EISDIR;
737
9875cf80
DH
738 current->total_link_count++;
739 if (current->total_link_count >= 40)
740 return -ELOOP;
741
742 mnt = path->dentry->d_op->d_automount(path);
743 if (IS_ERR(mnt)) {
744 /*
745 * The filesystem is allowed to return -EISDIR here to indicate
746 * it doesn't want to automount. For instance, autofs would do
747 * this so that its userspace daemon can mount on this dentry.
748 *
749 * However, we can only permit this if it's a terminal point in
750 * the path being looked up; if it wasn't then the remainder of
751 * the path is inaccessible and we should say so.
752 */
49084c3b 753 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9875cf80
DH
754 return -EREMOTE;
755 return PTR_ERR(mnt);
31e6b01f 756 }
ea5b778a 757
9875cf80
DH
758 if (!mnt) /* mount collision */
759 return 0;
31e6b01f 760
8aef1884
AV
761 if (!*need_mntput) {
762 /* lock_mount() may release path->mnt on error */
763 mntget(path->mnt);
764 *need_mntput = true;
765 }
19a167af 766 err = finish_automount(mnt, path);
9875cf80 767
ea5b778a
DH
768 switch (err) {
769 case -EBUSY:
770 /* Someone else made a mount here whilst we were busy */
19a167af 771 return 0;
ea5b778a 772 case 0:
8aef1884 773 path_put(path);
ea5b778a
DH
774 path->mnt = mnt;
775 path->dentry = dget(mnt->mnt_root);
ea5b778a 776 return 0;
19a167af
AV
777 default:
778 return err;
ea5b778a 779 }
19a167af 780
463ffb2e
AV
781}
782
9875cf80
DH
783/*
784 * Handle a dentry that is managed in some way.
cc53ce53 785 * - Flagged for transit management (autofs)
9875cf80
DH
786 * - Flagged as mountpoint
787 * - Flagged as automount point
788 *
789 * This may only be called in refwalk mode.
790 *
791 * Serialization is taken care of in namespace.c
792 */
793static int follow_managed(struct path *path, unsigned flags)
1da177e4 794{
8aef1884 795 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9875cf80
DH
796 unsigned managed;
797 bool need_mntput = false;
8aef1884 798 int ret = 0;
9875cf80
DH
799
800 /* Given that we're not holding a lock here, we retain the value in a
801 * local variable for each dentry as we look at it so that we don't see
802 * the components of that value change under us */
803 while (managed = ACCESS_ONCE(path->dentry->d_flags),
804 managed &= DCACHE_MANAGED_DENTRY,
805 unlikely(managed != 0)) {
cc53ce53
DH
806 /* Allow the filesystem to manage the transit without i_mutex
807 * being held. */
808 if (managed & DCACHE_MANAGE_TRANSIT) {
809 BUG_ON(!path->dentry->d_op);
810 BUG_ON(!path->dentry->d_op->d_manage);
1aed3e42 811 ret = path->dentry->d_op->d_manage(path->dentry, false);
cc53ce53 812 if (ret < 0)
8aef1884 813 break;
cc53ce53
DH
814 }
815
9875cf80
DH
816 /* Transit to a mounted filesystem. */
817 if (managed & DCACHE_MOUNTED) {
818 struct vfsmount *mounted = lookup_mnt(path);
819 if (mounted) {
820 dput(path->dentry);
821 if (need_mntput)
822 mntput(path->mnt);
823 path->mnt = mounted;
824 path->dentry = dget(mounted->mnt_root);
825 need_mntput = true;
826 continue;
827 }
828
829 /* Something is mounted on this dentry in another
830 * namespace and/or whatever was mounted there in this
831 * namespace got unmounted before we managed to get the
832 * vfsmount_lock */
833 }
834
835 /* Handle an automount point */
836 if (managed & DCACHE_NEED_AUTOMOUNT) {
837 ret = follow_automount(path, flags, &need_mntput);
838 if (ret < 0)
8aef1884 839 break;
9875cf80
DH
840 continue;
841 }
842
843 /* We didn't change the current path point */
844 break;
1da177e4 845 }
8aef1884
AV
846
847 if (need_mntput && path->mnt == mnt)
848 mntput(path->mnt);
849 if (ret == -EISDIR)
850 ret = 0;
851 return ret;
1da177e4
LT
852}
853
cc53ce53 854int follow_down_one(struct path *path)
1da177e4
LT
855{
856 struct vfsmount *mounted;
857
1c755af4 858 mounted = lookup_mnt(path);
1da177e4 859 if (mounted) {
9393bd07
AV
860 dput(path->dentry);
861 mntput(path->mnt);
862 path->mnt = mounted;
863 path->dentry = dget(mounted->mnt_root);
1da177e4
LT
864 return 1;
865 }
866 return 0;
867}
868
62a7375e
IK
869static inline bool managed_dentry_might_block(struct dentry *dentry)
870{
871 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
872 dentry->d_op->d_manage(dentry, true) < 0);
873}
874
9875cf80 875/*
287548e4
AV
876 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
877 * we meet a managed dentry that would need blocking.
9875cf80
DH
878 */
879static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
287548e4 880 struct inode **inode)
9875cf80 881{
62a7375e 882 for (;;) {
9875cf80 883 struct vfsmount *mounted;
62a7375e
IK
884 /*
885 * Don't forget we might have a non-mountpoint managed dentry
886 * that wants to block transit.
887 */
287548e4 888 if (unlikely(managed_dentry_might_block(path->dentry)))
ab90911f 889 return false;
62a7375e
IK
890
891 if (!d_mountpoint(path->dentry))
892 break;
893
9875cf80
DH
894 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
895 if (!mounted)
896 break;
897 path->mnt = mounted;
898 path->dentry = mounted->mnt_root;
899 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
59430262
LT
900 /*
901 * Update the inode too. We don't need to re-check the
902 * dentry sequence number here after this d_inode read,
903 * because a mount-point is always pinned.
904 */
905 *inode = path->dentry->d_inode;
9875cf80 906 }
9875cf80
DH
907 return true;
908}
909
dea39376 910static void follow_mount_rcu(struct nameidata *nd)
287548e4 911{
dea39376 912 while (d_mountpoint(nd->path.dentry)) {
287548e4 913 struct vfsmount *mounted;
dea39376 914 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
287548e4
AV
915 if (!mounted)
916 break;
dea39376
AV
917 nd->path.mnt = mounted;
918 nd->path.dentry = mounted->mnt_root;
919 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
287548e4
AV
920 }
921}
922
31e6b01f
NP
923static int follow_dotdot_rcu(struct nameidata *nd)
924{
31e6b01f
NP
925 set_root_rcu(nd);
926
9875cf80 927 while (1) {
31e6b01f
NP
928 if (nd->path.dentry == nd->root.dentry &&
929 nd->path.mnt == nd->root.mnt) {
930 break;
931 }
932 if (nd->path.dentry != nd->path.mnt->mnt_root) {
933 struct dentry *old = nd->path.dentry;
934 struct dentry *parent = old->d_parent;
935 unsigned seq;
936
937 seq = read_seqcount_begin(&parent->d_seq);
938 if (read_seqcount_retry(&old->d_seq, nd->seq))
ef7562d5 939 goto failed;
31e6b01f
NP
940 nd->path.dentry = parent;
941 nd->seq = seq;
942 break;
943 }
944 if (!follow_up_rcu(&nd->path))
945 break;
946 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
31e6b01f 947 }
dea39376
AV
948 follow_mount_rcu(nd);
949 nd->inode = nd->path.dentry->d_inode;
31e6b01f 950 return 0;
ef7562d5
AV
951
952failed:
953 nd->flags &= ~LOOKUP_RCU;
5b6ca027
AV
954 if (!(nd->flags & LOOKUP_ROOT))
955 nd->root.mnt = NULL;
ef7562d5
AV
956 rcu_read_unlock();
957 br_read_unlock(vfsmount_lock);
958 return -ECHILD;
31e6b01f
NP
959}
960
cc53ce53
DH
961/*
962 * Follow down to the covering mount currently visible to userspace. At each
963 * point, the filesystem owning that dentry may be queried as to whether the
964 * caller is permitted to proceed or not.
cc53ce53 965 */
7cc90cc3 966int follow_down(struct path *path)
cc53ce53
DH
967{
968 unsigned managed;
969 int ret;
970
971 while (managed = ACCESS_ONCE(path->dentry->d_flags),
972 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
973 /* Allow the filesystem to manage the transit without i_mutex
974 * being held.
975 *
976 * We indicate to the filesystem if someone is trying to mount
977 * something here. This gives autofs the chance to deny anyone
978 * other than its daemon the right to mount on its
979 * superstructure.
980 *
981 * The filesystem may sleep at this point.
982 */
983 if (managed & DCACHE_MANAGE_TRANSIT) {
984 BUG_ON(!path->dentry->d_op);
985 BUG_ON(!path->dentry->d_op->d_manage);
ab90911f 986 ret = path->dentry->d_op->d_manage(
1aed3e42 987 path->dentry, false);
cc53ce53
DH
988 if (ret < 0)
989 return ret == -EISDIR ? 0 : ret;
990 }
991
992 /* Transit to a mounted filesystem. */
993 if (managed & DCACHE_MOUNTED) {
994 struct vfsmount *mounted = lookup_mnt(path);
995 if (!mounted)
996 break;
997 dput(path->dentry);
998 mntput(path->mnt);
999 path->mnt = mounted;
1000 path->dentry = dget(mounted->mnt_root);
1001 continue;
1002 }
1003
1004 /* Don't handle automount points here */
1005 break;
1006 }
1007 return 0;
1008}
1009
9875cf80
DH
1010/*
1011 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1012 */
1013static void follow_mount(struct path *path)
1014{
1015 while (d_mountpoint(path->dentry)) {
1016 struct vfsmount *mounted = lookup_mnt(path);
1017 if (!mounted)
1018 break;
1019 dput(path->dentry);
1020 mntput(path->mnt);
1021 path->mnt = mounted;
1022 path->dentry = dget(mounted->mnt_root);
1023 }
1024}
1025
31e6b01f 1026static void follow_dotdot(struct nameidata *nd)
1da177e4 1027{
2a737871 1028 set_root(nd);
e518ddb7 1029
1da177e4 1030 while(1) {
4ac91378 1031 struct dentry *old = nd->path.dentry;
1da177e4 1032
2a737871
AV
1033 if (nd->path.dentry == nd->root.dentry &&
1034 nd->path.mnt == nd->root.mnt) {
1da177e4
LT
1035 break;
1036 }
4ac91378 1037 if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd70
AV
1038 /* rare case of legitimate dget_parent()... */
1039 nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4
LT
1040 dput(old);
1041 break;
1042 }
3088dd70 1043 if (!follow_up(&nd->path))
1da177e4 1044 break;
1da177e4 1045 }
79ed0226 1046 follow_mount(&nd->path);
31e6b01f 1047 nd->inode = nd->path.dentry->d_inode;
1da177e4
LT
1048}
1049
baa03890
NP
1050/*
1051 * Allocate a dentry with name and parent, and perform a parent
1052 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1053 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1054 * have verified that no child exists while under i_mutex.
1055 */
1056static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1057 struct qstr *name, struct nameidata *nd)
1058{
1059 struct inode *inode = parent->d_inode;
1060 struct dentry *dentry;
1061 struct dentry *old;
1062
1063 /* Don't create child dentry for a dead directory. */
1064 if (unlikely(IS_DEADDIR(inode)))
1065 return ERR_PTR(-ENOENT);
1066
1067 dentry = d_alloc(parent, name);
1068 if (unlikely(!dentry))
1069 return ERR_PTR(-ENOMEM);
1070
1071 old = inode->i_op->lookup(inode, dentry, nd);
1072 if (unlikely(old)) {
1073 dput(dentry);
1074 dentry = old;
1075 }
1076 return dentry;
1077}
1078
44396f4b
JB
1079/*
1080 * We already have a dentry, but require a lookup to be performed on the parent
1081 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1082 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1083 * child exists while under i_mutex.
1084 */
1085static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1086 struct nameidata *nd)
1087{
1088 struct inode *inode = parent->d_inode;
1089 struct dentry *old;
1090
1091 /* Don't create child dentry for a dead directory. */
1092 if (unlikely(IS_DEADDIR(inode)))
1093 return ERR_PTR(-ENOENT);
1094
1095 old = inode->i_op->lookup(inode, dentry, nd);
1096 if (unlikely(old)) {
1097 dput(dentry);
1098 dentry = old;
1099 }
1100 return dentry;
1101}
1102
1da177e4
LT
1103/*
1104 * It's more convoluted than I'd like it to be, but... it's still fairly
1105 * small and for now I'd prefer to have fast path as straight as possible.
1106 * It _is_ time-critical.
1107 */
1108static int do_lookup(struct nameidata *nd, struct qstr *name,
31e6b01f 1109 struct path *path, struct inode **inode)
1da177e4 1110{
4ac91378 1111 struct vfsmount *mnt = nd->path.mnt;
31e6b01f 1112 struct dentry *dentry, *parent = nd->path.dentry;
5a18fff2
AV
1113 int need_reval = 1;
1114 int status = 1;
9875cf80
DH
1115 int err;
1116
b04f784e
NP
1117 /*
1118 * Rename seqlock is not required here because in the off chance
1119 * of a false negative due to a concurrent rename, we're going to
1120 * do the non-racy lookup, below.
1121 */
31e6b01f
NP
1122 if (nd->flags & LOOKUP_RCU) {
1123 unsigned seq;
31e6b01f
NP
1124 *inode = nd->inode;
1125 dentry = __d_lookup_rcu(parent, name, &seq, inode);
5a18fff2
AV
1126 if (!dentry)
1127 goto unlazy;
1128
31e6b01f
NP
1129 /* Memory barrier in read_seqcount_begin of child is enough */
1130 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1131 return -ECHILD;
31e6b01f 1132 nd->seq = seq;
5a18fff2 1133
24643087 1134 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
5a18fff2
AV
1135 status = d_revalidate(dentry, nd);
1136 if (unlikely(status <= 0)) {
1137 if (status != -ECHILD)
1138 need_reval = 0;
1139 goto unlazy;
1140 }
24643087 1141 }
44396f4b
JB
1142 if (unlikely(d_need_lookup(dentry)))
1143 goto unlazy;
31e6b01f
NP
1144 path->mnt = mnt;
1145 path->dentry = dentry;
d6e9bd25
AV
1146 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1147 goto unlazy;
1148 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1149 goto unlazy;
1150 return 0;
5a18fff2 1151unlazy:
19660af7
AV
1152 if (unlazy_walk(nd, dentry))
1153 return -ECHILD;
5a18fff2
AV
1154 } else {
1155 dentry = __d_lookup(parent, name);
9875cf80 1156 }
5a18fff2 1157
44396f4b
JB
1158 if (dentry && unlikely(d_need_lookup(dentry))) {
1159 dput(dentry);
1160 dentry = NULL;
1161 }
5a18fff2
AV
1162retry:
1163 if (unlikely(!dentry)) {
1164 struct inode *dir = parent->d_inode;
1165 BUG_ON(nd->inode != dir);
1166
1167 mutex_lock(&dir->i_mutex);
1168 dentry = d_lookup(parent, name);
1169 if (likely(!dentry)) {
1170 dentry = d_alloc_and_lookup(parent, name, nd);
1171 if (IS_ERR(dentry)) {
1172 mutex_unlock(&dir->i_mutex);
1173 return PTR_ERR(dentry);
1174 }
1175 /* known good */
1176 need_reval = 0;
1177 status = 1;
44396f4b
JB
1178 } else if (unlikely(d_need_lookup(dentry))) {
1179 dentry = d_inode_lookup(parent, dentry, nd);
1180 if (IS_ERR(dentry)) {
1181 mutex_unlock(&dir->i_mutex);
1182 return PTR_ERR(dentry);
1183 }
1184 /* known good */
1185 need_reval = 0;
1186 status = 1;
5a18fff2
AV
1187 }
1188 mutex_unlock(&dir->i_mutex);
1189 }
1190 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1191 status = d_revalidate(dentry, nd);
1192 if (unlikely(status <= 0)) {
1193 if (status < 0) {
1194 dput(dentry);
1195 return status;
1196 }
1197 if (!d_invalidate(dentry)) {
1198 dput(dentry);
1199 dentry = NULL;
1200 need_reval = 1;
1201 goto retry;
1202 }
24643087 1203 }
5a18fff2 1204
9875cf80
DH
1205 path->mnt = mnt;
1206 path->dentry = dentry;
1207 err = follow_managed(path, nd->flags);
89312214
IK
1208 if (unlikely(err < 0)) {
1209 path_put_conditional(path, nd);
9875cf80 1210 return err;
89312214 1211 }
9875cf80 1212 *inode = path->dentry->d_inode;
1da177e4 1213 return 0;
1da177e4
LT
1214}
1215
52094c8a
AV
1216static inline int may_lookup(struct nameidata *nd)
1217{
1218 if (nd->flags & LOOKUP_RCU) {
4ad5abb3 1219 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a
AV
1220 if (err != -ECHILD)
1221 return err;
19660af7 1222 if (unlazy_walk(nd, NULL))
52094c8a
AV
1223 return -ECHILD;
1224 }
4ad5abb3 1225 return inode_permission(nd->inode, MAY_EXEC);
52094c8a
AV
1226}
1227
9856fa1b
AV
1228static inline int handle_dots(struct nameidata *nd, int type)
1229{
1230 if (type == LAST_DOTDOT) {
1231 if (nd->flags & LOOKUP_RCU) {
1232 if (follow_dotdot_rcu(nd))
1233 return -ECHILD;
1234 } else
1235 follow_dotdot(nd);
1236 }
1237 return 0;
1238}
1239
951361f9
AV
1240static void terminate_walk(struct nameidata *nd)
1241{
1242 if (!(nd->flags & LOOKUP_RCU)) {
1243 path_put(&nd->path);
1244 } else {
1245 nd->flags &= ~LOOKUP_RCU;
5b6ca027
AV
1246 if (!(nd->flags & LOOKUP_ROOT))
1247 nd->root.mnt = NULL;
951361f9
AV
1248 rcu_read_unlock();
1249 br_read_unlock(vfsmount_lock);
1250 }
1251}
1252
3ddcd056
LT
1253/*
1254 * Do we need to follow links? We _really_ want to be able
1255 * to do this check without having to look at inode->i_op,
1256 * so we keep a cache of "no, this doesn't need follow_link"
1257 * for the common case.
1258 */
7813b94a 1259static inline int should_follow_link(struct inode *inode, int follow)
3ddcd056
LT
1260{
1261 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1262 if (likely(inode->i_op->follow_link))
1263 return follow;
1264
1265 /* This gets set once for the inode lifetime */
1266 spin_lock(&inode->i_lock);
1267 inode->i_opflags |= IOP_NOFOLLOW;
1268 spin_unlock(&inode->i_lock);
1269 }
1270 return 0;
1271}
1272
ce57dfc1
AV
1273static inline int walk_component(struct nameidata *nd, struct path *path,
1274 struct qstr *name, int type, int follow)
1275{
1276 struct inode *inode;
1277 int err;
1278 /*
1279 * "." and ".." are special - ".." especially so because it has
1280 * to be able to know about the current root directory and
1281 * parent relationships.
1282 */
1283 if (unlikely(type != LAST_NORM))
1284 return handle_dots(nd, type);
1285 err = do_lookup(nd, name, path, &inode);
1286 if (unlikely(err)) {
1287 terminate_walk(nd);
1288 return err;
1289 }
1290 if (!inode) {
1291 path_to_nameidata(path, nd);
1292 terminate_walk(nd);
1293 return -ENOENT;
1294 }
7813b94a 1295 if (should_follow_link(inode, follow)) {
19660af7
AV
1296 if (nd->flags & LOOKUP_RCU) {
1297 if (unlikely(unlazy_walk(nd, path->dentry))) {
1298 terminate_walk(nd);
1299 return -ECHILD;
1300 }
1301 }
ce57dfc1
AV
1302 BUG_ON(inode != path->dentry->d_inode);
1303 return 1;
1304 }
1305 path_to_nameidata(path, nd);
1306 nd->inode = inode;
1307 return 0;
1308}
1309
b356379a
AV
1310/*
1311 * This limits recursive symlink follows to 8, while
1312 * limiting consecutive symlinks to 40.
1313 *
1314 * Without that kind of total limit, nasty chains of consecutive
1315 * symlinks can cause almost arbitrarily long lookups.
1316 */
1317static inline int nested_symlink(struct path *path, struct nameidata *nd)
1318{
1319 int res;
1320
b356379a
AV
1321 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1322 path_put_conditional(path, nd);
1323 path_put(&nd->path);
1324 return -ELOOP;
1325 }
1a4022f8 1326 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
b356379a
AV
1327
1328 nd->depth++;
1329 current->link_count++;
1330
1331 do {
1332 struct path link = *path;
1333 void *cookie;
574197e0
AV
1334
1335 res = follow_link(&link, nd, &cookie);
b356379a
AV
1336 if (!res)
1337 res = walk_component(nd, path, &nd->last,
1338 nd->last_type, LOOKUP_FOLLOW);
574197e0 1339 put_link(nd, &link, cookie);
b356379a
AV
1340 } while (res > 0);
1341
1342 current->link_count--;
1343 nd->depth--;
1344 return res;
1345}
1346
3ddcd056
LT
1347/*
1348 * We really don't want to look at inode->i_op->lookup
1349 * when we don't have to. So we keep a cache bit in
1350 * the inode ->i_opflags field that says "yes, we can
1351 * do lookup on this inode".
1352 */
1353static inline int can_lookup(struct inode *inode)
1354{
1355 if (likely(inode->i_opflags & IOP_LOOKUP))
1356 return 1;
1357 if (likely(!inode->i_op->lookup))
1358 return 0;
1359
1360 /* We do this once for the lifetime of the inode */
1361 spin_lock(&inode->i_lock);
1362 inode->i_opflags |= IOP_LOOKUP;
1363 spin_unlock(&inode->i_lock);
1364 return 1;
1365}
1366
1da177e4
LT
1367/*
1368 * Name resolution.
ea3834d9
PM
1369 * This is the basic name resolution function, turning a pathname into
1370 * the final dentry. We expect 'base' to be positive and a directory.
1da177e4 1371 *
ea3834d9
PM
1372 * Returns 0 and nd will have valid dentry and mnt on success.
1373 * Returns error and drops reference to input namei data on failure.
1da177e4 1374 */
6de88d72 1375static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4
LT
1376{
1377 struct path next;
1da177e4 1378 int err;
1da177e4
LT
1379
1380 while (*name=='/')
1381 name++;
1382 if (!*name)
086e183a 1383 return 0;
1da177e4 1384
1da177e4
LT
1385 /* At this point we know we have a real path component. */
1386 for(;;) {
1387 unsigned long hash;
1388 struct qstr this;
1389 unsigned int c;
fe479a58 1390 int type;
1da177e4 1391
52094c8a 1392 err = may_lookup(nd);
1da177e4
LT
1393 if (err)
1394 break;
1395
1396 this.name = name;
1397 c = *(const unsigned char *)name;
1398
1399 hash = init_name_hash();
1400 do {
1401 name++;
1402 hash = partial_name_hash(c, hash);
1403 c = *(const unsigned char *)name;
1404 } while (c && (c != '/'));
1405 this.len = name - (const char *) this.name;
1406 this.hash = end_name_hash(hash);
1407
fe479a58
AV
1408 type = LAST_NORM;
1409 if (this.name[0] == '.') switch (this.len) {
1410 case 2:
16c2cd71 1411 if (this.name[1] == '.') {
fe479a58 1412 type = LAST_DOTDOT;
16c2cd71
AV
1413 nd->flags |= LOOKUP_JUMPED;
1414 }
fe479a58
AV
1415 break;
1416 case 1:
1417 type = LAST_DOT;
1418 }
5a202bcd
AV
1419 if (likely(type == LAST_NORM)) {
1420 struct dentry *parent = nd->path.dentry;
16c2cd71 1421 nd->flags &= ~LOOKUP_JUMPED;
5a202bcd
AV
1422 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1423 err = parent->d_op->d_hash(parent, nd->inode,
1424 &this);
1425 if (err < 0)
1426 break;
1427 }
1428 }
fe479a58 1429
1da177e4
LT
1430 /* remove trailing slashes? */
1431 if (!c)
1432 goto last_component;
1433 while (*++name == '/');
1434 if (!*name)
b356379a 1435 goto last_component;
1da177e4 1436
ce57dfc1
AV
1437 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1438 if (err < 0)
1439 return err;
1da177e4 1440
ce57dfc1 1441 if (err) {
b356379a 1442 err = nested_symlink(&next, nd);
1da177e4 1443 if (err)
a7472bab 1444 return err;
31e6b01f 1445 }
3ddcd056
LT
1446 if (can_lookup(nd->inode))
1447 continue;
1da177e4 1448 err = -ENOTDIR;
3ddcd056 1449 break;
1da177e4
LT
1450 /* here ends the main loop */
1451
1da177e4 1452last_component:
b356379a
AV
1453 nd->last = this;
1454 nd->last_type = type;
086e183a 1455 return 0;
1da177e4 1456 }
951361f9 1457 terminate_walk(nd);
1da177e4
LT
1458 return err;
1459}
1460
70e9b357
AV
1461static int path_init(int dfd, const char *name, unsigned int flags,
1462 struct nameidata *nd, struct file **fp)
31e6b01f
NP
1463{
1464 int retval = 0;
1465 int fput_needed;
1466 struct file *file;
1467
1468 nd->last_type = LAST_ROOT; /* if there are only slashes... */
16c2cd71 1469 nd->flags = flags | LOOKUP_JUMPED;
31e6b01f 1470 nd->depth = 0;
5b6ca027
AV
1471 if (flags & LOOKUP_ROOT) {
1472 struct inode *inode = nd->root.dentry->d_inode;
73d049a4
AV
1473 if (*name) {
1474 if (!inode->i_op->lookup)
1475 return -ENOTDIR;
1476 retval = inode_permission(inode, MAY_EXEC);
1477 if (retval)
1478 return retval;
1479 }
5b6ca027
AV
1480 nd->path = nd->root;
1481 nd->inode = inode;
1482 if (flags & LOOKUP_RCU) {
1483 br_read_lock(vfsmount_lock);
1484 rcu_read_lock();
1485 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1486 } else {
1487 path_get(&nd->path);
1488 }
1489 return 0;
1490 }
1491
31e6b01f 1492 nd->root.mnt = NULL;
31e6b01f
NP
1493
1494 if (*name=='/') {
e41f7d4e
AV
1495 if (flags & LOOKUP_RCU) {
1496 br_read_lock(vfsmount_lock);
1497 rcu_read_lock();
1498 set_root_rcu(nd);
1499 } else {
1500 set_root(nd);
1501 path_get(&nd->root);
1502 }
1503 nd->path = nd->root;
31e6b01f 1504 } else if (dfd == AT_FDCWD) {
e41f7d4e
AV
1505 if (flags & LOOKUP_RCU) {
1506 struct fs_struct *fs = current->fs;
1507 unsigned seq;
31e6b01f 1508
e41f7d4e
AV
1509 br_read_lock(vfsmount_lock);
1510 rcu_read_lock();
c28cc364 1511
e41f7d4e
AV
1512 do {
1513 seq = read_seqcount_begin(&fs->seq);
1514 nd->path = fs->pwd;
1515 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1516 } while (read_seqcount_retry(&fs->seq, seq));
1517 } else {
1518 get_fs_pwd(current->fs, &nd->path);
1519 }
31e6b01f
NP
1520 } else {
1521 struct dentry *dentry;
1522
1abf0c71 1523 file = fget_raw_light(dfd, &fput_needed);
31e6b01f
NP
1524 retval = -EBADF;
1525 if (!file)
1526 goto out_fail;
1527
1528 dentry = file->f_path.dentry;
1529
f52e0c11
AV
1530 if (*name) {
1531 retval = -ENOTDIR;
1532 if (!S_ISDIR(dentry->d_inode->i_mode))
1533 goto fput_fail;
31e6b01f 1534
4ad5abb3 1535 retval = inode_permission(dentry->d_inode, MAY_EXEC);
f52e0c11
AV
1536 if (retval)
1537 goto fput_fail;
1538 }
31e6b01f
NP
1539
1540 nd->path = file->f_path;
e41f7d4e
AV
1541 if (flags & LOOKUP_RCU) {
1542 if (fput_needed)
70e9b357 1543 *fp = file;
e41f7d4e
AV
1544 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1545 br_read_lock(vfsmount_lock);
1546 rcu_read_lock();
1547 } else {
1548 path_get(&file->f_path);
1549 fput_light(file, fput_needed);
1550 }
31e6b01f 1551 }
31e6b01f 1552
31e6b01f 1553 nd->inode = nd->path.dentry->d_inode;
9b4a9b14 1554 return 0;
2dfdd266 1555
9b4a9b14
AV
1556fput_fail:
1557 fput_light(file, fput_needed);
1558out_fail:
1559 return retval;
1560}
1561
bd92d7fe
AV
1562static inline int lookup_last(struct nameidata *nd, struct path *path)
1563{
1564 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1565 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1566
1567 nd->flags &= ~LOOKUP_PARENT;
1568 return walk_component(nd, path, &nd->last, nd->last_type,
1569 nd->flags & LOOKUP_FOLLOW);
1570}
1571
9b4a9b14 1572/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
ee0827cd 1573static int path_lookupat(int dfd, const char *name,
9b4a9b14
AV
1574 unsigned int flags, struct nameidata *nd)
1575{
70e9b357 1576 struct file *base = NULL;
bd92d7fe
AV
1577 struct path path;
1578 int err;
31e6b01f
NP
1579
1580 /*
1581 * Path walking is largely split up into 2 different synchronisation
1582 * schemes, rcu-walk and ref-walk (explained in
1583 * Documentation/filesystems/path-lookup.txt). These share much of the
1584 * path walk code, but some things particularly setup, cleanup, and
1585 * following mounts are sufficiently divergent that functions are
1586 * duplicated. Typically there is a function foo(), and its RCU
1587 * analogue, foo_rcu().
1588 *
1589 * -ECHILD is the error number of choice (just to avoid clashes) that
1590 * is returned if some aspect of an rcu-walk fails. Such an error must
1591 * be handled by restarting a traditional ref-walk (which will always
1592 * be able to complete).
1593 */
bd92d7fe 1594 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
ee0827cd 1595
bd92d7fe
AV
1596 if (unlikely(err))
1597 return err;
ee0827cd
AV
1598
1599 current->total_link_count = 0;
bd92d7fe
AV
1600 err = link_path_walk(name, nd);
1601
1602 if (!err && !(flags & LOOKUP_PARENT)) {
bd92d7fe
AV
1603 err = lookup_last(nd, &path);
1604 while (err > 0) {
1605 void *cookie;
1606 struct path link = path;
bd92d7fe 1607 nd->flags |= LOOKUP_PARENT;
574197e0 1608 err = follow_link(&link, nd, &cookie);
bd92d7fe
AV
1609 if (!err)
1610 err = lookup_last(nd, &path);
574197e0 1611 put_link(nd, &link, cookie);
bd92d7fe
AV
1612 }
1613 }
ee0827cd 1614
9f1fafee
AV
1615 if (!err)
1616 err = complete_walk(nd);
bd92d7fe
AV
1617
1618 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1619 if (!nd->inode->i_op->lookup) {
1620 path_put(&nd->path);
bd23a539 1621 err = -ENOTDIR;
bd92d7fe
AV
1622 }
1623 }
16c2cd71 1624
70e9b357
AV
1625 if (base)
1626 fput(base);
ee0827cd 1627
5b6ca027 1628 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2a737871
AV
1629 path_put(&nd->root);
1630 nd->root.mnt = NULL;
1631 }
bd92d7fe 1632 return err;
ee0827cd 1633}
31e6b01f 1634
ee0827cd
AV
1635static int do_path_lookup(int dfd, const char *name,
1636 unsigned int flags, struct nameidata *nd)
1637{
1638 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1639 if (unlikely(retval == -ECHILD))
1640 retval = path_lookupat(dfd, name, flags, nd);
1641 if (unlikely(retval == -ESTALE))
1642 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
31e6b01f
NP
1643
1644 if (likely(!retval)) {
1645 if (unlikely(!audit_dummy_context())) {
1646 if (nd->path.dentry && nd->inode)
1647 audit_inode(name, nd->path.dentry);
1648 }
1649 }
170aa3d0 1650 return retval;
1da177e4
LT
1651}
1652
c9c6cac0 1653int kern_path_parent(const char *name, struct nameidata *nd)
5590ff0d 1654{
c9c6cac0 1655 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
5590ff0d
UD
1656}
1657
d1811465
AV
1658int kern_path(const char *name, unsigned int flags, struct path *path)
1659{
1660 struct nameidata nd;
1661 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1662 if (!res)
1663 *path = nd.path;
1664 return res;
1665}
1666
16f18200
JJS
1667/**
1668 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1669 * @dentry: pointer to dentry of the base directory
1670 * @mnt: pointer to vfs mount of the base directory
1671 * @name: pointer to file name
1672 * @flags: lookup flags
e0a01249 1673 * @path: pointer to struct path to fill
16f18200
JJS
1674 */
1675int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1676 const char *name, unsigned int flags,
e0a01249 1677 struct path *path)
16f18200 1678{
e0a01249
AV
1679 struct nameidata nd;
1680 int err;
1681 nd.root.dentry = dentry;
1682 nd.root.mnt = mnt;
1683 BUG_ON(flags & LOOKUP_PARENT);
5b6ca027 1684 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
e0a01249
AV
1685 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1686 if (!err)
1687 *path = nd.path;
1688 return err;
16f18200
JJS
1689}
1690
eead1911
CH
1691static struct dentry *__lookup_hash(struct qstr *name,
1692 struct dentry *base, struct nameidata *nd)
1da177e4 1693{
81fca444 1694 struct inode *inode = base->d_inode;
057f6c01 1695 struct dentry *dentry;
1da177e4
LT
1696 int err;
1697
4ad5abb3 1698 err = inode_permission(inode, MAY_EXEC);
81fca444
CH
1699 if (err)
1700 return ERR_PTR(err);
1da177e4 1701
b04f784e
NP
1702 /*
1703 * Don't bother with __d_lookup: callers are for creat as
1704 * well as unlink, so a lot of the time it would cost
1705 * a double lookup.
6e6b1bd1 1706 */
b04f784e 1707 dentry = d_lookup(base, name);
6e6b1bd1 1708
44396f4b
JB
1709 if (dentry && d_need_lookup(dentry)) {
1710 /*
1711 * __lookup_hash is called with the parent dir's i_mutex already
1712 * held, so we are good to go here.
1713 */
1714 dentry = d_inode_lookup(base, dentry, nd);
1715 if (IS_ERR(dentry))
1716 return dentry;
1717 }
1718
d2d9e9fb
AV
1719 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1720 int status = d_revalidate(dentry, nd);
1721 if (unlikely(status <= 0)) {
1722 /*
1723 * The dentry failed validation.
1724 * If d_revalidate returned 0 attempt to invalidate
1725 * the dentry otherwise d_revalidate is asking us
1726 * to return a fail status.
1727 */
1728 if (status < 0) {
1729 dput(dentry);
1730 return ERR_PTR(status);
1731 } else if (!d_invalidate(dentry)) {
1732 dput(dentry);
1733 dentry = NULL;
1734 }
1735 }
1736 }
6e6b1bd1 1737
baa03890
NP
1738 if (!dentry)
1739 dentry = d_alloc_and_lookup(base, name, nd);
5a202bcd 1740
1da177e4
LT
1741 return dentry;
1742}
1743
057f6c01
JM
1744/*
1745 * Restricted form of lookup. Doesn't follow links, single-component only,
1746 * needs parent already locked. Doesn't follow mounts.
1747 * SMP-safe.
1748 */
eead1911 1749static struct dentry *lookup_hash(struct nameidata *nd)
057f6c01 1750{
4ac91378 1751 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1da177e4
LT
1752}
1753
eead1911 1754/**
a6b91919 1755 * lookup_one_len - filesystem helper to lookup single pathname component
eead1911
CH
1756 * @name: pathname component to lookup
1757 * @base: base directory to lookup from
1758 * @len: maximum length @len should be interpreted to
1759 *
a6b91919
RD
1760 * Note that this routine is purely a helper for filesystem usage and should
1761 * not be called by generic code. Also note that by using this function the
eead1911
CH
1762 * nameidata argument is passed to the filesystem methods and a filesystem
1763 * using this helper needs to be prepared for that.
1764 */
057f6c01
JM
1765struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1766{
057f6c01 1767 struct qstr this;
6a96ba54
AV
1768 unsigned long hash;
1769 unsigned int c;
057f6c01 1770
2f9092e1
DW
1771 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1772
6a96ba54
AV
1773 this.name = name;
1774 this.len = len;
1775 if (!len)
1776 return ERR_PTR(-EACCES);
1777
1778 hash = init_name_hash();
1779 while (len--) {
1780 c = *(const unsigned char *)name++;
1781 if (c == '/' || c == '\0')
1782 return ERR_PTR(-EACCES);
1783 hash = partial_name_hash(c, hash);
1784 }
1785 this.hash = end_name_hash(hash);
5a202bcd
AV
1786 /*
1787 * See if the low-level filesystem might want
1788 * to use its own hash..
1789 */
1790 if (base->d_flags & DCACHE_OP_HASH) {
1791 int err = base->d_op->d_hash(base, base->d_inode, &this);
1792 if (err < 0)
1793 return ERR_PTR(err);
1794 }
eead1911 1795
49705b77 1796 return __lookup_hash(&this, base, NULL);
057f6c01
JM
1797}
1798
2d8f3038
AV
1799int user_path_at(int dfd, const char __user *name, unsigned flags,
1800 struct path *path)
1da177e4 1801{
2d8f3038 1802 struct nameidata nd;
f52e0c11 1803 char *tmp = getname_flags(name, flags);
1da177e4 1804 int err = PTR_ERR(tmp);
1da177e4 1805 if (!IS_ERR(tmp)) {
2d8f3038
AV
1806
1807 BUG_ON(flags & LOOKUP_PARENT);
1808
1809 err = do_path_lookup(dfd, tmp, flags, &nd);
1da177e4 1810 putname(tmp);
2d8f3038
AV
1811 if (!err)
1812 *path = nd.path;
1da177e4
LT
1813 }
1814 return err;
1815}
1816
2ad94ae6
AV
1817static int user_path_parent(int dfd, const char __user *path,
1818 struct nameidata *nd, char **name)
1819{
1820 char *s = getname(path);
1821 int error;
1822
1823 if (IS_ERR(s))
1824 return PTR_ERR(s);
1825
1826 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1827 if (error)
1828 putname(s);
1829 else
1830 *name = s;
1831
1832 return error;
1833}
1834
1da177e4
LT
1835/*
1836 * It's inline, so penalty for filesystems that don't use sticky bit is
1837 * minimal.
1838 */
1839static inline int check_sticky(struct inode *dir, struct inode *inode)
1840{
da9592ed
DH
1841 uid_t fsuid = current_fsuid();
1842
1da177e4
LT
1843 if (!(dir->i_mode & S_ISVTX))
1844 return 0;
e795b717
SH
1845 if (current_user_ns() != inode_userns(inode))
1846 goto other_userns;
da9592ed 1847 if (inode->i_uid == fsuid)
1da177e4 1848 return 0;
da9592ed 1849 if (dir->i_uid == fsuid)
1da177e4 1850 return 0;
e795b717
SH
1851
1852other_userns:
1853 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1da177e4
LT
1854}
1855
1856/*
1857 * Check whether we can remove a link victim from directory dir, check
1858 * whether the type of victim is right.
1859 * 1. We can't do it if dir is read-only (done in permission())
1860 * 2. We should have write and exec permissions on dir
1861 * 3. We can't remove anything from append-only dir
1862 * 4. We can't do anything with immutable dir (done in permission())
1863 * 5. If the sticky bit on dir is set we should either
1864 * a. be owner of dir, or
1865 * b. be owner of victim, or
1866 * c. have CAP_FOWNER capability
1867 * 6. If the victim is append-only or immutable we can't do antyhing with
1868 * links pointing to it.
1869 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1870 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1871 * 9. We can't remove a root or mountpoint.
1872 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1873 * nfs_async_unlink().
1874 */
858119e1 1875static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1da177e4
LT
1876{
1877 int error;
1878
1879 if (!victim->d_inode)
1880 return -ENOENT;
1881
1882 BUG_ON(victim->d_parent->d_inode != dir);
cccc6bba 1883 audit_inode_child(victim, dir);
1da177e4 1884
f419a2e3 1885 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
1886 if (error)
1887 return error;
1888 if (IS_APPEND(dir))
1889 return -EPERM;
1890 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
f9454548 1891 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1da177e4
LT
1892 return -EPERM;
1893 if (isdir) {
1894 if (!S_ISDIR(victim->d_inode->i_mode))
1895 return -ENOTDIR;
1896 if (IS_ROOT(victim))
1897 return -EBUSY;
1898 } else if (S_ISDIR(victim->d_inode->i_mode))
1899 return -EISDIR;
1900 if (IS_DEADDIR(dir))
1901 return -ENOENT;
1902 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1903 return -EBUSY;
1904 return 0;
1905}
1906
1907/* Check whether we can create an object with dentry child in directory
1908 * dir.
1909 * 1. We can't do it if child already exists (open has special treatment for
1910 * this case, but since we are inlined it's OK)
1911 * 2. We can't do it if dir is read-only (done in permission())
1912 * 3. We should have write and exec permissions on dir
1913 * 4. We can't do it if dir is immutable (done in permission())
1914 */
a95164d9 1915static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4
LT
1916{
1917 if (child->d_inode)
1918 return -EEXIST;
1919 if (IS_DEADDIR(dir))
1920 return -ENOENT;
f419a2e3 1921 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
1922}
1923
1da177e4
LT
1924/*
1925 * p1 and p2 should be directories on the same fs.
1926 */
1927struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1928{
1929 struct dentry *p;
1930
1931 if (p1 == p2) {
f2eace23 1932 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4
LT
1933 return NULL;
1934 }
1935
a11f3a05 1936 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4 1937
e2761a11
OH
1938 p = d_ancestor(p2, p1);
1939 if (p) {
1940 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1941 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1942 return p;
1da177e4
LT
1943 }
1944
e2761a11
OH
1945 p = d_ancestor(p1, p2);
1946 if (p) {
1947 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1948 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1949 return p;
1da177e4
LT
1950 }
1951
f2eace23
IM
1952 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1953 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1da177e4
LT
1954 return NULL;
1955}
1956
1957void unlock_rename(struct dentry *p1, struct dentry *p2)
1958{
1b1dcc1b 1959 mutex_unlock(&p1->d_inode->i_mutex);
1da177e4 1960 if (p1 != p2) {
1b1dcc1b 1961 mutex_unlock(&p2->d_inode->i_mutex);
a11f3a05 1962 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4
LT
1963 }
1964}
1965
1966int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1967 struct nameidata *nd)
1968{
a95164d9 1969 int error = may_create(dir, dentry);
1da177e4
LT
1970
1971 if (error)
1972 return error;
1973
acfa4380 1974 if (!dir->i_op->create)
1da177e4
LT
1975 return -EACCES; /* shouldn't it be ENOSYS? */
1976 mode &= S_IALLUGO;
1977 mode |= S_IFREG;
1978 error = security_inode_create(dir, dentry, mode);
1979 if (error)
1980 return error;
1da177e4 1981 error = dir->i_op->create(dir, dentry, mode, nd);
a74574aa 1982 if (!error)
f38aa942 1983 fsnotify_create(dir, dentry);
1da177e4
LT
1984 return error;
1985}
1986
73d049a4 1987static int may_open(struct path *path, int acc_mode, int flag)
1da177e4 1988{
3fb64190 1989 struct dentry *dentry = path->dentry;
1da177e4
LT
1990 struct inode *inode = dentry->d_inode;
1991 int error;
1992
bcda7652
AV
1993 /* O_PATH? */
1994 if (!acc_mode)
1995 return 0;
1996
1da177e4
LT
1997 if (!inode)
1998 return -ENOENT;
1999
c8fe8f30
CH
2000 switch (inode->i_mode & S_IFMT) {
2001 case S_IFLNK:
1da177e4 2002 return -ELOOP;
c8fe8f30
CH
2003 case S_IFDIR:
2004 if (acc_mode & MAY_WRITE)
2005 return -EISDIR;
2006 break;
2007 case S_IFBLK:
2008 case S_IFCHR:
3fb64190 2009 if (path->mnt->mnt_flags & MNT_NODEV)
1da177e4 2010 return -EACCES;
c8fe8f30
CH
2011 /*FALLTHRU*/
2012 case S_IFIFO:
2013 case S_IFSOCK:
1da177e4 2014 flag &= ~O_TRUNC;
c8fe8f30 2015 break;
4a3fd211 2016 }
b41572e9 2017
3fb64190 2018 error = inode_permission(inode, acc_mode);
b41572e9
DH
2019 if (error)
2020 return error;
6146f0d5 2021
1da177e4
LT
2022 /*
2023 * An append-only file must be opened in append mode for writing.
2024 */
2025 if (IS_APPEND(inode)) {
8737c930 2026 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521 2027 return -EPERM;
1da177e4 2028 if (flag & O_TRUNC)
7715b521 2029 return -EPERM;
1da177e4
LT
2030 }
2031
2032 /* O_NOATIME can only be set by the owner or superuser */
2e149670 2033 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b521 2034 return -EPERM;
1da177e4
LT
2035
2036 /*
2037 * Ensure there are no outstanding leases on the file.
2038 */
b65a9cfc 2039 return break_lease(inode, flag);
7715b521 2040}
1da177e4 2041
e1181ee6 2042static int handle_truncate(struct file *filp)
7715b521 2043{
e1181ee6 2044 struct path *path = &filp->f_path;
7715b521
AV
2045 struct inode *inode = path->dentry->d_inode;
2046 int error = get_write_access(inode);
2047 if (error)
2048 return error;
2049 /*
2050 * Refuse to truncate files with mandatory locks held on them.
2051 */
2052 error = locks_verify_locked(inode);
2053 if (!error)
ea0d3ab2 2054 error = security_path_truncate(path);
7715b521
AV
2055 if (!error) {
2056 error = do_truncate(path->dentry, 0,
2057 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6 2058 filp);
7715b521
AV
2059 }
2060 put_write_access(inode);
acd0c935 2061 return error;
1da177e4
LT
2062}
2063
d57999e1
DH
2064static inline int open_to_namei_flags(int flag)
2065{
8a5e929d
AV
2066 if ((flag & O_ACCMODE) == 3)
2067 flag--;
d57999e1
DH
2068 return flag;
2069}
2070
31e6b01f 2071/*
fe2d35ff 2072 * Handle the last step of open()
31e6b01f 2073 */
fb1cc555 2074static struct file *do_last(struct nameidata *nd, struct path *path,
c3e380b0 2075 const struct open_flags *op, const char *pathname)
fb1cc555 2076{
a1e28038 2077 struct dentry *dir = nd->path.dentry;
6c0d46c4 2078 struct dentry *dentry;
ca344a89 2079 int open_flag = op->open_flag;
6c0d46c4 2080 int will_truncate = open_flag & O_TRUNC;
ca344a89 2081 int want_write = 0;
bcda7652 2082 int acc_mode = op->acc_mode;
fb1cc555 2083 struct file *filp;
16c2cd71 2084 int error;
1f36f774 2085
c3e380b0
AV
2086 nd->flags &= ~LOOKUP_PARENT;
2087 nd->flags |= op->intent;
2088
1f36f774
AV
2089 switch (nd->last_type) {
2090 case LAST_DOTDOT:
176306f5 2091 case LAST_DOT:
fe2d35ff
AV
2092 error = handle_dots(nd, nd->last_type);
2093 if (error)
2094 return ERR_PTR(error);
1f36f774 2095 /* fallthrough */
1f36f774 2096 case LAST_ROOT:
9f1fafee 2097 error = complete_walk(nd);
16c2cd71 2098 if (error)
9f1fafee 2099 return ERR_PTR(error);
fe2d35ff 2100 audit_inode(pathname, nd->path.dentry);
ca344a89 2101 if (open_flag & O_CREAT) {
fe2d35ff
AV
2102 error = -EISDIR;
2103 goto exit;
2104 }
2105 goto ok;
1f36f774 2106 case LAST_BIND:
9f1fafee 2107 error = complete_walk(nd);
16c2cd71 2108 if (error)
9f1fafee 2109 return ERR_PTR(error);
1f36f774 2110 audit_inode(pathname, dir);
67ee3ad2 2111 goto ok;
1f36f774 2112 }
67ee3ad2 2113
ca344a89 2114 if (!(open_flag & O_CREAT)) {
bcda7652 2115 int symlink_ok = 0;
fe2d35ff
AV
2116 if (nd->last.name[nd->last.len])
2117 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
bcda7652
AV
2118 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2119 symlink_ok = 1;
fe2d35ff 2120 /* we _can_ be in RCU mode here */
ce57dfc1
AV
2121 error = walk_component(nd, path, &nd->last, LAST_NORM,
2122 !symlink_ok);
2123 if (error < 0)
fe2d35ff 2124 return ERR_PTR(error);
ce57dfc1 2125 if (error) /* symlink */
fe2d35ff 2126 return NULL;
fe2d35ff 2127 /* sayonara */
9f1fafee
AV
2128 error = complete_walk(nd);
2129 if (error)
2130 return ERR_PTR(-ECHILD);
fe2d35ff
AV
2131
2132 error = -ENOTDIR;
2133 if (nd->flags & LOOKUP_DIRECTORY) {
ce57dfc1 2134 if (!nd->inode->i_op->lookup)
fe2d35ff
AV
2135 goto exit;
2136 }
2137 audit_inode(pathname, nd->path.dentry);
2138 goto ok;
2139 }
2140
2141 /* create side of things */
9f1fafee
AV
2142 error = complete_walk(nd);
2143 if (error)
2144 return ERR_PTR(error);
fe2d35ff
AV
2145
2146 audit_inode(pathname, dir);
16c2cd71 2147 error = -EISDIR;
1f36f774 2148 /* trailing slashes? */
31e6b01f
NP
2149 if (nd->last.name[nd->last.len])
2150 goto exit;
a2c36b45 2151
a1e28038
AV
2152 mutex_lock(&dir->d_inode->i_mutex);
2153
6c0d46c4
AV
2154 dentry = lookup_hash(nd);
2155 error = PTR_ERR(dentry);
2156 if (IS_ERR(dentry)) {
fb1cc555
AV
2157 mutex_unlock(&dir->d_inode->i_mutex);
2158 goto exit;
2159 }
2160
6c0d46c4
AV
2161 path->dentry = dentry;
2162 path->mnt = nd->path.mnt;
2163
fb1cc555 2164 /* Negative dentry, just create the file */
6c0d46c4
AV
2165 if (!dentry->d_inode) {
2166 int mode = op->mode;
2167 if (!IS_POSIXACL(dir->d_inode))
2168 mode &= ~current_umask();
fb1cc555
AV
2169 /*
2170 * This write is needed to ensure that a
6c0d46c4 2171 * rw->ro transition does not occur between
fb1cc555
AV
2172 * the time when the file is created and when
2173 * a permanent write count is taken through
2174 * the 'struct file' in nameidata_to_filp().
2175 */
2176 error = mnt_want_write(nd->path.mnt);
2177 if (error)
2178 goto exit_mutex_unlock;
ca344a89 2179 want_write = 1;
9b44f1b3 2180 /* Don't check for write permission, don't truncate */
ca344a89 2181 open_flag &= ~O_TRUNC;
6c0d46c4 2182 will_truncate = 0;
bcda7652 2183 acc_mode = MAY_OPEN;
6c0d46c4
AV
2184 error = security_path_mknod(&nd->path, dentry, mode, 0);
2185 if (error)
2186 goto exit_mutex_unlock;
2187 error = vfs_create(dir->d_inode, dentry, mode, nd);
2188 if (error)
2189 goto exit_mutex_unlock;
2190 mutex_unlock(&dir->d_inode->i_mutex);
2191 dput(nd->path.dentry);
2192 nd->path.dentry = dentry;
ca344a89 2193 goto common;
fb1cc555
AV
2194 }
2195
2196 /*
2197 * It already exists.
2198 */
2199 mutex_unlock(&dir->d_inode->i_mutex);
2200 audit_inode(pathname, path->dentry);
2201
2202 error = -EEXIST;
ca344a89 2203 if (open_flag & O_EXCL)
fb1cc555
AV
2204 goto exit_dput;
2205
9875cf80
DH
2206 error = follow_managed(path, nd->flags);
2207 if (error < 0)
2208 goto exit_dput;
fb1cc555
AV
2209
2210 error = -ENOENT;
2211 if (!path->dentry->d_inode)
2212 goto exit_dput;
9e67f361
AV
2213
2214 if (path->dentry->d_inode->i_op->follow_link)
fb1cc555 2215 return NULL;
fb1cc555
AV
2216
2217 path_to_nameidata(path, nd);
31e6b01f 2218 nd->inode = path->dentry->d_inode;
fb1cc555 2219 error = -EISDIR;
31e6b01f 2220 if (S_ISDIR(nd->inode->i_mode))
fb1cc555 2221 goto exit;
67ee3ad2 2222ok:
6c0d46c4
AV
2223 if (!S_ISREG(nd->inode->i_mode))
2224 will_truncate = 0;
2225
0f9d1a10
AV
2226 if (will_truncate) {
2227 error = mnt_want_write(nd->path.mnt);
2228 if (error)
2229 goto exit;
ca344a89 2230 want_write = 1;
0f9d1a10 2231 }
ca344a89 2232common:
bcda7652 2233 error = may_open(&nd->path, acc_mode, open_flag);
ca344a89 2234 if (error)
0f9d1a10 2235 goto exit;
0f9d1a10
AV
2236 filp = nameidata_to_filp(nd);
2237 if (!IS_ERR(filp)) {
2238 error = ima_file_check(filp, op->acc_mode);
2239 if (error) {
2240 fput(filp);
2241 filp = ERR_PTR(error);
2242 }
2243 }
2244 if (!IS_ERR(filp)) {
2245 if (will_truncate) {
2246 error = handle_truncate(filp);
2247 if (error) {
2248 fput(filp);
2249 filp = ERR_PTR(error);
2250 }
2251 }
2252 }
ca344a89
AV
2253out:
2254 if (want_write)
0f9d1a10
AV
2255 mnt_drop_write(nd->path.mnt);
2256 path_put(&nd->path);
fb1cc555
AV
2257 return filp;
2258
2259exit_mutex_unlock:
2260 mutex_unlock(&dir->d_inode->i_mutex);
2261exit_dput:
2262 path_put_conditional(path, nd);
2263exit:
ca344a89
AV
2264 filp = ERR_PTR(error);
2265 goto out;
fb1cc555
AV
2266}
2267
13aab428 2268static struct file *path_openat(int dfd, const char *pathname,
73d049a4 2269 struct nameidata *nd, const struct open_flags *op, int flags)
1da177e4 2270{
fe2d35ff 2271 struct file *base = NULL;
4a3fd211 2272 struct file *filp;
9850c056 2273 struct path path;
13aab428 2274 int error;
31e6b01f
NP
2275
2276 filp = get_empty_filp();
2277 if (!filp)
2278 return ERR_PTR(-ENFILE);
2279
47c805dc 2280 filp->f_flags = op->open_flag;
73d049a4
AV
2281 nd->intent.open.file = filp;
2282 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2283 nd->intent.open.create_mode = op->mode;
31e6b01f 2284
73d049a4 2285 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
31e6b01f 2286 if (unlikely(error))
13aab428 2287 goto out_filp;
31e6b01f 2288
fe2d35ff 2289 current->total_link_count = 0;
73d049a4 2290 error = link_path_walk(pathname, nd);
31e6b01f
NP
2291 if (unlikely(error))
2292 goto out_filp;
1da177e4 2293
73d049a4 2294 filp = do_last(nd, &path, op, pathname);
806b681c 2295 while (unlikely(!filp)) { /* trailing symlink */
7b9337aa 2296 struct path link = path;
def4af30 2297 void *cookie;
574197e0 2298 if (!(nd->flags & LOOKUP_FOLLOW)) {
73d049a4
AV
2299 path_put_conditional(&path, nd);
2300 path_put(&nd->path);
40b39136
AV
2301 filp = ERR_PTR(-ELOOP);
2302 break;
2303 }
73d049a4
AV
2304 nd->flags |= LOOKUP_PARENT;
2305 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
574197e0 2306 error = follow_link(&link, nd, &cookie);
c3e380b0 2307 if (unlikely(error))
f1afe9ef 2308 filp = ERR_PTR(error);
c3e380b0 2309 else
73d049a4 2310 filp = do_last(nd, &path, op, pathname);
574197e0 2311 put_link(nd, &link, cookie);
806b681c 2312 }
10fa8e62 2313out:
73d049a4
AV
2314 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2315 path_put(&nd->root);
fe2d35ff
AV
2316 if (base)
2317 fput(base);
73d049a4 2318 release_open_intent(nd);
10fa8e62 2319 return filp;
1da177e4 2320
31e6b01f 2321out_filp:
806b681c 2322 filp = ERR_PTR(error);
10fa8e62 2323 goto out;
1da177e4
LT
2324}
2325
13aab428
AV
2326struct file *do_filp_open(int dfd, const char *pathname,
2327 const struct open_flags *op, int flags)
2328{
73d049a4 2329 struct nameidata nd;
13aab428
AV
2330 struct file *filp;
2331
73d049a4 2332 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
13aab428 2333 if (unlikely(filp == ERR_PTR(-ECHILD)))
73d049a4 2334 filp = path_openat(dfd, pathname, &nd, op, flags);
13aab428 2335 if (unlikely(filp == ERR_PTR(-ESTALE)))
73d049a4 2336 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
13aab428
AV
2337 return filp;
2338}
2339
73d049a4
AV
2340struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2341 const char *name, const struct open_flags *op, int flags)
2342{
2343 struct nameidata nd;
2344 struct file *file;
2345
2346 nd.root.mnt = mnt;
2347 nd.root.dentry = dentry;
2348
2349 flags |= LOOKUP_ROOT;
2350
bcda7652 2351 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
73d049a4
AV
2352 return ERR_PTR(-ELOOP);
2353
2354 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2355 if (unlikely(file == ERR_PTR(-ECHILD)))
2356 file = path_openat(-1, name, &nd, op, flags);
2357 if (unlikely(file == ERR_PTR(-ESTALE)))
2358 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2359 return file;
2360}
2361
ed75e95d 2362struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
1da177e4 2363{
c663e5d8 2364 struct dentry *dentry = ERR_PTR(-EEXIST);
ed75e95d
AV
2365 struct nameidata nd;
2366 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2367 if (error)
2368 return ERR_PTR(error);
1da177e4 2369
c663e5d8
CH
2370 /*
2371 * Yucky last component or no last component at all?
2372 * (foo/., foo/.., /////)
2373 */
ed75e95d
AV
2374 if (nd.last_type != LAST_NORM)
2375 goto out;
2376 nd.flags &= ~LOOKUP_PARENT;
2377 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2378 nd.intent.open.flags = O_EXCL;
c663e5d8
CH
2379
2380 /*
2381 * Do the final lookup.
2382 */
ed75e95d
AV
2383 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2384 dentry = lookup_hash(&nd);
1da177e4
LT
2385 if (IS_ERR(dentry))
2386 goto fail;
c663e5d8 2387
e9baf6e5
AV
2388 if (dentry->d_inode)
2389 goto eexist;
c663e5d8
CH
2390 /*
2391 * Special case - lookup gave negative, but... we had foo/bar/
2392 * From the vfs_mknod() POV we just have a negative dentry -
2393 * all is fine. Let's be bastards - you had / on the end, you've
2394 * been asking for (non-existent) directory. -ENOENT for you.
2395 */
ed75e95d 2396 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
e9baf6e5
AV
2397 dput(dentry);
2398 dentry = ERR_PTR(-ENOENT);
ed75e95d 2399 goto fail;
e9baf6e5 2400 }
ed75e95d 2401 *path = nd.path;
1da177e4 2402 return dentry;
e9baf6e5 2403eexist:
1da177e4 2404 dput(dentry);
e9baf6e5 2405 dentry = ERR_PTR(-EEXIST);
1da177e4 2406fail:
ed75e95d
AV
2407 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2408out:
2409 path_put(&nd.path);
1da177e4
LT
2410 return dentry;
2411}
dae6ad8f
AV
2412EXPORT_SYMBOL(kern_path_create);
2413
2414struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2415{
2416 char *tmp = getname(pathname);
2417 struct dentry *res;
2418 if (IS_ERR(tmp))
2419 return ERR_CAST(tmp);
2420 res = kern_path_create(dfd, tmp, path, is_dir);
2421 putname(tmp);
2422 return res;
2423}
2424EXPORT_SYMBOL(user_path_create);
2425
1da177e4
LT
2426int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2427{
a95164d9 2428 int error = may_create(dir, dentry);
1da177e4
LT
2429
2430 if (error)
2431 return error;
2432
e795b717
SH
2433 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2434 !ns_capable(inode_userns(dir), CAP_MKNOD))
1da177e4
LT
2435 return -EPERM;
2436
acfa4380 2437 if (!dir->i_op->mknod)
1da177e4
LT
2438 return -EPERM;
2439
08ce5f16
SH
2440 error = devcgroup_inode_mknod(mode, dev);
2441 if (error)
2442 return error;
2443
1da177e4
LT
2444 error = security_inode_mknod(dir, dentry, mode, dev);
2445 if (error)
2446 return error;
2447
1da177e4 2448 error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aa 2449 if (!error)
f38aa942 2450 fsnotify_create(dir, dentry);
1da177e4
LT
2451 return error;
2452}
2453
463c3197
DH
2454static int may_mknod(mode_t mode)
2455{
2456 switch (mode & S_IFMT) {
2457 case S_IFREG:
2458 case S_IFCHR:
2459 case S_IFBLK:
2460 case S_IFIFO:
2461 case S_IFSOCK:
2462 case 0: /* zero mode translates to S_IFREG */
2463 return 0;
2464 case S_IFDIR:
2465 return -EPERM;
2466 default:
2467 return -EINVAL;
2468 }
2469}
2470
2e4d0924
HC
2471SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2472 unsigned, dev)
1da177e4 2473{
2ad94ae6 2474 struct dentry *dentry;
dae6ad8f
AV
2475 struct path path;
2476 int error;
1da177e4
LT
2477
2478 if (S_ISDIR(mode))
2479 return -EPERM;
1da177e4 2480
dae6ad8f
AV
2481 dentry = user_path_create(dfd, filename, &path, 0);
2482 if (IS_ERR(dentry))
2483 return PTR_ERR(dentry);
2ad94ae6 2484
dae6ad8f 2485 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 2486 mode &= ~current_umask();
463c3197
DH
2487 error = may_mknod(mode);
2488 if (error)
2489 goto out_dput;
dae6ad8f 2490 error = mnt_want_write(path.mnt);
463c3197
DH
2491 if (error)
2492 goto out_dput;
dae6ad8f 2493 error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56
KT
2494 if (error)
2495 goto out_drop_write;
463c3197 2496 switch (mode & S_IFMT) {
1da177e4 2497 case 0: case S_IFREG:
dae6ad8f 2498 error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
1da177e4
LT
2499 break;
2500 case S_IFCHR: case S_IFBLK:
dae6ad8f 2501 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4
LT
2502 new_decode_dev(dev));
2503 break;
2504 case S_IFIFO: case S_IFSOCK:
dae6ad8f 2505 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4 2506 break;
1da177e4 2507 }
be6d3e56 2508out_drop_write:
dae6ad8f 2509 mnt_drop_write(path.mnt);
463c3197
DH
2510out_dput:
2511 dput(dentry);
dae6ad8f
AV
2512 mutex_unlock(&path.dentry->d_inode->i_mutex);
2513 path_put(&path);
1da177e4
LT
2514
2515 return error;
2516}
2517
3480b257 2518SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
5590ff0d
UD
2519{
2520 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2521}
2522
1da177e4
LT
2523int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2524{
a95164d9 2525 int error = may_create(dir, dentry);
1da177e4
LT
2526
2527 if (error)
2528 return error;
2529
acfa4380 2530 if (!dir->i_op->mkdir)
1da177e4
LT
2531 return -EPERM;
2532
2533 mode &= (S_IRWXUGO|S_ISVTX);
2534 error = security_inode_mkdir(dir, dentry, mode);
2535 if (error)
2536 return error;
2537
1da177e4 2538 error = dir->i_op->mkdir(dir, dentry, mode);
a74574aa 2539 if (!error)
f38aa942 2540 fsnotify_mkdir(dir, dentry);
1da177e4
LT
2541 return error;
2542}
2543
2e4d0924 2544SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
1da177e4 2545{
6902d925 2546 struct dentry *dentry;
dae6ad8f
AV
2547 struct path path;
2548 int error;
1da177e4 2549
dae6ad8f 2550 dentry = user_path_create(dfd, pathname, &path, 1);
6902d925 2551 if (IS_ERR(dentry))
dae6ad8f 2552 return PTR_ERR(dentry);
1da177e4 2553
dae6ad8f 2554 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 2555 mode &= ~current_umask();
dae6ad8f 2556 error = mnt_want_write(path.mnt);
463c3197
DH
2557 if (error)
2558 goto out_dput;
dae6ad8f 2559 error = security_path_mkdir(&path, dentry, mode);
be6d3e56
KT
2560 if (error)
2561 goto out_drop_write;
dae6ad8f 2562 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
be6d3e56 2563out_drop_write:
dae6ad8f 2564 mnt_drop_write(path.mnt);
463c3197 2565out_dput:
6902d925 2566 dput(dentry);
dae6ad8f
AV
2567 mutex_unlock(&path.dentry->d_inode->i_mutex);
2568 path_put(&path);
1da177e4
LT
2569 return error;
2570}
2571
3cdad428 2572SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
5590ff0d
UD
2573{
2574 return sys_mkdirat(AT_FDCWD, pathname, mode);
2575}
2576
1da177e4 2577/*
a71905f0
SW
2578 * The dentry_unhash() helper will try to drop the dentry early: we
2579 * should have a usage count of 2 if we're the only user of this
2580 * dentry, and if that is true (possibly after pruning the dcache),
2581 * then we drop the dentry now.
1da177e4
LT
2582 *
2583 * A low-level filesystem can, if it choses, legally
2584 * do a
2585 *
2586 * if (!d_unhashed(dentry))
2587 * return -EBUSY;
2588 *
2589 * if it cannot handle the case of removing a directory
2590 * that is still in use by something else..
2591 */
2592void dentry_unhash(struct dentry *dentry)
2593{
dc168427 2594 shrink_dcache_parent(dentry);
1da177e4 2595 spin_lock(&dentry->d_lock);
64252c75 2596 if (dentry->d_count == 1)
1da177e4
LT
2597 __d_drop(dentry);
2598 spin_unlock(&dentry->d_lock);
1da177e4
LT
2599}
2600
2601int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2602{
2603 int error = may_delete(dir, dentry, 1);
2604
2605 if (error)
2606 return error;
2607
acfa4380 2608 if (!dir->i_op->rmdir)
1da177e4
LT
2609 return -EPERM;
2610
1d2ef590 2611 dget(dentry);
1b1dcc1b 2612 mutex_lock(&dentry->d_inode->i_mutex);
912dbc15
SW
2613
2614 error = -EBUSY;
1da177e4 2615 if (d_mountpoint(dentry))
912dbc15
SW
2616 goto out;
2617
2618 error = security_inode_rmdir(dir, dentry);
2619 if (error)
2620 goto out;
2621
3cebde24 2622 shrink_dcache_parent(dentry);
912dbc15
SW
2623 error = dir->i_op->rmdir(dir, dentry);
2624 if (error)
2625 goto out;
2626
2627 dentry->d_inode->i_flags |= S_DEAD;
2628 dont_mount(dentry);
2629
2630out:
1b1dcc1b 2631 mutex_unlock(&dentry->d_inode->i_mutex);
1d2ef590 2632 dput(dentry);
912dbc15 2633 if (!error)
1da177e4 2634 d_delete(dentry);
1da177e4
LT
2635 return error;
2636}
2637
5590ff0d 2638static long do_rmdir(int dfd, const char __user *pathname)
1da177e4
LT
2639{
2640 int error = 0;
2641 char * name;
2642 struct dentry *dentry;
2643 struct nameidata nd;
2644
2ad94ae6 2645 error = user_path_parent(dfd, pathname, &nd, &name);
1da177e4 2646 if (error)
2ad94ae6 2647 return error;
1da177e4
LT
2648
2649 switch(nd.last_type) {
0612d9fb
OH
2650 case LAST_DOTDOT:
2651 error = -ENOTEMPTY;
2652 goto exit1;
2653 case LAST_DOT:
2654 error = -EINVAL;
2655 goto exit1;
2656 case LAST_ROOT:
2657 error = -EBUSY;
2658 goto exit1;
1da177e4 2659 }
0612d9fb
OH
2660
2661 nd.flags &= ~LOOKUP_PARENT;
2662
4ac91378 2663 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b77 2664 dentry = lookup_hash(&nd);
1da177e4 2665 error = PTR_ERR(dentry);
6902d925
DH
2666 if (IS_ERR(dentry))
2667 goto exit2;
e6bc45d6
TT
2668 if (!dentry->d_inode) {
2669 error = -ENOENT;
2670 goto exit3;
2671 }
0622753b
DH
2672 error = mnt_want_write(nd.path.mnt);
2673 if (error)
2674 goto exit3;
be6d3e56
KT
2675 error = security_path_rmdir(&nd.path, dentry);
2676 if (error)
2677 goto exit4;
4ac91378 2678 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
be6d3e56 2679exit4:
0622753b
DH
2680 mnt_drop_write(nd.path.mnt);
2681exit3:
6902d925
DH
2682 dput(dentry);
2683exit2:
4ac91378 2684 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4 2685exit1:
1d957f9b 2686 path_put(&nd.path);
1da177e4
LT
2687 putname(name);
2688 return error;
2689}
2690
3cdad428 2691SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d
UD
2692{
2693 return do_rmdir(AT_FDCWD, pathname);
2694}
2695
1da177e4
LT
2696int vfs_unlink(struct inode *dir, struct dentry *dentry)
2697{
2698 int error = may_delete(dir, dentry, 0);
2699
2700 if (error)
2701 return error;
2702
acfa4380 2703 if (!dir->i_op->unlink)
1da177e4
LT
2704 return -EPERM;
2705
1b1dcc1b 2706 mutex_lock(&dentry->d_inode->i_mutex);
1da177e4
LT
2707 if (d_mountpoint(dentry))
2708 error = -EBUSY;
2709 else {
2710 error = security_inode_unlink(dir, dentry);
bec1052e 2711 if (!error) {
1da177e4 2712 error = dir->i_op->unlink(dir, dentry);
bec1052e 2713 if (!error)
d83c49f3 2714 dont_mount(dentry);
bec1052e 2715 }
1da177e4 2716 }
1b1dcc1b 2717 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4
LT
2718
2719 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2720 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
ece95912 2721 fsnotify_link_count(dentry->d_inode);
e234f35c 2722 d_delete(dentry);
1da177e4 2723 }
0eeca283 2724
1da177e4
LT
2725 return error;
2726}
2727
2728/*
2729 * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b 2730 * directory's i_mutex. Truncate can take a long time if there is a lot of
1da177e4
LT
2731 * writeout happening, and we don't want to prevent access to the directory
2732 * while waiting on the I/O.
2733 */
5590ff0d 2734static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4 2735{
2ad94ae6
AV
2736 int error;
2737 char *name;
1da177e4
LT
2738 struct dentry *dentry;
2739 struct nameidata nd;
2740 struct inode *inode = NULL;
2741
2ad94ae6 2742 error = user_path_parent(dfd, pathname, &nd, &name);
1da177e4 2743 if (error)
2ad94ae6
AV
2744 return error;
2745
1da177e4
LT
2746 error = -EISDIR;
2747 if (nd.last_type != LAST_NORM)
2748 goto exit1;
0612d9fb
OH
2749
2750 nd.flags &= ~LOOKUP_PARENT;
2751
4ac91378 2752 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b77 2753 dentry = lookup_hash(&nd);
1da177e4
LT
2754 error = PTR_ERR(dentry);
2755 if (!IS_ERR(dentry)) {
2756 /* Why not before? Because we want correct error value */
50338b88
TE
2757 if (nd.last.name[nd.last.len])
2758 goto slashes;
1da177e4 2759 inode = dentry->d_inode;
50338b88 2760 if (!inode)
e6bc45d6
TT
2761 goto slashes;
2762 ihold(inode);
0622753b
DH
2763 error = mnt_want_write(nd.path.mnt);
2764 if (error)
2765 goto exit2;
be6d3e56
KT
2766 error = security_path_unlink(&nd.path, dentry);
2767 if (error)
2768 goto exit3;
4ac91378 2769 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
be6d3e56 2770exit3:
0622753b 2771 mnt_drop_write(nd.path.mnt);
1da177e4
LT
2772 exit2:
2773 dput(dentry);
2774 }
4ac91378 2775 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4
LT
2776 if (inode)
2777 iput(inode); /* truncate the inode here */
2778exit1:
1d957f9b 2779 path_put(&nd.path);
1da177e4
LT
2780 putname(name);
2781 return error;
2782
2783slashes:
2784 error = !dentry->d_inode ? -ENOENT :
2785 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2786 goto exit2;
2787}
2788
2e4d0924 2789SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d
UD
2790{
2791 if ((flag & ~AT_REMOVEDIR) != 0)
2792 return -EINVAL;
2793
2794 if (flag & AT_REMOVEDIR)
2795 return do_rmdir(dfd, pathname);
2796
2797 return do_unlinkat(dfd, pathname);
2798}
2799
3480b257 2800SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d
UD
2801{
2802 return do_unlinkat(AT_FDCWD, pathname);
2803}
2804
db2e747b 2805int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4 2806{
a95164d9 2807 int error = may_create(dir, dentry);
1da177e4
LT
2808
2809 if (error)
2810 return error;
2811
acfa4380 2812 if (!dir->i_op->symlink)
1da177e4
LT
2813 return -EPERM;
2814
2815 error = security_inode_symlink(dir, dentry, oldname);
2816 if (error)
2817 return error;
2818
1da177e4 2819 error = dir->i_op->symlink(dir, dentry, oldname);
a74574aa 2820 if (!error)
f38aa942 2821 fsnotify_create(dir, dentry);
1da177e4
LT
2822 return error;
2823}
2824
2e4d0924
HC
2825SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2826 int, newdfd, const char __user *, newname)
1da177e4 2827{
2ad94ae6
AV
2828 int error;
2829 char *from;
6902d925 2830 struct dentry *dentry;
dae6ad8f 2831 struct path path;
1da177e4
LT
2832
2833 from = getname(oldname);
2ad94ae6 2834 if (IS_ERR(from))
1da177e4 2835 return PTR_ERR(from);
1da177e4 2836
dae6ad8f 2837 dentry = user_path_create(newdfd, newname, &path, 0);
6902d925
DH
2838 error = PTR_ERR(dentry);
2839 if (IS_ERR(dentry))
dae6ad8f 2840 goto out_putname;
6902d925 2841
dae6ad8f 2842 error = mnt_want_write(path.mnt);
75c3f29d
DH
2843 if (error)
2844 goto out_dput;
dae6ad8f 2845 error = security_path_symlink(&path, dentry, from);
be6d3e56
KT
2846 if (error)
2847 goto out_drop_write;
dae6ad8f 2848 error = vfs_symlink(path.dentry->d_inode, dentry, from);
be6d3e56 2849out_drop_write:
dae6ad8f 2850 mnt_drop_write(path.mnt);
75c3f29d 2851out_dput:
6902d925 2852 dput(dentry);
dae6ad8f
AV
2853 mutex_unlock(&path.dentry->d_inode->i_mutex);
2854 path_put(&path);
6902d925 2855out_putname:
1da177e4
LT
2856 putname(from);
2857 return error;
2858}
2859
3480b257 2860SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d
UD
2861{
2862 return sys_symlinkat(oldname, AT_FDCWD, newname);
2863}
2864
1da177e4
LT
2865int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2866{
2867 struct inode *inode = old_dentry->d_inode;
2868 int error;
2869
2870 if (!inode)
2871 return -ENOENT;
2872
a95164d9 2873 error = may_create(dir, new_dentry);
1da177e4
LT
2874 if (error)
2875 return error;
2876
2877 if (dir->i_sb != inode->i_sb)
2878 return -EXDEV;
2879
2880 /*
2881 * A link to an append-only or immutable file cannot be created.
2882 */
2883 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2884 return -EPERM;
acfa4380 2885 if (!dir->i_op->link)
1da177e4 2886 return -EPERM;
7e79eedb 2887 if (S_ISDIR(inode->i_mode))
1da177e4
LT
2888 return -EPERM;
2889
2890 error = security_inode_link(old_dentry, dir, new_dentry);
2891 if (error)
2892 return error;
2893
7e79eedb 2894 mutex_lock(&inode->i_mutex);
aae8a97d
AK
2895 /* Make sure we don't allow creating hardlink to an unlinked file */
2896 if (inode->i_nlink == 0)
2897 error = -ENOENT;
2898 else
2899 error = dir->i_op->link(old_dentry, dir, new_dentry);
7e79eedb 2900 mutex_unlock(&inode->i_mutex);
e31e14ec 2901 if (!error)
7e79eedb 2902 fsnotify_link(dir, inode, new_dentry);
1da177e4
LT
2903 return error;
2904}
2905
2906/*
2907 * Hardlinks are often used in delicate situations. We avoid
2908 * security-related surprises by not following symlinks on the
2909 * newname. --KAB
2910 *
2911 * We don't follow them on the oldname either to be compatible
2912 * with linux 2.0, and to avoid hard-linking to directories
2913 * and other special files. --ADM
2914 */
2e4d0924
HC
2915SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2916 int, newdfd, const char __user *, newname, int, flags)
1da177e4
LT
2917{
2918 struct dentry *new_dentry;
dae6ad8f 2919 struct path old_path, new_path;
11a7b371 2920 int how = 0;
1da177e4 2921 int error;
1da177e4 2922
11a7b371 2923 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e1 2924 return -EINVAL;
11a7b371
AK
2925 /*
2926 * To use null names we require CAP_DAC_READ_SEARCH
2927 * This ensures that not everyone will be able to create
2928 * handlink using the passed filedescriptor.
2929 */
2930 if (flags & AT_EMPTY_PATH) {
2931 if (!capable(CAP_DAC_READ_SEARCH))
2932 return -ENOENT;
2933 how = LOOKUP_EMPTY;
2934 }
2935
2936 if (flags & AT_SYMLINK_FOLLOW)
2937 how |= LOOKUP_FOLLOW;
c04030e1 2938
11a7b371 2939 error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4 2940 if (error)
2ad94ae6
AV
2941 return error;
2942
dae6ad8f 2943 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
1da177e4 2944 error = PTR_ERR(new_dentry);
6902d925 2945 if (IS_ERR(new_dentry))
dae6ad8f
AV
2946 goto out;
2947
2948 error = -EXDEV;
2949 if (old_path.mnt != new_path.mnt)
2950 goto out_dput;
2951 error = mnt_want_write(new_path.mnt);
75c3f29d
DH
2952 if (error)
2953 goto out_dput;
dae6ad8f 2954 error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56
KT
2955 if (error)
2956 goto out_drop_write;
dae6ad8f 2957 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
be6d3e56 2958out_drop_write:
dae6ad8f 2959 mnt_drop_write(new_path.mnt);
75c3f29d 2960out_dput:
6902d925 2961 dput(new_dentry);
dae6ad8f
AV
2962 mutex_unlock(&new_path.dentry->d_inode->i_mutex);
2963 path_put(&new_path);
1da177e4 2964out:
2d8f3038 2965 path_put(&old_path);
1da177e4
LT
2966
2967 return error;
2968}
2969
3480b257 2970SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d 2971{
c04030e1 2972 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
2973}
2974
1da177e4
LT
2975/*
2976 * The worst of all namespace operations - renaming directory. "Perverted"
2977 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2978 * Problems:
2979 * a) we can get into loop creation. Check is done in is_subdir().
2980 * b) race potential - two innocent renames can create a loop together.
2981 * That's where 4.4 screws up. Current fix: serialization on
a11f3a05 2982 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4
LT
2983 * story.
2984 * c) we have to lock _three_ objects - parents and victim (if it exists).
1b1dcc1b 2985 * And that - after we got ->i_mutex on parents (until then we don't know
1da177e4
LT
2986 * whether the target exists). Solution: try to be smart with locking
2987 * order for inodes. We rely on the fact that tree topology may change
a11f3a05 2988 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4
LT
2989 * move will be locked. Thus we can rank directories by the tree
2990 * (ancestors first) and rank all non-directories after them.
2991 * That works since everybody except rename does "lock parent, lookup,
a11f3a05 2992 * lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4
LT
2993 * HOWEVER, it relies on the assumption that any object with ->lookup()
2994 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2995 * we'd better make sure that there's no link(2) for them.
e4eaac06 2996 * d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b 2997 * we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4 2998 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e7 2999 * ->i_mutex on parents, which works but leads to some truly excessive
1da177e4
LT
3000 * locking].
3001 */
75c96f85
AB
3002static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3003 struct inode *new_dir, struct dentry *new_dentry)
1da177e4
LT
3004{
3005 int error = 0;
9055cba7 3006 struct inode *target = new_dentry->d_inode;
1da177e4
LT
3007
3008 /*
3009 * If we are going to change the parent - check write permissions,
3010 * we'll need to flip '..'.
3011 */
3012 if (new_dir != old_dir) {
f419a2e3 3013 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
1da177e4
LT
3014 if (error)
3015 return error;
3016 }
3017
3018 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3019 if (error)
3020 return error;
3021
1d2ef590 3022 dget(new_dentry);
d83c49f3 3023 if (target)
1b1dcc1b 3024 mutex_lock(&target->i_mutex);
9055cba7
SW
3025
3026 error = -EBUSY;
3027 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3028 goto out;
3029
3cebde24
SW
3030 if (target)
3031 shrink_dcache_parent(new_dentry);
9055cba7
SW
3032 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3033 if (error)
3034 goto out;
3035
1da177e4 3036 if (target) {
9055cba7
SW
3037 target->i_flags |= S_DEAD;
3038 dont_mount(new_dentry);
1da177e4 3039 }
9055cba7
SW
3040out:
3041 if (target)
3042 mutex_unlock(&target->i_mutex);
1d2ef590 3043 dput(new_dentry);
e31e14ec 3044 if (!error)
349457cc
MF
3045 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3046 d_move(old_dentry,new_dentry);
1da177e4
LT
3047 return error;
3048}
3049
75c96f85
AB
3050static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3051 struct inode *new_dir, struct dentry *new_dentry)
1da177e4 3052{
51892bbb 3053 struct inode *target = new_dentry->d_inode;
1da177e4
LT
3054 int error;
3055
3056 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3057 if (error)
3058 return error;
3059
3060 dget(new_dentry);
1da177e4 3061 if (target)
1b1dcc1b 3062 mutex_lock(&target->i_mutex);
51892bbb
SW
3063
3064 error = -EBUSY;
1da177e4 3065 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
51892bbb
SW
3066 goto out;
3067
3068 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3069 if (error)
3070 goto out;
3071
3072 if (target)
3073 dont_mount(new_dentry);
3074 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3075 d_move(old_dentry, new_dentry);
3076out:
1da177e4 3077 if (target)
1b1dcc1b 3078 mutex_unlock(&target->i_mutex);
1da177e4
LT
3079 dput(new_dentry);
3080 return error;
3081}
3082
3083int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3084 struct inode *new_dir, struct dentry *new_dentry)
3085{
3086 int error;
3087 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
59b0df21 3088 const unsigned char *old_name;
1da177e4
LT
3089
3090 if (old_dentry->d_inode == new_dentry->d_inode)
3091 return 0;
3092
3093 error = may_delete(old_dir, old_dentry, is_dir);
3094 if (error)
3095 return error;
3096
3097 if (!new_dentry->d_inode)
a95164d9 3098 error = may_create(new_dir, new_dentry);
1da177e4
LT
3099 else
3100 error = may_delete(new_dir, new_dentry, is_dir);
3101 if (error)
3102 return error;
3103
acfa4380 3104 if (!old_dir->i_op->rename)
1da177e4
LT
3105 return -EPERM;
3106
0eeca283
RL
3107 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3108
1da177e4
LT
3109 if (is_dir)
3110 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3111 else
3112 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
123df294
AV
3113 if (!error)
3114 fsnotify_move(old_dir, new_dir, old_name, is_dir,
5a190ae6 3115 new_dentry->d_inode, old_dentry);
0eeca283
RL
3116 fsnotify_oldname_free(old_name);
3117
1da177e4
LT
3118 return error;
3119}
3120
2e4d0924
HC
3121SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3122 int, newdfd, const char __user *, newname)
1da177e4 3123{
2ad94ae6
AV
3124 struct dentry *old_dir, *new_dir;
3125 struct dentry *old_dentry, *new_dentry;
3126 struct dentry *trap;
1da177e4 3127 struct nameidata oldnd, newnd;
2ad94ae6
AV
3128 char *from;
3129 char *to;
3130 int error;
1da177e4 3131
2ad94ae6 3132 error = user_path_parent(olddfd, oldname, &oldnd, &from);
1da177e4
LT
3133 if (error)
3134 goto exit;
3135
2ad94ae6 3136 error = user_path_parent(newdfd, newname, &newnd, &to);
1da177e4
LT
3137 if (error)
3138 goto exit1;
3139
3140 error = -EXDEV;
4ac91378 3141 if (oldnd.path.mnt != newnd.path.mnt)
1da177e4
LT
3142 goto exit2;
3143
4ac91378 3144 old_dir = oldnd.path.dentry;
1da177e4
LT
3145 error = -EBUSY;
3146 if (oldnd.last_type != LAST_NORM)
3147 goto exit2;
3148
4ac91378 3149 new_dir = newnd.path.dentry;
1da177e4
LT
3150 if (newnd.last_type != LAST_NORM)
3151 goto exit2;
3152
0612d9fb
OH
3153 oldnd.flags &= ~LOOKUP_PARENT;
3154 newnd.flags &= ~LOOKUP_PARENT;
4e9ed2f8 3155 newnd.flags |= LOOKUP_RENAME_TARGET;
0612d9fb 3156
1da177e4
LT
3157 trap = lock_rename(new_dir, old_dir);
3158
49705b77 3159 old_dentry = lookup_hash(&oldnd);
1da177e4
LT
3160 error = PTR_ERR(old_dentry);
3161 if (IS_ERR(old_dentry))
3162 goto exit3;
3163 /* source must exist */
3164 error = -ENOENT;
3165 if (!old_dentry->d_inode)
3166 goto exit4;
3167 /* unless the source is a directory trailing slashes give -ENOTDIR */
3168 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3169 error = -ENOTDIR;
3170 if (oldnd.last.name[oldnd.last.len])
3171 goto exit4;
3172 if (newnd.last.name[newnd.last.len])
3173 goto exit4;
3174 }
3175 /* source should not be ancestor of target */
3176 error = -EINVAL;
3177 if (old_dentry == trap)
3178 goto exit4;
49705b77 3179 new_dentry = lookup_hash(&newnd);
1da177e4
LT
3180 error = PTR_ERR(new_dentry);
3181 if (IS_ERR(new_dentry))
3182 goto exit4;
3183 /* target should not be an ancestor of source */
3184 error = -ENOTEMPTY;
3185 if (new_dentry == trap)
3186 goto exit5;
3187
9079b1eb
DH
3188 error = mnt_want_write(oldnd.path.mnt);
3189 if (error)
3190 goto exit5;
be6d3e56
KT
3191 error = security_path_rename(&oldnd.path, old_dentry,
3192 &newnd.path, new_dentry);
3193 if (error)
3194 goto exit6;
1da177e4
LT
3195 error = vfs_rename(old_dir->d_inode, old_dentry,
3196 new_dir->d_inode, new_dentry);
be6d3e56 3197exit6:
9079b1eb 3198 mnt_drop_write(oldnd.path.mnt);
1da177e4
LT
3199exit5:
3200 dput(new_dentry);
3201exit4:
3202 dput(old_dentry);
3203exit3:
3204 unlock_rename(new_dir, old_dir);
3205exit2:
1d957f9b 3206 path_put(&newnd.path);
2ad94ae6 3207 putname(to);
1da177e4 3208exit1:
1d957f9b 3209 path_put(&oldnd.path);
1da177e4 3210 putname(from);
2ad94ae6 3211exit:
1da177e4
LT
3212 return error;
3213}
3214
a26eab24 3215SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d
UD
3216{
3217 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3218}
3219
1da177e4
LT
3220int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3221{
3222 int len;
3223
3224 len = PTR_ERR(link);
3225 if (IS_ERR(link))
3226 goto out;
3227
3228 len = strlen(link);
3229 if (len > (unsigned) buflen)
3230 len = buflen;
3231 if (copy_to_user(buffer, link, len))
3232 len = -EFAULT;
3233out:
3234 return len;
3235}
3236
3237/*
3238 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3239 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3240 * using) it for any given inode is up to filesystem.
3241 */
3242int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3243{
3244 struct nameidata nd;
cc314eef 3245 void *cookie;
694a1764 3246 int res;
cc314eef 3247
1da177e4 3248 nd.depth = 0;
cc314eef 3249 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
694a1764
MS
3250 if (IS_ERR(cookie))
3251 return PTR_ERR(cookie);
3252
3253 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3254 if (dentry->d_inode->i_op->put_link)
3255 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3256 return res;
1da177e4
LT
3257}
3258
3259int vfs_follow_link(struct nameidata *nd, const char *link)
3260{
3261 return __vfs_follow_link(nd, link);
3262}
3263
3264/* get the link contents into pagecache */
3265static char *page_getlink(struct dentry * dentry, struct page **ppage)
3266{
ebd09abb
DG
3267 char *kaddr;
3268 struct page *page;
1da177e4 3269 struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b18 3270 page = read_mapping_page(mapping, 0, NULL);
1da177e4 3271 if (IS_ERR(page))
6fe6900e 3272 return (char*)page;
1da177e4 3273 *ppage = page;
ebd09abb
DG
3274 kaddr = kmap(page);
3275 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3276 return kaddr;
1da177e4
LT
3277}
3278
3279int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3280{
3281 struct page *page = NULL;
3282 char *s = page_getlink(dentry, &page);
3283 int res = vfs_readlink(dentry,buffer,buflen,s);
3284 if (page) {
3285 kunmap(page);
3286 page_cache_release(page);
3287 }
3288 return res;
3289}
3290
cc314eef 3291void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4 3292{
cc314eef 3293 struct page *page = NULL;
1da177e4 3294 nd_set_link(nd, page_getlink(dentry, &page));
cc314eef 3295 return page;
1da177e4
LT
3296}
3297
cc314eef 3298void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4 3299{
cc314eef
LT
3300 struct page *page = cookie;
3301
3302 if (page) {
1da177e4
LT
3303 kunmap(page);
3304 page_cache_release(page);
1da177e4
LT
3305 }
3306}
3307
54566b2c
NP
3308/*
3309 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3310 */
3311int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4
LT
3312{
3313 struct address_space *mapping = inode->i_mapping;
0adb25d2 3314 struct page *page;
afddba49 3315 void *fsdata;
beb497ab 3316 int err;
1da177e4 3317 char *kaddr;
54566b2c
NP
3318 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3319 if (nofs)
3320 flags |= AOP_FLAG_NOFS;
1da177e4 3321
7e53cac4 3322retry:
afddba49 3323 err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c 3324 flags, &page, &fsdata);
1da177e4 3325 if (err)
afddba49
NP
3326 goto fail;
3327
1da177e4
LT
3328 kaddr = kmap_atomic(page, KM_USER0);
3329 memcpy(kaddr, symname, len-1);
3330 kunmap_atomic(kaddr, KM_USER0);
afddba49
NP
3331
3332 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3333 page, fsdata);
1da177e4
LT
3334 if (err < 0)
3335 goto fail;
afddba49
NP
3336 if (err < len-1)
3337 goto retry;
3338
1da177e4
LT
3339 mark_inode_dirty(inode);
3340 return 0;
1da177e4
LT
3341fail:
3342 return err;
3343}
3344
0adb25d2
KK
3345int page_symlink(struct inode *inode, const char *symname, int len)
3346{
3347 return __page_symlink(inode, symname, len,
54566b2c 3348 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2
KK
3349}
3350
92e1d5be 3351const struct inode_operations page_symlink_inode_operations = {
1da177e4
LT
3352 .readlink = generic_readlink,
3353 .follow_link = page_follow_link_light,
3354 .put_link = page_put_link,
3355};
3356
2d8f3038 3357EXPORT_SYMBOL(user_path_at);
cc53ce53 3358EXPORT_SYMBOL(follow_down_one);
1da177e4
LT
3359EXPORT_SYMBOL(follow_down);
3360EXPORT_SYMBOL(follow_up);
3361EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3362EXPORT_SYMBOL(getname);
3363EXPORT_SYMBOL(lock_rename);
1da177e4
LT
3364EXPORT_SYMBOL(lookup_one_len);
3365EXPORT_SYMBOL(page_follow_link_light);
3366EXPORT_SYMBOL(page_put_link);
3367EXPORT_SYMBOL(page_readlink);
0adb25d2 3368EXPORT_SYMBOL(__page_symlink);
1da177e4
LT
3369EXPORT_SYMBOL(page_symlink);
3370EXPORT_SYMBOL(page_symlink_inode_operations);
d1811465 3371EXPORT_SYMBOL(kern_path);
16f18200 3372EXPORT_SYMBOL(vfs_path_lookup);
f419a2e3 3373EXPORT_SYMBOL(inode_permission);
1da177e4
LT
3374EXPORT_SYMBOL(unlock_rename);
3375EXPORT_SYMBOL(vfs_create);
3376EXPORT_SYMBOL(vfs_follow_link);
3377EXPORT_SYMBOL(vfs_link);
3378EXPORT_SYMBOL(vfs_mkdir);
3379EXPORT_SYMBOL(vfs_mknod);
3380EXPORT_SYMBOL(generic_permission);
3381EXPORT_SYMBOL(vfs_readlink);
3382EXPORT_SYMBOL(vfs_rename);
3383EXPORT_SYMBOL(vfs_rmdir);
3384EXPORT_SYMBOL(vfs_symlink);
3385EXPORT_SYMBOL(vfs_unlink);
3386EXPORT_SYMBOL(dentry_unhash);
3387EXPORT_SYMBOL(generic_readlink);