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