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