blob: c731b50a618469cb70de76f98f8661ea15110ca3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/personality.h>
25#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050026#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080030#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070031#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080032#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36
Eric Parise81e3f42009-12-04 15:47:36 -050037#include "internal.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* [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)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * 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 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800117static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
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
139char * 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
159void putname(const char *name)
160{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400161 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 audit_putname(name);
163 else
164 __putname(name);
165}
166EXPORT_SYMBOL(putname);
167#endif
168
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700169/*
170 * This does basic POSIX ACL permission checking
171 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100172static inline int __acl_permission_check(struct inode *inode, int mask,
173 int (*check_acl)(struct inode *inode, int mask), int rcu)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700174{
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) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100183 if (rcu) {
184 return -ECHILD;
185 } else {
186 int error = check_acl(inode, mask);
187 if (error != -EAGAIN)
188 return error;
189 }
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700190 }
191
192 if (in_group_p(inode->i_gid))
193 mode >>= 3;
194 }
195
196 /*
197 * If the DACs are ok we don't need any capability check.
198 */
199 if ((mask & ~mode) == 0)
200 return 0;
201 return -EACCES;
202}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Nick Piggin31e6b012011-01-07 17:49:52 +1100204static inline int acl_permission_check(struct inode *inode, int mask,
205 int (*check_acl)(struct inode *inode, int mask))
206{
207 return __acl_permission_check(inode, mask, check_acl, 0);
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/**
211 * generic_permission - check for access rights on a Posix-like filesystem
212 * @inode: inode to check access rights for
213 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
214 * @check_acl: optional callback to check for Posix ACLs
215 *
216 * Used to check for read/write/execute permissions on a file.
217 * We use "fsuid" for this, letting us set arbitrary permissions
218 * for filesystem access without changing the "normal" uids which
219 * are used for other things..
220 */
221int generic_permission(struct inode *inode, int mask,
222 int (*check_acl)(struct inode *inode, int mask))
223{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700224 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700227 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700229 ret = acl_permission_check(inode, mask, check_acl);
230 if (ret != -EACCES)
231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /*
234 * Read/write DACs are always overridable.
235 * Executable DACs are overridable if at least one exec bit is set.
236 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200237 if (!(mask & MAY_EXEC) || execute_ok(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (capable(CAP_DAC_OVERRIDE))
239 return 0;
240
241 /*
242 * Searching includes executable on directories, else just read.
243 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600244 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
246 if (capable(CAP_DAC_READ_SEARCH))
247 return 0;
248
249 return -EACCES;
250}
251
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200252/**
253 * inode_permission - check for access rights to a given inode
254 * @inode: inode to check permission on
255 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
256 *
257 * Used to check for read/write/execute permissions on an inode.
258 * We use "fsuid" for this, letting us set arbitrary permissions
259 * for filesystem access without changing the "normal" uids which
260 * are used for other things.
261 */
Al Virof419a2e2008-07-22 00:07:17 -0400262int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Al Viroe6305c42008-07-15 21:03:57 -0400264 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700267 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /*
270 * Nobody gets write access to a read-only fs.
271 */
272 if (IS_RDONLY(inode) &&
273 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
274 return -EROFS;
275
276 /*
277 * Nobody gets write access to an immutable file.
278 */
279 if (IS_IMMUTABLE(inode))
280 return -EACCES;
281 }
282
Al Viroacfa4382008-12-04 10:06:33 -0500283 if (inode->i_op->permission)
Al Virob77b0642008-07-17 09:37:02 -0400284 retval = inode->i_op->permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200285 else
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700286 retval = generic_permission(inode, mask, inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (retval)
289 return retval;
290
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700291 retval = devcgroup_inode_permission(inode, mask);
292 if (retval)
293 return retval;
294
Eric Parisd09ca732010-07-23 11:43:57 -0400295 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800298/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800299 * file_permission - check for additional access rights to a given file
300 * @file: file to check access rights for
301 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
302 *
303 * Used to check for read/write/execute permissions on an already opened
304 * file.
305 *
306 * Note:
307 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200308 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800309 */
310int file_permission(struct file *file, int mask)
311{
Al Virof419a2e2008-07-22 00:07:17 -0400312 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
316 * get_write_access() gets write permission for a file.
317 * put_write_access() releases this write permission.
318 * This is used for regular files.
319 * We cannot support write (and maybe mmap read-write shared) accesses and
320 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
321 * can have the following values:
322 * 0: no writers, no VM_DENYWRITE mappings
323 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
324 * > 0: (i_writecount) users are writing to the file.
325 *
326 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
327 * except for the cases where we don't hold i_writecount yet. Then we need to
328 * use {get,deny}_write_access() - these functions check the sign and refuse
329 * to do the change if sign is wrong. Exclusion between them is provided by
330 * the inode->i_lock spinlock.
331 */
332
333int get_write_access(struct inode * inode)
334{
335 spin_lock(&inode->i_lock);
336 if (atomic_read(&inode->i_writecount) < 0) {
337 spin_unlock(&inode->i_lock);
338 return -ETXTBSY;
339 }
340 atomic_inc(&inode->i_writecount);
341 spin_unlock(&inode->i_lock);
342
343 return 0;
344}
345
346int deny_write_access(struct file * file)
347{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800348 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 spin_lock(&inode->i_lock);
351 if (atomic_read(&inode->i_writecount) > 0) {
352 spin_unlock(&inode->i_lock);
353 return -ETXTBSY;
354 }
355 atomic_dec(&inode->i_writecount);
356 spin_unlock(&inode->i_lock);
357
358 return 0;
359}
360
Jan Blunck1d957f92008-02-14 19:34:35 -0800361/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800362 * path_get - get a reference to a path
363 * @path: path to get the reference to
364 *
365 * Given a path increment the reference count to the dentry and the vfsmount.
366 */
367void path_get(struct path *path)
368{
369 mntget(path->mnt);
370 dget(path->dentry);
371}
372EXPORT_SYMBOL(path_get);
373
374/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800375 * path_put - put a reference to a path
376 * @path: path to put the reference to
377 *
378 * Given a path decrement the reference count to the dentry and the vfsmount.
379 */
380void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Jan Blunck1d957f92008-02-14 19:34:35 -0800382 dput(path->dentry);
383 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
Jan Blunck1d957f92008-02-14 19:34:35 -0800385EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Trond Myklebust834f2a42005-10-18 14:20:16 -0700387/**
Nick Piggin31e6b012011-01-07 17:49:52 +1100388 * nameidata_drop_rcu - drop this nameidata out of rcu-walk
389 * @nd: nameidata pathwalk data to drop
390 * @Returns: 0 on success, -ECHLID on failure
391 *
392 * Path walking has 2 modes, rcu-walk and ref-walk (see
393 * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
394 * to drop out of rcu-walk mode and take normal reference counts on dentries
395 * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
396 * refcounts at the last known good point before rcu-walk got stuck, so
397 * ref-walk may continue from there. If this is not successful (eg. a seqcount
398 * has changed), then failure is returned and path walk restarts from the
399 * beginning in ref-walk mode.
400 *
401 * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
402 * ref-walk. Must be called from rcu-walk context.
403 */
404static int nameidata_drop_rcu(struct nameidata *nd)
405{
406 struct fs_struct *fs = current->fs;
407 struct dentry *dentry = nd->path.dentry;
408
409 BUG_ON(!(nd->flags & LOOKUP_RCU));
410 if (nd->root.mnt) {
411 spin_lock(&fs->lock);
412 if (nd->root.mnt != fs->root.mnt ||
413 nd->root.dentry != fs->root.dentry)
414 goto err_root;
415 }
416 spin_lock(&dentry->d_lock);
417 if (!__d_rcu_to_refcount(dentry, nd->seq))
418 goto err;
419 BUG_ON(nd->inode != dentry->d_inode);
420 spin_unlock(&dentry->d_lock);
421 if (nd->root.mnt) {
422 path_get(&nd->root);
423 spin_unlock(&fs->lock);
424 }
425 mntget(nd->path.mnt);
426
427 rcu_read_unlock();
428 br_read_unlock(vfsmount_lock);
429 nd->flags &= ~LOOKUP_RCU;
430 return 0;
431err:
432 spin_unlock(&dentry->d_lock);
433err_root:
434 if (nd->root.mnt)
435 spin_unlock(&fs->lock);
436 return -ECHILD;
437}
438
439/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
440static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
441{
442 if (nd->flags & LOOKUP_RCU)
443 return nameidata_drop_rcu(nd);
444 return 0;
445}
446
447/**
448 * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
449 * @nd: nameidata pathwalk data to drop
450 * @dentry: dentry to drop
451 * @Returns: 0 on success, -ECHLID on failure
452 *
453 * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
454 * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
455 * @nd. Must be called from rcu-walk context.
456 */
457static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
458{
459 struct fs_struct *fs = current->fs;
460 struct dentry *parent = nd->path.dentry;
461
462 BUG_ON(!(nd->flags & LOOKUP_RCU));
463 if (nd->root.mnt) {
464 spin_lock(&fs->lock);
465 if (nd->root.mnt != fs->root.mnt ||
466 nd->root.dentry != fs->root.dentry)
467 goto err_root;
468 }
469 spin_lock(&parent->d_lock);
470 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
471 if (!__d_rcu_to_refcount(dentry, nd->seq))
472 goto err;
473 /*
474 * If the sequence check on the child dentry passed, then the child has
475 * not been removed from its parent. This means the parent dentry must
476 * be valid and able to take a reference at this point.
477 */
478 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
479 BUG_ON(!parent->d_count);
480 parent->d_count++;
481 spin_unlock(&dentry->d_lock);
482 spin_unlock(&parent->d_lock);
483 if (nd->root.mnt) {
484 path_get(&nd->root);
485 spin_unlock(&fs->lock);
486 }
487 mntget(nd->path.mnt);
488
489 rcu_read_unlock();
490 br_read_unlock(vfsmount_lock);
491 nd->flags &= ~LOOKUP_RCU;
492 return 0;
493err:
494 spin_unlock(&dentry->d_lock);
495 spin_unlock(&parent->d_lock);
496err_root:
497 if (nd->root.mnt)
498 spin_unlock(&fs->lock);
499 return -ECHILD;
500}
501
502/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
503static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
504{
505 if (nd->flags & LOOKUP_RCU)
506 return nameidata_dentry_drop_rcu(nd, dentry);
507 return 0;
508}
509
510/**
511 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
512 * @nd: nameidata pathwalk data to drop
513 * @Returns: 0 on success, -ECHLID on failure
514 *
515 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
516 * nd->path should be the final element of the lookup, so nd->root is discarded.
517 * Must be called from rcu-walk context.
518 */
519static int nameidata_drop_rcu_last(struct nameidata *nd)
520{
521 struct dentry *dentry = nd->path.dentry;
522
523 BUG_ON(!(nd->flags & LOOKUP_RCU));
524 nd->flags &= ~LOOKUP_RCU;
525 nd->root.mnt = NULL;
526 spin_lock(&dentry->d_lock);
527 if (!__d_rcu_to_refcount(dentry, nd->seq))
528 goto err_unlock;
529 BUG_ON(nd->inode != dentry->d_inode);
530 spin_unlock(&dentry->d_lock);
531
532 mntget(nd->path.mnt);
533
534 rcu_read_unlock();
535 br_read_unlock(vfsmount_lock);
536
537 return 0;
538
539err_unlock:
540 spin_unlock(&dentry->d_lock);
541 rcu_read_unlock();
542 br_read_unlock(vfsmount_lock);
543 return -ECHILD;
544}
545
546/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
547static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd)
548{
549 if (likely(nd->flags & LOOKUP_RCU))
550 return nameidata_drop_rcu_last(nd);
551 return 0;
552}
553
554/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700555 * release_open_intent - free up open intent resources
556 * @nd: pointer to nameidata
557 */
558void release_open_intent(struct nameidata *nd)
559{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800560 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700561 put_filp(nd->intent.open.file);
562 else
563 fput(nd->intent.open.file);
564}
565
Ian Kentbcdc5e02006-09-27 01:50:44 -0700566static inline struct dentry *
567do_revalidate(struct dentry *dentry, struct nameidata *nd)
568{
569 int status = dentry->d_op->d_revalidate(dentry, nd);
570 if (unlikely(status <= 0)) {
571 /*
572 * The dentry failed validation.
573 * If d_revalidate returned 0 attempt to invalidate
574 * the dentry otherwise d_revalidate is asking us
575 * to return a fail status.
576 */
577 if (!status) {
578 if (!d_invalidate(dentry)) {
579 dput(dentry);
580 dentry = NULL;
581 }
582 } else {
583 dput(dentry);
584 dentry = ERR_PTR(status);
585 }
586 }
587 return dentry;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/*
Jeff Layton39159de2009-12-07 12:01:50 -0500591 * force_reval_path - force revalidation of a dentry
592 *
593 * In some situations the path walking code will trust dentries without
594 * revalidating them. This causes problems for filesystems that depend on
595 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
596 * (which indicates that it's possible for the dentry to go stale), force
597 * a d_revalidate call before proceeding.
598 *
599 * Returns 0 if the revalidation was successful. If the revalidation fails,
600 * either return the error returned by d_revalidate or -ESTALE if the
601 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
602 * invalidate the dentry. It's up to the caller to handle putting references
603 * to the path if necessary.
604 */
605static int
606force_reval_path(struct path *path, struct nameidata *nd)
607{
608 int status;
609 struct dentry *dentry = path->dentry;
610
611 /*
612 * only check on filesystems where it's possible for the dentry to
613 * become stale. It's assumed that if this flag is set then the
614 * d_revalidate op will also be defined.
615 */
616 if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))
617 return 0;
618
619 status = dentry->d_op->d_revalidate(dentry, nd);
620 if (status > 0)
621 return 0;
622
623 if (!status) {
624 d_invalidate(dentry);
625 status = -ESTALE;
626 }
627 return status;
628}
629
630/*
Al Virob75b5082009-12-16 01:01:38 -0500631 * Short-cut version of permission(), for calling on directories
632 * during pathname resolution. Combines parts of permission()
633 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 *
635 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500636 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * complete permission check.
638 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100639static inline int __exec_permission(struct inode *inode, int rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700641 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700643 if (inode->i_op->permission) {
Nick Piggin31e6b012011-01-07 17:49:52 +1100644 if (rcu)
645 return -ECHILD;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700646 ret = inode->i_op->permission(inode, MAY_EXEC);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700647 if (!ret)
648 goto ok;
649 return ret;
650 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100651 ret = __acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl, rcu);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700652 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto ok;
Nick Piggin31e6b012011-01-07 17:49:52 +1100654 if (rcu && ret == -ECHILD)
655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linus Torvaldsf1ac9f62009-08-28 10:53:56 -0700657 if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto ok;
659
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661ok:
Nick Piggin31e6b012011-01-07 17:49:52 +1100662 return security_inode_exec_permission(inode, rcu);
663}
664
665static int exec_permission(struct inode *inode)
666{
667 return __exec_permission(inode, 0);
668}
669
670static int exec_permission_rcu(struct inode *inode)
671{
672 return __exec_permission(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Al Viro2a737872009-04-07 11:49:53 -0400675static __always_inline void set_root(struct nameidata *nd)
676{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200677 if (!nd->root.mnt)
678 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400679}
680
Al Viro6de88d72009-08-09 01:41:57 +0400681static int link_path_walk(const char *, struct nameidata *);
682
Nick Piggin31e6b012011-01-07 17:49:52 +1100683static __always_inline void set_root_rcu(struct nameidata *nd)
684{
685 if (!nd->root.mnt) {
686 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100687 unsigned seq;
688
689 do {
690 seq = read_seqcount_begin(&fs->seq);
691 nd->root = fs->root;
692 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100693 }
694}
695
Arjan van de Venf1662352006-01-14 13:21:31 -0800696static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Nick Piggin31e6b012011-01-07 17:49:52 +1100698 int ret;
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (IS_ERR(link))
701 goto fail;
702
703 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400704 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800705 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400706 nd->path = nd->root;
707 path_get(&nd->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100709 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100710
Nick Piggin31e6b012011-01-07 17:49:52 +1100711 ret = link_path_walk(link, nd);
712 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800714 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return PTR_ERR(link);
716}
717
Jan Blunck1d957f92008-02-14 19:34:35 -0800718static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700719{
720 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800721 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700722 mntput(path->mnt);
723}
724
725static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
726{
Nick Piggin31e6b012011-01-07 17:49:52 +1100727 if (!(nd->flags & LOOKUP_RCU)) {
728 dput(nd->path.dentry);
729 if (nd->path.mnt != path->mnt)
730 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800731 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100732 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800733 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700734}
735
Al Virodef4af32009-12-26 08:37:05 -0500736static __always_inline int
737__do_follow_link(struct path *path, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800738{
739 int error;
Ian Kent051d3812006-03-27 01:14:53 -0800740 struct dentry *dentry = path->dentry;
741
742 touch_atime(path->mnt, dentry);
743 nd_set_link(nd, NULL);
744
Jan Blunck4ac91372008-02-14 19:34:32 -0800745 if (path->mnt != nd->path.mnt) {
Ian Kent051d3812006-03-27 01:14:53 -0800746 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +1100747 nd->inode = nd->path.dentry->d_inode;
Ian Kent051d3812006-03-27 01:14:53 -0800748 dget(dentry);
749 }
750 mntget(path->mnt);
Nick Piggin31e6b012011-01-07 17:49:52 +1100751
Al Viro86acdca12009-12-22 23:45:11 -0500752 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500753 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
754 error = PTR_ERR(*p);
755 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800756 char *s = nd_get_link(nd);
757 error = 0;
758 if (s)
759 error = __vfs_follow_link(nd, s);
Jeff Layton39159de2009-12-07 12:01:50 -0500760 else if (nd->last_type == LAST_BIND) {
761 error = force_reval_path(&nd->path, nd);
762 if (error)
763 path_put(&nd->path);
764 }
Ian Kent051d3812006-03-27 01:14:53 -0800765 }
Ian Kent051d3812006-03-27 01:14:53 -0800766 return error;
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * This limits recursive symlink follows to 8, while
771 * limiting consecutive symlinks to 40.
772 *
773 * Without that kind of total limit, nasty chains of consecutive
774 * symlinks can cause almost arbitrarily long lookups.
775 */
Al Viro90ebe562005-06-06 13:35:58 -0700776static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Al Virodef4af32009-12-26 08:37:05 -0500778 void *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int err = -ELOOP;
780 if (current->link_count >= MAX_NESTED_LINKS)
781 goto loop;
782 if (current->total_link_count >= 40)
783 goto loop;
784 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
785 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700786 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (err)
788 goto loop;
789 current->link_count++;
790 current->total_link_count++;
791 nd->depth++;
Al Virodef4af32009-12-26 08:37:05 -0500792 err = __do_follow_link(path, nd, &cookie);
793 if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
794 path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
Al Viro258fa992009-08-09 01:32:02 +0400795 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 current->link_count--;
797 nd->depth--;
798 return err;
799loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800800 path_put_conditional(path, nd);
801 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return err;
803}
804
Nick Piggin31e6b012011-01-07 17:49:52 +1100805static int follow_up_rcu(struct path *path)
806{
807 struct vfsmount *parent;
808 struct dentry *mountpoint;
809
810 parent = path->mnt->mnt_parent;
811 if (parent == path->mnt)
812 return 0;
813 mountpoint = path->mnt->mnt_mountpoint;
814 path->dentry = mountpoint;
815 path->mnt = parent;
816 return 1;
817}
818
Al Virobab77eb2009-04-18 03:26:48 -0400819int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct vfsmount *parent;
822 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000823
824 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400825 parent = path->mnt->mnt_parent;
826 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000827 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829 }
830 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400831 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000832 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400833 dput(path->dentry);
834 path->dentry = mountpoint;
835 mntput(path->mnt);
836 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 1;
838}
839
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100840/*
841 * serialization is taken care of in namespace.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100843static void __follow_mount_rcu(struct nameidata *nd, struct path *path,
844 struct inode **inode)
845{
846 while (d_mountpoint(path->dentry)) {
847 struct vfsmount *mounted;
848 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
849 if (!mounted)
850 return;
851 path->mnt = mounted;
852 path->dentry = mounted->mnt_root;
853 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
854 *inode = path->dentry->d_inode;
855 }
856}
857
Al Viro463ffb22005-06-06 13:36:05 -0700858static int __follow_mount(struct path *path)
859{
860 int res = 0;
861 while (d_mountpoint(path->dentry)) {
Al Viro1c755af2009-04-18 14:06:57 -0400862 struct vfsmount *mounted = lookup_mnt(path);
Al Viro463ffb22005-06-06 13:36:05 -0700863 if (!mounted)
864 break;
865 dput(path->dentry);
866 if (res)
867 mntput(path->mnt);
868 path->mnt = mounted;
869 path->dentry = dget(mounted->mnt_root);
870 res = 1;
871 }
872 return res;
873}
874
Al Viro79ed0222009-04-18 13:59:41 -0400875static void follow_mount(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Al Viro79ed0222009-04-18 13:59:41 -0400877 while (d_mountpoint(path->dentry)) {
Al Viro1c755af2009-04-18 14:06:57 -0400878 struct vfsmount *mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (!mounted)
880 break;
Al Viro79ed0222009-04-18 13:59:41 -0400881 dput(path->dentry);
882 mntput(path->mnt);
883 path->mnt = mounted;
884 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Al Viro9393bd02009-04-18 13:58:15 -0400888int follow_down(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 struct vfsmount *mounted;
891
Al Viro1c755af2009-04-18 14:06:57 -0400892 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400894 dput(path->dentry);
895 mntput(path->mnt);
896 path->mnt = mounted;
897 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return 1;
899 }
900 return 0;
901}
902
Nick Piggin31e6b012011-01-07 17:49:52 +1100903static int follow_dotdot_rcu(struct nameidata *nd)
904{
905 struct inode *inode = nd->inode;
906
907 set_root_rcu(nd);
908
909 while(1) {
910 if (nd->path.dentry == nd->root.dentry &&
911 nd->path.mnt == nd->root.mnt) {
912 break;
913 }
914 if (nd->path.dentry != nd->path.mnt->mnt_root) {
915 struct dentry *old = nd->path.dentry;
916 struct dentry *parent = old->d_parent;
917 unsigned seq;
918
919 seq = read_seqcount_begin(&parent->d_seq);
920 if (read_seqcount_retry(&old->d_seq, nd->seq))
921 return -ECHILD;
922 inode = parent->d_inode;
923 nd->path.dentry = parent;
924 nd->seq = seq;
925 break;
926 }
927 if (!follow_up_rcu(&nd->path))
928 break;
929 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
930 inode = nd->path.dentry->d_inode;
931 }
932 __follow_mount_rcu(nd, &nd->path, &inode);
933 nd->inode = inode;
934
935 return 0;
936}
937
938static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Al Viro2a737872009-04-07 11:49:53 -0400940 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800943 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Al Viro2a737872009-04-07 11:49:53 -0400945 if (nd->path.dentry == nd->root.dentry &&
946 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
948 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800949 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -0500950 /* rare case of legitimate dget_parent()... */
951 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dput(old);
953 break;
954 }
Al Viro3088dd72010-01-30 15:47:29 -0500955 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Al Viro79ed0222009-04-18 13:59:41 -0400958 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +1100959 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/*
Nick Pigginbaa03892010-08-18 04:37:31 +1000963 * Allocate a dentry with name and parent, and perform a parent
964 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
965 * on error. parent->d_inode->i_mutex must be held. d_lookup must
966 * have verified that no child exists while under i_mutex.
967 */
968static struct dentry *d_alloc_and_lookup(struct dentry *parent,
969 struct qstr *name, struct nameidata *nd)
970{
971 struct inode *inode = parent->d_inode;
972 struct dentry *dentry;
973 struct dentry *old;
974
975 /* Don't create child dentry for a dead directory. */
976 if (unlikely(IS_DEADDIR(inode)))
977 return ERR_PTR(-ENOENT);
978
979 dentry = d_alloc(parent, name);
980 if (unlikely(!dentry))
981 return ERR_PTR(-ENOMEM);
982
983 old = inode->i_op->lookup(inode, dentry, nd);
984 if (unlikely(old)) {
985 dput(dentry);
986 dentry = old;
987 }
988 return dentry;
989}
990
991/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * It's more convoluted than I'd like it to be, but... it's still fairly
993 * small and for now I'd prefer to have fast path as straight as possible.
994 * It _is_ time-critical.
995 */
996static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +1100997 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Jan Blunck4ac91372008-02-14 19:34:32 -0800999 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001000 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro6e6b1bd2009-08-13 23:38:37 +04001001 struct inode *dir;
Al Viro3cac2602009-08-13 18:27:43 +04001002 /*
1003 * See if the low-level filesystem might want
1004 * to use its own hash..
1005 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001006 if (parent->d_op && parent->d_op->d_hash) {
1007 int err = parent->d_op->d_hash(parent, nd->inode, name);
Al Viro3cac2602009-08-13 18:27:43 +04001008 if (err < 0)
1009 return err;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Nick Pigginb04f7842010-08-18 04:37:34 +10001012 /*
1013 * Rename seqlock is not required here because in the off chance
1014 * of a false negative due to a concurrent rename, we're going to
1015 * do the non-racy lookup, below.
1016 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001017 if (nd->flags & LOOKUP_RCU) {
1018 unsigned seq;
1019
1020 *inode = nd->inode;
1021 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1022 if (!dentry) {
1023 if (nameidata_drop_rcu(nd))
1024 return -ECHILD;
1025 goto need_lookup;
1026 }
1027 /* Memory barrier in read_seqcount_begin of child is enough */
1028 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1029 return -ECHILD;
1030
1031 nd->seq = seq;
1032 if (dentry->d_op && dentry->d_op->d_revalidate) {
1033 /* We commonly drop rcu-walk here */
1034 if (nameidata_dentry_drop_rcu(nd, dentry))
1035 return -ECHILD;
1036 goto need_revalidate;
1037 }
1038 path->mnt = mnt;
1039 path->dentry = dentry;
1040 __follow_mount_rcu(nd, path, inode);
1041 } else {
1042 dentry = __d_lookup(parent, name);
1043 if (!dentry)
1044 goto need_lookup;
Nick Piggin2e2e88e2010-08-18 04:37:30 +10001045found:
Nick Piggin31e6b012011-01-07 17:49:52 +11001046 if (dentry->d_op && dentry->d_op->d_revalidate)
1047 goto need_revalidate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048done:
Nick Piggin31e6b012011-01-07 17:49:52 +11001049 path->mnt = mnt;
1050 path->dentry = dentry;
1051 __follow_mount(path);
1052 *inode = path->dentry->d_inode;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 0;
1055
1056need_lookup:
Al Viro6e6b1bd2009-08-13 23:38:37 +04001057 dir = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001058 BUG_ON(nd->inode != dir);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001059
1060 mutex_lock(&dir->i_mutex);
1061 /*
1062 * First re-do the cached lookup just in case it was created
Nick Pigginb04f7842010-08-18 04:37:34 +10001063 * while we waited for the directory semaphore, or the first
1064 * lookup failed due to an unrelated rename.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001065 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001066 * This could use version numbering or similar to avoid unnecessary
1067 * cache lookups, but then we'd have to do the first lookup in the
1068 * non-racy way. However in the common case here, everything should
1069 * be hot in cache, so would it be a big win?
Al Viro6e6b1bd2009-08-13 23:38:37 +04001070 */
1071 dentry = d_lookup(parent, name);
Nick Pigginbaa03892010-08-18 04:37:31 +10001072 if (likely(!dentry)) {
1073 dentry = d_alloc_and_lookup(parent, name, nd);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001074 mutex_unlock(&dir->i_mutex);
1075 if (IS_ERR(dentry))
1076 goto fail;
1077 goto done;
1078 }
Al Viro6e6b1bd2009-08-13 23:38:37 +04001079 /*
1080 * Uhhuh! Nasty case: the cache was re-populated while
1081 * we waited on the semaphore. Need to revalidate.
1082 */
1083 mutex_unlock(&dir->i_mutex);
Nick Piggin2e2e88e2010-08-18 04:37:30 +10001084 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -07001087 dentry = do_revalidate(dentry, nd);
1088 if (!dentry)
1089 goto need_lookup;
1090 if (IS_ERR(dentry))
1091 goto fail;
1092 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094fail:
1095 return PTR_ERR(dentry);
1096}
1097
1098/*
Al Viroac278a92010-02-16 18:09:36 +00001099 * This is a temporary kludge to deal with "automount" symlinks; proper
1100 * solution is to trigger them on follow_mount(), so that do_lookup()
1101 * would DTRT. To be killed before 2.6.34-final.
1102 */
1103static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
1104{
1105 return inode && unlikely(inode->i_op->follow_link) &&
1106 ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
1107}
1108
1109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001111 * This is the basic name resolution function, turning a pathname into
1112 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001114 * Returns 0 and nd will have valid dentry and mnt on success.
1115 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 */
Al Viro6de88d72009-08-09 01:41:57 +04001117static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int err;
1121 unsigned int lookup_flags = nd->flags;
1122
1123 while (*name=='/')
1124 name++;
1125 if (!*name)
1126 goto return_reval;
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -08001129 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 /* At this point we know we have a real path component. */
1132 for(;;) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001133 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 unsigned long hash;
1135 struct qstr this;
1136 unsigned int c;
1137
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001138 nd->flags |= LOOKUP_CONTINUE;
Nick Piggin31e6b012011-01-07 17:49:52 +11001139 if (nd->flags & LOOKUP_RCU) {
1140 err = exec_permission_rcu(nd->inode);
1141 if (err == -ECHILD) {
1142 if (nameidata_drop_rcu(nd))
1143 return -ECHILD;
1144 goto exec_again;
1145 }
1146 } else {
1147exec_again:
1148 err = exec_permission(nd->inode);
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (err)
1151 break;
1152
1153 this.name = name;
1154 c = *(const unsigned char *)name;
1155
1156 hash = init_name_hash();
1157 do {
1158 name++;
1159 hash = partial_name_hash(c, hash);
1160 c = *(const unsigned char *)name;
1161 } while (c && (c != '/'));
1162 this.len = name - (const char *) this.name;
1163 this.hash = end_name_hash(hash);
1164
1165 /* remove trailing slashes? */
1166 if (!c)
1167 goto last_component;
1168 while (*++name == '/');
1169 if (!*name)
1170 goto last_with_slashes;
1171
1172 /*
1173 * "." and ".." are special - ".." especially so because it has
1174 * to be able to know about the current root directory and
1175 * parent relationships.
1176 */
1177 if (this.name[0] == '.') switch (this.len) {
1178 default:
1179 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001180 case 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (this.name[1] != '.')
1182 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001183 if (nd->flags & LOOKUP_RCU) {
1184 if (follow_dotdot_rcu(nd))
1185 return -ECHILD;
1186 } else
1187 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /* fallthrough */
1189 case 1:
1190 continue;
1191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /* This does the actual lookups.. */
Nick Piggin31e6b012011-01-07 17:49:52 +11001193 err = do_lookup(nd, &this, &next, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (err)
1195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (!inode)
1198 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 if (inode->i_op->follow_link) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001201 /* We commonly drop rcu-walk here */
1202 if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
1203 return -ECHILD;
1204 BUG_ON(inode != next.dentry->d_inode);
Al Viro90ebe562005-06-06 13:35:58 -07001205 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (err)
1207 goto return_err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001208 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 err = -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001210 if (!nd->inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001212 } else {
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001213 path_to_nameidata(&next, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001214 nd->inode = inode;
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001217 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 break;
1219 continue;
1220 /* here ends the main loop */
1221
1222last_with_slashes:
1223 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1224last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001225 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1226 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (lookup_flags & LOOKUP_PARENT)
1228 goto lookup_parent;
1229 if (this.name[0] == '.') switch (this.len) {
1230 default:
1231 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001232 case 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (this.name[1] != '.')
1234 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001235 if (nd->flags & LOOKUP_RCU) {
1236 if (follow_dotdot_rcu(nd))
1237 return -ECHILD;
1238 } else
1239 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* fallthrough */
1241 case 1:
1242 goto return_reval;
1243 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001244 err = do_lookup(nd, &this, &next, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (err)
1246 break;
Al Viroac278a92010-02-16 18:09:36 +00001247 if (follow_on_final(inode, lookup_flags)) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001248 if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
1249 return -ECHILD;
1250 BUG_ON(inode != next.dentry->d_inode);
Al Viro90ebe562005-06-06 13:35:58 -07001251 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (err)
1253 goto return_err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001254 nd->inode = nd->path.dentry->d_inode;
1255 } else {
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001256 path_to_nameidata(&next, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001257 nd->inode = inode;
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 err = -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001260 if (!nd->inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 break;
1262 if (lookup_flags & LOOKUP_DIRECTORY) {
1263 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001264 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
1266 }
1267 goto return_base;
1268lookup_parent:
1269 nd->last = this;
1270 nd->last_type = LAST_NORM;
1271 if (this.name[0] != '.')
1272 goto return_base;
1273 if (this.len == 1)
1274 nd->last_type = LAST_DOT;
1275 else if (this.len == 2 && this.name[1] == '.')
1276 nd->last_type = LAST_DOTDOT;
1277 else
1278 goto return_base;
1279return_reval:
1280 /*
1281 * We bypassed the ordinary revalidation routines.
1282 * We may need to check the cached dentry for staleness.
1283 */
Jan Blunck4ac91372008-02-14 19:34:32 -08001284 if (nd->path.dentry && nd->path.dentry->d_sb &&
1285 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001286 if (nameidata_drop_rcu_maybe(nd))
1287 return -ECHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 err = -ESTALE;
1289 /* Note: we do not d_invalidate() */
Jan Blunck4ac91372008-02-14 19:34:32 -08001290 if (!nd->path.dentry->d_op->d_revalidate(
1291 nd->path.dentry, nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 break;
1293 }
1294return_base:
Nick Piggin31e6b012011-01-07 17:49:52 +11001295 if (nameidata_drop_rcu_last_maybe(nd))
1296 return -ECHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 0;
1298out_dput:
Nick Piggin31e6b012011-01-07 17:49:52 +11001299 if (!(nd->flags & LOOKUP_RCU))
1300 path_put_conditional(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 break;
1302 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001303 if (!(nd->flags & LOOKUP_RCU))
1304 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305return_err:
1306 return err;
1307}
1308
Nick Piggin31e6b012011-01-07 17:49:52 +11001309static inline int path_walk_rcu(const char *name, struct nameidata *nd)
1310{
1311 current->total_link_count = 0;
1312
1313 return link_path_walk(name, nd);
1314}
1315
1316static inline int path_walk_simple(const char *name, struct nameidata *nd)
1317{
1318 current->total_link_count = 0;
1319
1320 return link_path_walk(name, nd);
1321}
1322
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001323static int path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Al Viro6de88d72009-08-09 01:41:57 +04001325 struct path save = nd->path;
1326 int result;
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 current->total_link_count = 0;
Al Viro6de88d72009-08-09 01:41:57 +04001329
1330 /* make sure the stuff we saved doesn't go away */
1331 path_get(&save);
1332
1333 result = link_path_walk(name, nd);
1334 if (result == -ESTALE) {
1335 /* nd->path had been dropped */
1336 current->total_link_count = 0;
1337 nd->path = save;
1338 path_get(&nd->path);
1339 nd->flags |= LOOKUP_REVAL;
1340 result = link_path_walk(name, nd);
1341 }
1342
1343 path_put(&save);
1344
1345 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Nick Piggin31e6b012011-01-07 17:49:52 +11001348static void path_finish_rcu(struct nameidata *nd)
1349{
1350 if (nd->flags & LOOKUP_RCU) {
1351 /* RCU dangling. Cancel it. */
1352 nd->flags &= ~LOOKUP_RCU;
1353 nd->root.mnt = NULL;
1354 rcu_read_unlock();
1355 br_read_unlock(vfsmount_lock);
1356 }
1357 if (nd->file)
1358 fput(nd->file);
1359}
1360
1361static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1362{
1363 int retval = 0;
1364 int fput_needed;
1365 struct file *file;
1366
1367 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1368 nd->flags = flags | LOOKUP_RCU;
1369 nd->depth = 0;
1370 nd->root.mnt = NULL;
1371 nd->file = NULL;
1372
1373 if (*name=='/') {
1374 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +11001375 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001376
1377 br_read_lock(vfsmount_lock);
1378 rcu_read_lock();
1379
Nick Pigginc28cc362011-01-07 17:49:53 +11001380 do {
1381 seq = read_seqcount_begin(&fs->seq);
1382 nd->root = fs->root;
1383 nd->path = nd->root;
1384 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1385 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +11001386
1387 } else if (dfd == AT_FDCWD) {
1388 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +11001389 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001390
1391 br_read_lock(vfsmount_lock);
1392 rcu_read_lock();
1393
Nick Pigginc28cc362011-01-07 17:49:53 +11001394 do {
1395 seq = read_seqcount_begin(&fs->seq);
1396 nd->path = fs->pwd;
1397 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1398 } while (read_seqcount_retry(&fs->seq, seq));
1399
Nick Piggin31e6b012011-01-07 17:49:52 +11001400 } else {
1401 struct dentry *dentry;
1402
1403 file = fget_light(dfd, &fput_needed);
1404 retval = -EBADF;
1405 if (!file)
1406 goto out_fail;
1407
1408 dentry = file->f_path.dentry;
1409
1410 retval = -ENOTDIR;
1411 if (!S_ISDIR(dentry->d_inode->i_mode))
1412 goto fput_fail;
1413
1414 retval = file_permission(file, MAY_EXEC);
1415 if (retval)
1416 goto fput_fail;
1417
1418 nd->path = file->f_path;
1419 if (fput_needed)
1420 nd->file = file;
1421
Nick Pigginc28cc362011-01-07 17:49:53 +11001422 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001423 br_read_lock(vfsmount_lock);
1424 rcu_read_lock();
1425 }
1426 nd->inode = nd->path.dentry->d_inode;
1427 return 0;
1428
1429fput_fail:
1430 fput_light(file, fput_needed);
1431out_fail:
1432 return retval;
1433}
1434
Al Viro9b4a9b12009-04-07 11:44:16 -04001435static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001437 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001438 int fput_needed;
1439 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1442 nd->flags = flags;
1443 nd->depth = 0;
Al Viro2a737872009-04-07 11:49:53 -04001444 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (*name=='/') {
Al Viro2a737872009-04-07 11:49:53 -04001447 set_root(nd);
1448 nd->path = nd->root;
1449 path_get(&nd->root);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001450 } else if (dfd == AT_FDCWD) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02001451 get_fs_pwd(current->fs, &nd->path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001452 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001453 struct dentry *dentry;
1454
1455 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001456 retval = -EBADF;
1457 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001458 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001459
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001460 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001461
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001462 retval = -ENOTDIR;
1463 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001464 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001465
1466 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001467 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001468 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001469
Jan Blunck5dd784d2008-02-14 19:34:38 -08001470 nd->path = file->f_path;
1471 path_get(&file->f_path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001472
1473 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001475 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001476 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001477
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001478fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001479 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001480out_fail:
1481 return retval;
1482}
1483
1484/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1485static int do_path_lookup(int dfd, const char *name,
1486 unsigned int flags, struct nameidata *nd)
1487{
Nick Piggin31e6b012011-01-07 17:49:52 +11001488 int retval;
1489
1490 /*
1491 * Path walking is largely split up into 2 different synchronisation
1492 * schemes, rcu-walk and ref-walk (explained in
1493 * Documentation/filesystems/path-lookup.txt). These share much of the
1494 * path walk code, but some things particularly setup, cleanup, and
1495 * following mounts are sufficiently divergent that functions are
1496 * duplicated. Typically there is a function foo(), and its RCU
1497 * analogue, foo_rcu().
1498 *
1499 * -ECHILD is the error number of choice (just to avoid clashes) that
1500 * is returned if some aspect of an rcu-walk fails. Such an error must
1501 * be handled by restarting a traditional ref-walk (which will always
1502 * be able to complete).
1503 */
1504 retval = path_init_rcu(dfd, name, flags, nd);
1505 if (unlikely(retval))
1506 return retval;
1507 retval = path_walk_rcu(name, nd);
1508 path_finish_rcu(nd);
Al Viro2a737872009-04-07 11:49:53 -04001509 if (nd->root.mnt) {
1510 path_put(&nd->root);
1511 nd->root.mnt = NULL;
1512 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001513
1514 if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
1515 /* slower, locked walk */
1516 if (retval == -ESTALE)
1517 flags |= LOOKUP_REVAL;
1518 retval = path_init(dfd, name, flags, nd);
1519 if (unlikely(retval))
1520 return retval;
1521 retval = path_walk(name, nd);
1522 if (nd->root.mnt) {
1523 path_put(&nd->root);
1524 nd->root.mnt = NULL;
1525 }
1526 }
1527
1528 if (likely(!retval)) {
1529 if (unlikely(!audit_dummy_context())) {
1530 if (nd->path.dentry && nd->inode)
1531 audit_inode(name, nd->path.dentry);
1532 }
1533 }
1534
Al Viro9b4a9b12009-04-07 11:44:16 -04001535 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001538int path_lookup(const char *name, unsigned int flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001539 struct nameidata *nd)
1540{
1541 return do_path_lookup(AT_FDCWD, name, flags, nd);
1542}
1543
Al Virod1811462008-08-02 00:49:18 -04001544int kern_path(const char *name, unsigned int flags, struct path *path)
1545{
1546 struct nameidata nd;
1547 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1548 if (!res)
1549 *path = nd.path;
1550 return res;
1551}
1552
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001553/**
1554 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1555 * @dentry: pointer to dentry of the base directory
1556 * @mnt: pointer to vfs mount of the base directory
1557 * @name: pointer to file name
1558 * @flags: lookup flags
1559 * @nd: pointer to nameidata
1560 */
1561int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1562 const char *name, unsigned int flags,
1563 struct nameidata *nd)
1564{
1565 int retval;
1566
1567 /* same as do_path_lookup */
1568 nd->last_type = LAST_ROOT;
1569 nd->flags = flags;
1570 nd->depth = 0;
1571
Jan Blunckc8e7f442008-06-09 16:40:35 -07001572 nd->path.dentry = dentry;
1573 nd->path.mnt = mnt;
1574 path_get(&nd->path);
Al Viro5b857112009-04-07 11:53:49 -04001575 nd->root = nd->path;
1576 path_get(&nd->root);
Nick Piggin31e6b012011-01-07 17:49:52 +11001577 nd->inode = nd->path.dentry->d_inode;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001578
1579 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001580 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
Nick Piggin31e6b012011-01-07 17:49:52 +11001581 nd->inode))
Jan Blunck4ac91372008-02-14 19:34:32 -08001582 audit_inode(name, nd->path.dentry);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001583
Al Viro5b857112009-04-07 11:53:49 -04001584 path_put(&nd->root);
1585 nd->root.mnt = NULL;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001586
Al Viro2a737872009-04-07 11:49:53 -04001587 return retval;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001588}
1589
Christoph Hellwigeead1912007-10-16 23:25:38 -07001590static struct dentry *__lookup_hash(struct qstr *name,
1591 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001593 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001594 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int err;
1596
Christoph Hellwig81fca442010-10-06 10:47:47 +02001597 err = exec_permission(inode);
1598 if (err)
1599 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /*
1602 * See if the low-level filesystem might want
1603 * to use its own hash..
1604 */
1605 if (base->d_op && base->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001606 err = base->d_op->d_hash(base, inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 dentry = ERR_PTR(err);
1608 if (err < 0)
1609 goto out;
1610 }
1611
Nick Pigginb04f7842010-08-18 04:37:34 +10001612 /*
1613 * Don't bother with __d_lookup: callers are for creat as
1614 * well as unlink, so a lot of the time it would cost
1615 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001616 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001617 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001618
1619 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
1620 dentry = do_revalidate(dentry, nd);
1621
Nick Pigginbaa03892010-08-18 04:37:31 +10001622 if (!dentry)
1623 dentry = d_alloc_and_lookup(base, name, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624out:
1625 return dentry;
1626}
1627
James Morris057f6c02007-04-26 00:12:05 -07001628/*
1629 * Restricted form of lookup. Doesn't follow links, single-component only,
1630 * needs parent already locked. Doesn't follow mounts.
1631 * SMP-safe.
1632 */
Adrian Bunka244e162006-03-31 02:32:11 -08001633static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Jan Blunck4ac91372008-02-14 19:34:32 -08001635 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Christoph Hellwigeead1912007-10-16 23:25:38 -07001638static int __lookup_one_len(const char *name, struct qstr *this,
1639 struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 unsigned int c;
1643
James Morris057f6c02007-04-26 00:12:05 -07001644 this->name = name;
1645 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001647 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 hash = init_name_hash();
1650 while (len--) {
1651 c = *(const unsigned char *)name++;
1652 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001653 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 hash = partial_name_hash(c, hash);
1655 }
James Morris057f6c02007-04-26 00:12:05 -07001656 this->hash = end_name_hash(hash);
1657 return 0;
1658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Christoph Hellwigeead1912007-10-16 23:25:38 -07001660/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001661 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001662 * @name: pathname component to lookup
1663 * @base: base directory to lookup from
1664 * @len: maximum length @len should be interpreted to
1665 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001666 * Note that this routine is purely a helper for filesystem usage and should
1667 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001668 * nameidata argument is passed to the filesystem methods and a filesystem
1669 * using this helper needs to be prepared for that.
1670 */
James Morris057f6c02007-04-26 00:12:05 -07001671struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1672{
1673 int err;
1674 struct qstr this;
1675
David Woodhouse2f9092e2009-04-20 23:18:37 +01001676 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1677
James Morris057f6c02007-04-26 00:12:05 -07001678 err = __lookup_one_len(name, &this, base, len);
1679 if (err)
1680 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001681
Christoph Hellwig49705b72005-11-08 21:35:06 -08001682 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001683}
1684
Al Viro2d8f3032008-07-22 09:59:21 -04001685int user_path_at(int dfd, const char __user *name, unsigned flags,
1686 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Al Viro2d8f3032008-07-22 09:59:21 -04001688 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 char *tmp = getname(name);
1690 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001692
1693 BUG_ON(flags & LOOKUP_PARENT);
1694
1695 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001697 if (!err)
1698 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 return err;
1701}
1702
Al Viro2ad94ae2008-07-21 09:32:51 -04001703static int user_path_parent(int dfd, const char __user *path,
1704 struct nameidata *nd, char **name)
1705{
1706 char *s = getname(path);
1707 int error;
1708
1709 if (IS_ERR(s))
1710 return PTR_ERR(s);
1711
1712 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1713 if (error)
1714 putname(s);
1715 else
1716 *name = s;
1717
1718 return error;
1719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721/*
1722 * It's inline, so penalty for filesystems that don't use sticky bit is
1723 * minimal.
1724 */
1725static inline int check_sticky(struct inode *dir, struct inode *inode)
1726{
David Howellsda9592e2008-11-14 10:39:05 +11001727 uid_t fsuid = current_fsuid();
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (!(dir->i_mode & S_ISVTX))
1730 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001731 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001733 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return 0;
1735 return !capable(CAP_FOWNER);
1736}
1737
1738/*
1739 * Check whether we can remove a link victim from directory dir, check
1740 * whether the type of victim is right.
1741 * 1. We can't do it if dir is read-only (done in permission())
1742 * 2. We should have write and exec permissions on dir
1743 * 3. We can't remove anything from append-only dir
1744 * 4. We can't do anything with immutable dir (done in permission())
1745 * 5. If the sticky bit on dir is set we should either
1746 * a. be owner of dir, or
1747 * b. be owner of victim, or
1748 * c. have CAP_FOWNER capability
1749 * 6. If the victim is append-only or immutable we can't do antyhing with
1750 * links pointing to it.
1751 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1752 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1753 * 9. We can't remove a root or mountpoint.
1754 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1755 * nfs_async_unlink().
1756 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001757static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 int error;
1760
1761 if (!victim->d_inode)
1762 return -ENOENT;
1763
1764 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001765 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Al Virof419a2e2008-07-22 00:07:17 -04001767 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (error)
1769 return error;
1770 if (IS_APPEND(dir))
1771 return -EPERM;
1772 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001773 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return -EPERM;
1775 if (isdir) {
1776 if (!S_ISDIR(victim->d_inode->i_mode))
1777 return -ENOTDIR;
1778 if (IS_ROOT(victim))
1779 return -EBUSY;
1780 } else if (S_ISDIR(victim->d_inode->i_mode))
1781 return -EISDIR;
1782 if (IS_DEADDIR(dir))
1783 return -ENOENT;
1784 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1785 return -EBUSY;
1786 return 0;
1787}
1788
1789/* Check whether we can create an object with dentry child in directory
1790 * dir.
1791 * 1. We can't do it if child already exists (open has special treatment for
1792 * this case, but since we are inlined it's OK)
1793 * 2. We can't do it if dir is read-only (done in permission())
1794 * 3. We should have write and exec permissions on dir
1795 * 4. We can't do it if dir is immutable (done in permission())
1796 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001797static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 if (child->d_inode)
1800 return -EEXIST;
1801 if (IS_DEADDIR(dir))
1802 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001803 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806/*
1807 * p1 and p2 should be directories on the same fs.
1808 */
1809struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1810{
1811 struct dentry *p;
1812
1813 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001814 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return NULL;
1816 }
1817
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001818 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001820 p = d_ancestor(p2, p1);
1821 if (p) {
1822 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1823 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1824 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001827 p = d_ancestor(p1, p2);
1828 if (p) {
1829 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1830 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1831 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Ingo Molnarf2eace22006-07-03 00:25:05 -07001834 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1835 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return NULL;
1837}
1838
1839void unlock_rename(struct dentry *p1, struct dentry *p2)
1840{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001841 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001843 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001844 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846}
1847
1848int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1849 struct nameidata *nd)
1850{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001851 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 if (error)
1854 return error;
1855
Al Viroacfa4382008-12-04 10:06:33 -05001856 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return -EACCES; /* shouldn't it be ENOSYS? */
1858 mode &= S_IALLUGO;
1859 mode |= S_IFREG;
1860 error = security_inode_create(dir, dentry, mode);
1861 if (error)
1862 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001864 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001865 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return error;
1867}
1868
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001869int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001871 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct inode *inode = dentry->d_inode;
1873 int error;
1874
1875 if (!inode)
1876 return -ENOENT;
1877
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001878 switch (inode->i_mode & S_IFMT) {
1879 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001881 case S_IFDIR:
1882 if (acc_mode & MAY_WRITE)
1883 return -EISDIR;
1884 break;
1885 case S_IFBLK:
1886 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001887 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001889 /*FALLTHRU*/
1890 case S_IFIFO:
1891 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001893 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001894 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001895
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001896 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001897 if (error)
1898 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /*
1901 * An append-only file must be opened in append mode for writing.
1902 */
1903 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001904 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001905 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001907 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
1910 /* O_NOATIME can only be set by the owner or superuser */
Al Viro7715b522009-12-16 03:54:00 -05001911 if (flag & O_NOATIME && !is_owner_or_cap(inode))
1912 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /*
1915 * Ensure there are no outstanding leases on the file.
1916 */
Al Virob65a9cf2009-12-16 06:27:40 -05001917 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Al Viro7715b522009-12-16 03:54:00 -05001920static int handle_truncate(struct path *path)
1921{
1922 struct inode *inode = path->dentry->d_inode;
1923 int error = get_write_access(inode);
1924 if (error)
1925 return error;
1926 /*
1927 * Refuse to truncate files with mandatory locks held on them.
1928 */
1929 error = locks_verify_locked(inode);
1930 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09001931 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05001932 if (!error) {
1933 error = do_truncate(path->dentry, 0,
1934 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1935 NULL);
1936 }
1937 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04001938 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Dave Hansend57999e2008-02-15 14:37:27 -08001941/*
1942 * Be careful about ever adding any more callers of this
1943 * function. Its flags must be in the namei format, not
1944 * what get passed to sys_open().
1945 */
1946static int __open_namei_create(struct nameidata *nd, struct path *path,
Al Viro8737c932009-12-24 06:47:55 -05001947 int open_flag, int mode)
Dave Hansenaab520e2006-09-30 23:29:02 -07001948{
1949 int error;
Jan Blunck4ac91372008-02-14 19:34:32 -08001950 struct dentry *dir = nd->path.dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001951
1952 if (!IS_POSIXACL(dir->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04001953 mode &= ~current_umask();
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09001954 error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1955 if (error)
1956 goto out_unlock;
Dave Hansenaab520e2006-09-30 23:29:02 -07001957 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09001958out_unlock:
Dave Hansenaab520e2006-09-30 23:29:02 -07001959 mutex_unlock(&dir->d_inode->i_mutex);
Jan Blunck4ac91372008-02-14 19:34:32 -08001960 dput(nd->path.dentry);
1961 nd->path.dentry = path->dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11001962
Dave Hansenaab520e2006-09-30 23:29:02 -07001963 if (error)
1964 return error;
1965 /* Don't check for write permission, don't truncate */
Al Viro8737c932009-12-24 06:47:55 -05001966 return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
Dave Hansenaab520e2006-09-30 23:29:02 -07001967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/*
Dave Hansend57999e2008-02-15 14:37:27 -08001970 * Note that while the flag value (low two bits) for sys_open means:
1971 * 00 - read-only
1972 * 01 - write-only
1973 * 10 - read-write
1974 * 11 - special
1975 * it is changed into
1976 * 00 - no permissions needed
1977 * 01 - read-permission
1978 * 10 - write-permission
1979 * 11 - read-write
1980 * for the internal routines (ie open_namei()/follow_link() etc)
1981 * This is more logical, and also allows the 00 "no perm needed"
1982 * to be used for symlinks (where the permissions are checked
1983 * later).
1984 *
1985*/
1986static inline int open_to_namei_flags(int flag)
1987{
1988 if ((flag+1) & O_ACCMODE)
1989 flag++;
1990 return flag;
1991}
1992
Al Viro7715b522009-12-16 03:54:00 -05001993static int open_will_truncate(int flag, struct inode *inode)
Dave Hansen4a3fd212008-02-15 14:37:48 -08001994{
1995 /*
1996 * We'll never write to the fs underlying
1997 * a device file.
1998 */
1999 if (special_file(inode->i_mode))
2000 return 0;
2001 return (flag & O_TRUNC);
2002}
2003
Al Viro648fa862009-12-24 01:26:48 -05002004static struct file *finish_open(struct nameidata *nd,
Al Viro9a661792009-12-24 06:49:47 -05002005 int open_flag, int acc_mode)
Al Viro648fa862009-12-24 01:26:48 -05002006{
2007 struct file *filp;
2008 int will_truncate;
2009 int error;
2010
Al Viro9a661792009-12-24 06:49:47 -05002011 will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
Al Viro648fa862009-12-24 01:26:48 -05002012 if (will_truncate) {
2013 error = mnt_want_write(nd->path.mnt);
2014 if (error)
2015 goto exit;
2016 }
2017 error = may_open(&nd->path, acc_mode, open_flag);
2018 if (error) {
2019 if (will_truncate)
2020 mnt_drop_write(nd->path.mnt);
2021 goto exit;
2022 }
2023 filp = nameidata_to_filp(nd);
2024 if (!IS_ERR(filp)) {
2025 error = ima_file_check(filp, acc_mode);
2026 if (error) {
2027 fput(filp);
2028 filp = ERR_PTR(error);
2029 }
2030 }
2031 if (!IS_ERR(filp)) {
Al Viro648fa862009-12-24 01:26:48 -05002032 if (will_truncate) {
2033 error = handle_truncate(&nd->path);
2034 if (error) {
2035 fput(filp);
2036 filp = ERR_PTR(error);
2037 }
2038 }
2039 }
2040 /*
2041 * It is now safe to drop the mnt write
2042 * because the filp has had a write taken
2043 * on its behalf.
2044 */
2045 if (will_truncate)
2046 mnt_drop_write(nd->path.mnt);
Al Virod893f1b2010-10-29 03:30:42 -04002047 path_put(&nd->path);
Al Viro648fa862009-12-24 01:26:48 -05002048 return filp;
2049
2050exit:
2051 if (!IS_ERR(nd->intent.open.file))
2052 release_open_intent(nd);
2053 path_put(&nd->path);
2054 return ERR_PTR(error);
2055}
2056
Nick Piggin31e6b012011-01-07 17:49:52 +11002057/*
2058 * Handle O_CREAT case for do_filp_open
2059 */
Al Virofb1cc552009-12-24 01:58:28 -05002060static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viro5b369df2009-12-24 06:51:13 -05002061 int open_flag, int acc_mode,
Al Viro3e297b62010-03-26 12:40:13 -04002062 int mode, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002063{
Al Viroa1e28032009-12-24 02:12:06 -05002064 struct dentry *dir = nd->path.dentry;
Al Virofb1cc552009-12-24 01:58:28 -05002065 struct file *filp;
Al Viro1f36f772009-12-26 10:56:19 -05002066 int error = -EISDIR;
Al Virofb1cc552009-12-24 01:58:28 -05002067
Al Viro1f36f772009-12-26 10:56:19 -05002068 switch (nd->last_type) {
2069 case LAST_DOTDOT:
2070 follow_dotdot(nd);
2071 dir = nd->path.dentry;
Neil Brown176306f2010-05-24 16:57:56 +10002072 case LAST_DOT:
Al Viro1f36f772009-12-26 10:56:19 -05002073 if (nd->path.mnt->mnt_sb->s_type->fs_flags & FS_REVAL_DOT) {
2074 if (!dir->d_op->d_revalidate(dir, nd)) {
2075 error = -ESTALE;
2076 goto exit;
2077 }
2078 }
2079 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002080 case LAST_ROOT:
Nick Piggin31e6b012011-01-07 17:49:52 +11002081 goto exit;
Al Viro1f36f772009-12-26 10:56:19 -05002082 case LAST_BIND:
2083 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002084 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002085 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002086
Al Viro1f36f772009-12-26 10:56:19 -05002087 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002088 if (nd->last.name[nd->last.len])
2089 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002090
Al Viroa1e28032009-12-24 02:12:06 -05002091 mutex_lock(&dir->d_inode->i_mutex);
2092
2093 path->dentry = lookup_hash(nd);
2094 path->mnt = nd->path.mnt;
2095
Al Virofb1cc552009-12-24 01:58:28 -05002096 error = PTR_ERR(path->dentry);
2097 if (IS_ERR(path->dentry)) {
2098 mutex_unlock(&dir->d_inode->i_mutex);
2099 goto exit;
2100 }
2101
2102 if (IS_ERR(nd->intent.open.file)) {
2103 error = PTR_ERR(nd->intent.open.file);
2104 goto exit_mutex_unlock;
2105 }
2106
2107 /* Negative dentry, just create the file */
2108 if (!path->dentry->d_inode) {
2109 /*
2110 * This write is needed to ensure that a
2111 * ro->rw transition does not occur between
2112 * the time when the file is created and when
2113 * a permanent write count is taken through
2114 * the 'struct file' in nameidata_to_filp().
2115 */
2116 error = mnt_want_write(nd->path.mnt);
2117 if (error)
2118 goto exit_mutex_unlock;
2119 error = __open_namei_create(nd, path, open_flag, mode);
2120 if (error) {
2121 mnt_drop_write(nd->path.mnt);
2122 goto exit;
2123 }
2124 filp = nameidata_to_filp(nd);
2125 mnt_drop_write(nd->path.mnt);
Al Virod893f1b2010-10-29 03:30:42 -04002126 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002127 if (!IS_ERR(filp)) {
2128 error = ima_file_check(filp, acc_mode);
2129 if (error) {
2130 fput(filp);
2131 filp = ERR_PTR(error);
2132 }
2133 }
2134 return filp;
2135 }
2136
2137 /*
2138 * It already exists.
2139 */
2140 mutex_unlock(&dir->d_inode->i_mutex);
2141 audit_inode(pathname, path->dentry);
2142
2143 error = -EEXIST;
Al Viro5b369df2009-12-24 06:51:13 -05002144 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002145 goto exit_dput;
2146
2147 if (__follow_mount(path)) {
2148 error = -ELOOP;
Al Viro5b369df2009-12-24 06:51:13 -05002149 if (open_flag & O_NOFOLLOW)
Al Virofb1cc552009-12-24 01:58:28 -05002150 goto exit_dput;
2151 }
2152
2153 error = -ENOENT;
2154 if (!path->dentry->d_inode)
2155 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002156
2157 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002158 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002159
2160 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002161 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002162 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002163 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002164 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002165ok:
Al Viro9a661792009-12-24 06:49:47 -05002166 filp = finish_open(nd, open_flag, acc_mode);
Al Virofb1cc552009-12-24 01:58:28 -05002167 return filp;
2168
2169exit_mutex_unlock:
2170 mutex_unlock(&dir->d_inode->i_mutex);
2171exit_dput:
2172 path_put_conditional(path, nd);
2173exit:
2174 if (!IS_ERR(nd->intent.open.file))
2175 release_open_intent(nd);
Al Virofb1cc552009-12-24 01:58:28 -05002176 path_put(&nd->path);
2177 return ERR_PTR(error);
2178}
2179
Dave Hansend57999e2008-02-15 14:37:27 -08002180/*
Dave Hansen4a3fd212008-02-15 14:37:48 -08002181 * Note that the low bits of the passed in "open_flag"
2182 * are not the same as in the local variable "flag". See
2183 * open_to_namei_flags() for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002185struct file *do_filp_open(int dfd, const char *pathname,
Al Viro6e8341a2009-04-06 11:16:22 -04002186 int open_flag, int mode, int acc_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Dave Hansen4a3fd212008-02-15 14:37:48 -08002188 struct file *filp;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002189 struct nameidata nd;
Al Viro6e8341a2009-04-06 11:16:22 -04002190 int error;
Al Viro9850c052010-01-13 15:01:15 -05002191 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 int count = 0;
Dave Hansend57999e2008-02-15 14:37:27 -08002193 int flag = open_to_namei_flags(open_flag);
Nick Piggin31e6b012011-01-07 17:49:52 +11002194 int flags;
Al Viro1f36f772009-12-26 10:56:19 -05002195
2196 if (!(open_flag & O_CREAT))
2197 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Lino Sanfilippob1085ba2010-11-05 17:05:27 +01002199 /* Must never be set by userspace */
2200 open_flag &= ~FMODE_NONOTIFY;
2201
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01002202 /*
2203 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
2204 * check for O_DSYNC if the need any syncing at all we enforce it's
2205 * always set instead of having to deal with possibly weird behaviour
2206 * for malicious applications setting only __O_SYNC.
2207 */
2208 if (open_flag & __O_SYNC)
2209 open_flag |= O_DSYNC;
2210
Al Viro6e8341a2009-04-06 11:16:22 -04002211 if (!acc_mode)
Al Viro6d125522009-12-24 06:58:56 -05002212 acc_mode = MAY_OPEN | ACC_MODE(open_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Trond Myklebust834f2a42005-10-18 14:20:16 -07002214 /* O_TRUNC implies we need access checks for write permissions */
Al Viro4296e2c2009-12-24 07:15:41 -05002215 if (open_flag & O_TRUNC)
Trond Myklebust834f2a42005-10-18 14:20:16 -07002216 acc_mode |= MAY_WRITE;
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /* Allow the LSM permission hook to distinguish append
2219 access from general write access. */
Al Viro4296e2c2009-12-24 07:15:41 -05002220 if (open_flag & O_APPEND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 acc_mode |= MAY_APPEND;
2222
Nick Piggin31e6b012011-01-07 17:49:52 +11002223 flags = LOOKUP_OPEN;
2224 if (open_flag & O_CREAT) {
2225 flags |= LOOKUP_CREATE;
2226 if (open_flag & O_EXCL)
2227 flags |= LOOKUP_EXCL;
J. R. Okajima654f5622009-06-18 23:30:15 +09002228 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002229 if (open_flag & O_DIRECTORY)
2230 flags |= LOOKUP_DIRECTORY;
2231 if (!(open_flag & O_NOFOLLOW))
2232 flags |= LOOKUP_FOLLOW;
2233
2234 filp = get_empty_filp();
2235 if (!filp)
2236 return ERR_PTR(-ENFILE);
2237
2238 filp->f_flags = open_flag;
2239 nd.intent.open.file = filp;
2240 nd.intent.open.flags = flag;
2241 nd.intent.open.create_mode = mode;
2242
2243 if (open_flag & O_CREAT)
2244 goto creat;
2245
2246 /* !O_CREAT, simple open */
2247 error = do_path_lookup(dfd, pathname, flags, &nd);
2248 if (unlikely(error))
2249 goto out_filp;
2250 error = -ELOOP;
2251 if (!(nd.flags & LOOKUP_FOLLOW)) {
2252 if (nd.inode->i_op->follow_link)
2253 goto out_path;
2254 }
2255 error = -ENOTDIR;
2256 if (nd.flags & LOOKUP_DIRECTORY) {
2257 if (!nd.inode->i_op->lookup)
2258 goto out_path;
2259 }
2260 audit_inode(pathname, nd.path.dentry);
2261 filp = finish_open(&nd, open_flag, acc_mode);
2262 return filp;
2263
2264creat:
2265 /* OK, have to create the file. Find the parent. */
2266 error = path_init_rcu(dfd, pathname,
2267 LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2268 if (error)
2269 goto out_filp;
2270 error = path_walk_rcu(pathname, &nd);
2271 path_finish_rcu(&nd);
2272 if (unlikely(error == -ECHILD || error == -ESTALE)) {
2273 /* slower, locked walk */
2274 if (error == -ESTALE) {
2275reval:
2276 flags |= LOOKUP_REVAL;
2277 }
2278 error = path_init(dfd, pathname,
2279 LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2280 if (error)
2281 goto out_filp;
2282
2283 error = path_walk_simple(pathname, &nd);
2284 }
2285 if (unlikely(error))
2286 goto out_filp;
2287 if (unlikely(!audit_dummy_context()))
Al Viro9b4a9b12009-04-07 11:44:16 -04002288 audit_inode(pathname, nd.path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 /*
Al Viroa2c36b42009-12-24 03:39:50 -05002291 * We have the parent and last component.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 */
Nick Piggin31e6b012011-01-07 17:49:52 +11002293 nd.flags = flags;
Al Viro3e297b62010-03-26 12:40:13 -04002294 filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002295 while (unlikely(!filp)) { /* trailing symlink */
Al Virodef4af32009-12-26 08:37:05 -05002296 struct path holder;
Al Virodef4af32009-12-26 08:37:05 -05002297 void *cookie;
Al Viro806b6812009-12-26 07:16:40 -05002298 error = -ELOOP;
Al Viro1f36f772009-12-26 10:56:19 -05002299 /* S_ISDIR part is a temporary automount kludge */
Nick Piggin31e6b012011-01-07 17:49:52 +11002300 if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(nd.inode->i_mode))
Al Viro1f36f772009-12-26 10:56:19 -05002301 goto exit_dput;
2302 if (count++ == 32)
Al Viro806b6812009-12-26 07:16:40 -05002303 goto exit_dput;
2304 /*
2305 * This is subtle. Instead of calling do_follow_link() we do
2306 * the thing by hands. The reason is that this way we have zero
2307 * link_count and path_walk() (called from ->follow_link)
2308 * honoring LOOKUP_PARENT. After that we have the parent and
2309 * last component, i.e. we are in the same situation as after
2310 * the first path_walk(). Well, almost - if the last component
2311 * is normal we get its copy stored in nd->last.name and we will
2312 * have to putname() it when we are done. Procfs-like symlinks
2313 * just set LAST_BIND.
2314 */
2315 nd.flags |= LOOKUP_PARENT;
2316 error = security_inode_follow_link(path.dentry, &nd);
2317 if (error)
2318 goto exit_dput;
Al Virodef4af32009-12-26 08:37:05 -05002319 error = __do_follow_link(&path, &nd, &cookie);
2320 if (unlikely(error)) {
Nick Piggin31e6b012011-01-07 17:49:52 +11002321 if (!IS_ERR(cookie) && nd.inode->i_op->put_link)
2322 nd.inode->i_op->put_link(path.dentry, &nd, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002323 /* nd.path had been dropped */
Nick Piggin31e6b012011-01-07 17:49:52 +11002324 nd.path = path;
2325 goto out_path;
Al Viro806b6812009-12-26 07:16:40 -05002326 }
Al Virodef4af32009-12-26 08:37:05 -05002327 holder = path;
Al Viro806b6812009-12-26 07:16:40 -05002328 nd.flags &= ~LOOKUP_PARENT;
Al Viro3e297b62010-03-26 12:40:13 -04002329 filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
Nick Piggin31e6b012011-01-07 17:49:52 +11002330 if (nd.inode->i_op->put_link)
2331 nd.inode->i_op->put_link(holder.dentry, &nd, cookie);
Al Virodef4af32009-12-26 08:37:05 -05002332 path_put(&holder);
Al Viro806b6812009-12-26 07:16:40 -05002333 }
Al Viro10fa8e62009-12-26 07:09:49 -05002334out:
Al Viro2a737872009-04-07 11:49:53 -04002335 if (nd.root.mnt)
2336 path_put(&nd.root);
Nick Piggin31e6b012011-01-07 17:49:52 +11002337 if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
Al Viro10fa8e62009-12-26 07:09:49 -05002338 goto reval;
Al Viro10fa8e62009-12-26 07:09:49 -05002339 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Al Viro806b6812009-12-26 07:16:40 -05002341exit_dput:
2342 path_put_conditional(&path, &nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002343out_path:
2344 path_put(&nd.path);
2345out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002346 if (!IS_ERR(nd.intent.open.file))
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002347 release_open_intent(&nd);
Al Viro806b6812009-12-26 07:16:40 -05002348 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002349 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
2352/**
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002353 * filp_open - open file and return file pointer
2354 *
2355 * @filename: path to open
2356 * @flags: open flags as per the open(2) second argument
2357 * @mode: mode for the new file if O_CREAT is set, else ignored
2358 *
2359 * This is the helper to open a file from kernelspace if you really
2360 * have to. But in generally you should not do this, so please move
2361 * along, nothing to see here..
2362 */
2363struct file *filp_open(const char *filename, int flags, int mode)
2364{
Al Viro6e8341a2009-04-06 11:16:22 -04002365 return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002366}
2367EXPORT_SYMBOL(filp_open);
2368
2369/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 * lookup_create - lookup a dentry, creating it if it doesn't exist
2371 * @nd: nameidata info
2372 * @is_dir: directory flag
2373 *
2374 * Simple function to lookup and return a dentry and create it
2375 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002376 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002377 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 */
2379struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2380{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002381 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Jan Blunck4ac91372008-02-14 19:34:32 -08002383 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002384 /*
2385 * Yucky last component or no last component at all?
2386 * (foo/., foo/.., /////)
2387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (nd->last_type != LAST_NORM)
2389 goto fail;
2390 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002391 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002392 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002393
2394 /*
2395 * Do the final lookup.
2396 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002397 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (IS_ERR(dentry))
2399 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002400
Al Viroe9baf6e2008-05-15 04:49:12 -04002401 if (dentry->d_inode)
2402 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002403 /*
2404 * Special case - lookup gave negative, but... we had foo/bar/
2405 * From the vfs_mknod() POV we just have a negative dentry -
2406 * all is fine. Let's be bastards - you had / on the end, you've
2407 * been asking for (non-existent) directory. -ENOENT for you.
2408 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002409 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2410 dput(dentry);
2411 dentry = ERR_PTR(-ENOENT);
2412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002414eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002416 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417fail:
2418 return dentry;
2419}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002420EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2423{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002424 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 if (error)
2427 return error;
2428
2429 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2430 return -EPERM;
2431
Al Viroacfa4382008-12-04 10:06:33 -05002432 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return -EPERM;
2434
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002435 error = devcgroup_inode_mknod(mode, dev);
2436 if (error)
2437 return error;
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 error = security_inode_mknod(dir, dentry, mode, dev);
2440 if (error)
2441 return error;
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002444 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002445 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return error;
2447}
2448
Dave Hansen463c3192008-02-15 14:37:57 -08002449static int may_mknod(mode_t mode)
2450{
2451 switch (mode & S_IFMT) {
2452 case S_IFREG:
2453 case S_IFCHR:
2454 case S_IFBLK:
2455 case S_IFIFO:
2456 case S_IFSOCK:
2457 case 0: /* zero mode translates to S_IFREG */
2458 return 0;
2459 case S_IFDIR:
2460 return -EPERM;
2461 default:
2462 return -EINVAL;
2463 }
2464}
2465
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002466SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2467 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Al Viro2ad94ae2008-07-21 09:32:51 -04002469 int error;
2470 char *tmp;
2471 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct nameidata nd;
2473
2474 if (S_ISDIR(mode))
2475 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Al Viro2ad94ae2008-07-21 09:32:51 -04002477 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002479 return error;
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002482 if (IS_ERR(dentry)) {
2483 error = PTR_ERR(dentry);
2484 goto out_unlock;
2485 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002486 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002487 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002488 error = may_mknod(mode);
2489 if (error)
2490 goto out_dput;
2491 error = mnt_want_write(nd.path.mnt);
2492 if (error)
2493 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002494 error = security_path_mknod(&nd.path, dentry, mode, dev);
2495 if (error)
2496 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002497 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002499 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 break;
2501 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002502 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 new_decode_dev(dev));
2504 break;
2505 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002506 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002509out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002510 mnt_drop_write(nd.path.mnt);
2511out_dput:
2512 dput(dentry);
2513out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002514 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002515 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 putname(tmp);
2517
2518 return error;
2519}
2520
Heiko Carstens3480b252009-01-14 14:14:16 +01002521SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002522{
2523 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2527{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002528 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 if (error)
2531 return error;
2532
Al Viroacfa4382008-12-04 10:06:33 -05002533 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return -EPERM;
2535
2536 mode &= (S_IRWXUGO|S_ISVTX);
2537 error = security_inode_mkdir(dir, dentry, mode);
2538 if (error)
2539 return error;
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002542 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002543 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return error;
2545}
2546
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002547SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 int error = 0;
2550 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002551 struct dentry *dentry;
2552 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Al Viro2ad94ae2008-07-21 09:32:51 -04002554 error = user_path_parent(dfd, pathname, &nd, &tmp);
2555 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002556 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Dave Hansen6902d922006-09-30 23:29:01 -07002558 dentry = lookup_create(&nd, 1);
2559 error = PTR_ERR(dentry);
2560 if (IS_ERR(dentry))
2561 goto out_unlock;
2562
Jan Blunck4ac91372008-02-14 19:34:32 -08002563 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002564 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002565 error = mnt_want_write(nd.path.mnt);
2566 if (error)
2567 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002568 error = security_path_mkdir(&nd.path, dentry, mode);
2569 if (error)
2570 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002571 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002572out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002573 mnt_drop_write(nd.path.mnt);
2574out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002575 dput(dentry);
2576out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002577 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002578 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002579 putname(tmp);
2580out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return error;
2582}
2583
Heiko Carstens3cdad422009-01-14 14:14:22 +01002584SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002585{
2586 return sys_mkdirat(AT_FDCWD, pathname, mode);
2587}
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589/*
2590 * We try to drop the dentry early: we should have
2591 * a usage count of 2 if we're the only user of this
2592 * dentry, and if that is true (possibly after pruning
2593 * the dcache), then we drop the dentry now.
2594 *
2595 * A low-level filesystem can, if it choses, legally
2596 * do a
2597 *
2598 * if (!d_unhashed(dentry))
2599 * return -EBUSY;
2600 *
2601 * if it cannot handle the case of removing a directory
2602 * that is still in use by something else..
2603 */
2604void dentry_unhash(struct dentry *dentry)
2605{
2606 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002607 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002609 if (dentry->d_count == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 __d_drop(dentry);
2611 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
2614int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2615{
2616 int error = may_delete(dir, dentry, 1);
2617
2618 if (error)
2619 return error;
2620
Al Viroacfa4382008-12-04 10:06:33 -05002621 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return -EPERM;
2623
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002624 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 dentry_unhash(dentry);
2626 if (d_mountpoint(dentry))
2627 error = -EBUSY;
2628 else {
2629 error = security_inode_rmdir(dir, dentry);
2630 if (!error) {
2631 error = dir->i_op->rmdir(dir, dentry);
Al Virod83c49f2010-04-30 17:17:09 -04002632 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04002634 dont_mount(dentry);
2635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002638 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 d_delete(dentry);
2641 }
2642 dput(dentry);
2643
2644 return error;
2645}
2646
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002647static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
2649 int error = 0;
2650 char * name;
2651 struct dentry *dentry;
2652 struct nameidata nd;
2653
Al Viro2ad94ae2008-07-21 09:32:51 -04002654 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002656 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002659 case LAST_DOTDOT:
2660 error = -ENOTEMPTY;
2661 goto exit1;
2662 case LAST_DOT:
2663 error = -EINVAL;
2664 goto exit1;
2665 case LAST_ROOT:
2666 error = -EBUSY;
2667 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002669
2670 nd.flags &= ~LOOKUP_PARENT;
2671
Jan Blunck4ac91372008-02-14 19:34:32 -08002672 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002673 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002675 if (IS_ERR(dentry))
2676 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002677 error = mnt_want_write(nd.path.mnt);
2678 if (error)
2679 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002680 error = security_path_rmdir(&nd.path, dentry);
2681 if (error)
2682 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002683 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002684exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002685 mnt_drop_write(nd.path.mnt);
2686exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002687 dput(dentry);
2688exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002689 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002691 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 putname(name);
2693 return error;
2694}
2695
Heiko Carstens3cdad422009-01-14 14:14:22 +01002696SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002697{
2698 return do_rmdir(AT_FDCWD, pathname);
2699}
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701int vfs_unlink(struct inode *dir, struct dentry *dentry)
2702{
2703 int error = may_delete(dir, dentry, 0);
2704
2705 if (error)
2706 return error;
2707
Al Viroacfa4382008-12-04 10:06:33 -05002708 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return -EPERM;
2710
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002711 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (d_mountpoint(dentry))
2713 error = -EBUSY;
2714 else {
2715 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002716 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002718 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002719 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002722 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2725 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002726 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
Robert Love0eeca282005-07-12 17:06:03 -04002729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return error;
2731}
2732
2733/*
2734 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002735 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 * writeout happening, and we don't want to prevent access to the directory
2737 * while waiting on the I/O.
2738 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002739static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Al Viro2ad94ae2008-07-21 09:32:51 -04002741 int error;
2742 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct dentry *dentry;
2744 struct nameidata nd;
2745 struct inode *inode = NULL;
2746
Al Viro2ad94ae2008-07-21 09:32:51 -04002747 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002749 return error;
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 error = -EISDIR;
2752 if (nd.last_type != LAST_NORM)
2753 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002754
2755 nd.flags &= ~LOOKUP_PARENT;
2756
Jan Blunck4ac91372008-02-14 19:34:32 -08002757 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002758 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 error = PTR_ERR(dentry);
2760 if (!IS_ERR(dentry)) {
2761 /* Why not before? Because we want correct error value */
2762 if (nd.last.name[nd.last.len])
2763 goto slashes;
2764 inode = dentry->d_inode;
2765 if (inode)
Al Viro7de9c6e2010-10-23 11:11:40 -04002766 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002767 error = mnt_want_write(nd.path.mnt);
2768 if (error)
2769 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002770 error = security_path_unlink(&nd.path, dentry);
2771 if (error)
2772 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002773 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002774exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002775 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 exit2:
2777 dput(dentry);
2778 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002779 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (inode)
2781 iput(inode); /* truncate the inode here */
2782exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002783 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 putname(name);
2785 return error;
2786
2787slashes:
2788 error = !dentry->d_inode ? -ENOENT :
2789 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2790 goto exit2;
2791}
2792
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002793SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002794{
2795 if ((flag & ~AT_REMOVEDIR) != 0)
2796 return -EINVAL;
2797
2798 if (flag & AT_REMOVEDIR)
2799 return do_rmdir(dfd, pathname);
2800
2801 return do_unlinkat(dfd, pathname);
2802}
2803
Heiko Carstens3480b252009-01-14 14:14:16 +01002804SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002805{
2806 return do_unlinkat(AT_FDCWD, pathname);
2807}
2808
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002809int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002811 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813 if (error)
2814 return error;
2815
Al Viroacfa4382008-12-04 10:06:33 -05002816 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return -EPERM;
2818
2819 error = security_inode_symlink(dir, dentry, oldname);
2820 if (error)
2821 return error;
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002824 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002825 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return error;
2827}
2828
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002829SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2830 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
Al Viro2ad94ae2008-07-21 09:32:51 -04002832 int error;
2833 char *from;
2834 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002835 struct dentry *dentry;
2836 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002839 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002841
2842 error = user_path_parent(newdfd, newname, &nd, &to);
2843 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002844 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Dave Hansen6902d922006-09-30 23:29:01 -07002846 dentry = lookup_create(&nd, 0);
2847 error = PTR_ERR(dentry);
2848 if (IS_ERR(dentry))
2849 goto out_unlock;
2850
Dave Hansen75c3f292008-02-15 14:37:45 -08002851 error = mnt_want_write(nd.path.mnt);
2852 if (error)
2853 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002854 error = security_path_symlink(&nd.path, dentry, from);
2855 if (error)
2856 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002857 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002858out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002859 mnt_drop_write(nd.path.mnt);
2860out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002861 dput(dentry);
2862out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002863 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002864 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002865 putname(to);
2866out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 putname(from);
2868 return error;
2869}
2870
Heiko Carstens3480b252009-01-14 14:14:16 +01002871SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002872{
2873 return sys_symlinkat(oldname, AT_FDCWD, newname);
2874}
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2877{
2878 struct inode *inode = old_dentry->d_inode;
2879 int error;
2880
2881 if (!inode)
2882 return -ENOENT;
2883
Miklos Szeredia95164d2008-07-30 15:08:48 +02002884 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (error)
2886 return error;
2887
2888 if (dir->i_sb != inode->i_sb)
2889 return -EXDEV;
2890
2891 /*
2892 * A link to an append-only or immutable file cannot be created.
2893 */
2894 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2895 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002896 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002898 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return -EPERM;
2900
2901 error = security_inode_link(old_dentry, dir, new_dentry);
2902 if (error)
2903 return error;
2904
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002905 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002907 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002908 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002909 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return error;
2911}
2912
2913/*
2914 * Hardlinks are often used in delicate situations. We avoid
2915 * security-related surprises by not following symlinks on the
2916 * newname. --KAB
2917 *
2918 * We don't follow them on the oldname either to be compatible
2919 * with linux 2.0, and to avoid hard-linking to directories
2920 * and other special files. --ADM
2921 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002922SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2923 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
2925 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002926 struct nameidata nd;
2927 struct path old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002929 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002931 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002932 return -EINVAL;
2933
Al Viro2d8f3032008-07-22 09:59:21 -04002934 error = user_path_at(olddfd, oldname,
2935 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2936 &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002938 return error;
2939
2940 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if (error)
2942 goto out;
2943 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002944 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 goto out_release;
2946 new_dentry = lookup_create(&nd, 0);
2947 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002948 if (IS_ERR(new_dentry))
2949 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002950 error = mnt_want_write(nd.path.mnt);
2951 if (error)
2952 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002953 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2954 if (error)
2955 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04002956 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002957out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002958 mnt_drop_write(nd.path.mnt);
2959out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002960 dput(new_dentry);
2961out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002962 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002964 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002965 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966out:
Al Viro2d8f3032008-07-22 09:59:21 -04002967 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
2969 return error;
2970}
2971
Heiko Carstens3480b252009-01-14 14:14:16 +01002972SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002973{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002974 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002975}
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977/*
2978 * The worst of all namespace operations - renaming directory. "Perverted"
2979 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2980 * Problems:
2981 * a) we can get into loop creation. Check is done in is_subdir().
2982 * b) race potential - two innocent renames can create a loop together.
2983 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002984 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 * story.
2986 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002987 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 * whether the target exists). Solution: try to be smart with locking
2989 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002990 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 * move will be locked. Thus we can rank directories by the tree
2992 * (ancestors first) and rank all non-directories after them.
2993 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002994 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 * HOWEVER, it relies on the assumption that any object with ->lookup()
2996 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2997 * we'd better make sure that there's no link(2) for them.
2998 * d) some filesystems don't support opened-but-unlinked directories,
2999 * either because of layout or because they are not ready to deal with
3000 * all cases correctly. The latter will be fixed (taking this sort of
3001 * stuff into VFS), but the former is not going away. Solution: the same
3002 * trick as in rmdir().
3003 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003004 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003006 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 * locking].
3008 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003009static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3010 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
3012 int error = 0;
3013 struct inode *target;
3014
3015 /*
3016 * If we are going to change the parent - check write permissions,
3017 * we'll need to flip '..'.
3018 */
3019 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003020 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (error)
3022 return error;
3023 }
3024
3025 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3026 if (error)
3027 return error;
3028
3029 target = new_dentry->d_inode;
Al Virod83c49f2010-04-30 17:17:09 -04003030 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003031 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3033 error = -EBUSY;
Al Virod83c49f2010-04-30 17:17:09 -04003034 else {
3035 if (target)
3036 dentry_unhash(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (target) {
Al Virod83c49f2010-04-30 17:17:09 -04003040 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 target->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04003042 dont_mount(new_dentry);
3043 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003044 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (d_unhashed(new_dentry))
3046 d_rehash(new_dentry);
3047 dput(new_dentry);
3048 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003049 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003050 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3051 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return error;
3053}
3054
Adrian Bunk75c96f82005-05-05 16:16:09 -07003055static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3056 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct inode *target;
3059 int error;
3060
3061 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3062 if (error)
3063 return error;
3064
3065 dget(new_dentry);
3066 target = new_dentry->d_inode;
3067 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003068 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3070 error = -EBUSY;
3071 else
3072 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3073 if (!error) {
Al Virobec10522010-03-03 14:12:08 -05003074 if (target)
Al Virod83c49f2010-04-30 17:17:09 -04003075 dont_mount(new_dentry);
Mark Fasheh349457c2006-09-08 14:22:21 -07003076 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003080 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 dput(new_dentry);
3082 return error;
3083}
3084
3085int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3086 struct inode *new_dir, struct dentry *new_dentry)
3087{
3088 int error;
3089 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003090 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092 if (old_dentry->d_inode == new_dentry->d_inode)
3093 return 0;
3094
3095 error = may_delete(old_dir, old_dentry, is_dir);
3096 if (error)
3097 return error;
3098
3099 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003100 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 else
3102 error = may_delete(new_dir, new_dentry, is_dir);
3103 if (error)
3104 return error;
3105
Al Viroacfa4382008-12-04 10:06:33 -05003106 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return -EPERM;
3108
Robert Love0eeca282005-07-12 17:06:03 -04003109 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (is_dir)
3112 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3113 else
3114 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003115 if (!error)
3116 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003117 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003118 fsnotify_oldname_free(old_name);
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 return error;
3121}
3122
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003123SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3124 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
Al Viro2ad94ae2008-07-21 09:32:51 -04003126 struct dentry *old_dir, *new_dir;
3127 struct dentry *old_dentry, *new_dentry;
3128 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003130 char *from;
3131 char *to;
3132 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Al Viro2ad94ae2008-07-21 09:32:51 -04003134 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (error)
3136 goto exit;
3137
Al Viro2ad94ae2008-07-21 09:32:51 -04003138 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (error)
3140 goto exit1;
3141
3142 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003143 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 goto exit2;
3145
Jan Blunck4ac91372008-02-14 19:34:32 -08003146 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 error = -EBUSY;
3148 if (oldnd.last_type != LAST_NORM)
3149 goto exit2;
3150
Jan Blunck4ac91372008-02-14 19:34:32 -08003151 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (newnd.last_type != LAST_NORM)
3153 goto exit2;
3154
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003155 oldnd.flags &= ~LOOKUP_PARENT;
3156 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003157 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 trap = lock_rename(new_dir, old_dir);
3160
Christoph Hellwig49705b72005-11-08 21:35:06 -08003161 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 error = PTR_ERR(old_dentry);
3163 if (IS_ERR(old_dentry))
3164 goto exit3;
3165 /* source must exist */
3166 error = -ENOENT;
3167 if (!old_dentry->d_inode)
3168 goto exit4;
3169 /* unless the source is a directory trailing slashes give -ENOTDIR */
3170 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3171 error = -ENOTDIR;
3172 if (oldnd.last.name[oldnd.last.len])
3173 goto exit4;
3174 if (newnd.last.name[newnd.last.len])
3175 goto exit4;
3176 }
3177 /* source should not be ancestor of target */
3178 error = -EINVAL;
3179 if (old_dentry == trap)
3180 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003181 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 error = PTR_ERR(new_dentry);
3183 if (IS_ERR(new_dentry))
3184 goto exit4;
3185 /* target should not be an ancestor of source */
3186 error = -ENOTEMPTY;
3187 if (new_dentry == trap)
3188 goto exit5;
3189
Dave Hansen9079b1e2008-02-15 14:37:49 -08003190 error = mnt_want_write(oldnd.path.mnt);
3191 if (error)
3192 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003193 error = security_path_rename(&oldnd.path, old_dentry,
3194 &newnd.path, new_dentry);
3195 if (error)
3196 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 error = vfs_rename(old_dir->d_inode, old_dentry,
3198 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003199exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003200 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201exit5:
3202 dput(new_dentry);
3203exit4:
3204 dput(old_dentry);
3205exit3:
3206 unlock_rename(new_dir, old_dir);
3207exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003208 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003209 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003211 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003213exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return error;
3215}
3216
Heiko Carstensa26eab22009-01-14 14:14:17 +01003217SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003218{
3219 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3220}
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3223{
3224 int len;
3225
3226 len = PTR_ERR(link);
3227 if (IS_ERR(link))
3228 goto out;
3229
3230 len = strlen(link);
3231 if (len > (unsigned) buflen)
3232 len = buflen;
3233 if (copy_to_user(buffer, link, len))
3234 len = -EFAULT;
3235out:
3236 return len;
3237}
3238
3239/*
3240 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3241 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3242 * using) it for any given inode is up to filesystem.
3243 */
3244int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3245{
3246 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003247 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003248 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003251 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003252 if (IS_ERR(cookie))
3253 return PTR_ERR(cookie);
3254
3255 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3256 if (dentry->d_inode->i_op->put_link)
3257 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3258 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
3261int vfs_follow_link(struct nameidata *nd, const char *link)
3262{
3263 return __vfs_follow_link(nd, link);
3264}
3265
3266/* get the link contents into pagecache */
3267static char *page_getlink(struct dentry * dentry, struct page **ppage)
3268{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003269 char *kaddr;
3270 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003272 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003274 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003276 kaddr = kmap(page);
3277 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3278 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
3281int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3282{
3283 struct page *page = NULL;
3284 char *s = page_getlink(dentry, &page);
3285 int res = vfs_readlink(dentry,buffer,buflen,s);
3286 if (page) {
3287 kunmap(page);
3288 page_cache_release(page);
3289 }
3290 return res;
3291}
3292
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003293void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003295 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003297 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003300void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003302 struct page *page = cookie;
3303
3304 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 kunmap(page);
3306 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
3308}
3309
Nick Piggin54566b22009-01-04 12:00:53 -08003310/*
3311 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3312 */
3313int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
3315 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003316 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003317 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003318 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003320 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3321 if (nofs)
3322 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
NeilBrown7e53cac2006-03-25 03:07:57 -08003324retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003325 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003326 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003328 goto fail;
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 kaddr = kmap_atomic(page, KM_USER0);
3331 memcpy(kaddr, symname, len-1);
3332 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003333
3334 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3335 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (err < 0)
3337 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003338 if (err < len-1)
3339 goto retry;
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 mark_inode_dirty(inode);
3342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343fail:
3344 return err;
3345}
3346
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003347int page_symlink(struct inode *inode, const char *symname, int len)
3348{
3349 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003350 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003351}
3352
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003353const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 .readlink = generic_readlink,
3355 .follow_link = page_follow_link_light,
3356 .put_link = page_put_link,
3357};
3358
Al Viro2d8f3032008-07-22 09:59:21 -04003359EXPORT_SYMBOL(user_path_at);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360EXPORT_SYMBOL(follow_down);
3361EXPORT_SYMBOL(follow_up);
3362EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3363EXPORT_SYMBOL(getname);
3364EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365EXPORT_SYMBOL(lookup_one_len);
3366EXPORT_SYMBOL(page_follow_link_light);
3367EXPORT_SYMBOL(page_put_link);
3368EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003369EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370EXPORT_SYMBOL(page_symlink);
3371EXPORT_SYMBOL(page_symlink_inode_operations);
3372EXPORT_SYMBOL(path_lookup);
Al Virod1811462008-08-02 00:49:18 -04003373EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003374EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003375EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003376EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377EXPORT_SYMBOL(unlock_rename);
3378EXPORT_SYMBOL(vfs_create);
3379EXPORT_SYMBOL(vfs_follow_link);
3380EXPORT_SYMBOL(vfs_link);
3381EXPORT_SYMBOL(vfs_mkdir);
3382EXPORT_SYMBOL(vfs_mknod);
3383EXPORT_SYMBOL(generic_permission);
3384EXPORT_SYMBOL(vfs_readlink);
3385EXPORT_SYMBOL(vfs_rename);
3386EXPORT_SYMBOL(vfs_rmdir);
3387EXPORT_SYMBOL(vfs_symlink);
3388EXPORT_SYMBOL(vfs_unlink);
3389EXPORT_SYMBOL(dentry_unhash);
3390EXPORT_SYMBOL(generic_readlink);