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