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