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