blob: 1e5746eb13809777ca97c5cb4db2fc778008c30c [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/smp_lock.h>
26#include <linux/personality.h>
27#include <linux/security.h>
28#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/namei.h>
34#include <asm/uaccess.h>
35
36#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
37
38/* [Feb-1997 T. Schoebel-Theuer]
39 * Fundamental changes in the pathname lookup mechanisms (namei)
40 * were necessary because of omirr. The reason is that omirr needs
41 * to know the _real_ pathname, not the user-supplied one, in case
42 * of symlinks (and also when transname replacements occur).
43 *
44 * The new code replaces the old recursive symlink resolution with
45 * an iterative one (in case of non-nested symlink chains). It does
46 * this with calls to <fs>_follow_link().
47 * As a side effect, dir_namei(), _namei() and follow_link() are now
48 * replaced with a single function lookup_dentry() that can handle all
49 * the special cases of the former code.
50 *
51 * With the new dcache, the pathname is stored at each inode, at least as
52 * long as the refcount of the inode is positive. As a side effect, the
53 * size of the dcache depends on the inode cache and thus is dynamic.
54 *
55 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
56 * resolution to correspond with current state of the code.
57 *
58 * Note that the symlink resolution is not *completely* iterative.
59 * There is still a significant amount of tail- and mid- recursion in
60 * the algorithm. Also, note that <fs>_readlink() is not used in
61 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
62 * may return different results than <fs>_follow_link(). Many virtual
63 * filesystems (including /proc) exhibit this behavior.
64 */
65
66/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
67 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
68 * and the name already exists in form of a symlink, try to create the new
69 * name indicated by the symlink. The old code always complained that the
70 * name already exists, due to not following the symlink even if its target
71 * is nonexistent. The new semantics affects also mknod() and link() when
72 * the name is a symlink pointing to a non-existant name.
73 *
74 * I don't know which semantics is the right one, since I have no access
75 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
76 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
77 * "old" one. Personally, I think the new semantics is much more logical.
78 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
79 * file does succeed in both HP-UX and SunOs, but not in Solaris
80 * and in the old Linux semantics.
81 */
82
83/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
84 * semantics. See the comments in "open_namei" and "do_link" below.
85 *
86 * [10-Sep-98 Alan Modra] Another symlink change.
87 */
88
89/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
90 * inside the path - always follow.
91 * in the last component in creation/removal/renaming - never follow.
92 * if LOOKUP_FOLLOW passed - follow.
93 * if the pathname has trailing slashes - follow.
94 * otherwise - don't follow.
95 * (applied in that order).
96 *
97 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
98 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
99 * During the 2.4 we need to fix the userland stuff depending on it -
100 * hopefully we will be able to get rid of that wart in 2.5. So far only
101 * XEmacs seems to be relying on it...
102 */
103/*
104 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
105 * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives
106 * any extra contention...
107 */
108
109/* In order to reduce some races, while at the same time doing additional
110 * checking and hopefully speeding things up, we copy filenames to the
111 * kernel data space before using them..
112 *
113 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
114 * PATH_MAX includes the nul terminator --RR.
115 */
116static inline int do_getname(const char __user *filename, char *page)
117{
118 int retval;
119 unsigned long len = PATH_MAX;
120
121 if (!segment_eq(get_fs(), KERNEL_DS)) {
122 if ((unsigned long) filename >= TASK_SIZE)
123 return -EFAULT;
124 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
125 len = TASK_SIZE - (unsigned long) filename;
126 }
127
128 retval = strncpy_from_user(page, filename, len);
129 if (retval > 0) {
130 if (retval < len)
131 return 0;
132 return -ENAMETOOLONG;
133 } else if (!retval)
134 retval = -ENOENT;
135 return retval;
136}
137
138char * getname(const char __user * filename)
139{
140 char *tmp, *result;
141
142 result = ERR_PTR(-ENOMEM);
143 tmp = __getname();
144 if (tmp) {
145 int retval = do_getname(filename, tmp);
146
147 result = tmp;
148 if (retval < 0) {
149 __putname(tmp);
150 result = ERR_PTR(retval);
151 }
152 }
153 audit_getname(result);
154 return result;
155}
156
157#ifdef CONFIG_AUDITSYSCALL
158void putname(const char *name)
159{
160 if (unlikely(current->audit_context))
161 audit_putname(name);
162 else
163 __putname(name);
164}
165EXPORT_SYMBOL(putname);
166#endif
167
168
169/**
170 * generic_permission - check for access rights on a Posix-like filesystem
171 * @inode: inode to check access rights for
172 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
173 * @check_acl: optional callback to check for Posix ACLs
174 *
175 * Used to check for read/write/execute permissions on a file.
176 * We use "fsuid" for this, letting us set arbitrary permissions
177 * for filesystem access without changing the "normal" uids which
178 * are used for other things..
179 */
180int generic_permission(struct inode *inode, int mask,
181 int (*check_acl)(struct inode *inode, int mask))
182{
183 umode_t mode = inode->i_mode;
184
185 if (current->fsuid == inode->i_uid)
186 mode >>= 6;
187 else {
188 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
189 int error = check_acl(inode, mask);
190 if (error == -EACCES)
191 goto check_capabilities;
192 else if (error != -EAGAIN)
193 return error;
194 }
195
196 if (in_group_p(inode->i_gid))
197 mode >>= 3;
198 }
199
200 /*
201 * If the DACs are ok we don't need any capability check.
202 */
203 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
204 return 0;
205
206 check_capabilities:
207 /*
208 * Read/write DACs are always overridable.
209 * Executable DACs are overridable if at least one exec bit is set.
210 */
211 if (!(mask & MAY_EXEC) ||
212 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
213 if (capable(CAP_DAC_OVERRIDE))
214 return 0;
215
216 /*
217 * Searching includes executable on directories, else just read.
218 */
219 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
220 if (capable(CAP_DAC_READ_SEARCH))
221 return 0;
222
223 return -EACCES;
224}
225
226int permission(struct inode *inode, int mask, struct nameidata *nd)
227{
228 int retval, submask;
229
230 if (mask & MAY_WRITE) {
231 umode_t mode = inode->i_mode;
232
233 /*
234 * Nobody gets write access to a read-only fs.
235 */
236 if (IS_RDONLY(inode) &&
237 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
238 return -EROFS;
239
240 /*
241 * Nobody gets write access to an immutable file.
242 */
243 if (IS_IMMUTABLE(inode))
244 return -EACCES;
245 }
246
247
248 /* Ordinary permission routines do not understand MAY_APPEND. */
249 submask = mask & ~MAY_APPEND;
250 if (inode->i_op && inode->i_op->permission)
251 retval = inode->i_op->permission(inode, submask, nd);
252 else
253 retval = generic_permission(inode, submask, NULL);
254 if (retval)
255 return retval;
256
257 return security_inode_permission(inode, mask, nd);
258}
259
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800260/**
261 * vfs_permission - check for access rights to a given path
262 * @nd: lookup result that describes the path
263 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
264 *
265 * Used to check for read/write/execute permissions on a path.
266 * We use "fsuid" for this, letting us set arbitrary permissions
267 * for filesystem access without changing the "normal" uids which
268 * are used for other things.
269 */
270int vfs_permission(struct nameidata *nd, int mask)
271{
272 return permission(nd->dentry->d_inode, mask, nd);
273}
274
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800275/**
276 * file_permission - check for additional access rights to a given file
277 * @file: file to check access rights for
278 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
279 *
280 * Used to check for read/write/execute permissions on an already opened
281 * file.
282 *
283 * Note:
284 * Do not use this function in new code. All access checks should
285 * be done using vfs_permission().
286 */
287int file_permission(struct file *file, int mask)
288{
289 return permission(file->f_dentry->d_inode, mask, NULL);
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * get_write_access() gets write permission for a file.
294 * put_write_access() releases this write permission.
295 * This is used for regular files.
296 * We cannot support write (and maybe mmap read-write shared) accesses and
297 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
298 * can have the following values:
299 * 0: no writers, no VM_DENYWRITE mappings
300 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
301 * > 0: (i_writecount) users are writing to the file.
302 *
303 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
304 * except for the cases where we don't hold i_writecount yet. Then we need to
305 * use {get,deny}_write_access() - these functions check the sign and refuse
306 * to do the change if sign is wrong. Exclusion between them is provided by
307 * the inode->i_lock spinlock.
308 */
309
310int get_write_access(struct inode * inode)
311{
312 spin_lock(&inode->i_lock);
313 if (atomic_read(&inode->i_writecount) < 0) {
314 spin_unlock(&inode->i_lock);
315 return -ETXTBSY;
316 }
317 atomic_inc(&inode->i_writecount);
318 spin_unlock(&inode->i_lock);
319
320 return 0;
321}
322
323int deny_write_access(struct file * file)
324{
325 struct inode *inode = file->f_dentry->d_inode;
326
327 spin_lock(&inode->i_lock);
328 if (atomic_read(&inode->i_writecount) > 0) {
329 spin_unlock(&inode->i_lock);
330 return -ETXTBSY;
331 }
332 atomic_dec(&inode->i_writecount);
333 spin_unlock(&inode->i_lock);
334
335 return 0;
336}
337
338void path_release(struct nameidata *nd)
339{
340 dput(nd->dentry);
341 mntput(nd->mnt);
342}
343
344/*
345 * umount() mustn't call path_release()/mntput() as that would clear
346 * mnt_expiry_mark
347 */
348void path_release_on_umount(struct nameidata *nd)
349{
350 dput(nd->dentry);
Miklos Szeredi751c4042005-07-07 17:57:30 -0700351 mntput_no_expire(nd->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Trond Myklebust834f2a42005-10-18 14:20:16 -0700354/**
355 * release_open_intent - free up open intent resources
356 * @nd: pointer to nameidata
357 */
358void release_open_intent(struct nameidata *nd)
359{
360 if (nd->intent.open.file->f_dentry == NULL)
361 put_filp(nd->intent.open.file);
362 else
363 fput(nd->intent.open.file);
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * Internal lookup() using the new generic dcache.
368 * SMP-safe
369 */
370static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
371{
372 struct dentry * dentry = __d_lookup(parent, name);
373
374 /* lockess __d_lookup may fail due to concurrent d_move()
375 * in some unrelated directory, so try with d_lookup
376 */
377 if (!dentry)
378 dentry = d_lookup(parent, name);
379
380 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
381 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
382 dput(dentry);
383 dentry = NULL;
384 }
385 }
386 return dentry;
387}
388
389/*
390 * Short-cut version of permission(), for calling by
391 * path_walk(), when dcache lock is held. Combines parts
392 * of permission() and generic_permission(), and tests ONLY for
393 * MAY_EXEC permission.
394 *
395 * If appropriate, check DAC only. If not appropriate, or
396 * short-cut DAC fails, then call permission() to do more
397 * complete permission check.
398 */
399static inline int exec_permission_lite(struct inode *inode,
400 struct nameidata *nd)
401{
402 umode_t mode = inode->i_mode;
403
404 if (inode->i_op && inode->i_op->permission)
405 return -EAGAIN;
406
407 if (current->fsuid == inode->i_uid)
408 mode >>= 6;
409 else if (in_group_p(inode->i_gid))
410 mode >>= 3;
411
412 if (mode & MAY_EXEC)
413 goto ok;
414
415 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
416 goto ok;
417
418 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
419 goto ok;
420
421 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
422 goto ok;
423
424 return -EACCES;
425ok:
426 return security_inode_permission(inode, MAY_EXEC, nd);
427}
428
429/*
430 * This is called when everything else fails, and we actually have
431 * to go to the low-level filesystem to find out what we should do..
432 *
433 * We get the directory semaphore, and after getting that we also
434 * make sure that nobody added the entry to the dcache in the meantime..
435 * SMP-safe
436 */
437static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
438{
439 struct dentry * result;
440 struct inode *dir = parent->d_inode;
441
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800442 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
444 * First re-do the cached lookup just in case it was created
445 * while we waited for the directory semaphore..
446 *
447 * FIXME! This could use version numbering or similar to
448 * avoid unnecessary cache lookups.
449 *
450 * The "dcache_lock" is purely to protect the RCU list walker
451 * from concurrent renames at this point (we mustn't get false
452 * negatives from the RCU list walk here, unlike the optimistic
453 * fast walk).
454 *
455 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
456 */
457 result = d_lookup(parent, name);
458 if (!result) {
459 struct dentry * dentry = d_alloc(parent, name);
460 result = ERR_PTR(-ENOMEM);
461 if (dentry) {
462 result = dir->i_op->lookup(dir, dentry, nd);
463 if (result)
464 dput(dentry);
465 else
466 result = dentry;
467 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800468 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return result;
470 }
471
472 /*
473 * Uhhuh! Nasty case: the cache was re-populated while
474 * we waited on the semaphore. Need to revalidate.
475 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800476 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (result->d_op && result->d_op->d_revalidate) {
478 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
479 dput(result);
480 result = ERR_PTR(-ENOENT);
481 }
482 }
483 return result;
484}
485
486static int __emul_lookup_dentry(const char *, struct nameidata *);
487
488/* SMP-safe */
489static inline int
490walk_init_root(const char *name, struct nameidata *nd)
491{
492 read_lock(&current->fs->lock);
493 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
494 nd->mnt = mntget(current->fs->altrootmnt);
495 nd->dentry = dget(current->fs->altroot);
496 read_unlock(&current->fs->lock);
497 if (__emul_lookup_dentry(name,nd))
498 return 0;
499 read_lock(&current->fs->lock);
500 }
501 nd->mnt = mntget(current->fs->rootmnt);
502 nd->dentry = dget(current->fs->root);
503 read_unlock(&current->fs->lock);
504 return 1;
505}
506
507static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
508{
509 int res = 0;
510 char *name;
511 if (IS_ERR(link))
512 goto fail;
513
514 if (*link == '/') {
515 path_release(nd);
516 if (!walk_init_root(link, nd))
517 /* weird __emul_prefix() stuff did it */
518 goto out;
519 }
520 res = link_path_walk(link, nd);
521out:
522 if (nd->depth || res || nd->last_type!=LAST_NORM)
523 return res;
524 /*
525 * If it is an iterative symlinks resolution in open_namei() we
526 * have to copy the last component. And all that crap because of
527 * bloody create() on broken symlinks. Furrfu...
528 */
529 name = __getname();
530 if (unlikely(!name)) {
531 path_release(nd);
532 return -ENOMEM;
533 }
534 strcpy(name, nd->last.name);
535 nd->last.name = name;
536 return 0;
537fail:
538 path_release(nd);
539 return PTR_ERR(link);
540}
541
Al Viro90ebe562005-06-06 13:35:58 -0700542struct path {
543 struct vfsmount *mnt;
544 struct dentry *dentry;
545};
546
Al Virocd4e91d2005-06-06 13:36:03 -0700547static inline int __do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 int error;
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700550 void *cookie;
Al Virocd4e91d2005-06-06 13:36:03 -0700551 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Al Virod671a1c2005-06-06 13:36:14 -0700553 touch_atime(path->mnt, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 nd_set_link(nd, NULL);
Al Virocd4e91d2005-06-06 13:36:03 -0700555
Al Viro39ca6d42005-06-06 13:36:12 -0700556 if (path->mnt == nd->mnt)
557 mntget(path->mnt);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700558 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
559 error = PTR_ERR(cookie);
560 if (!IS_ERR(cookie)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 char *s = nd_get_link(nd);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700562 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (s)
564 error = __vfs_follow_link(nd, s);
565 if (dentry->d_inode->i_op->put_link)
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700566 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Al Virocd4e91d2005-06-06 13:36:03 -0700568 dput(dentry);
569 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 return error;
572}
573
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700574static inline void dput_path(struct path *path, struct nameidata *nd)
575{
576 dput(path->dentry);
577 if (path->mnt != nd->mnt)
578 mntput(path->mnt);
579}
580
581static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
582{
583 dput(nd->dentry);
584 if (nd->mnt != path->mnt)
585 mntput(nd->mnt);
586 nd->mnt = path->mnt;
587 nd->dentry = path->dentry;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/*
591 * This limits recursive symlink follows to 8, while
592 * limiting consecutive symlinks to 40.
593 *
594 * Without that kind of total limit, nasty chains of consecutive
595 * symlinks can cause almost arbitrarily long lookups.
596 */
Al Viro90ebe562005-06-06 13:35:58 -0700597static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 int err = -ELOOP;
600 if (current->link_count >= MAX_NESTED_LINKS)
601 goto loop;
602 if (current->total_link_count >= 40)
603 goto loop;
604 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
605 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700606 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (err)
608 goto loop;
609 current->link_count++;
610 current->total_link_count++;
611 nd->depth++;
Al Virocd4e91d2005-06-06 13:36:03 -0700612 err = __do_follow_link(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 current->link_count--;
614 nd->depth--;
615 return err;
616loop:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700617 dput_path(path, nd);
Al Viro839d9f92005-06-06 13:36:02 -0700618 path_release(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return err;
620}
621
622int follow_up(struct vfsmount **mnt, struct dentry **dentry)
623{
624 struct vfsmount *parent;
625 struct dentry *mountpoint;
626 spin_lock(&vfsmount_lock);
627 parent=(*mnt)->mnt_parent;
628 if (parent == *mnt) {
629 spin_unlock(&vfsmount_lock);
630 return 0;
631 }
632 mntget(parent);
633 mountpoint=dget((*mnt)->mnt_mountpoint);
634 spin_unlock(&vfsmount_lock);
635 dput(*dentry);
636 *dentry = mountpoint;
637 mntput(*mnt);
638 *mnt = parent;
639 return 1;
640}
641
642/* no need for dcache_lock, as serialization is taken care in
643 * namespace.c
644 */
Al Viro463ffb22005-06-06 13:36:05 -0700645static int __follow_mount(struct path *path)
646{
647 int res = 0;
648 while (d_mountpoint(path->dentry)) {
649 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
650 if (!mounted)
651 break;
652 dput(path->dentry);
653 if (res)
654 mntput(path->mnt);
655 path->mnt = mounted;
656 path->dentry = dget(mounted->mnt_root);
657 res = 1;
658 }
659 return res;
660}
661
Al Viro58c465e2005-06-06 13:36:13 -0700662static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 while (d_mountpoint(*dentry)) {
665 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
666 if (!mounted)
667 break;
Al Viro58c465e2005-06-06 13:36:13 -0700668 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 mntput(*mnt);
670 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/* no need for dcache_lock, as serialization is taken care in
676 * namespace.c
677 */
Al Viroe13b2102005-06-06 13:36:06 -0700678int follow_down(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct vfsmount *mounted;
681
682 mounted = lookup_mnt(*mnt, *dentry);
683 if (mounted) {
Al Viroe13b2102005-06-06 13:36:06 -0700684 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 mntput(*mnt);
686 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 *dentry = dget(mounted->mnt_root);
688 return 1;
689 }
690 return 0;
691}
692
Al Viro58c465e2005-06-06 13:36:13 -0700693static inline void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 while(1) {
696 struct vfsmount *parent;
Al Viro58c465e2005-06-06 13:36:13 -0700697 struct dentry *old = nd->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 read_lock(&current->fs->lock);
Al Viro58c465e2005-06-06 13:36:13 -0700700 if (nd->dentry == current->fs->root &&
701 nd->mnt == current->fs->rootmnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 read_unlock(&current->fs->lock);
703 break;
704 }
705 read_unlock(&current->fs->lock);
706 spin_lock(&dcache_lock);
Al Viro58c465e2005-06-06 13:36:13 -0700707 if (nd->dentry != nd->mnt->mnt_root) {
708 nd->dentry = dget(nd->dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 spin_unlock(&dcache_lock);
710 dput(old);
711 break;
712 }
713 spin_unlock(&dcache_lock);
714 spin_lock(&vfsmount_lock);
Al Viro58c465e2005-06-06 13:36:13 -0700715 parent = nd->mnt->mnt_parent;
716 if (parent == nd->mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 spin_unlock(&vfsmount_lock);
718 break;
719 }
720 mntget(parent);
Al Viro58c465e2005-06-06 13:36:13 -0700721 nd->dentry = dget(nd->mnt->mnt_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 spin_unlock(&vfsmount_lock);
723 dput(old);
Al Viro58c465e2005-06-06 13:36:13 -0700724 mntput(nd->mnt);
725 nd->mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Al Viro58c465e2005-06-06 13:36:13 -0700727 follow_mount(&nd->mnt, &nd->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/*
731 * It's more convoluted than I'd like it to be, but... it's still fairly
732 * small and for now I'd prefer to have fast path as straight as possible.
733 * It _is_ time-critical.
734 */
735static int do_lookup(struct nameidata *nd, struct qstr *name,
736 struct path *path)
737{
738 struct vfsmount *mnt = nd->mnt;
739 struct dentry *dentry = __d_lookup(nd->dentry, name);
740
741 if (!dentry)
742 goto need_lookup;
743 if (dentry->d_op && dentry->d_op->d_revalidate)
744 goto need_revalidate;
745done:
746 path->mnt = mnt;
747 path->dentry = dentry;
Al Viro634ee702005-06-06 13:36:13 -0700748 __follow_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
750
751need_lookup:
752 dentry = real_lookup(nd->dentry, name, nd);
753 if (IS_ERR(dentry))
754 goto fail;
755 goto done;
756
757need_revalidate:
758 if (dentry->d_op->d_revalidate(dentry, nd))
759 goto done;
760 if (d_invalidate(dentry))
761 goto done;
762 dput(dentry);
763 goto need_lookup;
764
765fail:
766 return PTR_ERR(dentry);
767}
768
769/*
770 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +0100771 * This is the basic name resolution function, turning a pathname into
772 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
Prasanna Medaea3834d2005-04-29 16:00:17 +0100774 * Returns 0 and nd will have valid dentry and mnt on success.
775 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
777static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
778{
779 struct path next;
780 struct inode *inode;
781 int err;
782 unsigned int lookup_flags = nd->flags;
783
784 while (*name=='/')
785 name++;
786 if (!*name)
787 goto return_reval;
788
789 inode = nd->dentry->d_inode;
790 if (nd->depth)
791 lookup_flags = LOOKUP_FOLLOW;
792
793 /* At this point we know we have a real path component. */
794 for(;;) {
795 unsigned long hash;
796 struct qstr this;
797 unsigned int c;
798
Trond Myklebustcdce5d62005-10-18 14:20:18 -0700799 nd->flags |= LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 err = exec_permission_lite(inode, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800801 if (err == -EAGAIN)
802 err = vfs_permission(nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (err)
804 break;
805
806 this.name = name;
807 c = *(const unsigned char *)name;
808
809 hash = init_name_hash();
810 do {
811 name++;
812 hash = partial_name_hash(c, hash);
813 c = *(const unsigned char *)name;
814 } while (c && (c != '/'));
815 this.len = name - (const char *) this.name;
816 this.hash = end_name_hash(hash);
817
818 /* remove trailing slashes? */
819 if (!c)
820 goto last_component;
821 while (*++name == '/');
822 if (!*name)
823 goto last_with_slashes;
824
825 /*
826 * "." and ".." are special - ".." especially so because it has
827 * to be able to know about the current root directory and
828 * parent relationships.
829 */
830 if (this.name[0] == '.') switch (this.len) {
831 default:
832 break;
833 case 2:
834 if (this.name[1] != '.')
835 break;
Al Viro58c465e2005-06-06 13:36:13 -0700836 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 inode = nd->dentry->d_inode;
838 /* fallthrough */
839 case 1:
840 continue;
841 }
842 /*
843 * See if the low-level filesystem might want
844 * to use its own hash..
845 */
846 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
847 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
848 if (err < 0)
849 break;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* This does the actual lookups.. */
852 err = do_lookup(nd, &this, &next);
853 if (err)
854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 err = -ENOENT;
857 inode = next.dentry->d_inode;
858 if (!inode)
859 goto out_dput;
860 err = -ENOTDIR;
861 if (!inode->i_op)
862 goto out_dput;
863
864 if (inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700865 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (err)
867 goto return_err;
868 err = -ENOENT;
869 inode = nd->dentry->d_inode;
870 if (!inode)
871 break;
872 err = -ENOTDIR;
873 if (!inode->i_op)
874 break;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700875 } else
876 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 err = -ENOTDIR;
878 if (!inode->i_op->lookup)
879 break;
880 continue;
881 /* here ends the main loop */
882
883last_with_slashes:
884 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
885last_component:
886 nd->flags &= ~LOOKUP_CONTINUE;
887 if (lookup_flags & LOOKUP_PARENT)
888 goto lookup_parent;
889 if (this.name[0] == '.') switch (this.len) {
890 default:
891 break;
892 case 2:
893 if (this.name[1] != '.')
894 break;
Al Viro58c465e2005-06-06 13:36:13 -0700895 follow_dotdot(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 inode = nd->dentry->d_inode;
897 /* fallthrough */
898 case 1:
899 goto return_reval;
900 }
901 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
902 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
903 if (err < 0)
904 break;
905 }
906 err = do_lookup(nd, &this, &next);
907 if (err)
908 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 inode = next.dentry->d_inode;
910 if ((lookup_flags & LOOKUP_FOLLOW)
911 && inode && inode->i_op && inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700912 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (err)
914 goto return_err;
915 inode = nd->dentry->d_inode;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700916 } else
917 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 err = -ENOENT;
919 if (!inode)
920 break;
921 if (lookup_flags & LOOKUP_DIRECTORY) {
922 err = -ENOTDIR;
923 if (!inode->i_op || !inode->i_op->lookup)
924 break;
925 }
926 goto return_base;
927lookup_parent:
928 nd->last = this;
929 nd->last_type = LAST_NORM;
930 if (this.name[0] != '.')
931 goto return_base;
932 if (this.len == 1)
933 nd->last_type = LAST_DOT;
934 else if (this.len == 2 && this.name[1] == '.')
935 nd->last_type = LAST_DOTDOT;
936 else
937 goto return_base;
938return_reval:
939 /*
940 * We bypassed the ordinary revalidation routines.
941 * We may need to check the cached dentry for staleness.
942 */
943 if (nd->dentry && nd->dentry->d_sb &&
944 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
945 err = -ESTALE;
946 /* Note: we do not d_invalidate() */
947 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
948 break;
949 }
950return_base:
951 return 0;
952out_dput:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700953 dput_path(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 break;
955 }
956 path_release(nd);
957return_err:
958 return err;
959}
960
961/*
962 * Wrapper to retry pathname resolution whenever the underlying
963 * file system returns an ESTALE.
964 *
965 * Retry the whole path once, forcing real lookup requests
966 * instead of relying on the dcache.
967 */
968int fastcall link_path_walk(const char *name, struct nameidata *nd)
969{
970 struct nameidata save = *nd;
971 int result;
972
973 /* make sure the stuff we saved doesn't go away */
974 dget(save.dentry);
975 mntget(save.mnt);
976
977 result = __link_path_walk(name, nd);
978 if (result == -ESTALE) {
979 *nd = save;
980 dget(nd->dentry);
981 mntget(nd->mnt);
982 nd->flags |= LOOKUP_REVAL;
983 result = __link_path_walk(name, nd);
984 }
985
986 dput(save.dentry);
987 mntput(save.mnt);
988
989 return result;
990}
991
992int fastcall path_walk(const char * name, struct nameidata *nd)
993{
994 current->total_link_count = 0;
995 return link_path_walk(name, nd);
996}
997
Prasanna Medaea3834d2005-04-29 16:00:17 +0100998/*
999 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1000 * everything is done. Returns 0 and drops input nd, if lookup failed;
1001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1003{
1004 if (path_walk(name, nd))
1005 return 0; /* something went wrong... */
1006
1007 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
1008 struct dentry *old_dentry = nd->dentry;
1009 struct vfsmount *old_mnt = nd->mnt;
1010 struct qstr last = nd->last;
1011 int last_type = nd->last_type;
1012 /*
1013 * NAME was not found in alternate root or it's a directory. Try to find
1014 * it in the normal root:
1015 */
1016 nd->last_type = LAST_ROOT;
1017 read_lock(&current->fs->lock);
1018 nd->mnt = mntget(current->fs->rootmnt);
1019 nd->dentry = dget(current->fs->root);
1020 read_unlock(&current->fs->lock);
1021 if (path_walk(name, nd) == 0) {
1022 if (nd->dentry->d_inode) {
1023 dput(old_dentry);
1024 mntput(old_mnt);
1025 return 1;
1026 }
1027 path_release(nd);
1028 }
1029 nd->dentry = old_dentry;
1030 nd->mnt = old_mnt;
1031 nd->last = last;
1032 nd->last_type = last_type;
1033 }
1034 return 1;
1035}
1036
1037void set_fs_altroot(void)
1038{
1039 char *emul = __emul_prefix();
1040 struct nameidata nd;
1041 struct vfsmount *mnt = NULL, *oldmnt;
1042 struct dentry *dentry = NULL, *olddentry;
1043 int err;
1044
1045 if (!emul)
1046 goto set_it;
1047 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
1048 if (!err) {
1049 mnt = nd.mnt;
1050 dentry = nd.dentry;
1051 }
1052set_it:
1053 write_lock(&current->fs->lock);
1054 oldmnt = current->fs->altrootmnt;
1055 olddentry = current->fs->altroot;
1056 current->fs->altrootmnt = mnt;
1057 current->fs->altroot = dentry;
1058 write_unlock(&current->fs->lock);
1059 if (olddentry) {
1060 dput(olddentry);
1061 mntput(oldmnt);
1062 }
1063}
1064
Prasanna Medaea3834d2005-04-29 16:00:17 +01001065/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1067{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001068 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1071 nd->flags = flags;
1072 nd->depth = 0;
1073
1074 read_lock(&current->fs->lock);
1075 if (*name=='/') {
1076 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1077 nd->mnt = mntget(current->fs->altrootmnt);
1078 nd->dentry = dget(current->fs->altroot);
1079 read_unlock(&current->fs->lock);
1080 if (__emul_lookup_dentry(name,nd))
Prasanna Medaea3834d2005-04-29 16:00:17 +01001081 goto out; /* found in altroot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 read_lock(&current->fs->lock);
1083 }
1084 nd->mnt = mntget(current->fs->rootmnt);
1085 nd->dentry = dget(current->fs->root);
1086 } else {
1087 nd->mnt = mntget(current->fs->pwdmnt);
1088 nd->dentry = dget(current->fs->pwd);
1089 }
1090 read_unlock(&current->fs->lock);
1091 current->total_link_count = 0;
1092 retval = link_path_walk(name, nd);
Prasanna Medaea3834d2005-04-29 16:00:17 +01001093out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (unlikely(current->audit_context
1095 && nd && nd->dentry && nd->dentry->d_inode))
David Woodhouseae7b9612005-06-20 16:11:05 +01001096 audit_inode(name, nd->dentry->d_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return retval;
1098}
1099
Trond Myklebust834f2a42005-10-18 14:20:16 -07001100static int __path_lookup_intent_open(const char *name, unsigned int lookup_flags,
1101 struct nameidata *nd, int open_flags, int create_mode)
1102{
1103 struct file *filp = get_empty_filp();
1104 int err;
1105
1106 if (filp == NULL)
1107 return -ENFILE;
1108 nd->intent.open.file = filp;
1109 nd->intent.open.flags = open_flags;
1110 nd->intent.open.create_mode = create_mode;
1111 err = path_lookup(name, lookup_flags|LOOKUP_OPEN, nd);
1112 if (IS_ERR(nd->intent.open.file)) {
1113 if (err == 0) {
1114 err = PTR_ERR(nd->intent.open.file);
1115 path_release(nd);
1116 }
1117 } else if (err != 0)
1118 release_open_intent(nd);
1119 return err;
1120}
1121
1122/**
1123 * path_lookup_open - lookup a file path with open intent
1124 * @name: pointer to file name
1125 * @lookup_flags: lookup intent flags
1126 * @nd: pointer to nameidata
1127 * @open_flags: open intent flags
1128 */
1129int path_lookup_open(const char *name, unsigned int lookup_flags,
1130 struct nameidata *nd, int open_flags)
1131{
1132 return __path_lookup_intent_open(name, lookup_flags, nd,
1133 open_flags, 0);
1134}
1135
1136/**
1137 * path_lookup_create - lookup a file path with open + create intent
1138 * @name: pointer to file name
1139 * @lookup_flags: lookup intent flags
1140 * @nd: pointer to nameidata
1141 * @open_flags: open intent flags
1142 * @create_mode: create intent flags
1143 */
Adrian Bunk0ce6e622005-11-08 21:39:46 -08001144static int path_lookup_create(const char *name, unsigned int lookup_flags,
1145 struct nameidata *nd, int open_flags,
1146 int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001147{
1148 return __path_lookup_intent_open(name, lookup_flags|LOOKUP_CREATE, nd,
1149 open_flags, create_mode);
1150}
1151
1152int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1153 struct nameidata *nd, int open_flags)
1154{
1155 char *tmp = getname(name);
1156 int err = PTR_ERR(tmp);
1157
1158 if (!IS_ERR(tmp)) {
1159 err = __path_lookup_intent_open(tmp, lookup_flags, nd, open_flags, 0);
1160 putname(tmp);
1161 }
1162 return err;
1163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/*
1166 * Restricted form of lookup. Doesn't follow links, single-component only,
1167 * needs parent already locked. Doesn't follow mounts.
1168 * SMP-safe.
1169 */
1170static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1171{
1172 struct dentry * dentry;
1173 struct inode *inode;
1174 int err;
1175
1176 inode = base->d_inode;
1177 err = permission(inode, MAY_EXEC, nd);
1178 dentry = ERR_PTR(err);
1179 if (err)
1180 goto out;
1181
1182 /*
1183 * See if the low-level filesystem might want
1184 * to use its own hash..
1185 */
1186 if (base->d_op && base->d_op->d_hash) {
1187 err = base->d_op->d_hash(base, name);
1188 dentry = ERR_PTR(err);
1189 if (err < 0)
1190 goto out;
1191 }
1192
1193 dentry = cached_lookup(base, name, nd);
1194 if (!dentry) {
1195 struct dentry *new = d_alloc(base, name);
1196 dentry = ERR_PTR(-ENOMEM);
1197 if (!new)
1198 goto out;
1199 dentry = inode->i_op->lookup(inode, new, nd);
1200 if (!dentry)
1201 dentry = new;
1202 else
1203 dput(new);
1204 }
1205out:
1206 return dentry;
1207}
1208
Christoph Hellwig49705b72005-11-08 21:35:06 -08001209struct dentry * lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Christoph Hellwig49705b72005-11-08 21:35:06 -08001211 return __lookup_hash(&nd->last, nd->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214/* SMP-safe */
1215struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1216{
1217 unsigned long hash;
1218 struct qstr this;
1219 unsigned int c;
1220
1221 this.name = name;
1222 this.len = len;
1223 if (!len)
1224 goto access;
1225
1226 hash = init_name_hash();
1227 while (len--) {
1228 c = *(const unsigned char *)name++;
1229 if (c == '/' || c == '\0')
1230 goto access;
1231 hash = partial_name_hash(c, hash);
1232 }
1233 this.hash = end_name_hash(hash);
1234
Christoph Hellwig49705b72005-11-08 21:35:06 -08001235 return __lookup_hash(&this, base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236access:
1237 return ERR_PTR(-EACCES);
1238}
1239
1240/*
1241 * namei()
1242 *
1243 * is used by most simple commands to get the inode of a specified name.
1244 * Open, link etc use their own routines, but this is enough for things
1245 * like 'chmod' etc.
1246 *
1247 * namei exists in two versions: namei/lnamei. The only difference is
1248 * that namei follows links, while lnamei does not.
1249 * SMP-safe
1250 */
1251int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1252{
1253 char *tmp = getname(name);
1254 int err = PTR_ERR(tmp);
1255
1256 if (!IS_ERR(tmp)) {
1257 err = path_lookup(tmp, flags, nd);
1258 putname(tmp);
1259 }
1260 return err;
1261}
1262
1263/*
1264 * It's inline, so penalty for filesystems that don't use sticky bit is
1265 * minimal.
1266 */
1267static inline int check_sticky(struct inode *dir, struct inode *inode)
1268{
1269 if (!(dir->i_mode & S_ISVTX))
1270 return 0;
1271 if (inode->i_uid == current->fsuid)
1272 return 0;
1273 if (dir->i_uid == current->fsuid)
1274 return 0;
1275 return !capable(CAP_FOWNER);
1276}
1277
1278/*
1279 * Check whether we can remove a link victim from directory dir, check
1280 * whether the type of victim is right.
1281 * 1. We can't do it if dir is read-only (done in permission())
1282 * 2. We should have write and exec permissions on dir
1283 * 3. We can't remove anything from append-only dir
1284 * 4. We can't do anything with immutable dir (done in permission())
1285 * 5. If the sticky bit on dir is set we should either
1286 * a. be owner of dir, or
1287 * b. be owner of victim, or
1288 * c. have CAP_FOWNER capability
1289 * 6. If the victim is append-only or immutable we can't do antyhing with
1290 * links pointing to it.
1291 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1292 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1293 * 9. We can't remove a root or mountpoint.
1294 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1295 * nfs_async_unlink().
1296 */
1297static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1298{
1299 int error;
1300
1301 if (!victim->d_inode)
1302 return -ENOENT;
1303
1304 BUG_ON(victim->d_parent->d_inode != dir);
1305
1306 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1307 if (error)
1308 return error;
1309 if (IS_APPEND(dir))
1310 return -EPERM;
1311 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1312 IS_IMMUTABLE(victim->d_inode))
1313 return -EPERM;
1314 if (isdir) {
1315 if (!S_ISDIR(victim->d_inode->i_mode))
1316 return -ENOTDIR;
1317 if (IS_ROOT(victim))
1318 return -EBUSY;
1319 } else if (S_ISDIR(victim->d_inode->i_mode))
1320 return -EISDIR;
1321 if (IS_DEADDIR(dir))
1322 return -ENOENT;
1323 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1324 return -EBUSY;
1325 return 0;
1326}
1327
1328/* Check whether we can create an object with dentry child in directory
1329 * dir.
1330 * 1. We can't do it if child already exists (open has special treatment for
1331 * this case, but since we are inlined it's OK)
1332 * 2. We can't do it if dir is read-only (done in permission())
1333 * 3. We should have write and exec permissions on dir
1334 * 4. We can't do it if dir is immutable (done in permission())
1335 */
1336static inline int may_create(struct inode *dir, struct dentry *child,
1337 struct nameidata *nd)
1338{
1339 if (child->d_inode)
1340 return -EEXIST;
1341 if (IS_DEADDIR(dir))
1342 return -ENOENT;
1343 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1344}
1345
1346/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * O_DIRECTORY translates into forcing a directory lookup.
1348 */
1349static inline int lookup_flags(unsigned int f)
1350{
1351 unsigned long retval = LOOKUP_FOLLOW;
1352
1353 if (f & O_NOFOLLOW)
1354 retval &= ~LOOKUP_FOLLOW;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (f & O_DIRECTORY)
1357 retval |= LOOKUP_DIRECTORY;
1358
1359 return retval;
1360}
1361
1362/*
1363 * p1 and p2 should be directories on the same fs.
1364 */
1365struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1366{
1367 struct dentry *p;
1368
1369 if (p1 == p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001370 mutex_lock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return NULL;
1372 }
1373
1374 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1375
1376 for (p = p1; p->d_parent != p; p = p->d_parent) {
1377 if (p->d_parent == p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001378 mutex_lock(&p2->d_inode->i_mutex);
1379 mutex_lock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return p;
1381 }
1382 }
1383
1384 for (p = p2; p->d_parent != p; p = p->d_parent) {
1385 if (p->d_parent == p1) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001386 mutex_lock(&p1->d_inode->i_mutex);
1387 mutex_lock(&p2->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return p;
1389 }
1390 }
1391
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001392 mutex_lock(&p1->d_inode->i_mutex);
1393 mutex_lock(&p2->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return NULL;
1395}
1396
1397void unlock_rename(struct dentry *p1, struct dentry *p2)
1398{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001399 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001401 mutex_unlock(&p2->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1403 }
1404}
1405
1406int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1407 struct nameidata *nd)
1408{
1409 int error = may_create(dir, dentry, nd);
1410
1411 if (error)
1412 return error;
1413
1414 if (!dir->i_op || !dir->i_op->create)
1415 return -EACCES; /* shouldn't it be ENOSYS? */
1416 mode &= S_IALLUGO;
1417 mode |= S_IFREG;
1418 error = security_inode_create(dir, dentry, mode);
1419 if (error)
1420 return error;
1421 DQUOT_INIT(dir);
1422 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001423 if (!error)
Robert Love0eeca282005-07-12 17:06:03 -04001424 fsnotify_create(dir, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return error;
1426}
1427
1428int may_open(struct nameidata *nd, int acc_mode, int flag)
1429{
1430 struct dentry *dentry = nd->dentry;
1431 struct inode *inode = dentry->d_inode;
1432 int error;
1433
1434 if (!inode)
1435 return -ENOENT;
1436
1437 if (S_ISLNK(inode->i_mode))
1438 return -ELOOP;
1439
1440 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1441 return -EISDIR;
1442
Christoph Hellwige4543ed2005-11-08 21:35:04 -08001443 error = vfs_permission(nd, acc_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (error)
1445 return error;
1446
1447 /*
1448 * FIFO's, sockets and device files are special: they don't
1449 * actually live on the filesystem itself, and as such you
1450 * can write to them even if the filesystem is read-only.
1451 */
1452 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1453 flag &= ~O_TRUNC;
1454 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1455 if (nd->mnt->mnt_flags & MNT_NODEV)
1456 return -EACCES;
1457
1458 flag &= ~O_TRUNC;
1459 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1460 return -EROFS;
1461 /*
1462 * An append-only file must be opened in append mode for writing.
1463 */
1464 if (IS_APPEND(inode)) {
1465 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1466 return -EPERM;
1467 if (flag & O_TRUNC)
1468 return -EPERM;
1469 }
1470
1471 /* O_NOATIME can only be set by the owner or superuser */
1472 if (flag & O_NOATIME)
1473 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1474 return -EPERM;
1475
1476 /*
1477 * Ensure there are no outstanding leases on the file.
1478 */
1479 error = break_lease(inode, flag);
1480 if (error)
1481 return error;
1482
1483 if (flag & O_TRUNC) {
1484 error = get_write_access(inode);
1485 if (error)
1486 return error;
1487
1488 /*
1489 * Refuse to truncate files with mandatory locks held on them.
1490 */
1491 error = locks_verify_locked(inode);
1492 if (!error) {
1493 DQUOT_INIT(inode);
1494
NeilBrown4a301312006-01-08 01:02:39 -08001495 error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 put_write_access(inode);
1498 if (error)
1499 return error;
1500 } else
1501 if (flag & FMODE_WRITE)
1502 DQUOT_INIT(inode);
1503
1504 return 0;
1505}
1506
1507/*
1508 * open_namei()
1509 *
1510 * namei for open - this is in fact almost the whole open-routine.
1511 *
1512 * Note that the low bits of "flag" aren't the same as in the open
1513 * system call - they are 00 - no permissions needed
1514 * 01 - read permission needed
1515 * 10 - write permission needed
1516 * 11 - read/write permissions needed
1517 * which is a lot more logical, and also allows the "no perm" needed
1518 * for symlinks (where the permissions are checked later).
1519 * SMP-safe
1520 */
1521int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1522{
Trond Myklebust834f2a42005-10-18 14:20:16 -07001523 int acc_mode, error;
Al Viro4e7506e2005-06-06 13:36:00 -07001524 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 struct dentry *dir;
1526 int count = 0;
1527
1528 acc_mode = ACC_MODE(flag);
1529
Trond Myklebust834f2a42005-10-18 14:20:16 -07001530 /* O_TRUNC implies we need access checks for write permissions */
1531 if (flag & O_TRUNC)
1532 acc_mode |= MAY_WRITE;
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* Allow the LSM permission hook to distinguish append
1535 access from general write access. */
1536 if (flag & O_APPEND)
1537 acc_mode |= MAY_APPEND;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /*
1540 * The simplest case - just a plain lookup.
1541 */
1542 if (!(flag & O_CREAT)) {
Trond Myklebust834f2a42005-10-18 14:20:16 -07001543 error = path_lookup_open(pathname, lookup_flags(flag), nd, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (error)
1545 return error;
1546 goto ok;
1547 }
1548
1549 /*
1550 * Create - we need to know the parent.
1551 */
Trond Myklebust834f2a42005-10-18 14:20:16 -07001552 error = path_lookup_create(pathname, LOOKUP_PARENT, nd, flag, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (error)
1554 return error;
1555
1556 /*
1557 * We have the parent and last component. First of all, check
1558 * that we are not asked to creat(2) an obvious directory - that
1559 * will not do.
1560 */
1561 error = -EISDIR;
1562 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1563 goto exit;
1564
1565 dir = nd->dentry;
1566 nd->flags &= ~LOOKUP_PARENT;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001567 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001568 path.dentry = lookup_hash(nd);
Al Virod73ffe12005-06-06 13:36:01 -07001569 path.mnt = nd->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571do_last:
Al Viro4e7506e2005-06-06 13:36:00 -07001572 error = PTR_ERR(path.dentry);
1573 if (IS_ERR(path.dentry)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001574 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 goto exit;
1576 }
1577
1578 /* Negative dentry, just create the file */
Al Viro4e7506e2005-06-06 13:36:00 -07001579 if (!path.dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (!IS_POSIXACL(dir->d_inode))
1581 mode &= ~current->fs->umask;
Al Viro4e7506e2005-06-06 13:36:00 -07001582 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001583 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 dput(nd->dentry);
Al Viro4e7506e2005-06-06 13:36:00 -07001585 nd->dentry = path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (error)
1587 goto exit;
1588 /* Don't check for write permission, don't truncate */
1589 acc_mode = 0;
1590 flag &= ~O_TRUNC;
1591 goto ok;
1592 }
1593
1594 /*
1595 * It already exists.
1596 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001597 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 error = -EEXIST;
1600 if (flag & O_EXCL)
1601 goto exit_dput;
1602
Al Viroe13b2102005-06-06 13:36:06 -07001603 if (__follow_mount(&path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 error = -ELOOP;
Al Viroba7a4c12005-06-06 13:36:08 -07001605 if (flag & O_NOFOLLOW)
1606 goto exit_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608 error = -ENOENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001609 if (!path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 goto exit_dput;
Al Viro4e7506e2005-06-06 13:36:00 -07001611 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 goto do_link;
1613
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001614 path_to_nameidata(&path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 error = -EISDIR;
Al Viro4e7506e2005-06-06 13:36:00 -07001616 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto exit;
1618ok:
1619 error = may_open(nd, acc_mode, flag);
1620 if (error)
1621 goto exit;
1622 return 0;
1623
1624exit_dput:
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001625 dput_path(&path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626exit:
Trond Myklebust834f2a42005-10-18 14:20:16 -07001627 if (!IS_ERR(nd->intent.open.file))
1628 release_open_intent(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 path_release(nd);
1630 return error;
1631
1632do_link:
1633 error = -ELOOP;
1634 if (flag & O_NOFOLLOW)
1635 goto exit_dput;
1636 /*
1637 * This is subtle. Instead of calling do_follow_link() we do the
1638 * thing by hands. The reason is that this way we have zero link_count
1639 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1640 * After that we have the parent and last component, i.e.
1641 * we are in the same situation as after the first path_walk().
1642 * Well, almost - if the last component is normal we get its copy
1643 * stored in nd->last.name and we will have to putname() it when we
1644 * are done. Procfs-like symlinks just set LAST_BIND.
1645 */
1646 nd->flags |= LOOKUP_PARENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001647 error = security_inode_follow_link(path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (error)
1649 goto exit_dput;
Al Virocd4e91d2005-06-06 13:36:03 -07001650 error = __do_follow_link(&path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (error)
1652 return error;
1653 nd->flags &= ~LOOKUP_PARENT;
Al Virod671d5e2005-06-06 13:36:04 -07001654 if (nd->last_type == LAST_BIND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 error = -EISDIR;
1657 if (nd->last_type != LAST_NORM)
1658 goto exit;
1659 if (nd->last.name[nd->last.len]) {
Linus Torvalds82984112005-10-06 21:54:21 -07001660 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto exit;
1662 }
1663 error = -ELOOP;
1664 if (count++==32) {
Linus Torvalds82984112005-10-06 21:54:21 -07001665 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto exit;
1667 }
1668 dir = nd->dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001669 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001670 path.dentry = lookup_hash(nd);
Al Virod671d5e2005-06-06 13:36:04 -07001671 path.mnt = nd->mnt;
Linus Torvalds82984112005-10-06 21:54:21 -07001672 __putname(nd->last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto do_last;
1674}
1675
1676/**
1677 * lookup_create - lookup a dentry, creating it if it doesn't exist
1678 * @nd: nameidata info
1679 * @is_dir: directory flag
1680 *
1681 * Simple function to lookup and return a dentry and create it
1682 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001683 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001684 * Returns with nd->dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 */
1686struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1687{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001688 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001690 mutex_lock(&nd->dentry->d_inode->i_mutex);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001691 /*
1692 * Yucky last component or no last component at all?
1693 * (foo/., foo/.., /////)
1694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (nd->last_type != LAST_NORM)
1696 goto fail;
1697 nd->flags &= ~LOOKUP_PARENT;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001698
1699 /*
1700 * Do the final lookup.
1701 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08001702 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (IS_ERR(dentry))
1704 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001705
1706 /*
1707 * Special case - lookup gave negative, but... we had foo/bar/
1708 * From the vfs_mknod() POV we just have a negative dentry -
1709 * all is fine. Let's be bastards - you had / on the end, you've
1710 * been asking for (non-existent) directory. -ENOENT for you.
1711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1713 goto enoent;
1714 return dentry;
1715enoent:
1716 dput(dentry);
1717 dentry = ERR_PTR(-ENOENT);
1718fail:
1719 return dentry;
1720}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07001721EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1724{
1725 int error = may_create(dir, dentry, NULL);
1726
1727 if (error)
1728 return error;
1729
1730 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1731 return -EPERM;
1732
1733 if (!dir->i_op || !dir->i_op->mknod)
1734 return -EPERM;
1735
1736 error = security_inode_mknod(dir, dentry, mode, dev);
1737 if (error)
1738 return error;
1739
1740 DQUOT_INIT(dir);
1741 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001742 if (!error)
Robert Love0eeca282005-07-12 17:06:03 -04001743 fsnotify_create(dir, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return error;
1745}
1746
1747asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1748{
1749 int error = 0;
1750 char * tmp;
1751 struct dentry * dentry;
1752 struct nameidata nd;
1753
1754 if (S_ISDIR(mode))
1755 return -EPERM;
1756 tmp = getname(filename);
1757 if (IS_ERR(tmp))
1758 return PTR_ERR(tmp);
1759
1760 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1761 if (error)
1762 goto out;
1763 dentry = lookup_create(&nd, 0);
1764 error = PTR_ERR(dentry);
1765
1766 if (!IS_POSIXACL(nd.dentry->d_inode))
1767 mode &= ~current->fs->umask;
1768 if (!IS_ERR(dentry)) {
1769 switch (mode & S_IFMT) {
1770 case 0: case S_IFREG:
1771 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1772 break;
1773 case S_IFCHR: case S_IFBLK:
1774 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1775 new_decode_dev(dev));
1776 break;
1777 case S_IFIFO: case S_IFSOCK:
1778 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1779 break;
1780 case S_IFDIR:
1781 error = -EPERM;
1782 break;
1783 default:
1784 error = -EINVAL;
1785 }
1786 dput(dentry);
1787 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001788 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 path_release(&nd);
1790out:
1791 putname(tmp);
1792
1793 return error;
1794}
1795
1796int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1797{
1798 int error = may_create(dir, dentry, NULL);
1799
1800 if (error)
1801 return error;
1802
1803 if (!dir->i_op || !dir->i_op->mkdir)
1804 return -EPERM;
1805
1806 mode &= (S_IRWXUGO|S_ISVTX);
1807 error = security_inode_mkdir(dir, dentry, mode);
1808 if (error)
1809 return error;
1810
1811 DQUOT_INIT(dir);
1812 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001813 if (!error)
Robert Love0eeca282005-07-12 17:06:03 -04001814 fsnotify_mkdir(dir, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return error;
1816}
1817
1818asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1819{
1820 int error = 0;
1821 char * tmp;
1822
1823 tmp = getname(pathname);
1824 error = PTR_ERR(tmp);
1825 if (!IS_ERR(tmp)) {
1826 struct dentry *dentry;
1827 struct nameidata nd;
1828
1829 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1830 if (error)
1831 goto out;
1832 dentry = lookup_create(&nd, 1);
1833 error = PTR_ERR(dentry);
1834 if (!IS_ERR(dentry)) {
1835 if (!IS_POSIXACL(nd.dentry->d_inode))
1836 mode &= ~current->fs->umask;
1837 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1838 dput(dentry);
1839 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001840 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 path_release(&nd);
1842out:
1843 putname(tmp);
1844 }
1845
1846 return error;
1847}
1848
1849/*
1850 * We try to drop the dentry early: we should have
1851 * a usage count of 2 if we're the only user of this
1852 * dentry, and if that is true (possibly after pruning
1853 * the dcache), then we drop the dentry now.
1854 *
1855 * A low-level filesystem can, if it choses, legally
1856 * do a
1857 *
1858 * if (!d_unhashed(dentry))
1859 * return -EBUSY;
1860 *
1861 * if it cannot handle the case of removing a directory
1862 * that is still in use by something else..
1863 */
1864void dentry_unhash(struct dentry *dentry)
1865{
1866 dget(dentry);
1867 if (atomic_read(&dentry->d_count))
1868 shrink_dcache_parent(dentry);
1869 spin_lock(&dcache_lock);
1870 spin_lock(&dentry->d_lock);
1871 if (atomic_read(&dentry->d_count) == 2)
1872 __d_drop(dentry);
1873 spin_unlock(&dentry->d_lock);
1874 spin_unlock(&dcache_lock);
1875}
1876
1877int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1878{
1879 int error = may_delete(dir, dentry, 1);
1880
1881 if (error)
1882 return error;
1883
1884 if (!dir->i_op || !dir->i_op->rmdir)
1885 return -EPERM;
1886
1887 DQUOT_INIT(dir);
1888
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001889 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 dentry_unhash(dentry);
1891 if (d_mountpoint(dentry))
1892 error = -EBUSY;
1893 else {
1894 error = security_inode_rmdir(dir, dentry);
1895 if (!error) {
1896 error = dir->i_op->rmdir(dir, dentry);
1897 if (!error)
1898 dentry->d_inode->i_flags |= S_DEAD;
1899 }
1900 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001901 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 d_delete(dentry);
1904 }
1905 dput(dentry);
1906
1907 return error;
1908}
1909
1910asmlinkage long sys_rmdir(const char __user * pathname)
1911{
1912 int error = 0;
1913 char * name;
1914 struct dentry *dentry;
1915 struct nameidata nd;
1916
1917 name = getname(pathname);
1918 if(IS_ERR(name))
1919 return PTR_ERR(name);
1920
1921 error = path_lookup(name, LOOKUP_PARENT, &nd);
1922 if (error)
1923 goto exit;
1924
1925 switch(nd.last_type) {
1926 case LAST_DOTDOT:
1927 error = -ENOTEMPTY;
1928 goto exit1;
1929 case LAST_DOT:
1930 error = -EINVAL;
1931 goto exit1;
1932 case LAST_ROOT:
1933 error = -EBUSY;
1934 goto exit1;
1935 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001936 mutex_lock(&nd.dentry->d_inode->i_mutex);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001937 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 error = PTR_ERR(dentry);
1939 if (!IS_ERR(dentry)) {
1940 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1941 dput(dentry);
1942 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001943 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944exit1:
1945 path_release(&nd);
1946exit:
1947 putname(name);
1948 return error;
1949}
1950
1951int vfs_unlink(struct inode *dir, struct dentry *dentry)
1952{
1953 int error = may_delete(dir, dentry, 0);
1954
1955 if (error)
1956 return error;
1957
1958 if (!dir->i_op || !dir->i_op->unlink)
1959 return -EPERM;
1960
1961 DQUOT_INIT(dir);
1962
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001963 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (d_mountpoint(dentry))
1965 error = -EBUSY;
1966 else {
1967 error = security_inode_unlink(dir, dentry);
1968 if (!error)
1969 error = dir->i_op->unlink(dir, dentry);
1970 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001971 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1974 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1975 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Robert Love0eeca282005-07-12 17:06:03 -04001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return error;
1979}
1980
1981/*
1982 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001983 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 * writeout happening, and we don't want to prevent access to the directory
1985 * while waiting on the I/O.
1986 */
1987asmlinkage long sys_unlink(const char __user * pathname)
1988{
1989 int error = 0;
1990 char * name;
1991 struct dentry *dentry;
1992 struct nameidata nd;
1993 struct inode *inode = NULL;
1994
1995 name = getname(pathname);
1996 if(IS_ERR(name))
1997 return PTR_ERR(name);
1998
1999 error = path_lookup(name, LOOKUP_PARENT, &nd);
2000 if (error)
2001 goto exit;
2002 error = -EISDIR;
2003 if (nd.last_type != LAST_NORM)
2004 goto exit1;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002005 mutex_lock(&nd.dentry->d_inode->i_mutex);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002006 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 error = PTR_ERR(dentry);
2008 if (!IS_ERR(dentry)) {
2009 /* Why not before? Because we want correct error value */
2010 if (nd.last.name[nd.last.len])
2011 goto slashes;
2012 inode = dentry->d_inode;
2013 if (inode)
2014 atomic_inc(&inode->i_count);
2015 error = vfs_unlink(nd.dentry->d_inode, dentry);
2016 exit2:
2017 dput(dentry);
2018 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002019 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (inode)
2021 iput(inode); /* truncate the inode here */
2022exit1:
2023 path_release(&nd);
2024exit:
2025 putname(name);
2026 return error;
2027
2028slashes:
2029 error = !dentry->d_inode ? -ENOENT :
2030 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2031 goto exit2;
2032}
2033
2034int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
2035{
2036 int error = may_create(dir, dentry, NULL);
2037
2038 if (error)
2039 return error;
2040
2041 if (!dir->i_op || !dir->i_op->symlink)
2042 return -EPERM;
2043
2044 error = security_inode_symlink(dir, dentry, oldname);
2045 if (error)
2046 return error;
2047
2048 DQUOT_INIT(dir);
2049 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002050 if (!error)
Robert Love0eeca282005-07-12 17:06:03 -04002051 fsnotify_create(dir, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return error;
2053}
2054
2055asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
2056{
2057 int error = 0;
2058 char * from;
2059 char * to;
2060
2061 from = getname(oldname);
2062 if(IS_ERR(from))
2063 return PTR_ERR(from);
2064 to = getname(newname);
2065 error = PTR_ERR(to);
2066 if (!IS_ERR(to)) {
2067 struct dentry *dentry;
2068 struct nameidata nd;
2069
2070 error = path_lookup(to, LOOKUP_PARENT, &nd);
2071 if (error)
2072 goto out;
2073 dentry = lookup_create(&nd, 0);
2074 error = PTR_ERR(dentry);
2075 if (!IS_ERR(dentry)) {
2076 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
2077 dput(dentry);
2078 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002079 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 path_release(&nd);
2081out:
2082 putname(to);
2083 }
2084 putname(from);
2085 return error;
2086}
2087
2088int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2089{
2090 struct inode *inode = old_dentry->d_inode;
2091 int error;
2092
2093 if (!inode)
2094 return -ENOENT;
2095
2096 error = may_create(dir, new_dentry, NULL);
2097 if (error)
2098 return error;
2099
2100 if (dir->i_sb != inode->i_sb)
2101 return -EXDEV;
2102
2103 /*
2104 * A link to an append-only or immutable file cannot be created.
2105 */
2106 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2107 return -EPERM;
2108 if (!dir->i_op || !dir->i_op->link)
2109 return -EPERM;
2110 if (S_ISDIR(old_dentry->d_inode->i_mode))
2111 return -EPERM;
2112
2113 error = security_inode_link(old_dentry, dir, new_dentry);
2114 if (error)
2115 return error;
2116
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002117 mutex_lock(&old_dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 DQUOT_INIT(dir);
2119 error = dir->i_op->link(old_dentry, dir, new_dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002120 mutex_unlock(&old_dentry->d_inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002121 if (!error)
Robert Love0eeca282005-07-12 17:06:03 -04002122 fsnotify_create(dir, new_dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return error;
2124}
2125
2126/*
2127 * Hardlinks are often used in delicate situations. We avoid
2128 * security-related surprises by not following symlinks on the
2129 * newname. --KAB
2130 *
2131 * We don't follow them on the oldname either to be compatible
2132 * with linux 2.0, and to avoid hard-linking to directories
2133 * and other special files. --ADM
2134 */
2135asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2136{
2137 struct dentry *new_dentry;
2138 struct nameidata nd, old_nd;
2139 int error;
2140 char * to;
2141
2142 to = getname(newname);
2143 if (IS_ERR(to))
2144 return PTR_ERR(to);
2145
2146 error = __user_walk(oldname, 0, &old_nd);
2147 if (error)
2148 goto exit;
2149 error = path_lookup(to, LOOKUP_PARENT, &nd);
2150 if (error)
2151 goto out;
2152 error = -EXDEV;
2153 if (old_nd.mnt != nd.mnt)
2154 goto out_release;
2155 new_dentry = lookup_create(&nd, 0);
2156 error = PTR_ERR(new_dentry);
2157 if (!IS_ERR(new_dentry)) {
2158 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2159 dput(new_dentry);
2160 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002161 mutex_unlock(&nd.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162out_release:
2163 path_release(&nd);
2164out:
2165 path_release(&old_nd);
2166exit:
2167 putname(to);
2168
2169 return error;
2170}
2171
2172/*
2173 * The worst of all namespace operations - renaming directory. "Perverted"
2174 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2175 * Problems:
2176 * a) we can get into loop creation. Check is done in is_subdir().
2177 * b) race potential - two innocent renames can create a loop together.
2178 * That's where 4.4 screws up. Current fix: serialization on
2179 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2180 * story.
2181 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002182 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 * whether the target exists). Solution: try to be smart with locking
2184 * order for inodes. We rely on the fact that tree topology may change
2185 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2186 * move will be locked. Thus we can rank directories by the tree
2187 * (ancestors first) and rank all non-directories after them.
2188 * That works since everybody except rename does "lock parent, lookup,
2189 * lock child" and rename is under ->s_vfs_rename_sem.
2190 * HOWEVER, it relies on the assumption that any object with ->lookup()
2191 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2192 * we'd better make sure that there's no link(2) for them.
2193 * d) some filesystems don't support opened-but-unlinked directories,
2194 * either because of layout or because they are not ready to deal with
2195 * all cases correctly. The latter will be fixed (taking this sort of
2196 * stuff into VFS), but the former is not going away. Solution: the same
2197 * trick as in rmdir().
2198 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002199 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002201 * ->i_mutex on parents, which works but leads to some truely excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 * locking].
2203 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002204static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2205 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 int error = 0;
2208 struct inode *target;
2209
2210 /*
2211 * If we are going to change the parent - check write permissions,
2212 * we'll need to flip '..'.
2213 */
2214 if (new_dir != old_dir) {
2215 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2216 if (error)
2217 return error;
2218 }
2219
2220 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2221 if (error)
2222 return error;
2223
2224 target = new_dentry->d_inode;
2225 if (target) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002226 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 dentry_unhash(new_dentry);
2228 }
2229 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2230 error = -EBUSY;
2231 else
2232 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2233 if (target) {
2234 if (!error)
2235 target->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002236 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (d_unhashed(new_dentry))
2238 d_rehash(new_dentry);
2239 dput(new_dentry);
2240 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002241 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return error;
2244}
2245
Adrian Bunk75c96f82005-05-05 16:16:09 -07002246static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2247 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 struct inode *target;
2250 int error;
2251
2252 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2253 if (error)
2254 return error;
2255
2256 dget(new_dentry);
2257 target = new_dentry->d_inode;
2258 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002259 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2261 error = -EBUSY;
2262 else
2263 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2264 if (!error) {
2265 /* The following d_move() should become unconditional */
2266 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2267 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002270 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 dput(new_dentry);
2272 return error;
2273}
2274
2275int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2276 struct inode *new_dir, struct dentry *new_dentry)
2277{
2278 int error;
2279 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Robert Love0eeca282005-07-12 17:06:03 -04002280 const char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 if (old_dentry->d_inode == new_dentry->d_inode)
2283 return 0;
2284
2285 error = may_delete(old_dir, old_dentry, is_dir);
2286 if (error)
2287 return error;
2288
2289 if (!new_dentry->d_inode)
2290 error = may_create(new_dir, new_dentry, NULL);
2291 else
2292 error = may_delete(new_dir, new_dentry, is_dir);
2293 if (error)
2294 return error;
2295
2296 if (!old_dir->i_op || !old_dir->i_op->rename)
2297 return -EPERM;
2298
2299 DQUOT_INIT(old_dir);
2300 DQUOT_INIT(new_dir);
2301
Robert Love0eeca282005-07-12 17:06:03 -04002302 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (is_dir)
2305 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2306 else
2307 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2308 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -04002309 const char *new_name = old_dentry->d_name.name;
John McCutchan89204c42005-08-15 12:13:28 -04002310 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
2311 new_dentry->d_inode, old_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Robert Love0eeca282005-07-12 17:06:03 -04002313 fsnotify_oldname_free(old_name);
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return error;
2316}
2317
2318static inline int do_rename(const char * oldname, const char * newname)
2319{
2320 int error = 0;
2321 struct dentry * old_dir, * new_dir;
2322 struct dentry * old_dentry, *new_dentry;
2323 struct dentry * trap;
2324 struct nameidata oldnd, newnd;
2325
2326 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2327 if (error)
2328 goto exit;
2329
2330 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2331 if (error)
2332 goto exit1;
2333
2334 error = -EXDEV;
2335 if (oldnd.mnt != newnd.mnt)
2336 goto exit2;
2337
2338 old_dir = oldnd.dentry;
2339 error = -EBUSY;
2340 if (oldnd.last_type != LAST_NORM)
2341 goto exit2;
2342
2343 new_dir = newnd.dentry;
2344 if (newnd.last_type != LAST_NORM)
2345 goto exit2;
2346
2347 trap = lock_rename(new_dir, old_dir);
2348
Christoph Hellwig49705b72005-11-08 21:35:06 -08002349 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 error = PTR_ERR(old_dentry);
2351 if (IS_ERR(old_dentry))
2352 goto exit3;
2353 /* source must exist */
2354 error = -ENOENT;
2355 if (!old_dentry->d_inode)
2356 goto exit4;
2357 /* unless the source is a directory trailing slashes give -ENOTDIR */
2358 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2359 error = -ENOTDIR;
2360 if (oldnd.last.name[oldnd.last.len])
2361 goto exit4;
2362 if (newnd.last.name[newnd.last.len])
2363 goto exit4;
2364 }
2365 /* source should not be ancestor of target */
2366 error = -EINVAL;
2367 if (old_dentry == trap)
2368 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08002369 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 error = PTR_ERR(new_dentry);
2371 if (IS_ERR(new_dentry))
2372 goto exit4;
2373 /* target should not be an ancestor of source */
2374 error = -ENOTEMPTY;
2375 if (new_dentry == trap)
2376 goto exit5;
2377
2378 error = vfs_rename(old_dir->d_inode, old_dentry,
2379 new_dir->d_inode, new_dentry);
2380exit5:
2381 dput(new_dentry);
2382exit4:
2383 dput(old_dentry);
2384exit3:
2385 unlock_rename(new_dir, old_dir);
2386exit2:
2387 path_release(&newnd);
2388exit1:
2389 path_release(&oldnd);
2390exit:
2391 return error;
2392}
2393
2394asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2395{
2396 int error;
2397 char * from;
2398 char * to;
2399
2400 from = getname(oldname);
2401 if(IS_ERR(from))
2402 return PTR_ERR(from);
2403 to = getname(newname);
2404 error = PTR_ERR(to);
2405 if (!IS_ERR(to)) {
2406 error = do_rename(from,to);
2407 putname(to);
2408 }
2409 putname(from);
2410 return error;
2411}
2412
2413int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2414{
2415 int len;
2416
2417 len = PTR_ERR(link);
2418 if (IS_ERR(link))
2419 goto out;
2420
2421 len = strlen(link);
2422 if (len > (unsigned) buflen)
2423 len = buflen;
2424 if (copy_to_user(buffer, link, len))
2425 len = -EFAULT;
2426out:
2427 return len;
2428}
2429
2430/*
2431 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2432 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2433 * using) it for any given inode is up to filesystem.
2434 */
2435int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2436{
2437 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002438 void *cookie;
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002441 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2442 if (!IS_ERR(cookie)) {
2443 int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (dentry->d_inode->i_op->put_link)
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002445 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2446 cookie = ERR_PTR(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002448 return PTR_ERR(cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
2451int vfs_follow_link(struct nameidata *nd, const char *link)
2452{
2453 return __vfs_follow_link(nd, link);
2454}
2455
2456/* get the link contents into pagecache */
2457static char *page_getlink(struct dentry * dentry, struct page **ppage)
2458{
2459 struct page * page;
2460 struct address_space *mapping = dentry->d_inode->i_mapping;
2461 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2462 NULL);
2463 if (IS_ERR(page))
2464 goto sync_fail;
2465 wait_on_page_locked(page);
2466 if (!PageUptodate(page))
2467 goto async_fail;
2468 *ppage = page;
2469 return kmap(page);
2470
2471async_fail:
2472 page_cache_release(page);
2473 return ERR_PTR(-EIO);
2474
2475sync_fail:
2476 return (char*)page;
2477}
2478
2479int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2480{
2481 struct page *page = NULL;
2482 char *s = page_getlink(dentry, &page);
2483 int res = vfs_readlink(dentry,buffer,buflen,s);
2484 if (page) {
2485 kunmap(page);
2486 page_cache_release(page);
2487 }
2488 return res;
2489}
2490
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002491void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002493 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002495 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002498void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002500 struct page *page = cookie;
2501
2502 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 kunmap(page);
2504 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506}
2507
2508int page_symlink(struct inode *inode, const char *symname, int len)
2509{
2510 struct address_space *mapping = inode->i_mapping;
2511 struct page *page = grab_cache_page(mapping, 0);
2512 int err = -ENOMEM;
2513 char *kaddr;
2514
2515 if (!page)
2516 goto fail;
2517 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2518 if (err)
2519 goto fail_map;
2520 kaddr = kmap_atomic(page, KM_USER0);
2521 memcpy(kaddr, symname, len-1);
2522 kunmap_atomic(kaddr, KM_USER0);
2523 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2524 /*
2525 * Notice that we are _not_ going to block here - end of page is
2526 * unmapped, so this will only try to map the rest of page, see
2527 * that it is unmapped (typically even will not look into inode -
2528 * ->i_size will be enough for everything) and zero it out.
2529 * OTOH it's obviously correct and should make the page up-to-date.
2530 */
2531 if (!PageUptodate(page)) {
2532 err = mapping->a_ops->readpage(NULL, page);
2533 wait_on_page_locked(page);
2534 } else {
2535 unlock_page(page);
2536 }
2537 page_cache_release(page);
2538 if (err < 0)
2539 goto fail;
2540 mark_inode_dirty(inode);
2541 return 0;
2542fail_map:
2543 unlock_page(page);
2544 page_cache_release(page);
2545fail:
2546 return err;
2547}
2548
2549struct inode_operations page_symlink_inode_operations = {
2550 .readlink = generic_readlink,
2551 .follow_link = page_follow_link_light,
2552 .put_link = page_put_link,
2553};
2554
2555EXPORT_SYMBOL(__user_walk);
2556EXPORT_SYMBOL(follow_down);
2557EXPORT_SYMBOL(follow_up);
2558EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2559EXPORT_SYMBOL(getname);
2560EXPORT_SYMBOL(lock_rename);
2561EXPORT_SYMBOL(lookup_hash);
2562EXPORT_SYMBOL(lookup_one_len);
2563EXPORT_SYMBOL(page_follow_link_light);
2564EXPORT_SYMBOL(page_put_link);
2565EXPORT_SYMBOL(page_readlink);
2566EXPORT_SYMBOL(page_symlink);
2567EXPORT_SYMBOL(page_symlink_inode_operations);
2568EXPORT_SYMBOL(path_lookup);
2569EXPORT_SYMBOL(path_release);
2570EXPORT_SYMBOL(path_walk);
2571EXPORT_SYMBOL(permission);
Christoph Hellwige4543ed2005-11-08 21:35:04 -08002572EXPORT_SYMBOL(vfs_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08002573EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574EXPORT_SYMBOL(unlock_rename);
2575EXPORT_SYMBOL(vfs_create);
2576EXPORT_SYMBOL(vfs_follow_link);
2577EXPORT_SYMBOL(vfs_link);
2578EXPORT_SYMBOL(vfs_mkdir);
2579EXPORT_SYMBOL(vfs_mknod);
2580EXPORT_SYMBOL(generic_permission);
2581EXPORT_SYMBOL(vfs_readlink);
2582EXPORT_SYMBOL(vfs_rename);
2583EXPORT_SYMBOL(vfs_rmdir);
2584EXPORT_SYMBOL(vfs_symlink);
2585EXPORT_SYMBOL(vfs_unlink);
2586EXPORT_SYMBOL(dentry_unhash);
2587EXPORT_SYMBOL(generic_readlink);