]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/namei.c
TPM: integrity fix
[mirror_ubuntu-artful-kernel.git] / fs / namei.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
22#include <linux/quotaops.h>
23#include <linux/pagemap.h>
0eeca283 24#include <linux/fsnotify.h>
1da177e4
LT
25#include <linux/personality.h>
26#include <linux/security.h>
6146f0d5 27#include <linux/ima.h>
1da177e4
LT
28#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
16f7e0fe 31#include <linux/capability.h>
834f2a4a 32#include <linux/file.h>
5590ff0d 33#include <linux/fcntl.h>
08ce5f16 34#include <linux/device_cgroup.h>
1da177e4
LT
35#include <asm/uaccess.h>
36
37#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
38
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
a02f76c3 110static int __link_path_walk(const char *name, struct nameidata *nd);
c4a7808f 111
1da177e4
LT
112/* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
115 *
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
118 */
858119e1 119static int do_getname(const char __user *filename, char *page)
1da177e4
LT
120{
121 int retval;
122 unsigned long len = PATH_MAX;
123
124 if (!segment_eq(get_fs(), KERNEL_DS)) {
125 if ((unsigned long) filename >= TASK_SIZE)
126 return -EFAULT;
127 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
128 len = TASK_SIZE - (unsigned long) filename;
129 }
130
131 retval = strncpy_from_user(page, filename, len);
132 if (retval > 0) {
133 if (retval < len)
134 return 0;
135 return -ENAMETOOLONG;
136 } else if (!retval)
137 retval = -ENOENT;
138 return retval;
139}
140
141char * getname(const char __user * filename)
142{
143 char *tmp, *result;
144
145 result = ERR_PTR(-ENOMEM);
146 tmp = __getname();
147 if (tmp) {
148 int retval = do_getname(filename, tmp);
149
150 result = tmp;
151 if (retval < 0) {
152 __putname(tmp);
153 result = ERR_PTR(retval);
154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
160#ifdef CONFIG_AUDITSYSCALL
161void putname(const char *name)
162{
5ac3a9c2 163 if (unlikely(!audit_dummy_context()))
1da177e4
LT
164 audit_putname(name);
165 else
166 __putname(name);
167}
168EXPORT_SYMBOL(putname);
169#endif
170
171
172/**
173 * generic_permission - check for access rights on a Posix-like filesystem
174 * @inode: inode to check access rights for
175 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
176 * @check_acl: optional callback to check for Posix ACLs
177 *
178 * Used to check for read/write/execute permissions on a file.
179 * We use "fsuid" for this, letting us set arbitrary permissions
180 * for filesystem access without changing the "normal" uids which
181 * are used for other things..
182 */
183int generic_permission(struct inode *inode, int mask,
184 int (*check_acl)(struct inode *inode, int mask))
185{
186 umode_t mode = inode->i_mode;
187
e6305c43
AV
188 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
189
da9592ed 190 if (current_fsuid() == inode->i_uid)
1da177e4
LT
191 mode >>= 6;
192 else {
193 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
194 int error = check_acl(inode, mask);
195 if (error == -EACCES)
196 goto check_capabilities;
197 else if (error != -EAGAIN)
198 return error;
199 }
200
201 if (in_group_p(inode->i_gid))
202 mode >>= 3;
203 }
204
205 /*
206 * If the DACs are ok we don't need any capability check.
207 */
e6305c43 208 if ((mask & ~mode) == 0)
1da177e4
LT
209 return 0;
210
211 check_capabilities:
212 /*
213 * Read/write DACs are always overridable.
214 * Executable DACs are overridable if at least one exec bit is set.
215 */
f696a365 216 if (!(mask & MAY_EXEC) || execute_ok(inode))
1da177e4
LT
217 if (capable(CAP_DAC_OVERRIDE))
218 return 0;
219
220 /*
221 * Searching includes executable on directories, else just read.
222 */
223 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
224 if (capable(CAP_DAC_READ_SEARCH))
225 return 0;
226
227 return -EACCES;
228}
229
f419a2e3 230int inode_permission(struct inode *inode, int mask)
1da177e4 231{
e6305c43 232 int retval;
1da177e4
LT
233
234 if (mask & MAY_WRITE) {
22590e41 235 umode_t mode = inode->i_mode;
1da177e4
LT
236
237 /*
238 * Nobody gets write access to a read-only fs.
239 */
240 if (IS_RDONLY(inode) &&
241 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
242 return -EROFS;
243
244 /*
245 * Nobody gets write access to an immutable file.
246 */
247 if (IS_IMMUTABLE(inode))
248 return -EACCES;
249 }
250
1da177e4 251 /* Ordinary permission routines do not understand MAY_APPEND. */
f696a365 252 if (inode->i_op && inode->i_op->permission)
b77b0646 253 retval = inode->i_op->permission(inode, mask);
f696a365 254 else
e6305c43 255 retval = generic_permission(inode, mask, NULL);
f696a365 256
1da177e4
LT
257 if (retval)
258 return retval;
259
08ce5f16
SH
260 retval = devcgroup_inode_permission(inode, mask);
261 if (retval)
262 return retval;
263
e6305c43 264 return security_inode_permission(inode,
f418b006 265 mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
1da177e4
LT
266}
267
e4543edd
CH
268/**
269 * vfs_permission - check for access rights to a given path
270 * @nd: lookup result that describes the path
271 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
272 *
273 * Used to check for read/write/execute permissions on a path.
274 * We use "fsuid" for this, letting us set arbitrary permissions
275 * for filesystem access without changing the "normal" uids which
276 * are used for other things.
277 */
278int vfs_permission(struct nameidata *nd, int mask)
279{
f419a2e3 280 return inode_permission(nd->path.dentry->d_inode, mask);
e4543edd
CH
281}
282
8c744fb8
CH
283/**
284 * file_permission - check for additional access rights to a given file
285 * @file: file to check access rights for
286 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
287 *
288 * Used to check for read/write/execute permissions on an already opened
289 * file.
290 *
291 * Note:
292 * Do not use this function in new code. All access checks should
293 * be done using vfs_permission().
294 */
295int file_permission(struct file *file, int mask)
296{
f419a2e3 297 return inode_permission(file->f_path.dentry->d_inode, mask);
8c744fb8
CH
298}
299
1da177e4
LT
300/*
301 * get_write_access() gets write permission for a file.
302 * put_write_access() releases this write permission.
303 * This is used for regular files.
304 * We cannot support write (and maybe mmap read-write shared) accesses and
305 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
306 * can have the following values:
307 * 0: no writers, no VM_DENYWRITE mappings
308 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
309 * > 0: (i_writecount) users are writing to the file.
310 *
311 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
312 * except for the cases where we don't hold i_writecount yet. Then we need to
313 * use {get,deny}_write_access() - these functions check the sign and refuse
314 * to do the change if sign is wrong. Exclusion between them is provided by
315 * the inode->i_lock spinlock.
316 */
317
318int get_write_access(struct inode * inode)
319{
320 spin_lock(&inode->i_lock);
321 if (atomic_read(&inode->i_writecount) < 0) {
322 spin_unlock(&inode->i_lock);
323 return -ETXTBSY;
324 }
325 atomic_inc(&inode->i_writecount);
326 spin_unlock(&inode->i_lock);
327
328 return 0;
329}
330
331int deny_write_access(struct file * file)
332{
0f7fc9e4 333 struct inode *inode = file->f_path.dentry->d_inode;
1da177e4
LT
334
335 spin_lock(&inode->i_lock);
336 if (atomic_read(&inode->i_writecount) > 0) {
337 spin_unlock(&inode->i_lock);
338 return -ETXTBSY;
339 }
340 atomic_dec(&inode->i_writecount);
341 spin_unlock(&inode->i_lock);
342
343 return 0;
344}
345
5dd784d0
JB
346/**
347 * path_get - get a reference to a path
348 * @path: path to get the reference to
349 *
350 * Given a path increment the reference count to the dentry and the vfsmount.
351 */
352void path_get(struct path *path)
353{
354 mntget(path->mnt);
355 dget(path->dentry);
356}
357EXPORT_SYMBOL(path_get);
358
1d957f9b
JB
359/**
360 * path_put - put a reference to a path
361 * @path: path to put the reference to
362 *
363 * Given a path decrement the reference count to the dentry and the vfsmount.
364 */
365void path_put(struct path *path)
1da177e4 366{
1d957f9b
JB
367 dput(path->dentry);
368 mntput(path->mnt);
1da177e4 369}
1d957f9b 370EXPORT_SYMBOL(path_put);
1da177e4 371
834f2a4a
TM
372/**
373 * release_open_intent - free up open intent resources
374 * @nd: pointer to nameidata
375 */
376void release_open_intent(struct nameidata *nd)
377{
0f7fc9e4 378 if (nd->intent.open.file->f_path.dentry == NULL)
834f2a4a
TM
379 put_filp(nd->intent.open.file);
380 else
381 fput(nd->intent.open.file);
382}
383
bcdc5e01
IK
384static inline struct dentry *
385do_revalidate(struct dentry *dentry, struct nameidata *nd)
386{
387 int status = dentry->d_op->d_revalidate(dentry, nd);
388 if (unlikely(status <= 0)) {
389 /*
390 * The dentry failed validation.
391 * If d_revalidate returned 0 attempt to invalidate
392 * the dentry otherwise d_revalidate is asking us
393 * to return a fail status.
394 */
395 if (!status) {
396 if (!d_invalidate(dentry)) {
397 dput(dentry);
398 dentry = NULL;
399 }
400 } else {
401 dput(dentry);
402 dentry = ERR_PTR(status);
403 }
404 }
405 return dentry;
406}
407
1da177e4
LT
408/*
409 * Internal lookup() using the new generic dcache.
410 * SMP-safe
411 */
412static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
413{
414 struct dentry * dentry = __d_lookup(parent, name);
415
416 /* lockess __d_lookup may fail due to concurrent d_move()
417 * in some unrelated directory, so try with d_lookup
418 */
419 if (!dentry)
420 dentry = d_lookup(parent, name);
421
bcdc5e01
IK
422 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
423 dentry = do_revalidate(dentry, nd);
424
1da177e4
LT
425 return dentry;
426}
427
428/*
429 * Short-cut version of permission(), for calling by
430 * path_walk(), when dcache lock is held. Combines parts
431 * of permission() and generic_permission(), and tests ONLY for
432 * MAY_EXEC permission.
433 *
434 * If appropriate, check DAC only. If not appropriate, or
435 * short-cut DAC fails, then call permission() to do more
436 * complete permission check.
437 */
672b16b2 438static int exec_permission_lite(struct inode *inode)
1da177e4
LT
439{
440 umode_t mode = inode->i_mode;
441
442 if (inode->i_op && inode->i_op->permission)
443 return -EAGAIN;
444
da9592ed 445 if (current_fsuid() == inode->i_uid)
1da177e4
LT
446 mode >>= 6;
447 else if (in_group_p(inode->i_gid))
448 mode >>= 3;
449
450 if (mode & MAY_EXEC)
451 goto ok;
452
453 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
454 goto ok;
455
456 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
457 goto ok;
458
459 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
460 goto ok;
461
462 return -EACCES;
463ok:
b77b0646 464 return security_inode_permission(inode, MAY_EXEC);
1da177e4
LT
465}
466
467/*
468 * This is called when everything else fails, and we actually have
469 * to go to the low-level filesystem to find out what we should do..
470 *
471 * We get the directory semaphore, and after getting that we also
472 * make sure that nobody added the entry to the dcache in the meantime..
473 * SMP-safe
474 */
475static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
476{
477 struct dentry * result;
478 struct inode *dir = parent->d_inode;
479
1b1dcc1b 480 mutex_lock(&dir->i_mutex);
1da177e4
LT
481 /*
482 * First re-do the cached lookup just in case it was created
483 * while we waited for the directory semaphore..
484 *
485 * FIXME! This could use version numbering or similar to
486 * avoid unnecessary cache lookups.
487 *
488 * The "dcache_lock" is purely to protect the RCU list walker
489 * from concurrent renames at this point (we mustn't get false
490 * negatives from the RCU list walk here, unlike the optimistic
491 * fast walk).
492 *
493 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
494 */
495 result = d_lookup(parent, name);
496 if (!result) {
d70b67c8
MS
497 struct dentry *dentry;
498
499 /* Don't create child dentry for a dead directory. */
500 result = ERR_PTR(-ENOENT);
501 if (IS_DEADDIR(dir))
502 goto out_unlock;
503
504 dentry = d_alloc(parent, name);
1da177e4
LT
505 result = ERR_PTR(-ENOMEM);
506 if (dentry) {
507 result = dir->i_op->lookup(dir, dentry, nd);
508 if (result)
509 dput(dentry);
510 else
511 result = dentry;
512 }
d70b67c8 513out_unlock:
1b1dcc1b 514 mutex_unlock(&dir->i_mutex);
1da177e4
LT
515 return result;
516 }
517
518 /*
519 * Uhhuh! Nasty case: the cache was re-populated while
520 * we waited on the semaphore. Need to revalidate.
521 */
1b1dcc1b 522 mutex_unlock(&dir->i_mutex);
1da177e4 523 if (result->d_op && result->d_op->d_revalidate) {
bcdc5e01
IK
524 result = do_revalidate(result, nd);
525 if (!result)
1da177e4 526 result = ERR_PTR(-ENOENT);
1da177e4
LT
527 }
528 return result;
529}
530
1da177e4 531/* SMP-safe */
7f2da1e7 532static __always_inline void
1da177e4
LT
533walk_init_root(const char *name, struct nameidata *nd)
534{
e518ddb7
AM
535 struct fs_struct *fs = current->fs;
536
537 read_lock(&fs->lock);
6ac08c39
JB
538 nd->path = fs->root;
539 path_get(&fs->root);
e518ddb7 540 read_unlock(&fs->lock);
1da177e4
LT
541}
542
a02f76c3
AV
543/*
544 * Wrapper to retry pathname resolution whenever the underlying
545 * file system returns an ESTALE.
546 *
547 * Retry the whole path once, forcing real lookup requests
548 * instead of relying on the dcache.
549 */
550static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
551{
552 struct path save = nd->path;
553 int result;
554
555 /* make sure the stuff we saved doesn't go away */
c8e7f449 556 path_get(&save);
a02f76c3
AV
557
558 result = __link_path_walk(name, nd);
559 if (result == -ESTALE) {
560 /* nd->path had been dropped */
561 nd->path = save;
c8e7f449 562 path_get(&nd->path);
a02f76c3
AV
563 nd->flags |= LOOKUP_REVAL;
564 result = __link_path_walk(name, nd);
565 }
566
567 path_put(&save);
568
569 return result;
570}
571
f1662356 572static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
1da177e4
LT
573{
574 int res = 0;
575 char *name;
576 if (IS_ERR(link))
577 goto fail;
578
579 if (*link == '/') {
1d957f9b 580 path_put(&nd->path);
7f2da1e7 581 walk_init_root(link, nd);
1da177e4
LT
582 }
583 res = link_path_walk(link, nd);
1da177e4
LT
584 if (nd->depth || res || nd->last_type!=LAST_NORM)
585 return res;
586 /*
587 * If it is an iterative symlinks resolution in open_namei() we
588 * have to copy the last component. And all that crap because of
589 * bloody create() on broken symlinks. Furrfu...
590 */
591 name = __getname();
592 if (unlikely(!name)) {
1d957f9b 593 path_put(&nd->path);
1da177e4
LT
594 return -ENOMEM;
595 }
596 strcpy(name, nd->last.name);
597 nd->last.name = name;
598 return 0;
599fail:
1d957f9b 600 path_put(&nd->path);
1da177e4
LT
601 return PTR_ERR(link);
602}
603
1d957f9b 604static void path_put_conditional(struct path *path, struct nameidata *nd)
051d3812
IK
605{
606 dput(path->dentry);
4ac91378 607 if (path->mnt != nd->path.mnt)
051d3812
IK
608 mntput(path->mnt);
609}
610
611static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
612{
4ac91378
JB
613 dput(nd->path.dentry);
614 if (nd->path.mnt != path->mnt)
615 mntput(nd->path.mnt);
616 nd->path.mnt = path->mnt;
617 nd->path.dentry = path->dentry;
051d3812
IK
618}
619
f1662356 620static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
1da177e4
LT
621{
622 int error;
cc314eef 623 void *cookie;
cd4e91d3 624 struct dentry *dentry = path->dentry;
1da177e4 625
d671a1cb 626 touch_atime(path->mnt, dentry);
1da177e4 627 nd_set_link(nd, NULL);
cd4e91d3 628
4ac91378 629 if (path->mnt != nd->path.mnt) {
051d3812
IK
630 path_to_nameidata(path, nd);
631 dget(dentry);
632 }
633 mntget(path->mnt);
cc314eef
LT
634 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
635 error = PTR_ERR(cookie);
636 if (!IS_ERR(cookie)) {
1da177e4 637 char *s = nd_get_link(nd);
cc314eef 638 error = 0;
1da177e4
LT
639 if (s)
640 error = __vfs_follow_link(nd, s);
641 if (dentry->d_inode->i_op->put_link)
cc314eef 642 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
1da177e4 643 }
09da5916 644 path_put(path);
1da177e4
LT
645
646 return error;
647}
648
649/*
650 * This limits recursive symlink follows to 8, while
651 * limiting consecutive symlinks to 40.
652 *
653 * Without that kind of total limit, nasty chains of consecutive
654 * symlinks can cause almost arbitrarily long lookups.
655 */
90ebe565 656static inline int do_follow_link(struct path *path, struct nameidata *nd)
1da177e4
LT
657{
658 int err = -ELOOP;
659 if (current->link_count >= MAX_NESTED_LINKS)
660 goto loop;
661 if (current->total_link_count >= 40)
662 goto loop;
663 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
664 cond_resched();
90ebe565 665 err = security_inode_follow_link(path->dentry, nd);
1da177e4
LT
666 if (err)
667 goto loop;
668 current->link_count++;
669 current->total_link_count++;
670 nd->depth++;
cd4e91d3 671 err = __do_follow_link(path, nd);
839d9f93
AV
672 current->link_count--;
673 nd->depth--;
1da177e4
LT
674 return err;
675loop:
1d957f9b
JB
676 path_put_conditional(path, nd);
677 path_put(&nd->path);
1da177e4
LT
678 return err;
679}
680
681int follow_up(struct vfsmount **mnt, struct dentry **dentry)
682{
683 struct vfsmount *parent;
684 struct dentry *mountpoint;
685 spin_lock(&vfsmount_lock);
686 parent=(*mnt)->mnt_parent;
687 if (parent == *mnt) {
688 spin_unlock(&vfsmount_lock);
689 return 0;
690 }
691 mntget(parent);
692 mountpoint=dget((*mnt)->mnt_mountpoint);
693 spin_unlock(&vfsmount_lock);
694 dput(*dentry);
695 *dentry = mountpoint;
696 mntput(*mnt);
697 *mnt = parent;
698 return 1;
699}
700
701/* no need for dcache_lock, as serialization is taken care in
702 * namespace.c
703 */
463ffb2e
AV
704static int __follow_mount(struct path *path)
705{
706 int res = 0;
707 while (d_mountpoint(path->dentry)) {
708 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
709 if (!mounted)
710 break;
711 dput(path->dentry);
712 if (res)
713 mntput(path->mnt);
714 path->mnt = mounted;
715 path->dentry = dget(mounted->mnt_root);
716 res = 1;
717 }
718 return res;
719}
720
58c465eb 721static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
1da177e4 722{
1da177e4
LT
723 while (d_mountpoint(*dentry)) {
724 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
725 if (!mounted)
726 break;
58c465eb 727 dput(*dentry);
1da177e4
LT
728 mntput(*mnt);
729 *mnt = mounted;
1da177e4 730 *dentry = dget(mounted->mnt_root);
1da177e4 731 }
1da177e4
LT
732}
733
734/* no need for dcache_lock, as serialization is taken care in
735 * namespace.c
736 */
e13b210f 737int follow_down(struct vfsmount **mnt, struct dentry **dentry)
1da177e4
LT
738{
739 struct vfsmount *mounted;
740
741 mounted = lookup_mnt(*mnt, *dentry);
742 if (mounted) {
e13b210f 743 dput(*dentry);
1da177e4
LT
744 mntput(*mnt);
745 *mnt = mounted;
1da177e4
LT
746 *dentry = dget(mounted->mnt_root);
747 return 1;
748 }
749 return 0;
750}
751
f1662356 752static __always_inline void follow_dotdot(struct nameidata *nd)
1da177e4 753{
e518ddb7
AM
754 struct fs_struct *fs = current->fs;
755
1da177e4
LT
756 while(1) {
757 struct vfsmount *parent;
4ac91378 758 struct dentry *old = nd->path.dentry;
1da177e4 759
e518ddb7 760 read_lock(&fs->lock);
6ac08c39
JB
761 if (nd->path.dentry == fs->root.dentry &&
762 nd->path.mnt == fs->root.mnt) {
e518ddb7 763 read_unlock(&fs->lock);
1da177e4
LT
764 break;
765 }
e518ddb7 766 read_unlock(&fs->lock);
1da177e4 767 spin_lock(&dcache_lock);
4ac91378
JB
768 if (nd->path.dentry != nd->path.mnt->mnt_root) {
769 nd->path.dentry = dget(nd->path.dentry->d_parent);
1da177e4
LT
770 spin_unlock(&dcache_lock);
771 dput(old);
772 break;
773 }
774 spin_unlock(&dcache_lock);
775 spin_lock(&vfsmount_lock);
4ac91378
JB
776 parent = nd->path.mnt->mnt_parent;
777 if (parent == nd->path.mnt) {
1da177e4
LT
778 spin_unlock(&vfsmount_lock);
779 break;
780 }
781 mntget(parent);
4ac91378 782 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
1da177e4
LT
783 spin_unlock(&vfsmount_lock);
784 dput(old);
4ac91378
JB
785 mntput(nd->path.mnt);
786 nd->path.mnt = parent;
1da177e4 787 }
4ac91378 788 follow_mount(&nd->path.mnt, &nd->path.dentry);
1da177e4
LT
789}
790
1da177e4
LT
791/*
792 * It's more convoluted than I'd like it to be, but... it's still fairly
793 * small and for now I'd prefer to have fast path as straight as possible.
794 * It _is_ time-critical.
795 */
796static int do_lookup(struct nameidata *nd, struct qstr *name,
797 struct path *path)
798{
4ac91378
JB
799 struct vfsmount *mnt = nd->path.mnt;
800 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
1da177e4
LT
801
802 if (!dentry)
803 goto need_lookup;
804 if (dentry->d_op && dentry->d_op->d_revalidate)
805 goto need_revalidate;
806done:
807 path->mnt = mnt;
808 path->dentry = dentry;
634ee701 809 __follow_mount(path);
1da177e4
LT
810 return 0;
811
812need_lookup:
4ac91378 813 dentry = real_lookup(nd->path.dentry, name, nd);
1da177e4
LT
814 if (IS_ERR(dentry))
815 goto fail;
816 goto done;
817
818need_revalidate:
bcdc5e01
IK
819 dentry = do_revalidate(dentry, nd);
820 if (!dentry)
821 goto need_lookup;
822 if (IS_ERR(dentry))
823 goto fail;
824 goto done;
1da177e4
LT
825
826fail:
827 return PTR_ERR(dentry);
828}
829
830/*
831 * Name resolution.
ea3834d9
PM
832 * This is the basic name resolution function, turning a pathname into
833 * the final dentry. We expect 'base' to be positive and a directory.
1da177e4 834 *
ea3834d9
PM
835 * Returns 0 and nd will have valid dentry and mnt on success.
836 * Returns error and drops reference to input namei data on failure.
1da177e4 837 */
fc9b52cd 838static int __link_path_walk(const char *name, struct nameidata *nd)
1da177e4
LT
839{
840 struct path next;
841 struct inode *inode;
842 int err;
843 unsigned int lookup_flags = nd->flags;
844
845 while (*name=='/')
846 name++;
847 if (!*name)
848 goto return_reval;
849
4ac91378 850 inode = nd->path.dentry->d_inode;
1da177e4 851 if (nd->depth)
f55eab82 852 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
1da177e4
LT
853
854 /* At this point we know we have a real path component. */
855 for(;;) {
856 unsigned long hash;
857 struct qstr this;
858 unsigned int c;
859
cdce5d6b 860 nd->flags |= LOOKUP_CONTINUE;
672b16b2 861 err = exec_permission_lite(inode);
e4543edd
CH
862 if (err == -EAGAIN)
863 err = vfs_permission(nd, MAY_EXEC);
6146f0d5
MZ
864 if (!err)
865 err = ima_path_check(&nd->path, MAY_EXEC);
1da177e4
LT
866 if (err)
867 break;
868
869 this.name = name;
870 c = *(const unsigned char *)name;
871
872 hash = init_name_hash();
873 do {
874 name++;
875 hash = partial_name_hash(c, hash);
876 c = *(const unsigned char *)name;
877 } while (c && (c != '/'));
878 this.len = name - (const char *) this.name;
879 this.hash = end_name_hash(hash);
880
881 /* remove trailing slashes? */
882 if (!c)
883 goto last_component;
884 while (*++name == '/');
885 if (!*name)
886 goto last_with_slashes;
887
888 /*
889 * "." and ".." are special - ".." especially so because it has
890 * to be able to know about the current root directory and
891 * parent relationships.
892 */
893 if (this.name[0] == '.') switch (this.len) {
894 default:
895 break;
896 case 2:
897 if (this.name[1] != '.')
898 break;
58c465eb 899 follow_dotdot(nd);
4ac91378 900 inode = nd->path.dentry->d_inode;
1da177e4
LT
901 /* fallthrough */
902 case 1:
903 continue;
904 }
905 /*
906 * See if the low-level filesystem might want
907 * to use its own hash..
908 */
4ac91378
JB
909 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
910 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
911 &this);
1da177e4
LT
912 if (err < 0)
913 break;
914 }
1da177e4
LT
915 /* This does the actual lookups.. */
916 err = do_lookup(nd, &this, &next);
917 if (err)
918 break;
1da177e4
LT
919
920 err = -ENOENT;
921 inode = next.dentry->d_inode;
922 if (!inode)
923 goto out_dput;
924 err = -ENOTDIR;
925 if (!inode->i_op)
926 goto out_dput;
927
928 if (inode->i_op->follow_link) {
90ebe565 929 err = do_follow_link(&next, nd);
1da177e4
LT
930 if (err)
931 goto return_err;
932 err = -ENOENT;
4ac91378 933 inode = nd->path.dentry->d_inode;
1da177e4
LT
934 if (!inode)
935 break;
936 err = -ENOTDIR;
937 if (!inode->i_op)
938 break;
09dd17d3
MS
939 } else
940 path_to_nameidata(&next, nd);
1da177e4
LT
941 err = -ENOTDIR;
942 if (!inode->i_op->lookup)
943 break;
944 continue;
945 /* here ends the main loop */
946
947last_with_slashes:
948 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
949last_component:
f55eab82
TM
950 /* Clear LOOKUP_CONTINUE iff it was previously unset */
951 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1da177e4
LT
952 if (lookup_flags & LOOKUP_PARENT)
953 goto lookup_parent;
954 if (this.name[0] == '.') switch (this.len) {
955 default:
956 break;
957 case 2:
958 if (this.name[1] != '.')
959 break;
58c465eb 960 follow_dotdot(nd);
4ac91378 961 inode = nd->path.dentry->d_inode;
1da177e4
LT
962 /* fallthrough */
963 case 1:
964 goto return_reval;
965 }
4ac91378
JB
966 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
967 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
968 &this);
1da177e4
LT
969 if (err < 0)
970 break;
971 }
972 err = do_lookup(nd, &this, &next);
973 if (err)
974 break;
1da177e4
LT
975 inode = next.dentry->d_inode;
976 if ((lookup_flags & LOOKUP_FOLLOW)
977 && inode && inode->i_op && inode->i_op->follow_link) {
90ebe565 978 err = do_follow_link(&next, nd);
1da177e4
LT
979 if (err)
980 goto return_err;
4ac91378 981 inode = nd->path.dentry->d_inode;
09dd17d3
MS
982 } else
983 path_to_nameidata(&next, nd);
1da177e4
LT
984 err = -ENOENT;
985 if (!inode)
986 break;
987 if (lookup_flags & LOOKUP_DIRECTORY) {
988 err = -ENOTDIR;
989 if (!inode->i_op || !inode->i_op->lookup)
990 break;
991 }
992 goto return_base;
993lookup_parent:
994 nd->last = this;
995 nd->last_type = LAST_NORM;
996 if (this.name[0] != '.')
997 goto return_base;
998 if (this.len == 1)
999 nd->last_type = LAST_DOT;
1000 else if (this.len == 2 && this.name[1] == '.')
1001 nd->last_type = LAST_DOTDOT;
1002 else
1003 goto return_base;
1004return_reval:
1005 /*
1006 * We bypassed the ordinary revalidation routines.
1007 * We may need to check the cached dentry for staleness.
1008 */
4ac91378
JB
1009 if (nd->path.dentry && nd->path.dentry->d_sb &&
1010 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
1da177e4
LT
1011 err = -ESTALE;
1012 /* Note: we do not d_invalidate() */
4ac91378
JB
1013 if (!nd->path.dentry->d_op->d_revalidate(
1014 nd->path.dentry, nd))
1da177e4
LT
1015 break;
1016 }
1017return_base:
1018 return 0;
1019out_dput:
1d957f9b 1020 path_put_conditional(&next, nd);
1da177e4
LT
1021 break;
1022 }
1d957f9b 1023 path_put(&nd->path);
1da177e4
LT
1024return_err:
1025 return err;
1026}
1027
fc9b52cd 1028static int path_walk(const char *name, struct nameidata *nd)
1da177e4
LT
1029{
1030 current->total_link_count = 0;
1031 return link_path_walk(name, nd);
1032}
1033
ea3834d9 1034/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
fc9b52cd 1035static int do_path_lookup(int dfd, const char *name,
5590ff0d 1036 unsigned int flags, struct nameidata *nd)
1da177e4 1037{
ea3834d9 1038 int retval = 0;
170aa3d0
UD
1039 int fput_needed;
1040 struct file *file;
e518ddb7 1041 struct fs_struct *fs = current->fs;
1da177e4
LT
1042
1043 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1044 nd->flags = flags;
1045 nd->depth = 0;
1046
1da177e4 1047 if (*name=='/') {
e518ddb7 1048 read_lock(&fs->lock);
6ac08c39
JB
1049 nd->path = fs->root;
1050 path_get(&fs->root);
e518ddb7 1051 read_unlock(&fs->lock);
5590ff0d 1052 } else if (dfd == AT_FDCWD) {
e518ddb7 1053 read_lock(&fs->lock);
6ac08c39
JB
1054 nd->path = fs->pwd;
1055 path_get(&fs->pwd);
e518ddb7 1056 read_unlock(&fs->lock);
5590ff0d 1057 } else {
5590ff0d
UD
1058 struct dentry *dentry;
1059
1060 file = fget_light(dfd, &fput_needed);
170aa3d0
UD
1061 retval = -EBADF;
1062 if (!file)
6d09bb62 1063 goto out_fail;
5590ff0d 1064
0f7fc9e4 1065 dentry = file->f_path.dentry;
5590ff0d 1066
170aa3d0
UD
1067 retval = -ENOTDIR;
1068 if (!S_ISDIR(dentry->d_inode->i_mode))
6d09bb62 1069 goto fput_fail;
5590ff0d
UD
1070
1071 retval = file_permission(file, MAY_EXEC);
170aa3d0 1072 if (retval)
6d09bb62 1073 goto fput_fail;
5590ff0d 1074
5dd784d0
JB
1075 nd->path = file->f_path;
1076 path_get(&file->f_path);
5590ff0d
UD
1077
1078 fput_light(file, fput_needed);
1da177e4 1079 }
2dfdd266
JJS
1080
1081 retval = path_walk(name, nd);
4ac91378
JB
1082 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1083 nd->path.dentry->d_inode))
1084 audit_inode(name, nd->path.dentry);
6d09bb62 1085out_fail:
170aa3d0
UD
1086 return retval;
1087
6d09bb62 1088fput_fail:
170aa3d0 1089 fput_light(file, fput_needed);
6d09bb62 1090 goto out_fail;
1da177e4
LT
1091}
1092
fc9b52cd 1093int path_lookup(const char *name, unsigned int flags,
5590ff0d
UD
1094 struct nameidata *nd)
1095{
1096 return do_path_lookup(AT_FDCWD, name, flags, nd);
1097}
1098
d1811465
AV
1099int kern_path(const char *name, unsigned int flags, struct path *path)
1100{
1101 struct nameidata nd;
1102 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1103 if (!res)
1104 *path = nd.path;
1105 return res;
1106}
1107
16f18200
JJS
1108/**
1109 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1110 * @dentry: pointer to dentry of the base directory
1111 * @mnt: pointer to vfs mount of the base directory
1112 * @name: pointer to file name
1113 * @flags: lookup flags
1114 * @nd: pointer to nameidata
1115 */
1116int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1117 const char *name, unsigned int flags,
1118 struct nameidata *nd)
1119{
1120 int retval;
1121
1122 /* same as do_path_lookup */
1123 nd->last_type = LAST_ROOT;
1124 nd->flags = flags;
1125 nd->depth = 0;
1126
c8e7f449
JB
1127 nd->path.dentry = dentry;
1128 nd->path.mnt = mnt;
1129 path_get(&nd->path);
16f18200
JJS
1130
1131 retval = path_walk(name, nd);
4ac91378
JB
1132 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1133 nd->path.dentry->d_inode))
1134 audit_inode(name, nd->path.dentry);
16f18200
JJS
1135
1136 return retval;
1137
1138}
1139
8737f3a1
AV
1140/**
1141 * path_lookup_open - lookup a file path with open intent
1142 * @dfd: the directory to use as base, or AT_FDCWD
1143 * @name: pointer to file name
1144 * @lookup_flags: lookup intent flags
1145 * @nd: pointer to nameidata
1146 * @open_flags: open intent flags
1147 */
1148int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1149 struct nameidata *nd, int open_flags)
834f2a4a
TM
1150{
1151 struct file *filp = get_empty_filp();
1152 int err;
1153
1154 if (filp == NULL)
1155 return -ENFILE;
1156 nd->intent.open.file = filp;
1157 nd->intent.open.flags = open_flags;
8737f3a1 1158 nd->intent.open.create_mode = 0;
5590ff0d 1159 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
834f2a4a
TM
1160 if (IS_ERR(nd->intent.open.file)) {
1161 if (err == 0) {
1162 err = PTR_ERR(nd->intent.open.file);
1d957f9b 1163 path_put(&nd->path);
834f2a4a
TM
1164 }
1165 } else if (err != 0)
1166 release_open_intent(nd);
1167 return err;
1168}
1169
eead1911
CH
1170static struct dentry *__lookup_hash(struct qstr *name,
1171 struct dentry *base, struct nameidata *nd)
1da177e4 1172{
057f6c01 1173 struct dentry *dentry;
1da177e4
LT
1174 struct inode *inode;
1175 int err;
1176
1177 inode = base->d_inode;
1da177e4
LT
1178
1179 /*
1180 * See if the low-level filesystem might want
1181 * to use its own hash..
1182 */
1183 if (base->d_op && base->d_op->d_hash) {
1184 err = base->d_op->d_hash(base, name);
1185 dentry = ERR_PTR(err);
1186 if (err < 0)
1187 goto out;
1188 }
1189
1190 dentry = cached_lookup(base, name, nd);
1191 if (!dentry) {
d70b67c8
MS
1192 struct dentry *new;
1193
1194 /* Don't create child dentry for a dead directory. */
1195 dentry = ERR_PTR(-ENOENT);
1196 if (IS_DEADDIR(inode))
1197 goto out;
1198
1199 new = d_alloc(base, name);
1da177e4
LT
1200 dentry = ERR_PTR(-ENOMEM);
1201 if (!new)
1202 goto out;
1203 dentry = inode->i_op->lookup(inode, new, nd);
1204 if (!dentry)
1205 dentry = new;
1206 else
1207 dput(new);
1208 }
1209out:
1210 return dentry;
1211}
1212
057f6c01
JM
1213/*
1214 * Restricted form of lookup. Doesn't follow links, single-component only,
1215 * needs parent already locked. Doesn't follow mounts.
1216 * SMP-safe.
1217 */
eead1911 1218static struct dentry *lookup_hash(struct nameidata *nd)
057f6c01 1219{
057f6c01
JM
1220 int err;
1221
f419a2e3 1222 err = inode_permission(nd->path.dentry->d_inode, MAY_EXEC);
057f6c01 1223 if (err)
eead1911 1224 return ERR_PTR(err);
4ac91378 1225 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1da177e4
LT
1226}
1227
eead1911
CH
1228static int __lookup_one_len(const char *name, struct qstr *this,
1229 struct dentry *base, int len)
1da177e4
LT
1230{
1231 unsigned long hash;
1da177e4
LT
1232 unsigned int c;
1233
057f6c01
JM
1234 this->name = name;
1235 this->len = len;
1da177e4 1236 if (!len)
057f6c01 1237 return -EACCES;
1da177e4
LT
1238
1239 hash = init_name_hash();
1240 while (len--) {
1241 c = *(const unsigned char *)name++;
1242 if (c == '/' || c == '\0')
057f6c01 1243 return -EACCES;
1da177e4
LT
1244 hash = partial_name_hash(c, hash);
1245 }
057f6c01
JM
1246 this->hash = end_name_hash(hash);
1247 return 0;
1248}
1da177e4 1249
eead1911 1250/**
a6b91919 1251 * lookup_one_len - filesystem helper to lookup single pathname component
eead1911
CH
1252 * @name: pathname component to lookup
1253 * @base: base directory to lookup from
1254 * @len: maximum length @len should be interpreted to
1255 *
a6b91919
RD
1256 * Note that this routine is purely a helper for filesystem usage and should
1257 * not be called by generic code. Also note that by using this function the
eead1911
CH
1258 * nameidata argument is passed to the filesystem methods and a filesystem
1259 * using this helper needs to be prepared for that.
1260 */
057f6c01
JM
1261struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1262{
1263 int err;
1264 struct qstr this;
1265
1266 err = __lookup_one_len(name, &this, base, len);
eead1911
CH
1267 if (err)
1268 return ERR_PTR(err);
1269
f419a2e3 1270 err = inode_permission(base->d_inode, MAY_EXEC);
057f6c01
JM
1271 if (err)
1272 return ERR_PTR(err);
49705b77 1273 return __lookup_hash(&this, base, NULL);
057f6c01
JM
1274}
1275
eead1911
CH
1276/**
1277 * lookup_one_noperm - bad hack for sysfs
1278 * @name: pathname component to lookup
1279 * @base: base directory to lookup from
1280 *
1281 * This is a variant of lookup_one_len that doesn't perform any permission
1282 * checks. It's a horrible hack to work around the braindead sysfs
1283 * architecture and should not be used anywhere else.
1284 *
1285 * DON'T USE THIS FUNCTION EVER, thanks.
1286 */
1287struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
057f6c01
JM
1288{
1289 int err;
1290 struct qstr this;
1291
eead1911 1292 err = __lookup_one_len(name, &this, base, strlen(name));
057f6c01
JM
1293 if (err)
1294 return ERR_PTR(err);
eead1911 1295 return __lookup_hash(&this, base, NULL);
1da177e4
LT
1296}
1297
2d8f3038
AV
1298int user_path_at(int dfd, const char __user *name, unsigned flags,
1299 struct path *path)
1da177e4 1300{
2d8f3038 1301 struct nameidata nd;
1da177e4
LT
1302 char *tmp = getname(name);
1303 int err = PTR_ERR(tmp);
1da177e4 1304 if (!IS_ERR(tmp)) {
2d8f3038
AV
1305
1306 BUG_ON(flags & LOOKUP_PARENT);
1307
1308 err = do_path_lookup(dfd, tmp, flags, &nd);
1da177e4 1309 putname(tmp);
2d8f3038
AV
1310 if (!err)
1311 *path = nd.path;
1da177e4
LT
1312 }
1313 return err;
1314}
1315
2ad94ae6
AV
1316static int user_path_parent(int dfd, const char __user *path,
1317 struct nameidata *nd, char **name)
1318{
1319 char *s = getname(path);
1320 int error;
1321
1322 if (IS_ERR(s))
1323 return PTR_ERR(s);
1324
1325 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1326 if (error)
1327 putname(s);
1328 else
1329 *name = s;
1330
1331 return error;
1332}
1333
1da177e4
LT
1334/*
1335 * It's inline, so penalty for filesystems that don't use sticky bit is
1336 * minimal.
1337 */
1338static inline int check_sticky(struct inode *dir, struct inode *inode)
1339{
da9592ed
DH
1340 uid_t fsuid = current_fsuid();
1341
1da177e4
LT
1342 if (!(dir->i_mode & S_ISVTX))
1343 return 0;
da9592ed 1344 if (inode->i_uid == fsuid)
1da177e4 1345 return 0;
da9592ed 1346 if (dir->i_uid == fsuid)
1da177e4
LT
1347 return 0;
1348 return !capable(CAP_FOWNER);
1349}
1350
1351/*
1352 * Check whether we can remove a link victim from directory dir, check
1353 * whether the type of victim is right.
1354 * 1. We can't do it if dir is read-only (done in permission())
1355 * 2. We should have write and exec permissions on dir
1356 * 3. We can't remove anything from append-only dir
1357 * 4. We can't do anything with immutable dir (done in permission())
1358 * 5. If the sticky bit on dir is set we should either
1359 * a. be owner of dir, or
1360 * b. be owner of victim, or
1361 * c. have CAP_FOWNER capability
1362 * 6. If the victim is append-only or immutable we can't do antyhing with
1363 * links pointing to it.
1364 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1365 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1366 * 9. We can't remove a root or mountpoint.
1367 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1368 * nfs_async_unlink().
1369 */
858119e1 1370static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1da177e4
LT
1371{
1372 int error;
1373
1374 if (!victim->d_inode)
1375 return -ENOENT;
1376
1377 BUG_ON(victim->d_parent->d_inode != dir);
5a190ae6 1378 audit_inode_child(victim->d_name.name, victim, dir);
1da177e4 1379
f419a2e3 1380 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
1381 if (error)
1382 return error;
1383 if (IS_APPEND(dir))
1384 return -EPERM;
1385 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
f9454548 1386 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1da177e4
LT
1387 return -EPERM;
1388 if (isdir) {
1389 if (!S_ISDIR(victim->d_inode->i_mode))
1390 return -ENOTDIR;
1391 if (IS_ROOT(victim))
1392 return -EBUSY;
1393 } else if (S_ISDIR(victim->d_inode->i_mode))
1394 return -EISDIR;
1395 if (IS_DEADDIR(dir))
1396 return -ENOENT;
1397 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1398 return -EBUSY;
1399 return 0;
1400}
1401
1402/* Check whether we can create an object with dentry child in directory
1403 * dir.
1404 * 1. We can't do it if child already exists (open has special treatment for
1405 * this case, but since we are inlined it's OK)
1406 * 2. We can't do it if dir is read-only (done in permission())
1407 * 3. We should have write and exec permissions on dir
1408 * 4. We can't do it if dir is immutable (done in permission())
1409 */
a95164d9 1410static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4
LT
1411{
1412 if (child->d_inode)
1413 return -EEXIST;
1414 if (IS_DEADDIR(dir))
1415 return -ENOENT;
f419a2e3 1416 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
1417}
1418
1419/*
1da177e4
LT
1420 * O_DIRECTORY translates into forcing a directory lookup.
1421 */
1422static inline int lookup_flags(unsigned int f)
1423{
1424 unsigned long retval = LOOKUP_FOLLOW;
1425
1426 if (f & O_NOFOLLOW)
1427 retval &= ~LOOKUP_FOLLOW;
1428
1da177e4
LT
1429 if (f & O_DIRECTORY)
1430 retval |= LOOKUP_DIRECTORY;
1431
1432 return retval;
1433}
1434
1435/*
1436 * p1 and p2 should be directories on the same fs.
1437 */
1438struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1439{
1440 struct dentry *p;
1441
1442 if (p1 == p2) {
f2eace23 1443 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4
LT
1444 return NULL;
1445 }
1446
a11f3a05 1447 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4 1448
e2761a11
OH
1449 p = d_ancestor(p2, p1);
1450 if (p) {
1451 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1452 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1453 return p;
1da177e4
LT
1454 }
1455
e2761a11
OH
1456 p = d_ancestor(p1, p2);
1457 if (p) {
1458 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1459 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1460 return p;
1da177e4
LT
1461 }
1462
f2eace23
IM
1463 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1464 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1da177e4
LT
1465 return NULL;
1466}
1467
1468void unlock_rename(struct dentry *p1, struct dentry *p2)
1469{
1b1dcc1b 1470 mutex_unlock(&p1->d_inode->i_mutex);
1da177e4 1471 if (p1 != p2) {
1b1dcc1b 1472 mutex_unlock(&p2->d_inode->i_mutex);
a11f3a05 1473 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4
LT
1474 }
1475}
1476
1477int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1478 struct nameidata *nd)
1479{
a95164d9 1480 int error = may_create(dir, dentry);
1da177e4
LT
1481
1482 if (error)
1483 return error;
1484
1485 if (!dir->i_op || !dir->i_op->create)
1486 return -EACCES; /* shouldn't it be ENOSYS? */
1487 mode &= S_IALLUGO;
1488 mode |= S_IFREG;
1489 error = security_inode_create(dir, dentry, mode);
1490 if (error)
1491 return error;
1492 DQUOT_INIT(dir);
1493 error = dir->i_op->create(dir, dentry, mode, nd);
a74574aa 1494 if (!error)
f38aa942 1495 fsnotify_create(dir, dentry);
1da177e4
LT
1496 return error;
1497}
1498
1499int may_open(struct nameidata *nd, int acc_mode, int flag)
1500{
4ac91378 1501 struct dentry *dentry = nd->path.dentry;
1da177e4
LT
1502 struct inode *inode = dentry->d_inode;
1503 int error;
1504
1505 if (!inode)
1506 return -ENOENT;
1507
1508 if (S_ISLNK(inode->i_mode))
1509 return -ELOOP;
1510
974a9f0b 1511 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
1da177e4
LT
1512 return -EISDIR;
1513
1da177e4
LT
1514 /*
1515 * FIFO's, sockets and device files are special: they don't
1516 * actually live on the filesystem itself, and as such you
1517 * can write to them even if the filesystem is read-only.
1518 */
1519 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1520 flag &= ~O_TRUNC;
1521 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
4ac91378 1522 if (nd->path.mnt->mnt_flags & MNT_NODEV)
1da177e4
LT
1523 return -EACCES;
1524
1525 flag &= ~O_TRUNC;
4a3fd211 1526 }
b41572e9
DH
1527
1528 error = vfs_permission(nd, acc_mode);
1529 if (error)
1530 return error;
6146f0d5
MZ
1531
1532 error = ima_path_check(&nd->path,
1533 acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC));
1534 if (error)
1535 return error;
1da177e4
LT
1536 /*
1537 * An append-only file must be opened in append mode for writing.
1538 */
1539 if (IS_APPEND(inode)) {
1540 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1541 return -EPERM;
1542 if (flag & O_TRUNC)
1543 return -EPERM;
1544 }
1545
1546 /* O_NOATIME can only be set by the owner or superuser */
1547 if (flag & O_NOATIME)
3bd858ab 1548 if (!is_owner_or_cap(inode))
1da177e4
LT
1549 return -EPERM;
1550
1551 /*
1552 * Ensure there are no outstanding leases on the file.
1553 */
1554 error = break_lease(inode, flag);
1555 if (error)
1556 return error;
1557
1558 if (flag & O_TRUNC) {
1559 error = get_write_access(inode);
1560 if (error)
1561 return error;
1562
1563 /*
1564 * Refuse to truncate files with mandatory locks held on them.
1565 */
1566 error = locks_verify_locked(inode);
1567 if (!error) {
1568 DQUOT_INIT(inode);
d139d7ff
MS
1569
1570 error = do_truncate(dentry, 0,
1571 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1572 NULL);
1da177e4
LT
1573 }
1574 put_write_access(inode);
1575 if (error)
1576 return error;
1577 } else
1578 if (flag & FMODE_WRITE)
1579 DQUOT_INIT(inode);
1580
1581 return 0;
1582}
1583
d57999e1
DH
1584/*
1585 * Be careful about ever adding any more callers of this
1586 * function. Its flags must be in the namei format, not
1587 * what get passed to sys_open().
1588 */
1589static int __open_namei_create(struct nameidata *nd, struct path *path,
aab520e2
DH
1590 int flag, int mode)
1591{
1592 int error;
4ac91378 1593 struct dentry *dir = nd->path.dentry;
aab520e2
DH
1594
1595 if (!IS_POSIXACL(dir->d_inode))
1596 mode &= ~current->fs->umask;
1597 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1598 mutex_unlock(&dir->d_inode->i_mutex);
4ac91378
JB
1599 dput(nd->path.dentry);
1600 nd->path.dentry = path->dentry;
aab520e2
DH
1601 if (error)
1602 return error;
1603 /* Don't check for write permission, don't truncate */
1604 return may_open(nd, 0, flag & ~O_TRUNC);
1605}
1606
d57999e1
DH
1607/*
1608 * Note that while the flag value (low two bits) for sys_open means:
1609 * 00 - read-only
1610 * 01 - write-only
1611 * 10 - read-write
1612 * 11 - special
1613 * it is changed into
1614 * 00 - no permissions needed
1615 * 01 - read-permission
1616 * 10 - write-permission
1617 * 11 - read-write
1618 * for the internal routines (ie open_namei()/follow_link() etc)
1619 * This is more logical, and also allows the 00 "no perm needed"
1620 * to be used for symlinks (where the permissions are checked
1621 * later).
1622 *
1623*/
1624static inline int open_to_namei_flags(int flag)
1625{
1626 if ((flag+1) & O_ACCMODE)
1627 flag++;
1628 return flag;
1629}
1630
4a3fd211
DH
1631static int open_will_write_to_fs(int flag, struct inode *inode)
1632{
1633 /*
1634 * We'll never write to the fs underlying
1635 * a device file.
1636 */
1637 if (special_file(inode->i_mode))
1638 return 0;
1639 return (flag & O_TRUNC);
1640}
1641
1da177e4 1642/*
4a3fd211
DH
1643 * Note that the low bits of the passed in "open_flag"
1644 * are not the same as in the local variable "flag". See
1645 * open_to_namei_flags() for more details.
1da177e4 1646 */
a70e65df
CH
1647struct file *do_filp_open(int dfd, const char *pathname,
1648 int open_flag, int mode)
1da177e4 1649{
4a3fd211 1650 struct file *filp;
a70e65df 1651 struct nameidata nd;
834f2a4a 1652 int acc_mode, error;
4e7506e4 1653 struct path path;
1da177e4
LT
1654 struct dentry *dir;
1655 int count = 0;
4a3fd211 1656 int will_write;
d57999e1 1657 int flag = open_to_namei_flags(open_flag);
1da177e4 1658
b77b0646 1659 acc_mode = MAY_OPEN | ACC_MODE(flag);
1da177e4 1660
834f2a4a
TM
1661 /* O_TRUNC implies we need access checks for write permissions */
1662 if (flag & O_TRUNC)
1663 acc_mode |= MAY_WRITE;
1664
1da177e4
LT
1665 /* Allow the LSM permission hook to distinguish append
1666 access from general write access. */
1667 if (flag & O_APPEND)
1668 acc_mode |= MAY_APPEND;
1669
1da177e4
LT
1670 /*
1671 * The simplest case - just a plain lookup.
1672 */
1673 if (!(flag & O_CREAT)) {
5590ff0d 1674 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
a70e65df 1675 &nd, flag);
1da177e4 1676 if (error)
a70e65df 1677 return ERR_PTR(error);
1da177e4
LT
1678 goto ok;
1679 }
1680
1681 /*
1682 * Create - we need to know the parent.
1683 */
8737f3a1 1684 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
1da177e4 1685 if (error)
a70e65df 1686 return ERR_PTR(error);
1da177e4
LT
1687
1688 /*
1689 * We have the parent and last component. First of all, check
1690 * that we are not asked to creat(2) an obvious directory - that
1691 * will not do.
1692 */
1693 error = -EISDIR;
a70e65df 1694 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
8737f3a1 1695 goto exit_parent;
1da177e4 1696
8737f3a1
AV
1697 error = -ENFILE;
1698 filp = get_empty_filp();
1699 if (filp == NULL)
1700 goto exit_parent;
1701 nd.intent.open.file = filp;
1702 nd.intent.open.flags = flag;
1703 nd.intent.open.create_mode = mode;
a70e65df
CH
1704 dir = nd.path.dentry;
1705 nd.flags &= ~LOOKUP_PARENT;
8737f3a1 1706 nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
3516586a
AV
1707 if (flag & O_EXCL)
1708 nd.flags |= LOOKUP_EXCL;
1b1dcc1b 1709 mutex_lock(&dir->d_inode->i_mutex);
a70e65df
CH
1710 path.dentry = lookup_hash(&nd);
1711 path.mnt = nd.path.mnt;
1da177e4
LT
1712
1713do_last:
4e7506e4
AV
1714 error = PTR_ERR(path.dentry);
1715 if (IS_ERR(path.dentry)) {
1b1dcc1b 1716 mutex_unlock(&dir->d_inode->i_mutex);
1da177e4
LT
1717 goto exit;
1718 }
1719
a70e65df 1720 if (IS_ERR(nd.intent.open.file)) {
a70e65df 1721 error = PTR_ERR(nd.intent.open.file);
4a3fd211 1722 goto exit_mutex_unlock;
4af4c52f
OD
1723 }
1724
1da177e4 1725 /* Negative dentry, just create the file */
4e7506e4 1726 if (!path.dentry->d_inode) {
4a3fd211
DH
1727 /*
1728 * This write is needed to ensure that a
1729 * ro->rw transition does not occur between
1730 * the time when the file is created and when
1731 * a permanent write count is taken through
1732 * the 'struct file' in nameidata_to_filp().
1733 */
1734 error = mnt_want_write(nd.path.mnt);
1da177e4 1735 if (error)
4a3fd211
DH
1736 goto exit_mutex_unlock;
1737 error = __open_namei_create(&nd, &path, flag, mode);
1738 if (error) {
1739 mnt_drop_write(nd.path.mnt);
1da177e4 1740 goto exit;
4a3fd211
DH
1741 }
1742 filp = nameidata_to_filp(&nd, open_flag);
1743 mnt_drop_write(nd.path.mnt);
1744 return filp;
1da177e4
LT
1745 }
1746
1747 /*
1748 * It already exists.
1749 */
1b1dcc1b 1750 mutex_unlock(&dir->d_inode->i_mutex);
5a190ae6 1751 audit_inode(pathname, path.dentry);
1da177e4
LT
1752
1753 error = -EEXIST;
1754 if (flag & O_EXCL)
1755 goto exit_dput;
1756
e13b210f 1757 if (__follow_mount(&path)) {
1da177e4 1758 error = -ELOOP;
ba7a4c1a
AV
1759 if (flag & O_NOFOLLOW)
1760 goto exit_dput;
1da177e4 1761 }
3e2efce0 1762
1da177e4 1763 error = -ENOENT;
4e7506e4 1764 if (!path.dentry->d_inode)
1da177e4 1765 goto exit_dput;
4e7506e4 1766 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1da177e4
LT
1767 goto do_link;
1768
a70e65df 1769 path_to_nameidata(&path, &nd);
1da177e4 1770 error = -EISDIR;
4e7506e4 1771 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1da177e4
LT
1772 goto exit;
1773ok:
4a3fd211
DH
1774 /*
1775 * Consider:
1776 * 1. may_open() truncates a file
1777 * 2. a rw->ro mount transition occurs
1778 * 3. nameidata_to_filp() fails due to
1779 * the ro mount.
1780 * That would be inconsistent, and should
1781 * be avoided. Taking this mnt write here
1782 * ensures that (2) can not occur.
1783 */
1784 will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
1785 if (will_write) {
1786 error = mnt_want_write(nd.path.mnt);
1787 if (error)
1788 goto exit;
1789 }
a70e65df 1790 error = may_open(&nd, acc_mode, flag);
4a3fd211
DH
1791 if (error) {
1792 if (will_write)
1793 mnt_drop_write(nd.path.mnt);
1da177e4 1794 goto exit;
4a3fd211
DH
1795 }
1796 filp = nameidata_to_filp(&nd, open_flag);
1797 /*
1798 * It is now safe to drop the mnt write
1799 * because the filp has had a write taken
1800 * on its behalf.
1801 */
1802 if (will_write)
1803 mnt_drop_write(nd.path.mnt);
1804 return filp;
1da177e4 1805
4a3fd211
DH
1806exit_mutex_unlock:
1807 mutex_unlock(&dir->d_inode->i_mutex);
1da177e4 1808exit_dput:
a70e65df 1809 path_put_conditional(&path, &nd);
1da177e4 1810exit:
a70e65df
CH
1811 if (!IS_ERR(nd.intent.open.file))
1812 release_open_intent(&nd);
8737f3a1 1813exit_parent:
a70e65df
CH
1814 path_put(&nd.path);
1815 return ERR_PTR(error);
1da177e4
LT
1816
1817do_link:
1818 error = -ELOOP;
1819 if (flag & O_NOFOLLOW)
1820 goto exit_dput;
1821 /*
1822 * This is subtle. Instead of calling do_follow_link() we do the
1823 * thing by hands. The reason is that this way we have zero link_count
1824 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1825 * After that we have the parent and last component, i.e.
1826 * we are in the same situation as after the first path_walk().
1827 * Well, almost - if the last component is normal we get its copy
1828 * stored in nd->last.name and we will have to putname() it when we
1829 * are done. Procfs-like symlinks just set LAST_BIND.
1830 */
a70e65df
CH
1831 nd.flags |= LOOKUP_PARENT;
1832 error = security_inode_follow_link(path.dentry, &nd);
1da177e4
LT
1833 if (error)
1834 goto exit_dput;
a70e65df 1835 error = __do_follow_link(&path, &nd);
de459215
KK
1836 if (error) {
1837 /* Does someone understand code flow here? Or it is only
1838 * me so stupid? Anathema to whoever designed this non-sense
1839 * with "intent.open".
1840 */
a70e65df
CH
1841 release_open_intent(&nd);
1842 return ERR_PTR(error);
de459215 1843 }
a70e65df
CH
1844 nd.flags &= ~LOOKUP_PARENT;
1845 if (nd.last_type == LAST_BIND)
1da177e4 1846 goto ok;
1da177e4 1847 error = -EISDIR;
a70e65df 1848 if (nd.last_type != LAST_NORM)
1da177e4 1849 goto exit;
a70e65df
CH
1850 if (nd.last.name[nd.last.len]) {
1851 __putname(nd.last.name);
1da177e4
LT
1852 goto exit;
1853 }
1854 error = -ELOOP;
1855 if (count++==32) {
a70e65df 1856 __putname(nd.last.name);
1da177e4
LT
1857 goto exit;
1858 }
a70e65df 1859 dir = nd.path.dentry;
1b1dcc1b 1860 mutex_lock(&dir->d_inode->i_mutex);
a70e65df
CH
1861 path.dentry = lookup_hash(&nd);
1862 path.mnt = nd.path.mnt;
1863 __putname(nd.last.name);
1da177e4
LT
1864 goto do_last;
1865}
1866
a70e65df
CH
1867/**
1868 * filp_open - open file and return file pointer
1869 *
1870 * @filename: path to open
1871 * @flags: open flags as per the open(2) second argument
1872 * @mode: mode for the new file if O_CREAT is set, else ignored
1873 *
1874 * This is the helper to open a file from kernelspace if you really
1875 * have to. But in generally you should not do this, so please move
1876 * along, nothing to see here..
1877 */
1878struct file *filp_open(const char *filename, int flags, int mode)
1879{
1880 return do_filp_open(AT_FDCWD, filename, flags, mode);
1881}
1882EXPORT_SYMBOL(filp_open);
1883
1da177e4
LT
1884/**
1885 * lookup_create - lookup a dentry, creating it if it doesn't exist
1886 * @nd: nameidata info
1887 * @is_dir: directory flag
1888 *
1889 * Simple function to lookup and return a dentry and create it
1890 * if it doesn't exist. Is SMP-safe.
c663e5d8 1891 *
4ac91378 1892 * Returns with nd->path.dentry->d_inode->i_mutex locked.
1da177e4
LT
1893 */
1894struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1895{
c663e5d8 1896 struct dentry *dentry = ERR_PTR(-EEXIST);
1da177e4 1897
4ac91378 1898 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
c663e5d8
CH
1899 /*
1900 * Yucky last component or no last component at all?
1901 * (foo/., foo/.., /////)
1902 */
1da177e4
LT
1903 if (nd->last_type != LAST_NORM)
1904 goto fail;
1905 nd->flags &= ~LOOKUP_PARENT;
3516586a 1906 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
a634904a 1907 nd->intent.open.flags = O_EXCL;
c663e5d8
CH
1908
1909 /*
1910 * Do the final lookup.
1911 */
49705b77 1912 dentry = lookup_hash(nd);
1da177e4
LT
1913 if (IS_ERR(dentry))
1914 goto fail;
c663e5d8 1915
e9baf6e5
AV
1916 if (dentry->d_inode)
1917 goto eexist;
c663e5d8
CH
1918 /*
1919 * Special case - lookup gave negative, but... we had foo/bar/
1920 * From the vfs_mknod() POV we just have a negative dentry -
1921 * all is fine. Let's be bastards - you had / on the end, you've
1922 * been asking for (non-existent) directory. -ENOENT for you.
1923 */
e9baf6e5
AV
1924 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
1925 dput(dentry);
1926 dentry = ERR_PTR(-ENOENT);
1927 }
1da177e4 1928 return dentry;
e9baf6e5 1929eexist:
1da177e4 1930 dput(dentry);
e9baf6e5 1931 dentry = ERR_PTR(-EEXIST);
1da177e4
LT
1932fail:
1933 return dentry;
1934}
f81a0bff 1935EXPORT_SYMBOL_GPL(lookup_create);
1da177e4
LT
1936
1937int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1938{
a95164d9 1939 int error = may_create(dir, dentry);
1da177e4
LT
1940
1941 if (error)
1942 return error;
1943
1944 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1945 return -EPERM;
1946
1947 if (!dir->i_op || !dir->i_op->mknod)
1948 return -EPERM;
1949
08ce5f16
SH
1950 error = devcgroup_inode_mknod(mode, dev);
1951 if (error)
1952 return error;
1953
1da177e4
LT
1954 error = security_inode_mknod(dir, dentry, mode, dev);
1955 if (error)
1956 return error;
1957
1958 DQUOT_INIT(dir);
1959 error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aa 1960 if (!error)
f38aa942 1961 fsnotify_create(dir, dentry);
1da177e4
LT
1962 return error;
1963}
1964
463c3197
DH
1965static int may_mknod(mode_t mode)
1966{
1967 switch (mode & S_IFMT) {
1968 case S_IFREG:
1969 case S_IFCHR:
1970 case S_IFBLK:
1971 case S_IFIFO:
1972 case S_IFSOCK:
1973 case 0: /* zero mode translates to S_IFREG */
1974 return 0;
1975 case S_IFDIR:
1976 return -EPERM;
1977 default:
1978 return -EINVAL;
1979 }
1980}
1981
5590ff0d
UD
1982asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1983 unsigned dev)
1da177e4 1984{
2ad94ae6
AV
1985 int error;
1986 char *tmp;
1987 struct dentry *dentry;
1da177e4
LT
1988 struct nameidata nd;
1989
1990 if (S_ISDIR(mode))
1991 return -EPERM;
1da177e4 1992
2ad94ae6 1993 error = user_path_parent(dfd, filename, &nd, &tmp);
1da177e4 1994 if (error)
2ad94ae6
AV
1995 return error;
1996
1da177e4 1997 dentry = lookup_create(&nd, 0);
463c3197
DH
1998 if (IS_ERR(dentry)) {
1999 error = PTR_ERR(dentry);
2000 goto out_unlock;
2001 }
4ac91378 2002 if (!IS_POSIXACL(nd.path.dentry->d_inode))
1da177e4 2003 mode &= ~current->fs->umask;
463c3197
DH
2004 error = may_mknod(mode);
2005 if (error)
2006 goto out_dput;
2007 error = mnt_want_write(nd.path.mnt);
2008 if (error)
2009 goto out_dput;
2010 switch (mode & S_IFMT) {
1da177e4 2011 case 0: case S_IFREG:
4ac91378 2012 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
1da177e4
LT
2013 break;
2014 case S_IFCHR: case S_IFBLK:
4ac91378 2015 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
1da177e4
LT
2016 new_decode_dev(dev));
2017 break;
2018 case S_IFIFO: case S_IFSOCK:
4ac91378 2019 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
1da177e4 2020 break;
1da177e4 2021 }
463c3197
DH
2022 mnt_drop_write(nd.path.mnt);
2023out_dput:
2024 dput(dentry);
2025out_unlock:
4ac91378 2026 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1d957f9b 2027 path_put(&nd.path);
1da177e4
LT
2028 putname(tmp);
2029
2030 return error;
2031}
2032
5590ff0d
UD
2033asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
2034{
2035 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2036}
2037
1da177e4
LT
2038int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2039{
a95164d9 2040 int error = may_create(dir, dentry);
1da177e4
LT
2041
2042 if (error)
2043 return error;
2044
2045 if (!dir->i_op || !dir->i_op->mkdir)
2046 return -EPERM;
2047
2048 mode &= (S_IRWXUGO|S_ISVTX);
2049 error = security_inode_mkdir(dir, dentry, mode);
2050 if (error)
2051 return error;
2052
2053 DQUOT_INIT(dir);
2054 error = dir->i_op->mkdir(dir, dentry, mode);
a74574aa 2055 if (!error)
f38aa942 2056 fsnotify_mkdir(dir, dentry);
1da177e4
LT
2057 return error;
2058}
2059
5590ff0d 2060asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
1da177e4
LT
2061{
2062 int error = 0;
2063 char * tmp;
6902d925
DH
2064 struct dentry *dentry;
2065 struct nameidata nd;
1da177e4 2066
2ad94ae6
AV
2067 error = user_path_parent(dfd, pathname, &nd, &tmp);
2068 if (error)
6902d925 2069 goto out_err;
1da177e4 2070
6902d925
DH
2071 dentry = lookup_create(&nd, 1);
2072 error = PTR_ERR(dentry);
2073 if (IS_ERR(dentry))
2074 goto out_unlock;
1da177e4 2075
4ac91378 2076 if (!IS_POSIXACL(nd.path.dentry->d_inode))
6902d925 2077 mode &= ~current->fs->umask;
463c3197
DH
2078 error = mnt_want_write(nd.path.mnt);
2079 if (error)
2080 goto out_dput;
4ac91378 2081 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
463c3197
DH
2082 mnt_drop_write(nd.path.mnt);
2083out_dput:
6902d925
DH
2084 dput(dentry);
2085out_unlock:
4ac91378 2086 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1d957f9b 2087 path_put(&nd.path);
6902d925
DH
2088 putname(tmp);
2089out_err:
1da177e4
LT
2090 return error;
2091}
2092
5590ff0d
UD
2093asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2094{
2095 return sys_mkdirat(AT_FDCWD, pathname, mode);
2096}
2097
1da177e4
LT
2098/*
2099 * We try to drop the dentry early: we should have
2100 * a usage count of 2 if we're the only user of this
2101 * dentry, and if that is true (possibly after pruning
2102 * the dcache), then we drop the dentry now.
2103 *
2104 * A low-level filesystem can, if it choses, legally
2105 * do a
2106 *
2107 * if (!d_unhashed(dentry))
2108 * return -EBUSY;
2109 *
2110 * if it cannot handle the case of removing a directory
2111 * that is still in use by something else..
2112 */
2113void dentry_unhash(struct dentry *dentry)
2114{
2115 dget(dentry);
dc168427 2116 shrink_dcache_parent(dentry);
1da177e4
LT
2117 spin_lock(&dcache_lock);
2118 spin_lock(&dentry->d_lock);
2119 if (atomic_read(&dentry->d_count) == 2)
2120 __d_drop(dentry);
2121 spin_unlock(&dentry->d_lock);
2122 spin_unlock(&dcache_lock);
2123}
2124
2125int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2126{
2127 int error = may_delete(dir, dentry, 1);
2128
2129 if (error)
2130 return error;
2131
2132 if (!dir->i_op || !dir->i_op->rmdir)
2133 return -EPERM;
2134
2135 DQUOT_INIT(dir);
2136
1b1dcc1b 2137 mutex_lock(&dentry->d_inode->i_mutex);
1da177e4
LT
2138 dentry_unhash(dentry);
2139 if (d_mountpoint(dentry))
2140 error = -EBUSY;
2141 else {
2142 error = security_inode_rmdir(dir, dentry);
2143 if (!error) {
2144 error = dir->i_op->rmdir(dir, dentry);
2145 if (!error)
2146 dentry->d_inode->i_flags |= S_DEAD;
2147 }
2148 }
1b1dcc1b 2149 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4 2150 if (!error) {
1da177e4
LT
2151 d_delete(dentry);
2152 }
2153 dput(dentry);
2154
2155 return error;
2156}
2157
5590ff0d 2158static long do_rmdir(int dfd, const char __user *pathname)
1da177e4
LT
2159{
2160 int error = 0;
2161 char * name;
2162 struct dentry *dentry;
2163 struct nameidata nd;
2164
2ad94ae6 2165 error = user_path_parent(dfd, pathname, &nd, &name);
1da177e4 2166 if (error)
2ad94ae6 2167 return error;
1da177e4
LT
2168
2169 switch(nd.last_type) {
0612d9fb
OH
2170 case LAST_DOTDOT:
2171 error = -ENOTEMPTY;
2172 goto exit1;
2173 case LAST_DOT:
2174 error = -EINVAL;
2175 goto exit1;
2176 case LAST_ROOT:
2177 error = -EBUSY;
2178 goto exit1;
1da177e4 2179 }
0612d9fb
OH
2180
2181 nd.flags &= ~LOOKUP_PARENT;
2182
4ac91378 2183 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b77 2184 dentry = lookup_hash(&nd);
1da177e4 2185 error = PTR_ERR(dentry);
6902d925
DH
2186 if (IS_ERR(dentry))
2187 goto exit2;
0622753b
DH
2188 error = mnt_want_write(nd.path.mnt);
2189 if (error)
2190 goto exit3;
4ac91378 2191 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
0622753b
DH
2192 mnt_drop_write(nd.path.mnt);
2193exit3:
6902d925
DH
2194 dput(dentry);
2195exit2:
4ac91378 2196 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4 2197exit1:
1d957f9b 2198 path_put(&nd.path);
1da177e4
LT
2199 putname(name);
2200 return error;
2201}
2202
5590ff0d
UD
2203asmlinkage long sys_rmdir(const char __user *pathname)
2204{
2205 return do_rmdir(AT_FDCWD, pathname);
2206}
2207
1da177e4
LT
2208int vfs_unlink(struct inode *dir, struct dentry *dentry)
2209{
2210 int error = may_delete(dir, dentry, 0);
2211
2212 if (error)
2213 return error;
2214
2215 if (!dir->i_op || !dir->i_op->unlink)
2216 return -EPERM;
2217
2218 DQUOT_INIT(dir);
2219
1b1dcc1b 2220 mutex_lock(&dentry->d_inode->i_mutex);
1da177e4
LT
2221 if (d_mountpoint(dentry))
2222 error = -EBUSY;
2223 else {
2224 error = security_inode_unlink(dir, dentry);
2225 if (!error)
2226 error = dir->i_op->unlink(dir, dentry);
2227 }
1b1dcc1b 2228 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4
LT
2229
2230 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2231 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
ece95912 2232 fsnotify_link_count(dentry->d_inode);
e234f35c 2233 d_delete(dentry);
1da177e4 2234 }
0eeca283 2235
1da177e4
LT
2236 return error;
2237}
2238
2239/*
2240 * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b 2241 * directory's i_mutex. Truncate can take a long time if there is a lot of
1da177e4
LT
2242 * writeout happening, and we don't want to prevent access to the directory
2243 * while waiting on the I/O.
2244 */
5590ff0d 2245static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4 2246{
2ad94ae6
AV
2247 int error;
2248 char *name;
1da177e4
LT
2249 struct dentry *dentry;
2250 struct nameidata nd;
2251 struct inode *inode = NULL;
2252
2ad94ae6 2253 error = user_path_parent(dfd, pathname, &nd, &name);
1da177e4 2254 if (error)
2ad94ae6
AV
2255 return error;
2256
1da177e4
LT
2257 error = -EISDIR;
2258 if (nd.last_type != LAST_NORM)
2259 goto exit1;
0612d9fb
OH
2260
2261 nd.flags &= ~LOOKUP_PARENT;
2262
4ac91378 2263 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b77 2264 dentry = lookup_hash(&nd);
1da177e4
LT
2265 error = PTR_ERR(dentry);
2266 if (!IS_ERR(dentry)) {
2267 /* Why not before? Because we want correct error value */
2268 if (nd.last.name[nd.last.len])
2269 goto slashes;
2270 inode = dentry->d_inode;
2271 if (inode)
2272 atomic_inc(&inode->i_count);
0622753b
DH
2273 error = mnt_want_write(nd.path.mnt);
2274 if (error)
2275 goto exit2;
4ac91378 2276 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
0622753b 2277 mnt_drop_write(nd.path.mnt);
1da177e4
LT
2278 exit2:
2279 dput(dentry);
2280 }
4ac91378 2281 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4
LT
2282 if (inode)
2283 iput(inode); /* truncate the inode here */
2284exit1:
1d957f9b 2285 path_put(&nd.path);
1da177e4
LT
2286 putname(name);
2287 return error;
2288
2289slashes:
2290 error = !dentry->d_inode ? -ENOENT :
2291 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2292 goto exit2;
2293}
2294
5590ff0d
UD
2295asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2296{
2297 if ((flag & ~AT_REMOVEDIR) != 0)
2298 return -EINVAL;
2299
2300 if (flag & AT_REMOVEDIR)
2301 return do_rmdir(dfd, pathname);
2302
2303 return do_unlinkat(dfd, pathname);
2304}
2305
2306asmlinkage long sys_unlink(const char __user *pathname)
2307{
2308 return do_unlinkat(AT_FDCWD, pathname);
2309}
2310
db2e747b 2311int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4 2312{
a95164d9 2313 int error = may_create(dir, dentry);
1da177e4
LT
2314
2315 if (error)
2316 return error;
2317
2318 if (!dir->i_op || !dir->i_op->symlink)
2319 return -EPERM;
2320
2321 error = security_inode_symlink(dir, dentry, oldname);
2322 if (error)
2323 return error;
2324
2325 DQUOT_INIT(dir);
2326 error = dir->i_op->symlink(dir, dentry, oldname);
a74574aa 2327 if (!error)
f38aa942 2328 fsnotify_create(dir, dentry);
1da177e4
LT
2329 return error;
2330}
2331
5590ff0d
UD
2332asmlinkage long sys_symlinkat(const char __user *oldname,
2333 int newdfd, const char __user *newname)
1da177e4 2334{
2ad94ae6
AV
2335 int error;
2336 char *from;
2337 char *to;
6902d925
DH
2338 struct dentry *dentry;
2339 struct nameidata nd;
1da177e4
LT
2340
2341 from = getname(oldname);
2ad94ae6 2342 if (IS_ERR(from))
1da177e4 2343 return PTR_ERR(from);
1da177e4 2344
2ad94ae6 2345 error = user_path_parent(newdfd, newname, &nd, &to);
6902d925 2346 if (error)
2ad94ae6
AV
2347 goto out_putname;
2348
6902d925
DH
2349 dentry = lookup_create(&nd, 0);
2350 error = PTR_ERR(dentry);
2351 if (IS_ERR(dentry))
2352 goto out_unlock;
2353
75c3f29d
DH
2354 error = mnt_want_write(nd.path.mnt);
2355 if (error)
2356 goto out_dput;
db2e747b 2357 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
75c3f29d
DH
2358 mnt_drop_write(nd.path.mnt);
2359out_dput:
6902d925
DH
2360 dput(dentry);
2361out_unlock:
4ac91378 2362 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1d957f9b 2363 path_put(&nd.path);
6902d925
DH
2364 putname(to);
2365out_putname:
1da177e4
LT
2366 putname(from);
2367 return error;
2368}
2369
5590ff0d
UD
2370asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2371{
2372 return sys_symlinkat(oldname, AT_FDCWD, newname);
2373}
2374
1da177e4
LT
2375int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2376{
2377 struct inode *inode = old_dentry->d_inode;
2378 int error;
2379
2380 if (!inode)
2381 return -ENOENT;
2382
a95164d9 2383 error = may_create(dir, new_dentry);
1da177e4
LT
2384 if (error)
2385 return error;
2386
2387 if (dir->i_sb != inode->i_sb)
2388 return -EXDEV;
2389
2390 /*
2391 * A link to an append-only or immutable file cannot be created.
2392 */
2393 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2394 return -EPERM;
2395 if (!dir->i_op || !dir->i_op->link)
2396 return -EPERM;
7e79eedb 2397 if (S_ISDIR(inode->i_mode))
1da177e4
LT
2398 return -EPERM;
2399
2400 error = security_inode_link(old_dentry, dir, new_dentry);
2401 if (error)
2402 return error;
2403
7e79eedb 2404 mutex_lock(&inode->i_mutex);
1da177e4
LT
2405 DQUOT_INIT(dir);
2406 error = dir->i_op->link(old_dentry, dir, new_dentry);
7e79eedb 2407 mutex_unlock(&inode->i_mutex);
e31e14ec 2408 if (!error)
7e79eedb 2409 fsnotify_link(dir, inode, new_dentry);
1da177e4
LT
2410 return error;
2411}
2412
2413/*
2414 * Hardlinks are often used in delicate situations. We avoid
2415 * security-related surprises by not following symlinks on the
2416 * newname. --KAB
2417 *
2418 * We don't follow them on the oldname either to be compatible
2419 * with linux 2.0, and to avoid hard-linking to directories
2420 * and other special files. --ADM
2421 */
5590ff0d 2422asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
c04030e1
UD
2423 int newdfd, const char __user *newname,
2424 int flags)
1da177e4
LT
2425{
2426 struct dentry *new_dentry;
2d8f3038
AV
2427 struct nameidata nd;
2428 struct path old_path;
1da177e4 2429 int error;
2ad94ae6 2430 char *to;
1da177e4 2431
45c9b11a 2432 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
c04030e1
UD
2433 return -EINVAL;
2434
2d8f3038
AV
2435 error = user_path_at(olddfd, oldname,
2436 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2437 &old_path);
1da177e4 2438 if (error)
2ad94ae6
AV
2439 return error;
2440
2441 error = user_path_parent(newdfd, newname, &nd, &to);
1da177e4
LT
2442 if (error)
2443 goto out;
2444 error = -EXDEV;
2d8f3038 2445 if (old_path.mnt != nd.path.mnt)
1da177e4
LT
2446 goto out_release;
2447 new_dentry = lookup_create(&nd, 0);
2448 error = PTR_ERR(new_dentry);
6902d925
DH
2449 if (IS_ERR(new_dentry))
2450 goto out_unlock;
75c3f29d
DH
2451 error = mnt_want_write(nd.path.mnt);
2452 if (error)
2453 goto out_dput;
2d8f3038 2454 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
75c3f29d
DH
2455 mnt_drop_write(nd.path.mnt);
2456out_dput:
6902d925
DH
2457 dput(new_dentry);
2458out_unlock:
4ac91378 2459 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4 2460out_release:
1d957f9b 2461 path_put(&nd.path);
2ad94ae6 2462 putname(to);
1da177e4 2463out:
2d8f3038 2464 path_put(&old_path);
1da177e4
LT
2465
2466 return error;
2467}
2468
5590ff0d
UD
2469asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2470{
c04030e1 2471 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
2472}
2473
1da177e4
LT
2474/*
2475 * The worst of all namespace operations - renaming directory. "Perverted"
2476 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2477 * Problems:
2478 * a) we can get into loop creation. Check is done in is_subdir().
2479 * b) race potential - two innocent renames can create a loop together.
2480 * That's where 4.4 screws up. Current fix: serialization on
a11f3a05 2481 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4
LT
2482 * story.
2483 * c) we have to lock _three_ objects - parents and victim (if it exists).
1b1dcc1b 2484 * And that - after we got ->i_mutex on parents (until then we don't know
1da177e4
LT
2485 * whether the target exists). Solution: try to be smart with locking
2486 * order for inodes. We rely on the fact that tree topology may change
a11f3a05 2487 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4
LT
2488 * move will be locked. Thus we can rank directories by the tree
2489 * (ancestors first) and rank all non-directories after them.
2490 * That works since everybody except rename does "lock parent, lookup,
a11f3a05 2491 * lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4
LT
2492 * HOWEVER, it relies on the assumption that any object with ->lookup()
2493 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2494 * we'd better make sure that there's no link(2) for them.
2495 * d) some filesystems don't support opened-but-unlinked directories,
2496 * either because of layout or because they are not ready to deal with
2497 * all cases correctly. The latter will be fixed (taking this sort of
2498 * stuff into VFS), but the former is not going away. Solution: the same
2499 * trick as in rmdir().
2500 * e) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b 2501 * we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4 2502 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1b1dcc1b 2503 * ->i_mutex on parents, which works but leads to some truely excessive
1da177e4
LT
2504 * locking].
2505 */
75c96f85
AB
2506static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2507 struct inode *new_dir, struct dentry *new_dentry)
1da177e4
LT
2508{
2509 int error = 0;
2510 struct inode *target;
2511
2512 /*
2513 * If we are going to change the parent - check write permissions,
2514 * we'll need to flip '..'.
2515 */
2516 if (new_dir != old_dir) {
f419a2e3 2517 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
1da177e4
LT
2518 if (error)
2519 return error;
2520 }
2521
2522 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2523 if (error)
2524 return error;
2525
2526 target = new_dentry->d_inode;
2527 if (target) {
1b1dcc1b 2528 mutex_lock(&target->i_mutex);
1da177e4
LT
2529 dentry_unhash(new_dentry);
2530 }
2531 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2532 error = -EBUSY;
2533 else
2534 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2535 if (target) {
2536 if (!error)
2537 target->i_flags |= S_DEAD;
1b1dcc1b 2538 mutex_unlock(&target->i_mutex);
1da177e4
LT
2539 if (d_unhashed(new_dentry))
2540 d_rehash(new_dentry);
2541 dput(new_dentry);
2542 }
e31e14ec 2543 if (!error)
349457cc
MF
2544 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2545 d_move(old_dentry,new_dentry);
1da177e4
LT
2546 return error;
2547}
2548
75c96f85
AB
2549static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2550 struct inode *new_dir, struct dentry *new_dentry)
1da177e4
LT
2551{
2552 struct inode *target;
2553 int error;
2554
2555 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2556 if (error)
2557 return error;
2558
2559 dget(new_dentry);
2560 target = new_dentry->d_inode;
2561 if (target)
1b1dcc1b 2562 mutex_lock(&target->i_mutex);
1da177e4
LT
2563 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2564 error = -EBUSY;
2565 else
2566 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2567 if (!error) {
349457cc 2568 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
1da177e4 2569 d_move(old_dentry, new_dentry);
1da177e4
LT
2570 }
2571 if (target)
1b1dcc1b 2572 mutex_unlock(&target->i_mutex);
1da177e4
LT
2573 dput(new_dentry);
2574 return error;
2575}
2576
2577int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2578 struct inode *new_dir, struct dentry *new_dentry)
2579{
2580 int error;
2581 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
0eeca283 2582 const char *old_name;
1da177e4
LT
2583
2584 if (old_dentry->d_inode == new_dentry->d_inode)
2585 return 0;
2586
2587 error = may_delete(old_dir, old_dentry, is_dir);
2588 if (error)
2589 return error;
2590
2591 if (!new_dentry->d_inode)
a95164d9 2592 error = may_create(new_dir, new_dentry);
1da177e4
LT
2593 else
2594 error = may_delete(new_dir, new_dentry, is_dir);
2595 if (error)
2596 return error;
2597
2598 if (!old_dir->i_op || !old_dir->i_op->rename)
2599 return -EPERM;
2600
2601 DQUOT_INIT(old_dir);
2602 DQUOT_INIT(new_dir);
2603
0eeca283
RL
2604 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2605
1da177e4
LT
2606 if (is_dir)
2607 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2608 else
2609 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2610 if (!error) {
0eeca283 2611 const char *new_name = old_dentry->d_name.name;
89204c40 2612 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
5a190ae6 2613 new_dentry->d_inode, old_dentry);
1da177e4 2614 }
0eeca283
RL
2615 fsnotify_oldname_free(old_name);
2616
1da177e4
LT
2617 return error;
2618}
2619
2ad94ae6
AV
2620asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2621 int newdfd, const char __user *newname)
1da177e4 2622{
2ad94ae6
AV
2623 struct dentry *old_dir, *new_dir;
2624 struct dentry *old_dentry, *new_dentry;
2625 struct dentry *trap;
1da177e4 2626 struct nameidata oldnd, newnd;
2ad94ae6
AV
2627 char *from;
2628 char *to;
2629 int error;
1da177e4 2630
2ad94ae6 2631 error = user_path_parent(olddfd, oldname, &oldnd, &from);
1da177e4
LT
2632 if (error)
2633 goto exit;
2634
2ad94ae6 2635 error = user_path_parent(newdfd, newname, &newnd, &to);
1da177e4
LT
2636 if (error)
2637 goto exit1;
2638
2639 error = -EXDEV;
4ac91378 2640 if (oldnd.path.mnt != newnd.path.mnt)
1da177e4
LT
2641 goto exit2;
2642
4ac91378 2643 old_dir = oldnd.path.dentry;
1da177e4
LT
2644 error = -EBUSY;
2645 if (oldnd.last_type != LAST_NORM)
2646 goto exit2;
2647
4ac91378 2648 new_dir = newnd.path.dentry;
1da177e4
LT
2649 if (newnd.last_type != LAST_NORM)
2650 goto exit2;
2651
0612d9fb
OH
2652 oldnd.flags &= ~LOOKUP_PARENT;
2653 newnd.flags &= ~LOOKUP_PARENT;
4e9ed2f8 2654 newnd.flags |= LOOKUP_RENAME_TARGET;
0612d9fb 2655
1da177e4
LT
2656 trap = lock_rename(new_dir, old_dir);
2657
49705b77 2658 old_dentry = lookup_hash(&oldnd);
1da177e4
LT
2659 error = PTR_ERR(old_dentry);
2660 if (IS_ERR(old_dentry))
2661 goto exit3;
2662 /* source must exist */
2663 error = -ENOENT;
2664 if (!old_dentry->d_inode)
2665 goto exit4;
2666 /* unless the source is a directory trailing slashes give -ENOTDIR */
2667 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2668 error = -ENOTDIR;
2669 if (oldnd.last.name[oldnd.last.len])
2670 goto exit4;
2671 if (newnd.last.name[newnd.last.len])
2672 goto exit4;
2673 }
2674 /* source should not be ancestor of target */
2675 error = -EINVAL;
2676 if (old_dentry == trap)
2677 goto exit4;
49705b77 2678 new_dentry = lookup_hash(&newnd);
1da177e4
LT
2679 error = PTR_ERR(new_dentry);
2680 if (IS_ERR(new_dentry))
2681 goto exit4;
2682 /* target should not be an ancestor of source */
2683 error = -ENOTEMPTY;
2684 if (new_dentry == trap)
2685 goto exit5;
2686
9079b1eb
DH
2687 error = mnt_want_write(oldnd.path.mnt);
2688 if (error)
2689 goto exit5;
1da177e4
LT
2690 error = vfs_rename(old_dir->d_inode, old_dentry,
2691 new_dir->d_inode, new_dentry);
9079b1eb 2692 mnt_drop_write(oldnd.path.mnt);
1da177e4
LT
2693exit5:
2694 dput(new_dentry);
2695exit4:
2696 dput(old_dentry);
2697exit3:
2698 unlock_rename(new_dir, old_dir);
2699exit2:
1d957f9b 2700 path_put(&newnd.path);
2ad94ae6 2701 putname(to);
1da177e4 2702exit1:
1d957f9b 2703 path_put(&oldnd.path);
1da177e4 2704 putname(from);
2ad94ae6 2705exit:
1da177e4
LT
2706 return error;
2707}
2708
5590ff0d
UD
2709asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2710{
2711 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2712}
2713
1da177e4
LT
2714int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2715{
2716 int len;
2717
2718 len = PTR_ERR(link);
2719 if (IS_ERR(link))
2720 goto out;
2721
2722 len = strlen(link);
2723 if (len > (unsigned) buflen)
2724 len = buflen;
2725 if (copy_to_user(buffer, link, len))
2726 len = -EFAULT;
2727out:
2728 return len;
2729}
2730
2731/*
2732 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2733 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2734 * using) it for any given inode is up to filesystem.
2735 */
2736int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2737{
2738 struct nameidata nd;
cc314eef 2739 void *cookie;
694a1764 2740 int res;
cc314eef 2741
1da177e4 2742 nd.depth = 0;
cc314eef 2743 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
694a1764
MS
2744 if (IS_ERR(cookie))
2745 return PTR_ERR(cookie);
2746
2747 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2748 if (dentry->d_inode->i_op->put_link)
2749 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2750 return res;
1da177e4
LT
2751}
2752
2753int vfs_follow_link(struct nameidata *nd, const char *link)
2754{
2755 return __vfs_follow_link(nd, link);
2756}
2757
2758/* get the link contents into pagecache */
2759static char *page_getlink(struct dentry * dentry, struct page **ppage)
2760{
2761 struct page * page;
2762 struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b18 2763 page = read_mapping_page(mapping, 0, NULL);
1da177e4 2764 if (IS_ERR(page))
6fe6900e 2765 return (char*)page;
1da177e4
LT
2766 *ppage = page;
2767 return kmap(page);
1da177e4
LT
2768}
2769
2770int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2771{
2772 struct page *page = NULL;
2773 char *s = page_getlink(dentry, &page);
2774 int res = vfs_readlink(dentry,buffer,buflen,s);
2775 if (page) {
2776 kunmap(page);
2777 page_cache_release(page);
2778 }
2779 return res;
2780}
2781
cc314eef 2782void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4 2783{
cc314eef 2784 struct page *page = NULL;
1da177e4 2785 nd_set_link(nd, page_getlink(dentry, &page));
cc314eef 2786 return page;
1da177e4
LT
2787}
2788
cc314eef 2789void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4 2790{
cc314eef
LT
2791 struct page *page = cookie;
2792
2793 if (page) {
1da177e4
LT
2794 kunmap(page);
2795 page_cache_release(page);
1da177e4
LT
2796 }
2797}
2798
0adb25d2
KK
2799int __page_symlink(struct inode *inode, const char *symname, int len,
2800 gfp_t gfp_mask)
1da177e4
LT
2801{
2802 struct address_space *mapping = inode->i_mapping;
0adb25d2 2803 struct page *page;
afddba49 2804 void *fsdata;
beb497ab 2805 int err;
1da177e4
LT
2806 char *kaddr;
2807
7e53cac4 2808retry:
afddba49
NP
2809 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2810 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
1da177e4 2811 if (err)
afddba49
NP
2812 goto fail;
2813
1da177e4
LT
2814 kaddr = kmap_atomic(page, KM_USER0);
2815 memcpy(kaddr, symname, len-1);
2816 kunmap_atomic(kaddr, KM_USER0);
afddba49
NP
2817
2818 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2819 page, fsdata);
1da177e4
LT
2820 if (err < 0)
2821 goto fail;
afddba49
NP
2822 if (err < len-1)
2823 goto retry;
2824
1da177e4
LT
2825 mark_inode_dirty(inode);
2826 return 0;
1da177e4
LT
2827fail:
2828 return err;
2829}
2830
0adb25d2
KK
2831int page_symlink(struct inode *inode, const char *symname, int len)
2832{
2833 return __page_symlink(inode, symname, len,
2834 mapping_gfp_mask(inode->i_mapping));
2835}
2836
92e1d5be 2837const struct inode_operations page_symlink_inode_operations = {
1da177e4
LT
2838 .readlink = generic_readlink,
2839 .follow_link = page_follow_link_light,
2840 .put_link = page_put_link,
2841};
2842
2d8f3038 2843EXPORT_SYMBOL(user_path_at);
1da177e4
LT
2844EXPORT_SYMBOL(follow_down);
2845EXPORT_SYMBOL(follow_up);
2846EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2847EXPORT_SYMBOL(getname);
2848EXPORT_SYMBOL(lock_rename);
1da177e4
LT
2849EXPORT_SYMBOL(lookup_one_len);
2850EXPORT_SYMBOL(page_follow_link_light);
2851EXPORT_SYMBOL(page_put_link);
2852EXPORT_SYMBOL(page_readlink);
0adb25d2 2853EXPORT_SYMBOL(__page_symlink);
1da177e4
LT
2854EXPORT_SYMBOL(page_symlink);
2855EXPORT_SYMBOL(page_symlink_inode_operations);
2856EXPORT_SYMBOL(path_lookup);
d1811465 2857EXPORT_SYMBOL(kern_path);
16f18200 2858EXPORT_SYMBOL(vfs_path_lookup);
f419a2e3 2859EXPORT_SYMBOL(inode_permission);
e4543edd 2860EXPORT_SYMBOL(vfs_permission);
8c744fb8 2861EXPORT_SYMBOL(file_permission);
1da177e4
LT
2862EXPORT_SYMBOL(unlock_rename);
2863EXPORT_SYMBOL(vfs_create);
2864EXPORT_SYMBOL(vfs_follow_link);
2865EXPORT_SYMBOL(vfs_link);
2866EXPORT_SYMBOL(vfs_mkdir);
2867EXPORT_SYMBOL(vfs_mknod);
2868EXPORT_SYMBOL(generic_permission);
2869EXPORT_SYMBOL(vfs_readlink);
2870EXPORT_SYMBOL(vfs_rename);
2871EXPORT_SYMBOL(vfs_rmdir);
2872EXPORT_SYMBOL(vfs_symlink);
2873EXPORT_SYMBOL(vfs_unlink);
2874EXPORT_SYMBOL(dentry_unhash);
2875EXPORT_SYMBOL(generic_readlink);