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