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