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