blob: 095818089ac10c71e47f7e0ce55e1110fec1afe7 [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>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070033#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800105 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * any extra contention...
107 */
108
Al Viroa02f76c2008-02-23 15:14:28 +0000109static int __link_path_walk(const char *name, struct nameidata *nd);
Josef 'Jeff' Sipekc4a78082007-07-19 01:48:22 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* In order to reduce some races, while at the same time doing additional
112 * checking and hopefully speeding things up, we copy filenames to the
113 * kernel data space before using them..
114 *
115 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
116 * PATH_MAX includes the nul terminator --RR.
117 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800118static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int retval;
121 unsigned long len = PATH_MAX;
122
123 if (!segment_eq(get_fs(), KERNEL_DS)) {
124 if ((unsigned long) filename >= TASK_SIZE)
125 return -EFAULT;
126 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
127 len = TASK_SIZE - (unsigned long) filename;
128 }
129
130 retval = strncpy_from_user(page, filename, len);
131 if (retval > 0) {
132 if (retval < len)
133 return 0;
134 return -ENAMETOOLONG;
135 } else if (!retval)
136 retval = -ENOENT;
137 return retval;
138}
139
140char * getname(const char __user * filename)
141{
142 char *tmp, *result;
143
144 result = ERR_PTR(-ENOMEM);
145 tmp = __getname();
146 if (tmp) {
147 int retval = do_getname(filename, tmp);
148
149 result = tmp;
150 if (retval < 0) {
151 __putname(tmp);
152 result = ERR_PTR(retval);
153 }
154 }
155 audit_getname(result);
156 return result;
157}
158
159#ifdef CONFIG_AUDITSYSCALL
160void putname(const char *name)
161{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400162 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 audit_putname(name);
164 else
165 __putname(name);
166}
167EXPORT_SYMBOL(putname);
168#endif
169
170
171/**
172 * generic_permission - check for access rights on a Posix-like filesystem
173 * @inode: inode to check access rights for
174 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
175 * @check_acl: optional callback to check for Posix ACLs
176 *
177 * Used to check for read/write/execute permissions on a file.
178 * We use "fsuid" for this, letting us set arbitrary permissions
179 * for filesystem access without changing the "normal" uids which
180 * are used for other things..
181 */
182int generic_permission(struct inode *inode, int mask,
183 int (*check_acl)(struct inode *inode, int mask))
184{
185 umode_t mode = inode->i_mode;
186
Al Viroe6305c42008-07-15 21:03:57 -0400187 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (current->fsuid == inode->i_uid)
190 mode >>= 6;
191 else {
192 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
193 int error = check_acl(inode, mask);
194 if (error == -EACCES)
195 goto check_capabilities;
196 else if (error != -EAGAIN)
197 return error;
198 }
199
200 if (in_group_p(inode->i_gid))
201 mode >>= 3;
202 }
203
204 /*
205 * If the DACs are ok we don't need any capability check.
206 */
Al Viroe6305c42008-07-15 21:03:57 -0400207 if ((mask & ~mode) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209
210 check_capabilities:
211 /*
212 * Read/write DACs are always overridable.
213 * Executable DACs are overridable if at least one exec bit is set.
214 */
215 if (!(mask & MAY_EXEC) ||
216 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
217 if (capable(CAP_DAC_OVERRIDE))
218 return 0;
219
220 /*
221 * Searching includes executable on directories, else just read.
222 */
223 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
224 if (capable(CAP_DAC_READ_SEARCH))
225 return 0;
226
227 return -EACCES;
228}
229
230int permission(struct inode *inode, int mask, struct nameidata *nd)
231{
Al Viroe6305c42008-07-15 21:03:57 -0400232 int retval;
Dave Hansenc7eb2662007-10-16 23:31:14 -0700233 struct vfsmount *mnt = NULL;
234
235 if (nd)
Jan Blunck4ac91372008-02-14 19:34:32 -0800236 mnt = nd->path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700239 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /*
242 * Nobody gets write access to a read-only fs.
243 */
244 if (IS_RDONLY(inode) &&
245 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
246 return -EROFS;
247
248 /*
249 * Nobody gets write access to an immutable file.
250 */
251 if (IS_IMMUTABLE(inode))
252 return -EACCES;
253 }
254
Miklos Szeredi22590e42007-10-16 23:27:08 -0700255 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
256 /*
257 * MAY_EXEC on regular files is denied if the fs is mounted
258 * with the "noexec" flag.
259 */
Dave Hansenc7eb2662007-10-16 23:31:14 -0700260 if (mnt && (mnt->mnt_flags & MNT_NOEXEC))
Miklos Szeredi22590e42007-10-16 23:27:08 -0700261 return -EACCES;
262 }
Trond Myklebusta343bb72006-08-22 20:06:03 -0400263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* Ordinary permission routines do not understand MAY_APPEND. */
Miklos Szeredi22590e42007-10-16 23:27:08 -0700265 if (inode->i_op && inode->i_op->permission) {
Al Viroe6305c42008-07-15 21:03:57 -0400266 int extra = 0;
267 if (nd) {
268 if (nd->flags & LOOKUP_ACCESS)
269 extra |= MAY_ACCESS;
Al Viroe6305c42008-07-15 21:03:57 -0400270 if (nd->flags & LOOKUP_OPEN)
271 extra |= MAY_OPEN;
272 }
273 retval = inode->i_op->permission(inode, mask | extra);
Miklos Szeredi22590e42007-10-16 23:27:08 -0700274 if (!retval) {
275 /*
276 * Exec permission on a regular file is denied if none
277 * of the execute bits are set.
278 *
279 * This check should be done by the ->permission()
280 * method.
281 */
282 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
283 !(inode->i_mode & S_IXUGO))
284 return -EACCES;
285 }
286 } else {
Al Viroe6305c42008-07-15 21:03:57 -0400287 retval = generic_permission(inode, mask, NULL);
Miklos Szeredi22590e42007-10-16 23:27:08 -0700288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (retval)
290 return retval;
291
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700292 retval = devcgroup_inode_permission(inode, mask);
293 if (retval)
294 return retval;
295
Al Viroe6305c42008-07-15 21:03:57 -0400296 return security_inode_permission(inode,
297 mask & (MAY_READ|MAY_WRITE|MAY_EXEC), nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800300/**
301 * vfs_permission - check for access rights to a given path
302 * @nd: lookup result that describes the path
303 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
304 *
305 * Used to check for read/write/execute permissions on a path.
306 * We use "fsuid" for this, letting us set arbitrary permissions
307 * for filesystem access without changing the "normal" uids which
308 * are used for other things.
309 */
310int vfs_permission(struct nameidata *nd, int mask)
311{
Jan Blunck4ac91372008-02-14 19:34:32 -0800312 return permission(nd->path.dentry->d_inode, mask, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800313}
314
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800315/**
316 * file_permission - check for additional access rights to a given file
317 * @file: file to check access rights for
318 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
319 *
320 * Used to check for read/write/execute permissions on an already opened
321 * file.
322 *
323 * Note:
324 * Do not use this function in new code. All access checks should
325 * be done using vfs_permission().
326 */
327int file_permission(struct file *file, int mask)
328{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800329 return permission(file->f_path.dentry->d_inode, mask, NULL);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * get_write_access() gets write permission for a file.
334 * put_write_access() releases this write permission.
335 * This is used for regular files.
336 * We cannot support write (and maybe mmap read-write shared) accesses and
337 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
338 * can have the following values:
339 * 0: no writers, no VM_DENYWRITE mappings
340 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
341 * > 0: (i_writecount) users are writing to the file.
342 *
343 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
344 * except for the cases where we don't hold i_writecount yet. Then we need to
345 * use {get,deny}_write_access() - these functions check the sign and refuse
346 * to do the change if sign is wrong. Exclusion between them is provided by
347 * the inode->i_lock spinlock.
348 */
349
350int get_write_access(struct inode * inode)
351{
352 spin_lock(&inode->i_lock);
353 if (atomic_read(&inode->i_writecount) < 0) {
354 spin_unlock(&inode->i_lock);
355 return -ETXTBSY;
356 }
357 atomic_inc(&inode->i_writecount);
358 spin_unlock(&inode->i_lock);
359
360 return 0;
361}
362
363int deny_write_access(struct file * file)
364{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800365 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 spin_lock(&inode->i_lock);
368 if (atomic_read(&inode->i_writecount) > 0) {
369 spin_unlock(&inode->i_lock);
370 return -ETXTBSY;
371 }
372 atomic_dec(&inode->i_writecount);
373 spin_unlock(&inode->i_lock);
374
375 return 0;
376}
377
Jan Blunck1d957f92008-02-14 19:34:35 -0800378/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800379 * path_get - get a reference to a path
380 * @path: path to get the reference to
381 *
382 * Given a path increment the reference count to the dentry and the vfsmount.
383 */
384void path_get(struct path *path)
385{
386 mntget(path->mnt);
387 dget(path->dentry);
388}
389EXPORT_SYMBOL(path_get);
390
391/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800392 * path_put - put a reference to a path
393 * @path: path to put the reference to
394 *
395 * Given a path decrement the reference count to the dentry and the vfsmount.
396 */
397void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Jan Blunck1d957f92008-02-14 19:34:35 -0800399 dput(path->dentry);
400 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
Jan Blunck1d957f92008-02-14 19:34:35 -0800402EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Trond Myklebust834f2a42005-10-18 14:20:16 -0700404/**
405 * release_open_intent - free up open intent resources
406 * @nd: pointer to nameidata
407 */
408void release_open_intent(struct nameidata *nd)
409{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800410 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700411 put_filp(nd->intent.open.file);
412 else
413 fput(nd->intent.open.file);
414}
415
Ian Kentbcdc5e02006-09-27 01:50:44 -0700416static inline struct dentry *
417do_revalidate(struct dentry *dentry, struct nameidata *nd)
418{
419 int status = dentry->d_op->d_revalidate(dentry, nd);
420 if (unlikely(status <= 0)) {
421 /*
422 * The dentry failed validation.
423 * If d_revalidate returned 0 attempt to invalidate
424 * the dentry otherwise d_revalidate is asking us
425 * to return a fail status.
426 */
427 if (!status) {
428 if (!d_invalidate(dentry)) {
429 dput(dentry);
430 dentry = NULL;
431 }
432 } else {
433 dput(dentry);
434 dentry = ERR_PTR(status);
435 }
436 }
437 return dentry;
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
441 * Internal lookup() using the new generic dcache.
442 * SMP-safe
443 */
444static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
445{
446 struct dentry * dentry = __d_lookup(parent, name);
447
448 /* lockess __d_lookup may fail due to concurrent d_move()
449 * in some unrelated directory, so try with d_lookup
450 */
451 if (!dentry)
452 dentry = d_lookup(parent, name);
453
Ian Kentbcdc5e02006-09-27 01:50:44 -0700454 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
455 dentry = do_revalidate(dentry, nd);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return dentry;
458}
459
460/*
461 * Short-cut version of permission(), for calling by
462 * path_walk(), when dcache lock is held. Combines parts
463 * of permission() and generic_permission(), and tests ONLY for
464 * MAY_EXEC permission.
465 *
466 * If appropriate, check DAC only. If not appropriate, or
467 * short-cut DAC fails, then call permission() to do more
468 * complete permission check.
469 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800470static int exec_permission_lite(struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct nameidata *nd)
472{
473 umode_t mode = inode->i_mode;
474
475 if (inode->i_op && inode->i_op->permission)
476 return -EAGAIN;
477
478 if (current->fsuid == inode->i_uid)
479 mode >>= 6;
480 else if (in_group_p(inode->i_gid))
481 mode >>= 3;
482
483 if (mode & MAY_EXEC)
484 goto ok;
485
486 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
487 goto ok;
488
489 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
490 goto ok;
491
492 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
493 goto ok;
494
495 return -EACCES;
496ok:
497 return security_inode_permission(inode, MAY_EXEC, nd);
498}
499
500/*
501 * This is called when everything else fails, and we actually have
502 * to go to the low-level filesystem to find out what we should do..
503 *
504 * We get the directory semaphore, and after getting that we also
505 * make sure that nobody added the entry to the dcache in the meantime..
506 * SMP-safe
507 */
508static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
509{
510 struct dentry * result;
511 struct inode *dir = parent->d_inode;
512
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800513 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * First re-do the cached lookup just in case it was created
516 * while we waited for the directory semaphore..
517 *
518 * FIXME! This could use version numbering or similar to
519 * avoid unnecessary cache lookups.
520 *
521 * The "dcache_lock" is purely to protect the RCU list walker
522 * from concurrent renames at this point (we mustn't get false
523 * negatives from the RCU list walk here, unlike the optimistic
524 * fast walk).
525 *
526 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
527 */
528 result = d_lookup(parent, name);
529 if (!result) {
Miklos Szeredid70b67c2008-07-02 21:30:15 +0200530 struct dentry *dentry;
531
532 /* Don't create child dentry for a dead directory. */
533 result = ERR_PTR(-ENOENT);
534 if (IS_DEADDIR(dir))
535 goto out_unlock;
536
537 dentry = d_alloc(parent, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 result = ERR_PTR(-ENOMEM);
539 if (dentry) {
540 result = dir->i_op->lookup(dir, dentry, nd);
541 if (result)
542 dput(dentry);
543 else
544 result = dentry;
545 }
Miklos Szeredid70b67c2008-07-02 21:30:15 +0200546out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800547 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return result;
549 }
550
551 /*
552 * Uhhuh! Nasty case: the cache was re-populated while
553 * we waited on the semaphore. Need to revalidate.
554 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800555 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (result->d_op && result->d_op->d_revalidate) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700557 result = do_revalidate(result, nd);
558 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 return result;
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/* SMP-safe */
Al Viro7f2da1e2008-05-10 20:44:54 -0400565static __always_inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566walk_init_root(const char *name, struct nameidata *nd)
567{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700568 struct fs_struct *fs = current->fs;
569
570 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800571 nd->path = fs->root;
572 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700573 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Al Viroa02f76c2008-02-23 15:14:28 +0000576/*
577 * Wrapper to retry pathname resolution whenever the underlying
578 * file system returns an ESTALE.
579 *
580 * Retry the whole path once, forcing real lookup requests
581 * instead of relying on the dcache.
582 */
583static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
584{
585 struct path save = nd->path;
586 int result;
587
588 /* make sure the stuff we saved doesn't go away */
Jan Blunckc8e7f442008-06-09 16:40:35 -0700589 path_get(&save);
Al Viroa02f76c2008-02-23 15:14:28 +0000590
591 result = __link_path_walk(name, nd);
592 if (result == -ESTALE) {
593 /* nd->path had been dropped */
594 nd->path = save;
Jan Blunckc8e7f442008-06-09 16:40:35 -0700595 path_get(&nd->path);
Al Viroa02f76c2008-02-23 15:14:28 +0000596 nd->flags |= LOOKUP_REVAL;
597 result = __link_path_walk(name, nd);
598 }
599
600 path_put(&save);
601
602 return result;
603}
604
Arjan van de Venf1662352006-01-14 13:21:31 -0800605static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 int res = 0;
608 char *name;
609 if (IS_ERR(link))
610 goto fail;
611
612 if (*link == '/') {
Jan Blunck1d957f92008-02-14 19:34:35 -0800613 path_put(&nd->path);
Al Viro7f2da1e2008-05-10 20:44:54 -0400614 walk_init_root(link, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 res = link_path_walk(link, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (nd->depth || res || nd->last_type!=LAST_NORM)
618 return res;
619 /*
620 * If it is an iterative symlinks resolution in open_namei() we
621 * have to copy the last component. And all that crap because of
622 * bloody create() on broken symlinks. Furrfu...
623 */
624 name = __getname();
625 if (unlikely(!name)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800626 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -ENOMEM;
628 }
629 strcpy(name, nd->last.name);
630 nd->last.name = name;
631 return 0;
632fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800633 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return PTR_ERR(link);
635}
636
Jan Blunck1d957f92008-02-14 19:34:35 -0800637static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700638{
639 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800640 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700641 mntput(path->mnt);
642}
643
644static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
645{
Jan Blunck4ac91372008-02-14 19:34:32 -0800646 dput(nd->path.dentry);
647 if (nd->path.mnt != path->mnt)
648 mntput(nd->path.mnt);
649 nd->path.mnt = path->mnt;
650 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700651}
652
Ian Kent051d3812006-03-27 01:14:53 -0800653static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
654{
655 int error;
656 void *cookie;
657 struct dentry *dentry = path->dentry;
658
659 touch_atime(path->mnt, dentry);
660 nd_set_link(nd, NULL);
661
Jan Blunck4ac91372008-02-14 19:34:32 -0800662 if (path->mnt != nd->path.mnt) {
Ian Kent051d3812006-03-27 01:14:53 -0800663 path_to_nameidata(path, nd);
664 dget(dentry);
665 }
666 mntget(path->mnt);
667 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
668 error = PTR_ERR(cookie);
669 if (!IS_ERR(cookie)) {
670 char *s = nd_get_link(nd);
671 error = 0;
672 if (s)
673 error = __vfs_follow_link(nd, s);
674 if (dentry->d_inode->i_op->put_link)
675 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
676 }
Jan Blunck09da59162008-02-14 19:34:37 -0800677 path_put(path);
Ian Kent051d3812006-03-27 01:14:53 -0800678
679 return error;
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/*
683 * This limits recursive symlink follows to 8, while
684 * limiting consecutive symlinks to 40.
685 *
686 * Without that kind of total limit, nasty chains of consecutive
687 * symlinks can cause almost arbitrarily long lookups.
688 */
Al Viro90ebe562005-06-06 13:35:58 -0700689static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 int err = -ELOOP;
692 if (current->link_count >= MAX_NESTED_LINKS)
693 goto loop;
694 if (current->total_link_count >= 40)
695 goto loop;
696 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
697 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700698 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (err)
700 goto loop;
701 current->link_count++;
702 current->total_link_count++;
703 nd->depth++;
Al Virocd4e91d2005-06-06 13:36:03 -0700704 err = __do_follow_link(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 current->link_count--;
706 nd->depth--;
707 return err;
708loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800709 path_put_conditional(path, nd);
710 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return err;
712}
713
714int follow_up(struct vfsmount **mnt, struct dentry **dentry)
715{
716 struct vfsmount *parent;
717 struct dentry *mountpoint;
718 spin_lock(&vfsmount_lock);
719 parent=(*mnt)->mnt_parent;
720 if (parent == *mnt) {
721 spin_unlock(&vfsmount_lock);
722 return 0;
723 }
724 mntget(parent);
725 mountpoint=dget((*mnt)->mnt_mountpoint);
726 spin_unlock(&vfsmount_lock);
727 dput(*dentry);
728 *dentry = mountpoint;
729 mntput(*mnt);
730 *mnt = parent;
731 return 1;
732}
733
734/* no need for dcache_lock, as serialization is taken care in
735 * namespace.c
736 */
Al Viro463ffb22005-06-06 13:36:05 -0700737static int __follow_mount(struct path *path)
738{
739 int res = 0;
740 while (d_mountpoint(path->dentry)) {
741 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
742 if (!mounted)
743 break;
744 dput(path->dentry);
745 if (res)
746 mntput(path->mnt);
747 path->mnt = mounted;
748 path->dentry = dget(mounted->mnt_root);
749 res = 1;
750 }
751 return res;
752}
753
Al Viro58c465e2005-06-06 13:36:13 -0700754static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 while (d_mountpoint(*dentry)) {
757 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
758 if (!mounted)
759 break;
Al Viro58c465e2005-06-06 13:36:13 -0700760 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 mntput(*mnt);
762 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 *dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/* no need for dcache_lock, as serialization is taken care in
768 * namespace.c
769 */
Al Viroe13b2102005-06-06 13:36:06 -0700770int follow_down(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct vfsmount *mounted;
773
774 mounted = lookup_mnt(*mnt, *dentry);
775 if (mounted) {
Al Viroe13b2102005-06-06 13:36:06 -0700776 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 mntput(*mnt);
778 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *dentry = dget(mounted->mnt_root);
780 return 1;
781 }
782 return 0;
783}
784
Arjan van de Venf1662352006-01-14 13:21:31 -0800785static __always_inline void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700787 struct fs_struct *fs = current->fs;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 while(1) {
790 struct vfsmount *parent;
Jan Blunck4ac91372008-02-14 19:34:32 -0800791 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Andreas Mohre518ddb2006-09-29 02:01:22 -0700793 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800794 if (nd->path.dentry == fs->root.dentry &&
795 nd->path.mnt == fs->root.mnt) {
Andreas Mohre518ddb2006-09-29 02:01:22 -0700796 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 break;
798 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700799 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 spin_lock(&dcache_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800801 if (nd->path.dentry != nd->path.mnt->mnt_root) {
802 nd->path.dentry = dget(nd->path.dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 spin_unlock(&dcache_lock);
804 dput(old);
805 break;
806 }
807 spin_unlock(&dcache_lock);
808 spin_lock(&vfsmount_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800809 parent = nd->path.mnt->mnt_parent;
810 if (parent == nd->path.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 spin_unlock(&vfsmount_lock);
812 break;
813 }
814 mntget(parent);
Jan Blunck4ac91372008-02-14 19:34:32 -0800815 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 spin_unlock(&vfsmount_lock);
817 dput(old);
Jan Blunck4ac91372008-02-14 19:34:32 -0800818 mntput(nd->path.mnt);
819 nd->path.mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800821 follow_mount(&nd->path.mnt, &nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824/*
825 * It's more convoluted than I'd like it to be, but... it's still fairly
826 * small and for now I'd prefer to have fast path as straight as possible.
827 * It _is_ time-critical.
828 */
829static int do_lookup(struct nameidata *nd, struct qstr *name,
830 struct path *path)
831{
Jan Blunck4ac91372008-02-14 19:34:32 -0800832 struct vfsmount *mnt = nd->path.mnt;
833 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (!dentry)
836 goto need_lookup;
837 if (dentry->d_op && dentry->d_op->d_revalidate)
838 goto need_revalidate;
839done:
840 path->mnt = mnt;
841 path->dentry = dentry;
Al Viro634ee702005-06-06 13:36:13 -0700842 __follow_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 0;
844
845need_lookup:
Jan Blunck4ac91372008-02-14 19:34:32 -0800846 dentry = real_lookup(nd->path.dentry, name, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (IS_ERR(dentry))
848 goto fail;
849 goto done;
850
851need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -0700852 dentry = do_revalidate(dentry, nd);
853 if (!dentry)
854 goto need_lookup;
855 if (IS_ERR(dentry))
856 goto fail;
857 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859fail:
860 return PTR_ERR(dentry);
861}
862
863/*
864 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +0100865 * This is the basic name resolution function, turning a pathname into
866 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 *
Prasanna Medaea3834d2005-04-29 16:00:17 +0100868 * Returns 0 and nd will have valid dentry and mnt on success.
869 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800871static int __link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct path next;
874 struct inode *inode;
875 int err;
876 unsigned int lookup_flags = nd->flags;
877
878 while (*name=='/')
879 name++;
880 if (!*name)
881 goto return_reval;
882
Jan Blunck4ac91372008-02-14 19:34:32 -0800883 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -0800885 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* At this point we know we have a real path component. */
888 for(;;) {
889 unsigned long hash;
890 struct qstr this;
891 unsigned int c;
892
Trond Myklebustcdce5d62005-10-18 14:20:18 -0700893 nd->flags |= LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 err = exec_permission_lite(inode, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800895 if (err == -EAGAIN)
896 err = vfs_permission(nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (err)
898 break;
899
900 this.name = name;
901 c = *(const unsigned char *)name;
902
903 hash = init_name_hash();
904 do {
905 name++;
906 hash = partial_name_hash(c, hash);
907 c = *(const unsigned char *)name;
908 } while (c && (c != '/'));
909 this.len = name - (const char *) this.name;
910 this.hash = end_name_hash(hash);
911
912 /* remove trailing slashes? */
913 if (!c)
914 goto last_component;
915 while (*++name == '/');
916 if (!*name)
917 goto last_with_slashes;
918
919 /*
920 * "." and ".." are special - ".." especially so because it has
921 * to be able to know about the current root directory and
922 * parent relationships.
923 */
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);
Jan Blunck4ac91372008-02-14 19:34:32 -0800931 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* fallthrough */
933 case 1:
934 continue;
935 }
936 /*
937 * See if the low-level filesystem might want
938 * to use its own hash..
939 */
Jan Blunck4ac91372008-02-14 19:34:32 -0800940 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
941 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
942 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (err < 0)
944 break;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* This does the actual lookups.. */
947 err = do_lookup(nd, &this, &next);
948 if (err)
949 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 err = -ENOENT;
952 inode = next.dentry->d_inode;
953 if (!inode)
954 goto out_dput;
955 err = -ENOTDIR;
956 if (!inode->i_op)
957 goto out_dput;
958
959 if (inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700960 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (err)
962 goto return_err;
963 err = -ENOENT;
Jan Blunck4ac91372008-02-14 19:34:32 -0800964 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (!inode)
966 break;
967 err = -ENOTDIR;
968 if (!inode->i_op)
969 break;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700970 } else
971 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 err = -ENOTDIR;
973 if (!inode->i_op->lookup)
974 break;
975 continue;
976 /* here ends the main loop */
977
978last_with_slashes:
979 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
980last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -0800981 /* Clear LOOKUP_CONTINUE iff it was previously unset */
982 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (lookup_flags & LOOKUP_PARENT)
984 goto lookup_parent;
985 if (this.name[0] == '.') switch (this.len) {
986 default:
987 break;
988 case 2:
989 if (this.name[1] != '.')
990 break;
Al Viro58c465e2005-06-06 13:36:13 -0700991 follow_dotdot(nd);
Jan Blunck4ac91372008-02-14 19:34:32 -0800992 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* fallthrough */
994 case 1:
995 goto return_reval;
996 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800997 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
998 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
999 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (err < 0)
1001 break;
1002 }
1003 err = do_lookup(nd, &this, &next);
1004 if (err)
1005 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 inode = next.dentry->d_inode;
1007 if ((lookup_flags & LOOKUP_FOLLOW)
1008 && inode && inode->i_op && inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -07001009 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (err)
1011 goto return_err;
Jan Blunck4ac91372008-02-14 19:34:32 -08001012 inode = nd->path.dentry->d_inode;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001013 } else
1014 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 err = -ENOENT;
1016 if (!inode)
1017 break;
1018 if (lookup_flags & LOOKUP_DIRECTORY) {
1019 err = -ENOTDIR;
1020 if (!inode->i_op || !inode->i_op->lookup)
1021 break;
1022 }
1023 goto return_base;
1024lookup_parent:
1025 nd->last = this;
1026 nd->last_type = LAST_NORM;
1027 if (this.name[0] != '.')
1028 goto return_base;
1029 if (this.len == 1)
1030 nd->last_type = LAST_DOT;
1031 else if (this.len == 2 && this.name[1] == '.')
1032 nd->last_type = LAST_DOTDOT;
1033 else
1034 goto return_base;
1035return_reval:
1036 /*
1037 * We bypassed the ordinary revalidation routines.
1038 * We may need to check the cached dentry for staleness.
1039 */
Jan Blunck4ac91372008-02-14 19:34:32 -08001040 if (nd->path.dentry && nd->path.dentry->d_sb &&
1041 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 err = -ESTALE;
1043 /* Note: we do not d_invalidate() */
Jan Blunck4ac91372008-02-14 19:34:32 -08001044 if (!nd->path.dentry->d_op->d_revalidate(
1045 nd->path.dentry, nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
1047 }
1048return_base:
1049 return 0;
1050out_dput:
Jan Blunck1d957f92008-02-14 19:34:35 -08001051 path_put_conditional(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 break;
1053 }
Jan Blunck1d957f92008-02-14 19:34:35 -08001054 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055return_err:
1056 return err;
1057}
1058
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001059static int path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 current->total_link_count = 0;
1062 return link_path_walk(name, nd);
1063}
1064
Prasanna Medaea3834d2005-04-29 16:00:17 +01001065/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001066static int do_path_lookup(int dfd, const char *name,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001067 unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001069 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001070 int fput_needed;
1071 struct file *file;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001072 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1075 nd->flags = flags;
1076 nd->depth = 0;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (*name=='/') {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001079 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001080 nd->path = fs->root;
1081 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001082 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001083 } else if (dfd == AT_FDCWD) {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001084 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001085 nd->path = fs->pwd;
1086 path_get(&fs->pwd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001087 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001088 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001089 struct dentry *dentry;
1090
1091 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001092 retval = -EBADF;
1093 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001094 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001095
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001096 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001097
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001098 retval = -ENOTDIR;
1099 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001100 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001101
1102 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001103 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001104 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001105
Jan Blunck5dd784d2008-02-14 19:34:38 -08001106 nd->path = file->f_path;
1107 path_get(&file->f_path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001108
1109 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Josef 'Jeff' Sipek2dfdd262007-05-09 02:33:41 -07001111
1112 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001113 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1114 nd->path.dentry->d_inode))
1115 audit_inode(name, nd->path.dentry);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001116out_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001117 return retval;
1118
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001119fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001120 fput_light(file, fput_needed);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001121 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001124int path_lookup(const char *name, unsigned int flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001125 struct nameidata *nd)
1126{
1127 return do_path_lookup(AT_FDCWD, name, flags, nd);
1128}
1129
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001130/**
1131 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1132 * @dentry: pointer to dentry of the base directory
1133 * @mnt: pointer to vfs mount of the base directory
1134 * @name: pointer to file name
1135 * @flags: lookup flags
1136 * @nd: pointer to nameidata
1137 */
1138int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1139 const char *name, unsigned int flags,
1140 struct nameidata *nd)
1141{
1142 int retval;
1143
1144 /* same as do_path_lookup */
1145 nd->last_type = LAST_ROOT;
1146 nd->flags = flags;
1147 nd->depth = 0;
1148
Jan Blunckc8e7f442008-06-09 16:40:35 -07001149 nd->path.dentry = dentry;
1150 nd->path.mnt = mnt;
1151 path_get(&nd->path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001152
1153 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001154 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1155 nd->path.dentry->d_inode))
1156 audit_inode(name, nd->path.dentry);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001157
1158 return retval;
1159
1160}
1161
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001162static int __path_lookup_intent_open(int dfd, const char *name,
1163 unsigned int lookup_flags, struct nameidata *nd,
1164 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001165{
1166 struct file *filp = get_empty_filp();
1167 int err;
1168
1169 if (filp == NULL)
1170 return -ENFILE;
1171 nd->intent.open.file = filp;
1172 nd->intent.open.flags = open_flags;
1173 nd->intent.open.create_mode = create_mode;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001174 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001175 if (IS_ERR(nd->intent.open.file)) {
1176 if (err == 0) {
1177 err = PTR_ERR(nd->intent.open.file);
Jan Blunck1d957f92008-02-14 19:34:35 -08001178 path_put(&nd->path);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001179 }
1180 } else if (err != 0)
1181 release_open_intent(nd);
1182 return err;
1183}
1184
1185/**
1186 * path_lookup_open - lookup a file path with open intent
Martin Waitz7045f372006-02-01 03:06:57 -08001187 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001188 * @name: pointer to file name
1189 * @lookup_flags: lookup intent flags
1190 * @nd: pointer to nameidata
1191 * @open_flags: open intent flags
1192 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001193int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001194 struct nameidata *nd, int open_flags)
1195{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001196 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001197 open_flags, 0);
1198}
1199
1200/**
1201 * path_lookup_create - lookup a file path with open + create intent
Martin Waitz7045f372006-02-01 03:06:57 -08001202 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001203 * @name: pointer to file name
1204 * @lookup_flags: lookup intent flags
1205 * @nd: pointer to nameidata
1206 * @open_flags: open intent flags
1207 * @create_mode: create intent flags
1208 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001209static int path_lookup_create(int dfd, const char *name,
1210 unsigned int lookup_flags, struct nameidata *nd,
1211 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001212{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001213 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1214 nd, open_flags, create_mode);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001215}
1216
1217int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1218 struct nameidata *nd, int open_flags)
1219{
1220 char *tmp = getname(name);
1221 int err = PTR_ERR(tmp);
1222
1223 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001224 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001225 putname(tmp);
1226 }
1227 return err;
1228}
1229
Christoph Hellwigeead1912007-10-16 23:25:38 -07001230static struct dentry *__lookup_hash(struct qstr *name,
1231 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
James Morris057f6c02007-04-26 00:12:05 -07001233 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct inode *inode;
1235 int err;
1236
1237 inode = base->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /*
1240 * See if the low-level filesystem might want
1241 * to use its own hash..
1242 */
1243 if (base->d_op && base->d_op->d_hash) {
1244 err = base->d_op->d_hash(base, name);
1245 dentry = ERR_PTR(err);
1246 if (err < 0)
1247 goto out;
1248 }
1249
1250 dentry = cached_lookup(base, name, nd);
1251 if (!dentry) {
Miklos Szeredid70b67c2008-07-02 21:30:15 +02001252 struct dentry *new;
1253
1254 /* Don't create child dentry for a dead directory. */
1255 dentry = ERR_PTR(-ENOENT);
1256 if (IS_DEADDIR(inode))
1257 goto out;
1258
1259 new = d_alloc(base, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 dentry = ERR_PTR(-ENOMEM);
1261 if (!new)
1262 goto out;
1263 dentry = inode->i_op->lookup(inode, new, nd);
1264 if (!dentry)
1265 dentry = new;
1266 else
1267 dput(new);
1268 }
1269out:
1270 return dentry;
1271}
1272
James Morris057f6c02007-04-26 00:12:05 -07001273/*
1274 * Restricted form of lookup. Doesn't follow links, single-component only,
1275 * needs parent already locked. Doesn't follow mounts.
1276 * SMP-safe.
1277 */
Adrian Bunka244e162006-03-31 02:32:11 -08001278static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Christoph Hellwigeead1912007-10-16 23:25:38 -07001280 int err;
1281
Jan Blunck4ac91372008-02-14 19:34:32 -08001282 err = permission(nd->path.dentry->d_inode, MAY_EXEC, nd);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001283 if (err)
1284 return ERR_PTR(err);
Jan Blunck4ac91372008-02-14 19:34:32 -08001285 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Christoph Hellwigeead1912007-10-16 23:25:38 -07001288static int __lookup_one_len(const char *name, struct qstr *this,
1289 struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 unsigned int c;
1293
James Morris057f6c02007-04-26 00:12:05 -07001294 this->name = name;
1295 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001297 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 hash = init_name_hash();
1300 while (len--) {
1301 c = *(const unsigned char *)name++;
1302 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001303 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 hash = partial_name_hash(c, hash);
1305 }
James Morris057f6c02007-04-26 00:12:05 -07001306 this->hash = end_name_hash(hash);
1307 return 0;
1308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Christoph Hellwigeead1912007-10-16 23:25:38 -07001310/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001311 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001312 * @name: pathname component to lookup
1313 * @base: base directory to lookup from
1314 * @len: maximum length @len should be interpreted to
1315 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001316 * Note that this routine is purely a helper for filesystem usage and should
1317 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001318 * nameidata argument is passed to the filesystem methods and a filesystem
1319 * using this helper needs to be prepared for that.
1320 */
James Morris057f6c02007-04-26 00:12:05 -07001321struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1322{
1323 int err;
1324 struct qstr this;
1325
1326 err = __lookup_one_len(name, &this, base, len);
1327 if (err)
1328 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001329
1330 err = permission(base->d_inode, MAY_EXEC, NULL);
1331 if (err)
1332 return ERR_PTR(err);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001333 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001334}
1335
Christoph Hellwigeead1912007-10-16 23:25:38 -07001336/**
1337 * lookup_one_noperm - bad hack for sysfs
1338 * @name: pathname component to lookup
1339 * @base: base directory to lookup from
1340 *
1341 * This is a variant of lookup_one_len that doesn't perform any permission
1342 * checks. It's a horrible hack to work around the braindead sysfs
1343 * architecture and should not be used anywhere else.
1344 *
1345 * DON'T USE THIS FUNCTION EVER, thanks.
1346 */
1347struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
James Morris057f6c02007-04-26 00:12:05 -07001348{
1349 int err;
1350 struct qstr this;
1351
Christoph Hellwigeead1912007-10-16 23:25:38 -07001352 err = __lookup_one_len(name, &this, base, strlen(name));
James Morris057f6c02007-04-26 00:12:05 -07001353 if (err)
1354 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001355 return __lookup_hash(&this, base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001358int __user_walk_fd(int dfd, const char __user *name, unsigned flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001359 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 char *tmp = getname(name);
1362 int err = PTR_ERR(tmp);
1363
1364 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001365 err = do_path_lookup(dfd, tmp, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 putname(tmp);
1367 }
1368 return err;
1369}
1370
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001371int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001372{
1373 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/*
1377 * It's inline, so penalty for filesystems that don't use sticky bit is
1378 * minimal.
1379 */
1380static inline int check_sticky(struct inode *dir, struct inode *inode)
1381{
1382 if (!(dir->i_mode & S_ISVTX))
1383 return 0;
1384 if (inode->i_uid == current->fsuid)
1385 return 0;
1386 if (dir->i_uid == current->fsuid)
1387 return 0;
1388 return !capable(CAP_FOWNER);
1389}
1390
1391/*
1392 * Check whether we can remove a link victim from directory dir, check
1393 * whether the type of victim is right.
1394 * 1. We can't do it if dir is read-only (done in permission())
1395 * 2. We should have write and exec permissions on dir
1396 * 3. We can't remove anything from append-only dir
1397 * 4. We can't do anything with immutable dir (done in permission())
1398 * 5. If the sticky bit on dir is set we should either
1399 * a. be owner of dir, or
1400 * b. be owner of victim, or
1401 * c. have CAP_FOWNER capability
1402 * 6. If the victim is append-only or immutable we can't do antyhing with
1403 * links pointing to it.
1404 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1405 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1406 * 9. We can't remove a root or mountpoint.
1407 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1408 * nfs_async_unlink().
1409 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001410static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 int error;
1413
1414 if (!victim->d_inode)
1415 return -ENOENT;
1416
1417 BUG_ON(victim->d_parent->d_inode != dir);
Al Viro5a190ae2007-06-07 12:19:32 -04001418 audit_inode_child(victim->d_name.name, victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1421 if (error)
1422 return error;
1423 if (IS_APPEND(dir))
1424 return -EPERM;
1425 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1426 IS_IMMUTABLE(victim->d_inode))
1427 return -EPERM;
1428 if (isdir) {
1429 if (!S_ISDIR(victim->d_inode->i_mode))
1430 return -ENOTDIR;
1431 if (IS_ROOT(victim))
1432 return -EBUSY;
1433 } else if (S_ISDIR(victim->d_inode->i_mode))
1434 return -EISDIR;
1435 if (IS_DEADDIR(dir))
1436 return -ENOENT;
1437 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1438 return -EBUSY;
1439 return 0;
1440}
1441
1442/* Check whether we can create an object with dentry child in directory
1443 * dir.
1444 * 1. We can't do it if child already exists (open has special treatment for
1445 * this case, but since we are inlined it's OK)
1446 * 2. We can't do it if dir is read-only (done in permission())
1447 * 3. We should have write and exec permissions on dir
1448 * 4. We can't do it if dir is immutable (done in permission())
1449 */
1450static inline int may_create(struct inode *dir, struct dentry *child,
1451 struct nameidata *nd)
1452{
1453 if (child->d_inode)
1454 return -EEXIST;
1455 if (IS_DEADDIR(dir))
1456 return -ENOENT;
1457 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1458}
1459
1460/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 * O_DIRECTORY translates into forcing a directory lookup.
1462 */
1463static inline int lookup_flags(unsigned int f)
1464{
1465 unsigned long retval = LOOKUP_FOLLOW;
1466
1467 if (f & O_NOFOLLOW)
1468 retval &= ~LOOKUP_FOLLOW;
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (f & O_DIRECTORY)
1471 retval |= LOOKUP_DIRECTORY;
1472
1473 return retval;
1474}
1475
1476/*
1477 * p1 and p2 should be directories on the same fs.
1478 */
1479struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1480{
1481 struct dentry *p;
1482
1483 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001484 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return NULL;
1486 }
1487
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001488 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 for (p = p1; p->d_parent != p; p = p->d_parent) {
1491 if (p->d_parent == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001492 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1493 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return p;
1495 }
1496 }
1497
1498 for (p = p2; p->d_parent != p; p = p->d_parent) {
1499 if (p->d_parent == p1) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001500 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1501 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return p;
1503 }
1504 }
1505
Ingo Molnarf2eace22006-07-03 00:25:05 -07001506 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1507 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return NULL;
1509}
1510
1511void unlock_rename(struct dentry *p1, struct dentry *p2)
1512{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001513 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001515 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001516 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518}
1519
1520int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1521 struct nameidata *nd)
1522{
1523 int error = may_create(dir, dentry, nd);
1524
1525 if (error)
1526 return error;
1527
1528 if (!dir->i_op || !dir->i_op->create)
1529 return -EACCES; /* shouldn't it be ENOSYS? */
1530 mode &= S_IALLUGO;
1531 mode |= S_IFREG;
1532 error = security_inode_create(dir, dentry, mode);
1533 if (error)
1534 return error;
1535 DQUOT_INIT(dir);
1536 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001537 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001538 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return error;
1540}
1541
1542int may_open(struct nameidata *nd, int acc_mode, int flag)
1543{
Jan Blunck4ac91372008-02-14 19:34:32 -08001544 struct dentry *dentry = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct inode *inode = dentry->d_inode;
1546 int error;
1547
1548 if (!inode)
1549 return -ENOENT;
1550
1551 if (S_ISLNK(inode->i_mode))
1552 return -ELOOP;
1553
Linus Torvalds974a9f02008-01-12 14:06:34 -08001554 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return -EISDIR;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /*
1558 * FIFO's, sockets and device files are special: they don't
1559 * actually live on the filesystem itself, and as such you
1560 * can write to them even if the filesystem is read-only.
1561 */
1562 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1563 flag &= ~O_TRUNC;
1564 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001565 if (nd->path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return -EACCES;
1567
1568 flag &= ~O_TRUNC;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001569 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001570
1571 error = vfs_permission(nd, acc_mode);
1572 if (error)
1573 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /*
1575 * An append-only file must be opened in append mode for writing.
1576 */
1577 if (IS_APPEND(inode)) {
1578 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1579 return -EPERM;
1580 if (flag & O_TRUNC)
1581 return -EPERM;
1582 }
1583
1584 /* O_NOATIME can only be set by the owner or superuser */
1585 if (flag & O_NOATIME)
Satyam Sharma3bd858a2007-07-17 15:00:08 +05301586 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -EPERM;
1588
1589 /*
1590 * Ensure there are no outstanding leases on the file.
1591 */
1592 error = break_lease(inode, flag);
1593 if (error)
1594 return error;
1595
1596 if (flag & O_TRUNC) {
1597 error = get_write_access(inode);
1598 if (error)
1599 return error;
1600
1601 /*
1602 * Refuse to truncate files with mandatory locks held on them.
1603 */
1604 error = locks_verify_locked(inode);
1605 if (!error) {
1606 DQUOT_INIT(inode);
Miklos Szeredid139d7f2007-10-18 03:07:00 -07001607
1608 error = do_truncate(dentry, 0,
1609 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1610 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 put_write_access(inode);
1613 if (error)
1614 return error;
1615 } else
1616 if (flag & FMODE_WRITE)
1617 DQUOT_INIT(inode);
1618
1619 return 0;
1620}
1621
Dave Hansend57999e2008-02-15 14:37:27 -08001622/*
1623 * Be careful about ever adding any more callers of this
1624 * function. Its flags must be in the namei format, not
1625 * what get passed to sys_open().
1626 */
1627static int __open_namei_create(struct nameidata *nd, struct path *path,
Dave Hansenaab520e2006-09-30 23:29:02 -07001628 int flag, int mode)
1629{
1630 int error;
Jan Blunck4ac91372008-02-14 19:34:32 -08001631 struct dentry *dir = nd->path.dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001632
1633 if (!IS_POSIXACL(dir->d_inode))
1634 mode &= ~current->fs->umask;
1635 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1636 mutex_unlock(&dir->d_inode->i_mutex);
Jan Blunck4ac91372008-02-14 19:34:32 -08001637 dput(nd->path.dentry);
1638 nd->path.dentry = path->dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001639 if (error)
1640 return error;
1641 /* Don't check for write permission, don't truncate */
1642 return may_open(nd, 0, flag & ~O_TRUNC);
1643}
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645/*
Dave Hansend57999e2008-02-15 14:37:27 -08001646 * Note that while the flag value (low two bits) for sys_open means:
1647 * 00 - read-only
1648 * 01 - write-only
1649 * 10 - read-write
1650 * 11 - special
1651 * it is changed into
1652 * 00 - no permissions needed
1653 * 01 - read-permission
1654 * 10 - write-permission
1655 * 11 - read-write
1656 * for the internal routines (ie open_namei()/follow_link() etc)
1657 * This is more logical, and also allows the 00 "no perm needed"
1658 * to be used for symlinks (where the permissions are checked
1659 * later).
1660 *
1661*/
1662static inline int open_to_namei_flags(int flag)
1663{
1664 if ((flag+1) & O_ACCMODE)
1665 flag++;
1666 return flag;
1667}
1668
Dave Hansen4a3fd212008-02-15 14:37:48 -08001669static int open_will_write_to_fs(int flag, struct inode *inode)
1670{
1671 /*
1672 * We'll never write to the fs underlying
1673 * a device file.
1674 */
1675 if (special_file(inode->i_mode))
1676 return 0;
1677 return (flag & O_TRUNC);
1678}
1679
Dave Hansend57999e2008-02-15 14:37:27 -08001680/*
Dave Hansen4a3fd212008-02-15 14:37:48 -08001681 * Note that the low bits of the passed in "open_flag"
1682 * are not the same as in the local variable "flag". See
1683 * open_to_namei_flags() for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001685struct file *do_filp_open(int dfd, const char *pathname,
1686 int open_flag, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Dave Hansen4a3fd212008-02-15 14:37:48 -08001688 struct file *filp;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001689 struct nameidata nd;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001690 int acc_mode, error;
Al Viro4e7506e2005-06-06 13:36:00 -07001691 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct dentry *dir;
1693 int count = 0;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001694 int will_write;
Dave Hansend57999e2008-02-15 14:37:27 -08001695 int flag = open_to_namei_flags(open_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 acc_mode = ACC_MODE(flag);
1698
Trond Myklebust834f2a42005-10-18 14:20:16 -07001699 /* O_TRUNC implies we need access checks for write permissions */
1700 if (flag & O_TRUNC)
1701 acc_mode |= MAY_WRITE;
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /* Allow the LSM permission hook to distinguish append
1704 access from general write access. */
1705 if (flag & O_APPEND)
1706 acc_mode |= MAY_APPEND;
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /*
1709 * The simplest case - just a plain lookup.
1710 */
1711 if (!(flag & O_CREAT)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001712 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001713 &nd, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001715 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto ok;
1717 }
1718
1719 /*
1720 * Create - we need to know the parent.
1721 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001722 error = path_lookup_create(dfd, pathname, LOOKUP_PARENT,
1723 &nd, flag, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001725 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 /*
1728 * We have the parent and last component. First of all, check
1729 * that we are not asked to creat(2) an obvious directory - that
1730 * will not do.
1731 */
1732 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001733 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 goto exit;
1735
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001736 dir = nd.path.dentry;
1737 nd.flags &= ~LOOKUP_PARENT;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001738 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001739 path.dentry = lookup_hash(&nd);
1740 path.mnt = nd.path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742do_last:
Al Viro4e7506e2005-06-06 13:36:00 -07001743 error = PTR_ERR(path.dentry);
1744 if (IS_ERR(path.dentry)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001745 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto exit;
1747 }
1748
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001749 if (IS_ERR(nd.intent.open.file)) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001750 error = PTR_ERR(nd.intent.open.file);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001751 goto exit_mutex_unlock;
Oleg Drokin4af4c522006-03-25 03:06:54 -08001752 }
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /* Negative dentry, just create the file */
Al Viro4e7506e2005-06-06 13:36:00 -07001755 if (!path.dentry->d_inode) {
Dave Hansen4a3fd212008-02-15 14:37:48 -08001756 /*
1757 * This write is needed to ensure that a
1758 * ro->rw transition does not occur between
1759 * the time when the file is created and when
1760 * a permanent write count is taken through
1761 * the 'struct file' in nameidata_to_filp().
1762 */
1763 error = mnt_want_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (error)
Dave Hansen4a3fd212008-02-15 14:37:48 -08001765 goto exit_mutex_unlock;
1766 error = __open_namei_create(&nd, &path, flag, mode);
1767 if (error) {
1768 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001770 }
1771 filp = nameidata_to_filp(&nd, open_flag);
1772 mnt_drop_write(nd.path.mnt);
1773 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
1776 /*
1777 * It already exists.
1778 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001779 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro5a190ae2007-06-07 12:19:32 -04001780 audit_inode(pathname, path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 error = -EEXIST;
1783 if (flag & O_EXCL)
1784 goto exit_dput;
1785
Al Viroe13b2102005-06-06 13:36:06 -07001786 if (__follow_mount(&path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 error = -ELOOP;
Al Viroba7a4c12005-06-06 13:36:08 -07001788 if (flag & O_NOFOLLOW)
1789 goto exit_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 error = -ENOENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001793 if (!path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto exit_dput;
Al Viro4e7506e2005-06-06 13:36:00 -07001795 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 goto do_link;
1797
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001798 path_to_nameidata(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 error = -EISDIR;
Al Viro4e7506e2005-06-06 13:36:00 -07001800 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 goto exit;
1802ok:
Dave Hansen4a3fd212008-02-15 14:37:48 -08001803 /*
1804 * Consider:
1805 * 1. may_open() truncates a file
1806 * 2. a rw->ro mount transition occurs
1807 * 3. nameidata_to_filp() fails due to
1808 * the ro mount.
1809 * That would be inconsistent, and should
1810 * be avoided. Taking this mnt write here
1811 * ensures that (2) can not occur.
1812 */
1813 will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
1814 if (will_write) {
1815 error = mnt_want_write(nd.path.mnt);
1816 if (error)
1817 goto exit;
1818 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001819 error = may_open(&nd, acc_mode, flag);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001820 if (error) {
1821 if (will_write)
1822 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001824 }
1825 filp = nameidata_to_filp(&nd, open_flag);
1826 /*
1827 * It is now safe to drop the mnt write
1828 * because the filp has had a write taken
1829 * on its behalf.
1830 */
1831 if (will_write)
1832 mnt_drop_write(nd.path.mnt);
1833 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Dave Hansen4a3fd212008-02-15 14:37:48 -08001835exit_mutex_unlock:
1836 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837exit_dput:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001838 path_put_conditional(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839exit:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001840 if (!IS_ERR(nd.intent.open.file))
1841 release_open_intent(&nd);
1842 path_put(&nd.path);
1843 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845do_link:
1846 error = -ELOOP;
1847 if (flag & O_NOFOLLOW)
1848 goto exit_dput;
1849 /*
1850 * This is subtle. Instead of calling do_follow_link() we do the
1851 * thing by hands. The reason is that this way we have zero link_count
1852 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1853 * After that we have the parent and last component, i.e.
1854 * we are in the same situation as after the first path_walk().
1855 * Well, almost - if the last component is normal we get its copy
1856 * stored in nd->last.name and we will have to putname() it when we
1857 * are done. Procfs-like symlinks just set LAST_BIND.
1858 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001859 nd.flags |= LOOKUP_PARENT;
1860 error = security_inode_follow_link(path.dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (error)
1862 goto exit_dput;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001863 error = __do_follow_link(&path, &nd);
Kirill Korotaevde459212006-07-14 00:23:49 -07001864 if (error) {
1865 /* Does someone understand code flow here? Or it is only
1866 * me so stupid? Anathema to whoever designed this non-sense
1867 * with "intent.open".
1868 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001869 release_open_intent(&nd);
1870 return ERR_PTR(error);
Kirill Korotaevde459212006-07-14 00:23:49 -07001871 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001872 nd.flags &= ~LOOKUP_PARENT;
1873 if (nd.last_type == LAST_BIND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001876 if (nd.last_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto exit;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001878 if (nd.last.name[nd.last.len]) {
1879 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto exit;
1881 }
1882 error = -ELOOP;
1883 if (count++==32) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001884 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 goto exit;
1886 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001887 dir = nd.path.dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001888 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001889 path.dentry = lookup_hash(&nd);
1890 path.mnt = nd.path.mnt;
1891 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 goto do_last;
1893}
1894
1895/**
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001896 * filp_open - open file and return file pointer
1897 *
1898 * @filename: path to open
1899 * @flags: open flags as per the open(2) second argument
1900 * @mode: mode for the new file if O_CREAT is set, else ignored
1901 *
1902 * This is the helper to open a file from kernelspace if you really
1903 * have to. But in generally you should not do this, so please move
1904 * along, nothing to see here..
1905 */
1906struct file *filp_open(const char *filename, int flags, int mode)
1907{
1908 return do_filp_open(AT_FDCWD, filename, flags, mode);
1909}
1910EXPORT_SYMBOL(filp_open);
1911
1912/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 * lookup_create - lookup a dentry, creating it if it doesn't exist
1914 * @nd: nameidata info
1915 * @is_dir: directory flag
1916 *
1917 * Simple function to lookup and return a dentry and create it
1918 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001919 *
Jan Blunck4ac91372008-02-14 19:34:32 -08001920 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
1922struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1923{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001924 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Jan Blunck4ac91372008-02-14 19:34:32 -08001926 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001927 /*
1928 * Yucky last component or no last component at all?
1929 * (foo/., foo/.., /////)
1930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (nd->last_type != LAST_NORM)
1932 goto fail;
1933 nd->flags &= ~LOOKUP_PARENT;
ASANO Masahiroa6349042006-08-22 20:06:02 -04001934 nd->flags |= LOOKUP_CREATE;
1935 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001936
1937 /*
1938 * Do the final lookup.
1939 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08001940 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (IS_ERR(dentry))
1942 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001943
Al Viroe9baf6e2008-05-15 04:49:12 -04001944 if (dentry->d_inode)
1945 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001946 /*
1947 * Special case - lookup gave negative, but... we had foo/bar/
1948 * From the vfs_mknod() POV we just have a negative dentry -
1949 * all is fine. Let's be bastards - you had / on the end, you've
1950 * been asking for (non-existent) directory. -ENOENT for you.
1951 */
Al Viroe9baf6e2008-05-15 04:49:12 -04001952 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
1953 dput(dentry);
1954 dentry = ERR_PTR(-ENOENT);
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04001957eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04001959 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960fail:
1961 return dentry;
1962}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07001963EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1966{
1967 int error = may_create(dir, dentry, NULL);
1968
1969 if (error)
1970 return error;
1971
1972 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1973 return -EPERM;
1974
1975 if (!dir->i_op || !dir->i_op->mknod)
1976 return -EPERM;
1977
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07001978 error = devcgroup_inode_mknod(mode, dev);
1979 if (error)
1980 return error;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 error = security_inode_mknod(dir, dentry, mode, dev);
1983 if (error)
1984 return error;
1985
1986 DQUOT_INIT(dir);
1987 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001988 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001989 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return error;
1991}
1992
Dave Hansen463c3192008-02-15 14:37:57 -08001993static int may_mknod(mode_t mode)
1994{
1995 switch (mode & S_IFMT) {
1996 case S_IFREG:
1997 case S_IFCHR:
1998 case S_IFBLK:
1999 case S_IFIFO:
2000 case S_IFSOCK:
2001 case 0: /* zero mode translates to S_IFREG */
2002 return 0;
2003 case S_IFDIR:
2004 return -EPERM;
2005 default:
2006 return -EINVAL;
2007 }
2008}
2009
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002010asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
2011 unsigned dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 int error = 0;
2014 char * tmp;
2015 struct dentry * dentry;
2016 struct nameidata nd;
2017
2018 if (S_ISDIR(mode))
2019 return -EPERM;
2020 tmp = getname(filename);
2021 if (IS_ERR(tmp))
2022 return PTR_ERR(tmp);
2023
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002024 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (error)
2026 goto out;
2027 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002028 if (IS_ERR(dentry)) {
2029 error = PTR_ERR(dentry);
2030 goto out_unlock;
2031 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002032 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002034 error = may_mknod(mode);
2035 if (error)
2036 goto out_dput;
2037 error = mnt_want_write(nd.path.mnt);
2038 if (error)
2039 goto out_dput;
2040 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002042 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 break;
2044 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002045 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 new_decode_dev(dev));
2047 break;
2048 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002049 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
Dave Hansen463c3192008-02-15 14:37:57 -08002052 mnt_drop_write(nd.path.mnt);
2053out_dput:
2054 dput(dentry);
2055out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002056 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002057 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058out:
2059 putname(tmp);
2060
2061 return error;
2062}
2063
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002064asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
2065{
2066 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2067}
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2070{
2071 int error = may_create(dir, dentry, NULL);
2072
2073 if (error)
2074 return error;
2075
2076 if (!dir->i_op || !dir->i_op->mkdir)
2077 return -EPERM;
2078
2079 mode &= (S_IRWXUGO|S_ISVTX);
2080 error = security_inode_mkdir(dir, dentry, mode);
2081 if (error)
2082 return error;
2083
2084 DQUOT_INIT(dir);
2085 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002086 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002087 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return error;
2089}
2090
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002091asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 int error = 0;
2094 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002095 struct dentry *dentry;
2096 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 tmp = getname(pathname);
2099 error = PTR_ERR(tmp);
Dave Hansen6902d922006-09-30 23:29:01 -07002100 if (IS_ERR(tmp))
2101 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Dave Hansen6902d922006-09-30 23:29:01 -07002103 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2104 if (error)
2105 goto out;
2106 dentry = lookup_create(&nd, 1);
2107 error = PTR_ERR(dentry);
2108 if (IS_ERR(dentry))
2109 goto out_unlock;
2110
Jan Blunck4ac91372008-02-14 19:34:32 -08002111 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Dave Hansen6902d922006-09-30 23:29:01 -07002112 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002113 error = mnt_want_write(nd.path.mnt);
2114 if (error)
2115 goto out_dput;
Jan Blunck4ac91372008-02-14 19:34:32 -08002116 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Dave Hansen463c3192008-02-15 14:37:57 -08002117 mnt_drop_write(nd.path.mnt);
2118out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002119 dput(dentry);
2120out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002121 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002122 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123out:
Dave Hansen6902d922006-09-30 23:29:01 -07002124 putname(tmp);
2125out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return error;
2127}
2128
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002129asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2130{
2131 return sys_mkdirat(AT_FDCWD, pathname, mode);
2132}
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134/*
2135 * We try to drop the dentry early: we should have
2136 * a usage count of 2 if we're the only user of this
2137 * dentry, and if that is true (possibly after pruning
2138 * the dcache), then we drop the dentry now.
2139 *
2140 * A low-level filesystem can, if it choses, legally
2141 * do a
2142 *
2143 * if (!d_unhashed(dentry))
2144 * return -EBUSY;
2145 *
2146 * if it cannot handle the case of removing a directory
2147 * that is still in use by something else..
2148 */
2149void dentry_unhash(struct dentry *dentry)
2150{
2151 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002152 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 spin_lock(&dcache_lock);
2154 spin_lock(&dentry->d_lock);
2155 if (atomic_read(&dentry->d_count) == 2)
2156 __d_drop(dentry);
2157 spin_unlock(&dentry->d_lock);
2158 spin_unlock(&dcache_lock);
2159}
2160
2161int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2162{
2163 int error = may_delete(dir, dentry, 1);
2164
2165 if (error)
2166 return error;
2167
2168 if (!dir->i_op || !dir->i_op->rmdir)
2169 return -EPERM;
2170
2171 DQUOT_INIT(dir);
2172
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002173 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 dentry_unhash(dentry);
2175 if (d_mountpoint(dentry))
2176 error = -EBUSY;
2177 else {
2178 error = security_inode_rmdir(dir, dentry);
2179 if (!error) {
2180 error = dir->i_op->rmdir(dir, dentry);
2181 if (!error)
2182 dentry->d_inode->i_flags |= S_DEAD;
2183 }
2184 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002185 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 d_delete(dentry);
2188 }
2189 dput(dentry);
2190
2191 return error;
2192}
2193
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002194static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 int error = 0;
2197 char * name;
2198 struct dentry *dentry;
2199 struct nameidata nd;
2200
2201 name = getname(pathname);
2202 if(IS_ERR(name))
2203 return PTR_ERR(name);
2204
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002205 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (error)
2207 goto exit;
2208
2209 switch(nd.last_type) {
2210 case LAST_DOTDOT:
2211 error = -ENOTEMPTY;
2212 goto exit1;
2213 case LAST_DOT:
2214 error = -EINVAL;
2215 goto exit1;
2216 case LAST_ROOT:
2217 error = -EBUSY;
2218 goto exit1;
2219 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002220 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002221 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002223 if (IS_ERR(dentry))
2224 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002225 error = mnt_want_write(nd.path.mnt);
2226 if (error)
2227 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002228 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08002229 mnt_drop_write(nd.path.mnt);
2230exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002231 dput(dentry);
2232exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002233 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002235 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236exit:
2237 putname(name);
2238 return error;
2239}
2240
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002241asmlinkage long sys_rmdir(const char __user *pathname)
2242{
2243 return do_rmdir(AT_FDCWD, pathname);
2244}
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246int vfs_unlink(struct inode *dir, struct dentry *dentry)
2247{
2248 int error = may_delete(dir, dentry, 0);
2249
2250 if (error)
2251 return error;
2252
2253 if (!dir->i_op || !dir->i_op->unlink)
2254 return -EPERM;
2255
2256 DQUOT_INIT(dir);
2257
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002258 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (d_mountpoint(dentry))
2260 error = -EBUSY;
2261 else {
2262 error = security_inode_unlink(dir, dentry);
2263 if (!error)
2264 error = dir->i_op->unlink(dir, dentry);
2265 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002266 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2269 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002270 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
Robert Love0eeca282005-07-12 17:06:03 -04002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return error;
2275}
2276
2277/*
2278 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002279 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 * writeout happening, and we don't want to prevent access to the directory
2281 * while waiting on the I/O.
2282 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002283static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 int error = 0;
2286 char * name;
2287 struct dentry *dentry;
2288 struct nameidata nd;
2289 struct inode *inode = NULL;
2290
2291 name = getname(pathname);
2292 if(IS_ERR(name))
2293 return PTR_ERR(name);
2294
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002295 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (error)
2297 goto exit;
2298 error = -EISDIR;
2299 if (nd.last_type != LAST_NORM)
2300 goto exit1;
Jan Blunck4ac91372008-02-14 19:34:32 -08002301 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002302 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 error = PTR_ERR(dentry);
2304 if (!IS_ERR(dentry)) {
2305 /* Why not before? Because we want correct error value */
2306 if (nd.last.name[nd.last.len])
2307 goto slashes;
2308 inode = dentry->d_inode;
2309 if (inode)
2310 atomic_inc(&inode->i_count);
Dave Hansen06227532008-02-15 14:37:34 -08002311 error = mnt_want_write(nd.path.mnt);
2312 if (error)
2313 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08002314 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08002315 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 exit2:
2317 dput(dentry);
2318 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002319 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (inode)
2321 iput(inode); /* truncate the inode here */
2322exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002323 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324exit:
2325 putname(name);
2326 return error;
2327
2328slashes:
2329 error = !dentry->d_inode ? -ENOENT :
2330 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2331 goto exit2;
2332}
2333
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002334asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2335{
2336 if ((flag & ~AT_REMOVEDIR) != 0)
2337 return -EINVAL;
2338
2339 if (flag & AT_REMOVEDIR)
2340 return do_rmdir(dfd, pathname);
2341
2342 return do_unlinkat(dfd, pathname);
2343}
2344
2345asmlinkage long sys_unlink(const char __user *pathname)
2346{
2347 return do_unlinkat(AT_FDCWD, pathname);
2348}
2349
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002350int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 int error = may_create(dir, dentry, NULL);
2353
2354 if (error)
2355 return error;
2356
2357 if (!dir->i_op || !dir->i_op->symlink)
2358 return -EPERM;
2359
2360 error = security_inode_symlink(dir, dentry, oldname);
2361 if (error)
2362 return error;
2363
2364 DQUOT_INIT(dir);
2365 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002366 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002367 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return error;
2369}
2370
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002371asmlinkage long sys_symlinkat(const char __user *oldname,
2372 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
2374 int error = 0;
2375 char * from;
2376 char * to;
Dave Hansen6902d922006-09-30 23:29:01 -07002377 struct dentry *dentry;
2378 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 from = getname(oldname);
2381 if(IS_ERR(from))
2382 return PTR_ERR(from);
2383 to = getname(newname);
2384 error = PTR_ERR(to);
Dave Hansen6902d922006-09-30 23:29:01 -07002385 if (IS_ERR(to))
2386 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Dave Hansen6902d922006-09-30 23:29:01 -07002388 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2389 if (error)
2390 goto out;
2391 dentry = lookup_create(&nd, 0);
2392 error = PTR_ERR(dentry);
2393 if (IS_ERR(dentry))
2394 goto out_unlock;
2395
Dave Hansen75c3f292008-02-15 14:37:45 -08002396 error = mnt_want_write(nd.path.mnt);
2397 if (error)
2398 goto out_dput;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002399 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Dave Hansen75c3f292008-02-15 14:37:45 -08002400 mnt_drop_write(nd.path.mnt);
2401out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002402 dput(dentry);
2403out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002404 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002405 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406out:
Dave Hansen6902d922006-09-30 23:29:01 -07002407 putname(to);
2408out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 putname(from);
2410 return error;
2411}
2412
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002413asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2414{
2415 return sys_symlinkat(oldname, AT_FDCWD, newname);
2416}
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2419{
2420 struct inode *inode = old_dentry->d_inode;
2421 int error;
2422
2423 if (!inode)
2424 return -ENOENT;
2425
2426 error = may_create(dir, new_dentry, NULL);
2427 if (error)
2428 return error;
2429
2430 if (dir->i_sb != inode->i_sb)
2431 return -EXDEV;
2432
2433 /*
2434 * A link to an append-only or immutable file cannot be created.
2435 */
2436 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2437 return -EPERM;
2438 if (!dir->i_op || !dir->i_op->link)
2439 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002440 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return -EPERM;
2442
2443 error = security_inode_link(old_dentry, dir, new_dentry);
2444 if (error)
2445 return error;
2446
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002447 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 DQUOT_INIT(dir);
2449 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002450 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002451 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002452 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return error;
2454}
2455
2456/*
2457 * Hardlinks are often used in delicate situations. We avoid
2458 * security-related surprises by not following symlinks on the
2459 * newname. --KAB
2460 *
2461 * We don't follow them on the oldname either to be compatible
2462 * with linux 2.0, and to avoid hard-linking to directories
2463 * and other special files. --ADM
2464 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002465asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002466 int newdfd, const char __user *newname,
2467 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
2469 struct dentry *new_dentry;
2470 struct nameidata nd, old_nd;
2471 int error;
2472 char * to;
2473
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002474 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002475 return -EINVAL;
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 to = getname(newname);
2478 if (IS_ERR(to))
2479 return PTR_ERR(to);
2480
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002481 error = __user_walk_fd(olddfd, oldname,
2482 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2483 &old_nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (error)
2485 goto exit;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002486 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (error)
2488 goto out;
2489 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08002490 if (old_nd.path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto out_release;
2492 new_dentry = lookup_create(&nd, 0);
2493 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002494 if (IS_ERR(new_dentry))
2495 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002496 error = mnt_want_write(nd.path.mnt);
2497 if (error)
2498 goto out_dput;
Jan Blunck4ac91372008-02-14 19:34:32 -08002499 error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08002500 mnt_drop_write(nd.path.mnt);
2501out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002502 dput(new_dentry);
2503out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002504 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002506 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507out:
Jan Blunck1d957f92008-02-14 19:34:35 -08002508 path_put(&old_nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509exit:
2510 putname(to);
2511
2512 return error;
2513}
2514
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002515asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2516{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002517 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520/*
2521 * The worst of all namespace operations - renaming directory. "Perverted"
2522 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2523 * Problems:
2524 * a) we can get into loop creation. Check is done in is_subdir().
2525 * b) race potential - two innocent renames can create a loop together.
2526 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002527 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 * story.
2529 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002530 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 * whether the target exists). Solution: try to be smart with locking
2532 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002533 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 * move will be locked. Thus we can rank directories by the tree
2535 * (ancestors first) and rank all non-directories after them.
2536 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002537 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 * HOWEVER, it relies on the assumption that any object with ->lookup()
2539 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2540 * we'd better make sure that there's no link(2) for them.
2541 * d) some filesystems don't support opened-but-unlinked directories,
2542 * either because of layout or because they are not ready to deal with
2543 * all cases correctly. The latter will be fixed (taking this sort of
2544 * stuff into VFS), but the former is not going away. Solution: the same
2545 * trick as in rmdir().
2546 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002547 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002549 * ->i_mutex on parents, which works but leads to some truely excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 * locking].
2551 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002552static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2553 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 int error = 0;
2556 struct inode *target;
2557
2558 /*
2559 * If we are going to change the parent - check write permissions,
2560 * we'll need to flip '..'.
2561 */
2562 if (new_dir != old_dir) {
2563 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2564 if (error)
2565 return error;
2566 }
2567
2568 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2569 if (error)
2570 return error;
2571
2572 target = new_dentry->d_inode;
2573 if (target) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002574 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 dentry_unhash(new_dentry);
2576 }
2577 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2578 error = -EBUSY;
2579 else
2580 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2581 if (target) {
2582 if (!error)
2583 target->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002584 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (d_unhashed(new_dentry))
2586 d_rehash(new_dentry);
2587 dput(new_dentry);
2588 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002589 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002590 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2591 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return error;
2593}
2594
Adrian Bunk75c96f82005-05-05 16:16:09 -07002595static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2596 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
2598 struct inode *target;
2599 int error;
2600
2601 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2602 if (error)
2603 return error;
2604
2605 dget(new_dentry);
2606 target = new_dentry->d_inode;
2607 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002608 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2610 error = -EBUSY;
2611 else
2612 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2613 if (!error) {
Mark Fasheh349457c2006-09-08 14:22:21 -07002614 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002618 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 dput(new_dentry);
2620 return error;
2621}
2622
2623int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2624 struct inode *new_dir, struct dentry *new_dentry)
2625{
2626 int error;
2627 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Robert Love0eeca282005-07-12 17:06:03 -04002628 const char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 if (old_dentry->d_inode == new_dentry->d_inode)
2631 return 0;
2632
2633 error = may_delete(old_dir, old_dentry, is_dir);
2634 if (error)
2635 return error;
2636
2637 if (!new_dentry->d_inode)
2638 error = may_create(new_dir, new_dentry, NULL);
2639 else
2640 error = may_delete(new_dir, new_dentry, is_dir);
2641 if (error)
2642 return error;
2643
2644 if (!old_dir->i_op || !old_dir->i_op->rename)
2645 return -EPERM;
2646
2647 DQUOT_INIT(old_dir);
2648 DQUOT_INIT(new_dir);
2649
Robert Love0eeca282005-07-12 17:06:03 -04002650 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (is_dir)
2653 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2654 else
2655 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2656 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -04002657 const char *new_name = old_dentry->d_name.name;
John McCutchan89204c42005-08-15 12:13:28 -04002658 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04002659 new_dentry->d_inode, old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
Robert Love0eeca282005-07-12 17:06:03 -04002661 fsnotify_oldname_free(old_name);
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return error;
2664}
2665
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002666static int do_rename(int olddfd, const char *oldname,
2667 int newdfd, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 int error = 0;
2670 struct dentry * old_dir, * new_dir;
2671 struct dentry * old_dentry, *new_dentry;
2672 struct dentry * trap;
2673 struct nameidata oldnd, newnd;
2674
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002675 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (error)
2677 goto exit;
2678
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002679 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (error)
2681 goto exit1;
2682
2683 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08002684 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 goto exit2;
2686
Jan Blunck4ac91372008-02-14 19:34:32 -08002687 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 error = -EBUSY;
2689 if (oldnd.last_type != LAST_NORM)
2690 goto exit2;
2691
Jan Blunck4ac91372008-02-14 19:34:32 -08002692 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (newnd.last_type != LAST_NORM)
2694 goto exit2;
2695
2696 trap = lock_rename(new_dir, old_dir);
2697
Christoph Hellwig49705b72005-11-08 21:35:06 -08002698 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 error = PTR_ERR(old_dentry);
2700 if (IS_ERR(old_dentry))
2701 goto exit3;
2702 /* source must exist */
2703 error = -ENOENT;
2704 if (!old_dentry->d_inode)
2705 goto exit4;
2706 /* unless the source is a directory trailing slashes give -ENOTDIR */
2707 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2708 error = -ENOTDIR;
2709 if (oldnd.last.name[oldnd.last.len])
2710 goto exit4;
2711 if (newnd.last.name[newnd.last.len])
2712 goto exit4;
2713 }
2714 /* source should not be ancestor of target */
2715 error = -EINVAL;
2716 if (old_dentry == trap)
2717 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08002718 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 error = PTR_ERR(new_dentry);
2720 if (IS_ERR(new_dentry))
2721 goto exit4;
2722 /* target should not be an ancestor of source */
2723 error = -ENOTEMPTY;
2724 if (new_dentry == trap)
2725 goto exit5;
2726
Dave Hansen9079b1e2008-02-15 14:37:49 -08002727 error = mnt_want_write(oldnd.path.mnt);
2728 if (error)
2729 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 error = vfs_rename(old_dir->d_inode, old_dentry,
2731 new_dir->d_inode, new_dentry);
Dave Hansen9079b1e2008-02-15 14:37:49 -08002732 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733exit5:
2734 dput(new_dentry);
2735exit4:
2736 dput(old_dentry);
2737exit3:
2738 unlock_rename(new_dir, old_dir);
2739exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08002740 path_put(&newnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002742 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743exit:
2744 return error;
2745}
2746
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002747asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2748 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
2750 int error;
2751 char * from;
2752 char * to;
2753
2754 from = getname(oldname);
2755 if(IS_ERR(from))
2756 return PTR_ERR(from);
2757 to = getname(newname);
2758 error = PTR_ERR(to);
2759 if (!IS_ERR(to)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002760 error = do_rename(olddfd, from, newdfd, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 putname(to);
2762 }
2763 putname(from);
2764 return error;
2765}
2766
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002767asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2768{
2769 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2773{
2774 int len;
2775
2776 len = PTR_ERR(link);
2777 if (IS_ERR(link))
2778 goto out;
2779
2780 len = strlen(link);
2781 if (len > (unsigned) buflen)
2782 len = buflen;
2783 if (copy_to_user(buffer, link, len))
2784 len = -EFAULT;
2785out:
2786 return len;
2787}
2788
2789/*
2790 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2791 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2792 * using) it for any given inode is up to filesystem.
2793 */
2794int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2795{
2796 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002797 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07002798 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002801 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07002802 if (IS_ERR(cookie))
2803 return PTR_ERR(cookie);
2804
2805 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2806 if (dentry->d_inode->i_op->put_link)
2807 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2808 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809}
2810
2811int vfs_follow_link(struct nameidata *nd, const char *link)
2812{
2813 return __vfs_follow_link(nd, link);
2814}
2815
2816/* get the link contents into pagecache */
2817static char *page_getlink(struct dentry * dentry, struct page **ppage)
2818{
2819 struct page * page;
2820 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07002821 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07002823 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 *ppage = page;
2825 return kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2829{
2830 struct page *page = NULL;
2831 char *s = page_getlink(dentry, &page);
2832 int res = vfs_readlink(dentry,buffer,buflen,s);
2833 if (page) {
2834 kunmap(page);
2835 page_cache_release(page);
2836 }
2837 return res;
2838}
2839
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002840void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002842 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002844 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002847void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002849 struct page *page = cookie;
2850
2851 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 kunmap(page);
2853 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
2855}
2856
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002857int __page_symlink(struct inode *inode, const char *symname, int len,
2858 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
2860 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002861 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002862 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08002863 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 char *kaddr;
2865
NeilBrown7e53cac2006-03-25 03:07:57 -08002866retry:
Nick Pigginafddba42007-10-16 01:25:01 -07002867 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2868 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07002870 goto fail;
2871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 kaddr = kmap_atomic(page, KM_USER0);
2873 memcpy(kaddr, symname, len-1);
2874 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07002875
2876 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2877 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (err < 0)
2879 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07002880 if (err < len-1)
2881 goto retry;
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 mark_inode_dirty(inode);
2884 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885fail:
2886 return err;
2887}
2888
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002889int page_symlink(struct inode *inode, const char *symname, int len)
2890{
2891 return __page_symlink(inode, symname, len,
2892 mapping_gfp_mask(inode->i_mapping));
2893}
2894
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002895const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 .readlink = generic_readlink,
2897 .follow_link = page_follow_link_light,
2898 .put_link = page_put_link,
2899};
2900
2901EXPORT_SYMBOL(__user_walk);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002902EXPORT_SYMBOL(__user_walk_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903EXPORT_SYMBOL(follow_down);
2904EXPORT_SYMBOL(follow_up);
2905EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2906EXPORT_SYMBOL(getname);
2907EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908EXPORT_SYMBOL(lookup_one_len);
2909EXPORT_SYMBOL(page_follow_link_light);
2910EXPORT_SYMBOL(page_put_link);
2911EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002912EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913EXPORT_SYMBOL(page_symlink);
2914EXPORT_SYMBOL(page_symlink_inode_operations);
2915EXPORT_SYMBOL(path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002916EXPORT_SYMBOL(vfs_path_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917EXPORT_SYMBOL(permission);
Christoph Hellwige4543ed2005-11-08 21:35:04 -08002918EXPORT_SYMBOL(vfs_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08002919EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920EXPORT_SYMBOL(unlock_rename);
2921EXPORT_SYMBOL(vfs_create);
2922EXPORT_SYMBOL(vfs_follow_link);
2923EXPORT_SYMBOL(vfs_link);
2924EXPORT_SYMBOL(vfs_mkdir);
2925EXPORT_SYMBOL(vfs_mknod);
2926EXPORT_SYMBOL(generic_permission);
2927EXPORT_SYMBOL(vfs_readlink);
2928EXPORT_SYMBOL(vfs_rename);
2929EXPORT_SYMBOL(vfs_rmdir);
2930EXPORT_SYMBOL(vfs_symlink);
2931EXPORT_SYMBOL(vfs_unlink);
2932EXPORT_SYMBOL(dentry_unhash);
2933EXPORT_SYMBOL(generic_readlink);