blob: bab0d19a2e3651db9c70860d77b76f54fe1829da [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>
22#include <linux/quotaops.h>
23#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
27#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>
33#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/namei.h>
35#include <asm/uaccess.h>
36
37#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
38
39/* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
44 *
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
51 *
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
55 *
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
58 *
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
65 */
66
67/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existant name.
74 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
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
Josef 'Jeff' Sipekc4a78082007-07-19 01:48:22 -0700110static int fastcall link_path_walk(const char *name, struct nameidata *nd);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
115 *
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
118 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800119static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 int retval;
122 unsigned long len = PATH_MAX;
123
124 if (!segment_eq(get_fs(), KERNEL_DS)) {
125 if ((unsigned long) filename >= TASK_SIZE)
126 return -EFAULT;
127 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
128 len = TASK_SIZE - (unsigned long) filename;
129 }
130
131 retval = strncpy_from_user(page, filename, len);
132 if (retval > 0) {
133 if (retval < len)
134 return 0;
135 return -ENAMETOOLONG;
136 } else if (!retval)
137 retval = -ENOENT;
138 return retval;
139}
140
141char * getname(const char __user * filename)
142{
143 char *tmp, *result;
144
145 result = ERR_PTR(-ENOMEM);
146 tmp = __getname();
147 if (tmp) {
148 int retval = do_getname(filename, tmp);
149
150 result = tmp;
151 if (retval < 0) {
152 __putname(tmp);
153 result = ERR_PTR(retval);
154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
160#ifdef CONFIG_AUDITSYSCALL
161void putname(const char *name)
162{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400163 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 audit_putname(name);
165 else
166 __putname(name);
167}
168EXPORT_SYMBOL(putname);
169#endif
170
171
172/**
173 * generic_permission - check for access rights on a Posix-like filesystem
174 * @inode: inode to check access rights for
175 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
176 * @check_acl: optional callback to check for Posix ACLs
177 *
178 * Used to check for read/write/execute permissions on a file.
179 * We use "fsuid" for this, letting us set arbitrary permissions
180 * for filesystem access without changing the "normal" uids which
181 * are used for other things..
182 */
183int generic_permission(struct inode *inode, int mask,
184 int (*check_acl)(struct inode *inode, int mask))
185{
186 umode_t mode = inode->i_mode;
187
188 if (current->fsuid == inode->i_uid)
189 mode >>= 6;
190 else {
191 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
192 int error = check_acl(inode, mask);
193 if (error == -EACCES)
194 goto check_capabilities;
195 else if (error != -EAGAIN)
196 return error;
197 }
198
199 if (in_group_p(inode->i_gid))
200 mode >>= 3;
201 }
202
203 /*
204 * If the DACs are ok we don't need any capability check.
205 */
206 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
207 return 0;
208
209 check_capabilities:
210 /*
211 * Read/write DACs are always overridable.
212 * Executable DACs are overridable if at least one exec bit is set.
213 */
214 if (!(mask & MAY_EXEC) ||
215 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
216 if (capable(CAP_DAC_OVERRIDE))
217 return 0;
218
219 /*
220 * Searching includes executable on directories, else just read.
221 */
222 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
223 if (capable(CAP_DAC_READ_SEARCH))
224 return 0;
225
226 return -EACCES;
227}
228
229int permission(struct inode *inode, int mask, struct nameidata *nd)
230{
Trond Myklebusta343bb72006-08-22 20:06:03 -0400231 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int retval, submask;
233
234 if (mask & MAY_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * Nobody gets write access to a read-only fs.
238 */
239 if (IS_RDONLY(inode) &&
240 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
241 return -EROFS;
242
243 /*
244 * Nobody gets write access to an immutable file.
245 */
246 if (IS_IMMUTABLE(inode))
247 return -EACCES;
248 }
249
250
Trond Myklebusta343bb72006-08-22 20:06:03 -0400251 /*
252 * MAY_EXEC on regular files requires special handling: We override
Stas Sergeev317a40a2006-12-06 20:35:25 -0800253 * filesystem execute permissions if the mode bits aren't set or
254 * the fs is mounted with the "noexec" flag.
Trond Myklebusta343bb72006-08-22 20:06:03 -0400255 */
Stas Sergeev317a40a2006-12-06 20:35:25 -0800256 if ((mask & MAY_EXEC) && S_ISREG(mode) && (!(mode & S_IXUGO) ||
257 (nd && nd->mnt && (nd->mnt->mnt_flags & MNT_NOEXEC))))
Trond Myklebusta343bb72006-08-22 20:06:03 -0400258 return -EACCES;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Ordinary permission routines do not understand MAY_APPEND. */
261 submask = mask & ~MAY_APPEND;
262 if (inode->i_op && inode->i_op->permission)
263 retval = inode->i_op->permission(inode, submask, nd);
264 else
265 retval = generic_permission(inode, submask, NULL);
266 if (retval)
267 return retval;
268
269 return security_inode_permission(inode, mask, nd);
270}
271
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800272/**
273 * vfs_permission - check for access rights to a given path
274 * @nd: lookup result that describes the path
275 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
276 *
277 * Used to check for read/write/execute permissions on a path.
278 * We use "fsuid" for this, letting us set arbitrary permissions
279 * for filesystem access without changing the "normal" uids which
280 * are used for other things.
281 */
282int vfs_permission(struct nameidata *nd, int mask)
283{
284 return permission(nd->dentry->d_inode, mask, nd);
285}
286
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800287/**
288 * file_permission - check for additional access rights to a given file
289 * @file: file to check access rights for
290 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
291 *
292 * Used to check for read/write/execute permissions on an already opened
293 * file.
294 *
295 * Note:
296 * Do not use this function in new code. All access checks should
297 * be done using vfs_permission().
298 */
299int file_permission(struct file *file, int mask)
300{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800301 return permission(file->f_path.dentry->d_inode, mask, NULL);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/*
305 * get_write_access() gets write permission for a file.
306 * put_write_access() releases this write permission.
307 * This is used for regular files.
308 * We cannot support write (and maybe mmap read-write shared) accesses and
309 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
310 * can have the following values:
311 * 0: no writers, no VM_DENYWRITE mappings
312 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
313 * > 0: (i_writecount) users are writing to the file.
314 *
315 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
316 * except for the cases where we don't hold i_writecount yet. Then we need to
317 * use {get,deny}_write_access() - these functions check the sign and refuse
318 * to do the change if sign is wrong. Exclusion between them is provided by
319 * the inode->i_lock spinlock.
320 */
321
322int get_write_access(struct inode * inode)
323{
324 spin_lock(&inode->i_lock);
325 if (atomic_read(&inode->i_writecount) < 0) {
326 spin_unlock(&inode->i_lock);
327 return -ETXTBSY;
328 }
329 atomic_inc(&inode->i_writecount);
330 spin_unlock(&inode->i_lock);
331
332 return 0;
333}
334
335int deny_write_access(struct file * file)
336{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800337 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 spin_lock(&inode->i_lock);
340 if (atomic_read(&inode->i_writecount) > 0) {
341 spin_unlock(&inode->i_lock);
342 return -ETXTBSY;
343 }
344 atomic_dec(&inode->i_writecount);
345 spin_unlock(&inode->i_lock);
346
347 return 0;
348}
349
350void path_release(struct nameidata *nd)
351{
352 dput(nd->dentry);
353 mntput(nd->mnt);
354}
355
356/*
357 * umount() mustn't call path_release()/mntput() as that would clear
358 * mnt_expiry_mark
359 */
360void path_release_on_umount(struct nameidata *nd)
361{
362 dput(nd->dentry);
Miklos Szeredi751c4042005-07-07 17:57:30 -0700363 mntput_no_expire(nd->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Trond Myklebust834f2a42005-10-18 14:20:16 -0700366/**
367 * release_open_intent - free up open intent resources
368 * @nd: pointer to nameidata
369 */
370void release_open_intent(struct nameidata *nd)
371{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800372 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700373 put_filp(nd->intent.open.file);
374 else
375 fput(nd->intent.open.file);
376}
377
Ian Kentbcdc5e02006-09-27 01:50:44 -0700378static inline struct dentry *
379do_revalidate(struct dentry *dentry, struct nameidata *nd)
380{
381 int status = dentry->d_op->d_revalidate(dentry, nd);
382 if (unlikely(status <= 0)) {
383 /*
384 * The dentry failed validation.
385 * If d_revalidate returned 0 attempt to invalidate
386 * the dentry otherwise d_revalidate is asking us
387 * to return a fail status.
388 */
389 if (!status) {
390 if (!d_invalidate(dentry)) {
391 dput(dentry);
392 dentry = NULL;
393 }
394 } else {
395 dput(dentry);
396 dentry = ERR_PTR(status);
397 }
398 }
399 return dentry;
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/*
403 * Internal lookup() using the new generic dcache.
404 * SMP-safe
405 */
406static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
407{
408 struct dentry * dentry = __d_lookup(parent, name);
409
410 /* lockess __d_lookup may fail due to concurrent d_move()
411 * in some unrelated directory, so try with d_lookup
412 */
413 if (!dentry)
414 dentry = d_lookup(parent, name);
415
Ian Kentbcdc5e02006-09-27 01:50:44 -0700416 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
417 dentry = do_revalidate(dentry, nd);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return dentry;
420}
421
422/*
423 * Short-cut version of permission(), for calling by
424 * path_walk(), when dcache lock is held. Combines parts
425 * of permission() and generic_permission(), and tests ONLY for
426 * MAY_EXEC permission.
427 *
428 * If appropriate, check DAC only. If not appropriate, or
429 * short-cut DAC fails, then call permission() to do more
430 * complete permission check.
431 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800432static int exec_permission_lite(struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct nameidata *nd)
434{
435 umode_t mode = inode->i_mode;
436
437 if (inode->i_op && inode->i_op->permission)
438 return -EAGAIN;
439
440 if (current->fsuid == inode->i_uid)
441 mode >>= 6;
442 else if (in_group_p(inode->i_gid))
443 mode >>= 3;
444
445 if (mode & MAY_EXEC)
446 goto ok;
447
448 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
449 goto ok;
450
451 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
452 goto ok;
453
454 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
455 goto ok;
456
457 return -EACCES;
458ok:
459 return security_inode_permission(inode, MAY_EXEC, nd);
460}
461
462/*
463 * This is called when everything else fails, and we actually have
464 * to go to the low-level filesystem to find out what we should do..
465 *
466 * We get the directory semaphore, and after getting that we also
467 * make sure that nobody added the entry to the dcache in the meantime..
468 * SMP-safe
469 */
470static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
471{
472 struct dentry * result;
473 struct inode *dir = parent->d_inode;
474
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800475 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
477 * First re-do the cached lookup just in case it was created
478 * while we waited for the directory semaphore..
479 *
480 * FIXME! This could use version numbering or similar to
481 * avoid unnecessary cache lookups.
482 *
483 * The "dcache_lock" is purely to protect the RCU list walker
484 * from concurrent renames at this point (we mustn't get false
485 * negatives from the RCU list walk here, unlike the optimistic
486 * fast walk).
487 *
488 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
489 */
490 result = d_lookup(parent, name);
491 if (!result) {
492 struct dentry * dentry = d_alloc(parent, name);
493 result = ERR_PTR(-ENOMEM);
494 if (dentry) {
495 result = dir->i_op->lookup(dir, dentry, nd);
496 if (result)
497 dput(dentry);
498 else
499 result = dentry;
500 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800501 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return result;
503 }
504
505 /*
506 * Uhhuh! Nasty case: the cache was re-populated while
507 * we waited on the semaphore. Need to revalidate.
508 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800509 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (result->d_op && result->d_op->d_revalidate) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700511 result = do_revalidate(result, nd);
512 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 return result;
516}
517
518static int __emul_lookup_dentry(const char *, struct nameidata *);
519
520/* SMP-safe */
Arjan van de Venf1662352006-01-14 13:21:31 -0800521static __always_inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522walk_init_root(const char *name, struct nameidata *nd)
523{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700524 struct fs_struct *fs = current->fs;
525
526 read_lock(&fs->lock);
527 if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
528 nd->mnt = mntget(fs->altrootmnt);
529 nd->dentry = dget(fs->altroot);
530 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (__emul_lookup_dentry(name,nd))
532 return 0;
Andreas Mohre518ddb2006-09-29 02:01:22 -0700533 read_lock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700535 nd->mnt = mntget(fs->rootmnt);
536 nd->dentry = dget(fs->root);
537 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 1;
539}
540
Arjan van de Venf1662352006-01-14 13:21:31 -0800541static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 int res = 0;
544 char *name;
545 if (IS_ERR(link))
546 goto fail;
547
548 if (*link == '/') {
549 path_release(nd);
550 if (!walk_init_root(link, nd))
551 /* weird __emul_prefix() stuff did it */
552 goto out;
553 }
554 res = link_path_walk(link, nd);
555out:
556 if (nd->depth || res || nd->last_type!=LAST_NORM)
557 return res;
558 /*
559 * If it is an iterative symlinks resolution in open_namei() we
560 * have to copy the last component. And all that crap because of
561 * bloody create() on broken symlinks. Furrfu...
562 */
563 name = __getname();
564 if (unlikely(!name)) {
565 path_release(nd);
566 return -ENOMEM;
567 }
568 strcpy(name, nd->last.name);
569 nd->last.name = name;
570 return 0;
571fail:
572 path_release(nd);
573 return PTR_ERR(link);
574}
575
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700576static inline void dput_path(struct path *path, struct nameidata *nd)
577{
578 dput(path->dentry);
579 if (path->mnt != nd->mnt)
580 mntput(path->mnt);
581}
582
583static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
584{
585 dput(nd->dentry);
586 if (nd->mnt != path->mnt)
587 mntput(nd->mnt);
588 nd->mnt = path->mnt;
589 nd->dentry = path->dentry;
590}
591
Ian Kent051d3812006-03-27 01:14:53 -0800592static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
593{
594 int error;
595 void *cookie;
596 struct dentry *dentry = path->dentry;
597
598 touch_atime(path->mnt, dentry);
599 nd_set_link(nd, NULL);
600
601 if (path->mnt != nd->mnt) {
602 path_to_nameidata(path, nd);
603 dget(dentry);
604 }
605 mntget(path->mnt);
606 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
607 error = PTR_ERR(cookie);
608 if (!IS_ERR(cookie)) {
609 char *s = nd_get_link(nd);
610 error = 0;
611 if (s)
612 error = __vfs_follow_link(nd, s);
613 if (dentry->d_inode->i_op->put_link)
614 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
615 }
616 dput(dentry);
617 mntput(path->mnt);
618
619 return error;
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/*
623 * This limits recursive symlink follows to 8, while
624 * limiting consecutive symlinks to 40.
625 *
626 * Without that kind of total limit, nasty chains of consecutive
627 * symlinks can cause almost arbitrarily long lookups.
628 */
Al Viro90ebe562005-06-06 13:35:58 -0700629static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 int err = -ELOOP;
632 if (current->link_count >= MAX_NESTED_LINKS)
633 goto loop;
634 if (current->total_link_count >= 40)
635 goto loop;
636 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
637 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700638 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (err)
640 goto loop;
641 current->link_count++;
642 current->total_link_count++;
643 nd->depth++;
Al Virocd4e91d2005-06-06 13:36:03 -0700644 err = __do_follow_link(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 current->link_count--;
646 nd->depth--;
647 return err;
648loop:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700649 dput_path(path, nd);
Al Viro839d9f92005-06-06 13:36:02 -0700650 path_release(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return err;
652}
653
654int follow_up(struct vfsmount **mnt, struct dentry **dentry)
655{
656 struct vfsmount *parent;
657 struct dentry *mountpoint;
658 spin_lock(&vfsmount_lock);
659 parent=(*mnt)->mnt_parent;
660 if (parent == *mnt) {
661 spin_unlock(&vfsmount_lock);
662 return 0;
663 }
664 mntget(parent);
665 mountpoint=dget((*mnt)->mnt_mountpoint);
666 spin_unlock(&vfsmount_lock);
667 dput(*dentry);
668 *dentry = mountpoint;
669 mntput(*mnt);
670 *mnt = parent;
671 return 1;
672}
673
674/* no need for dcache_lock, as serialization is taken care in
675 * namespace.c
676 */
Al Viro463ffb22005-06-06 13:36:05 -0700677static int __follow_mount(struct path *path)
678{
679 int res = 0;
680 while (d_mountpoint(path->dentry)) {
681 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
682 if (!mounted)
683 break;
684 dput(path->dentry);
685 if (res)
686 mntput(path->mnt);
687 path->mnt = mounted;
688 path->dentry = dget(mounted->mnt_root);
689 res = 1;
690 }
691 return res;
692}
693
Al Viro58c465e2005-06-06 13:36:13 -0700694static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 while (d_mountpoint(*dentry)) {
697 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
698 if (!mounted)
699 break;
Al Viro58c465e2005-06-06 13:36:13 -0700700 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 mntput(*mnt);
702 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707/* no need for dcache_lock, as serialization is taken care in
708 * namespace.c
709 */
Al Viroe13b2102005-06-06 13:36:06 -0700710int follow_down(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct vfsmount *mounted;
713
714 mounted = lookup_mnt(*mnt, *dentry);
715 if (mounted) {
Al Viroe13b2102005-06-06 13:36:06 -0700716 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 mntput(*mnt);
718 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 *dentry = dget(mounted->mnt_root);
720 return 1;
721 }
722 return 0;
723}
724
Arjan van de Venf1662352006-01-14 13:21:31 -0800725static __always_inline void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700727 struct fs_struct *fs = current->fs;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 while(1) {
730 struct vfsmount *parent;
Al Viro58c465e2005-06-06 13:36:13 -0700731 struct dentry *old = nd->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Andreas Mohre518ddb2006-09-29 02:01:22 -0700733 read_lock(&fs->lock);
734 if (nd->dentry == fs->root &&
735 nd->mnt == fs->rootmnt) {
736 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700739 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_lock(&dcache_lock);
Al Viro58c465e2005-06-06 13:36:13 -0700741 if (nd->dentry != nd->mnt->mnt_root) {
742 nd->dentry = dget(nd->dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 spin_unlock(&dcache_lock);
744 dput(old);
745 break;
746 }
747 spin_unlock(&dcache_lock);
748 spin_lock(&vfsmount_lock);
Al Viro58c465e2005-06-06 13:36:13 -0700749 parent = nd->mnt->mnt_parent;
750 if (parent == nd->mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 spin_unlock(&vfsmount_lock);
752 break;
753 }
754 mntget(parent);
Al Viro58c465e2005-06-06 13:36:13 -0700755 nd->dentry = dget(nd->mnt->mnt_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 spin_unlock(&vfsmount_lock);
757 dput(old);
Al Viro58c465e2005-06-06 13:36:13 -0700758 mntput(nd->mnt);
759 nd->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Al Viro58c465e2005-06-06 13:36:13 -0700761 follow_mount(&nd->mnt, &nd->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/*
765 * It's more convoluted than I'd like it to be, but... it's still fairly
766 * small and for now I'd prefer to have fast path as straight as possible.
767 * It _is_ time-critical.
768 */
769static int do_lookup(struct nameidata *nd, struct qstr *name,
770 struct path *path)
771{
772 struct vfsmount *mnt = nd->mnt;
773 struct dentry *dentry = __d_lookup(nd->dentry, name);
774
775 if (!dentry)
776 goto need_lookup;
777 if (dentry->d_op && dentry->d_op->d_revalidate)
778 goto need_revalidate;
779done:
780 path->mnt = mnt;
781 path->dentry = dentry;
Al Viro634ee702005-06-06 13:36:13 -0700782 __follow_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784
785need_lookup:
786 dentry = real_lookup(nd->dentry, name, nd);
787 if (IS_ERR(dentry))
788 goto fail;
789 goto done;
790
791need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -0700792 dentry = do_revalidate(dentry, nd);
793 if (!dentry)
794 goto need_lookup;
795 if (IS_ERR(dentry))
796 goto fail;
797 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799fail:
800 return PTR_ERR(dentry);
801}
802
803/*
804 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +0100805 * This is the basic name resolution function, turning a pathname into
806 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
Prasanna Medaea3834d2005-04-29 16:00:17 +0100808 * Returns 0 and nd will have valid dentry and mnt on success.
809 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
811static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
812{
813 struct path next;
814 struct inode *inode;
815 int err;
816 unsigned int lookup_flags = nd->flags;
817
818 while (*name=='/')
819 name++;
820 if (!*name)
821 goto return_reval;
822
823 inode = nd->dentry->d_inode;
824 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -0800825 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* At this point we know we have a real path component. */
828 for(;;) {
829 unsigned long hash;
830 struct qstr this;
831 unsigned int c;
832
Trond Myklebustcdce5d62005-10-18 14:20:18 -0700833 nd->flags |= LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 err = exec_permission_lite(inode, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800835 if (err == -EAGAIN)
836 err = vfs_permission(nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (err)
838 break;
839
840 this.name = name;
841 c = *(const unsigned char *)name;
842
843 hash = init_name_hash();
844 do {
845 name++;
846 hash = partial_name_hash(c, hash);
847 c = *(const unsigned char *)name;
848 } while (c && (c != '/'));
849 this.len = name - (const char *) this.name;
850 this.hash = end_name_hash(hash);
851
852 /* remove trailing slashes? */
853 if (!c)
854 goto last_component;
855 while (*++name == '/');
856 if (!*name)
857 goto last_with_slashes;
858
859 /*
860 * "." and ".." are special - ".." especially so because it has
861 * to be able to know about the current root directory and
862 * parent relationships.
863 */
864 if (this.name[0] == '.') switch (this.len) {
865 default:
866 break;
867 case 2:
868 if (this.name[1] != '.')
869 break;
Al Viro58c465e2005-06-06 13:36:13 -0700870 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 inode = nd->dentry->d_inode;
872 /* fallthrough */
873 case 1:
874 continue;
875 }
876 /*
877 * See if the low-level filesystem might want
878 * to use its own hash..
879 */
880 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
881 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
882 if (err < 0)
883 break;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /* This does the actual lookups.. */
886 err = do_lookup(nd, &this, &next);
887 if (err)
888 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 err = -ENOENT;
891 inode = next.dentry->d_inode;
892 if (!inode)
893 goto out_dput;
894 err = -ENOTDIR;
895 if (!inode->i_op)
896 goto out_dput;
897
898 if (inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700899 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (err)
901 goto return_err;
902 err = -ENOENT;
903 inode = nd->dentry->d_inode;
904 if (!inode)
905 break;
906 err = -ENOTDIR;
907 if (!inode->i_op)
908 break;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700909 } else
910 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 err = -ENOTDIR;
912 if (!inode->i_op->lookup)
913 break;
914 continue;
915 /* here ends the main loop */
916
917last_with_slashes:
918 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
919last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -0800920 /* Clear LOOKUP_CONTINUE iff it was previously unset */
921 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (lookup_flags & LOOKUP_PARENT)
923 goto lookup_parent;
924 if (this.name[0] == '.') switch (this.len) {
925 default:
926 break;
927 case 2:
928 if (this.name[1] != '.')
929 break;
Al Viro58c465e2005-06-06 13:36:13 -0700930 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 inode = nd->dentry->d_inode;
932 /* fallthrough */
933 case 1:
934 goto return_reval;
935 }
936 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
937 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
938 if (err < 0)
939 break;
940 }
941 err = do_lookup(nd, &this, &next);
942 if (err)
943 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 inode = next.dentry->d_inode;
945 if ((lookup_flags & LOOKUP_FOLLOW)
946 && inode && inode->i_op && inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700947 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (err)
949 goto return_err;
950 inode = nd->dentry->d_inode;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700951 } else
952 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 err = -ENOENT;
954 if (!inode)
955 break;
956 if (lookup_flags & LOOKUP_DIRECTORY) {
957 err = -ENOTDIR;
958 if (!inode->i_op || !inode->i_op->lookup)
959 break;
960 }
961 goto return_base;
962lookup_parent:
963 nd->last = this;
964 nd->last_type = LAST_NORM;
965 if (this.name[0] != '.')
966 goto return_base;
967 if (this.len == 1)
968 nd->last_type = LAST_DOT;
969 else if (this.len == 2 && this.name[1] == '.')
970 nd->last_type = LAST_DOTDOT;
971 else
972 goto return_base;
973return_reval:
974 /*
975 * We bypassed the ordinary revalidation routines.
976 * We may need to check the cached dentry for staleness.
977 */
978 if (nd->dentry && nd->dentry->d_sb &&
979 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
980 err = -ESTALE;
981 /* Note: we do not d_invalidate() */
982 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
983 break;
984 }
985return_base:
986 return 0;
987out_dput:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700988 dput_path(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 break;
990 }
991 path_release(nd);
992return_err:
993 return err;
994}
995
996/*
997 * Wrapper to retry pathname resolution whenever the underlying
998 * file system returns an ESTALE.
999 *
1000 * Retry the whole path once, forcing real lookup requests
1001 * instead of relying on the dcache.
1002 */
Josef 'Jeff' Sipekc4a78082007-07-19 01:48:22 -07001003static int fastcall link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct nameidata save = *nd;
1006 int result;
1007
1008 /* make sure the stuff we saved doesn't go away */
1009 dget(save.dentry);
1010 mntget(save.mnt);
1011
1012 result = __link_path_walk(name, nd);
1013 if (result == -ESTALE) {
1014 *nd = save;
1015 dget(nd->dentry);
1016 mntget(nd->mnt);
1017 nd->flags |= LOOKUP_REVAL;
1018 result = __link_path_walk(name, nd);
1019 }
1020
1021 dput(save.dentry);
1022 mntput(save.mnt);
1023
1024 return result;
1025}
1026
Josef 'Jeff' Sipekf79c20f2007-07-19 01:48:22 -07001027static int fastcall path_walk(const char * name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 current->total_link_count = 0;
1030 return link_path_walk(name, nd);
1031}
1032
Prasanna Medaea3834d2005-04-29 16:00:17 +01001033/*
1034 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1035 * everything is done. Returns 0 and drops input nd, if lookup failed;
1036 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1038{
1039 if (path_walk(name, nd))
1040 return 0; /* something went wrong... */
1041
1042 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1043 struct dentry *old_dentry = nd->dentry;
1044 struct vfsmount *old_mnt = nd->mnt;
1045 struct qstr last = nd->last;
1046 int last_type = nd->last_type;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001047 struct fs_struct *fs = current->fs;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
Andreas Mohre518ddb2006-09-29 02:01:22 -07001050 * NAME was not found in alternate root or it's a directory.
1051 * Try to find it in the normal root:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 */
1053 nd->last_type = LAST_ROOT;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001054 read_lock(&fs->lock);
1055 nd->mnt = mntget(fs->rootmnt);
1056 nd->dentry = dget(fs->root);
1057 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (path_walk(name, nd) == 0) {
1059 if (nd->dentry->d_inode) {
1060 dput(old_dentry);
1061 mntput(old_mnt);
1062 return 1;
1063 }
1064 path_release(nd);
1065 }
1066 nd->dentry = old_dentry;
1067 nd->mnt = old_mnt;
1068 nd->last = last;
1069 nd->last_type = last_type;
1070 }
1071 return 1;
1072}
1073
1074void set_fs_altroot(void)
1075{
1076 char *emul = __emul_prefix();
1077 struct nameidata nd;
1078 struct vfsmount *mnt = NULL, *oldmnt;
1079 struct dentry *dentry = NULL, *olddentry;
1080 int err;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001081 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 if (!emul)
1084 goto set_it;
1085 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1086 if (!err) {
1087 mnt = nd.mnt;
1088 dentry = nd.dentry;
1089 }
1090set_it:
Andreas Mohre518ddb2006-09-29 02:01:22 -07001091 write_lock(&fs->lock);
1092 oldmnt = fs->altrootmnt;
1093 olddentry = fs->altroot;
1094 fs->altrootmnt = mnt;
1095 fs->altroot = dentry;
1096 write_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (olddentry) {
1098 dput(olddentry);
1099 mntput(oldmnt);
1100 }
1101}
1102
Prasanna Medaea3834d2005-04-29 16:00:17 +01001103/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001104static int fastcall do_path_lookup(int dfd, const char *name,
1105 unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001107 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001108 int fput_needed;
1109 struct file *file;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001110 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1113 nd->flags = flags;
1114 nd->depth = 0;
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (*name=='/') {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001117 read_lock(&fs->lock);
1118 if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1119 nd->mnt = mntget(fs->altrootmnt);
1120 nd->dentry = dget(fs->altroot);
1121 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (__emul_lookup_dentry(name,nd))
Prasanna Medaea3834d2005-04-29 16:00:17 +01001123 goto out; /* found in altroot */
Andreas Mohre518ddb2006-09-29 02:01:22 -07001124 read_lock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Andreas Mohre518ddb2006-09-29 02:01:22 -07001126 nd->mnt = mntget(fs->rootmnt);
1127 nd->dentry = dget(fs->root);
1128 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001129 } else if (dfd == AT_FDCWD) {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001130 read_lock(&fs->lock);
1131 nd->mnt = mntget(fs->pwdmnt);
1132 nd->dentry = dget(fs->pwd);
1133 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001134 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001135 struct dentry *dentry;
1136
1137 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001138 retval = -EBADF;
1139 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001140 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001141
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001142 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001143
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001144 retval = -ENOTDIR;
1145 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001146 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001147
1148 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001149 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001150 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001151
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001152 nd->mnt = mntget(file->f_path.mnt);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001153 nd->dentry = dget(dentry);
1154
1155 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Josef 'Jeff' Sipek2dfdd262007-05-09 02:33:41 -07001157
1158 retval = path_walk(name, nd);
Prasanna Medaea3834d2005-04-29 16:00:17 +01001159out:
Josef 'Jeff' Sipek62ce39c2007-05-09 02:33:41 -07001160 if (unlikely(!retval && !audit_dummy_context() && nd->dentry &&
Suzuki3bc84142006-02-07 12:58:36 -08001161 nd->dentry->d_inode))
Amy Griffis9c937dc2006-06-08 23:19:31 -04001162 audit_inode(name, nd->dentry->d_inode);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001163out_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001164 return retval;
1165
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001166fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001167 fput_light(file, fput_needed);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001168 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001171int fastcall path_lookup(const char *name, unsigned int flags,
1172 struct nameidata *nd)
1173{
1174 return do_path_lookup(AT_FDCWD, name, flags, nd);
1175}
1176
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001177/**
1178 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1179 * @dentry: pointer to dentry of the base directory
1180 * @mnt: pointer to vfs mount of the base directory
1181 * @name: pointer to file name
1182 * @flags: lookup flags
1183 * @nd: pointer to nameidata
1184 */
1185int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1186 const char *name, unsigned int flags,
1187 struct nameidata *nd)
1188{
1189 int retval;
1190
1191 /* same as do_path_lookup */
1192 nd->last_type = LAST_ROOT;
1193 nd->flags = flags;
1194 nd->depth = 0;
1195
1196 nd->mnt = mntget(mnt);
1197 nd->dentry = dget(dentry);
1198
1199 retval = path_walk(name, nd);
1200 if (unlikely(!retval && !audit_dummy_context() && nd->dentry &&
1201 nd->dentry->d_inode))
1202 audit_inode(name, nd->dentry->d_inode);
1203
1204 return retval;
1205
1206}
1207
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001208static int __path_lookup_intent_open(int dfd, const char *name,
1209 unsigned int lookup_flags, struct nameidata *nd,
1210 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001211{
1212 struct file *filp = get_empty_filp();
1213 int err;
1214
1215 if (filp == NULL)
1216 return -ENFILE;
1217 nd->intent.open.file = filp;
1218 nd->intent.open.flags = open_flags;
1219 nd->intent.open.create_mode = create_mode;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001220 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001221 if (IS_ERR(nd->intent.open.file)) {
1222 if (err == 0) {
1223 err = PTR_ERR(nd->intent.open.file);
1224 path_release(nd);
1225 }
1226 } else if (err != 0)
1227 release_open_intent(nd);
1228 return err;
1229}
1230
1231/**
1232 * path_lookup_open - lookup a file path with open intent
Martin Waitz7045f372006-02-01 03:06:57 -08001233 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001234 * @name: pointer to file name
1235 * @lookup_flags: lookup intent flags
1236 * @nd: pointer to nameidata
1237 * @open_flags: open intent flags
1238 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001239int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001240 struct nameidata *nd, int open_flags)
1241{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001242 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001243 open_flags, 0);
1244}
1245
1246/**
1247 * path_lookup_create - lookup a file path with open + create intent
Martin Waitz7045f372006-02-01 03:06:57 -08001248 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001249 * @name: pointer to file name
1250 * @lookup_flags: lookup intent flags
1251 * @nd: pointer to nameidata
1252 * @open_flags: open intent flags
1253 * @create_mode: create intent flags
1254 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001255static int path_lookup_create(int dfd, const char *name,
1256 unsigned int lookup_flags, struct nameidata *nd,
1257 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001258{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001259 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1260 nd, open_flags, create_mode);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001261}
1262
1263int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1264 struct nameidata *nd, int open_flags)
1265{
1266 char *tmp = getname(name);
1267 int err = PTR_ERR(tmp);
1268
1269 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001270 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001271 putname(tmp);
1272 }
1273 return err;
1274}
1275
Christoph Hellwigeead1912007-10-16 23:25:38 -07001276static struct dentry *__lookup_hash(struct qstr *name,
1277 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
James Morris057f6c02007-04-26 00:12:05 -07001279 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct inode *inode;
1281 int err;
1282
1283 inode = base->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /*
1286 * See if the low-level filesystem might want
1287 * to use its own hash..
1288 */
1289 if (base->d_op && base->d_op->d_hash) {
1290 err = base->d_op->d_hash(base, name);
1291 dentry = ERR_PTR(err);
1292 if (err < 0)
1293 goto out;
1294 }
1295
1296 dentry = cached_lookup(base, name, nd);
1297 if (!dentry) {
1298 struct dentry *new = d_alloc(base, name);
1299 dentry = ERR_PTR(-ENOMEM);
1300 if (!new)
1301 goto out;
1302 dentry = inode->i_op->lookup(inode, new, nd);
1303 if (!dentry)
1304 dentry = new;
1305 else
1306 dput(new);
1307 }
1308out:
1309 return dentry;
1310}
1311
James Morris057f6c02007-04-26 00:12:05 -07001312/*
1313 * Restricted form of lookup. Doesn't follow links, single-component only,
1314 * needs parent already locked. Doesn't follow mounts.
1315 * SMP-safe.
1316 */
Adrian Bunka244e162006-03-31 02:32:11 -08001317static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Christoph Hellwigeead1912007-10-16 23:25:38 -07001319 int err;
1320
1321 err = permission(nd->dentry->d_inode, MAY_EXEC, nd);
1322 if (err)
1323 return ERR_PTR(err);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001324 return __lookup_hash(&nd->last, nd->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Christoph Hellwigeead1912007-10-16 23:25:38 -07001327static int __lookup_one_len(const char *name, struct qstr *this,
1328 struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 unsigned int c;
1332
James Morris057f6c02007-04-26 00:12:05 -07001333 this->name = name;
1334 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001336 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 hash = init_name_hash();
1339 while (len--) {
1340 c = *(const unsigned char *)name++;
1341 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001342 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 hash = partial_name_hash(c, hash);
1344 }
James Morris057f6c02007-04-26 00:12:05 -07001345 this->hash = end_name_hash(hash);
1346 return 0;
1347}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Christoph Hellwigeead1912007-10-16 23:25:38 -07001349/**
1350 * lookup_one_len: filesystem helper to lookup single pathname component
1351 * @name: pathname component to lookup
1352 * @base: base directory to lookup from
1353 * @len: maximum length @len should be interpreted to
1354 *
1355 * Note that this routine is purely a helper for filesystem useage and should
1356 * not be called by generic code. Also note that by using this function to
1357 * nameidata argument is passed to the filesystem methods and a filesystem
1358 * using this helper needs to be prepared for that.
1359 */
James Morris057f6c02007-04-26 00:12:05 -07001360struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1361{
1362 int err;
1363 struct qstr this;
1364
1365 err = __lookup_one_len(name, &this, base, len);
1366 if (err)
1367 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001368
1369 err = permission(base->d_inode, MAY_EXEC, NULL);
1370 if (err)
1371 return ERR_PTR(err);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001372 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001373}
1374
Christoph Hellwigeead1912007-10-16 23:25:38 -07001375/**
1376 * lookup_one_noperm - bad hack for sysfs
1377 * @name: pathname component to lookup
1378 * @base: base directory to lookup from
1379 *
1380 * This is a variant of lookup_one_len that doesn't perform any permission
1381 * checks. It's a horrible hack to work around the braindead sysfs
1382 * architecture and should not be used anywhere else.
1383 *
1384 * DON'T USE THIS FUNCTION EVER, thanks.
1385 */
1386struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
James Morris057f6c02007-04-26 00:12:05 -07001387{
1388 int err;
1389 struct qstr this;
1390
Christoph Hellwigeead1912007-10-16 23:25:38 -07001391 err = __lookup_one_len(name, &this, base, strlen(name));
James Morris057f6c02007-04-26 00:12:05 -07001392 if (err)
1393 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001394 return __lookup_hash(&this, base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001397int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
1398 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 char *tmp = getname(name);
1401 int err = PTR_ERR(tmp);
1402
1403 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001404 err = do_path_lookup(dfd, tmp, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 putname(tmp);
1406 }
1407 return err;
1408}
1409
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001410int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1411{
1412 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415/*
1416 * It's inline, so penalty for filesystems that don't use sticky bit is
1417 * minimal.
1418 */
1419static inline int check_sticky(struct inode *dir, struct inode *inode)
1420{
1421 if (!(dir->i_mode & S_ISVTX))
1422 return 0;
1423 if (inode->i_uid == current->fsuid)
1424 return 0;
1425 if (dir->i_uid == current->fsuid)
1426 return 0;
1427 return !capable(CAP_FOWNER);
1428}
1429
1430/*
1431 * Check whether we can remove a link victim from directory dir, check
1432 * whether the type of victim is right.
1433 * 1. We can't do it if dir is read-only (done in permission())
1434 * 2. We should have write and exec permissions on dir
1435 * 3. We can't remove anything from append-only dir
1436 * 4. We can't do anything with immutable dir (done in permission())
1437 * 5. If the sticky bit on dir is set we should either
1438 * a. be owner of dir, or
1439 * b. be owner of victim, or
1440 * c. have CAP_FOWNER capability
1441 * 6. If the victim is append-only or immutable we can't do antyhing with
1442 * links pointing to it.
1443 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1444 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1445 * 9. We can't remove a root or mountpoint.
1446 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1447 * nfs_async_unlink().
1448 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001449static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 int error;
1452
1453 if (!victim->d_inode)
1454 return -ENOENT;
1455
1456 BUG_ON(victim->d_parent->d_inode != dir);
Amy Griffis73d3ec52006-07-13 13:16:39 -04001457 audit_inode_child(victim->d_name.name, victim->d_inode, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1460 if (error)
1461 return error;
1462 if (IS_APPEND(dir))
1463 return -EPERM;
1464 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1465 IS_IMMUTABLE(victim->d_inode))
1466 return -EPERM;
1467 if (isdir) {
1468 if (!S_ISDIR(victim->d_inode->i_mode))
1469 return -ENOTDIR;
1470 if (IS_ROOT(victim))
1471 return -EBUSY;
1472 } else if (S_ISDIR(victim->d_inode->i_mode))
1473 return -EISDIR;
1474 if (IS_DEADDIR(dir))
1475 return -ENOENT;
1476 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1477 return -EBUSY;
1478 return 0;
1479}
1480
1481/* Check whether we can create an object with dentry child in directory
1482 * dir.
1483 * 1. We can't do it if child already exists (open has special treatment for
1484 * this case, but since we are inlined it's OK)
1485 * 2. We can't do it if dir is read-only (done in permission())
1486 * 3. We should have write and exec permissions on dir
1487 * 4. We can't do it if dir is immutable (done in permission())
1488 */
1489static inline int may_create(struct inode *dir, struct dentry *child,
1490 struct nameidata *nd)
1491{
1492 if (child->d_inode)
1493 return -EEXIST;
1494 if (IS_DEADDIR(dir))
1495 return -ENOENT;
1496 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1497}
1498
1499/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 * O_DIRECTORY translates into forcing a directory lookup.
1501 */
1502static inline int lookup_flags(unsigned int f)
1503{
1504 unsigned long retval = LOOKUP_FOLLOW;
1505
1506 if (f & O_NOFOLLOW)
1507 retval &= ~LOOKUP_FOLLOW;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (f & O_DIRECTORY)
1510 retval |= LOOKUP_DIRECTORY;
1511
1512 return retval;
1513}
1514
1515/*
1516 * p1 and p2 should be directories on the same fs.
1517 */
1518struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1519{
1520 struct dentry *p;
1521
1522 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001523 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return NULL;
1525 }
1526
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001527 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 for (p = p1; p->d_parent != p; p = p->d_parent) {
1530 if (p->d_parent == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001531 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1532 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return p;
1534 }
1535 }
1536
1537 for (p = p2; p->d_parent != p; p = p->d_parent) {
1538 if (p->d_parent == p1) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001539 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1540 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return p;
1542 }
1543 }
1544
Ingo Molnarf2eace22006-07-03 00:25:05 -07001545 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1546 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return NULL;
1548}
1549
1550void unlock_rename(struct dentry *p1, struct dentry *p2)
1551{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001552 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001554 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001555 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557}
1558
1559int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1560 struct nameidata *nd)
1561{
1562 int error = may_create(dir, dentry, nd);
1563
1564 if (error)
1565 return error;
1566
1567 if (!dir->i_op || !dir->i_op->create)
1568 return -EACCES; /* shouldn't it be ENOSYS? */
1569 mode &= S_IALLUGO;
1570 mode |= S_IFREG;
1571 error = security_inode_create(dir, dentry, mode);
1572 if (error)
1573 return error;
1574 DQUOT_INIT(dir);
1575 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001576 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001577 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return error;
1579}
1580
1581int may_open(struct nameidata *nd, int acc_mode, int flag)
1582{
1583 struct dentry *dentry = nd->dentry;
1584 struct inode *inode = dentry->d_inode;
1585 int error;
1586
1587 if (!inode)
1588 return -ENOENT;
1589
1590 if (S_ISLNK(inode->i_mode))
1591 return -ELOOP;
1592
1593 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1594 return -EISDIR;
1595
Christoph Hellwige4543ed2005-11-08 21:35:04 -08001596 error = vfs_permission(nd, acc_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (error)
1598 return error;
1599
1600 /*
1601 * FIFO's, sockets and device files are special: they don't
1602 * actually live on the filesystem itself, and as such you
1603 * can write to them even if the filesystem is read-only.
1604 */
1605 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1606 flag &= ~O_TRUNC;
1607 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1608 if (nd->mnt->mnt_flags & MNT_NODEV)
1609 return -EACCES;
1610
1611 flag &= ~O_TRUNC;
1612 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1613 return -EROFS;
1614 /*
1615 * An append-only file must be opened in append mode for writing.
1616 */
1617 if (IS_APPEND(inode)) {
1618 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1619 return -EPERM;
1620 if (flag & O_TRUNC)
1621 return -EPERM;
1622 }
1623
1624 /* O_NOATIME can only be set by the owner or superuser */
1625 if (flag & O_NOATIME)
Satyam Sharma3bd858a2007-07-17 15:00:08 +05301626 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return -EPERM;
1628
1629 /*
1630 * Ensure there are no outstanding leases on the file.
1631 */
1632 error = break_lease(inode, flag);
1633 if (error)
1634 return error;
1635
1636 if (flag & O_TRUNC) {
1637 error = get_write_access(inode);
1638 if (error)
1639 return error;
1640
1641 /*
1642 * Refuse to truncate files with mandatory locks held on them.
1643 */
1644 error = locks_verify_locked(inode);
1645 if (!error) {
1646 DQUOT_INIT(inode);
1647
NeilBrown4a301312006-01-08 01:02:39 -08001648 error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650 put_write_access(inode);
1651 if (error)
1652 return error;
1653 } else
1654 if (flag & FMODE_WRITE)
1655 DQUOT_INIT(inode);
1656
1657 return 0;
1658}
1659
Dave Hansenaab520e2006-09-30 23:29:02 -07001660static int open_namei_create(struct nameidata *nd, struct path *path,
1661 int flag, int mode)
1662{
1663 int error;
1664 struct dentry *dir = nd->dentry;
1665
1666 if (!IS_POSIXACL(dir->d_inode))
1667 mode &= ~current->fs->umask;
1668 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1669 mutex_unlock(&dir->d_inode->i_mutex);
1670 dput(nd->dentry);
1671 nd->dentry = path->dentry;
1672 if (error)
1673 return error;
1674 /* Don't check for write permission, don't truncate */
1675 return may_open(nd, 0, flag & ~O_TRUNC);
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
1679 * open_namei()
1680 *
1681 * namei for open - this is in fact almost the whole open-routine.
1682 *
1683 * Note that the low bits of "flag" aren't the same as in the open
1684 * system call - they are 00 - no permissions needed
1685 * 01 - read permission needed
1686 * 10 - write permission needed
1687 * 11 - read/write permissions needed
1688 * which is a lot more logical, and also allows the "no perm" needed
1689 * for symlinks (where the permissions are checked later).
1690 * SMP-safe
1691 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001692int open_namei(int dfd, const char *pathname, int flag,
1693 int mode, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Trond Myklebust834f2a42005-10-18 14:20:16 -07001695 int acc_mode, error;
Al Viro4e7506e2005-06-06 13:36:00 -07001696 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct dentry *dir;
1698 int count = 0;
1699
1700 acc_mode = ACC_MODE(flag);
1701
Trond Myklebust834f2a42005-10-18 14:20:16 -07001702 /* O_TRUNC implies we need access checks for write permissions */
1703 if (flag & O_TRUNC)
1704 acc_mode |= MAY_WRITE;
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* Allow the LSM permission hook to distinguish append
1707 access from general write access. */
1708 if (flag & O_APPEND)
1709 acc_mode |= MAY_APPEND;
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /*
1712 * The simplest case - just a plain lookup.
1713 */
1714 if (!(flag & O_CREAT)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001715 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1716 nd, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (error)
1718 return error;
1719 goto ok;
1720 }
1721
1722 /*
1723 * Create - we need to know the parent.
1724 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001725 error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (error)
1727 return error;
1728
1729 /*
1730 * We have the parent and last component. First of all, check
1731 * that we are not asked to creat(2) an obvious directory - that
1732 * will not do.
1733 */
1734 error = -EISDIR;
1735 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1736 goto exit;
1737
1738 dir = nd->dentry;
1739 nd->flags &= ~LOOKUP_PARENT;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001740 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001741 path.dentry = lookup_hash(nd);
Al Virod73ffe12005-06-06 13:36:01 -07001742 path.mnt = nd->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744do_last:
Al Viro4e7506e2005-06-06 13:36:00 -07001745 error = PTR_ERR(path.dentry);
1746 if (IS_ERR(path.dentry)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001747 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 goto exit;
1749 }
1750
Oleg Drokin4af4c522006-03-25 03:06:54 -08001751 if (IS_ERR(nd->intent.open.file)) {
1752 mutex_unlock(&dir->d_inode->i_mutex);
1753 error = PTR_ERR(nd->intent.open.file);
1754 goto exit_dput;
1755 }
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /* Negative dentry, just create the file */
Al Viro4e7506e2005-06-06 13:36:00 -07001758 if (!path.dentry->d_inode) {
Dave Hansenaab520e2006-09-30 23:29:02 -07001759 error = open_namei_create(nd, &path, flag, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (error)
1761 goto exit;
Dave Hansenaab520e2006-09-30 23:29:02 -07001762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
1765 /*
1766 * It already exists.
1767 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001768 mutex_unlock(&dir->d_inode->i_mutex);
Amy Griffis4fc03b92007-02-13 14:15:01 -05001769 audit_inode(pathname, path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 error = -EEXIST;
1772 if (flag & O_EXCL)
1773 goto exit_dput;
1774
Al Viroe13b2102005-06-06 13:36:06 -07001775 if (__follow_mount(&path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 error = -ELOOP;
Al Viroba7a4c12005-06-06 13:36:08 -07001777 if (flag & O_NOFOLLOW)
1778 goto exit_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 error = -ENOENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001782 if (!path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 goto exit_dput;
Al Viro4e7506e2005-06-06 13:36:00 -07001784 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto do_link;
1786
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001787 path_to_nameidata(&path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 error = -EISDIR;
Al Viro4e7506e2005-06-06 13:36:00 -07001789 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto exit;
1791ok:
1792 error = may_open(nd, acc_mode, flag);
1793 if (error)
1794 goto exit;
1795 return 0;
1796
1797exit_dput:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001798 dput_path(&path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799exit:
Trond Myklebust834f2a42005-10-18 14:20:16 -07001800 if (!IS_ERR(nd->intent.open.file))
1801 release_open_intent(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 path_release(nd);
1803 return error;
1804
1805do_link:
1806 error = -ELOOP;
1807 if (flag & O_NOFOLLOW)
1808 goto exit_dput;
1809 /*
1810 * This is subtle. Instead of calling do_follow_link() we do the
1811 * thing by hands. The reason is that this way we have zero link_count
1812 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1813 * After that we have the parent and last component, i.e.
1814 * we are in the same situation as after the first path_walk().
1815 * Well, almost - if the last component is normal we get its copy
1816 * stored in nd->last.name and we will have to putname() it when we
1817 * are done. Procfs-like symlinks just set LAST_BIND.
1818 */
1819 nd->flags |= LOOKUP_PARENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001820 error = security_inode_follow_link(path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (error)
1822 goto exit_dput;
Al Virocd4e91d2005-06-06 13:36:03 -07001823 error = __do_follow_link(&path, nd);
Kirill Korotaevde459212006-07-14 00:23:49 -07001824 if (error) {
1825 /* Does someone understand code flow here? Or it is only
1826 * me so stupid? Anathema to whoever designed this non-sense
1827 * with "intent.open".
1828 */
1829 release_open_intent(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return error;
Kirill Korotaevde459212006-07-14 00:23:49 -07001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 nd->flags &= ~LOOKUP_PARENT;
Al Virod671d5e2005-06-06 13:36:04 -07001833 if (nd->last_type == LAST_BIND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 error = -EISDIR;
1836 if (nd->last_type != LAST_NORM)
1837 goto exit;
1838 if (nd->last.name[nd->last.len]) {
Linus Torvalds82984112005-10-06 21:54:21 -07001839 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto exit;
1841 }
1842 error = -ELOOP;
1843 if (count++==32) {
Linus Torvalds82984112005-10-06 21:54:21 -07001844 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto exit;
1846 }
1847 dir = nd->dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001848 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001849 path.dentry = lookup_hash(nd);
Al Virod671d5e2005-06-06 13:36:04 -07001850 path.mnt = nd->mnt;
Linus Torvalds82984112005-10-06 21:54:21 -07001851 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto do_last;
1853}
1854
1855/**
1856 * lookup_create - lookup a dentry, creating it if it doesn't exist
1857 * @nd: nameidata info
1858 * @is_dir: directory flag
1859 *
1860 * Simple function to lookup and return a dentry and create it
1861 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001862 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001863 * Returns with nd->dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 */
1865struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1866{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001867 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Ingo Molnarf2eace22006-07-03 00:25:05 -07001869 mutex_lock_nested(&nd->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001870 /*
1871 * Yucky last component or no last component at all?
1872 * (foo/., foo/.., /////)
1873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (nd->last_type != LAST_NORM)
1875 goto fail;
1876 nd->flags &= ~LOOKUP_PARENT;
ASANO Masahiroa6349042006-08-22 20:06:02 -04001877 nd->flags |= LOOKUP_CREATE;
1878 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001879
1880 /*
1881 * Do the final lookup.
1882 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08001883 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (IS_ERR(dentry))
1885 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001886
1887 /*
1888 * Special case - lookup gave negative, but... we had foo/bar/
1889 * From the vfs_mknod() POV we just have a negative dentry -
1890 * all is fine. Let's be bastards - you had / on the end, you've
1891 * been asking for (non-existent) directory. -ENOENT for you.
1892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1894 goto enoent;
1895 return dentry;
1896enoent:
1897 dput(dentry);
1898 dentry = ERR_PTR(-ENOENT);
1899fail:
1900 return dentry;
1901}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07001902EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1905{
1906 int error = may_create(dir, dentry, NULL);
1907
1908 if (error)
1909 return error;
1910
1911 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1912 return -EPERM;
1913
1914 if (!dir->i_op || !dir->i_op->mknod)
1915 return -EPERM;
1916
1917 error = security_inode_mknod(dir, dentry, mode, dev);
1918 if (error)
1919 return error;
1920
1921 DQUOT_INIT(dir);
1922 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001923 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001924 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return error;
1926}
1927
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001928asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1929 unsigned dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 int error = 0;
1932 char * tmp;
1933 struct dentry * dentry;
1934 struct nameidata nd;
1935
1936 if (S_ISDIR(mode))
1937 return -EPERM;
1938 tmp = getname(filename);
1939 if (IS_ERR(tmp))
1940 return PTR_ERR(tmp);
1941
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001942 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (error)
1944 goto out;
1945 dentry = lookup_create(&nd, 0);
1946 error = PTR_ERR(dentry);
1947
1948 if (!IS_POSIXACL(nd.dentry->d_inode))
1949 mode &= ~current->fs->umask;
1950 if (!IS_ERR(dentry)) {
1951 switch (mode & S_IFMT) {
1952 case 0: case S_IFREG:
1953 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1954 break;
1955 case S_IFCHR: case S_IFBLK:
1956 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1957 new_decode_dev(dev));
1958 break;
1959 case S_IFIFO: case S_IFSOCK:
1960 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1961 break;
1962 case S_IFDIR:
1963 error = -EPERM;
1964 break;
1965 default:
1966 error = -EINVAL;
1967 }
1968 dput(dentry);
1969 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001970 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 path_release(&nd);
1972out:
1973 putname(tmp);
1974
1975 return error;
1976}
1977
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001978asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
1979{
1980 return sys_mknodat(AT_FDCWD, filename, mode, dev);
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1984{
1985 int error = may_create(dir, dentry, NULL);
1986
1987 if (error)
1988 return error;
1989
1990 if (!dir->i_op || !dir->i_op->mkdir)
1991 return -EPERM;
1992
1993 mode &= (S_IRWXUGO|S_ISVTX);
1994 error = security_inode_mkdir(dir, dentry, mode);
1995 if (error)
1996 return error;
1997
1998 DQUOT_INIT(dir);
1999 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002000 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002001 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return error;
2003}
2004
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002005asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 int error = 0;
2008 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002009 struct dentry *dentry;
2010 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 tmp = getname(pathname);
2013 error = PTR_ERR(tmp);
Dave Hansen6902d922006-09-30 23:29:01 -07002014 if (IS_ERR(tmp))
2015 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Dave Hansen6902d922006-09-30 23:29:01 -07002017 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2018 if (error)
2019 goto out;
2020 dentry = lookup_create(&nd, 1);
2021 error = PTR_ERR(dentry);
2022 if (IS_ERR(dentry))
2023 goto out_unlock;
2024
2025 if (!IS_POSIXACL(nd.dentry->d_inode))
2026 mode &= ~current->fs->umask;
2027 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
2028 dput(dentry);
2029out_unlock:
2030 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2031 path_release(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032out:
Dave Hansen6902d922006-09-30 23:29:01 -07002033 putname(tmp);
2034out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return error;
2036}
2037
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002038asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2039{
2040 return sys_mkdirat(AT_FDCWD, pathname, mode);
2041}
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043/*
2044 * We try to drop the dentry early: we should have
2045 * a usage count of 2 if we're the only user of this
2046 * dentry, and if that is true (possibly after pruning
2047 * the dcache), then we drop the dentry now.
2048 *
2049 * A low-level filesystem can, if it choses, legally
2050 * do a
2051 *
2052 * if (!d_unhashed(dentry))
2053 * return -EBUSY;
2054 *
2055 * if it cannot handle the case of removing a directory
2056 * that is still in use by something else..
2057 */
2058void dentry_unhash(struct dentry *dentry)
2059{
2060 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002061 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 spin_lock(&dcache_lock);
2063 spin_lock(&dentry->d_lock);
2064 if (atomic_read(&dentry->d_count) == 2)
2065 __d_drop(dentry);
2066 spin_unlock(&dentry->d_lock);
2067 spin_unlock(&dcache_lock);
2068}
2069
2070int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2071{
2072 int error = may_delete(dir, dentry, 1);
2073
2074 if (error)
2075 return error;
2076
2077 if (!dir->i_op || !dir->i_op->rmdir)
2078 return -EPERM;
2079
2080 DQUOT_INIT(dir);
2081
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002082 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 dentry_unhash(dentry);
2084 if (d_mountpoint(dentry))
2085 error = -EBUSY;
2086 else {
2087 error = security_inode_rmdir(dir, dentry);
2088 if (!error) {
2089 error = dir->i_op->rmdir(dir, dentry);
2090 if (!error)
2091 dentry->d_inode->i_flags |= S_DEAD;
2092 }
2093 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002094 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 d_delete(dentry);
2097 }
2098 dput(dentry);
2099
2100 return error;
2101}
2102
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002103static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 int error = 0;
2106 char * name;
2107 struct dentry *dentry;
2108 struct nameidata nd;
2109
2110 name = getname(pathname);
2111 if(IS_ERR(name))
2112 return PTR_ERR(name);
2113
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002114 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (error)
2116 goto exit;
2117
2118 switch(nd.last_type) {
2119 case LAST_DOTDOT:
2120 error = -ENOTEMPTY;
2121 goto exit1;
2122 case LAST_DOT:
2123 error = -EINVAL;
2124 goto exit1;
2125 case LAST_ROOT:
2126 error = -EBUSY;
2127 goto exit1;
2128 }
Ingo Molnarf2eace22006-07-03 00:25:05 -07002129 mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002130 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002132 if (IS_ERR(dentry))
2133 goto exit2;
2134 error = vfs_rmdir(nd.dentry->d_inode, dentry);
2135 dput(dentry);
2136exit2:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002137 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138exit1:
2139 path_release(&nd);
2140exit:
2141 putname(name);
2142 return error;
2143}
2144
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002145asmlinkage long sys_rmdir(const char __user *pathname)
2146{
2147 return do_rmdir(AT_FDCWD, pathname);
2148}
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150int vfs_unlink(struct inode *dir, struct dentry *dentry)
2151{
2152 int error = may_delete(dir, dentry, 0);
2153
2154 if (error)
2155 return error;
2156
2157 if (!dir->i_op || !dir->i_op->unlink)
2158 return -EPERM;
2159
2160 DQUOT_INIT(dir);
2161
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002162 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (d_mountpoint(dentry))
2164 error = -EBUSY;
2165 else {
2166 error = security_inode_unlink(dir, dentry);
2167 if (!error)
2168 error = dir->i_op->unlink(dir, dentry);
2169 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002170 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2173 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2174 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
Robert Love0eeca282005-07-12 17:06:03 -04002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return error;
2178}
2179
2180/*
2181 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002182 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 * writeout happening, and we don't want to prevent access to the directory
2184 * while waiting on the I/O.
2185 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002186static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 int error = 0;
2189 char * name;
2190 struct dentry *dentry;
2191 struct nameidata nd;
2192 struct inode *inode = NULL;
2193
2194 name = getname(pathname);
2195 if(IS_ERR(name))
2196 return PTR_ERR(name);
2197
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002198 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (error)
2200 goto exit;
2201 error = -EISDIR;
2202 if (nd.last_type != LAST_NORM)
2203 goto exit1;
Ingo Molnarf2eace22006-07-03 00:25:05 -07002204 mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002205 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 error = PTR_ERR(dentry);
2207 if (!IS_ERR(dentry)) {
2208 /* Why not before? Because we want correct error value */
2209 if (nd.last.name[nd.last.len])
2210 goto slashes;
2211 inode = dentry->d_inode;
2212 if (inode)
2213 atomic_inc(&inode->i_count);
2214 error = vfs_unlink(nd.dentry->d_inode, dentry);
2215 exit2:
2216 dput(dentry);
2217 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002218 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (inode)
2220 iput(inode); /* truncate the inode here */
2221exit1:
2222 path_release(&nd);
2223exit:
2224 putname(name);
2225 return error;
2226
2227slashes:
2228 error = !dentry->d_inode ? -ENOENT :
2229 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2230 goto exit2;
2231}
2232
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002233asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2234{
2235 if ((flag & ~AT_REMOVEDIR) != 0)
2236 return -EINVAL;
2237
2238 if (flag & AT_REMOVEDIR)
2239 return do_rmdir(dfd, pathname);
2240
2241 return do_unlinkat(dfd, pathname);
2242}
2243
2244asmlinkage long sys_unlink(const char __user *pathname)
2245{
2246 return do_unlinkat(AT_FDCWD, pathname);
2247}
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
2250{
2251 int error = may_create(dir, dentry, NULL);
2252
2253 if (error)
2254 return error;
2255
2256 if (!dir->i_op || !dir->i_op->symlink)
2257 return -EPERM;
2258
2259 error = security_inode_symlink(dir, dentry, oldname);
2260 if (error)
2261 return error;
2262
2263 DQUOT_INIT(dir);
2264 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002265 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002266 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return error;
2268}
2269
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002270asmlinkage long sys_symlinkat(const char __user *oldname,
2271 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 int error = 0;
2274 char * from;
2275 char * to;
Dave Hansen6902d922006-09-30 23:29:01 -07002276 struct dentry *dentry;
2277 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 from = getname(oldname);
2280 if(IS_ERR(from))
2281 return PTR_ERR(from);
2282 to = getname(newname);
2283 error = PTR_ERR(to);
Dave Hansen6902d922006-09-30 23:29:01 -07002284 if (IS_ERR(to))
2285 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Dave Hansen6902d922006-09-30 23:29:01 -07002287 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2288 if (error)
2289 goto out;
2290 dentry = lookup_create(&nd, 0);
2291 error = PTR_ERR(dentry);
2292 if (IS_ERR(dentry))
2293 goto out_unlock;
2294
2295 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
2296 dput(dentry);
2297out_unlock:
2298 mutex_unlock(&nd.dentry->d_inode->i_mutex);
2299 path_release(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300out:
Dave Hansen6902d922006-09-30 23:29:01 -07002301 putname(to);
2302out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 putname(from);
2304 return error;
2305}
2306
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002307asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2308{
2309 return sys_symlinkat(oldname, AT_FDCWD, newname);
2310}
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2313{
2314 struct inode *inode = old_dentry->d_inode;
2315 int error;
2316
2317 if (!inode)
2318 return -ENOENT;
2319
2320 error = may_create(dir, new_dentry, NULL);
2321 if (error)
2322 return error;
2323
2324 if (dir->i_sb != inode->i_sb)
2325 return -EXDEV;
2326
2327 /*
2328 * A link to an append-only or immutable file cannot be created.
2329 */
2330 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2331 return -EPERM;
2332 if (!dir->i_op || !dir->i_op->link)
2333 return -EPERM;
2334 if (S_ISDIR(old_dentry->d_inode->i_mode))
2335 return -EPERM;
2336
2337 error = security_inode_link(old_dentry, dir, new_dentry);
2338 if (error)
2339 return error;
2340
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002341 mutex_lock(&old_dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 DQUOT_INIT(dir);
2343 error = dir->i_op->link(old_dentry, dir, new_dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002344 mutex_unlock(&old_dentry->d_inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002345 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002346 fsnotify_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return error;
2348}
2349
2350/*
2351 * Hardlinks are often used in delicate situations. We avoid
2352 * security-related surprises by not following symlinks on the
2353 * newname. --KAB
2354 *
2355 * We don't follow them on the oldname either to be compatible
2356 * with linux 2.0, and to avoid hard-linking to directories
2357 * and other special files. --ADM
2358 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002359asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002360 int newdfd, const char __user *newname,
2361 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
2363 struct dentry *new_dentry;
2364 struct nameidata nd, old_nd;
2365 int error;
2366 char * to;
2367
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002368 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002369 return -EINVAL;
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 to = getname(newname);
2372 if (IS_ERR(to))
2373 return PTR_ERR(to);
2374
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002375 error = __user_walk_fd(olddfd, oldname,
2376 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2377 &old_nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (error)
2379 goto exit;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002380 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (error)
2382 goto out;
2383 error = -EXDEV;
2384 if (old_nd.mnt != nd.mnt)
2385 goto out_release;
2386 new_dentry = lookup_create(&nd, 0);
2387 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002388 if (IS_ERR(new_dentry))
2389 goto out_unlock;
2390 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2391 dput(new_dentry);
2392out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002393 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394out_release:
2395 path_release(&nd);
2396out:
2397 path_release(&old_nd);
2398exit:
2399 putname(to);
2400
2401 return error;
2402}
2403
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002404asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2405{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002406 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002407}
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409/*
2410 * The worst of all namespace operations - renaming directory. "Perverted"
2411 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2412 * Problems:
2413 * a) we can get into loop creation. Check is done in is_subdir().
2414 * b) race potential - two innocent renames can create a loop together.
2415 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002416 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 * story.
2418 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002419 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 * whether the target exists). Solution: try to be smart with locking
2421 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002422 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 * move will be locked. Thus we can rank directories by the tree
2424 * (ancestors first) and rank all non-directories after them.
2425 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002426 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * HOWEVER, it relies on the assumption that any object with ->lookup()
2428 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2429 * we'd better make sure that there's no link(2) for them.
2430 * d) some filesystems don't support opened-but-unlinked directories,
2431 * either because of layout or because they are not ready to deal with
2432 * all cases correctly. The latter will be fixed (taking this sort of
2433 * stuff into VFS), but the former is not going away. Solution: the same
2434 * trick as in rmdir().
2435 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002436 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002438 * ->i_mutex on parents, which works but leads to some truely excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 * locking].
2440 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002441static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2442 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
2444 int error = 0;
2445 struct inode *target;
2446
2447 /*
2448 * If we are going to change the parent - check write permissions,
2449 * we'll need to flip '..'.
2450 */
2451 if (new_dir != old_dir) {
2452 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2453 if (error)
2454 return error;
2455 }
2456
2457 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2458 if (error)
2459 return error;
2460
2461 target = new_dentry->d_inode;
2462 if (target) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002463 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 dentry_unhash(new_dentry);
2465 }
2466 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2467 error = -EBUSY;
2468 else
2469 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2470 if (target) {
2471 if (!error)
2472 target->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002473 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (d_unhashed(new_dentry))
2475 d_rehash(new_dentry);
2476 dput(new_dentry);
2477 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002478 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002479 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2480 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return error;
2482}
2483
Adrian Bunk75c96f82005-05-05 16:16:09 -07002484static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2485 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 struct inode *target;
2488 int error;
2489
2490 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2491 if (error)
2492 return error;
2493
2494 dget(new_dentry);
2495 target = new_dentry->d_inode;
2496 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002497 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2499 error = -EBUSY;
2500 else
2501 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2502 if (!error) {
Mark Fasheh349457c2006-09-08 14:22:21 -07002503 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002507 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 dput(new_dentry);
2509 return error;
2510}
2511
2512int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2513 struct inode *new_dir, struct dentry *new_dentry)
2514{
2515 int error;
2516 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Robert Love0eeca282005-07-12 17:06:03 -04002517 const char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 if (old_dentry->d_inode == new_dentry->d_inode)
2520 return 0;
2521
2522 error = may_delete(old_dir, old_dentry, is_dir);
2523 if (error)
2524 return error;
2525
2526 if (!new_dentry->d_inode)
2527 error = may_create(new_dir, new_dentry, NULL);
2528 else
2529 error = may_delete(new_dir, new_dentry, is_dir);
2530 if (error)
2531 return error;
2532
2533 if (!old_dir->i_op || !old_dir->i_op->rename)
2534 return -EPERM;
2535
2536 DQUOT_INIT(old_dir);
2537 DQUOT_INIT(new_dir);
2538
Robert Love0eeca282005-07-12 17:06:03 -04002539 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (is_dir)
2542 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2543 else
2544 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2545 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -04002546 const char *new_name = old_dentry->d_name.name;
John McCutchan89204c42005-08-15 12:13:28 -04002547 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2548 new_dentry->d_inode, old_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
Robert Love0eeca282005-07-12 17:06:03 -04002550 fsnotify_oldname_free(old_name);
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return error;
2553}
2554
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002555static int do_rename(int olddfd, const char *oldname,
2556 int newdfd, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
2558 int error = 0;
2559 struct dentry * old_dir, * new_dir;
2560 struct dentry * old_dentry, *new_dentry;
2561 struct dentry * trap;
2562 struct nameidata oldnd, newnd;
2563
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002564 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (error)
2566 goto exit;
2567
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002568 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (error)
2570 goto exit1;
2571
2572 error = -EXDEV;
2573 if (oldnd.mnt != newnd.mnt)
2574 goto exit2;
2575
2576 old_dir = oldnd.dentry;
2577 error = -EBUSY;
2578 if (oldnd.last_type != LAST_NORM)
2579 goto exit2;
2580
2581 new_dir = newnd.dentry;
2582 if (newnd.last_type != LAST_NORM)
2583 goto exit2;
2584
2585 trap = lock_rename(new_dir, old_dir);
2586
Christoph Hellwig49705b72005-11-08 21:35:06 -08002587 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 error = PTR_ERR(old_dentry);
2589 if (IS_ERR(old_dentry))
2590 goto exit3;
2591 /* source must exist */
2592 error = -ENOENT;
2593 if (!old_dentry->d_inode)
2594 goto exit4;
2595 /* unless the source is a directory trailing slashes give -ENOTDIR */
2596 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2597 error = -ENOTDIR;
2598 if (oldnd.last.name[oldnd.last.len])
2599 goto exit4;
2600 if (newnd.last.name[newnd.last.len])
2601 goto exit4;
2602 }
2603 /* source should not be ancestor of target */
2604 error = -EINVAL;
2605 if (old_dentry == trap)
2606 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08002607 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 error = PTR_ERR(new_dentry);
2609 if (IS_ERR(new_dentry))
2610 goto exit4;
2611 /* target should not be an ancestor of source */
2612 error = -ENOTEMPTY;
2613 if (new_dentry == trap)
2614 goto exit5;
2615
2616 error = vfs_rename(old_dir->d_inode, old_dentry,
2617 new_dir->d_inode, new_dentry);
2618exit5:
2619 dput(new_dentry);
2620exit4:
2621 dput(old_dentry);
2622exit3:
2623 unlock_rename(new_dir, old_dir);
2624exit2:
2625 path_release(&newnd);
2626exit1:
2627 path_release(&oldnd);
2628exit:
2629 return error;
2630}
2631
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002632asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2633 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 int error;
2636 char * from;
2637 char * to;
2638
2639 from = getname(oldname);
2640 if(IS_ERR(from))
2641 return PTR_ERR(from);
2642 to = getname(newname);
2643 error = PTR_ERR(to);
2644 if (!IS_ERR(to)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002645 error = do_rename(olddfd, from, newdfd, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 putname(to);
2647 }
2648 putname(from);
2649 return error;
2650}
2651
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002652asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2653{
2654 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2655}
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2658{
2659 int len;
2660
2661 len = PTR_ERR(link);
2662 if (IS_ERR(link))
2663 goto out;
2664
2665 len = strlen(link);
2666 if (len > (unsigned) buflen)
2667 len = buflen;
2668 if (copy_to_user(buffer, link, len))
2669 len = -EFAULT;
2670out:
2671 return len;
2672}
2673
2674/*
2675 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2676 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2677 * using) it for any given inode is up to filesystem.
2678 */
2679int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2680{
2681 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002682 void *cookie;
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002685 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2686 if (!IS_ERR(cookie)) {
2687 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (dentry->d_inode->i_op->put_link)
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002689 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2690 cookie = ERR_PTR(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002692 return PTR_ERR(cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
2695int vfs_follow_link(struct nameidata *nd, const char *link)
2696{
2697 return __vfs_follow_link(nd, link);
2698}
2699
2700/* get the link contents into pagecache */
2701static char *page_getlink(struct dentry * dentry, struct page **ppage)
2702{
2703 struct page * page;
2704 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07002705 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07002707 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 *ppage = page;
2709 return kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
2712int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2713{
2714 struct page *page = NULL;
2715 char *s = page_getlink(dentry, &page);
2716 int res = vfs_readlink(dentry,buffer,buflen,s);
2717 if (page) {
2718 kunmap(page);
2719 page_cache_release(page);
2720 }
2721 return res;
2722}
2723
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002724void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002726 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002728 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002731void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002733 struct page *page = cookie;
2734
2735 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 kunmap(page);
2737 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 }
2739}
2740
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002741int __page_symlink(struct inode *inode, const char *symname, int len,
2742 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002745 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002746 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08002747 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 char *kaddr;
2749
NeilBrown7e53cac2006-03-25 03:07:57 -08002750retry:
Nick Pigginafddba42007-10-16 01:25:01 -07002751 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2752 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07002754 goto fail;
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 kaddr = kmap_atomic(page, KM_USER0);
2757 memcpy(kaddr, symname, len-1);
2758 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07002759
2760 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2761 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (err < 0)
2763 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07002764 if (err < len-1)
2765 goto retry;
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 mark_inode_dirty(inode);
2768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769fail:
2770 return err;
2771}
2772
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002773int page_symlink(struct inode *inode, const char *symname, int len)
2774{
2775 return __page_symlink(inode, symname, len,
2776 mapping_gfp_mask(inode->i_mapping));
2777}
2778
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002779const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 .readlink = generic_readlink,
2781 .follow_link = page_follow_link_light,
2782 .put_link = page_put_link,
2783};
2784
2785EXPORT_SYMBOL(__user_walk);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002786EXPORT_SYMBOL(__user_walk_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787EXPORT_SYMBOL(follow_down);
2788EXPORT_SYMBOL(follow_up);
2789EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2790EXPORT_SYMBOL(getname);
2791EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792EXPORT_SYMBOL(lookup_one_len);
2793EXPORT_SYMBOL(page_follow_link_light);
2794EXPORT_SYMBOL(page_put_link);
2795EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002796EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797EXPORT_SYMBOL(page_symlink);
2798EXPORT_SYMBOL(page_symlink_inode_operations);
2799EXPORT_SYMBOL(path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002800EXPORT_SYMBOL(vfs_path_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801EXPORT_SYMBOL(path_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802EXPORT_SYMBOL(permission);
Christoph Hellwige4543ed2005-11-08 21:35:04 -08002803EXPORT_SYMBOL(vfs_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08002804EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805EXPORT_SYMBOL(unlock_rename);
2806EXPORT_SYMBOL(vfs_create);
2807EXPORT_SYMBOL(vfs_follow_link);
2808EXPORT_SYMBOL(vfs_link);
2809EXPORT_SYMBOL(vfs_mkdir);
2810EXPORT_SYMBOL(vfs_mknod);
2811EXPORT_SYMBOL(generic_permission);
2812EXPORT_SYMBOL(vfs_readlink);
2813EXPORT_SYMBOL(vfs_rename);
2814EXPORT_SYMBOL(vfs_rmdir);
2815EXPORT_SYMBOL(vfs_symlink);
2816EXPORT_SYMBOL(vfs_unlink);
2817EXPORT_SYMBOL(dentry_unhash);
2818EXPORT_SYMBOL(generic_readlink);