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