blob: 0f02359ce685c54652e56b870c4e1dde988b0679 [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 Pigginb74c79e2011-01-07 17:49:58 +1100172static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
173 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
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 Pigginb74c79e2011-01-07 17:49:58 +1100183 int error = check_acl(inode, mask, flags);
184 if (error != -EAGAIN)
185 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700186 }
187
188 if (in_group_p(inode->i_gid))
189 mode >>= 3;
190 }
191
192 /*
193 * If the DACs are ok we don't need any capability check.
194 */
195 if ((mask & ~mode) == 0)
196 return 0;
197 return -EACCES;
198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100201 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * @inode: inode to check access rights for
203 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
204 * @check_acl: optional callback to check for Posix ACLs
Nick Pigginb74c79e2011-01-07 17:49:58 +1100205 * @flags IPERM_FLAG_ flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
207 * Used to check for read/write/execute permissions on a file.
208 * We use "fsuid" for this, letting us set arbitrary permissions
209 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100210 * are used for other things.
211 *
212 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
213 * request cannot be satisfied (eg. requires blocking or too much complexity).
214 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100216int generic_permission(struct inode *inode, int mask, unsigned int flags,
217 int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700219 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /*
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700222 * Do the basic POSIX ACL permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100224 ret = acl_permission_check(inode, mask, flags, check_acl);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700225 if (ret != -EACCES)
226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * Read/write DACs are always overridable.
230 * Executable DACs are overridable if at least one exec bit is set.
231 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200232 if (!(mask & MAY_EXEC) || execute_ok(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (capable(CAP_DAC_OVERRIDE))
234 return 0;
235
236 /*
237 * Searching includes executable on directories, else just read.
238 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600239 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
241 if (capable(CAP_DAC_READ_SEARCH))
242 return 0;
243
244 return -EACCES;
245}
246
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200247/**
248 * inode_permission - check for access rights to a given inode
249 * @inode: inode to check permission on
250 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
251 *
252 * Used to check for read/write/execute permissions on an inode.
253 * We use "fsuid" for this, letting us set arbitrary permissions
254 * for filesystem access without changing the "normal" uids which
255 * are used for other things.
256 */
Al Virof419a2e2008-07-22 00:07:17 -0400257int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Al Viroe6305c42008-07-15 21:03:57 -0400259 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700262 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
265 * Nobody gets write access to a read-only fs.
266 */
267 if (IS_RDONLY(inode) &&
268 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
269 return -EROFS;
270
271 /*
272 * Nobody gets write access to an immutable file.
273 */
274 if (IS_IMMUTABLE(inode))
275 return -EACCES;
276 }
277
Al Viroacfa4382008-12-04 10:06:33 -0500278 if (inode->i_op->permission)
Nick Pigginb74c79e2011-01-07 17:49:58 +1100279 retval = inode->i_op->permission(inode, mask, 0);
Miklos Szeredif696a362008-07-31 13:41:58 +0200280 else
Nick Pigginb74c79e2011-01-07 17:49:58 +1100281 retval = generic_permission(inode, mask, 0,
282 inode->i_op->check_acl);
Miklos Szeredif696a362008-07-31 13:41:58 +0200283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (retval)
285 return retval;
286
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700287 retval = devcgroup_inode_permission(inode, mask);
288 if (retval)
289 return retval;
290
Eric Parisd09ca732010-07-23 11:43:57 -0400291 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800294/**
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800295 * file_permission - check for additional access rights to a given file
296 * @file: file to check access rights for
297 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
298 *
299 * Used to check for read/write/execute permissions on an already opened
300 * file.
301 *
302 * Note:
303 * Do not use this function in new code. All access checks should
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200304 * be done using inode_permission().
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800305 */
306int file_permission(struct file *file, int mask)
307{
Al Virof419a2e2008-07-22 00:07:17 -0400308 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
312 * get_write_access() gets write permission for a file.
313 * put_write_access() releases this write permission.
314 * This is used for regular files.
315 * We cannot support write (and maybe mmap read-write shared) accesses and
316 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
317 * can have the following values:
318 * 0: no writers, no VM_DENYWRITE mappings
319 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
320 * > 0: (i_writecount) users are writing to the file.
321 *
322 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
323 * except for the cases where we don't hold i_writecount yet. Then we need to
324 * use {get,deny}_write_access() - these functions check the sign and refuse
325 * to do the change if sign is wrong. Exclusion between them is provided by
326 * the inode->i_lock spinlock.
327 */
328
329int get_write_access(struct inode * inode)
330{
331 spin_lock(&inode->i_lock);
332 if (atomic_read(&inode->i_writecount) < 0) {
333 spin_unlock(&inode->i_lock);
334 return -ETXTBSY;
335 }
336 atomic_inc(&inode->i_writecount);
337 spin_unlock(&inode->i_lock);
338
339 return 0;
340}
341
342int deny_write_access(struct file * file)
343{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800344 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 spin_lock(&inode->i_lock);
347 if (atomic_read(&inode->i_writecount) > 0) {
348 spin_unlock(&inode->i_lock);
349 return -ETXTBSY;
350 }
351 atomic_dec(&inode->i_writecount);
352 spin_unlock(&inode->i_lock);
353
354 return 0;
355}
356
Jan Blunck1d957f92008-02-14 19:34:35 -0800357/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800358 * path_get - get a reference to a path
359 * @path: path to get the reference to
360 *
361 * Given a path increment the reference count to the dentry and the vfsmount.
362 */
363void path_get(struct path *path)
364{
365 mntget(path->mnt);
366 dget(path->dentry);
367}
368EXPORT_SYMBOL(path_get);
369
370/**
Nick Pigginb3e19d92011-01-07 17:50:11 +1100371 * path_get_long - get a long reference to a path
372 * @path: path to get the reference to
373 *
374 * Given a path increment the reference count to the dentry and the vfsmount.
375 */
376void path_get_long(struct path *path)
377{
378 mntget_long(path->mnt);
379 dget(path->dentry);
380}
381
382/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800383 * path_put - put a reference to a path
384 * @path: path to put the reference to
385 *
386 * Given a path decrement the reference count to the dentry and the vfsmount.
387 */
388void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jan Blunck1d957f92008-02-14 19:34:35 -0800390 dput(path->dentry);
391 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Jan Blunck1d957f92008-02-14 19:34:35 -0800393EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Trond Myklebust834f2a42005-10-18 14:20:16 -0700395/**
Nick Pigginb3e19d92011-01-07 17:50:11 +1100396 * path_put_long - put a long reference to a path
397 * @path: path to put the reference to
398 *
399 * Given a path decrement the reference count to the dentry and the vfsmount.
400 */
401void path_put_long(struct path *path)
402{
403 dput(path->dentry);
404 mntput_long(path->mnt);
405}
406
407/**
Nick Piggin31e6b012011-01-07 17:49:52 +1100408 * nameidata_drop_rcu - drop this nameidata out of rcu-walk
409 * @nd: nameidata pathwalk data to drop
410 * @Returns: 0 on success, -ECHLID on failure
411 *
412 * Path walking has 2 modes, rcu-walk and ref-walk (see
413 * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
414 * to drop out of rcu-walk mode and take normal reference counts on dentries
415 * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
416 * refcounts at the last known good point before rcu-walk got stuck, so
417 * ref-walk may continue from there. If this is not successful (eg. a seqcount
418 * has changed), then failure is returned and path walk restarts from the
419 * beginning in ref-walk mode.
420 *
421 * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
422 * ref-walk. Must be called from rcu-walk context.
423 */
424static int nameidata_drop_rcu(struct nameidata *nd)
425{
426 struct fs_struct *fs = current->fs;
427 struct dentry *dentry = nd->path.dentry;
428
429 BUG_ON(!(nd->flags & LOOKUP_RCU));
430 if (nd->root.mnt) {
431 spin_lock(&fs->lock);
432 if (nd->root.mnt != fs->root.mnt ||
433 nd->root.dentry != fs->root.dentry)
434 goto err_root;
435 }
436 spin_lock(&dentry->d_lock);
437 if (!__d_rcu_to_refcount(dentry, nd->seq))
438 goto err;
439 BUG_ON(nd->inode != dentry->d_inode);
440 spin_unlock(&dentry->d_lock);
441 if (nd->root.mnt) {
442 path_get(&nd->root);
443 spin_unlock(&fs->lock);
444 }
445 mntget(nd->path.mnt);
446
447 rcu_read_unlock();
448 br_read_unlock(vfsmount_lock);
449 nd->flags &= ~LOOKUP_RCU;
450 return 0;
451err:
452 spin_unlock(&dentry->d_lock);
453err_root:
454 if (nd->root.mnt)
455 spin_unlock(&fs->lock);
456 return -ECHILD;
457}
458
459/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
460static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
461{
462 if (nd->flags & LOOKUP_RCU)
463 return nameidata_drop_rcu(nd);
464 return 0;
465}
466
467/**
468 * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
469 * @nd: nameidata pathwalk data to drop
470 * @dentry: dentry to drop
471 * @Returns: 0 on success, -ECHLID on failure
472 *
473 * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
474 * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
475 * @nd. Must be called from rcu-walk context.
476 */
477static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
478{
479 struct fs_struct *fs = current->fs;
480 struct dentry *parent = nd->path.dentry;
481
482 BUG_ON(!(nd->flags & LOOKUP_RCU));
483 if (nd->root.mnt) {
484 spin_lock(&fs->lock);
485 if (nd->root.mnt != fs->root.mnt ||
486 nd->root.dentry != fs->root.dentry)
487 goto err_root;
488 }
489 spin_lock(&parent->d_lock);
490 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
491 if (!__d_rcu_to_refcount(dentry, nd->seq))
492 goto err;
493 /*
494 * If the sequence check on the child dentry passed, then the child has
495 * not been removed from its parent. This means the parent dentry must
496 * be valid and able to take a reference at this point.
497 */
498 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
499 BUG_ON(!parent->d_count);
500 parent->d_count++;
501 spin_unlock(&dentry->d_lock);
502 spin_unlock(&parent->d_lock);
503 if (nd->root.mnt) {
504 path_get(&nd->root);
505 spin_unlock(&fs->lock);
506 }
507 mntget(nd->path.mnt);
508
509 rcu_read_unlock();
510 br_read_unlock(vfsmount_lock);
511 nd->flags &= ~LOOKUP_RCU;
512 return 0;
513err:
514 spin_unlock(&dentry->d_lock);
515 spin_unlock(&parent->d_lock);
516err_root:
517 if (nd->root.mnt)
518 spin_unlock(&fs->lock);
519 return -ECHILD;
520}
521
522/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
523static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
524{
525 if (nd->flags & LOOKUP_RCU)
526 return nameidata_dentry_drop_rcu(nd, dentry);
527 return 0;
528}
529
530/**
531 * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
532 * @nd: nameidata pathwalk data to drop
533 * @Returns: 0 on success, -ECHLID on failure
534 *
535 * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
536 * nd->path should be the final element of the lookup, so nd->root is discarded.
537 * Must be called from rcu-walk context.
538 */
539static int nameidata_drop_rcu_last(struct nameidata *nd)
540{
541 struct dentry *dentry = nd->path.dentry;
542
543 BUG_ON(!(nd->flags & LOOKUP_RCU));
544 nd->flags &= ~LOOKUP_RCU;
545 nd->root.mnt = NULL;
546 spin_lock(&dentry->d_lock);
547 if (!__d_rcu_to_refcount(dentry, nd->seq))
548 goto err_unlock;
549 BUG_ON(nd->inode != dentry->d_inode);
550 spin_unlock(&dentry->d_lock);
551
552 mntget(nd->path.mnt);
553
554 rcu_read_unlock();
555 br_read_unlock(vfsmount_lock);
556
557 return 0;
558
559err_unlock:
560 spin_unlock(&dentry->d_lock);
561 rcu_read_unlock();
562 br_read_unlock(vfsmount_lock);
563 return -ECHILD;
564}
565
566/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
567static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd)
568{
569 if (likely(nd->flags & LOOKUP_RCU))
570 return nameidata_drop_rcu_last(nd);
571 return 0;
572}
573
574/**
Trond Myklebust834f2a42005-10-18 14:20:16 -0700575 * release_open_intent - free up open intent resources
576 * @nd: pointer to nameidata
577 */
578void release_open_intent(struct nameidata *nd)
579{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800580 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700581 put_filp(nd->intent.open.file);
582 else
583 fput(nd->intent.open.file);
584}
585
Nick Pigginbb20c182011-01-14 02:35:53 +0000586/*
587 * Call d_revalidate and handle filesystems that request rcu-walk
588 * to be dropped. This may be called and return in rcu-walk mode,
589 * regardless of success or error. If -ECHILD is returned, the caller
590 * must return -ECHILD back up the path walk stack so path walk may
591 * be restarted in ref-walk mode.
592 */
Nick Piggin34286d62011-01-07 17:49:57 +1100593static int d_revalidate(struct dentry *dentry, struct nameidata *nd)
594{
595 int status;
596
597 status = dentry->d_op->d_revalidate(dentry, nd);
598 if (status == -ECHILD) {
599 if (nameidata_dentry_drop_rcu(nd, dentry))
600 return status;
601 status = dentry->d_op->d_revalidate(dentry, nd);
602 }
603
604 return status;
605}
606
Ian Kentbcdc5e02006-09-27 01:50:44 -0700607static inline struct dentry *
608do_revalidate(struct dentry *dentry, struct nameidata *nd)
609{
Nick Piggin34286d62011-01-07 17:49:57 +1100610 int status;
611
612 status = d_revalidate(dentry, nd);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700613 if (unlikely(status <= 0)) {
614 /*
615 * The dentry failed validation.
616 * If d_revalidate returned 0 attempt to invalidate
617 * the dentry otherwise d_revalidate is asking us
618 * to return a fail status.
619 */
Nick Piggin34286d62011-01-07 17:49:57 +1100620 if (status < 0) {
621 /* If we're in rcu-walk, we don't have a ref */
622 if (!(nd->flags & LOOKUP_RCU))
623 dput(dentry);
624 dentry = ERR_PTR(status);
625
626 } else {
627 /* Don't d_invalidate in rcu-walk mode */
628 if (nameidata_dentry_drop_rcu_maybe(nd, dentry))
629 return ERR_PTR(-ECHILD);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700630 if (!d_invalidate(dentry)) {
631 dput(dentry);
632 dentry = NULL;
633 }
Ian Kentbcdc5e02006-09-27 01:50:44 -0700634 }
635 }
636 return dentry;
637}
638
Nick Pigginfb045ad2011-01-07 17:49:55 +1100639static inline int need_reval_dot(struct dentry *dentry)
640{
641 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
642 return 0;
643
644 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
645 return 0;
646
647 return 1;
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/*
Jeff Layton39159de2009-12-07 12:01:50 -0500651 * force_reval_path - force revalidation of a dentry
652 *
653 * In some situations the path walking code will trust dentries without
654 * revalidating them. This causes problems for filesystems that depend on
655 * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
656 * (which indicates that it's possible for the dentry to go stale), force
657 * a d_revalidate call before proceeding.
658 *
659 * Returns 0 if the revalidation was successful. If the revalidation fails,
660 * either return the error returned by d_revalidate or -ESTALE if the
661 * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
662 * invalidate the dentry. It's up to the caller to handle putting references
663 * to the path if necessary.
664 */
665static int
666force_reval_path(struct path *path, struct nameidata *nd)
667{
668 int status;
669 struct dentry *dentry = path->dentry;
670
671 /*
672 * only check on filesystems where it's possible for the dentry to
Nick Pigginfb045ad2011-01-07 17:49:55 +1100673 * become stale.
Jeff Layton39159de2009-12-07 12:01:50 -0500674 */
Nick Pigginfb045ad2011-01-07 17:49:55 +1100675 if (!need_reval_dot(dentry))
Jeff Layton39159de2009-12-07 12:01:50 -0500676 return 0;
677
Nick Piggin34286d62011-01-07 17:49:57 +1100678 status = d_revalidate(dentry, nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500679 if (status > 0)
680 return 0;
681
682 if (!status) {
Nick Pigginbb20c182011-01-14 02:35:53 +0000683 /* Don't d_invalidate in rcu-walk mode */
684 if (nameidata_drop_rcu(nd))
685 return -ECHILD;
Jeff Layton39159de2009-12-07 12:01:50 -0500686 d_invalidate(dentry);
687 status = -ESTALE;
688 }
689 return status;
690}
691
692/*
Al Virob75b5082009-12-16 01:01:38 -0500693 * Short-cut version of permission(), for calling on directories
694 * during pathname resolution. Combines parts of permission()
695 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
697 * If appropriate, check DAC only. If not appropriate, or
Al Virob75b5082009-12-16 01:01:38 -0500698 * short-cut DAC fails, then call ->permission() to do more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * complete permission check.
700 */
Nick Pigginb74c79e2011-01-07 17:49:58 +1100701static inline int exec_permission(struct inode *inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700703 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700705 if (inode->i_op->permission) {
Nick Pigginb74c79e2011-01-07 17:49:58 +1100706 ret = inode->i_op->permission(inode, MAY_EXEC, flags);
707 } else {
708 ret = acl_permission_check(inode, MAY_EXEC, flags,
709 inode->i_op->check_acl);
Linus Torvaldscb9179e2009-08-28 11:08:31 -0700710 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100711 if (likely(!ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto ok;
Nick Pigginb74c79e2011-01-07 17:49:58 +1100713 if (ret == -ECHILD)
Nick Piggin31e6b012011-01-07 17:49:52 +1100714 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvaldsf1ac9f62009-08-28 10:53:56 -0700716 if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto ok;
718
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720ok:
Nick Pigginb74c79e2011-01-07 17:49:58 +1100721 return security_inode_exec_permission(inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Al Viro2a737872009-04-07 11:49:53 -0400724static __always_inline void set_root(struct nameidata *nd)
725{
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200726 if (!nd->root.mnt)
727 get_fs_root(current->fs, &nd->root);
Al Viro2a737872009-04-07 11:49:53 -0400728}
729
Al Viro6de88d72009-08-09 01:41:57 +0400730static int link_path_walk(const char *, struct nameidata *);
731
Nick Piggin31e6b012011-01-07 17:49:52 +1100732static __always_inline void set_root_rcu(struct nameidata *nd)
733{
734 if (!nd->root.mnt) {
735 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100736 unsigned seq;
737
738 do {
739 seq = read_seqcount_begin(&fs->seq);
740 nd->root = fs->root;
741 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +1100742 }
743}
744
Arjan van de Venf1662352006-01-14 13:21:31 -0800745static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Nick Piggin31e6b012011-01-07 17:49:52 +1100747 int ret;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (IS_ERR(link))
750 goto fail;
751
752 if (*link == '/') {
Al Viro2a737872009-04-07 11:49:53 -0400753 set_root(nd);
Jan Blunck1d957f92008-02-14 19:34:35 -0800754 path_put(&nd->path);
Al Viro2a737872009-04-07 11:49:53 -0400755 nd->path = nd->root;
756 path_get(&nd->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100758 nd->inode = nd->path.dentry->d_inode;
Christoph Hellwigb4091d52008-11-05 15:07:21 +0100759
Nick Piggin31e6b012011-01-07 17:49:52 +1100760 ret = link_path_walk(link, nd);
761 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800763 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return PTR_ERR(link);
765}
766
Jan Blunck1d957f92008-02-14 19:34:35 -0800767static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700768{
769 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800770 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700771 mntput(path->mnt);
772}
773
774static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
775{
Nick Piggin31e6b012011-01-07 17:49:52 +1100776 if (!(nd->flags & LOOKUP_RCU)) {
777 dput(nd->path.dentry);
778 if (nd->path.mnt != path->mnt)
779 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800780 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100781 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800782 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700783}
784
Al Virodef4af32009-12-26 08:37:05 -0500785static __always_inline int
786__do_follow_link(struct path *path, struct nameidata *nd, void **p)
Ian Kent051d3812006-03-27 01:14:53 -0800787{
788 int error;
Ian Kent051d3812006-03-27 01:14:53 -0800789 struct dentry *dentry = path->dentry;
790
791 touch_atime(path->mnt, dentry);
792 nd_set_link(nd, NULL);
793
Jan Blunck4ac91372008-02-14 19:34:32 -0800794 if (path->mnt != nd->path.mnt) {
Ian Kent051d3812006-03-27 01:14:53 -0800795 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +1100796 nd->inode = nd->path.dentry->d_inode;
Ian Kent051d3812006-03-27 01:14:53 -0800797 dget(dentry);
798 }
799 mntget(path->mnt);
Nick Piggin31e6b012011-01-07 17:49:52 +1100800
Al Viro86acdca12009-12-22 23:45:11 -0500801 nd->last_type = LAST_BIND;
Al Virodef4af32009-12-26 08:37:05 -0500802 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
803 error = PTR_ERR(*p);
804 if (!IS_ERR(*p)) {
Ian Kent051d3812006-03-27 01:14:53 -0800805 char *s = nd_get_link(nd);
806 error = 0;
807 if (s)
808 error = __vfs_follow_link(nd, s);
Jeff Layton39159de2009-12-07 12:01:50 -0500809 else if (nd->last_type == LAST_BIND) {
810 error = force_reval_path(&nd->path, nd);
811 if (error)
812 path_put(&nd->path);
813 }
Ian Kent051d3812006-03-27 01:14:53 -0800814 }
Ian Kent051d3812006-03-27 01:14:53 -0800815 return error;
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/*
819 * This limits recursive symlink follows to 8, while
820 * limiting consecutive symlinks to 40.
821 *
822 * Without that kind of total limit, nasty chains of consecutive
823 * symlinks can cause almost arbitrarily long lookups.
824 */
Al Viro90ebe562005-06-06 13:35:58 -0700825static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Al Virodef4af32009-12-26 08:37:05 -0500827 void *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int err = -ELOOP;
829 if (current->link_count >= MAX_NESTED_LINKS)
830 goto loop;
831 if (current->total_link_count >= 40)
832 goto loop;
833 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
834 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700835 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (err)
837 goto loop;
838 current->link_count++;
839 current->total_link_count++;
840 nd->depth++;
Al Virodef4af32009-12-26 08:37:05 -0500841 err = __do_follow_link(path, nd, &cookie);
842 if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
843 path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
Al Viro258fa992009-08-09 01:32:02 +0400844 path_put(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 current->link_count--;
846 nd->depth--;
847 return err;
848loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800849 path_put_conditional(path, nd);
850 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return err;
852}
853
Nick Piggin31e6b012011-01-07 17:49:52 +1100854static int follow_up_rcu(struct path *path)
855{
856 struct vfsmount *parent;
857 struct dentry *mountpoint;
858
859 parent = path->mnt->mnt_parent;
860 if (parent == path->mnt)
861 return 0;
862 mountpoint = path->mnt->mnt_mountpoint;
863 path->dentry = mountpoint;
864 path->mnt = parent;
865 return 1;
866}
867
Al Virobab77eb2009-04-18 03:26:48 -0400868int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct vfsmount *parent;
871 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +1000872
873 br_read_lock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400874 parent = path->mnt->mnt_parent;
875 if (parent == path->mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +1000876 br_read_unlock(vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 0;
878 }
879 mntget(parent);
Al Virobab77eb2009-04-18 03:26:48 -0400880 mountpoint = dget(path->mnt->mnt_mountpoint);
Nick Piggin99b7db72010-08-18 04:37:39 +1000881 br_read_unlock(vfsmount_lock);
Al Virobab77eb2009-04-18 03:26:48 -0400882 dput(path->dentry);
883 path->dentry = mountpoint;
884 mntput(path->mnt);
885 path->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return 1;
887}
888
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100889/*
890 * serialization is taken care of in namespace.c
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100892static void __follow_mount_rcu(struct nameidata *nd, struct path *path,
893 struct inode **inode)
894{
895 while (d_mountpoint(path->dentry)) {
896 struct vfsmount *mounted;
897 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
898 if (!mounted)
899 return;
900 path->mnt = mounted;
901 path->dentry = mounted->mnt_root;
902 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
903 *inode = path->dentry->d_inode;
904 }
905}
906
Al Viro463ffb22005-06-06 13:36:05 -0700907static int __follow_mount(struct path *path)
908{
909 int res = 0;
910 while (d_mountpoint(path->dentry)) {
Al Viro1c755af2009-04-18 14:06:57 -0400911 struct vfsmount *mounted = lookup_mnt(path);
Al Viro463ffb22005-06-06 13:36:05 -0700912 if (!mounted)
913 break;
914 dput(path->dentry);
915 if (res)
916 mntput(path->mnt);
917 path->mnt = mounted;
918 path->dentry = dget(mounted->mnt_root);
919 res = 1;
920 }
921 return res;
922}
923
Al Viro79ed0222009-04-18 13:59:41 -0400924static void follow_mount(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Al Viro79ed0222009-04-18 13:59:41 -0400926 while (d_mountpoint(path->dentry)) {
Al Viro1c755af2009-04-18 14:06:57 -0400927 struct vfsmount *mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (!mounted)
929 break;
Al Viro79ed0222009-04-18 13:59:41 -0400930 dput(path->dentry);
931 mntput(path->mnt);
932 path->mnt = mounted;
933 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Al Viro9393bd02009-04-18 13:58:15 -0400937int follow_down(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct vfsmount *mounted;
940
Al Viro1c755af2009-04-18 14:06:57 -0400941 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -0400943 dput(path->dentry);
944 mntput(path->mnt);
945 path->mnt = mounted;
946 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return 1;
948 }
949 return 0;
950}
951
Nick Piggin31e6b012011-01-07 17:49:52 +1100952static int follow_dotdot_rcu(struct nameidata *nd)
953{
954 struct inode *inode = nd->inode;
955
956 set_root_rcu(nd);
957
958 while(1) {
959 if (nd->path.dentry == nd->root.dentry &&
960 nd->path.mnt == nd->root.mnt) {
961 break;
962 }
963 if (nd->path.dentry != nd->path.mnt->mnt_root) {
964 struct dentry *old = nd->path.dentry;
965 struct dentry *parent = old->d_parent;
966 unsigned seq;
967
968 seq = read_seqcount_begin(&parent->d_seq);
969 if (read_seqcount_retry(&old->d_seq, nd->seq))
970 return -ECHILD;
971 inode = parent->d_inode;
972 nd->path.dentry = parent;
973 nd->seq = seq;
974 break;
975 }
976 if (!follow_up_rcu(&nd->path))
977 break;
978 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
979 inode = nd->path.dentry->d_inode;
980 }
981 __follow_mount_rcu(nd, &nd->path, &inode);
982 nd->inode = inode;
983
984 return 0;
985}
986
987static void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Al Viro2a737872009-04-07 11:49:53 -0400989 set_root(nd);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 while(1) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800992 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Al Viro2a737872009-04-07 11:49:53 -0400994 if (nd->path.dentry == nd->root.dentry &&
995 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
997 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800998 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Al Viro3088dd72010-01-30 15:47:29 -0500999 /* rare case of legitimate dget_parent()... */
1000 nd->path.dentry = dget_parent(nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 dput(old);
1002 break;
1003 }
Al Viro3088dd72010-01-30 15:47:29 -05001004 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Al Viro79ed0222009-04-18 13:59:41 -04001007 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001008 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/*
Nick Pigginbaa03892010-08-18 04:37:31 +10001012 * Allocate a dentry with name and parent, and perform a parent
1013 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1014 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1015 * have verified that no child exists while under i_mutex.
1016 */
1017static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1018 struct qstr *name, struct nameidata *nd)
1019{
1020 struct inode *inode = parent->d_inode;
1021 struct dentry *dentry;
1022 struct dentry *old;
1023
1024 /* Don't create child dentry for a dead directory. */
1025 if (unlikely(IS_DEADDIR(inode)))
1026 return ERR_PTR(-ENOENT);
1027
1028 dentry = d_alloc(parent, name);
1029 if (unlikely(!dentry))
1030 return ERR_PTR(-ENOMEM);
1031
1032 old = inode->i_op->lookup(inode, dentry, nd);
1033 if (unlikely(old)) {
1034 dput(dentry);
1035 dentry = old;
1036 }
1037 return dentry;
1038}
1039
1040/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 * It's more convoluted than I'd like it to be, but... it's still fairly
1042 * small and for now I'd prefer to have fast path as straight as possible.
1043 * It _is_ time-critical.
1044 */
1045static int do_lookup(struct nameidata *nd, struct qstr *name,
Nick Piggin31e6b012011-01-07 17:49:52 +11001046 struct path *path, struct inode **inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Jan Blunck4ac91372008-02-14 19:34:32 -08001048 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001049 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro6e6b1bd2009-08-13 23:38:37 +04001050 struct inode *dir;
Al Viro3cac2602009-08-13 18:27:43 +04001051 /*
1052 * See if the low-level filesystem might want
1053 * to use its own hash..
1054 */
Nick Pigginfb045ad2011-01-07 17:49:55 +11001055 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001056 int err = parent->d_op->d_hash(parent, nd->inode, name);
Al Viro3cac2602009-08-13 18:27:43 +04001057 if (err < 0)
1058 return err;
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Nick Pigginb04f7842010-08-18 04:37:34 +10001061 /*
1062 * Rename seqlock is not required here because in the off chance
1063 * of a false negative due to a concurrent rename, we're going to
1064 * do the non-racy lookup, below.
1065 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001066 if (nd->flags & LOOKUP_RCU) {
1067 unsigned seq;
1068
1069 *inode = nd->inode;
1070 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1071 if (!dentry) {
1072 if (nameidata_drop_rcu(nd))
1073 return -ECHILD;
1074 goto need_lookup;
1075 }
1076 /* Memory barrier in read_seqcount_begin of child is enough */
1077 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1078 return -ECHILD;
1079
1080 nd->seq = seq;
Nick Piggin34286d62011-01-07 17:49:57 +11001081 if (dentry->d_flags & DCACHE_OP_REVALIDATE)
Nick Piggin31e6b012011-01-07 17:49:52 +11001082 goto need_revalidate;
Nick Piggin31e6b012011-01-07 17:49:52 +11001083 path->mnt = mnt;
1084 path->dentry = dentry;
1085 __follow_mount_rcu(nd, path, inode);
1086 } else {
1087 dentry = __d_lookup(parent, name);
1088 if (!dentry)
1089 goto need_lookup;
Nick Piggin2e2e88e2010-08-18 04:37:30 +10001090found:
Nick Pigginfb045ad2011-01-07 17:49:55 +11001091 if (dentry->d_flags & DCACHE_OP_REVALIDATE)
Nick Piggin31e6b012011-01-07 17:49:52 +11001092 goto need_revalidate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093done:
Nick Piggin31e6b012011-01-07 17:49:52 +11001094 path->mnt = mnt;
1095 path->dentry = dentry;
1096 __follow_mount(path);
1097 *inode = path->dentry->d_inode;
1098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return 0;
1100
1101need_lookup:
Al Viro6e6b1bd2009-08-13 23:38:37 +04001102 dir = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001103 BUG_ON(nd->inode != dir);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001104
1105 mutex_lock(&dir->i_mutex);
1106 /*
1107 * First re-do the cached lookup just in case it was created
Nick Pigginb04f7842010-08-18 04:37:34 +10001108 * while we waited for the directory semaphore, or the first
1109 * lookup failed due to an unrelated rename.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001110 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001111 * This could use version numbering or similar to avoid unnecessary
1112 * cache lookups, but then we'd have to do the first lookup in the
1113 * non-racy way. However in the common case here, everything should
1114 * be hot in cache, so would it be a big win?
Al Viro6e6b1bd2009-08-13 23:38:37 +04001115 */
1116 dentry = d_lookup(parent, name);
Nick Pigginbaa03892010-08-18 04:37:31 +10001117 if (likely(!dentry)) {
1118 dentry = d_alloc_and_lookup(parent, name, nd);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001119 mutex_unlock(&dir->i_mutex);
1120 if (IS_ERR(dentry))
1121 goto fail;
1122 goto done;
1123 }
Al Viro6e6b1bd2009-08-13 23:38:37 +04001124 /*
1125 * Uhhuh! Nasty case: the cache was re-populated while
1126 * we waited on the semaphore. Need to revalidate.
1127 */
1128 mutex_unlock(&dir->i_mutex);
Nick Piggin2e2e88e2010-08-18 04:37:30 +10001129 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -07001132 dentry = do_revalidate(dentry, nd);
1133 if (!dentry)
1134 goto need_lookup;
1135 if (IS_ERR(dentry))
1136 goto fail;
1137 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139fail:
1140 return PTR_ERR(dentry);
1141}
1142
1143/*
Al Viroac278a92010-02-16 18:09:36 +00001144 * This is a temporary kludge to deal with "automount" symlinks; proper
1145 * solution is to trigger them on follow_mount(), so that do_lookup()
1146 * would DTRT. To be killed before 2.6.34-final.
1147 */
1148static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
1149{
1150 return inode && unlikely(inode->i_op->follow_link) &&
1151 ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
1152}
1153
1154/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01001156 * This is the basic name resolution function, turning a pathname into
1157 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01001159 * Returns 0 and nd will have valid dentry and mnt on success.
1160 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Al Viro6de88d72009-08-09 01:41:57 +04001162static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct path next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int err;
1166 unsigned int lookup_flags = nd->flags;
1167
1168 while (*name=='/')
1169 name++;
1170 if (!*name)
1171 goto return_reval;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -08001174 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* At this point we know we have a real path component. */
1177 for(;;) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001178 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 unsigned long hash;
1180 struct qstr this;
1181 unsigned int c;
1182
Trond Myklebustcdce5d62005-10-18 14:20:18 -07001183 nd->flags |= LOOKUP_CONTINUE;
Nick Piggin31e6b012011-01-07 17:49:52 +11001184 if (nd->flags & LOOKUP_RCU) {
Nick Pigginb74c79e2011-01-07 17:49:58 +11001185 err = exec_permission(nd->inode, IPERM_FLAG_RCU);
Nick Piggin31e6b012011-01-07 17:49:52 +11001186 if (err == -ECHILD) {
1187 if (nameidata_drop_rcu(nd))
1188 return -ECHILD;
1189 goto exec_again;
1190 }
1191 } else {
1192exec_again:
Nick Pigginb74c79e2011-01-07 17:49:58 +11001193 err = exec_permission(nd->inode, 0);
Nick Piggin31e6b012011-01-07 17:49:52 +11001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (err)
1196 break;
1197
1198 this.name = name;
1199 c = *(const unsigned char *)name;
1200
1201 hash = init_name_hash();
1202 do {
1203 name++;
1204 hash = partial_name_hash(c, hash);
1205 c = *(const unsigned char *)name;
1206 } while (c && (c != '/'));
1207 this.len = name - (const char *) this.name;
1208 this.hash = end_name_hash(hash);
1209
1210 /* remove trailing slashes? */
1211 if (!c)
1212 goto last_component;
1213 while (*++name == '/');
1214 if (!*name)
1215 goto last_with_slashes;
1216
1217 /*
1218 * "." and ".." are special - ".." especially so because it has
1219 * to be able to know about the current root directory and
1220 * parent relationships.
1221 */
1222 if (this.name[0] == '.') switch (this.len) {
1223 default:
1224 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001225 case 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (this.name[1] != '.')
1227 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001228 if (nd->flags & LOOKUP_RCU) {
1229 if (follow_dotdot_rcu(nd))
1230 return -ECHILD;
1231 } else
1232 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* fallthrough */
1234 case 1:
1235 continue;
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* This does the actual lookups.. */
Nick Piggin31e6b012011-01-07 17:49:52 +11001238 err = do_lookup(nd, &this, &next, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (err)
1240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (!inode)
1243 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 if (inode->i_op->follow_link) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001246 /* We commonly drop rcu-walk here */
1247 if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
1248 return -ECHILD;
1249 BUG_ON(inode != next.dentry->d_inode);
Al Viro90ebe562005-06-06 13:35:58 -07001250 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (err)
1252 goto return_err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001253 nd->inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 err = -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001255 if (!nd->inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001257 } else {
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001258 path_to_nameidata(&next, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001259 nd->inode = inode;
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001262 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
1264 continue;
1265 /* here ends the main loop */
1266
1267last_with_slashes:
1268 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1269last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -08001270 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1271 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (lookup_flags & LOOKUP_PARENT)
1273 goto lookup_parent;
1274 if (this.name[0] == '.') switch (this.len) {
1275 default:
1276 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001277 case 2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (this.name[1] != '.')
1279 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001280 if (nd->flags & LOOKUP_RCU) {
1281 if (follow_dotdot_rcu(nd))
1282 return -ECHILD;
1283 } else
1284 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* fallthrough */
1286 case 1:
1287 goto return_reval;
1288 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001289 err = do_lookup(nd, &this, &next, &inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (err)
1291 break;
Al Viroac278a92010-02-16 18:09:36 +00001292 if (follow_on_final(inode, lookup_flags)) {
Nick Piggin31e6b012011-01-07 17:49:52 +11001293 if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
1294 return -ECHILD;
1295 BUG_ON(inode != next.dentry->d_inode);
Al Viro90ebe562005-06-06 13:35:58 -07001296 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (err)
1298 goto return_err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001299 nd->inode = nd->path.dentry->d_inode;
1300 } else {
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001301 path_to_nameidata(&next, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11001302 nd->inode = inode;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 err = -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001305 if (!nd->inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
1307 if (lookup_flags & LOOKUP_DIRECTORY) {
1308 err = -ENOTDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11001309 if (!nd->inode->i_op->lookup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311 }
1312 goto return_base;
1313lookup_parent:
1314 nd->last = this;
1315 nd->last_type = LAST_NORM;
1316 if (this.name[0] != '.')
1317 goto return_base;
1318 if (this.len == 1)
1319 nd->last_type = LAST_DOT;
1320 else if (this.len == 2 && this.name[1] == '.')
1321 nd->last_type = LAST_DOTDOT;
1322 else
1323 goto return_base;
1324return_reval:
1325 /*
1326 * We bypassed the ordinary revalidation routines.
1327 * We may need to check the cached dentry for staleness.
1328 */
Nick Pigginfb045ad2011-01-07 17:49:55 +11001329 if (need_reval_dot(nd->path.dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* Note: we do not d_invalidate() */
Nick Piggin34286d62011-01-07 17:49:57 +11001331 err = d_revalidate(nd->path.dentry, nd);
1332 if (!err)
1333 err = -ESTALE;
1334 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
1336 }
1337return_base:
Nick Piggin31e6b012011-01-07 17:49:52 +11001338 if (nameidata_drop_rcu_last_maybe(nd))
1339 return -ECHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return 0;
1341out_dput:
Nick Piggin31e6b012011-01-07 17:49:52 +11001342 if (!(nd->flags & LOOKUP_RCU))
1343 path_put_conditional(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
1345 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001346 if (!(nd->flags & LOOKUP_RCU))
1347 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348return_err:
1349 return err;
1350}
1351
Nick Piggin31e6b012011-01-07 17:49:52 +11001352static inline int path_walk_rcu(const char *name, struct nameidata *nd)
1353{
1354 current->total_link_count = 0;
1355
1356 return link_path_walk(name, nd);
1357}
1358
1359static inline int path_walk_simple(const char *name, struct nameidata *nd)
1360{
1361 current->total_link_count = 0;
1362
1363 return link_path_walk(name, nd);
1364}
1365
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001366static int path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Al Viro6de88d72009-08-09 01:41:57 +04001368 struct path save = nd->path;
1369 int result;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 current->total_link_count = 0;
Al Viro6de88d72009-08-09 01:41:57 +04001372
1373 /* make sure the stuff we saved doesn't go away */
1374 path_get(&save);
1375
1376 result = link_path_walk(name, nd);
1377 if (result == -ESTALE) {
1378 /* nd->path had been dropped */
1379 current->total_link_count = 0;
1380 nd->path = save;
1381 path_get(&nd->path);
1382 nd->flags |= LOOKUP_REVAL;
1383 result = link_path_walk(name, nd);
1384 }
1385
1386 path_put(&save);
1387
1388 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Nick Piggin31e6b012011-01-07 17:49:52 +11001391static void path_finish_rcu(struct nameidata *nd)
1392{
1393 if (nd->flags & LOOKUP_RCU) {
1394 /* RCU dangling. Cancel it. */
1395 nd->flags &= ~LOOKUP_RCU;
1396 nd->root.mnt = NULL;
1397 rcu_read_unlock();
1398 br_read_unlock(vfsmount_lock);
1399 }
1400 if (nd->file)
1401 fput(nd->file);
1402}
1403
1404static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1405{
1406 int retval = 0;
1407 int fput_needed;
1408 struct file *file;
1409
1410 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1411 nd->flags = flags | LOOKUP_RCU;
1412 nd->depth = 0;
1413 nd->root.mnt = NULL;
1414 nd->file = NULL;
1415
1416 if (*name=='/') {
1417 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +11001418 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001419
1420 br_read_lock(vfsmount_lock);
1421 rcu_read_lock();
1422
Nick Pigginc28cc362011-01-07 17:49:53 +11001423 do {
1424 seq = read_seqcount_begin(&fs->seq);
1425 nd->root = fs->root;
1426 nd->path = nd->root;
1427 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1428 } while (read_seqcount_retry(&fs->seq, seq));
Nick Piggin31e6b012011-01-07 17:49:52 +11001429
1430 } else if (dfd == AT_FDCWD) {
1431 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +11001432 unsigned seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001433
1434 br_read_lock(vfsmount_lock);
1435 rcu_read_lock();
1436
Nick Pigginc28cc362011-01-07 17:49:53 +11001437 do {
1438 seq = read_seqcount_begin(&fs->seq);
1439 nd->path = fs->pwd;
1440 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1441 } while (read_seqcount_retry(&fs->seq, seq));
1442
Nick Piggin31e6b012011-01-07 17:49:52 +11001443 } else {
1444 struct dentry *dentry;
1445
1446 file = fget_light(dfd, &fput_needed);
1447 retval = -EBADF;
1448 if (!file)
1449 goto out_fail;
1450
1451 dentry = file->f_path.dentry;
1452
1453 retval = -ENOTDIR;
1454 if (!S_ISDIR(dentry->d_inode->i_mode))
1455 goto fput_fail;
1456
1457 retval = file_permission(file, MAY_EXEC);
1458 if (retval)
1459 goto fput_fail;
1460
1461 nd->path = file->f_path;
1462 if (fput_needed)
1463 nd->file = file;
1464
Nick Pigginc28cc362011-01-07 17:49:53 +11001465 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Nick Piggin31e6b012011-01-07 17:49:52 +11001466 br_read_lock(vfsmount_lock);
1467 rcu_read_lock();
1468 }
1469 nd->inode = nd->path.dentry->d_inode;
1470 return 0;
1471
1472fput_fail:
1473 fput_light(file, fput_needed);
1474out_fail:
1475 return retval;
1476}
1477
Al Viro9b4a9b12009-04-07 11:44:16 -04001478static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001480 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001481 int fput_needed;
1482 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1485 nd->flags = flags;
1486 nd->depth = 0;
Al Viro2a737872009-04-07 11:49:53 -04001487 nd->root.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (*name=='/') {
Al Viro2a737872009-04-07 11:49:53 -04001490 set_root(nd);
1491 nd->path = nd->root;
1492 path_get(&nd->root);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001493 } else if (dfd == AT_FDCWD) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02001494 get_fs_pwd(current->fs, &nd->path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001495 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001496 struct dentry *dentry;
1497
1498 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001499 retval = -EBADF;
1500 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001501 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001502
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001503 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001504
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001505 retval = -ENOTDIR;
1506 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001507 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001508
1509 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001510 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001511 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001512
Jan Blunck5dd784d2008-02-14 19:34:38 -08001513 nd->path = file->f_path;
1514 path_get(&file->f_path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001515
1516 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001518 nd->inode = nd->path.dentry->d_inode;
Al Viro9b4a9b12009-04-07 11:44:16 -04001519 return 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001520
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001521fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001522 fput_light(file, fput_needed);
Al Viro9b4a9b12009-04-07 11:44:16 -04001523out_fail:
1524 return retval;
1525}
1526
1527/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1528static int do_path_lookup(int dfd, const char *name,
1529 unsigned int flags, struct nameidata *nd)
1530{
Nick Piggin31e6b012011-01-07 17:49:52 +11001531 int retval;
1532
1533 /*
1534 * Path walking is largely split up into 2 different synchronisation
1535 * schemes, rcu-walk and ref-walk (explained in
1536 * Documentation/filesystems/path-lookup.txt). These share much of the
1537 * path walk code, but some things particularly setup, cleanup, and
1538 * following mounts are sufficiently divergent that functions are
1539 * duplicated. Typically there is a function foo(), and its RCU
1540 * analogue, foo_rcu().
1541 *
1542 * -ECHILD is the error number of choice (just to avoid clashes) that
1543 * is returned if some aspect of an rcu-walk fails. Such an error must
1544 * be handled by restarting a traditional ref-walk (which will always
1545 * be able to complete).
1546 */
1547 retval = path_init_rcu(dfd, name, flags, nd);
1548 if (unlikely(retval))
1549 return retval;
1550 retval = path_walk_rcu(name, nd);
1551 path_finish_rcu(nd);
Al Viro2a737872009-04-07 11:49:53 -04001552 if (nd->root.mnt) {
1553 path_put(&nd->root);
1554 nd->root.mnt = NULL;
1555 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001556
1557 if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
1558 /* slower, locked walk */
1559 if (retval == -ESTALE)
1560 flags |= LOOKUP_REVAL;
1561 retval = path_init(dfd, name, flags, nd);
1562 if (unlikely(retval))
1563 return retval;
1564 retval = path_walk(name, nd);
1565 if (nd->root.mnt) {
1566 path_put(&nd->root);
1567 nd->root.mnt = NULL;
1568 }
1569 }
1570
1571 if (likely(!retval)) {
1572 if (unlikely(!audit_dummy_context())) {
1573 if (nd->path.dentry && nd->inode)
1574 audit_inode(name, nd->path.dentry);
1575 }
1576 }
1577
Al Viro9b4a9b12009-04-07 11:44:16 -04001578 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001581int path_lookup(const char *name, unsigned int flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001582 struct nameidata *nd)
1583{
1584 return do_path_lookup(AT_FDCWD, name, flags, nd);
1585}
1586
Al Virod1811462008-08-02 00:49:18 -04001587int kern_path(const char *name, unsigned int flags, struct path *path)
1588{
1589 struct nameidata nd;
1590 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1591 if (!res)
1592 *path = nd.path;
1593 return res;
1594}
1595
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001596/**
1597 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1598 * @dentry: pointer to dentry of the base directory
1599 * @mnt: pointer to vfs mount of the base directory
1600 * @name: pointer to file name
1601 * @flags: lookup flags
1602 * @nd: pointer to nameidata
1603 */
1604int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1605 const char *name, unsigned int flags,
1606 struct nameidata *nd)
1607{
1608 int retval;
1609
1610 /* same as do_path_lookup */
1611 nd->last_type = LAST_ROOT;
1612 nd->flags = flags;
1613 nd->depth = 0;
1614
Jan Blunckc8e7f442008-06-09 16:40:35 -07001615 nd->path.dentry = dentry;
1616 nd->path.mnt = mnt;
1617 path_get(&nd->path);
Al Viro5b857112009-04-07 11:53:49 -04001618 nd->root = nd->path;
1619 path_get(&nd->root);
Nick Piggin31e6b012011-01-07 17:49:52 +11001620 nd->inode = nd->path.dentry->d_inode;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001621
1622 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001623 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
Nick Piggin31e6b012011-01-07 17:49:52 +11001624 nd->inode))
Jan Blunck4ac91372008-02-14 19:34:32 -08001625 audit_inode(name, nd->path.dentry);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001626
Al Viro5b857112009-04-07 11:53:49 -04001627 path_put(&nd->root);
1628 nd->root.mnt = NULL;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001629
Al Viro2a737872009-04-07 11:49:53 -04001630 return retval;
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001631}
1632
Christoph Hellwigeead1912007-10-16 23:25:38 -07001633static struct dentry *__lookup_hash(struct qstr *name,
1634 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Christoph Hellwig81fca442010-10-06 10:47:47 +02001636 struct inode *inode = base->d_inode;
James Morris057f6c02007-04-26 00:12:05 -07001637 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int err;
1639
Nick Pigginb74c79e2011-01-07 17:49:58 +11001640 err = exec_permission(inode, 0);
Christoph Hellwig81fca442010-10-06 10:47:47 +02001641 if (err)
1642 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /*
1645 * See if the low-level filesystem might want
1646 * to use its own hash..
1647 */
Nick Pigginfb045ad2011-01-07 17:49:55 +11001648 if (base->d_flags & DCACHE_OP_HASH) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001649 err = base->d_op->d_hash(base, inode, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 dentry = ERR_PTR(err);
1651 if (err < 0)
1652 goto out;
1653 }
1654
Nick Pigginb04f7842010-08-18 04:37:34 +10001655 /*
1656 * Don't bother with __d_lookup: callers are for creat as
1657 * well as unlink, so a lot of the time it would cost
1658 * a double lookup.
Al Viro6e6b1bd2009-08-13 23:38:37 +04001659 */
Nick Pigginb04f7842010-08-18 04:37:34 +10001660 dentry = d_lookup(base, name);
Al Viro6e6b1bd2009-08-13 23:38:37 +04001661
Nick Pigginfb045ad2011-01-07 17:49:55 +11001662 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro6e6b1bd2009-08-13 23:38:37 +04001663 dentry = do_revalidate(dentry, nd);
1664
Nick Pigginbaa03892010-08-18 04:37:31 +10001665 if (!dentry)
1666 dentry = d_alloc_and_lookup(base, name, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667out:
1668 return dentry;
1669}
1670
James Morris057f6c02007-04-26 00:12:05 -07001671/*
1672 * Restricted form of lookup. Doesn't follow links, single-component only,
1673 * needs parent already locked. Doesn't follow mounts.
1674 * SMP-safe.
1675 */
Adrian Bunka244e162006-03-31 02:32:11 -08001676static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Jan Blunck4ac91372008-02-14 19:34:32 -08001678 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Christoph Hellwigeead1912007-10-16 23:25:38 -07001681static int __lookup_one_len(const char *name, struct qstr *this,
1682 struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
1684 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 unsigned int c;
1686
James Morris057f6c02007-04-26 00:12:05 -07001687 this->name = name;
1688 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001690 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 hash = init_name_hash();
1693 while (len--) {
1694 c = *(const unsigned char *)name++;
1695 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001696 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 hash = partial_name_hash(c, hash);
1698 }
James Morris057f6c02007-04-26 00:12:05 -07001699 this->hash = end_name_hash(hash);
1700 return 0;
1701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Christoph Hellwigeead1912007-10-16 23:25:38 -07001703/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001704 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001705 * @name: pathname component to lookup
1706 * @base: base directory to lookup from
1707 * @len: maximum length @len should be interpreted to
1708 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001709 * Note that this routine is purely a helper for filesystem usage and should
1710 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001711 * nameidata argument is passed to the filesystem methods and a filesystem
1712 * using this helper needs to be prepared for that.
1713 */
James Morris057f6c02007-04-26 00:12:05 -07001714struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1715{
1716 int err;
1717 struct qstr this;
1718
David Woodhouse2f9092e2009-04-20 23:18:37 +01001719 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1720
James Morris057f6c02007-04-26 00:12:05 -07001721 err = __lookup_one_len(name, &this, base, len);
1722 if (err)
1723 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001724
Christoph Hellwig49705b72005-11-08 21:35:06 -08001725 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001726}
1727
Al Viro2d8f3032008-07-22 09:59:21 -04001728int user_path_at(int dfd, const char __user *name, unsigned flags,
1729 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Al Viro2d8f3032008-07-22 09:59:21 -04001731 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 char *tmp = getname(name);
1733 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001735
1736 BUG_ON(flags & LOOKUP_PARENT);
1737
1738 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001740 if (!err)
1741 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 return err;
1744}
1745
Al Viro2ad94ae2008-07-21 09:32:51 -04001746static int user_path_parent(int dfd, const char __user *path,
1747 struct nameidata *nd, char **name)
1748{
1749 char *s = getname(path);
1750 int error;
1751
1752 if (IS_ERR(s))
1753 return PTR_ERR(s);
1754
1755 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1756 if (error)
1757 putname(s);
1758 else
1759 *name = s;
1760
1761 return error;
1762}
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/*
1765 * It's inline, so penalty for filesystems that don't use sticky bit is
1766 * minimal.
1767 */
1768static inline int check_sticky(struct inode *dir, struct inode *inode)
1769{
David Howellsda9592e2008-11-14 10:39:05 +11001770 uid_t fsuid = current_fsuid();
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (!(dir->i_mode & S_ISVTX))
1773 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001774 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001776 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return 0;
1778 return !capable(CAP_FOWNER);
1779}
1780
1781/*
1782 * Check whether we can remove a link victim from directory dir, check
1783 * whether the type of victim is right.
1784 * 1. We can't do it if dir is read-only (done in permission())
1785 * 2. We should have write and exec permissions on dir
1786 * 3. We can't remove anything from append-only dir
1787 * 4. We can't do anything with immutable dir (done in permission())
1788 * 5. If the sticky bit on dir is set we should either
1789 * a. be owner of dir, or
1790 * b. be owner of victim, or
1791 * c. have CAP_FOWNER capability
1792 * 6. If the victim is append-only or immutable we can't do antyhing with
1793 * links pointing to it.
1794 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1795 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1796 * 9. We can't remove a root or mountpoint.
1797 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1798 * nfs_async_unlink().
1799 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001800static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 int error;
1803
1804 if (!victim->d_inode)
1805 return -ENOENT;
1806
1807 BUG_ON(victim->d_parent->d_inode != dir);
Al Virocccc6bb2009-12-25 05:07:33 -05001808 audit_inode_child(victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Al Virof419a2e2008-07-22 00:07:17 -04001810 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (error)
1812 return error;
1813 if (IS_APPEND(dir))
1814 return -EPERM;
1815 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001816 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return -EPERM;
1818 if (isdir) {
1819 if (!S_ISDIR(victim->d_inode->i_mode))
1820 return -ENOTDIR;
1821 if (IS_ROOT(victim))
1822 return -EBUSY;
1823 } else if (S_ISDIR(victim->d_inode->i_mode))
1824 return -EISDIR;
1825 if (IS_DEADDIR(dir))
1826 return -ENOENT;
1827 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1828 return -EBUSY;
1829 return 0;
1830}
1831
1832/* Check whether we can create an object with dentry child in directory
1833 * dir.
1834 * 1. We can't do it if child already exists (open has special treatment for
1835 * this case, but since we are inlined it's OK)
1836 * 2. We can't do it if dir is read-only (done in permission())
1837 * 3. We should have write and exec permissions on dir
1838 * 4. We can't do it if dir is immutable (done in permission())
1839 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001840static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 if (child->d_inode)
1843 return -EEXIST;
1844 if (IS_DEADDIR(dir))
1845 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001846 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849/*
1850 * p1 and p2 should be directories on the same fs.
1851 */
1852struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1853{
1854 struct dentry *p;
1855
1856 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001857 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return NULL;
1859 }
1860
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001861 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001863 p = d_ancestor(p2, p1);
1864 if (p) {
1865 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1866 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1867 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001870 p = d_ancestor(p1, p2);
1871 if (p) {
1872 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1873 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1874 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
1876
Ingo Molnarf2eace22006-07-03 00:25:05 -07001877 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1878 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return NULL;
1880}
1881
1882void unlock_rename(struct dentry *p1, struct dentry *p2)
1883{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001884 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001886 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001887 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889}
1890
1891int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1892 struct nameidata *nd)
1893{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001894 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 if (error)
1897 return error;
1898
Al Viroacfa4382008-12-04 10:06:33 -05001899 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return -EACCES; /* shouldn't it be ENOSYS? */
1901 mode &= S_IALLUGO;
1902 mode |= S_IFREG;
1903 error = security_inode_create(dir, dentry, mode);
1904 if (error)
1905 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001907 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001908 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return error;
1910}
1911
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001912int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001914 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 struct inode *inode = dentry->d_inode;
1916 int error;
1917
1918 if (!inode)
1919 return -ENOENT;
1920
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001921 switch (inode->i_mode & S_IFMT) {
1922 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001924 case S_IFDIR:
1925 if (acc_mode & MAY_WRITE)
1926 return -EISDIR;
1927 break;
1928 case S_IFBLK:
1929 case S_IFCHR:
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001930 if (path->mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001932 /*FALLTHRU*/
1933 case S_IFIFO:
1934 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01001936 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001937 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001938
Christoph Hellwig3fb64192008-10-24 09:58:10 +02001939 error = inode_permission(inode, acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07001940 if (error)
1941 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /*
1944 * An append-only file must be opened in append mode for writing.
1945 */
1946 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05001947 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05001948 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05001950 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
1953 /* O_NOATIME can only be set by the owner or superuser */
Al Viro7715b522009-12-16 03:54:00 -05001954 if (flag & O_NOATIME && !is_owner_or_cap(inode))
1955 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 /*
1958 * Ensure there are no outstanding leases on the file.
1959 */
Al Virob65a9cf2009-12-16 06:27:40 -05001960 return break_lease(inode, flag);
Al Viro7715b522009-12-16 03:54:00 -05001961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Al Viro7715b522009-12-16 03:54:00 -05001963static int handle_truncate(struct path *path)
1964{
1965 struct inode *inode = path->dentry->d_inode;
1966 int error = get_write_access(inode);
1967 if (error)
1968 return error;
1969 /*
1970 * Refuse to truncate files with mandatory locks held on them.
1971 */
1972 error = locks_verify_locked(inode);
1973 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09001974 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05001975 if (!error) {
1976 error = do_truncate(path->dentry, 0,
1977 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1978 NULL);
1979 }
1980 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04001981 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Dave Hansend57999e2008-02-15 14:37:27 -08001984/*
1985 * Be careful about ever adding any more callers of this
1986 * function. Its flags must be in the namei format, not
1987 * what get passed to sys_open().
1988 */
1989static int __open_namei_create(struct nameidata *nd, struct path *path,
Al Viro8737c932009-12-24 06:47:55 -05001990 int open_flag, int mode)
Dave Hansenaab520e2006-09-30 23:29:02 -07001991{
1992 int error;
Jan Blunck4ac91372008-02-14 19:34:32 -08001993 struct dentry *dir = nd->path.dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001994
1995 if (!IS_POSIXACL(dir->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04001996 mode &= ~current_umask();
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09001997 error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1998 if (error)
1999 goto out_unlock;
Dave Hansenaab520e2006-09-30 23:29:02 -07002000 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002001out_unlock:
Dave Hansenaab520e2006-09-30 23:29:02 -07002002 mutex_unlock(&dir->d_inode->i_mutex);
Jan Blunck4ac91372008-02-14 19:34:32 -08002003 dput(nd->path.dentry);
2004 nd->path.dentry = path->dentry;
Nick Piggin31e6b012011-01-07 17:49:52 +11002005
Dave Hansenaab520e2006-09-30 23:29:02 -07002006 if (error)
2007 return error;
2008 /* Don't check for write permission, don't truncate */
Al Viro8737c932009-12-24 06:47:55 -05002009 return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
Dave Hansenaab520e2006-09-30 23:29:02 -07002010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012/*
Dave Hansend57999e2008-02-15 14:37:27 -08002013 * Note that while the flag value (low two bits) for sys_open means:
2014 * 00 - read-only
2015 * 01 - write-only
2016 * 10 - read-write
2017 * 11 - special
2018 * it is changed into
2019 * 00 - no permissions needed
2020 * 01 - read-permission
2021 * 10 - write-permission
2022 * 11 - read-write
2023 * for the internal routines (ie open_namei()/follow_link() etc)
2024 * This is more logical, and also allows the 00 "no perm needed"
2025 * to be used for symlinks (where the permissions are checked
2026 * later).
2027 *
2028*/
2029static inline int open_to_namei_flags(int flag)
2030{
2031 if ((flag+1) & O_ACCMODE)
2032 flag++;
2033 return flag;
2034}
2035
Al Viro7715b522009-12-16 03:54:00 -05002036static int open_will_truncate(int flag, struct inode *inode)
Dave Hansen4a3fd212008-02-15 14:37:48 -08002037{
2038 /*
2039 * We'll never write to the fs underlying
2040 * a device file.
2041 */
2042 if (special_file(inode->i_mode))
2043 return 0;
2044 return (flag & O_TRUNC);
2045}
2046
Al Viro648fa862009-12-24 01:26:48 -05002047static struct file *finish_open(struct nameidata *nd,
Al Viro9a661792009-12-24 06:49:47 -05002048 int open_flag, int acc_mode)
Al Viro648fa862009-12-24 01:26:48 -05002049{
2050 struct file *filp;
2051 int will_truncate;
2052 int error;
2053
Al Viro9a661792009-12-24 06:49:47 -05002054 will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
Al Viro648fa862009-12-24 01:26:48 -05002055 if (will_truncate) {
2056 error = mnt_want_write(nd->path.mnt);
2057 if (error)
2058 goto exit;
2059 }
2060 error = may_open(&nd->path, acc_mode, open_flag);
2061 if (error) {
2062 if (will_truncate)
2063 mnt_drop_write(nd->path.mnt);
2064 goto exit;
2065 }
2066 filp = nameidata_to_filp(nd);
2067 if (!IS_ERR(filp)) {
2068 error = ima_file_check(filp, acc_mode);
2069 if (error) {
2070 fput(filp);
2071 filp = ERR_PTR(error);
2072 }
2073 }
2074 if (!IS_ERR(filp)) {
Al Viro648fa862009-12-24 01:26:48 -05002075 if (will_truncate) {
2076 error = handle_truncate(&nd->path);
2077 if (error) {
2078 fput(filp);
2079 filp = ERR_PTR(error);
2080 }
2081 }
2082 }
2083 /*
2084 * It is now safe to drop the mnt write
2085 * because the filp has had a write taken
2086 * on its behalf.
2087 */
2088 if (will_truncate)
2089 mnt_drop_write(nd->path.mnt);
Al Virod893f1b2010-10-29 03:30:42 -04002090 path_put(&nd->path);
Al Viro648fa862009-12-24 01:26:48 -05002091 return filp;
2092
2093exit:
2094 if (!IS_ERR(nd->intent.open.file))
2095 release_open_intent(nd);
2096 path_put(&nd->path);
2097 return ERR_PTR(error);
2098}
2099
Nick Piggin31e6b012011-01-07 17:49:52 +11002100/*
2101 * Handle O_CREAT case for do_filp_open
2102 */
Al Virofb1cc552009-12-24 01:58:28 -05002103static struct file *do_last(struct nameidata *nd, struct path *path,
Al Viro5b369df2009-12-24 06:51:13 -05002104 int open_flag, int acc_mode,
Al Viro3e297b62010-03-26 12:40:13 -04002105 int mode, const char *pathname)
Al Virofb1cc552009-12-24 01:58:28 -05002106{
Al Viroa1e28032009-12-24 02:12:06 -05002107 struct dentry *dir = nd->path.dentry;
Al Virofb1cc552009-12-24 01:58:28 -05002108 struct file *filp;
Al Viro1f36f772009-12-26 10:56:19 -05002109 int error = -EISDIR;
Al Virofb1cc552009-12-24 01:58:28 -05002110
Al Viro1f36f772009-12-26 10:56:19 -05002111 switch (nd->last_type) {
2112 case LAST_DOTDOT:
2113 follow_dotdot(nd);
2114 dir = nd->path.dentry;
Neil Brown176306f2010-05-24 16:57:56 +10002115 case LAST_DOT:
Nick Pigginfb045ad2011-01-07 17:49:55 +11002116 if (need_reval_dot(dir)) {
Nick Piggin34286d62011-01-07 17:49:57 +11002117 error = d_revalidate(nd->path.dentry, nd);
2118 if (!error)
Al Viro1f36f772009-12-26 10:56:19 -05002119 error = -ESTALE;
Nick Piggin34286d62011-01-07 17:49:57 +11002120 if (error < 0)
Al Viro1f36f772009-12-26 10:56:19 -05002121 goto exit;
Al Viro1f36f772009-12-26 10:56:19 -05002122 }
2123 /* fallthrough */
Al Viro1f36f772009-12-26 10:56:19 -05002124 case LAST_ROOT:
Nick Piggin31e6b012011-01-07 17:49:52 +11002125 goto exit;
Al Viro1f36f772009-12-26 10:56:19 -05002126 case LAST_BIND:
2127 audit_inode(pathname, dir);
Al Viro67ee3ad2009-12-26 07:01:01 -05002128 goto ok;
Al Viro1f36f772009-12-26 10:56:19 -05002129 }
Al Viro67ee3ad2009-12-26 07:01:01 -05002130
Al Viro1f36f772009-12-26 10:56:19 -05002131 /* trailing slashes? */
Nick Piggin31e6b012011-01-07 17:49:52 +11002132 if (nd->last.name[nd->last.len])
2133 goto exit;
Al Viroa2c36b42009-12-24 03:39:50 -05002134
Al Viroa1e28032009-12-24 02:12:06 -05002135 mutex_lock(&dir->d_inode->i_mutex);
2136
2137 path->dentry = lookup_hash(nd);
2138 path->mnt = nd->path.mnt;
2139
Al Virofb1cc552009-12-24 01:58:28 -05002140 error = PTR_ERR(path->dentry);
2141 if (IS_ERR(path->dentry)) {
2142 mutex_unlock(&dir->d_inode->i_mutex);
2143 goto exit;
2144 }
2145
2146 if (IS_ERR(nd->intent.open.file)) {
2147 error = PTR_ERR(nd->intent.open.file);
2148 goto exit_mutex_unlock;
2149 }
2150
2151 /* Negative dentry, just create the file */
2152 if (!path->dentry->d_inode) {
2153 /*
2154 * This write is needed to ensure that a
2155 * ro->rw transition does not occur between
2156 * the time when the file is created and when
2157 * a permanent write count is taken through
2158 * the 'struct file' in nameidata_to_filp().
2159 */
2160 error = mnt_want_write(nd->path.mnt);
2161 if (error)
2162 goto exit_mutex_unlock;
2163 error = __open_namei_create(nd, path, open_flag, mode);
2164 if (error) {
2165 mnt_drop_write(nd->path.mnt);
2166 goto exit;
2167 }
2168 filp = nameidata_to_filp(nd);
2169 mnt_drop_write(nd->path.mnt);
Al Virod893f1b2010-10-29 03:30:42 -04002170 path_put(&nd->path);
Al Virofb1cc552009-12-24 01:58:28 -05002171 if (!IS_ERR(filp)) {
2172 error = ima_file_check(filp, acc_mode);
2173 if (error) {
2174 fput(filp);
2175 filp = ERR_PTR(error);
2176 }
2177 }
2178 return filp;
2179 }
2180
2181 /*
2182 * It already exists.
2183 */
2184 mutex_unlock(&dir->d_inode->i_mutex);
2185 audit_inode(pathname, path->dentry);
2186
2187 error = -EEXIST;
Al Viro5b369df2009-12-24 06:51:13 -05002188 if (open_flag & O_EXCL)
Al Virofb1cc552009-12-24 01:58:28 -05002189 goto exit_dput;
2190
2191 if (__follow_mount(path)) {
2192 error = -ELOOP;
Al Viro5b369df2009-12-24 06:51:13 -05002193 if (open_flag & O_NOFOLLOW)
Al Virofb1cc552009-12-24 01:58:28 -05002194 goto exit_dput;
2195 }
2196
2197 error = -ENOENT;
2198 if (!path->dentry->d_inode)
2199 goto exit_dput;
Al Viro9e67f362009-12-26 07:04:50 -05002200
2201 if (path->dentry->d_inode->i_op->follow_link)
Al Virofb1cc552009-12-24 01:58:28 -05002202 return NULL;
Al Virofb1cc552009-12-24 01:58:28 -05002203
2204 path_to_nameidata(path, nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002205 nd->inode = path->dentry->d_inode;
Al Virofb1cc552009-12-24 01:58:28 -05002206 error = -EISDIR;
Nick Piggin31e6b012011-01-07 17:49:52 +11002207 if (S_ISDIR(nd->inode->i_mode))
Al Virofb1cc552009-12-24 01:58:28 -05002208 goto exit;
Al Viro67ee3ad2009-12-26 07:01:01 -05002209ok:
Al Viro9a661792009-12-24 06:49:47 -05002210 filp = finish_open(nd, open_flag, acc_mode);
Al Virofb1cc552009-12-24 01:58:28 -05002211 return filp;
2212
2213exit_mutex_unlock:
2214 mutex_unlock(&dir->d_inode->i_mutex);
2215exit_dput:
2216 path_put_conditional(path, nd);
2217exit:
2218 if (!IS_ERR(nd->intent.open.file))
2219 release_open_intent(nd);
Al Virofb1cc552009-12-24 01:58:28 -05002220 path_put(&nd->path);
2221 return ERR_PTR(error);
2222}
2223
Dave Hansend57999e2008-02-15 14:37:27 -08002224/*
Dave Hansen4a3fd212008-02-15 14:37:48 -08002225 * Note that the low bits of the passed in "open_flag"
2226 * are not the same as in the local variable "flag". See
2227 * open_to_namei_flags() for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002229struct file *do_filp_open(int dfd, const char *pathname,
Al Viro6e8341a2009-04-06 11:16:22 -04002230 int open_flag, int mode, int acc_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Dave Hansen4a3fd212008-02-15 14:37:48 -08002232 struct file *filp;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002233 struct nameidata nd;
Al Viro6e8341a2009-04-06 11:16:22 -04002234 int error;
Al Viro9850c052010-01-13 15:01:15 -05002235 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 int count = 0;
Dave Hansend57999e2008-02-15 14:37:27 -08002237 int flag = open_to_namei_flags(open_flag);
Nick Piggin31e6b012011-01-07 17:49:52 +11002238 int flags;
Al Viro1f36f772009-12-26 10:56:19 -05002239
2240 if (!(open_flag & O_CREAT))
2241 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Lino Sanfilippob1085ba2010-11-05 17:05:27 +01002243 /* Must never be set by userspace */
2244 open_flag &= ~FMODE_NONOTIFY;
2245
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01002246 /*
2247 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
2248 * check for O_DSYNC if the need any syncing at all we enforce it's
2249 * always set instead of having to deal with possibly weird behaviour
2250 * for malicious applications setting only __O_SYNC.
2251 */
2252 if (open_flag & __O_SYNC)
2253 open_flag |= O_DSYNC;
2254
Al Viro6e8341a2009-04-06 11:16:22 -04002255 if (!acc_mode)
Al Viro6d125522009-12-24 06:58:56 -05002256 acc_mode = MAY_OPEN | ACC_MODE(open_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Trond Myklebust834f2a42005-10-18 14:20:16 -07002258 /* O_TRUNC implies we need access checks for write permissions */
Al Viro4296e2c2009-12-24 07:15:41 -05002259 if (open_flag & O_TRUNC)
Trond Myklebust834f2a42005-10-18 14:20:16 -07002260 acc_mode |= MAY_WRITE;
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /* Allow the LSM permission hook to distinguish append
2263 access from general write access. */
Al Viro4296e2c2009-12-24 07:15:41 -05002264 if (open_flag & O_APPEND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 acc_mode |= MAY_APPEND;
2266
Nick Piggin31e6b012011-01-07 17:49:52 +11002267 flags = LOOKUP_OPEN;
2268 if (open_flag & O_CREAT) {
2269 flags |= LOOKUP_CREATE;
2270 if (open_flag & O_EXCL)
2271 flags |= LOOKUP_EXCL;
J. R. Okajima654f5622009-06-18 23:30:15 +09002272 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002273 if (open_flag & O_DIRECTORY)
2274 flags |= LOOKUP_DIRECTORY;
2275 if (!(open_flag & O_NOFOLLOW))
2276 flags |= LOOKUP_FOLLOW;
2277
2278 filp = get_empty_filp();
2279 if (!filp)
2280 return ERR_PTR(-ENFILE);
2281
2282 filp->f_flags = open_flag;
2283 nd.intent.open.file = filp;
2284 nd.intent.open.flags = flag;
2285 nd.intent.open.create_mode = mode;
2286
2287 if (open_flag & O_CREAT)
2288 goto creat;
2289
2290 /* !O_CREAT, simple open */
2291 error = do_path_lookup(dfd, pathname, flags, &nd);
2292 if (unlikely(error))
2293 goto out_filp;
2294 error = -ELOOP;
2295 if (!(nd.flags & LOOKUP_FOLLOW)) {
2296 if (nd.inode->i_op->follow_link)
2297 goto out_path;
2298 }
2299 error = -ENOTDIR;
2300 if (nd.flags & LOOKUP_DIRECTORY) {
2301 if (!nd.inode->i_op->lookup)
2302 goto out_path;
2303 }
2304 audit_inode(pathname, nd.path.dentry);
2305 filp = finish_open(&nd, open_flag, acc_mode);
2306 return filp;
2307
2308creat:
2309 /* OK, have to create the file. Find the parent. */
2310 error = path_init_rcu(dfd, pathname,
2311 LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2312 if (error)
2313 goto out_filp;
2314 error = path_walk_rcu(pathname, &nd);
2315 path_finish_rcu(&nd);
2316 if (unlikely(error == -ECHILD || error == -ESTALE)) {
2317 /* slower, locked walk */
2318 if (error == -ESTALE) {
2319reval:
2320 flags |= LOOKUP_REVAL;
2321 }
2322 error = path_init(dfd, pathname,
2323 LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2324 if (error)
2325 goto out_filp;
2326
2327 error = path_walk_simple(pathname, &nd);
2328 }
2329 if (unlikely(error))
2330 goto out_filp;
2331 if (unlikely(!audit_dummy_context()))
Al Viro9b4a9b12009-04-07 11:44:16 -04002332 audit_inode(pathname, nd.path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 /*
Al Viroa2c36b42009-12-24 03:39:50 -05002335 * We have the parent and last component.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 */
Nick Piggin31e6b012011-01-07 17:49:52 +11002337 nd.flags = flags;
Al Viro3e297b62010-03-26 12:40:13 -04002338 filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
Al Viro806b6812009-12-26 07:16:40 -05002339 while (unlikely(!filp)) { /* trailing symlink */
Al Virodef4af32009-12-26 08:37:05 -05002340 struct path holder;
Al Virodef4af32009-12-26 08:37:05 -05002341 void *cookie;
Al Viro806b6812009-12-26 07:16:40 -05002342 error = -ELOOP;
Al Viro1f36f772009-12-26 10:56:19 -05002343 /* S_ISDIR part is a temporary automount kludge */
Nick Piggin31e6b012011-01-07 17:49:52 +11002344 if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(nd.inode->i_mode))
Al Viro1f36f772009-12-26 10:56:19 -05002345 goto exit_dput;
2346 if (count++ == 32)
Al Viro806b6812009-12-26 07:16:40 -05002347 goto exit_dput;
2348 /*
2349 * This is subtle. Instead of calling do_follow_link() we do
2350 * the thing by hands. The reason is that this way we have zero
2351 * link_count and path_walk() (called from ->follow_link)
2352 * honoring LOOKUP_PARENT. After that we have the parent and
2353 * last component, i.e. we are in the same situation as after
2354 * the first path_walk(). Well, almost - if the last component
2355 * is normal we get its copy stored in nd->last.name and we will
2356 * have to putname() it when we are done. Procfs-like symlinks
2357 * just set LAST_BIND.
2358 */
2359 nd.flags |= LOOKUP_PARENT;
2360 error = security_inode_follow_link(path.dentry, &nd);
2361 if (error)
2362 goto exit_dput;
Al Virodef4af32009-12-26 08:37:05 -05002363 error = __do_follow_link(&path, &nd, &cookie);
2364 if (unlikely(error)) {
Nick Piggin31e6b012011-01-07 17:49:52 +11002365 if (!IS_ERR(cookie) && nd.inode->i_op->put_link)
2366 nd.inode->i_op->put_link(path.dentry, &nd, cookie);
Al Viro806b6812009-12-26 07:16:40 -05002367 /* nd.path had been dropped */
Nick Piggin31e6b012011-01-07 17:49:52 +11002368 nd.path = path;
2369 goto out_path;
Al Viro806b6812009-12-26 07:16:40 -05002370 }
Al Virodef4af32009-12-26 08:37:05 -05002371 holder = path;
Al Viro806b6812009-12-26 07:16:40 -05002372 nd.flags &= ~LOOKUP_PARENT;
Al Viro3e297b62010-03-26 12:40:13 -04002373 filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
Nick Piggin31e6b012011-01-07 17:49:52 +11002374 if (nd.inode->i_op->put_link)
2375 nd.inode->i_op->put_link(holder.dentry, &nd, cookie);
Al Virodef4af32009-12-26 08:37:05 -05002376 path_put(&holder);
Al Viro806b6812009-12-26 07:16:40 -05002377 }
Al Viro10fa8e62009-12-26 07:09:49 -05002378out:
Al Viro2a737872009-04-07 11:49:53 -04002379 if (nd.root.mnt)
2380 path_put(&nd.root);
Nick Piggin31e6b012011-01-07 17:49:52 +11002381 if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
Al Viro10fa8e62009-12-26 07:09:49 -05002382 goto reval;
Al Viro10fa8e62009-12-26 07:09:49 -05002383 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Al Viro806b6812009-12-26 07:16:40 -05002385exit_dput:
2386 path_put_conditional(&path, &nd);
Nick Piggin31e6b012011-01-07 17:49:52 +11002387out_path:
2388 path_put(&nd.path);
2389out_filp:
Al Viro806b6812009-12-26 07:16:40 -05002390 if (!IS_ERR(nd.intent.open.file))
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002391 release_open_intent(&nd);
Al Viro806b6812009-12-26 07:16:40 -05002392 filp = ERR_PTR(error);
Al Viro10fa8e62009-12-26 07:09:49 -05002393 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
2396/**
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002397 * filp_open - open file and return file pointer
2398 *
2399 * @filename: path to open
2400 * @flags: open flags as per the open(2) second argument
2401 * @mode: mode for the new file if O_CREAT is set, else ignored
2402 *
2403 * This is the helper to open a file from kernelspace if you really
2404 * have to. But in generally you should not do this, so please move
2405 * along, nothing to see here..
2406 */
2407struct file *filp_open(const char *filename, int flags, int mode)
2408{
Al Viro6e8341a2009-04-06 11:16:22 -04002409 return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08002410}
2411EXPORT_SYMBOL(filp_open);
2412
2413/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * lookup_create - lookup a dentry, creating it if it doesn't exist
2415 * @nd: nameidata info
2416 * @is_dir: directory flag
2417 *
2418 * Simple function to lookup and return a dentry and create it
2419 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002420 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002421 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 */
2423struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2424{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002425 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Jan Blunck4ac91372008-02-14 19:34:32 -08002427 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002428 /*
2429 * Yucky last component or no last component at all?
2430 * (foo/., foo/.., /////)
2431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (nd->last_type != LAST_NORM)
2433 goto fail;
2434 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04002435 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002436 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002437
2438 /*
2439 * Do the final lookup.
2440 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002441 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (IS_ERR(dentry))
2443 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002444
Al Viroe9baf6e2008-05-15 04:49:12 -04002445 if (dentry->d_inode)
2446 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002447 /*
2448 * Special case - lookup gave negative, but... we had foo/bar/
2449 * From the vfs_mknod() POV we just have a negative dentry -
2450 * all is fine. Let's be bastards - you had / on the end, you've
2451 * been asking for (non-existent) directory. -ENOENT for you.
2452 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002453 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2454 dput(dentry);
2455 dentry = ERR_PTR(-ENOENT);
2456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002458eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002460 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461fail:
2462 return dentry;
2463}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002464EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2467{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002468 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 if (error)
2471 return error;
2472
2473 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2474 return -EPERM;
2475
Al Viroacfa4382008-12-04 10:06:33 -05002476 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return -EPERM;
2478
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002479 error = devcgroup_inode_mknod(mode, dev);
2480 if (error)
2481 return error;
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 error = security_inode_mknod(dir, dentry, mode, dev);
2484 if (error)
2485 return error;
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002488 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002489 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return error;
2491}
2492
Dave Hansen463c3192008-02-15 14:37:57 -08002493static int may_mknod(mode_t mode)
2494{
2495 switch (mode & S_IFMT) {
2496 case S_IFREG:
2497 case S_IFCHR:
2498 case S_IFBLK:
2499 case S_IFIFO:
2500 case S_IFSOCK:
2501 case 0: /* zero mode translates to S_IFREG */
2502 return 0;
2503 case S_IFDIR:
2504 return -EPERM;
2505 default:
2506 return -EINVAL;
2507 }
2508}
2509
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002510SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2511 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Al Viro2ad94ae2008-07-21 09:32:51 -04002513 int error;
2514 char *tmp;
2515 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct nameidata nd;
2517
2518 if (S_ISDIR(mode))
2519 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Al Viro2ad94ae2008-07-21 09:32:51 -04002521 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002523 return error;
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002526 if (IS_ERR(dentry)) {
2527 error = PTR_ERR(dentry);
2528 goto out_unlock;
2529 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002530 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002531 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002532 error = may_mknod(mode);
2533 if (error)
2534 goto out_dput;
2535 error = mnt_want_write(nd.path.mnt);
2536 if (error)
2537 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002538 error = security_path_mknod(&nd.path, dentry, mode, dev);
2539 if (error)
2540 goto out_drop_write;
Dave Hansen463c3192008-02-15 14:37:57 -08002541 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002543 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 break;
2545 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002546 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 new_decode_dev(dev));
2548 break;
2549 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002550 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002553out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002554 mnt_drop_write(nd.path.mnt);
2555out_dput:
2556 dput(dentry);
2557out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002558 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002559 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 putname(tmp);
2561
2562 return error;
2563}
2564
Heiko Carstens3480b252009-01-14 14:14:16 +01002565SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002566{
2567 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2568}
2569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2571{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002572 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 if (error)
2575 return error;
2576
Al Viroacfa4382008-12-04 10:06:33 -05002577 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return -EPERM;
2579
2580 mode &= (S_IRWXUGO|S_ISVTX);
2581 error = security_inode_mkdir(dir, dentry, mode);
2582 if (error)
2583 return error;
2584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002586 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002587 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return error;
2589}
2590
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002591SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 int error = 0;
2594 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002595 struct dentry *dentry;
2596 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Al Viro2ad94ae2008-07-21 09:32:51 -04002598 error = user_path_parent(dfd, pathname, &nd, &tmp);
2599 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002600 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Dave Hansen6902d922006-09-30 23:29:01 -07002602 dentry = lookup_create(&nd, 1);
2603 error = PTR_ERR(dentry);
2604 if (IS_ERR(dentry))
2605 goto out_unlock;
2606
Jan Blunck4ac91372008-02-14 19:34:32 -08002607 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04002608 mode &= ~current_umask();
Dave Hansen463c3192008-02-15 14:37:57 -08002609 error = mnt_want_write(nd.path.mnt);
2610 if (error)
2611 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002612 error = security_path_mkdir(&nd.path, dentry, mode);
2613 if (error)
2614 goto out_drop_write;
Jan Blunck4ac91372008-02-14 19:34:32 -08002615 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002616out_drop_write:
Dave Hansen463c3192008-02-15 14:37:57 -08002617 mnt_drop_write(nd.path.mnt);
2618out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002619 dput(dentry);
2620out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002621 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002622 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002623 putname(tmp);
2624out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return error;
2626}
2627
Heiko Carstens3cdad422009-01-14 14:14:22 +01002628SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002629{
2630 return sys_mkdirat(AT_FDCWD, pathname, mode);
2631}
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/*
2634 * We try to drop the dentry early: we should have
2635 * a usage count of 2 if we're the only user of this
2636 * dentry, and if that is true (possibly after pruning
2637 * the dcache), then we drop the dentry now.
2638 *
2639 * A low-level filesystem can, if it choses, legally
2640 * do a
2641 *
2642 * if (!d_unhashed(dentry))
2643 * return -EBUSY;
2644 *
2645 * if it cannot handle the case of removing a directory
2646 * that is still in use by something else..
2647 */
2648void dentry_unhash(struct dentry *dentry)
2649{
2650 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002651 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002653 if (dentry->d_count == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 __d_drop(dentry);
2655 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2659{
2660 int error = may_delete(dir, dentry, 1);
2661
2662 if (error)
2663 return error;
2664
Al Viroacfa4382008-12-04 10:06:33 -05002665 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return -EPERM;
2667
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002668 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 dentry_unhash(dentry);
2670 if (d_mountpoint(dentry))
2671 error = -EBUSY;
2672 else {
2673 error = security_inode_rmdir(dir, dentry);
2674 if (!error) {
2675 error = dir->i_op->rmdir(dir, dentry);
Al Virod83c49f2010-04-30 17:17:09 -04002676 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 dentry->d_inode->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04002678 dont_mount(dentry);
2679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
2681 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002682 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 d_delete(dentry);
2685 }
2686 dput(dentry);
2687
2688 return error;
2689}
2690
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002691static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 int error = 0;
2694 char * name;
2695 struct dentry *dentry;
2696 struct nameidata nd;
2697
Al Viro2ad94ae2008-07-21 09:32:51 -04002698 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002700 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002703 case LAST_DOTDOT:
2704 error = -ENOTEMPTY;
2705 goto exit1;
2706 case LAST_DOT:
2707 error = -EINVAL;
2708 goto exit1;
2709 case LAST_ROOT:
2710 error = -EBUSY;
2711 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002713
2714 nd.flags &= ~LOOKUP_PARENT;
2715
Jan Blunck4ac91372008-02-14 19:34:32 -08002716 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002717 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002719 if (IS_ERR(dentry))
2720 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002721 error = mnt_want_write(nd.path.mnt);
2722 if (error)
2723 goto exit3;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002724 error = security_path_rmdir(&nd.path, dentry);
2725 if (error)
2726 goto exit4;
Jan Blunck4ac91372008-02-14 19:34:32 -08002727 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002728exit4:
Dave Hansen06227532008-02-15 14:37:34 -08002729 mnt_drop_write(nd.path.mnt);
2730exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002731 dput(dentry);
2732exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002733 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002735 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 putname(name);
2737 return error;
2738}
2739
Heiko Carstens3cdad422009-01-14 14:14:22 +01002740SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002741{
2742 return do_rmdir(AT_FDCWD, pathname);
2743}
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745int vfs_unlink(struct inode *dir, struct dentry *dentry)
2746{
2747 int error = may_delete(dir, dentry, 0);
2748
2749 if (error)
2750 return error;
2751
Al Viroacfa4382008-12-04 10:06:33 -05002752 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return -EPERM;
2754
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002755 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (d_mountpoint(dentry))
2757 error = -EBUSY;
2758 else {
2759 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002760 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 error = dir->i_op->unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05002762 if (!error)
Al Virod83c49f2010-04-30 17:17:09 -04002763 dont_mount(dentry);
Al Virobec10522010-03-03 14:12:08 -05002764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002766 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2769 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002770 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
Robert Love0eeca282005-07-12 17:06:03 -04002773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return error;
2775}
2776
2777/*
2778 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002779 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 * writeout happening, and we don't want to prevent access to the directory
2781 * while waiting on the I/O.
2782 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002783static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
Al Viro2ad94ae2008-07-21 09:32:51 -04002785 int error;
2786 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 struct dentry *dentry;
2788 struct nameidata nd;
2789 struct inode *inode = NULL;
2790
Al Viro2ad94ae2008-07-21 09:32:51 -04002791 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002793 return error;
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 error = -EISDIR;
2796 if (nd.last_type != LAST_NORM)
2797 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002798
2799 nd.flags &= ~LOOKUP_PARENT;
2800
Jan Blunck4ac91372008-02-14 19:34:32 -08002801 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002802 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 error = PTR_ERR(dentry);
2804 if (!IS_ERR(dentry)) {
2805 /* Why not before? Because we want correct error value */
2806 if (nd.last.name[nd.last.len])
2807 goto slashes;
2808 inode = dentry->d_inode;
2809 if (inode)
Al Viro7de9c6e2010-10-23 11:11:40 -04002810 ihold(inode);
Dave Hansen06227532008-02-15 14:37:34 -08002811 error = mnt_want_write(nd.path.mnt);
2812 if (error)
2813 goto exit2;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002814 error = security_path_unlink(&nd.path, dentry);
2815 if (error)
2816 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002817 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002818exit3:
Dave Hansen06227532008-02-15 14:37:34 -08002819 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 exit2:
2821 dput(dentry);
2822 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002823 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if (inode)
2825 iput(inode); /* truncate the inode here */
2826exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002827 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 putname(name);
2829 return error;
2830
2831slashes:
2832 error = !dentry->d_inode ? -ENOENT :
2833 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2834 goto exit2;
2835}
2836
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002837SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002838{
2839 if ((flag & ~AT_REMOVEDIR) != 0)
2840 return -EINVAL;
2841
2842 if (flag & AT_REMOVEDIR)
2843 return do_rmdir(dfd, pathname);
2844
2845 return do_unlinkat(dfd, pathname);
2846}
2847
Heiko Carstens3480b252009-01-14 14:14:16 +01002848SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002849{
2850 return do_unlinkat(AT_FDCWD, pathname);
2851}
2852
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002853int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002855 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 if (error)
2858 return error;
2859
Al Viroacfa4382008-12-04 10:06:33 -05002860 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return -EPERM;
2862
2863 error = security_inode_symlink(dir, dentry, oldname);
2864 if (error)
2865 return error;
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002868 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002869 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return error;
2871}
2872
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002873SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2874 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Al Viro2ad94ae2008-07-21 09:32:51 -04002876 int error;
2877 char *from;
2878 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002879 struct dentry *dentry;
2880 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002883 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002885
2886 error = user_path_parent(newdfd, newname, &nd, &to);
2887 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002888 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Dave Hansen6902d922006-09-30 23:29:01 -07002890 dentry = lookup_create(&nd, 0);
2891 error = PTR_ERR(dentry);
2892 if (IS_ERR(dentry))
2893 goto out_unlock;
2894
Dave Hansen75c3f292008-02-15 14:37:45 -08002895 error = mnt_want_write(nd.path.mnt);
2896 if (error)
2897 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002898 error = security_path_symlink(&nd.path, dentry, from);
2899 if (error)
2900 goto out_drop_write;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002901 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002902out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08002903 mnt_drop_write(nd.path.mnt);
2904out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002905 dput(dentry);
2906out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002907 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002908 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002909 putname(to);
2910out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 putname(from);
2912 return error;
2913}
2914
Heiko Carstens3480b252009-01-14 14:14:16 +01002915SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002916{
2917 return sys_symlinkat(oldname, AT_FDCWD, newname);
2918}
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2921{
2922 struct inode *inode = old_dentry->d_inode;
2923 int error;
2924
2925 if (!inode)
2926 return -ENOENT;
2927
Miklos Szeredia95164d2008-07-30 15:08:48 +02002928 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (error)
2930 return error;
2931
2932 if (dir->i_sb != inode->i_sb)
2933 return -EXDEV;
2934
2935 /*
2936 * A link to an append-only or immutable file cannot be created.
2937 */
2938 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2939 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05002940 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002942 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return -EPERM;
2944
2945 error = security_inode_link(old_dentry, dir, new_dentry);
2946 if (error)
2947 return error;
2948
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002949 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002951 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002952 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002953 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return error;
2955}
2956
2957/*
2958 * Hardlinks are often used in delicate situations. We avoid
2959 * security-related surprises by not following symlinks on the
2960 * newname. --KAB
2961 *
2962 * We don't follow them on the oldname either to be compatible
2963 * with linux 2.0, and to avoid hard-linking to directories
2964 * and other special files. --ADM
2965 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01002966SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2967 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002970 struct nameidata nd;
2971 struct path old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002973 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002975 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002976 return -EINVAL;
2977
Al Viro2d8f3032008-07-22 09:59:21 -04002978 error = user_path_at(olddfd, oldname,
2979 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2980 &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002982 return error;
2983
2984 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (error)
2986 goto out;
2987 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002988 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 goto out_release;
2990 new_dentry = lookup_create(&nd, 0);
2991 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002992 if (IS_ERR(new_dentry))
2993 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002994 error = mnt_want_write(nd.path.mnt);
2995 if (error)
2996 goto out_dput;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09002997 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2998 if (error)
2999 goto out_drop_write;
Al Viro2d8f3032008-07-22 09:59:21 -04003000 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003001out_drop_write:
Dave Hansen75c3f292008-02-15 14:37:45 -08003002 mnt_drop_write(nd.path.mnt);
3003out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07003004 dput(new_dentry);
3005out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08003006 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08003008 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003009 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010out:
Al Viro2d8f3032008-07-22 09:59:21 -04003011 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 return error;
3014}
3015
Heiko Carstens3480b252009-01-14 14:14:16 +01003016SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003017{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08003018 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003019}
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021/*
3022 * The worst of all namespace operations - renaming directory. "Perverted"
3023 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3024 * Problems:
3025 * a) we can get into loop creation. Check is done in is_subdir().
3026 * b) race potential - two innocent renames can create a loop together.
3027 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003028 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 * story.
3030 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003031 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 * whether the target exists). Solution: try to be smart with locking
3033 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003034 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 * move will be locked. Thus we can rank directories by the tree
3036 * (ancestors first) and rank all non-directories after them.
3037 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08003038 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 * HOWEVER, it relies on the assumption that any object with ->lookup()
3040 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3041 * we'd better make sure that there's no link(2) for them.
3042 * d) some filesystems don't support opened-but-unlinked directories,
3043 * either because of layout or because they are not ready to deal with
3044 * all cases correctly. The latter will be fixed (taking this sort of
3045 * stuff into VFS), but the former is not going away. Solution: the same
3046 * trick as in rmdir().
3047 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003048 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05003050 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 * locking].
3052 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07003053static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3054 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 int error = 0;
3057 struct inode *target;
3058
3059 /*
3060 * If we are going to change the parent - check write permissions,
3061 * we'll need to flip '..'.
3062 */
3063 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04003064 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (error)
3066 return error;
3067 }
3068
3069 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3070 if (error)
3071 return error;
3072
3073 target = new_dentry->d_inode;
Al Virod83c49f2010-04-30 17:17:09 -04003074 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003075 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3077 error = -EBUSY;
Al Virod83c49f2010-04-30 17:17:09 -04003078 else {
3079 if (target)
3080 dentry_unhash(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
Al Virod83c49f2010-04-30 17:17:09 -04003082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (target) {
Al Virod83c49f2010-04-30 17:17:09 -04003084 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 target->i_flags |= S_DEAD;
Al Virod83c49f2010-04-30 17:17:09 -04003086 dont_mount(new_dentry);
3087 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003088 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 if (d_unhashed(new_dentry))
3090 d_rehash(new_dentry);
3091 dput(new_dentry);
3092 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07003093 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07003094 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3095 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return error;
3097}
3098
Adrian Bunk75c96f82005-05-05 16:16:09 -07003099static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3100 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
3102 struct inode *target;
3103 int error;
3104
3105 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3106 if (error)
3107 return error;
3108
3109 dget(new_dentry);
3110 target = new_dentry->d_inode;
3111 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003112 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3114 error = -EBUSY;
3115 else
3116 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3117 if (!error) {
Al Virobec10522010-03-03 14:12:08 -05003118 if (target)
Al Virod83c49f2010-04-30 17:17:09 -04003119 dont_mount(new_dentry);
Mark Fasheh349457c2006-09-08 14:22:21 -07003120 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003124 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 dput(new_dentry);
3126 return error;
3127}
3128
3129int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3130 struct inode *new_dir, struct dentry *new_dentry)
3131{
3132 int error;
3133 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Eric Paris59b0df22010-02-08 12:53:52 -05003134 const unsigned char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 if (old_dentry->d_inode == new_dentry->d_inode)
3137 return 0;
3138
3139 error = may_delete(old_dir, old_dentry, is_dir);
3140 if (error)
3141 return error;
3142
3143 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02003144 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 else
3146 error = may_delete(new_dir, new_dentry, is_dir);
3147 if (error)
3148 return error;
3149
Al Viroacfa4382008-12-04 10:06:33 -05003150 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return -EPERM;
3152
Robert Love0eeca282005-07-12 17:06:03 -04003153 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (is_dir)
3156 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3157 else
3158 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
Al Viro123df292009-12-25 04:57:57 -05003159 if (!error)
3160 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04003161 new_dentry->d_inode, old_dentry);
Robert Love0eeca282005-07-12 17:06:03 -04003162 fsnotify_oldname_free(old_name);
3163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return error;
3165}
3166
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003167SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3168 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Al Viro2ad94ae2008-07-21 09:32:51 -04003170 struct dentry *old_dir, *new_dir;
3171 struct dentry *old_dentry, *new_dentry;
3172 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04003174 char *from;
3175 char *to;
3176 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Al Viro2ad94ae2008-07-21 09:32:51 -04003178 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (error)
3180 goto exit;
3181
Al Viro2ad94ae2008-07-21 09:32:51 -04003182 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (error)
3184 goto exit1;
3185
3186 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08003187 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 goto exit2;
3189
Jan Blunck4ac91372008-02-14 19:34:32 -08003190 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 error = -EBUSY;
3192 if (oldnd.last_type != LAST_NORM)
3193 goto exit2;
3194
Jan Blunck4ac91372008-02-14 19:34:32 -08003195 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 if (newnd.last_type != LAST_NORM)
3197 goto exit2;
3198
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003199 oldnd.flags &= ~LOOKUP_PARENT;
3200 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09003201 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 trap = lock_rename(new_dir, old_dir);
3204
Christoph Hellwig49705b72005-11-08 21:35:06 -08003205 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 error = PTR_ERR(old_dentry);
3207 if (IS_ERR(old_dentry))
3208 goto exit3;
3209 /* source must exist */
3210 error = -ENOENT;
3211 if (!old_dentry->d_inode)
3212 goto exit4;
3213 /* unless the source is a directory trailing slashes give -ENOTDIR */
3214 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3215 error = -ENOTDIR;
3216 if (oldnd.last.name[oldnd.last.len])
3217 goto exit4;
3218 if (newnd.last.name[newnd.last.len])
3219 goto exit4;
3220 }
3221 /* source should not be ancestor of target */
3222 error = -EINVAL;
3223 if (old_dentry == trap)
3224 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08003225 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 error = PTR_ERR(new_dentry);
3227 if (IS_ERR(new_dentry))
3228 goto exit4;
3229 /* target should not be an ancestor of source */
3230 error = -ENOTEMPTY;
3231 if (new_dentry == trap)
3232 goto exit5;
3233
Dave Hansen9079b1e2008-02-15 14:37:49 -08003234 error = mnt_want_write(oldnd.path.mnt);
3235 if (error)
3236 goto exit5;
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003237 error = security_path_rename(&oldnd.path, old_dentry,
3238 &newnd.path, new_dentry);
3239 if (error)
3240 goto exit6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 error = vfs_rename(old_dir->d_inode, old_dentry,
3242 new_dir->d_inode, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003243exit6:
Dave Hansen9079b1e2008-02-15 14:37:49 -08003244 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245exit5:
3246 dput(new_dentry);
3247exit4:
3248 dput(old_dentry);
3249exit3:
3250 unlock_rename(new_dir, old_dir);
3251exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08003252 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04003253 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08003255 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04003257exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return error;
3259}
3260
Heiko Carstensa26eab22009-01-14 14:14:17 +01003261SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003262{
3263 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3264}
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3267{
3268 int len;
3269
3270 len = PTR_ERR(link);
3271 if (IS_ERR(link))
3272 goto out;
3273
3274 len = strlen(link);
3275 if (len > (unsigned) buflen)
3276 len = buflen;
3277 if (copy_to_user(buffer, link, len))
3278 len = -EFAULT;
3279out:
3280 return len;
3281}
3282
3283/*
3284 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3285 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3286 * using) it for any given inode is up to filesystem.
3287 */
3288int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3289{
3290 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003291 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07003292 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003295 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07003296 if (IS_ERR(cookie))
3297 return PTR_ERR(cookie);
3298
3299 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3300 if (dentry->d_inode->i_op->put_link)
3301 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3302 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
3305int vfs_follow_link(struct nameidata *nd, const char *link)
3306{
3307 return __vfs_follow_link(nd, link);
3308}
3309
3310/* get the link contents into pagecache */
3311static char *page_getlink(struct dentry * dentry, struct page **ppage)
3312{
Duane Griffinebd09ab2008-12-19 20:47:12 +00003313 char *kaddr;
3314 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07003316 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07003318 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 *ppage = page;
Duane Griffinebd09ab2008-12-19 20:47:12 +00003320 kaddr = kmap(page);
3321 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3322 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
3325int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3326{
3327 struct page *page = NULL;
3328 char *s = page_getlink(dentry, &page);
3329 int res = vfs_readlink(dentry,buffer,buflen,s);
3330 if (page) {
3331 kunmap(page);
3332 page_cache_release(page);
3333 }
3334 return res;
3335}
3336
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003337void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003339 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003341 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003344void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07003346 struct page *page = cookie;
3347
3348 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 kunmap(page);
3350 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
3352}
3353
Nick Piggin54566b22009-01-04 12:00:53 -08003354/*
3355 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3356 */
3357int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
3359 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003360 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07003361 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08003362 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 char *kaddr;
Nick Piggin54566b22009-01-04 12:00:53 -08003364 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3365 if (nofs)
3366 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
NeilBrown7e53cac2006-03-25 03:07:57 -08003368retry:
Nick Pigginafddba42007-10-16 01:25:01 -07003369 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08003370 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07003372 goto fail;
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 kaddr = kmap_atomic(page, KM_USER0);
3375 memcpy(kaddr, symname, len-1);
3376 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07003377
3378 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3379 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (err < 0)
3381 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07003382 if (err < len-1)
3383 goto retry;
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 mark_inode_dirty(inode);
3386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387fail:
3388 return err;
3389}
3390
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003391int page_symlink(struct inode *inode, const char *symname, int len)
3392{
3393 return __page_symlink(inode, symname, len,
Nick Piggin54566b22009-01-04 12:00:53 -08003394 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003395}
3396
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003397const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 .readlink = generic_readlink,
3399 .follow_link = page_follow_link_light,
3400 .put_link = page_put_link,
3401};
3402
Al Viro2d8f3032008-07-22 09:59:21 -04003403EXPORT_SYMBOL(user_path_at);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404EXPORT_SYMBOL(follow_down);
3405EXPORT_SYMBOL(follow_up);
3406EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3407EXPORT_SYMBOL(getname);
3408EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409EXPORT_SYMBOL(lookup_one_len);
3410EXPORT_SYMBOL(page_follow_link_light);
3411EXPORT_SYMBOL(page_put_link);
3412EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08003413EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414EXPORT_SYMBOL(page_symlink);
3415EXPORT_SYMBOL(page_symlink_inode_operations);
3416EXPORT_SYMBOL(path_lookup);
Al Virod1811462008-08-02 00:49:18 -04003417EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003418EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04003419EXPORT_SYMBOL(inode_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003420EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421EXPORT_SYMBOL(unlock_rename);
3422EXPORT_SYMBOL(vfs_create);
3423EXPORT_SYMBOL(vfs_follow_link);
3424EXPORT_SYMBOL(vfs_link);
3425EXPORT_SYMBOL(vfs_mkdir);
3426EXPORT_SYMBOL(vfs_mknod);
3427EXPORT_SYMBOL(generic_permission);
3428EXPORT_SYMBOL(vfs_readlink);
3429EXPORT_SYMBOL(vfs_rename);
3430EXPORT_SYMBOL(vfs_rmdir);
3431EXPORT_SYMBOL(vfs_symlink);
3432EXPORT_SYMBOL(vfs_unlink);
3433EXPORT_SYMBOL(dentry_unhash);
3434EXPORT_SYMBOL(generic_readlink);