~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/namei.c

Version: ~ [ linux-5.9 ] ~ [ linux-5.8.14 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.70 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.150 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.200 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.238 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.238 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp