vfs: make path_openat take a struct filename pointer
[deliverable/linux.git] / fs / namei.c
1 /*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 /*
8 * Some corrections by tytso.
9 */
10
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/fs.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <asm/uaccess.h>
38
39 #include "internal.h"
40 #include "mount.h"
41
42 /* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
47 *
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
54 *
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
58 *
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
61 *
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
68 */
69
70 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
76 * the name is a symlink pointing to a non-existent name.
77 *
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
85 */
86
87 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
89 *
90 * [10-Sep-98 Alan Modra] Another symlink change.
91 */
92
93 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
100 *
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
106 */
107 /*
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
110 * any extra contention...
111 */
112
113 /* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
116 *
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
119 */
120 void final_putname(struct filename *name)
121 {
122 __putname(name->name);
123 kfree(name);
124 }
125
126 static struct filename *
127 getname_flags(const char __user *filename, int flags, int *empty)
128 {
129 struct filename *result, *err;
130 char *kname;
131 int len;
132
133 result = audit_reusename(filename);
134 if (result)
135 return result;
136
137 /* FIXME: create dedicated slabcache? */
138 result = kzalloc(sizeof(*result), GFP_KERNEL);
139 if (unlikely(!result))
140 return ERR_PTR(-ENOMEM);
141
142 kname = __getname();
143 if (unlikely(!kname)) {
144 err = ERR_PTR(-ENOMEM);
145 goto error_free_name;
146 }
147
148 result->name = kname;
149 result->uptr = filename;
150 len = strncpy_from_user(kname, filename, PATH_MAX);
151 if (unlikely(len < 0)) {
152 err = ERR_PTR(len);
153 goto error;
154 }
155
156 /* The empty path is special. */
157 if (unlikely(!len)) {
158 if (empty)
159 *empty = 1;
160 err = ERR_PTR(-ENOENT);
161 if (!(flags & LOOKUP_EMPTY))
162 goto error;
163 }
164
165 err = ERR_PTR(-ENAMETOOLONG);
166 if (likely(len < PATH_MAX)) {
167 audit_getname(result);
168 return result;
169 }
170
171 error:
172 __putname(kname);
173 error_free_name:
174 kfree(result);
175 return err;
176 }
177
178 struct filename *
179 getname(const char __user * filename)
180 {
181 return getname_flags(filename, 0, NULL);
182 }
183 EXPORT_SYMBOL(getname);
184
185 #ifdef CONFIG_AUDITSYSCALL
186 void putname(struct filename *name)
187 {
188 if (unlikely(!audit_dummy_context()))
189 return audit_putname(name);
190 final_putname(name);
191 }
192 #endif
193
194 static int check_acl(struct inode *inode, int mask)
195 {
196 #ifdef CONFIG_FS_POSIX_ACL
197 struct posix_acl *acl;
198
199 if (mask & MAY_NOT_BLOCK) {
200 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
201 if (!acl)
202 return -EAGAIN;
203 /* no ->get_acl() calls in RCU mode... */
204 if (acl == ACL_NOT_CACHED)
205 return -ECHILD;
206 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
207 }
208
209 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
210
211 /*
212 * A filesystem can force a ACL callback by just never filling the
213 * ACL cache. But normally you'd fill the cache either at inode
214 * instantiation time, or on the first ->get_acl call.
215 *
216 * If the filesystem doesn't have a get_acl() function at all, we'll
217 * just create the negative cache entry.
218 */
219 if (acl == ACL_NOT_CACHED) {
220 if (inode->i_op->get_acl) {
221 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
222 if (IS_ERR(acl))
223 return PTR_ERR(acl);
224 } else {
225 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
226 return -EAGAIN;
227 }
228 }
229
230 if (acl) {
231 int error = posix_acl_permission(inode, acl, mask);
232 posix_acl_release(acl);
233 return error;
234 }
235 #endif
236
237 return -EAGAIN;
238 }
239
240 /*
241 * This does the basic permission checking
242 */
243 static int acl_permission_check(struct inode *inode, int mask)
244 {
245 unsigned int mode = inode->i_mode;
246
247 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
248 mode >>= 6;
249 else {
250 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
251 int error = check_acl(inode, mask);
252 if (error != -EAGAIN)
253 return error;
254 }
255
256 if (in_group_p(inode->i_gid))
257 mode >>= 3;
258 }
259
260 /*
261 * If the DACs are ok we don't need any capability check.
262 */
263 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
264 return 0;
265 return -EACCES;
266 }
267
268 /**
269 * generic_permission - check for access rights on a Posix-like filesystem
270 * @inode: inode to check access rights for
271 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
272 *
273 * Used to check for read/write/execute permissions on a file.
274 * We use "fsuid" for this, letting us set arbitrary permissions
275 * for filesystem access without changing the "normal" uids which
276 * are used for other things.
277 *
278 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
279 * request cannot be satisfied (eg. requires blocking or too much complexity).
280 * It would then be called again in ref-walk mode.
281 */
282 int generic_permission(struct inode *inode, int mask)
283 {
284 int ret;
285
286 /*
287 * Do the basic permission checks.
288 */
289 ret = acl_permission_check(inode, mask);
290 if (ret != -EACCES)
291 return ret;
292
293 if (S_ISDIR(inode->i_mode)) {
294 /* DACs are overridable for directories */
295 if (inode_capable(inode, CAP_DAC_OVERRIDE))
296 return 0;
297 if (!(mask & MAY_WRITE))
298 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
299 return 0;
300 return -EACCES;
301 }
302 /*
303 * Read/write DACs are always overridable.
304 * Executable DACs are overridable when there is
305 * at least one exec bit set.
306 */
307 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
308 if (inode_capable(inode, CAP_DAC_OVERRIDE))
309 return 0;
310
311 /*
312 * Searching includes executable on directories, else just read.
313 */
314 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
315 if (mask == MAY_READ)
316 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
317 return 0;
318
319 return -EACCES;
320 }
321
322 /*
323 * We _really_ want to just do "generic_permission()" without
324 * even looking at the inode->i_op values. So we keep a cache
325 * flag in inode->i_opflags, that says "this has not special
326 * permission function, use the fast case".
327 */
328 static inline int do_inode_permission(struct inode *inode, int mask)
329 {
330 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
331 if (likely(inode->i_op->permission))
332 return inode->i_op->permission(inode, mask);
333
334 /* This gets set once for the inode lifetime */
335 spin_lock(&inode->i_lock);
336 inode->i_opflags |= IOP_FASTPERM;
337 spin_unlock(&inode->i_lock);
338 }
339 return generic_permission(inode, mask);
340 }
341
342 /**
343 * __inode_permission - Check for access rights to a given inode
344 * @inode: Inode to check permission on
345 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
346 *
347 * Check for read/write/execute permissions on an inode.
348 *
349 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
350 *
351 * This does not check for a read-only file system. You probably want
352 * inode_permission().
353 */
354 int __inode_permission(struct inode *inode, int mask)
355 {
356 int retval;
357
358 if (unlikely(mask & MAY_WRITE)) {
359 /*
360 * Nobody gets write access to an immutable file.
361 */
362 if (IS_IMMUTABLE(inode))
363 return -EACCES;
364 }
365
366 retval = do_inode_permission(inode, mask);
367 if (retval)
368 return retval;
369
370 retval = devcgroup_inode_permission(inode, mask);
371 if (retval)
372 return retval;
373
374 return security_inode_permission(inode, mask);
375 }
376
377 /**
378 * sb_permission - Check superblock-level permissions
379 * @sb: Superblock of inode to check permission on
380 * @inode: Inode to check permission on
381 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
382 *
383 * Separate out file-system wide checks from inode-specific permission checks.
384 */
385 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
386 {
387 if (unlikely(mask & MAY_WRITE)) {
388 umode_t mode = inode->i_mode;
389
390 /* Nobody gets write access to a read-only fs. */
391 if ((sb->s_flags & MS_RDONLY) &&
392 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
393 return -EROFS;
394 }
395 return 0;
396 }
397
398 /**
399 * inode_permission - Check for access rights to a given inode
400 * @inode: Inode to check permission on
401 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
402 *
403 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
404 * this, letting us set arbitrary permissions for filesystem access without
405 * changing the "normal" UIDs which are used for other things.
406 *
407 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
408 */
409 int inode_permission(struct inode *inode, int mask)
410 {
411 int retval;
412
413 retval = sb_permission(inode->i_sb, inode, mask);
414 if (retval)
415 return retval;
416 return __inode_permission(inode, mask);
417 }
418
419 /**
420 * path_get - get a reference to a path
421 * @path: path to get the reference to
422 *
423 * Given a path increment the reference count to the dentry and the vfsmount.
424 */
425 void path_get(struct path *path)
426 {
427 mntget(path->mnt);
428 dget(path->dentry);
429 }
430 EXPORT_SYMBOL(path_get);
431
432 /**
433 * path_put - put a reference to a path
434 * @path: path to put the reference to
435 *
436 * Given a path decrement the reference count to the dentry and the vfsmount.
437 */
438 void path_put(struct path *path)
439 {
440 dput(path->dentry);
441 mntput(path->mnt);
442 }
443 EXPORT_SYMBOL(path_put);
444
445 /*
446 * Path walking has 2 modes, rcu-walk and ref-walk (see
447 * Documentation/filesystems/path-lookup.txt). In situations when we can't
448 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
449 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
450 * mode. Refcounts are grabbed at the last known good point before rcu-walk
451 * got stuck, so ref-walk may continue from there. If this is not successful
452 * (eg. a seqcount has changed), then failure is returned and it's up to caller
453 * to restart the path walk from the beginning in ref-walk mode.
454 */
455
456 static inline void lock_rcu_walk(void)
457 {
458 br_read_lock(&vfsmount_lock);
459 rcu_read_lock();
460 }
461
462 static inline void unlock_rcu_walk(void)
463 {
464 rcu_read_unlock();
465 br_read_unlock(&vfsmount_lock);
466 }
467
468 /**
469 * unlazy_walk - try to switch to ref-walk mode.
470 * @nd: nameidata pathwalk data
471 * @dentry: child of nd->path.dentry or NULL
472 * Returns: 0 on success, -ECHILD on failure
473 *
474 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
475 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
476 * @nd or NULL. Must be called from rcu-walk context.
477 */
478 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
479 {
480 struct fs_struct *fs = current->fs;
481 struct dentry *parent = nd->path.dentry;
482 int want_root = 0;
483
484 BUG_ON(!(nd->flags & LOOKUP_RCU));
485 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
486 want_root = 1;
487 spin_lock(&fs->lock);
488 if (nd->root.mnt != fs->root.mnt ||
489 nd->root.dentry != fs->root.dentry)
490 goto err_root;
491 }
492 spin_lock(&parent->d_lock);
493 if (!dentry) {
494 if (!__d_rcu_to_refcount(parent, nd->seq))
495 goto err_parent;
496 BUG_ON(nd->inode != parent->d_inode);
497 } else {
498 if (dentry->d_parent != parent)
499 goto err_parent;
500 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
501 if (!__d_rcu_to_refcount(dentry, nd->seq))
502 goto err_child;
503 /*
504 * If the sequence check on the child dentry passed, then
505 * the child has not been removed from its parent. This
506 * means the parent dentry must be valid and able to take
507 * a reference at this point.
508 */
509 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
510 BUG_ON(!parent->d_count);
511 parent->d_count++;
512 spin_unlock(&dentry->d_lock);
513 }
514 spin_unlock(&parent->d_lock);
515 if (want_root) {
516 path_get(&nd->root);
517 spin_unlock(&fs->lock);
518 }
519 mntget(nd->path.mnt);
520
521 unlock_rcu_walk();
522 nd->flags &= ~LOOKUP_RCU;
523 return 0;
524
525 err_child:
526 spin_unlock(&dentry->d_lock);
527 err_parent:
528 spin_unlock(&parent->d_lock);
529 err_root:
530 if (want_root)
531 spin_unlock(&fs->lock);
532 return -ECHILD;
533 }
534
535 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
536 {
537 return dentry->d_op->d_revalidate(dentry, flags);
538 }
539
540 /**
541 * complete_walk - successful completion of path walk
542 * @nd: pointer nameidata
543 *
544 * If we had been in RCU mode, drop out of it and legitimize nd->path.
545 * Revalidate the final result, unless we'd already done that during
546 * the path walk or the filesystem doesn't ask for it. Return 0 on
547 * success, -error on failure. In case of failure caller does not
548 * need to drop nd->path.
549 */
550 static int complete_walk(struct nameidata *nd)
551 {
552 struct dentry *dentry = nd->path.dentry;
553 int status;
554
555 if (nd->flags & LOOKUP_RCU) {
556 nd->flags &= ~LOOKUP_RCU;
557 if (!(nd->flags & LOOKUP_ROOT))
558 nd->root.mnt = NULL;
559 spin_lock(&dentry->d_lock);
560 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
561 spin_unlock(&dentry->d_lock);
562 unlock_rcu_walk();
563 return -ECHILD;
564 }
565 BUG_ON(nd->inode != dentry->d_inode);
566 spin_unlock(&dentry->d_lock);
567 mntget(nd->path.mnt);
568 unlock_rcu_walk();
569 }
570
571 if (likely(!(nd->flags & LOOKUP_JUMPED)))
572 return 0;
573
574 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
575 return 0;
576
577 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
578 return 0;
579
580 /* Note: we do not d_invalidate() */
581 status = d_revalidate(dentry, nd->flags);
582 if (status > 0)
583 return 0;
584
585 if (!status)
586 status = -ESTALE;
587
588 path_put(&nd->path);
589 return status;
590 }
591
592 static __always_inline void set_root(struct nameidata *nd)
593 {
594 if (!nd->root.mnt)
595 get_fs_root(current->fs, &nd->root);
596 }
597
598 static int link_path_walk(const char *, struct nameidata *);
599
600 static __always_inline void set_root_rcu(struct nameidata *nd)
601 {
602 if (!nd->root.mnt) {
603 struct fs_struct *fs = current->fs;
604 unsigned seq;
605
606 do {
607 seq = read_seqcount_begin(&fs->seq);
608 nd->root = fs->root;
609 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
610 } while (read_seqcount_retry(&fs->seq, seq));
611 }
612 }
613
614 static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
615 {
616 int ret;
617
618 if (IS_ERR(link))
619 goto fail;
620
621 if (*link == '/') {
622 set_root(nd);
623 path_put(&nd->path);
624 nd->path = nd->root;
625 path_get(&nd->root);
626 nd->flags |= LOOKUP_JUMPED;
627 }
628 nd->inode = nd->path.dentry->d_inode;
629
630 ret = link_path_walk(link, nd);
631 return ret;
632 fail:
633 path_put(&nd->path);
634 return PTR_ERR(link);
635 }
636
637 static void path_put_conditional(struct path *path, struct nameidata *nd)
638 {
639 dput(path->dentry);
640 if (path->mnt != nd->path.mnt)
641 mntput(path->mnt);
642 }
643
644 static inline void path_to_nameidata(const struct path *path,
645 struct nameidata *nd)
646 {
647 if (!(nd->flags & LOOKUP_RCU)) {
648 dput(nd->path.dentry);
649 if (nd->path.mnt != path->mnt)
650 mntput(nd->path.mnt);
651 }
652 nd->path.mnt = path->mnt;
653 nd->path.dentry = path->dentry;
654 }
655
656 /*
657 * Helper to directly jump to a known parsed path from ->follow_link,
658 * caller must have taken a reference to path beforehand.
659 */
660 void nd_jump_link(struct nameidata *nd, struct path *path)
661 {
662 path_put(&nd->path);
663
664 nd->path = *path;
665 nd->inode = nd->path.dentry->d_inode;
666 nd->flags |= LOOKUP_JUMPED;
667
668 BUG_ON(nd->inode->i_op->follow_link);
669 }
670
671 static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
672 {
673 struct inode *inode = link->dentry->d_inode;
674 if (inode->i_op->put_link)
675 inode->i_op->put_link(link->dentry, nd, cookie);
676 path_put(link);
677 }
678
679 int sysctl_protected_symlinks __read_mostly = 1;
680 int sysctl_protected_hardlinks __read_mostly = 1;
681
682 /**
683 * may_follow_link - Check symlink following for unsafe situations
684 * @link: The path of the symlink
685 * @nd: nameidata pathwalk data
686 *
687 * In the case of the sysctl_protected_symlinks sysctl being enabled,
688 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
689 * in a sticky world-writable directory. This is to protect privileged
690 * processes from failing races against path names that may change out
691 * from under them by way of other users creating malicious symlinks.
692 * It will permit symlinks to be followed only when outside a sticky
693 * world-writable directory, or when the uid of the symlink and follower
694 * match, or when the directory owner matches the symlink's owner.
695 *
696 * Returns 0 if following the symlink is allowed, -ve on error.
697 */
698 static inline int may_follow_link(struct path *link, struct nameidata *nd)
699 {
700 const struct inode *inode;
701 const struct inode *parent;
702
703 if (!sysctl_protected_symlinks)
704 return 0;
705
706 /* Allowed if owner and follower match. */
707 inode = link->dentry->d_inode;
708 if (uid_eq(current_cred()->fsuid, inode->i_uid))
709 return 0;
710
711 /* Allowed if parent directory not sticky and world-writable. */
712 parent = nd->path.dentry->d_inode;
713 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
714 return 0;
715
716 /* Allowed if parent directory and link owner match. */
717 if (uid_eq(parent->i_uid, inode->i_uid))
718 return 0;
719
720 audit_log_link_denied("follow_link", link);
721 path_put_conditional(link, nd);
722 path_put(&nd->path);
723 return -EACCES;
724 }
725
726 /**
727 * safe_hardlink_source - Check for safe hardlink conditions
728 * @inode: the source inode to hardlink from
729 *
730 * Return false if at least one of the following conditions:
731 * - inode is not a regular file
732 * - inode is setuid
733 * - inode is setgid and group-exec
734 * - access failure for read and write
735 *
736 * Otherwise returns true.
737 */
738 static bool safe_hardlink_source(struct inode *inode)
739 {
740 umode_t mode = inode->i_mode;
741
742 /* Special files should not get pinned to the filesystem. */
743 if (!S_ISREG(mode))
744 return false;
745
746 /* Setuid files should not get pinned to the filesystem. */
747 if (mode & S_ISUID)
748 return false;
749
750 /* Executable setgid files should not get pinned to the filesystem. */
751 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
752 return false;
753
754 /* Hardlinking to unreadable or unwritable sources is dangerous. */
755 if (inode_permission(inode, MAY_READ | MAY_WRITE))
756 return false;
757
758 return true;
759 }
760
761 /**
762 * may_linkat - Check permissions for creating a hardlink
763 * @link: the source to hardlink from
764 *
765 * Block hardlink when all of:
766 * - sysctl_protected_hardlinks enabled
767 * - fsuid does not match inode
768 * - hardlink source is unsafe (see safe_hardlink_source() above)
769 * - not CAP_FOWNER
770 *
771 * Returns 0 if successful, -ve on error.
772 */
773 static int may_linkat(struct path *link)
774 {
775 const struct cred *cred;
776 struct inode *inode;
777
778 if (!sysctl_protected_hardlinks)
779 return 0;
780
781 cred = current_cred();
782 inode = link->dentry->d_inode;
783
784 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
785 * otherwise, it must be a safe source.
786 */
787 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
788 capable(CAP_FOWNER))
789 return 0;
790
791 audit_log_link_denied("linkat", link);
792 return -EPERM;
793 }
794
795 static __always_inline int
796 follow_link(struct path *link, struct nameidata *nd, void **p)
797 {
798 struct dentry *dentry = link->dentry;
799 int error;
800 char *s;
801
802 BUG_ON(nd->flags & LOOKUP_RCU);
803
804 if (link->mnt == nd->path.mnt)
805 mntget(link->mnt);
806
807 error = -ELOOP;
808 if (unlikely(current->total_link_count >= 40))
809 goto out_put_nd_path;
810
811 cond_resched();
812 current->total_link_count++;
813
814 touch_atime(link);
815 nd_set_link(nd, NULL);
816
817 error = security_inode_follow_link(link->dentry, nd);
818 if (error)
819 goto out_put_nd_path;
820
821 nd->last_type = LAST_BIND;
822 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
823 error = PTR_ERR(*p);
824 if (IS_ERR(*p))
825 goto out_put_nd_path;
826
827 error = 0;
828 s = nd_get_link(nd);
829 if (s) {
830 error = __vfs_follow_link(nd, s);
831 if (unlikely(error))
832 put_link(nd, link, *p);
833 }
834
835 return error;
836
837 out_put_nd_path:
838 *p = NULL;
839 path_put(&nd->path);
840 path_put(link);
841 return error;
842 }
843
844 static int follow_up_rcu(struct path *path)
845 {
846 struct mount *mnt = real_mount(path->mnt);
847 struct mount *parent;
848 struct dentry *mountpoint;
849
850 parent = mnt->mnt_parent;
851 if (&parent->mnt == path->mnt)
852 return 0;
853 mountpoint = mnt->mnt_mountpoint;
854 path->dentry = mountpoint;
855 path->mnt = &parent->mnt;
856 return 1;
857 }
858
859 /*
860 * follow_up - Find the mountpoint of path's vfsmount
861 *
862 * Given a path, find the mountpoint of its source file system.
863 * Replace @path with the path of the mountpoint in the parent mount.
864 * Up is towards /.
865 *
866 * Return 1 if we went up a level and 0 if we were already at the
867 * root.
868 */
869 int follow_up(struct path *path)
870 {
871 struct mount *mnt = real_mount(path->mnt);
872 struct mount *parent;
873 struct dentry *mountpoint;
874
875 br_read_lock(&vfsmount_lock);
876 parent = mnt->mnt_parent;
877 if (parent == mnt) {
878 br_read_unlock(&vfsmount_lock);
879 return 0;
880 }
881 mntget(&parent->mnt);
882 mountpoint = dget(mnt->mnt_mountpoint);
883 br_read_unlock(&vfsmount_lock);
884 dput(path->dentry);
885 path->dentry = mountpoint;
886 mntput(path->mnt);
887 path->mnt = &parent->mnt;
888 return 1;
889 }
890
891 /*
892 * Perform an automount
893 * - return -EISDIR to tell follow_managed() to stop and return the path we
894 * were called with.
895 */
896 static int follow_automount(struct path *path, unsigned flags,
897 bool *need_mntput)
898 {
899 struct vfsmount *mnt;
900 int err;
901
902 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
903 return -EREMOTE;
904
905 /* We don't want to mount if someone's just doing a stat -
906 * unless they're stat'ing a directory and appended a '/' to
907 * the name.
908 *
909 * We do, however, want to mount if someone wants to open or
910 * create a file of any type under the mountpoint, wants to
911 * traverse through the mountpoint or wants to open the
912 * mounted directory. Also, autofs may mark negative dentries
913 * as being automount points. These will need the attentions
914 * of the daemon to instantiate them before they can be used.
915 */
916 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
917 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
918 path->dentry->d_inode)
919 return -EISDIR;
920
921 current->total_link_count++;
922 if (current->total_link_count >= 40)
923 return -ELOOP;
924
925 mnt = path->dentry->d_op->d_automount(path);
926 if (IS_ERR(mnt)) {
927 /*
928 * The filesystem is allowed to return -EISDIR here to indicate
929 * it doesn't want to automount. For instance, autofs would do
930 * this so that its userspace daemon can mount on this dentry.
931 *
932 * However, we can only permit this if it's a terminal point in
933 * the path being looked up; if it wasn't then the remainder of
934 * the path is inaccessible and we should say so.
935 */
936 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
937 return -EREMOTE;
938 return PTR_ERR(mnt);
939 }
940
941 if (!mnt) /* mount collision */
942 return 0;
943
944 if (!*need_mntput) {
945 /* lock_mount() may release path->mnt on error */
946 mntget(path->mnt);
947 *need_mntput = true;
948 }
949 err = finish_automount(mnt, path);
950
951 switch (err) {
952 case -EBUSY:
953 /* Someone else made a mount here whilst we were busy */
954 return 0;
955 case 0:
956 path_put(path);
957 path->mnt = mnt;
958 path->dentry = dget(mnt->mnt_root);
959 return 0;
960 default:
961 return err;
962 }
963
964 }
965
966 /*
967 * Handle a dentry that is managed in some way.
968 * - Flagged for transit management (autofs)
969 * - Flagged as mountpoint
970 * - Flagged as automount point
971 *
972 * This may only be called in refwalk mode.
973 *
974 * Serialization is taken care of in namespace.c
975 */
976 static int follow_managed(struct path *path, unsigned flags)
977 {
978 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
979 unsigned managed;
980 bool need_mntput = false;
981 int ret = 0;
982
983 /* Given that we're not holding a lock here, we retain the value in a
984 * local variable for each dentry as we look at it so that we don't see
985 * the components of that value change under us */
986 while (managed = ACCESS_ONCE(path->dentry->d_flags),
987 managed &= DCACHE_MANAGED_DENTRY,
988 unlikely(managed != 0)) {
989 /* Allow the filesystem to manage the transit without i_mutex
990 * being held. */
991 if (managed & DCACHE_MANAGE_TRANSIT) {
992 BUG_ON(!path->dentry->d_op);
993 BUG_ON(!path->dentry->d_op->d_manage);
994 ret = path->dentry->d_op->d_manage(path->dentry, false);
995 if (ret < 0)
996 break;
997 }
998
999 /* Transit to a mounted filesystem. */
1000 if (managed & DCACHE_MOUNTED) {
1001 struct vfsmount *mounted = lookup_mnt(path);
1002 if (mounted) {
1003 dput(path->dentry);
1004 if (need_mntput)
1005 mntput(path->mnt);
1006 path->mnt = mounted;
1007 path->dentry = dget(mounted->mnt_root);
1008 need_mntput = true;
1009 continue;
1010 }
1011
1012 /* Something is mounted on this dentry in another
1013 * namespace and/or whatever was mounted there in this
1014 * namespace got unmounted before we managed to get the
1015 * vfsmount_lock */
1016 }
1017
1018 /* Handle an automount point */
1019 if (managed & DCACHE_NEED_AUTOMOUNT) {
1020 ret = follow_automount(path, flags, &need_mntput);
1021 if (ret < 0)
1022 break;
1023 continue;
1024 }
1025
1026 /* We didn't change the current path point */
1027 break;
1028 }
1029
1030 if (need_mntput && path->mnt == mnt)
1031 mntput(path->mnt);
1032 if (ret == -EISDIR)
1033 ret = 0;
1034 return ret < 0 ? ret : need_mntput;
1035 }
1036
1037 int follow_down_one(struct path *path)
1038 {
1039 struct vfsmount *mounted;
1040
1041 mounted = lookup_mnt(path);
1042 if (mounted) {
1043 dput(path->dentry);
1044 mntput(path->mnt);
1045 path->mnt = mounted;
1046 path->dentry = dget(mounted->mnt_root);
1047 return 1;
1048 }
1049 return 0;
1050 }
1051
1052 static inline bool managed_dentry_might_block(struct dentry *dentry)
1053 {
1054 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1055 dentry->d_op->d_manage(dentry, true) < 0);
1056 }
1057
1058 /*
1059 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1060 * we meet a managed dentry that would need blocking.
1061 */
1062 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1063 struct inode **inode)
1064 {
1065 for (;;) {
1066 struct mount *mounted;
1067 /*
1068 * Don't forget we might have a non-mountpoint managed dentry
1069 * that wants to block transit.
1070 */
1071 if (unlikely(managed_dentry_might_block(path->dentry)))
1072 return false;
1073
1074 if (!d_mountpoint(path->dentry))
1075 break;
1076
1077 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1078 if (!mounted)
1079 break;
1080 path->mnt = &mounted->mnt;
1081 path->dentry = mounted->mnt.mnt_root;
1082 nd->flags |= LOOKUP_JUMPED;
1083 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1084 /*
1085 * Update the inode too. We don't need to re-check the
1086 * dentry sequence number here after this d_inode read,
1087 * because a mount-point is always pinned.
1088 */
1089 *inode = path->dentry->d_inode;
1090 }
1091 return true;
1092 }
1093
1094 static void follow_mount_rcu(struct nameidata *nd)
1095 {
1096 while (d_mountpoint(nd->path.dentry)) {
1097 struct mount *mounted;
1098 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1099 if (!mounted)
1100 break;
1101 nd->path.mnt = &mounted->mnt;
1102 nd->path.dentry = mounted->mnt.mnt_root;
1103 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1104 }
1105 }
1106
1107 static int follow_dotdot_rcu(struct nameidata *nd)
1108 {
1109 set_root_rcu(nd);
1110
1111 while (1) {
1112 if (nd->path.dentry == nd->root.dentry &&
1113 nd->path.mnt == nd->root.mnt) {
1114 break;
1115 }
1116 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1117 struct dentry *old = nd->path.dentry;
1118 struct dentry *parent = old->d_parent;
1119 unsigned seq;
1120
1121 seq = read_seqcount_begin(&parent->d_seq);
1122 if (read_seqcount_retry(&old->d_seq, nd->seq))
1123 goto failed;
1124 nd->path.dentry = parent;
1125 nd->seq = seq;
1126 break;
1127 }
1128 if (!follow_up_rcu(&nd->path))
1129 break;
1130 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1131 }
1132 follow_mount_rcu(nd);
1133 nd->inode = nd->path.dentry->d_inode;
1134 return 0;
1135
1136 failed:
1137 nd->flags &= ~LOOKUP_RCU;
1138 if (!(nd->flags & LOOKUP_ROOT))
1139 nd->root.mnt = NULL;
1140 unlock_rcu_walk();
1141 return -ECHILD;
1142 }
1143
1144 /*
1145 * Follow down to the covering mount currently visible to userspace. At each
1146 * point, the filesystem owning that dentry may be queried as to whether the
1147 * caller is permitted to proceed or not.
1148 */
1149 int follow_down(struct path *path)
1150 {
1151 unsigned managed;
1152 int ret;
1153
1154 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1155 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1156 /* Allow the filesystem to manage the transit without i_mutex
1157 * being held.
1158 *
1159 * We indicate to the filesystem if someone is trying to mount
1160 * something here. This gives autofs the chance to deny anyone
1161 * other than its daemon the right to mount on its
1162 * superstructure.
1163 *
1164 * The filesystem may sleep at this point.
1165 */
1166 if (managed & DCACHE_MANAGE_TRANSIT) {
1167 BUG_ON(!path->dentry->d_op);
1168 BUG_ON(!path->dentry->d_op->d_manage);
1169 ret = path->dentry->d_op->d_manage(
1170 path->dentry, false);
1171 if (ret < 0)
1172 return ret == -EISDIR ? 0 : ret;
1173 }
1174
1175 /* Transit to a mounted filesystem. */
1176 if (managed & DCACHE_MOUNTED) {
1177 struct vfsmount *mounted = lookup_mnt(path);
1178 if (!mounted)
1179 break;
1180 dput(path->dentry);
1181 mntput(path->mnt);
1182 path->mnt = mounted;
1183 path->dentry = dget(mounted->mnt_root);
1184 continue;
1185 }
1186
1187 /* Don't handle automount points here */
1188 break;
1189 }
1190 return 0;
1191 }
1192
1193 /*
1194 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1195 */
1196 static void follow_mount(struct path *path)
1197 {
1198 while (d_mountpoint(path->dentry)) {
1199 struct vfsmount *mounted = lookup_mnt(path);
1200 if (!mounted)
1201 break;
1202 dput(path->dentry);
1203 mntput(path->mnt);
1204 path->mnt = mounted;
1205 path->dentry = dget(mounted->mnt_root);
1206 }
1207 }
1208
1209 static void follow_dotdot(struct nameidata *nd)
1210 {
1211 set_root(nd);
1212
1213 while(1) {
1214 struct dentry *old = nd->path.dentry;
1215
1216 if (nd->path.dentry == nd->root.dentry &&
1217 nd->path.mnt == nd->root.mnt) {
1218 break;
1219 }
1220 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1221 /* rare case of legitimate dget_parent()... */
1222 nd->path.dentry = dget_parent(nd->path.dentry);
1223 dput(old);
1224 break;
1225 }
1226 if (!follow_up(&nd->path))
1227 break;
1228 }
1229 follow_mount(&nd->path);
1230 nd->inode = nd->path.dentry->d_inode;
1231 }
1232
1233 /*
1234 * This looks up the name in dcache, possibly revalidates the old dentry and
1235 * allocates a new one if not found or not valid. In the need_lookup argument
1236 * returns whether i_op->lookup is necessary.
1237 *
1238 * dir->d_inode->i_mutex must be held
1239 */
1240 static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1241 unsigned int flags, bool *need_lookup)
1242 {
1243 struct dentry *dentry;
1244 int error;
1245
1246 *need_lookup = false;
1247 dentry = d_lookup(dir, name);
1248 if (dentry) {
1249 if (d_need_lookup(dentry)) {
1250 *need_lookup = true;
1251 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1252 error = d_revalidate(dentry, flags);
1253 if (unlikely(error <= 0)) {
1254 if (error < 0) {
1255 dput(dentry);
1256 return ERR_PTR(error);
1257 } else if (!d_invalidate(dentry)) {
1258 dput(dentry);
1259 dentry = NULL;
1260 }
1261 }
1262 }
1263 }
1264
1265 if (!dentry) {
1266 dentry = d_alloc(dir, name);
1267 if (unlikely(!dentry))
1268 return ERR_PTR(-ENOMEM);
1269
1270 *need_lookup = true;
1271 }
1272 return dentry;
1273 }
1274
1275 /*
1276 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1277 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1278 *
1279 * dir->d_inode->i_mutex must be held
1280 */
1281 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1282 unsigned int flags)
1283 {
1284 struct dentry *old;
1285
1286 /* Don't create child dentry for a dead directory. */
1287 if (unlikely(IS_DEADDIR(dir))) {
1288 dput(dentry);
1289 return ERR_PTR(-ENOENT);
1290 }
1291
1292 old = dir->i_op->lookup(dir, dentry, flags);
1293 if (unlikely(old)) {
1294 dput(dentry);
1295 dentry = old;
1296 }
1297 return dentry;
1298 }
1299
1300 static struct dentry *__lookup_hash(struct qstr *name,
1301 struct dentry *base, unsigned int flags)
1302 {
1303 bool need_lookup;
1304 struct dentry *dentry;
1305
1306 dentry = lookup_dcache(name, base, flags, &need_lookup);
1307 if (!need_lookup)
1308 return dentry;
1309
1310 return lookup_real(base->d_inode, dentry, flags);
1311 }
1312
1313 /*
1314 * It's more convoluted than I'd like it to be, but... it's still fairly
1315 * small and for now I'd prefer to have fast path as straight as possible.
1316 * It _is_ time-critical.
1317 */
1318 static int lookup_fast(struct nameidata *nd, struct qstr *name,
1319 struct path *path, struct inode **inode)
1320 {
1321 struct vfsmount *mnt = nd->path.mnt;
1322 struct dentry *dentry, *parent = nd->path.dentry;
1323 int need_reval = 1;
1324 int status = 1;
1325 int err;
1326
1327 /*
1328 * Rename seqlock is not required here because in the off chance
1329 * of a false negative due to a concurrent rename, we're going to
1330 * do the non-racy lookup, below.
1331 */
1332 if (nd->flags & LOOKUP_RCU) {
1333 unsigned seq;
1334 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
1335 if (!dentry)
1336 goto unlazy;
1337
1338 /*
1339 * This sequence count validates that the inode matches
1340 * the dentry name information from lookup.
1341 */
1342 *inode = dentry->d_inode;
1343 if (read_seqcount_retry(&dentry->d_seq, seq))
1344 return -ECHILD;
1345
1346 /*
1347 * This sequence count validates that the parent had no
1348 * changes while we did the lookup of the dentry above.
1349 *
1350 * The memory barrier in read_seqcount_begin of child is
1351 * enough, we can use __read_seqcount_retry here.
1352 */
1353 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1354 return -ECHILD;
1355 nd->seq = seq;
1356
1357 if (unlikely(d_need_lookup(dentry)))
1358 goto unlazy;
1359 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1360 status = d_revalidate(dentry, nd->flags);
1361 if (unlikely(status <= 0)) {
1362 if (status != -ECHILD)
1363 need_reval = 0;
1364 goto unlazy;
1365 }
1366 }
1367 path->mnt = mnt;
1368 path->dentry = dentry;
1369 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1370 goto unlazy;
1371 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1372 goto unlazy;
1373 return 0;
1374 unlazy:
1375 if (unlazy_walk(nd, dentry))
1376 return -ECHILD;
1377 } else {
1378 dentry = __d_lookup(parent, name);
1379 }
1380
1381 if (unlikely(!dentry))
1382 goto need_lookup;
1383
1384 if (unlikely(d_need_lookup(dentry))) {
1385 dput(dentry);
1386 goto need_lookup;
1387 }
1388
1389 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1390 status = d_revalidate(dentry, nd->flags);
1391 if (unlikely(status <= 0)) {
1392 if (status < 0) {
1393 dput(dentry);
1394 return status;
1395 }
1396 if (!d_invalidate(dentry)) {
1397 dput(dentry);
1398 goto need_lookup;
1399 }
1400 }
1401
1402 path->mnt = mnt;
1403 path->dentry = dentry;
1404 err = follow_managed(path, nd->flags);
1405 if (unlikely(err < 0)) {
1406 path_put_conditional(path, nd);
1407 return err;
1408 }
1409 if (err)
1410 nd->flags |= LOOKUP_JUMPED;
1411 *inode = path->dentry->d_inode;
1412 return 0;
1413
1414 need_lookup:
1415 return 1;
1416 }
1417
1418 /* Fast lookup failed, do it the slow way */
1419 static int lookup_slow(struct nameidata *nd, struct qstr *name,
1420 struct path *path)
1421 {
1422 struct dentry *dentry, *parent;
1423 int err;
1424
1425 parent = nd->path.dentry;
1426 BUG_ON(nd->inode != parent->d_inode);
1427
1428 mutex_lock(&parent->d_inode->i_mutex);
1429 dentry = __lookup_hash(name, parent, nd->flags);
1430 mutex_unlock(&parent->d_inode->i_mutex);
1431 if (IS_ERR(dentry))
1432 return PTR_ERR(dentry);
1433 path->mnt = nd->path.mnt;
1434 path->dentry = dentry;
1435 err = follow_managed(path, nd->flags);
1436 if (unlikely(err < 0)) {
1437 path_put_conditional(path, nd);
1438 return err;
1439 }
1440 if (err)
1441 nd->flags |= LOOKUP_JUMPED;
1442 return 0;
1443 }
1444
1445 static inline int may_lookup(struct nameidata *nd)
1446 {
1447 if (nd->flags & LOOKUP_RCU) {
1448 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1449 if (err != -ECHILD)
1450 return err;
1451 if (unlazy_walk(nd, NULL))
1452 return -ECHILD;
1453 }
1454 return inode_permission(nd->inode, MAY_EXEC);
1455 }
1456
1457 static inline int handle_dots(struct nameidata *nd, int type)
1458 {
1459 if (type == LAST_DOTDOT) {
1460 if (nd->flags & LOOKUP_RCU) {
1461 if (follow_dotdot_rcu(nd))
1462 return -ECHILD;
1463 } else
1464 follow_dotdot(nd);
1465 }
1466 return 0;
1467 }
1468
1469 static void terminate_walk(struct nameidata *nd)
1470 {
1471 if (!(nd->flags & LOOKUP_RCU)) {
1472 path_put(&nd->path);
1473 } else {
1474 nd->flags &= ~LOOKUP_RCU;
1475 if (!(nd->flags & LOOKUP_ROOT))
1476 nd->root.mnt = NULL;
1477 unlock_rcu_walk();
1478 }
1479 }
1480
1481 /*
1482 * Do we need to follow links? We _really_ want to be able
1483 * to do this check without having to look at inode->i_op,
1484 * so we keep a cache of "no, this doesn't need follow_link"
1485 * for the common case.
1486 */
1487 static inline int should_follow_link(struct inode *inode, int follow)
1488 {
1489 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1490 if (likely(inode->i_op->follow_link))
1491 return follow;
1492
1493 /* This gets set once for the inode lifetime */
1494 spin_lock(&inode->i_lock);
1495 inode->i_opflags |= IOP_NOFOLLOW;
1496 spin_unlock(&inode->i_lock);
1497 }
1498 return 0;
1499 }
1500
1501 static inline int walk_component(struct nameidata *nd, struct path *path,
1502 struct qstr *name, int type, int follow)
1503 {
1504 struct inode *inode;
1505 int err;
1506 /*
1507 * "." and ".." are special - ".." especially so because it has
1508 * to be able to know about the current root directory and
1509 * parent relationships.
1510 */
1511 if (unlikely(type != LAST_NORM))
1512 return handle_dots(nd, type);
1513 err = lookup_fast(nd, name, path, &inode);
1514 if (unlikely(err)) {
1515 if (err < 0)
1516 goto out_err;
1517
1518 err = lookup_slow(nd, name, path);
1519 if (err < 0)
1520 goto out_err;
1521
1522 inode = path->dentry->d_inode;
1523 }
1524 err = -ENOENT;
1525 if (!inode)
1526 goto out_path_put;
1527
1528 if (should_follow_link(inode, follow)) {
1529 if (nd->flags & LOOKUP_RCU) {
1530 if (unlikely(unlazy_walk(nd, path->dentry))) {
1531 err = -ECHILD;
1532 goto out_err;
1533 }
1534 }
1535 BUG_ON(inode != path->dentry->d_inode);
1536 return 1;
1537 }
1538 path_to_nameidata(path, nd);
1539 nd->inode = inode;
1540 return 0;
1541
1542 out_path_put:
1543 path_to_nameidata(path, nd);
1544 out_err:
1545 terminate_walk(nd);
1546 return err;
1547 }
1548
1549 /*
1550 * This limits recursive symlink follows to 8, while
1551 * limiting consecutive symlinks to 40.
1552 *
1553 * Without that kind of total limit, nasty chains of consecutive
1554 * symlinks can cause almost arbitrarily long lookups.
1555 */
1556 static inline int nested_symlink(struct path *path, struct nameidata *nd)
1557 {
1558 int res;
1559
1560 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1561 path_put_conditional(path, nd);
1562 path_put(&nd->path);
1563 return -ELOOP;
1564 }
1565 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1566
1567 nd->depth++;
1568 current->link_count++;
1569
1570 do {
1571 struct path link = *path;
1572 void *cookie;
1573
1574 res = follow_link(&link, nd, &cookie);
1575 if (res)
1576 break;
1577 res = walk_component(nd, path, &nd->last,
1578 nd->last_type, LOOKUP_FOLLOW);
1579 put_link(nd, &link, cookie);
1580 } while (res > 0);
1581
1582 current->link_count--;
1583 nd->depth--;
1584 return res;
1585 }
1586
1587 /*
1588 * We really don't want to look at inode->i_op->lookup
1589 * when we don't have to. So we keep a cache bit in
1590 * the inode ->i_opflags field that says "yes, we can
1591 * do lookup on this inode".
1592 */
1593 static inline int can_lookup(struct inode *inode)
1594 {
1595 if (likely(inode->i_opflags & IOP_LOOKUP))
1596 return 1;
1597 if (likely(!inode->i_op->lookup))
1598 return 0;
1599
1600 /* We do this once for the lifetime of the inode */
1601 spin_lock(&inode->i_lock);
1602 inode->i_opflags |= IOP_LOOKUP;
1603 spin_unlock(&inode->i_lock);
1604 return 1;
1605 }
1606
1607 /*
1608 * We can do the critical dentry name comparison and hashing
1609 * operations one word at a time, but we are limited to:
1610 *
1611 * - Architectures with fast unaligned word accesses. We could
1612 * do a "get_unaligned()" if this helps and is sufficiently
1613 * fast.
1614 *
1615 * - Little-endian machines (so that we can generate the mask
1616 * of low bytes efficiently). Again, we *could* do a byte
1617 * swapping load on big-endian architectures if that is not
1618 * expensive enough to make the optimization worthless.
1619 *
1620 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1621 * do not trap on the (extremely unlikely) case of a page
1622 * crossing operation.
1623 *
1624 * - Furthermore, we need an efficient 64-bit compile for the
1625 * 64-bit case in order to generate the "number of bytes in
1626 * the final mask". Again, that could be replaced with a
1627 * efficient population count instruction or similar.
1628 */
1629 #ifdef CONFIG_DCACHE_WORD_ACCESS
1630
1631 #include <asm/word-at-a-time.h>
1632
1633 #ifdef CONFIG_64BIT
1634
1635 static inline unsigned int fold_hash(unsigned long hash)
1636 {
1637 hash += hash >> (8*sizeof(int));
1638 return hash;
1639 }
1640
1641 #else /* 32-bit case */
1642
1643 #define fold_hash(x) (x)
1644
1645 #endif
1646
1647 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1648 {
1649 unsigned long a, mask;
1650 unsigned long hash = 0;
1651
1652 for (;;) {
1653 a = load_unaligned_zeropad(name);
1654 if (len < sizeof(unsigned long))
1655 break;
1656 hash += a;
1657 hash *= 9;
1658 name += sizeof(unsigned long);
1659 len -= sizeof(unsigned long);
1660 if (!len)
1661 goto done;
1662 }
1663 mask = ~(~0ul << len*8);
1664 hash += mask & a;
1665 done:
1666 return fold_hash(hash);
1667 }
1668 EXPORT_SYMBOL(full_name_hash);
1669
1670 /*
1671 * Calculate the length and hash of the path component, and
1672 * return the length of the component;
1673 */
1674 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1675 {
1676 unsigned long a, b, adata, bdata, mask, hash, len;
1677 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1678
1679 hash = a = 0;
1680 len = -sizeof(unsigned long);
1681 do {
1682 hash = (hash + a) * 9;
1683 len += sizeof(unsigned long);
1684 a = load_unaligned_zeropad(name+len);
1685 b = a ^ REPEAT_BYTE('/');
1686 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1687
1688 adata = prep_zero_mask(a, adata, &constants);
1689 bdata = prep_zero_mask(b, bdata, &constants);
1690
1691 mask = create_zero_mask(adata | bdata);
1692
1693 hash += a & zero_bytemask(mask);
1694 *hashp = fold_hash(hash);
1695
1696 return len + find_zero(mask);
1697 }
1698
1699 #else
1700
1701 unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1702 {
1703 unsigned long hash = init_name_hash();
1704 while (len--)
1705 hash = partial_name_hash(*name++, hash);
1706 return end_name_hash(hash);
1707 }
1708 EXPORT_SYMBOL(full_name_hash);
1709
1710 /*
1711 * We know there's a real path component here of at least
1712 * one character.
1713 */
1714 static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1715 {
1716 unsigned long hash = init_name_hash();
1717 unsigned long len = 0, c;
1718
1719 c = (unsigned char)*name;
1720 do {
1721 len++;
1722 hash = partial_name_hash(c, hash);
1723 c = (unsigned char)name[len];
1724 } while (c && c != '/');
1725 *hashp = end_name_hash(hash);
1726 return len;
1727 }
1728
1729 #endif
1730
1731 /*
1732 * Name resolution.
1733 * This is the basic name resolution function, turning a pathname into
1734 * the final dentry. We expect 'base' to be positive and a directory.
1735 *
1736 * Returns 0 and nd will have valid dentry and mnt on success.
1737 * Returns error and drops reference to input namei data on failure.
1738 */
1739 static int link_path_walk(const char *name, struct nameidata *nd)
1740 {
1741 struct path next;
1742 int err;
1743
1744 while (*name=='/')
1745 name++;
1746 if (!*name)
1747 return 0;
1748
1749 /* At this point we know we have a real path component. */
1750 for(;;) {
1751 struct qstr this;
1752 long len;
1753 int type;
1754
1755 err = may_lookup(nd);
1756 if (err)
1757 break;
1758
1759 len = hash_name(name, &this.hash);
1760 this.name = name;
1761 this.len = len;
1762
1763 type = LAST_NORM;
1764 if (name[0] == '.') switch (len) {
1765 case 2:
1766 if (name[1] == '.') {
1767 type = LAST_DOTDOT;
1768 nd->flags |= LOOKUP_JUMPED;
1769 }
1770 break;
1771 case 1:
1772 type = LAST_DOT;
1773 }
1774 if (likely(type == LAST_NORM)) {
1775 struct dentry *parent = nd->path.dentry;
1776 nd->flags &= ~LOOKUP_JUMPED;
1777 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1778 err = parent->d_op->d_hash(parent, nd->inode,
1779 &this);
1780 if (err < 0)
1781 break;
1782 }
1783 }
1784
1785 if (!name[len])
1786 goto last_component;
1787 /*
1788 * If it wasn't NUL, we know it was '/'. Skip that
1789 * slash, and continue until no more slashes.
1790 */
1791 do {
1792 len++;
1793 } while (unlikely(name[len] == '/'));
1794 if (!name[len])
1795 goto last_component;
1796 name += len;
1797
1798 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1799 if (err < 0)
1800 return err;
1801
1802 if (err) {
1803 err = nested_symlink(&next, nd);
1804 if (err)
1805 return err;
1806 }
1807 if (can_lookup(nd->inode))
1808 continue;
1809 err = -ENOTDIR;
1810 break;
1811 /* here ends the main loop */
1812
1813 last_component:
1814 nd->last = this;
1815 nd->last_type = type;
1816 return 0;
1817 }
1818 terminate_walk(nd);
1819 return err;
1820 }
1821
1822 static int path_init(int dfd, const char *name, unsigned int flags,
1823 struct nameidata *nd, struct file **fp)
1824 {
1825 int retval = 0;
1826
1827 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1828 nd->flags = flags | LOOKUP_JUMPED;
1829 nd->depth = 0;
1830 if (flags & LOOKUP_ROOT) {
1831 struct inode *inode = nd->root.dentry->d_inode;
1832 if (*name) {
1833 if (!inode->i_op->lookup)
1834 return -ENOTDIR;
1835 retval = inode_permission(inode, MAY_EXEC);
1836 if (retval)
1837 return retval;
1838 }
1839 nd->path = nd->root;
1840 nd->inode = inode;
1841 if (flags & LOOKUP_RCU) {
1842 lock_rcu_walk();
1843 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1844 } else {
1845 path_get(&nd->path);
1846 }
1847 return 0;
1848 }
1849
1850 nd->root.mnt = NULL;
1851
1852 if (*name=='/') {
1853 if (flags & LOOKUP_RCU) {
1854 lock_rcu_walk();
1855 set_root_rcu(nd);
1856 } else {
1857 set_root(nd);
1858 path_get(&nd->root);
1859 }
1860 nd->path = nd->root;
1861 } else if (dfd == AT_FDCWD) {
1862 if (flags & LOOKUP_RCU) {
1863 struct fs_struct *fs = current->fs;
1864 unsigned seq;
1865
1866 lock_rcu_walk();
1867
1868 do {
1869 seq = read_seqcount_begin(&fs->seq);
1870 nd->path = fs->pwd;
1871 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1872 } while (read_seqcount_retry(&fs->seq, seq));
1873 } else {
1874 get_fs_pwd(current->fs, &nd->path);
1875 }
1876 } else {
1877 struct fd f = fdget_raw(dfd);
1878 struct dentry *dentry;
1879
1880 if (!f.file)
1881 return -EBADF;
1882
1883 dentry = f.file->f_path.dentry;
1884
1885 if (*name) {
1886 if (!S_ISDIR(dentry->d_inode->i_mode)) {
1887 fdput(f);
1888 return -ENOTDIR;
1889 }
1890
1891 retval = inode_permission(dentry->d_inode, MAY_EXEC);
1892 if (retval) {
1893 fdput(f);
1894 return retval;
1895 }
1896 }
1897
1898 nd->path = f.file->f_path;
1899 if (flags & LOOKUP_RCU) {
1900 if (f.need_put)
1901 *fp = f.file;
1902 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1903 lock_rcu_walk();
1904 } else {
1905 path_get(&nd->path);
1906 fdput(f);
1907 }
1908 }
1909
1910 nd->inode = nd->path.dentry->d_inode;
1911 return 0;
1912 }
1913
1914 static inline int lookup_last(struct nameidata *nd, struct path *path)
1915 {
1916 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1917 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1918
1919 nd->flags &= ~LOOKUP_PARENT;
1920 return walk_component(nd, path, &nd->last, nd->last_type,
1921 nd->flags & LOOKUP_FOLLOW);
1922 }
1923
1924 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1925 static int path_lookupat(int dfd, const char *name,
1926 unsigned int flags, struct nameidata *nd)
1927 {
1928 struct file *base = NULL;
1929 struct path path;
1930 int err;
1931
1932 /*
1933 * Path walking is largely split up into 2 different synchronisation
1934 * schemes, rcu-walk and ref-walk (explained in
1935 * Documentation/filesystems/path-lookup.txt). These share much of the
1936 * path walk code, but some things particularly setup, cleanup, and
1937 * following mounts are sufficiently divergent that functions are
1938 * duplicated. Typically there is a function foo(), and its RCU
1939 * analogue, foo_rcu().
1940 *
1941 * -ECHILD is the error number of choice (just to avoid clashes) that
1942 * is returned if some aspect of an rcu-walk fails. Such an error must
1943 * be handled by restarting a traditional ref-walk (which will always
1944 * be able to complete).
1945 */
1946 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1947
1948 if (unlikely(err))
1949 return err;
1950
1951 current->total_link_count = 0;
1952 err = link_path_walk(name, nd);
1953
1954 if (!err && !(flags & LOOKUP_PARENT)) {
1955 err = lookup_last(nd, &path);
1956 while (err > 0) {
1957 void *cookie;
1958 struct path link = path;
1959 err = may_follow_link(&link, nd);
1960 if (unlikely(err))
1961 break;
1962 nd->flags |= LOOKUP_PARENT;
1963 err = follow_link(&link, nd, &cookie);
1964 if (err)
1965 break;
1966 err = lookup_last(nd, &path);
1967 put_link(nd, &link, cookie);
1968 }
1969 }
1970
1971 if (!err)
1972 err = complete_walk(nd);
1973
1974 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1975 if (!nd->inode->i_op->lookup) {
1976 path_put(&nd->path);
1977 err = -ENOTDIR;
1978 }
1979 }
1980
1981 if (base)
1982 fput(base);
1983
1984 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1985 path_put(&nd->root);
1986 nd->root.mnt = NULL;
1987 }
1988 return err;
1989 }
1990
1991 static int filename_lookup(int dfd, struct filename *name,
1992 unsigned int flags, struct nameidata *nd)
1993 {
1994 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1995 if (unlikely(retval == -ECHILD))
1996 retval = path_lookupat(dfd, name->name, flags, nd);
1997 if (unlikely(retval == -ESTALE))
1998 retval = path_lookupat(dfd, name->name,
1999 flags | LOOKUP_REVAL, nd);
2000
2001 if (likely(!retval))
2002 audit_inode(name->name, nd->path.dentry,
2003 flags & LOOKUP_PARENT);
2004 return retval;
2005 }
2006
2007 static int do_path_lookup(int dfd, const char *name,
2008 unsigned int flags, struct nameidata *nd)
2009 {
2010 struct filename filename = { .name = name };
2011
2012 return filename_lookup(dfd, &filename, flags, nd);
2013 }
2014
2015 /* does lookup, returns the object with parent locked */
2016 struct dentry *kern_path_locked(const char *name, struct path *path)
2017 {
2018 struct nameidata nd;
2019 struct dentry *d;
2020 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2021 if (err)
2022 return ERR_PTR(err);
2023 if (nd.last_type != LAST_NORM) {
2024 path_put(&nd.path);
2025 return ERR_PTR(-EINVAL);
2026 }
2027 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2028 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
2029 if (IS_ERR(d)) {
2030 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2031 path_put(&nd.path);
2032 return d;
2033 }
2034 *path = nd.path;
2035 return d;
2036 }
2037
2038 int kern_path(const char *name, unsigned int flags, struct path *path)
2039 {
2040 struct nameidata nd;
2041 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2042 if (!res)
2043 *path = nd.path;
2044 return res;
2045 }
2046
2047 /**
2048 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2049 * @dentry: pointer to dentry of the base directory
2050 * @mnt: pointer to vfs mount of the base directory
2051 * @name: pointer to file name
2052 * @flags: lookup flags
2053 * @path: pointer to struct path to fill
2054 */
2055 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2056 const char *name, unsigned int flags,
2057 struct path *path)
2058 {
2059 struct nameidata nd;
2060 int err;
2061 nd.root.dentry = dentry;
2062 nd.root.mnt = mnt;
2063 BUG_ON(flags & LOOKUP_PARENT);
2064 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2065 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2066 if (!err)
2067 *path = nd.path;
2068 return err;
2069 }
2070
2071 /*
2072 * Restricted form of lookup. Doesn't follow links, single-component only,
2073 * needs parent already locked. Doesn't follow mounts.
2074 * SMP-safe.
2075 */
2076 static struct dentry *lookup_hash(struct nameidata *nd)
2077 {
2078 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
2079 }
2080
2081 /**
2082 * lookup_one_len - filesystem helper to lookup single pathname component
2083 * @name: pathname component to lookup
2084 * @base: base directory to lookup from
2085 * @len: maximum length @len should be interpreted to
2086 *
2087 * Note that this routine is purely a helper for filesystem usage and should
2088 * not be called by generic code. Also note that by using this function the
2089 * nameidata argument is passed to the filesystem methods and a filesystem
2090 * using this helper needs to be prepared for that.
2091 */
2092 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2093 {
2094 struct qstr this;
2095 unsigned int c;
2096 int err;
2097
2098 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2099
2100 this.name = name;
2101 this.len = len;
2102 this.hash = full_name_hash(name, len);
2103 if (!len)
2104 return ERR_PTR(-EACCES);
2105
2106 while (len--) {
2107 c = *(const unsigned char *)name++;
2108 if (c == '/' || c == '\0')
2109 return ERR_PTR(-EACCES);
2110 }
2111 /*
2112 * See if the low-level filesystem might want
2113 * to use its own hash..
2114 */
2115 if (base->d_flags & DCACHE_OP_HASH) {
2116 int err = base->d_op->d_hash(base, base->d_inode, &this);
2117 if (err < 0)
2118 return ERR_PTR(err);
2119 }
2120
2121 err = inode_permission(base->d_inode, MAY_EXEC);
2122 if (err)
2123 return ERR_PTR(err);
2124
2125 return __lookup_hash(&this, base, 0);
2126 }
2127
2128 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2129 struct path *path, int *empty)
2130 {
2131 struct nameidata nd;
2132 struct filename *tmp = getname_flags(name, flags, empty);
2133 int err = PTR_ERR(tmp);
2134 if (!IS_ERR(tmp)) {
2135
2136 BUG_ON(flags & LOOKUP_PARENT);
2137
2138 err = filename_lookup(dfd, tmp, flags, &nd);
2139 putname(tmp);
2140 if (!err)
2141 *path = nd.path;
2142 }
2143 return err;
2144 }
2145
2146 int user_path_at(int dfd, const char __user *name, unsigned flags,
2147 struct path *path)
2148 {
2149 return user_path_at_empty(dfd, name, flags, path, NULL);
2150 }
2151
2152 /*
2153 * NB: most callers don't do anything directly with the reference to the
2154 * to struct filename, but the nd->last pointer points into the name string
2155 * allocated by getname. So we must hold the reference to it until all
2156 * path-walking is complete.
2157 */
2158 static struct filename *
2159 user_path_parent(int dfd, const char __user *path, struct nameidata *nd)
2160 {
2161 struct filename *s = getname(path);
2162 int error;
2163
2164 if (IS_ERR(s))
2165 return s;
2166
2167 error = filename_lookup(dfd, s, LOOKUP_PARENT, nd);
2168 if (error) {
2169 putname(s);
2170 return ERR_PTR(error);
2171 }
2172
2173 return s;
2174 }
2175
2176 /*
2177 * It's inline, so penalty for filesystems that don't use sticky bit is
2178 * minimal.
2179 */
2180 static inline int check_sticky(struct inode *dir, struct inode *inode)
2181 {
2182 kuid_t fsuid = current_fsuid();
2183
2184 if (!(dir->i_mode & S_ISVTX))
2185 return 0;
2186 if (uid_eq(inode->i_uid, fsuid))
2187 return 0;
2188 if (uid_eq(dir->i_uid, fsuid))
2189 return 0;
2190 return !inode_capable(inode, CAP_FOWNER);
2191 }
2192
2193 /*
2194 * Check whether we can remove a link victim from directory dir, check
2195 * whether the type of victim is right.
2196 * 1. We can't do it if dir is read-only (done in permission())
2197 * 2. We should have write and exec permissions on dir
2198 * 3. We can't remove anything from append-only dir
2199 * 4. We can't do anything with immutable dir (done in permission())
2200 * 5. If the sticky bit on dir is set we should either
2201 * a. be owner of dir, or
2202 * b. be owner of victim, or
2203 * c. have CAP_FOWNER capability
2204 * 6. If the victim is append-only or immutable we can't do antyhing with
2205 * links pointing to it.
2206 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2207 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2208 * 9. We can't remove a root or mountpoint.
2209 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2210 * nfs_async_unlink().
2211 */
2212 static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
2213 {
2214 int error;
2215
2216 if (!victim->d_inode)
2217 return -ENOENT;
2218
2219 BUG_ON(victim->d_parent->d_inode != dir);
2220 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2221
2222 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2223 if (error)
2224 return error;
2225 if (IS_APPEND(dir))
2226 return -EPERM;
2227 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2228 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
2229 return -EPERM;
2230 if (isdir) {
2231 if (!S_ISDIR(victim->d_inode->i_mode))
2232 return -ENOTDIR;
2233 if (IS_ROOT(victim))
2234 return -EBUSY;
2235 } else if (S_ISDIR(victim->d_inode->i_mode))
2236 return -EISDIR;
2237 if (IS_DEADDIR(dir))
2238 return -ENOENT;
2239 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2240 return -EBUSY;
2241 return 0;
2242 }
2243
2244 /* Check whether we can create an object with dentry child in directory
2245 * dir.
2246 * 1. We can't do it if child already exists (open has special treatment for
2247 * this case, but since we are inlined it's OK)
2248 * 2. We can't do it if dir is read-only (done in permission())
2249 * 3. We should have write and exec permissions on dir
2250 * 4. We can't do it if dir is immutable (done in permission())
2251 */
2252 static inline int may_create(struct inode *dir, struct dentry *child)
2253 {
2254 if (child->d_inode)
2255 return -EEXIST;
2256 if (IS_DEADDIR(dir))
2257 return -ENOENT;
2258 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2259 }
2260
2261 /*
2262 * p1 and p2 should be directories on the same fs.
2263 */
2264 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2265 {
2266 struct dentry *p;
2267
2268 if (p1 == p2) {
2269 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2270 return NULL;
2271 }
2272
2273 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2274
2275 p = d_ancestor(p2, p1);
2276 if (p) {
2277 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2278 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2279 return p;
2280 }
2281
2282 p = d_ancestor(p1, p2);
2283 if (p) {
2284 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2285 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2286 return p;
2287 }
2288
2289 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2290 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2291 return NULL;
2292 }
2293
2294 void unlock_rename(struct dentry *p1, struct dentry *p2)
2295 {
2296 mutex_unlock(&p1->d_inode->i_mutex);
2297 if (p1 != p2) {
2298 mutex_unlock(&p2->d_inode->i_mutex);
2299 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2300 }
2301 }
2302
2303 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2304 bool want_excl)
2305 {
2306 int error = may_create(dir, dentry);
2307 if (error)
2308 return error;
2309
2310 if (!dir->i_op->create)
2311 return -EACCES; /* shouldn't it be ENOSYS? */
2312 mode &= S_IALLUGO;
2313 mode |= S_IFREG;
2314 error = security_inode_create(dir, dentry, mode);
2315 if (error)
2316 return error;
2317 error = dir->i_op->create(dir, dentry, mode, want_excl);
2318 if (!error)
2319 fsnotify_create(dir, dentry);
2320 return error;
2321 }
2322
2323 static int may_open(struct path *path, int acc_mode, int flag)
2324 {
2325 struct dentry *dentry = path->dentry;
2326 struct inode *inode = dentry->d_inode;
2327 int error;
2328
2329 /* O_PATH? */
2330 if (!acc_mode)
2331 return 0;
2332
2333 if (!inode)
2334 return -ENOENT;
2335
2336 switch (inode->i_mode & S_IFMT) {
2337 case S_IFLNK:
2338 return -ELOOP;
2339 case S_IFDIR:
2340 if (acc_mode & MAY_WRITE)
2341 return -EISDIR;
2342 break;
2343 case S_IFBLK:
2344 case S_IFCHR:
2345 if (path->mnt->mnt_flags & MNT_NODEV)
2346 return -EACCES;
2347 /*FALLTHRU*/
2348 case S_IFIFO:
2349 case S_IFSOCK:
2350 flag &= ~O_TRUNC;
2351 break;
2352 }
2353
2354 error = inode_permission(inode, acc_mode);
2355 if (error)
2356 return error;
2357
2358 /*
2359 * An append-only file must be opened in append mode for writing.
2360 */
2361 if (IS_APPEND(inode)) {
2362 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2363 return -EPERM;
2364 if (flag & O_TRUNC)
2365 return -EPERM;
2366 }
2367
2368 /* O_NOATIME can only be set by the owner or superuser */
2369 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2370 return -EPERM;
2371
2372 return 0;
2373 }
2374
2375 static int handle_truncate(struct file *filp)
2376 {
2377 struct path *path = &filp->f_path;
2378 struct inode *inode = path->dentry->d_inode;
2379 int error = get_write_access(inode);
2380 if (error)
2381 return error;
2382 /*
2383 * Refuse to truncate files with mandatory locks held on them.
2384 */
2385 error = locks_verify_locked(inode);
2386 if (!error)
2387 error = security_path_truncate(path);
2388 if (!error) {
2389 error = do_truncate(path->dentry, 0,
2390 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2391 filp);
2392 }
2393 put_write_access(inode);
2394 return error;
2395 }
2396
2397 static inline int open_to_namei_flags(int flag)
2398 {
2399 if ((flag & O_ACCMODE) == 3)
2400 flag--;
2401 return flag;
2402 }
2403
2404 static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2405 {
2406 int error = security_path_mknod(dir, dentry, mode, 0);
2407 if (error)
2408 return error;
2409
2410 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2411 if (error)
2412 return error;
2413
2414 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2415 }
2416
2417 /*
2418 * Attempt to atomically look up, create and open a file from a negative
2419 * dentry.
2420 *
2421 * Returns 0 if successful. The file will have been created and attached to
2422 * @file by the filesystem calling finish_open().
2423 *
2424 * Returns 1 if the file was looked up only or didn't need creating. The
2425 * caller will need to perform the open themselves. @path will have been
2426 * updated to point to the new dentry. This may be negative.
2427 *
2428 * Returns an error code otherwise.
2429 */
2430 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2431 struct path *path, struct file *file,
2432 const struct open_flags *op,
2433 bool got_write, bool need_lookup,
2434 int *opened)
2435 {
2436 struct inode *dir = nd->path.dentry->d_inode;
2437 unsigned open_flag = open_to_namei_flags(op->open_flag);
2438 umode_t mode;
2439 int error;
2440 int acc_mode;
2441 int create_error = 0;
2442 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2443
2444 BUG_ON(dentry->d_inode);
2445
2446 /* Don't create child dentry for a dead directory. */
2447 if (unlikely(IS_DEADDIR(dir))) {
2448 error = -ENOENT;
2449 goto out;
2450 }
2451
2452 mode = op->mode;
2453 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2454 mode &= ~current_umask();
2455
2456 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2457 open_flag &= ~O_TRUNC;
2458 *opened |= FILE_CREATED;
2459 }
2460
2461 /*
2462 * Checking write permission is tricky, bacuse we don't know if we are
2463 * going to actually need it: O_CREAT opens should work as long as the
2464 * file exists. But checking existence breaks atomicity. The trick is
2465 * to check access and if not granted clear O_CREAT from the flags.
2466 *
2467 * Another problem is returing the "right" error value (e.g. for an
2468 * O_EXCL open we want to return EEXIST not EROFS).
2469 */
2470 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2471 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2472 if (!(open_flag & O_CREAT)) {
2473 /*
2474 * No O_CREATE -> atomicity not a requirement -> fall
2475 * back to lookup + open
2476 */
2477 goto no_open;
2478 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2479 /* Fall back and fail with the right error */
2480 create_error = -EROFS;
2481 goto no_open;
2482 } else {
2483 /* No side effects, safe to clear O_CREAT */
2484 create_error = -EROFS;
2485 open_flag &= ~O_CREAT;
2486 }
2487 }
2488
2489 if (open_flag & O_CREAT) {
2490 error = may_o_create(&nd->path, dentry, mode);
2491 if (error) {
2492 create_error = error;
2493 if (open_flag & O_EXCL)
2494 goto no_open;
2495 open_flag &= ~O_CREAT;
2496 }
2497 }
2498
2499 if (nd->flags & LOOKUP_DIRECTORY)
2500 open_flag |= O_DIRECTORY;
2501
2502 file->f_path.dentry = DENTRY_NOT_SET;
2503 file->f_path.mnt = nd->path.mnt;
2504 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
2505 opened);
2506 if (error < 0) {
2507 if (create_error && error == -ENOENT)
2508 error = create_error;
2509 goto out;
2510 }
2511
2512 acc_mode = op->acc_mode;
2513 if (*opened & FILE_CREATED) {
2514 fsnotify_create(dir, dentry);
2515 acc_mode = MAY_OPEN;
2516 }
2517
2518 if (error) { /* returned 1, that is */
2519 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2520 error = -EIO;
2521 goto out;
2522 }
2523 if (file->f_path.dentry) {
2524 dput(dentry);
2525 dentry = file->f_path.dentry;
2526 }
2527 if (create_error && dentry->d_inode == NULL) {
2528 error = create_error;
2529 goto out;
2530 }
2531 goto looked_up;
2532 }
2533
2534 /*
2535 * We didn't have the inode before the open, so check open permission
2536 * here.
2537 */
2538 error = may_open(&file->f_path, acc_mode, open_flag);
2539 if (error)
2540 fput(file);
2541
2542 out:
2543 dput(dentry);
2544 return error;
2545
2546 no_open:
2547 if (need_lookup) {
2548 dentry = lookup_real(dir, dentry, nd->flags);
2549 if (IS_ERR(dentry))
2550 return PTR_ERR(dentry);
2551
2552 if (create_error) {
2553 int open_flag = op->open_flag;
2554
2555 error = create_error;
2556 if ((open_flag & O_EXCL)) {
2557 if (!dentry->d_inode)
2558 goto out;
2559 } else if (!dentry->d_inode) {
2560 goto out;
2561 } else if ((open_flag & O_TRUNC) &&
2562 S_ISREG(dentry->d_inode->i_mode)) {
2563 goto out;
2564 }
2565 /* will fail later, go on to get the right error */
2566 }
2567 }
2568 looked_up:
2569 path->dentry = dentry;
2570 path->mnt = nd->path.mnt;
2571 return 1;
2572 }
2573
2574 /*
2575 * Look up and maybe create and open the last component.
2576 *
2577 * Must be called with i_mutex held on parent.
2578 *
2579 * Returns 0 if the file was successfully atomically created (if necessary) and
2580 * opened. In this case the file will be returned attached to @file.
2581 *
2582 * Returns 1 if the file was not completely opened at this time, though lookups
2583 * and creations will have been performed and the dentry returned in @path will
2584 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2585 * specified then a negative dentry may be returned.
2586 *
2587 * An error code is returned otherwise.
2588 *
2589 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2590 * cleared otherwise prior to returning.
2591 */
2592 static int lookup_open(struct nameidata *nd, struct path *path,
2593 struct file *file,
2594 const struct open_flags *op,
2595 bool got_write, int *opened)
2596 {
2597 struct dentry *dir = nd->path.dentry;
2598 struct inode *dir_inode = dir->d_inode;
2599 struct dentry *dentry;
2600 int error;
2601 bool need_lookup;
2602
2603 *opened &= ~FILE_CREATED;
2604 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2605 if (IS_ERR(dentry))
2606 return PTR_ERR(dentry);
2607
2608 /* Cached positive dentry: will open in f_op->open */
2609 if (!need_lookup && dentry->d_inode)
2610 goto out_no_open;
2611
2612 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2613 return atomic_open(nd, dentry, path, file, op, got_write,
2614 need_lookup, opened);
2615 }
2616
2617 if (need_lookup) {
2618 BUG_ON(dentry->d_inode);
2619
2620 dentry = lookup_real(dir_inode, dentry, nd->flags);
2621 if (IS_ERR(dentry))
2622 return PTR_ERR(dentry);
2623 }
2624
2625 /* Negative dentry, just create the file */
2626 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2627 umode_t mode = op->mode;
2628 if (!IS_POSIXACL(dir->d_inode))
2629 mode &= ~current_umask();
2630 /*
2631 * This write is needed to ensure that a
2632 * rw->ro transition does not occur between
2633 * the time when the file is created and when
2634 * a permanent write count is taken through
2635 * the 'struct file' in finish_open().
2636 */
2637 if (!got_write) {
2638 error = -EROFS;
2639 goto out_dput;
2640 }
2641 *opened |= FILE_CREATED;
2642 error = security_path_mknod(&nd->path, dentry, mode, 0);
2643 if (error)
2644 goto out_dput;
2645 error = vfs_create(dir->d_inode, dentry, mode,
2646 nd->flags & LOOKUP_EXCL);
2647 if (error)
2648 goto out_dput;
2649 }
2650 out_no_open:
2651 path->dentry = dentry;
2652 path->mnt = nd->path.mnt;
2653 return 1;
2654
2655 out_dput:
2656 dput(dentry);
2657 return error;
2658 }
2659
2660 /*
2661 * Handle the last step of open()
2662 */
2663 static int do_last(struct nameidata *nd, struct path *path,
2664 struct file *file, const struct open_flags *op,
2665 int *opened, struct filename *name)
2666 {
2667 struct dentry *dir = nd->path.dentry;
2668 int open_flag = op->open_flag;
2669 bool will_truncate = (open_flag & O_TRUNC) != 0;
2670 bool got_write = false;
2671 int acc_mode = op->acc_mode;
2672 struct inode *inode;
2673 bool symlink_ok = false;
2674 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2675 bool retried = false;
2676 int error;
2677 const char *pathname = name->name;
2678
2679 nd->flags &= ~LOOKUP_PARENT;
2680 nd->flags |= op->intent;
2681
2682 switch (nd->last_type) {
2683 case LAST_DOTDOT:
2684 case LAST_DOT:
2685 error = handle_dots(nd, nd->last_type);
2686 if (error)
2687 return error;
2688 /* fallthrough */
2689 case LAST_ROOT:
2690 error = complete_walk(nd);
2691 if (error)
2692 return error;
2693 audit_inode(pathname, nd->path.dentry, 0);
2694 if (open_flag & O_CREAT) {
2695 error = -EISDIR;
2696 goto out;
2697 }
2698 goto finish_open;
2699 case LAST_BIND:
2700 error = complete_walk(nd);
2701 if (error)
2702 return error;
2703 audit_inode(pathname, dir, 0);
2704 goto finish_open;
2705 }
2706
2707 if (!(open_flag & O_CREAT)) {
2708 if (nd->last.name[nd->last.len])
2709 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2710 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2711 symlink_ok = true;
2712 /* we _can_ be in RCU mode here */
2713 error = lookup_fast(nd, &nd->last, path, &inode);
2714 if (likely(!error))
2715 goto finish_lookup;
2716
2717 if (error < 0)
2718 goto out;
2719
2720 BUG_ON(nd->inode != dir->d_inode);
2721 } else {
2722 /* create side of things */
2723 /*
2724 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2725 * has been cleared when we got to the last component we are
2726 * about to look up
2727 */
2728 error = complete_walk(nd);
2729 if (error)
2730 return error;
2731
2732 audit_inode(pathname, dir, 0);
2733 error = -EISDIR;
2734 /* trailing slashes? */
2735 if (nd->last.name[nd->last.len])
2736 goto out;
2737 }
2738
2739 retry_lookup:
2740 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2741 error = mnt_want_write(nd->path.mnt);
2742 if (!error)
2743 got_write = true;
2744 /*
2745 * do _not_ fail yet - we might not need that or fail with
2746 * a different error; let lookup_open() decide; we'll be
2747 * dropping this one anyway.
2748 */
2749 }
2750 mutex_lock(&dir->d_inode->i_mutex);
2751 error = lookup_open(nd, path, file, op, got_write, opened);
2752 mutex_unlock(&dir->d_inode->i_mutex);
2753
2754 if (error <= 0) {
2755 if (error)
2756 goto out;
2757
2758 if ((*opened & FILE_CREATED) ||
2759 !S_ISREG(file->f_path.dentry->d_inode->i_mode))
2760 will_truncate = false;
2761
2762 audit_inode(pathname, file->f_path.dentry, 0);
2763 goto opened;
2764 }
2765
2766 if (*opened & FILE_CREATED) {
2767 /* Don't check for write permission, don't truncate */
2768 open_flag &= ~O_TRUNC;
2769 will_truncate = false;
2770 acc_mode = MAY_OPEN;
2771 path_to_nameidata(path, nd);
2772 goto finish_open_created;
2773 }
2774
2775 /*
2776 * create/update audit record if it already exists.
2777 */
2778 if (path->dentry->d_inode)
2779 audit_inode(pathname, path->dentry, 0);
2780
2781 /*
2782 * If atomic_open() acquired write access it is dropped now due to
2783 * possible mount and symlink following (this might be optimized away if
2784 * necessary...)
2785 */
2786 if (got_write) {
2787 mnt_drop_write(nd->path.mnt);
2788 got_write = false;
2789 }
2790
2791 error = -EEXIST;
2792 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2793 goto exit_dput;
2794
2795 error = follow_managed(path, nd->flags);
2796 if (error < 0)
2797 goto exit_dput;
2798
2799 if (error)
2800 nd->flags |= LOOKUP_JUMPED;
2801
2802 BUG_ON(nd->flags & LOOKUP_RCU);
2803 inode = path->dentry->d_inode;
2804 finish_lookup:
2805 /* we _can_ be in RCU mode here */
2806 error = -ENOENT;
2807 if (!inode) {
2808 path_to_nameidata(path, nd);
2809 goto out;
2810 }
2811
2812 if (should_follow_link(inode, !symlink_ok)) {
2813 if (nd->flags & LOOKUP_RCU) {
2814 if (unlikely(unlazy_walk(nd, path->dentry))) {
2815 error = -ECHILD;
2816 goto out;
2817 }
2818 }
2819 BUG_ON(inode != path->dentry->d_inode);
2820 return 1;
2821 }
2822
2823 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2824 path_to_nameidata(path, nd);
2825 } else {
2826 save_parent.dentry = nd->path.dentry;
2827 save_parent.mnt = mntget(path->mnt);
2828 nd->path.dentry = path->dentry;
2829
2830 }
2831 nd->inode = inode;
2832 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2833 error = complete_walk(nd);
2834 if (error) {
2835 path_put(&save_parent);
2836 return error;
2837 }
2838 error = -EISDIR;
2839 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
2840 goto out;
2841 error = -ENOTDIR;
2842 if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
2843 goto out;
2844 audit_inode(pathname, nd->path.dentry, 0);
2845 finish_open:
2846 if (!S_ISREG(nd->inode->i_mode))
2847 will_truncate = false;
2848
2849 if (will_truncate) {
2850 error = mnt_want_write(nd->path.mnt);
2851 if (error)
2852 goto out;
2853 got_write = true;
2854 }
2855 finish_open_created:
2856 error = may_open(&nd->path, acc_mode, open_flag);
2857 if (error)
2858 goto out;
2859 file->f_path.mnt = nd->path.mnt;
2860 error = finish_open(file, nd->path.dentry, NULL, opened);
2861 if (error) {
2862 if (error == -EOPENSTALE)
2863 goto stale_open;
2864 goto out;
2865 }
2866 opened:
2867 error = open_check_o_direct(file);
2868 if (error)
2869 goto exit_fput;
2870 error = ima_file_check(file, op->acc_mode);
2871 if (error)
2872 goto exit_fput;
2873
2874 if (will_truncate) {
2875 error = handle_truncate(file);
2876 if (error)
2877 goto exit_fput;
2878 }
2879 out:
2880 if (got_write)
2881 mnt_drop_write(nd->path.mnt);
2882 path_put(&save_parent);
2883 terminate_walk(nd);
2884 return error;
2885
2886 exit_dput:
2887 path_put_conditional(path, nd);
2888 goto out;
2889 exit_fput:
2890 fput(file);
2891 goto out;
2892
2893 stale_open:
2894 /* If no saved parent or already retried then can't retry */
2895 if (!save_parent.dentry || retried)
2896 goto out;
2897
2898 BUG_ON(save_parent.dentry != dir);
2899 path_put(&nd->path);
2900 nd->path = save_parent;
2901 nd->inode = dir->d_inode;
2902 save_parent.mnt = NULL;
2903 save_parent.dentry = NULL;
2904 if (got_write) {
2905 mnt_drop_write(nd->path.mnt);
2906 got_write = false;
2907 }
2908 retried = true;
2909 goto retry_lookup;
2910 }
2911
2912 static struct file *path_openat(int dfd, struct filename *pathname,
2913 struct nameidata *nd, const struct open_flags *op, int flags)
2914 {
2915 struct file *base = NULL;
2916 struct file *file;
2917 struct path path;
2918 int opened = 0;
2919 int error;
2920
2921 file = get_empty_filp();
2922 if (!file)
2923 return ERR_PTR(-ENFILE);
2924
2925 file->f_flags = op->open_flag;
2926
2927 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
2928 if (unlikely(error))
2929 goto out;
2930
2931 current->total_link_count = 0;
2932 error = link_path_walk(pathname->name, nd);
2933 if (unlikely(error))
2934 goto out;
2935
2936 error = do_last(nd, &path, file, op, &opened, pathname);
2937 while (unlikely(error > 0)) { /* trailing symlink */
2938 struct path link = path;
2939 void *cookie;
2940 if (!(nd->flags & LOOKUP_FOLLOW)) {
2941 path_put_conditional(&path, nd);
2942 path_put(&nd->path);
2943 error = -ELOOP;
2944 break;
2945 }
2946 error = may_follow_link(&link, nd);
2947 if (unlikely(error))
2948 break;
2949 nd->flags |= LOOKUP_PARENT;
2950 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2951 error = follow_link(&link, nd, &cookie);
2952 if (unlikely(error))
2953 break;
2954 error = do_last(nd, &path, file, op, &opened, pathname);
2955 put_link(nd, &link, cookie);
2956 }
2957 out:
2958 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2959 path_put(&nd->root);
2960 if (base)
2961 fput(base);
2962 if (!(opened & FILE_OPENED)) {
2963 BUG_ON(!error);
2964 put_filp(file);
2965 }
2966 if (unlikely(error)) {
2967 if (error == -EOPENSTALE) {
2968 if (flags & LOOKUP_RCU)
2969 error = -ECHILD;
2970 else
2971 error = -ESTALE;
2972 }
2973 file = ERR_PTR(error);
2974 }
2975 return file;
2976 }
2977
2978 struct file *do_filp_open(int dfd, struct filename *pathname,
2979 const struct open_flags *op, int flags)
2980 {
2981 struct nameidata nd;
2982 struct file *filp;
2983
2984 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2985 if (unlikely(filp == ERR_PTR(-ECHILD)))
2986 filp = path_openat(dfd, pathname, &nd, op, flags);
2987 if (unlikely(filp == ERR_PTR(-ESTALE)))
2988 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2989 return filp;
2990 }
2991
2992 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2993 const char *name, const struct open_flags *op, int flags)
2994 {
2995 struct nameidata nd;
2996 struct file *file;
2997 struct filename filename = { .name = name };
2998
2999 nd.root.mnt = mnt;
3000 nd.root.dentry = dentry;
3001
3002 flags |= LOOKUP_ROOT;
3003
3004 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
3005 return ERR_PTR(-ELOOP);
3006
3007 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
3008 if (unlikely(file == ERR_PTR(-ECHILD)))
3009 file = path_openat(-1, &filename, &nd, op, flags);
3010 if (unlikely(file == ERR_PTR(-ESTALE)))
3011 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
3012 return file;
3013 }
3014
3015 struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
3016 {
3017 struct dentry *dentry = ERR_PTR(-EEXIST);
3018 struct nameidata nd;
3019 int err2;
3020 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
3021 if (error)
3022 return ERR_PTR(error);
3023
3024 /*
3025 * Yucky last component or no last component at all?
3026 * (foo/., foo/.., /////)
3027 */
3028 if (nd.last_type != LAST_NORM)
3029 goto out;
3030 nd.flags &= ~LOOKUP_PARENT;
3031 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3032
3033 /* don't fail immediately if it's r/o, at least try to report other errors */
3034 err2 = mnt_want_write(nd.path.mnt);
3035 /*
3036 * Do the final lookup.
3037 */
3038 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3039 dentry = lookup_hash(&nd);
3040 if (IS_ERR(dentry))
3041 goto unlock;
3042
3043 error = -EEXIST;
3044 if (dentry->d_inode)
3045 goto fail;
3046 /*
3047 * Special case - lookup gave negative, but... we had foo/bar/
3048 * From the vfs_mknod() POV we just have a negative dentry -
3049 * all is fine. Let's be bastards - you had / on the end, you've
3050 * been asking for (non-existent) directory. -ENOENT for you.
3051 */
3052 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3053 error = -ENOENT;
3054 goto fail;
3055 }
3056 if (unlikely(err2)) {
3057 error = err2;
3058 goto fail;
3059 }
3060 *path = nd.path;
3061 return dentry;
3062 fail:
3063 dput(dentry);
3064 dentry = ERR_PTR(error);
3065 unlock:
3066 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3067 if (!err2)
3068 mnt_drop_write(nd.path.mnt);
3069 out:
3070 path_put(&nd.path);
3071 return dentry;
3072 }
3073 EXPORT_SYMBOL(kern_path_create);
3074
3075 void done_path_create(struct path *path, struct dentry *dentry)
3076 {
3077 dput(dentry);
3078 mutex_unlock(&path->dentry->d_inode->i_mutex);
3079 mnt_drop_write(path->mnt);
3080 path_put(path);
3081 }
3082 EXPORT_SYMBOL(done_path_create);
3083
3084 struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3085 {
3086 struct filename *tmp = getname(pathname);
3087 struct dentry *res;
3088 if (IS_ERR(tmp))
3089 return ERR_CAST(tmp);
3090 res = kern_path_create(dfd, tmp->name, path, is_dir);
3091 putname(tmp);
3092 return res;
3093 }
3094 EXPORT_SYMBOL(user_path_create);
3095
3096 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3097 {
3098 int error = may_create(dir, dentry);
3099
3100 if (error)
3101 return error;
3102
3103 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3104 return -EPERM;
3105
3106 if (!dir->i_op->mknod)
3107 return -EPERM;
3108
3109 error = devcgroup_inode_mknod(mode, dev);
3110 if (error)
3111 return error;
3112
3113 error = security_inode_mknod(dir, dentry, mode, dev);
3114 if (error)
3115 return error;
3116
3117 error = dir->i_op->mknod(dir, dentry, mode, dev);
3118 if (!error)
3119 fsnotify_create(dir, dentry);
3120 return error;
3121 }
3122
3123 static int may_mknod(umode_t mode)
3124 {
3125 switch (mode & S_IFMT) {
3126 case S_IFREG:
3127 case S_IFCHR:
3128 case S_IFBLK:
3129 case S_IFIFO:
3130 case S_IFSOCK:
3131 case 0: /* zero mode translates to S_IFREG */
3132 return 0;
3133 case S_IFDIR:
3134 return -EPERM;
3135 default:
3136 return -EINVAL;
3137 }
3138 }
3139
3140 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3141 unsigned, dev)
3142 {
3143 struct dentry *dentry;
3144 struct path path;
3145 int error;
3146
3147 error = may_mknod(mode);
3148 if (error)
3149 return error;
3150
3151 dentry = user_path_create(dfd, filename, &path, 0);
3152 if (IS_ERR(dentry))
3153 return PTR_ERR(dentry);
3154
3155 if (!IS_POSIXACL(path.dentry->d_inode))
3156 mode &= ~current_umask();
3157 error = security_path_mknod(&path, dentry, mode, dev);
3158 if (error)
3159 goto out;
3160 switch (mode & S_IFMT) {
3161 case 0: case S_IFREG:
3162 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3163 break;
3164 case S_IFCHR: case S_IFBLK:
3165 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3166 new_decode_dev(dev));
3167 break;
3168 case S_IFIFO: case S_IFSOCK:
3169 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3170 break;
3171 }
3172 out:
3173 done_path_create(&path, dentry);
3174 return error;
3175 }
3176
3177 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3178 {
3179 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3180 }
3181
3182 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3183 {
3184 int error = may_create(dir, dentry);
3185 unsigned max_links = dir->i_sb->s_max_links;
3186
3187 if (error)
3188 return error;
3189
3190 if (!dir->i_op->mkdir)
3191 return -EPERM;
3192
3193 mode &= (S_IRWXUGO|S_ISVTX);
3194 error = security_inode_mkdir(dir, dentry, mode);
3195 if (error)
3196 return error;
3197
3198 if (max_links && dir->i_nlink >= max_links)
3199 return -EMLINK;
3200
3201 error = dir->i_op->mkdir(dir, dentry, mode);
3202 if (!error)
3203 fsnotify_mkdir(dir, dentry);
3204 return error;
3205 }
3206
3207 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3208 {
3209 struct dentry *dentry;
3210 struct path path;
3211 int error;
3212
3213 dentry = user_path_create(dfd, pathname, &path, 1);
3214 if (IS_ERR(dentry))
3215 return PTR_ERR(dentry);
3216
3217 if (!IS_POSIXACL(path.dentry->d_inode))
3218 mode &= ~current_umask();
3219 error = security_path_mkdir(&path, dentry, mode);
3220 if (!error)
3221 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3222 done_path_create(&path, dentry);
3223 return error;
3224 }
3225
3226 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3227 {
3228 return sys_mkdirat(AT_FDCWD, pathname, mode);
3229 }
3230
3231 /*
3232 * The dentry_unhash() helper will try to drop the dentry early: we
3233 * should have a usage count of 1 if we're the only user of this
3234 * dentry, and if that is true (possibly after pruning the dcache),
3235 * then we drop the dentry now.
3236 *
3237 * A low-level filesystem can, if it choses, legally
3238 * do a
3239 *
3240 * if (!d_unhashed(dentry))
3241 * return -EBUSY;
3242 *
3243 * if it cannot handle the case of removing a directory
3244 * that is still in use by something else..
3245 */
3246 void dentry_unhash(struct dentry *dentry)
3247 {
3248 shrink_dcache_parent(dentry);
3249 spin_lock(&dentry->d_lock);
3250 if (dentry->d_count == 1)
3251 __d_drop(dentry);
3252 spin_unlock(&dentry->d_lock);
3253 }
3254
3255 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3256 {
3257 int error = may_delete(dir, dentry, 1);
3258
3259 if (error)
3260 return error;
3261
3262 if (!dir->i_op->rmdir)
3263 return -EPERM;
3264
3265 dget(dentry);
3266 mutex_lock(&dentry->d_inode->i_mutex);
3267
3268 error = -EBUSY;
3269 if (d_mountpoint(dentry))
3270 goto out;
3271
3272 error = security_inode_rmdir(dir, dentry);
3273 if (error)
3274 goto out;
3275
3276 shrink_dcache_parent(dentry);
3277 error = dir->i_op->rmdir(dir, dentry);
3278 if (error)
3279 goto out;
3280
3281 dentry->d_inode->i_flags |= S_DEAD;
3282 dont_mount(dentry);
3283
3284 out:
3285 mutex_unlock(&dentry->d_inode->i_mutex);
3286 dput(dentry);
3287 if (!error)
3288 d_delete(dentry);
3289 return error;
3290 }
3291
3292 static long do_rmdir(int dfd, const char __user *pathname)
3293 {
3294 int error = 0;
3295 struct filename *name;
3296 struct dentry *dentry;
3297 struct nameidata nd;
3298
3299 name = user_path_parent(dfd, pathname, &nd);
3300 if (IS_ERR(name))
3301 return PTR_ERR(name);
3302
3303 switch(nd.last_type) {
3304 case LAST_DOTDOT:
3305 error = -ENOTEMPTY;
3306 goto exit1;
3307 case LAST_DOT:
3308 error = -EINVAL;
3309 goto exit1;
3310 case LAST_ROOT:
3311 error = -EBUSY;
3312 goto exit1;
3313 }
3314
3315 nd.flags &= ~LOOKUP_PARENT;
3316 error = mnt_want_write(nd.path.mnt);
3317 if (error)
3318 goto exit1;
3319
3320 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3321 dentry = lookup_hash(&nd);
3322 error = PTR_ERR(dentry);
3323 if (IS_ERR(dentry))
3324 goto exit2;
3325 if (!dentry->d_inode) {
3326 error = -ENOENT;
3327 goto exit3;
3328 }
3329 error = security_path_rmdir(&nd.path, dentry);
3330 if (error)
3331 goto exit3;
3332 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3333 exit3:
3334 dput(dentry);
3335 exit2:
3336 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3337 mnt_drop_write(nd.path.mnt);
3338 exit1:
3339 path_put(&nd.path);
3340 putname(name);
3341 return error;
3342 }
3343
3344 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3345 {
3346 return do_rmdir(AT_FDCWD, pathname);
3347 }
3348
3349 int vfs_unlink(struct inode *dir, struct dentry *dentry)
3350 {
3351 int error = may_delete(dir, dentry, 0);
3352
3353 if (error)
3354 return error;
3355
3356 if (!dir->i_op->unlink)
3357 return -EPERM;
3358
3359 mutex_lock(&dentry->d_inode->i_mutex);
3360 if (d_mountpoint(dentry))
3361 error = -EBUSY;
3362 else {
3363 error = security_inode_unlink(dir, dentry);
3364 if (!error) {
3365 error = dir->i_op->unlink(dir, dentry);
3366 if (!error)
3367 dont_mount(dentry);
3368 }
3369 }
3370 mutex_unlock(&dentry->d_inode->i_mutex);
3371
3372 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3373 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3374 fsnotify_link_count(dentry->d_inode);
3375 d_delete(dentry);
3376 }
3377
3378 return error;
3379 }
3380
3381 /*
3382 * Make sure that the actual truncation of the file will occur outside its
3383 * directory's i_mutex. Truncate can take a long time if there is a lot of
3384 * writeout happening, and we don't want to prevent access to the directory
3385 * while waiting on the I/O.
3386 */
3387 static long do_unlinkat(int dfd, const char __user *pathname)
3388 {
3389 int error;
3390 struct filename *name;
3391 struct dentry *dentry;
3392 struct nameidata nd;
3393 struct inode *inode = NULL;
3394
3395 name = user_path_parent(dfd, pathname, &nd);
3396 if (IS_ERR(name))
3397 return PTR_ERR(name);
3398
3399 error = -EISDIR;
3400 if (nd.last_type != LAST_NORM)
3401 goto exit1;
3402
3403 nd.flags &= ~LOOKUP_PARENT;
3404 error = mnt_want_write(nd.path.mnt);
3405 if (error)
3406 goto exit1;
3407
3408 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3409 dentry = lookup_hash(&nd);
3410 error = PTR_ERR(dentry);
3411 if (!IS_ERR(dentry)) {
3412 /* Why not before? Because we want correct error value */
3413 if (nd.last.name[nd.last.len])
3414 goto slashes;
3415 inode = dentry->d_inode;
3416 if (!inode)
3417 goto slashes;
3418 ihold(inode);
3419 error = security_path_unlink(&nd.path, dentry);
3420 if (error)
3421 goto exit2;
3422 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3423 exit2:
3424 dput(dentry);
3425 }
3426 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3427 if (inode)
3428 iput(inode); /* truncate the inode here */
3429 mnt_drop_write(nd.path.mnt);
3430 exit1:
3431 path_put(&nd.path);
3432 putname(name);
3433 return error;
3434
3435 slashes:
3436 error = !dentry->d_inode ? -ENOENT :
3437 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3438 goto exit2;
3439 }
3440
3441 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3442 {
3443 if ((flag & ~AT_REMOVEDIR) != 0)
3444 return -EINVAL;
3445
3446 if (flag & AT_REMOVEDIR)
3447 return do_rmdir(dfd, pathname);
3448
3449 return do_unlinkat(dfd, pathname);
3450 }
3451
3452 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3453 {
3454 return do_unlinkat(AT_FDCWD, pathname);
3455 }
3456
3457 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3458 {
3459 int error = may_create(dir, dentry);
3460
3461 if (error)
3462 return error;
3463
3464 if (!dir->i_op->symlink)
3465 return -EPERM;
3466
3467 error = security_inode_symlink(dir, dentry, oldname);
3468 if (error)
3469 return error;
3470
3471 error = dir->i_op->symlink(dir, dentry, oldname);
3472 if (!error)
3473 fsnotify_create(dir, dentry);
3474 return error;
3475 }
3476
3477 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3478 int, newdfd, const char __user *, newname)
3479 {
3480 int error;
3481 struct filename *from;
3482 struct dentry *dentry;
3483 struct path path;
3484
3485 from = getname(oldname);
3486 if (IS_ERR(from))
3487 return PTR_ERR(from);
3488
3489 dentry = user_path_create(newdfd, newname, &path, 0);
3490 error = PTR_ERR(dentry);
3491 if (IS_ERR(dentry))
3492 goto out_putname;
3493
3494 error = security_path_symlink(&path, dentry, from->name);
3495 if (!error)
3496 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3497 done_path_create(&path, dentry);
3498 out_putname:
3499 putname(from);
3500 return error;
3501 }
3502
3503 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3504 {
3505 return sys_symlinkat(oldname, AT_FDCWD, newname);
3506 }
3507
3508 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3509 {
3510 struct inode *inode = old_dentry->d_inode;
3511 unsigned max_links = dir->i_sb->s_max_links;
3512 int error;
3513
3514 if (!inode)
3515 return -ENOENT;
3516
3517 error = may_create(dir, new_dentry);
3518 if (error)
3519 return error;
3520
3521 if (dir->i_sb != inode->i_sb)
3522 return -EXDEV;
3523
3524 /*
3525 * A link to an append-only or immutable file cannot be created.
3526 */
3527 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3528 return -EPERM;
3529 if (!dir->i_op->link)
3530 return -EPERM;
3531 if (S_ISDIR(inode->i_mode))
3532 return -EPERM;
3533
3534 error = security_inode_link(old_dentry, dir, new_dentry);
3535 if (error)
3536 return error;
3537
3538 mutex_lock(&inode->i_mutex);
3539 /* Make sure we don't allow creating hardlink to an unlinked file */
3540 if (inode->i_nlink == 0)
3541 error = -ENOENT;
3542 else if (max_links && inode->i_nlink >= max_links)
3543 error = -EMLINK;
3544 else
3545 error = dir->i_op->link(old_dentry, dir, new_dentry);
3546 mutex_unlock(&inode->i_mutex);
3547 if (!error)
3548 fsnotify_link(dir, inode, new_dentry);
3549 return error;
3550 }
3551
3552 /*
3553 * Hardlinks are often used in delicate situations. We avoid
3554 * security-related surprises by not following symlinks on the
3555 * newname. --KAB
3556 *
3557 * We don't follow them on the oldname either to be compatible
3558 * with linux 2.0, and to avoid hard-linking to directories
3559 * and other special files. --ADM
3560 */
3561 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3562 int, newdfd, const char __user *, newname, int, flags)
3563 {
3564 struct dentry *new_dentry;
3565 struct path old_path, new_path;
3566 int how = 0;
3567 int error;
3568
3569 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3570 return -EINVAL;
3571 /*
3572 * To use null names we require CAP_DAC_READ_SEARCH
3573 * This ensures that not everyone will be able to create
3574 * handlink using the passed filedescriptor.
3575 */
3576 if (flags & AT_EMPTY_PATH) {
3577 if (!capable(CAP_DAC_READ_SEARCH))
3578 return -ENOENT;
3579 how = LOOKUP_EMPTY;
3580 }
3581
3582 if (flags & AT_SYMLINK_FOLLOW)
3583 how |= LOOKUP_FOLLOW;
3584
3585 error = user_path_at(olddfd, oldname, how, &old_path);
3586 if (error)
3587 return error;
3588
3589 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
3590 error = PTR_ERR(new_dentry);
3591 if (IS_ERR(new_dentry))
3592 goto out;
3593
3594 error = -EXDEV;
3595 if (old_path.mnt != new_path.mnt)
3596 goto out_dput;
3597 error = may_linkat(&old_path);
3598 if (unlikely(error))
3599 goto out_dput;
3600 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3601 if (error)
3602 goto out_dput;
3603 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3604 out_dput:
3605 done_path_create(&new_path, new_dentry);
3606 out:
3607 path_put(&old_path);
3608
3609 return error;
3610 }
3611
3612 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3613 {
3614 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3615 }
3616
3617 /*
3618 * The worst of all namespace operations - renaming directory. "Perverted"
3619 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3620 * Problems:
3621 * a) we can get into loop creation. Check is done in is_subdir().
3622 * b) race potential - two innocent renames can create a loop together.
3623 * That's where 4.4 screws up. Current fix: serialization on
3624 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3625 * story.
3626 * c) we have to lock _three_ objects - parents and victim (if it exists).
3627 * And that - after we got ->i_mutex on parents (until then we don't know
3628 * whether the target exists). Solution: try to be smart with locking
3629 * order for inodes. We rely on the fact that tree topology may change
3630 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3631 * move will be locked. Thus we can rank directories by the tree
3632 * (ancestors first) and rank all non-directories after them.
3633 * That works since everybody except rename does "lock parent, lookup,
3634 * lock child" and rename is under ->s_vfs_rename_mutex.
3635 * HOWEVER, it relies on the assumption that any object with ->lookup()
3636 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3637 * we'd better make sure that there's no link(2) for them.
3638 * d) conversion from fhandle to dentry may come in the wrong moment - when
3639 * we are removing the target. Solution: we will have to grab ->i_mutex
3640 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3641 * ->i_mutex on parents, which works but leads to some truly excessive
3642 * locking].
3643 */
3644 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3645 struct inode *new_dir, struct dentry *new_dentry)
3646 {
3647 int error = 0;
3648 struct inode *target = new_dentry->d_inode;
3649 unsigned max_links = new_dir->i_sb->s_max_links;
3650
3651 /*
3652 * If we are going to change the parent - check write permissions,
3653 * we'll need to flip '..'.
3654 */
3655 if (new_dir != old_dir) {
3656 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3657 if (error)
3658 return error;
3659 }
3660
3661 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3662 if (error)
3663 return error;
3664
3665 dget(new_dentry);
3666 if (target)
3667 mutex_lock(&target->i_mutex);
3668
3669 error = -EBUSY;
3670 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3671 goto out;
3672
3673 error = -EMLINK;
3674 if (max_links && !target && new_dir != old_dir &&
3675 new_dir->i_nlink >= max_links)
3676 goto out;
3677
3678 if (target)
3679 shrink_dcache_parent(new_dentry);
3680 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3681 if (error)
3682 goto out;
3683
3684 if (target) {
3685 target->i_flags |= S_DEAD;
3686 dont_mount(new_dentry);
3687 }
3688 out:
3689 if (target)
3690 mutex_unlock(&target->i_mutex);
3691 dput(new_dentry);
3692 if (!error)
3693 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3694 d_move(old_dentry,new_dentry);
3695 return error;
3696 }
3697
3698 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3699 struct inode *new_dir, struct dentry *new_dentry)
3700 {
3701 struct inode *target = new_dentry->d_inode;
3702 int error;
3703
3704 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3705 if (error)
3706 return error;
3707
3708 dget(new_dentry);
3709 if (target)
3710 mutex_lock(&target->i_mutex);
3711
3712 error = -EBUSY;
3713 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3714 goto out;
3715
3716 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3717 if (error)
3718 goto out;
3719
3720 if (target)
3721 dont_mount(new_dentry);
3722 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3723 d_move(old_dentry, new_dentry);
3724 out:
3725 if (target)
3726 mutex_unlock(&target->i_mutex);
3727 dput(new_dentry);
3728 return error;
3729 }
3730
3731 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3732 struct inode *new_dir, struct dentry *new_dentry)
3733 {
3734 int error;
3735 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3736 const unsigned char *old_name;
3737
3738 if (old_dentry->d_inode == new_dentry->d_inode)
3739 return 0;
3740
3741 error = may_delete(old_dir, old_dentry, is_dir);
3742 if (error)
3743 return error;
3744
3745 if (!new_dentry->d_inode)
3746 error = may_create(new_dir, new_dentry);
3747 else
3748 error = may_delete(new_dir, new_dentry, is_dir);
3749 if (error)
3750 return error;
3751
3752 if (!old_dir->i_op->rename)
3753 return -EPERM;
3754
3755 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3756
3757 if (is_dir)
3758 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3759 else
3760 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3761 if (!error)
3762 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3763 new_dentry->d_inode, old_dentry);
3764 fsnotify_oldname_free(old_name);
3765
3766 return error;
3767 }
3768
3769 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3770 int, newdfd, const char __user *, newname)
3771 {
3772 struct dentry *old_dir, *new_dir;
3773 struct dentry *old_dentry, *new_dentry;
3774 struct dentry *trap;
3775 struct nameidata oldnd, newnd;
3776 struct filename *from;
3777 struct filename *to;
3778 int error;
3779
3780 from = user_path_parent(olddfd, oldname, &oldnd);
3781 if (IS_ERR(from)) {
3782 error = PTR_ERR(from);
3783 goto exit;
3784 }
3785
3786 to = user_path_parent(newdfd, newname, &newnd);
3787 if (IS_ERR(to)) {
3788 error = PTR_ERR(to);
3789 goto exit1;
3790 }
3791
3792 error = -EXDEV;
3793 if (oldnd.path.mnt != newnd.path.mnt)
3794 goto exit2;
3795
3796 old_dir = oldnd.path.dentry;
3797 error = -EBUSY;
3798 if (oldnd.last_type != LAST_NORM)
3799 goto exit2;
3800
3801 new_dir = newnd.path.dentry;
3802 if (newnd.last_type != LAST_NORM)
3803 goto exit2;
3804
3805 error = mnt_want_write(oldnd.path.mnt);
3806 if (error)
3807 goto exit2;
3808
3809 oldnd.flags &= ~LOOKUP_PARENT;
3810 newnd.flags &= ~LOOKUP_PARENT;
3811 newnd.flags |= LOOKUP_RENAME_TARGET;
3812
3813 trap = lock_rename(new_dir, old_dir);
3814
3815 old_dentry = lookup_hash(&oldnd);
3816 error = PTR_ERR(old_dentry);
3817 if (IS_ERR(old_dentry))
3818 goto exit3;
3819 /* source must exist */
3820 error = -ENOENT;
3821 if (!old_dentry->d_inode)
3822 goto exit4;
3823 /* unless the source is a directory trailing slashes give -ENOTDIR */
3824 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3825 error = -ENOTDIR;
3826 if (oldnd.last.name[oldnd.last.len])
3827 goto exit4;
3828 if (newnd.last.name[newnd.last.len])
3829 goto exit4;
3830 }
3831 /* source should not be ancestor of target */
3832 error = -EINVAL;
3833 if (old_dentry == trap)
3834 goto exit4;
3835 new_dentry = lookup_hash(&newnd);
3836 error = PTR_ERR(new_dentry);
3837 if (IS_ERR(new_dentry))
3838 goto exit4;
3839 /* target should not be an ancestor of source */
3840 error = -ENOTEMPTY;
3841 if (new_dentry == trap)
3842 goto exit5;
3843
3844 error = security_path_rename(&oldnd.path, old_dentry,
3845 &newnd.path, new_dentry);
3846 if (error)
3847 goto exit5;
3848 error = vfs_rename(old_dir->d_inode, old_dentry,
3849 new_dir->d_inode, new_dentry);
3850 exit5:
3851 dput(new_dentry);
3852 exit4:
3853 dput(old_dentry);
3854 exit3:
3855 unlock_rename(new_dir, old_dir);
3856 mnt_drop_write(oldnd.path.mnt);
3857 exit2:
3858 path_put(&newnd.path);
3859 putname(to);
3860 exit1:
3861 path_put(&oldnd.path);
3862 putname(from);
3863 exit:
3864 return error;
3865 }
3866
3867 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3868 {
3869 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3870 }
3871
3872 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3873 {
3874 int len;
3875
3876 len = PTR_ERR(link);
3877 if (IS_ERR(link))
3878 goto out;
3879
3880 len = strlen(link);
3881 if (len > (unsigned) buflen)
3882 len = buflen;
3883 if (copy_to_user(buffer, link, len))
3884 len = -EFAULT;
3885 out:
3886 return len;
3887 }
3888
3889 /*
3890 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3891 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3892 * using) it for any given inode is up to filesystem.
3893 */
3894 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3895 {
3896 struct nameidata nd;
3897 void *cookie;
3898 int res;
3899
3900 nd.depth = 0;
3901 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3902 if (IS_ERR(cookie))
3903 return PTR_ERR(cookie);
3904
3905 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3906 if (dentry->d_inode->i_op->put_link)
3907 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3908 return res;
3909 }
3910
3911 int vfs_follow_link(struct nameidata *nd, const char *link)
3912 {
3913 return __vfs_follow_link(nd, link);
3914 }
3915
3916 /* get the link contents into pagecache */
3917 static char *page_getlink(struct dentry * dentry, struct page **ppage)
3918 {
3919 char *kaddr;
3920 struct page *page;
3921 struct address_space *mapping = dentry->d_inode->i_mapping;
3922 page = read_mapping_page(mapping, 0, NULL);
3923 if (IS_ERR(page))
3924 return (char*)page;
3925 *ppage = page;
3926 kaddr = kmap(page);
3927 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3928 return kaddr;
3929 }
3930
3931 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3932 {
3933 struct page *page = NULL;
3934 char *s = page_getlink(dentry, &page);
3935 int res = vfs_readlink(dentry,buffer,buflen,s);
3936 if (page) {
3937 kunmap(page);
3938 page_cache_release(page);
3939 }
3940 return res;
3941 }
3942
3943 void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3944 {
3945 struct page *page = NULL;
3946 nd_set_link(nd, page_getlink(dentry, &page));
3947 return page;
3948 }
3949
3950 void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3951 {
3952 struct page *page = cookie;
3953
3954 if (page) {
3955 kunmap(page);
3956 page_cache_release(page);
3957 }
3958 }
3959
3960 /*
3961 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3962 */
3963 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3964 {
3965 struct address_space *mapping = inode->i_mapping;
3966 struct page *page;
3967 void *fsdata;
3968 int err;
3969 char *kaddr;
3970 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3971 if (nofs)
3972 flags |= AOP_FLAG_NOFS;
3973
3974 retry:
3975 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3976 flags, &page, &fsdata);
3977 if (err)
3978 goto fail;
3979
3980 kaddr = kmap_atomic(page);
3981 memcpy(kaddr, symname, len-1);
3982 kunmap_atomic(kaddr);
3983
3984 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3985 page, fsdata);
3986 if (err < 0)
3987 goto fail;
3988 if (err < len-1)
3989 goto retry;
3990
3991 mark_inode_dirty(inode);
3992 return 0;
3993 fail:
3994 return err;
3995 }
3996
3997 int page_symlink(struct inode *inode, const char *symname, int len)
3998 {
3999 return __page_symlink(inode, symname, len,
4000 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
4001 }
4002
4003 const struct inode_operations page_symlink_inode_operations = {
4004 .readlink = generic_readlink,
4005 .follow_link = page_follow_link_light,
4006 .put_link = page_put_link,
4007 };
4008
4009 EXPORT_SYMBOL(user_path_at);
4010 EXPORT_SYMBOL(follow_down_one);
4011 EXPORT_SYMBOL(follow_down);
4012 EXPORT_SYMBOL(follow_up);
4013 EXPORT_SYMBOL(get_write_access); /* nfsd */
4014 EXPORT_SYMBOL(lock_rename);
4015 EXPORT_SYMBOL(lookup_one_len);
4016 EXPORT_SYMBOL(page_follow_link_light);
4017 EXPORT_SYMBOL(page_put_link);
4018 EXPORT_SYMBOL(page_readlink);
4019 EXPORT_SYMBOL(__page_symlink);
4020 EXPORT_SYMBOL(page_symlink);
4021 EXPORT_SYMBOL(page_symlink_inode_operations);
4022 EXPORT_SYMBOL(kern_path);
4023 EXPORT_SYMBOL(vfs_path_lookup);
4024 EXPORT_SYMBOL(inode_permission);
4025 EXPORT_SYMBOL(unlock_rename);
4026 EXPORT_SYMBOL(vfs_create);
4027 EXPORT_SYMBOL(vfs_follow_link);
4028 EXPORT_SYMBOL(vfs_link);
4029 EXPORT_SYMBOL(vfs_mkdir);
4030 EXPORT_SYMBOL(vfs_mknod);
4031 EXPORT_SYMBOL(generic_permission);
4032 EXPORT_SYMBOL(vfs_readlink);
4033 EXPORT_SYMBOL(vfs_rename);
4034 EXPORT_SYMBOL(vfs_rmdir);
4035 EXPORT_SYMBOL(vfs_symlink);
4036 EXPORT_SYMBOL(vfs_unlink);
4037 EXPORT_SYMBOL(dentry_unhash);
4038 EXPORT_SYMBOL(generic_readlink);
This page took 0.153717 seconds and 6 git commands to generate.