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