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