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