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