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