blob: 734f2b5591bf9758aedc875381a9456fe7853929 [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>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
36
37#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
38
39/* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
44 *
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
51 *
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
55 *
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
58 *
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
65 */
66
67/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existant name.
74 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * any extra contention...
108 */
109
Al Viroa02f76c2008-02-23 15:14:28 +0000110static int __link_path_walk(const char *name, struct nameidata *nd);
Josef 'Jeff' Sipekc4a78082007-07-19 01:48:22 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* In order to reduce some races, while at the same time doing additional
113 * checking and hopefully speeding things up, we copy filenames to the
114 * kernel data space before using them..
115 *
116 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
117 * PATH_MAX includes the nul terminator --RR.
118 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800119static int do_getname(const char __user *filename, char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 int retval;
122 unsigned long len = PATH_MAX;
123
124 if (!segment_eq(get_fs(), KERNEL_DS)) {
125 if ((unsigned long) filename >= TASK_SIZE)
126 return -EFAULT;
127 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
128 len = TASK_SIZE - (unsigned long) filename;
129 }
130
131 retval = strncpy_from_user(page, filename, len);
132 if (retval > 0) {
133 if (retval < len)
134 return 0;
135 return -ENAMETOOLONG;
136 } else if (!retval)
137 retval = -ENOENT;
138 return retval;
139}
140
141char * getname(const char __user * filename)
142{
143 char *tmp, *result;
144
145 result = ERR_PTR(-ENOMEM);
146 tmp = __getname();
147 if (tmp) {
148 int retval = do_getname(filename, tmp);
149
150 result = tmp;
151 if (retval < 0) {
152 __putname(tmp);
153 result = ERR_PTR(retval);
154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
160#ifdef CONFIG_AUDITSYSCALL
161void putname(const char *name)
162{
Al Viro5ac3a9c2006-07-16 06:38:45 -0400163 if (unlikely(!audit_dummy_context()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 audit_putname(name);
165 else
166 __putname(name);
167}
168EXPORT_SYMBOL(putname);
169#endif
170
171
172/**
173 * generic_permission - check for access rights on a Posix-like filesystem
174 * @inode: inode to check access rights for
175 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
176 * @check_acl: optional callback to check for Posix ACLs
177 *
178 * Used to check for read/write/execute permissions on a file.
179 * We use "fsuid" for this, letting us set arbitrary permissions
180 * for filesystem access without changing the "normal" uids which
181 * are used for other things..
182 */
183int generic_permission(struct inode *inode, int mask,
184 int (*check_acl)(struct inode *inode, int mask))
185{
186 umode_t mode = inode->i_mode;
187
Al Viroe6305c42008-07-15 21:03:57 -0400188 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
189
David Howellsda9592e2008-11-14 10:39:05 +1100190 if (current_fsuid() == inode->i_uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 mode >>= 6;
192 else {
193 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
194 int error = check_acl(inode, mask);
195 if (error == -EACCES)
196 goto check_capabilities;
197 else if (error != -EAGAIN)
198 return error;
199 }
200
201 if (in_group_p(inode->i_gid))
202 mode >>= 3;
203 }
204
205 /*
206 * If the DACs are ok we don't need any capability check.
207 */
Al Viroe6305c42008-07-15 21:03:57 -0400208 if ((mask & ~mode) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210
211 check_capabilities:
212 /*
213 * Read/write DACs are always overridable.
214 * Executable DACs are overridable if at least one exec bit is set.
215 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200216 if (!(mask & MAY_EXEC) || execute_ok(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 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
Al Virof419a2e2008-07-22 00:07:17 -0400230int inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Al Viroe6305c42008-07-15 21:03:57 -0400232 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700235 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /*
238 * Nobody gets write access to a read-only fs.
239 */
240 if (IS_RDONLY(inode) &&
241 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
242 return -EROFS;
243
244 /*
245 * Nobody gets write access to an immutable file.
246 */
247 if (IS_IMMUTABLE(inode))
248 return -EACCES;
249 }
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Ordinary permission routines do not understand MAY_APPEND. */
Miklos Szeredif696a362008-07-31 13:41:58 +0200252 if (inode->i_op && inode->i_op->permission)
Al Virob77b0642008-07-17 09:37:02 -0400253 retval = inode->i_op->permission(inode, mask);
Miklos Szeredif696a362008-07-31 13:41:58 +0200254 else
Al Viroe6305c42008-07-15 21:03:57 -0400255 retval = generic_permission(inode, mask, NULL);
Miklos Szeredif696a362008-07-31 13:41:58 +0200256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (retval)
258 return retval;
259
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700260 retval = devcgroup_inode_permission(inode, mask);
261 if (retval)
262 return retval;
263
Al Viroe6305c42008-07-15 21:03:57 -0400264 return security_inode_permission(inode,
Stephen Smalleyf418b002008-07-28 13:32:38 -0400265 mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800268/**
269 * vfs_permission - check for access rights to a given path
270 * @nd: lookup result that describes the path
271 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
272 *
273 * Used to check for read/write/execute permissions on a path.
274 * We use "fsuid" for this, letting us set arbitrary permissions
275 * for filesystem access without changing the "normal" uids which
276 * are used for other things.
277 */
278int vfs_permission(struct nameidata *nd, int mask)
279{
Al Virof419a2e2008-07-22 00:07:17 -0400280 return inode_permission(nd->path.dentry->d_inode, mask);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800281}
282
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800283/**
284 * file_permission - check for additional access rights to a given file
285 * @file: file to check access rights for
286 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
287 *
288 * Used to check for read/write/execute permissions on an already opened
289 * file.
290 *
291 * Note:
292 * Do not use this function in new code. All access checks should
293 * be done using vfs_permission().
294 */
295int file_permission(struct file *file, int mask)
296{
Al Virof419a2e2008-07-22 00:07:17 -0400297 return inode_permission(file->f_path.dentry->d_inode, mask);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
301 * get_write_access() gets write permission for a file.
302 * put_write_access() releases this write permission.
303 * This is used for regular files.
304 * We cannot support write (and maybe mmap read-write shared) accesses and
305 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
306 * can have the following values:
307 * 0: no writers, no VM_DENYWRITE mappings
308 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
309 * > 0: (i_writecount) users are writing to the file.
310 *
311 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
312 * except for the cases where we don't hold i_writecount yet. Then we need to
313 * use {get,deny}_write_access() - these functions check the sign and refuse
314 * to do the change if sign is wrong. Exclusion between them is provided by
315 * the inode->i_lock spinlock.
316 */
317
318int get_write_access(struct inode * inode)
319{
320 spin_lock(&inode->i_lock);
321 if (atomic_read(&inode->i_writecount) < 0) {
322 spin_unlock(&inode->i_lock);
323 return -ETXTBSY;
324 }
325 atomic_inc(&inode->i_writecount);
326 spin_unlock(&inode->i_lock);
327
328 return 0;
329}
330
331int deny_write_access(struct file * file)
332{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800333 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 spin_lock(&inode->i_lock);
336 if (atomic_read(&inode->i_writecount) > 0) {
337 spin_unlock(&inode->i_lock);
338 return -ETXTBSY;
339 }
340 atomic_dec(&inode->i_writecount);
341 spin_unlock(&inode->i_lock);
342
343 return 0;
344}
345
Jan Blunck1d957f92008-02-14 19:34:35 -0800346/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800347 * path_get - get a reference to a path
348 * @path: path to get the reference to
349 *
350 * Given a path increment the reference count to the dentry and the vfsmount.
351 */
352void path_get(struct path *path)
353{
354 mntget(path->mnt);
355 dget(path->dentry);
356}
357EXPORT_SYMBOL(path_get);
358
359/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800360 * path_put - put a reference to a path
361 * @path: path to put the reference to
362 *
363 * Given a path decrement the reference count to the dentry and the vfsmount.
364 */
365void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Jan Blunck1d957f92008-02-14 19:34:35 -0800367 dput(path->dentry);
368 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
Jan Blunck1d957f92008-02-14 19:34:35 -0800370EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Trond Myklebust834f2a42005-10-18 14:20:16 -0700372/**
373 * release_open_intent - free up open intent resources
374 * @nd: pointer to nameidata
375 */
376void release_open_intent(struct nameidata *nd)
377{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800378 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700379 put_filp(nd->intent.open.file);
380 else
381 fput(nd->intent.open.file);
382}
383
Ian Kentbcdc5e02006-09-27 01:50:44 -0700384static inline struct dentry *
385do_revalidate(struct dentry *dentry, struct nameidata *nd)
386{
387 int status = dentry->d_op->d_revalidate(dentry, nd);
388 if (unlikely(status <= 0)) {
389 /*
390 * The dentry failed validation.
391 * If d_revalidate returned 0 attempt to invalidate
392 * the dentry otherwise d_revalidate is asking us
393 * to return a fail status.
394 */
395 if (!status) {
396 if (!d_invalidate(dentry)) {
397 dput(dentry);
398 dentry = NULL;
399 }
400 } else {
401 dput(dentry);
402 dentry = ERR_PTR(status);
403 }
404 }
405 return dentry;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
409 * Internal lookup() using the new generic dcache.
410 * SMP-safe
411 */
412static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
413{
414 struct dentry * dentry = __d_lookup(parent, name);
415
416 /* lockess __d_lookup may fail due to concurrent d_move()
417 * in some unrelated directory, so try with d_lookup
418 */
419 if (!dentry)
420 dentry = d_lookup(parent, name);
421
Ian Kentbcdc5e02006-09-27 01:50:44 -0700422 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
423 dentry = do_revalidate(dentry, nd);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return dentry;
426}
427
428/*
429 * Short-cut version of permission(), for calling by
430 * path_walk(), when dcache lock is held. Combines parts
431 * of permission() and generic_permission(), and tests ONLY for
432 * MAY_EXEC permission.
433 *
434 * If appropriate, check DAC only. If not appropriate, or
435 * short-cut DAC fails, then call permission() to do more
436 * complete permission check.
437 */
Al Viro672b16b2008-07-17 09:45:01 -0400438static int exec_permission_lite(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 umode_t mode = inode->i_mode;
441
442 if (inode->i_op && inode->i_op->permission)
443 return -EAGAIN;
444
David Howellsda9592e2008-11-14 10:39:05 +1100445 if (current_fsuid() == inode->i_uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 mode >>= 6;
447 else if (in_group_p(inode->i_gid))
448 mode >>= 3;
449
450 if (mode & MAY_EXEC)
451 goto ok;
452
453 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
454 goto ok;
455
456 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
457 goto ok;
458
459 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
460 goto ok;
461
462 return -EACCES;
463ok:
Al Virob77b0642008-07-17 09:37:02 -0400464 return security_inode_permission(inode, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467/*
468 * This is called when everything else fails, and we actually have
469 * to go to the low-level filesystem to find out what we should do..
470 *
471 * We get the directory semaphore, and after getting that we also
472 * make sure that nobody added the entry to the dcache in the meantime..
473 * SMP-safe
474 */
475static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
476{
477 struct dentry * result;
478 struct inode *dir = parent->d_inode;
479
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800480 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /*
482 * First re-do the cached lookup just in case it was created
483 * while we waited for the directory semaphore..
484 *
485 * FIXME! This could use version numbering or similar to
486 * avoid unnecessary cache lookups.
487 *
488 * The "dcache_lock" is purely to protect the RCU list walker
489 * from concurrent renames at this point (we mustn't get false
490 * negatives from the RCU list walk here, unlike the optimistic
491 * fast walk).
492 *
493 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
494 */
495 result = d_lookup(parent, name);
496 if (!result) {
Miklos Szeredid70b67c2008-07-02 21:30:15 +0200497 struct dentry *dentry;
498
499 /* Don't create child dentry for a dead directory. */
500 result = ERR_PTR(-ENOENT);
501 if (IS_DEADDIR(dir))
502 goto out_unlock;
503
504 dentry = d_alloc(parent, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 result = ERR_PTR(-ENOMEM);
506 if (dentry) {
507 result = dir->i_op->lookup(dir, dentry, nd);
508 if (result)
509 dput(dentry);
510 else
511 result = dentry;
512 }
Miklos Szeredid70b67c2008-07-02 21:30:15 +0200513out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800514 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return result;
516 }
517
518 /*
519 * Uhhuh! Nasty case: the cache was re-populated while
520 * we waited on the semaphore. Need to revalidate.
521 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800522 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (result->d_op && result->d_op->d_revalidate) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700524 result = do_revalidate(result, nd);
525 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528 return result;
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/* SMP-safe */
Al Viro7f2da1e2008-05-10 20:44:54 -0400532static __always_inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533walk_init_root(const char *name, struct nameidata *nd)
534{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700535 struct fs_struct *fs = current->fs;
536
537 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800538 nd->path = fs->root;
539 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700540 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Al Viroa02f76c2008-02-23 15:14:28 +0000543/*
544 * Wrapper to retry pathname resolution whenever the underlying
545 * file system returns an ESTALE.
546 *
547 * Retry the whole path once, forcing real lookup requests
548 * instead of relying on the dcache.
549 */
550static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
551{
552 struct path save = nd->path;
553 int result;
554
555 /* make sure the stuff we saved doesn't go away */
Jan Blunckc8e7f442008-06-09 16:40:35 -0700556 path_get(&save);
Al Viroa02f76c2008-02-23 15:14:28 +0000557
558 result = __link_path_walk(name, nd);
559 if (result == -ESTALE) {
560 /* nd->path had been dropped */
561 nd->path = save;
Jan Blunckc8e7f442008-06-09 16:40:35 -0700562 path_get(&nd->path);
Al Viroa02f76c2008-02-23 15:14:28 +0000563 nd->flags |= LOOKUP_REVAL;
564 result = __link_path_walk(name, nd);
565 }
566
567 path_put(&save);
568
569 return result;
570}
571
Arjan van de Venf1662352006-01-14 13:21:31 -0800572static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int res = 0;
575 char *name;
576 if (IS_ERR(link))
577 goto fail;
578
579 if (*link == '/') {
Jan Blunck1d957f92008-02-14 19:34:35 -0800580 path_put(&nd->path);
Al Viro7f2da1e2008-05-10 20:44:54 -0400581 walk_init_root(link, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583 res = link_path_walk(link, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (nd->depth || res || nd->last_type!=LAST_NORM)
585 return res;
586 /*
587 * If it is an iterative symlinks resolution in open_namei() we
588 * have to copy the last component. And all that crap because of
589 * bloody create() on broken symlinks. Furrfu...
590 */
591 name = __getname();
592 if (unlikely(!name)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800593 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -ENOMEM;
595 }
596 strcpy(name, nd->last.name);
597 nd->last.name = name;
598 return 0;
599fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800600 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return PTR_ERR(link);
602}
603
Jan Blunck1d957f92008-02-14 19:34:35 -0800604static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700605{
606 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800607 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700608 mntput(path->mnt);
609}
610
611static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
612{
Jan Blunck4ac91372008-02-14 19:34:32 -0800613 dput(nd->path.dentry);
614 if (nd->path.mnt != path->mnt)
615 mntput(nd->path.mnt);
616 nd->path.mnt = path->mnt;
617 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700618}
619
Ian Kent051d3812006-03-27 01:14:53 -0800620static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
621{
622 int error;
623 void *cookie;
624 struct dentry *dentry = path->dentry;
625
626 touch_atime(path->mnt, dentry);
627 nd_set_link(nd, NULL);
628
Jan Blunck4ac91372008-02-14 19:34:32 -0800629 if (path->mnt != nd->path.mnt) {
Ian Kent051d3812006-03-27 01:14:53 -0800630 path_to_nameidata(path, nd);
631 dget(dentry);
632 }
633 mntget(path->mnt);
634 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
635 error = PTR_ERR(cookie);
636 if (!IS_ERR(cookie)) {
637 char *s = nd_get_link(nd);
638 error = 0;
639 if (s)
640 error = __vfs_follow_link(nd, s);
641 if (dentry->d_inode->i_op->put_link)
642 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
643 }
Jan Blunck09da59162008-02-14 19:34:37 -0800644 path_put(path);
Ian Kent051d3812006-03-27 01:14:53 -0800645
646 return error;
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
650 * This limits recursive symlink follows to 8, while
651 * limiting consecutive symlinks to 40.
652 *
653 * Without that kind of total limit, nasty chains of consecutive
654 * symlinks can cause almost arbitrarily long lookups.
655 */
Al Viro90ebe562005-06-06 13:35:58 -0700656static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 int err = -ELOOP;
659 if (current->link_count >= MAX_NESTED_LINKS)
660 goto loop;
661 if (current->total_link_count >= 40)
662 goto loop;
663 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
664 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700665 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (err)
667 goto loop;
668 current->link_count++;
669 current->total_link_count++;
670 nd->depth++;
Al Virocd4e91d2005-06-06 13:36:03 -0700671 err = __do_follow_link(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 current->link_count--;
673 nd->depth--;
674 return err;
675loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800676 path_put_conditional(path, nd);
677 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return err;
679}
680
681int follow_up(struct vfsmount **mnt, struct dentry **dentry)
682{
683 struct vfsmount *parent;
684 struct dentry *mountpoint;
685 spin_lock(&vfsmount_lock);
686 parent=(*mnt)->mnt_parent;
687 if (parent == *mnt) {
688 spin_unlock(&vfsmount_lock);
689 return 0;
690 }
691 mntget(parent);
692 mountpoint=dget((*mnt)->mnt_mountpoint);
693 spin_unlock(&vfsmount_lock);
694 dput(*dentry);
695 *dentry = mountpoint;
696 mntput(*mnt);
697 *mnt = parent;
698 return 1;
699}
700
701/* no need for dcache_lock, as serialization is taken care in
702 * namespace.c
703 */
Al Viro463ffb22005-06-06 13:36:05 -0700704static int __follow_mount(struct path *path)
705{
706 int res = 0;
707 while (d_mountpoint(path->dentry)) {
708 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
709 if (!mounted)
710 break;
711 dput(path->dentry);
712 if (res)
713 mntput(path->mnt);
714 path->mnt = mounted;
715 path->dentry = dget(mounted->mnt_root);
716 res = 1;
717 }
718 return res;
719}
720
Al Viro58c465e2005-06-06 13:36:13 -0700721static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 while (d_mountpoint(*dentry)) {
724 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
725 if (!mounted)
726 break;
Al Viro58c465e2005-06-06 13:36:13 -0700727 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 mntput(*mnt);
729 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 *dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/* no need for dcache_lock, as serialization is taken care in
735 * namespace.c
736 */
Al Viroe13b2102005-06-06 13:36:06 -0700737int follow_down(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 struct vfsmount *mounted;
740
741 mounted = lookup_mnt(*mnt, *dentry);
742 if (mounted) {
Al Viroe13b2102005-06-06 13:36:06 -0700743 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 mntput(*mnt);
745 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *dentry = dget(mounted->mnt_root);
747 return 1;
748 }
749 return 0;
750}
751
Arjan van de Venf1662352006-01-14 13:21:31 -0800752static __always_inline void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700754 struct fs_struct *fs = current->fs;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 while(1) {
757 struct vfsmount *parent;
Jan Blunck4ac91372008-02-14 19:34:32 -0800758 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Andreas Mohre518ddb2006-09-29 02:01:22 -0700760 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800761 if (nd->path.dentry == fs->root.dentry &&
762 nd->path.mnt == fs->root.mnt) {
Andreas Mohre518ddb2006-09-29 02:01:22 -0700763 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700766 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 spin_lock(&dcache_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800768 if (nd->path.dentry != nd->path.mnt->mnt_root) {
769 nd->path.dentry = dget(nd->path.dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 spin_unlock(&dcache_lock);
771 dput(old);
772 break;
773 }
774 spin_unlock(&dcache_lock);
775 spin_lock(&vfsmount_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800776 parent = nd->path.mnt->mnt_parent;
777 if (parent == nd->path.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 spin_unlock(&vfsmount_lock);
779 break;
780 }
781 mntget(parent);
Jan Blunck4ac91372008-02-14 19:34:32 -0800782 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 spin_unlock(&vfsmount_lock);
784 dput(old);
Jan Blunck4ac91372008-02-14 19:34:32 -0800785 mntput(nd->path.mnt);
786 nd->path.mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800788 follow_mount(&nd->path.mnt, &nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
792 * It's more convoluted than I'd like it to be, but... it's still fairly
793 * small and for now I'd prefer to have fast path as straight as possible.
794 * It _is_ time-critical.
795 */
796static int do_lookup(struct nameidata *nd, struct qstr *name,
797 struct path *path)
798{
Jan Blunck4ac91372008-02-14 19:34:32 -0800799 struct vfsmount *mnt = nd->path.mnt;
800 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (!dentry)
803 goto need_lookup;
804 if (dentry->d_op && dentry->d_op->d_revalidate)
805 goto need_revalidate;
806done:
807 path->mnt = mnt;
808 path->dentry = dentry;
Al Viro634ee702005-06-06 13:36:13 -0700809 __follow_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return 0;
811
812need_lookup:
Jan Blunck4ac91372008-02-14 19:34:32 -0800813 dentry = real_lookup(nd->path.dentry, name, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (IS_ERR(dentry))
815 goto fail;
816 goto done;
817
818need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -0700819 dentry = do_revalidate(dentry, nd);
820 if (!dentry)
821 goto need_lookup;
822 if (IS_ERR(dentry))
823 goto fail;
824 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826fail:
827 return PTR_ERR(dentry);
828}
829
830/*
831 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +0100832 * This is the basic name resolution function, turning a pathname into
833 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 *
Prasanna Medaea3834d2005-04-29 16:00:17 +0100835 * Returns 0 and nd will have valid dentry and mnt on success.
836 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800838static int __link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct path next;
841 struct inode *inode;
842 int err;
843 unsigned int lookup_flags = nd->flags;
844
845 while (*name=='/')
846 name++;
847 if (!*name)
848 goto return_reval;
849
Jan Blunck4ac91372008-02-14 19:34:32 -0800850 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -0800852 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* At this point we know we have a real path component. */
855 for(;;) {
856 unsigned long hash;
857 struct qstr this;
858 unsigned int c;
859
Trond Myklebustcdce5d62005-10-18 14:20:18 -0700860 nd->flags |= LOOKUP_CONTINUE;
Al Viro672b16b2008-07-17 09:45:01 -0400861 err = exec_permission_lite(inode);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800862 if (err == -EAGAIN)
863 err = vfs_permission(nd, MAY_EXEC);
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500864 if (!err)
865 err = ima_path_check(&nd->path, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (err)
867 break;
868
869 this.name = name;
870 c = *(const unsigned char *)name;
871
872 hash = init_name_hash();
873 do {
874 name++;
875 hash = partial_name_hash(c, hash);
876 c = *(const unsigned char *)name;
877 } while (c && (c != '/'));
878 this.len = name - (const char *) this.name;
879 this.hash = end_name_hash(hash);
880
881 /* remove trailing slashes? */
882 if (!c)
883 goto last_component;
884 while (*++name == '/');
885 if (!*name)
886 goto last_with_slashes;
887
888 /*
889 * "." and ".." are special - ".." especially so because it has
890 * to be able to know about the current root directory and
891 * parent relationships.
892 */
893 if (this.name[0] == '.') switch (this.len) {
894 default:
895 break;
896 case 2:
897 if (this.name[1] != '.')
898 break;
Al Viro58c465e2005-06-06 13:36:13 -0700899 follow_dotdot(nd);
Jan Blunck4ac91372008-02-14 19:34:32 -0800900 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /* fallthrough */
902 case 1:
903 continue;
904 }
905 /*
906 * See if the low-level filesystem might want
907 * to use its own hash..
908 */
Jan Blunck4ac91372008-02-14 19:34:32 -0800909 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
910 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
911 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (err < 0)
913 break;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* This does the actual lookups.. */
916 err = do_lookup(nd, &this, &next);
917 if (err)
918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 err = -ENOENT;
921 inode = next.dentry->d_inode;
922 if (!inode)
923 goto out_dput;
924 err = -ENOTDIR;
925 if (!inode->i_op)
926 goto out_dput;
927
928 if (inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700929 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (err)
931 goto return_err;
932 err = -ENOENT;
Jan Blunck4ac91372008-02-14 19:34:32 -0800933 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!inode)
935 break;
936 err = -ENOTDIR;
937 if (!inode->i_op)
938 break;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700939 } else
940 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 err = -ENOTDIR;
942 if (!inode->i_op->lookup)
943 break;
944 continue;
945 /* here ends the main loop */
946
947last_with_slashes:
948 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
949last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -0800950 /* Clear LOOKUP_CONTINUE iff it was previously unset */
951 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (lookup_flags & LOOKUP_PARENT)
953 goto lookup_parent;
954 if (this.name[0] == '.') switch (this.len) {
955 default:
956 break;
957 case 2:
958 if (this.name[1] != '.')
959 break;
Al Viro58c465e2005-06-06 13:36:13 -0700960 follow_dotdot(nd);
Jan Blunck4ac91372008-02-14 19:34:32 -0800961 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* fallthrough */
963 case 1:
964 goto return_reval;
965 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800966 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
967 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
968 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (err < 0)
970 break;
971 }
972 err = do_lookup(nd, &this, &next);
973 if (err)
974 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 inode = next.dentry->d_inode;
976 if ((lookup_flags & LOOKUP_FOLLOW)
977 && inode && inode->i_op && inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700978 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (err)
980 goto return_err;
Jan Blunck4ac91372008-02-14 19:34:32 -0800981 inode = nd->path.dentry->d_inode;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700982 } else
983 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 err = -ENOENT;
985 if (!inode)
986 break;
987 if (lookup_flags & LOOKUP_DIRECTORY) {
988 err = -ENOTDIR;
989 if (!inode->i_op || !inode->i_op->lookup)
990 break;
991 }
992 goto return_base;
993lookup_parent:
994 nd->last = this;
995 nd->last_type = LAST_NORM;
996 if (this.name[0] != '.')
997 goto return_base;
998 if (this.len == 1)
999 nd->last_type = LAST_DOT;
1000 else if (this.len == 2 && this.name[1] == '.')
1001 nd->last_type = LAST_DOTDOT;
1002 else
1003 goto return_base;
1004return_reval:
1005 /*
1006 * We bypassed the ordinary revalidation routines.
1007 * We may need to check the cached dentry for staleness.
1008 */
Jan Blunck4ac91372008-02-14 19:34:32 -08001009 if (nd->path.dentry && nd->path.dentry->d_sb &&
1010 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 err = -ESTALE;
1012 /* Note: we do not d_invalidate() */
Jan Blunck4ac91372008-02-14 19:34:32 -08001013 if (!nd->path.dentry->d_op->d_revalidate(
1014 nd->path.dentry, nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 break;
1016 }
1017return_base:
1018 return 0;
1019out_dput:
Jan Blunck1d957f92008-02-14 19:34:35 -08001020 path_put_conditional(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
1022 }
Jan Blunck1d957f92008-02-14 19:34:35 -08001023 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024return_err:
1025 return err;
1026}
1027
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001028static int path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 current->total_link_count = 0;
1031 return link_path_walk(name, nd);
1032}
1033
Prasanna Medaea3834d2005-04-29 16:00:17 +01001034/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001035static int do_path_lookup(int dfd, const char *name,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001036 unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001038 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001039 int fput_needed;
1040 struct file *file;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001041 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1044 nd->flags = flags;
1045 nd->depth = 0;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (*name=='/') {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001048 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001049 nd->path = fs->root;
1050 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001051 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001052 } else if (dfd == AT_FDCWD) {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001053 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001054 nd->path = fs->pwd;
1055 path_get(&fs->pwd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001056 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001057 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001058 struct dentry *dentry;
1059
1060 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001061 retval = -EBADF;
1062 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001063 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001064
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001065 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001066
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001067 retval = -ENOTDIR;
1068 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001069 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001070
1071 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001072 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001073 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001074
Jan Blunck5dd784d02008-02-14 19:34:38 -08001075 nd->path = file->f_path;
1076 path_get(&file->f_path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001077
1078 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Josef 'Jeff' Sipek2dfdd262007-05-09 02:33:41 -07001080
1081 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001082 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1083 nd->path.dentry->d_inode))
1084 audit_inode(name, nd->path.dentry);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001085out_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001086 return retval;
1087
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001088fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001089 fput_light(file, fput_needed);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001090 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001093int path_lookup(const char *name, unsigned int flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001094 struct nameidata *nd)
1095{
1096 return do_path_lookup(AT_FDCWD, name, flags, nd);
1097}
1098
Al Virod1811462008-08-02 00:49:18 -04001099int kern_path(const char *name, unsigned int flags, struct path *path)
1100{
1101 struct nameidata nd;
1102 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1103 if (!res)
1104 *path = nd.path;
1105 return res;
1106}
1107
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001108/**
1109 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1110 * @dentry: pointer to dentry of the base directory
1111 * @mnt: pointer to vfs mount of the base directory
1112 * @name: pointer to file name
1113 * @flags: lookup flags
1114 * @nd: pointer to nameidata
1115 */
1116int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1117 const char *name, unsigned int flags,
1118 struct nameidata *nd)
1119{
1120 int retval;
1121
1122 /* same as do_path_lookup */
1123 nd->last_type = LAST_ROOT;
1124 nd->flags = flags;
1125 nd->depth = 0;
1126
Jan Blunckc8e7f442008-06-09 16:40:35 -07001127 nd->path.dentry = dentry;
1128 nd->path.mnt = mnt;
1129 path_get(&nd->path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001130
1131 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001132 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1133 nd->path.dentry->d_inode))
1134 audit_inode(name, nd->path.dentry);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001135
1136 return retval;
1137
1138}
1139
Trond Myklebust834f2a42005-10-18 14:20:16 -07001140/**
1141 * path_lookup_open - lookup a file path with open intent
Martin Waitz7045f372006-02-01 03:06:57 -08001142 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001143 * @name: pointer to file name
1144 * @lookup_flags: lookup intent flags
1145 * @nd: pointer to nameidata
1146 * @open_flags: open intent flags
1147 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001148int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001149 struct nameidata *nd, int open_flags)
1150{
Al Viro8737f3a2008-08-02 22:36:57 -04001151 struct file *filp = get_empty_filp();
1152 int err;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001153
Al Viro8737f3a2008-08-02 22:36:57 -04001154 if (filp == NULL)
1155 return -ENFILE;
1156 nd->intent.open.file = filp;
1157 nd->intent.open.flags = open_flags;
1158 nd->intent.open.create_mode = 0;
1159 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
1160 if (IS_ERR(nd->intent.open.file)) {
1161 if (err == 0) {
1162 err = PTR_ERR(nd->intent.open.file);
1163 path_put(&nd->path);
1164 }
1165 } else if (err != 0)
1166 release_open_intent(nd);
1167 return err;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001168}
1169
Christoph Hellwigeead1912007-10-16 23:25:38 -07001170static struct dentry *__lookup_hash(struct qstr *name,
1171 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
James Morris057f6c02007-04-26 00:12:05 -07001173 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 struct inode *inode;
1175 int err;
1176
1177 inode = base->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /*
1180 * See if the low-level filesystem might want
1181 * to use its own hash..
1182 */
1183 if (base->d_op && base->d_op->d_hash) {
1184 err = base->d_op->d_hash(base, name);
1185 dentry = ERR_PTR(err);
1186 if (err < 0)
1187 goto out;
1188 }
1189
1190 dentry = cached_lookup(base, name, nd);
1191 if (!dentry) {
Miklos Szeredid70b67c2008-07-02 21:30:15 +02001192 struct dentry *new;
1193
1194 /* Don't create child dentry for a dead directory. */
1195 dentry = ERR_PTR(-ENOENT);
1196 if (IS_DEADDIR(inode))
1197 goto out;
1198
1199 new = d_alloc(base, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 dentry = ERR_PTR(-ENOMEM);
1201 if (!new)
1202 goto out;
1203 dentry = inode->i_op->lookup(inode, new, nd);
1204 if (!dentry)
1205 dentry = new;
1206 else
1207 dput(new);
1208 }
1209out:
1210 return dentry;
1211}
1212
James Morris057f6c02007-04-26 00:12:05 -07001213/*
1214 * Restricted form of lookup. Doesn't follow links, single-component only,
1215 * needs parent already locked. Doesn't follow mounts.
1216 * SMP-safe.
1217 */
Adrian Bunka244e162006-03-31 02:32:11 -08001218static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Christoph Hellwigeead1912007-10-16 23:25:38 -07001220 int err;
1221
Al Virof419a2e2008-07-22 00:07:17 -04001222 err = inode_permission(nd->path.dentry->d_inode, MAY_EXEC);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001223 if (err)
1224 return ERR_PTR(err);
Jan Blunck4ac91372008-02-14 19:34:32 -08001225 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Christoph Hellwigeead1912007-10-16 23:25:38 -07001228static int __lookup_one_len(const char *name, struct qstr *this,
1229 struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 unsigned int c;
1233
James Morris057f6c02007-04-26 00:12:05 -07001234 this->name = name;
1235 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001237 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 hash = init_name_hash();
1240 while (len--) {
1241 c = *(const unsigned char *)name++;
1242 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001243 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 hash = partial_name_hash(c, hash);
1245 }
James Morris057f6c02007-04-26 00:12:05 -07001246 this->hash = end_name_hash(hash);
1247 return 0;
1248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Christoph Hellwigeead1912007-10-16 23:25:38 -07001250/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001251 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001252 * @name: pathname component to lookup
1253 * @base: base directory to lookup from
1254 * @len: maximum length @len should be interpreted to
1255 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001256 * Note that this routine is purely a helper for filesystem usage and should
1257 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001258 * nameidata argument is passed to the filesystem methods and a filesystem
1259 * using this helper needs to be prepared for that.
1260 */
James Morris057f6c02007-04-26 00:12:05 -07001261struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1262{
1263 int err;
1264 struct qstr this;
1265
1266 err = __lookup_one_len(name, &this, base, len);
1267 if (err)
1268 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001269
Al Virof419a2e2008-07-22 00:07:17 -04001270 err = inode_permission(base->d_inode, MAY_EXEC);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001271 if (err)
1272 return ERR_PTR(err);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001273 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001274}
1275
Christoph Hellwigeead1912007-10-16 23:25:38 -07001276/**
1277 * lookup_one_noperm - bad hack for sysfs
1278 * @name: pathname component to lookup
1279 * @base: base directory to lookup from
1280 *
1281 * This is a variant of lookup_one_len that doesn't perform any permission
1282 * checks. It's a horrible hack to work around the braindead sysfs
1283 * architecture and should not be used anywhere else.
1284 *
1285 * DON'T USE THIS FUNCTION EVER, thanks.
1286 */
1287struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
James Morris057f6c02007-04-26 00:12:05 -07001288{
1289 int err;
1290 struct qstr this;
1291
Christoph Hellwigeead1912007-10-16 23:25:38 -07001292 err = __lookup_one_len(name, &this, base, strlen(name));
James Morris057f6c02007-04-26 00:12:05 -07001293 if (err)
1294 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001295 return __lookup_hash(&this, base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Al Viro2d8f3032008-07-22 09:59:21 -04001298int user_path_at(int dfd, const char __user *name, unsigned flags,
1299 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Al Viro2d8f3032008-07-22 09:59:21 -04001301 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 char *tmp = getname(name);
1303 int err = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (!IS_ERR(tmp)) {
Al Viro2d8f3032008-07-22 09:59:21 -04001305
1306 BUG_ON(flags & LOOKUP_PARENT);
1307
1308 err = do_path_lookup(dfd, tmp, flags, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 putname(tmp);
Al Viro2d8f3032008-07-22 09:59:21 -04001310 if (!err)
1311 *path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313 return err;
1314}
1315
Al Viro2ad94ae2008-07-21 09:32:51 -04001316static int user_path_parent(int dfd, const char __user *path,
1317 struct nameidata *nd, char **name)
1318{
1319 char *s = getname(path);
1320 int error;
1321
1322 if (IS_ERR(s))
1323 return PTR_ERR(s);
1324
1325 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1326 if (error)
1327 putname(s);
1328 else
1329 *name = s;
1330
1331 return error;
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/*
1335 * It's inline, so penalty for filesystems that don't use sticky bit is
1336 * minimal.
1337 */
1338static inline int check_sticky(struct inode *dir, struct inode *inode)
1339{
David Howellsda9592e2008-11-14 10:39:05 +11001340 uid_t fsuid = current_fsuid();
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (!(dir->i_mode & S_ISVTX))
1343 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001344 if (inode->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return 0;
David Howellsda9592e2008-11-14 10:39:05 +11001346 if (dir->i_uid == fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 0;
1348 return !capable(CAP_FOWNER);
1349}
1350
1351/*
1352 * Check whether we can remove a link victim from directory dir, check
1353 * whether the type of victim is right.
1354 * 1. We can't do it if dir is read-only (done in permission())
1355 * 2. We should have write and exec permissions on dir
1356 * 3. We can't remove anything from append-only dir
1357 * 4. We can't do anything with immutable dir (done in permission())
1358 * 5. If the sticky bit on dir is set we should either
1359 * a. be owner of dir, or
1360 * b. be owner of victim, or
1361 * c. have CAP_FOWNER capability
1362 * 6. If the victim is append-only or immutable we can't do antyhing with
1363 * links pointing to it.
1364 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1365 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1366 * 9. We can't remove a root or mountpoint.
1367 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1368 * nfs_async_unlink().
1369 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001370static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 int error;
1373
1374 if (!victim->d_inode)
1375 return -ENOENT;
1376
1377 BUG_ON(victim->d_parent->d_inode != dir);
Al Viro5a190ae2007-06-07 12:19:32 -04001378 audit_inode_child(victim->d_name.name, victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Al Virof419a2e2008-07-22 00:07:17 -04001380 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (error)
1382 return error;
1383 if (IS_APPEND(dir))
1384 return -EPERM;
1385 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
Hugh Dickinsf9454542008-11-19 15:36:38 -08001386 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EPERM;
1388 if (isdir) {
1389 if (!S_ISDIR(victim->d_inode->i_mode))
1390 return -ENOTDIR;
1391 if (IS_ROOT(victim))
1392 return -EBUSY;
1393 } else if (S_ISDIR(victim->d_inode->i_mode))
1394 return -EISDIR;
1395 if (IS_DEADDIR(dir))
1396 return -ENOENT;
1397 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1398 return -EBUSY;
1399 return 0;
1400}
1401
1402/* Check whether we can create an object with dentry child in directory
1403 * dir.
1404 * 1. We can't do it if child already exists (open has special treatment for
1405 * this case, but since we are inlined it's OK)
1406 * 2. We can't do it if dir is read-only (done in permission())
1407 * 3. We should have write and exec permissions on dir
1408 * 4. We can't do it if dir is immutable (done in permission())
1409 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02001410static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 if (child->d_inode)
1413 return -EEXIST;
1414 if (IS_DEADDIR(dir))
1415 return -ENOENT;
Al Virof419a2e2008-07-22 00:07:17 -04001416 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 * O_DIRECTORY translates into forcing a directory lookup.
1421 */
1422static inline int lookup_flags(unsigned int f)
1423{
1424 unsigned long retval = LOOKUP_FOLLOW;
1425
1426 if (f & O_NOFOLLOW)
1427 retval &= ~LOOKUP_FOLLOW;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (f & O_DIRECTORY)
1430 retval |= LOOKUP_DIRECTORY;
1431
1432 return retval;
1433}
1434
1435/*
1436 * p1 and p2 should be directories on the same fs.
1437 */
1438struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1439{
1440 struct dentry *p;
1441
1442 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001443 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return NULL;
1445 }
1446
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001447 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001449 p = d_ancestor(p2, p1);
1450 if (p) {
1451 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1452 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1453 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001456 p = d_ancestor(p1, p2);
1457 if (p) {
1458 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1459 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1460 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
Ingo Molnarf2eace22006-07-03 00:25:05 -07001463 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1464 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return NULL;
1466}
1467
1468void unlock_rename(struct dentry *p1, struct dentry *p2)
1469{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001470 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001472 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001473 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475}
1476
1477int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1478 struct nameidata *nd)
1479{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001480 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 if (error)
1483 return error;
1484
1485 if (!dir->i_op || !dir->i_op->create)
1486 return -EACCES; /* shouldn't it be ENOSYS? */
1487 mode &= S_IALLUGO;
1488 mode |= S_IFREG;
1489 error = security_inode_create(dir, dentry, mode);
1490 if (error)
1491 return error;
1492 DQUOT_INIT(dir);
1493 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001494 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001495 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return error;
1497}
1498
1499int may_open(struct nameidata *nd, int acc_mode, int flag)
1500{
Jan Blunck4ac91372008-02-14 19:34:32 -08001501 struct dentry *dentry = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 struct inode *inode = dentry->d_inode;
1503 int error;
1504
1505 if (!inode)
1506 return -ENOENT;
1507
1508 if (S_ISLNK(inode->i_mode))
1509 return -ELOOP;
1510
Linus Torvalds974a9f02008-01-12 14:06:34 -08001511 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EISDIR;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /*
1515 * FIFO's, sockets and device files are special: they don't
1516 * actually live on the filesystem itself, and as such you
1517 * can write to them even if the filesystem is read-only.
1518 */
1519 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1520 flag &= ~O_TRUNC;
1521 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001522 if (nd->path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -EACCES;
1524
1525 flag &= ~O_TRUNC;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001526 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001527
1528 error = vfs_permission(nd, acc_mode);
1529 if (error)
1530 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05001531
1532 error = ima_path_check(&nd->path,
1533 acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC));
1534 if (error)
1535 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /*
1537 * An append-only file must be opened in append mode for writing.
1538 */
1539 if (IS_APPEND(inode)) {
1540 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1541 return -EPERM;
1542 if (flag & O_TRUNC)
1543 return -EPERM;
1544 }
1545
1546 /* O_NOATIME can only be set by the owner or superuser */
1547 if (flag & O_NOATIME)
Satyam Sharma3bd858a2007-07-17 15:00:08 +05301548 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return -EPERM;
1550
1551 /*
1552 * Ensure there are no outstanding leases on the file.
1553 */
1554 error = break_lease(inode, flag);
1555 if (error)
1556 return error;
1557
1558 if (flag & O_TRUNC) {
1559 error = get_write_access(inode);
1560 if (error)
1561 return error;
1562
1563 /*
1564 * Refuse to truncate files with mandatory locks held on them.
1565 */
1566 error = locks_verify_locked(inode);
1567 if (!error) {
1568 DQUOT_INIT(inode);
Miklos Szeredid139d7f2007-10-18 03:07:00 -07001569
1570 error = do_truncate(dentry, 0,
1571 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1572 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 put_write_access(inode);
1575 if (error)
1576 return error;
1577 } else
1578 if (flag & FMODE_WRITE)
1579 DQUOT_INIT(inode);
1580
1581 return 0;
1582}
1583
Dave Hansend57999e2008-02-15 14:37:27 -08001584/*
1585 * Be careful about ever adding any more callers of this
1586 * function. Its flags must be in the namei format, not
1587 * what get passed to sys_open().
1588 */
1589static int __open_namei_create(struct nameidata *nd, struct path *path,
Dave Hansenaab520e2006-09-30 23:29:02 -07001590 int flag, int mode)
1591{
1592 int error;
Jan Blunck4ac91372008-02-14 19:34:32 -08001593 struct dentry *dir = nd->path.dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001594
1595 if (!IS_POSIXACL(dir->d_inode))
1596 mode &= ~current->fs->umask;
1597 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1598 mutex_unlock(&dir->d_inode->i_mutex);
Jan Blunck4ac91372008-02-14 19:34:32 -08001599 dput(nd->path.dentry);
1600 nd->path.dentry = path->dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001601 if (error)
1602 return error;
1603 /* Don't check for write permission, don't truncate */
1604 return may_open(nd, 0, flag & ~O_TRUNC);
1605}
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607/*
Dave Hansend57999e2008-02-15 14:37:27 -08001608 * Note that while the flag value (low two bits) for sys_open means:
1609 * 00 - read-only
1610 * 01 - write-only
1611 * 10 - read-write
1612 * 11 - special
1613 * it is changed into
1614 * 00 - no permissions needed
1615 * 01 - read-permission
1616 * 10 - write-permission
1617 * 11 - read-write
1618 * for the internal routines (ie open_namei()/follow_link() etc)
1619 * This is more logical, and also allows the 00 "no perm needed"
1620 * to be used for symlinks (where the permissions are checked
1621 * later).
1622 *
1623*/
1624static inline int open_to_namei_flags(int flag)
1625{
1626 if ((flag+1) & O_ACCMODE)
1627 flag++;
1628 return flag;
1629}
1630
Dave Hansen4a3fd212008-02-15 14:37:48 -08001631static int open_will_write_to_fs(int flag, struct inode *inode)
1632{
1633 /*
1634 * We'll never write to the fs underlying
1635 * a device file.
1636 */
1637 if (special_file(inode->i_mode))
1638 return 0;
1639 return (flag & O_TRUNC);
1640}
1641
Dave Hansend57999e2008-02-15 14:37:27 -08001642/*
Dave Hansen4a3fd212008-02-15 14:37:48 -08001643 * Note that the low bits of the passed in "open_flag"
1644 * are not the same as in the local variable "flag". See
1645 * open_to_namei_flags() for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001647struct file *do_filp_open(int dfd, const char *pathname,
1648 int open_flag, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Dave Hansen4a3fd212008-02-15 14:37:48 -08001650 struct file *filp;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001651 struct nameidata nd;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001652 int acc_mode, error;
Al Viro4e7506e2005-06-06 13:36:00 -07001653 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct dentry *dir;
1655 int count = 0;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001656 int will_write;
Dave Hansend57999e2008-02-15 14:37:27 -08001657 int flag = open_to_namei_flags(open_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Al Virob77b0642008-07-17 09:37:02 -04001659 acc_mode = MAY_OPEN | ACC_MODE(flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Trond Myklebust834f2a42005-10-18 14:20:16 -07001661 /* O_TRUNC implies we need access checks for write permissions */
1662 if (flag & O_TRUNC)
1663 acc_mode |= MAY_WRITE;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /* Allow the LSM permission hook to distinguish append
1666 access from general write access. */
1667 if (flag & O_APPEND)
1668 acc_mode |= MAY_APPEND;
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /*
1671 * The simplest case - just a plain lookup.
1672 */
1673 if (!(flag & O_CREAT)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001674 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001675 &nd, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001677 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 goto ok;
1679 }
1680
1681 /*
1682 * Create - we need to know the parent.
1683 */
Al Viro8737f3a2008-08-02 22:36:57 -04001684 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001686 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /*
1689 * We have the parent and last component. First of all, check
1690 * that we are not asked to creat(2) an obvious directory - that
1691 * will not do.
1692 */
1693 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001694 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
Al Viro8737f3a2008-08-02 22:36:57 -04001695 goto exit_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Al Viro8737f3a2008-08-02 22:36:57 -04001697 error = -ENFILE;
1698 filp = get_empty_filp();
1699 if (filp == NULL)
1700 goto exit_parent;
1701 nd.intent.open.file = filp;
1702 nd.intent.open.flags = flag;
1703 nd.intent.open.create_mode = mode;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001704 dir = nd.path.dentry;
1705 nd.flags &= ~LOOKUP_PARENT;
Al Viro8737f3a2008-08-02 22:36:57 -04001706 nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
Al Viro35165862008-08-05 03:00:49 -04001707 if (flag & O_EXCL)
1708 nd.flags |= LOOKUP_EXCL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001709 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001710 path.dentry = lookup_hash(&nd);
1711 path.mnt = nd.path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713do_last:
Al Viro4e7506e2005-06-06 13:36:00 -07001714 error = PTR_ERR(path.dentry);
1715 if (IS_ERR(path.dentry)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001716 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 goto exit;
1718 }
1719
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001720 if (IS_ERR(nd.intent.open.file)) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001721 error = PTR_ERR(nd.intent.open.file);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001722 goto exit_mutex_unlock;
Oleg Drokin4af4c522006-03-25 03:06:54 -08001723 }
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 /* Negative dentry, just create the file */
Al Viro4e7506e2005-06-06 13:36:00 -07001726 if (!path.dentry->d_inode) {
Dave Hansen4a3fd212008-02-15 14:37:48 -08001727 /*
1728 * This write is needed to ensure that a
1729 * ro->rw transition does not occur between
1730 * the time when the file is created and when
1731 * a permanent write count is taken through
1732 * the 'struct file' in nameidata_to_filp().
1733 */
1734 error = mnt_want_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (error)
Dave Hansen4a3fd212008-02-15 14:37:48 -08001736 goto exit_mutex_unlock;
1737 error = __open_namei_create(&nd, &path, flag, mode);
1738 if (error) {
1739 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001741 }
1742 filp = nameidata_to_filp(&nd, open_flag);
1743 mnt_drop_write(nd.path.mnt);
1744 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
1747 /*
1748 * It already exists.
1749 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001750 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro5a190ae2007-06-07 12:19:32 -04001751 audit_inode(pathname, path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 error = -EEXIST;
1754 if (flag & O_EXCL)
1755 goto exit_dput;
1756
Al Viroe13b2102005-06-06 13:36:06 -07001757 if (__follow_mount(&path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 error = -ELOOP;
Al Viroba7a4c12005-06-06 13:36:08 -07001759 if (flag & O_NOFOLLOW)
1760 goto exit_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 error = -ENOENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001764 if (!path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 goto exit_dput;
Al Viro4e7506e2005-06-06 13:36:00 -07001766 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto do_link;
1768
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001769 path_to_nameidata(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 error = -EISDIR;
Al Viro4e7506e2005-06-06 13:36:00 -07001771 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto exit;
1773ok:
Dave Hansen4a3fd212008-02-15 14:37:48 -08001774 /*
1775 * Consider:
1776 * 1. may_open() truncates a file
1777 * 2. a rw->ro mount transition occurs
1778 * 3. nameidata_to_filp() fails due to
1779 * the ro mount.
1780 * That would be inconsistent, and should
1781 * be avoided. Taking this mnt write here
1782 * ensures that (2) can not occur.
1783 */
1784 will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
1785 if (will_write) {
1786 error = mnt_want_write(nd.path.mnt);
1787 if (error)
1788 goto exit;
1789 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001790 error = may_open(&nd, acc_mode, flag);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001791 if (error) {
1792 if (will_write)
1793 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001795 }
1796 filp = nameidata_to_filp(&nd, open_flag);
1797 /*
1798 * It is now safe to drop the mnt write
1799 * because the filp has had a write taken
1800 * on its behalf.
1801 */
1802 if (will_write)
1803 mnt_drop_write(nd.path.mnt);
1804 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Dave Hansen4a3fd212008-02-15 14:37:48 -08001806exit_mutex_unlock:
1807 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808exit_dput:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001809 path_put_conditional(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810exit:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001811 if (!IS_ERR(nd.intent.open.file))
1812 release_open_intent(&nd);
Al Viro8737f3a2008-08-02 22:36:57 -04001813exit_parent:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001814 path_put(&nd.path);
1815 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817do_link:
1818 error = -ELOOP;
1819 if (flag & O_NOFOLLOW)
1820 goto exit_dput;
1821 /*
1822 * This is subtle. Instead of calling do_follow_link() we do the
1823 * thing by hands. The reason is that this way we have zero link_count
1824 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1825 * After that we have the parent and last component, i.e.
1826 * we are in the same situation as after the first path_walk().
1827 * Well, almost - if the last component is normal we get its copy
1828 * stored in nd->last.name and we will have to putname() it when we
1829 * are done. Procfs-like symlinks just set LAST_BIND.
1830 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001831 nd.flags |= LOOKUP_PARENT;
1832 error = security_inode_follow_link(path.dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (error)
1834 goto exit_dput;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001835 error = __do_follow_link(&path, &nd);
Kirill Korotaevde459212006-07-14 00:23:49 -07001836 if (error) {
1837 /* Does someone understand code flow here? Or it is only
1838 * me so stupid? Anathema to whoever designed this non-sense
1839 * with "intent.open".
1840 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001841 release_open_intent(&nd);
1842 return ERR_PTR(error);
Kirill Korotaevde459212006-07-14 00:23:49 -07001843 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001844 nd.flags &= ~LOOKUP_PARENT;
1845 if (nd.last_type == LAST_BIND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001848 if (nd.last_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto exit;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001850 if (nd.last.name[nd.last.len]) {
1851 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto exit;
1853 }
1854 error = -ELOOP;
1855 if (count++==32) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001856 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto exit;
1858 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001859 dir = nd.path.dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001860 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001861 path.dentry = lookup_hash(&nd);
1862 path.mnt = nd.path.mnt;
1863 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto do_last;
1865}
1866
1867/**
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001868 * filp_open - open file and return file pointer
1869 *
1870 * @filename: path to open
1871 * @flags: open flags as per the open(2) second argument
1872 * @mode: mode for the new file if O_CREAT is set, else ignored
1873 *
1874 * This is the helper to open a file from kernelspace if you really
1875 * have to. But in generally you should not do this, so please move
1876 * along, nothing to see here..
1877 */
1878struct file *filp_open(const char *filename, int flags, int mode)
1879{
1880 return do_filp_open(AT_FDCWD, filename, flags, mode);
1881}
1882EXPORT_SYMBOL(filp_open);
1883
1884/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 * lookup_create - lookup a dentry, creating it if it doesn't exist
1886 * @nd: nameidata info
1887 * @is_dir: directory flag
1888 *
1889 * Simple function to lookup and return a dentry and create it
1890 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001891 *
Jan Blunck4ac91372008-02-14 19:34:32 -08001892 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
1894struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1895{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001896 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Jan Blunck4ac91372008-02-14 19:34:32 -08001898 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001899 /*
1900 * Yucky last component or no last component at all?
1901 * (foo/., foo/.., /////)
1902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (nd->last_type != LAST_NORM)
1904 goto fail;
1905 nd->flags &= ~LOOKUP_PARENT;
Al Viro35165862008-08-05 03:00:49 -04001906 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
ASANO Masahiroa6349042006-08-22 20:06:02 -04001907 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001908
1909 /*
1910 * Do the final lookup.
1911 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08001912 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (IS_ERR(dentry))
1914 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001915
Al Viroe9baf6e2008-05-15 04:49:12 -04001916 if (dentry->d_inode)
1917 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07001918 /*
1919 * Special case - lookup gave negative, but... we had foo/bar/
1920 * From the vfs_mknod() POV we just have a negative dentry -
1921 * all is fine. Let's be bastards - you had / on the end, you've
1922 * been asking for (non-existent) directory. -ENOENT for you.
1923 */
Al Viroe9baf6e2008-05-15 04:49:12 -04001924 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
1925 dput(dentry);
1926 dentry = ERR_PTR(-ENOENT);
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04001929eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04001931 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932fail:
1933 return dentry;
1934}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07001935EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1938{
Miklos Szeredia95164d2008-07-30 15:08:48 +02001939 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 if (error)
1942 return error;
1943
1944 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1945 return -EPERM;
1946
1947 if (!dir->i_op || !dir->i_op->mknod)
1948 return -EPERM;
1949
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07001950 error = devcgroup_inode_mknod(mode, dev);
1951 if (error)
1952 return error;
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 error = security_inode_mknod(dir, dentry, mode, dev);
1955 if (error)
1956 return error;
1957
1958 DQUOT_INIT(dir);
1959 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001960 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001961 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return error;
1963}
1964
Dave Hansen463c3192008-02-15 14:37:57 -08001965static int may_mknod(mode_t mode)
1966{
1967 switch (mode & S_IFMT) {
1968 case S_IFREG:
1969 case S_IFCHR:
1970 case S_IFBLK:
1971 case S_IFIFO:
1972 case S_IFSOCK:
1973 case 0: /* zero mode translates to S_IFREG */
1974 return 0;
1975 case S_IFDIR:
1976 return -EPERM;
1977 default:
1978 return -EINVAL;
1979 }
1980}
1981
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001982asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
1983 unsigned dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Al Viro2ad94ae2008-07-21 09:32:51 -04001985 int error;
1986 char *tmp;
1987 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 struct nameidata nd;
1989
1990 if (S_ISDIR(mode))
1991 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Al Viro2ad94ae2008-07-21 09:32:51 -04001993 error = user_path_parent(dfd, filename, &nd, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04001995 return error;
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08001998 if (IS_ERR(dentry)) {
1999 error = PTR_ERR(dentry);
2000 goto out_unlock;
2001 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002002 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002004 error = may_mknod(mode);
2005 if (error)
2006 goto out_dput;
2007 error = mnt_want_write(nd.path.mnt);
2008 if (error)
2009 goto out_dput;
2010 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002012 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 break;
2014 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002015 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 new_decode_dev(dev));
2017 break;
2018 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002019 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Dave Hansen463c3192008-02-15 14:37:57 -08002022 mnt_drop_write(nd.path.mnt);
2023out_dput:
2024 dput(dentry);
2025out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002026 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002027 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 putname(tmp);
2029
2030 return error;
2031}
2032
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002033asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
2034{
2035 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2039{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002040 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 if (error)
2043 return error;
2044
2045 if (!dir->i_op || !dir->i_op->mkdir)
2046 return -EPERM;
2047
2048 mode &= (S_IRWXUGO|S_ISVTX);
2049 error = security_inode_mkdir(dir, dentry, mode);
2050 if (error)
2051 return error;
2052
2053 DQUOT_INIT(dir);
2054 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002055 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002056 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return error;
2058}
2059
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002060asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 int error = 0;
2063 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002064 struct dentry *dentry;
2065 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Al Viro2ad94ae2008-07-21 09:32:51 -04002067 error = user_path_parent(dfd, pathname, &nd, &tmp);
2068 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002069 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Dave Hansen6902d922006-09-30 23:29:01 -07002071 dentry = lookup_create(&nd, 1);
2072 error = PTR_ERR(dentry);
2073 if (IS_ERR(dentry))
2074 goto out_unlock;
2075
Jan Blunck4ac91372008-02-14 19:34:32 -08002076 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Dave Hansen6902d922006-09-30 23:29:01 -07002077 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002078 error = mnt_want_write(nd.path.mnt);
2079 if (error)
2080 goto out_dput;
Jan Blunck4ac91372008-02-14 19:34:32 -08002081 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Dave Hansen463c3192008-02-15 14:37:57 -08002082 mnt_drop_write(nd.path.mnt);
2083out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002084 dput(dentry);
2085out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002086 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002087 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002088 putname(tmp);
2089out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return error;
2091}
2092
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002093asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2094{
2095 return sys_mkdirat(AT_FDCWD, pathname, mode);
2096}
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098/*
2099 * We try to drop the dentry early: we should have
2100 * a usage count of 2 if we're the only user of this
2101 * dentry, and if that is true (possibly after pruning
2102 * the dcache), then we drop the dentry now.
2103 *
2104 * A low-level filesystem can, if it choses, legally
2105 * do a
2106 *
2107 * if (!d_unhashed(dentry))
2108 * return -EBUSY;
2109 *
2110 * if it cannot handle the case of removing a directory
2111 * that is still in use by something else..
2112 */
2113void dentry_unhash(struct dentry *dentry)
2114{
2115 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002116 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 spin_lock(&dcache_lock);
2118 spin_lock(&dentry->d_lock);
2119 if (atomic_read(&dentry->d_count) == 2)
2120 __d_drop(dentry);
2121 spin_unlock(&dentry->d_lock);
2122 spin_unlock(&dcache_lock);
2123}
2124
2125int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2126{
2127 int error = may_delete(dir, dentry, 1);
2128
2129 if (error)
2130 return error;
2131
2132 if (!dir->i_op || !dir->i_op->rmdir)
2133 return -EPERM;
2134
2135 DQUOT_INIT(dir);
2136
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002137 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 dentry_unhash(dentry);
2139 if (d_mountpoint(dentry))
2140 error = -EBUSY;
2141 else {
2142 error = security_inode_rmdir(dir, dentry);
2143 if (!error) {
2144 error = dir->i_op->rmdir(dir, dentry);
2145 if (!error)
2146 dentry->d_inode->i_flags |= S_DEAD;
2147 }
2148 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002149 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 d_delete(dentry);
2152 }
2153 dput(dentry);
2154
2155 return error;
2156}
2157
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002158static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 int error = 0;
2161 char * name;
2162 struct dentry *dentry;
2163 struct nameidata nd;
2164
Al Viro2ad94ae2008-07-21 09:32:51 -04002165 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002167 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 switch(nd.last_type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002170 case LAST_DOTDOT:
2171 error = -ENOTEMPTY;
2172 goto exit1;
2173 case LAST_DOT:
2174 error = -EINVAL;
2175 goto exit1;
2176 case LAST_ROOT:
2177 error = -EBUSY;
2178 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002180
2181 nd.flags &= ~LOOKUP_PARENT;
2182
Jan Blunck4ac91372008-02-14 19:34:32 -08002183 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002184 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002186 if (IS_ERR(dentry))
2187 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002188 error = mnt_want_write(nd.path.mnt);
2189 if (error)
2190 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002191 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08002192 mnt_drop_write(nd.path.mnt);
2193exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002194 dput(dentry);
2195exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002196 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002198 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 putname(name);
2200 return error;
2201}
2202
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002203asmlinkage long sys_rmdir(const char __user *pathname)
2204{
2205 return do_rmdir(AT_FDCWD, pathname);
2206}
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208int vfs_unlink(struct inode *dir, struct dentry *dentry)
2209{
2210 int error = may_delete(dir, dentry, 0);
2211
2212 if (error)
2213 return error;
2214
2215 if (!dir->i_op || !dir->i_op->unlink)
2216 return -EPERM;
2217
2218 DQUOT_INIT(dir);
2219
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002220 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (d_mountpoint(dentry))
2222 error = -EBUSY;
2223 else {
2224 error = security_inode_unlink(dir, dentry);
2225 if (!error)
2226 error = dir->i_op->unlink(dir, dentry);
2227 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002228 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2231 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002232 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Robert Love0eeca282005-07-12 17:06:03 -04002235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return error;
2237}
2238
2239/*
2240 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002241 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 * writeout happening, and we don't want to prevent access to the directory
2243 * while waiting on the I/O.
2244 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002245static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Al Viro2ad94ae2008-07-21 09:32:51 -04002247 int error;
2248 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct dentry *dentry;
2250 struct nameidata nd;
2251 struct inode *inode = NULL;
2252
Al Viro2ad94ae2008-07-21 09:32:51 -04002253 error = user_path_parent(dfd, pathname, &nd, &name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002255 return error;
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 error = -EISDIR;
2258 if (nd.last_type != LAST_NORM)
2259 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002260
2261 nd.flags &= ~LOOKUP_PARENT;
2262
Jan Blunck4ac91372008-02-14 19:34:32 -08002263 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002264 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 error = PTR_ERR(dentry);
2266 if (!IS_ERR(dentry)) {
2267 /* Why not before? Because we want correct error value */
2268 if (nd.last.name[nd.last.len])
2269 goto slashes;
2270 inode = dentry->d_inode;
2271 if (inode)
2272 atomic_inc(&inode->i_count);
Dave Hansen06227532008-02-15 14:37:34 -08002273 error = mnt_want_write(nd.path.mnt);
2274 if (error)
2275 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08002276 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08002277 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 exit2:
2279 dput(dentry);
2280 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002281 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (inode)
2283 iput(inode); /* truncate the inode here */
2284exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002285 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 putname(name);
2287 return error;
2288
2289slashes:
2290 error = !dentry->d_inode ? -ENOENT :
2291 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2292 goto exit2;
2293}
2294
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002295asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2296{
2297 if ((flag & ~AT_REMOVEDIR) != 0)
2298 return -EINVAL;
2299
2300 if (flag & AT_REMOVEDIR)
2301 return do_rmdir(dfd, pathname);
2302
2303 return do_unlinkat(dfd, pathname);
2304}
2305
2306asmlinkage long sys_unlink(const char __user *pathname)
2307{
2308 return do_unlinkat(AT_FDCWD, pathname);
2309}
2310
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002311int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002313 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (error)
2316 return error;
2317
2318 if (!dir->i_op || !dir->i_op->symlink)
2319 return -EPERM;
2320
2321 error = security_inode_symlink(dir, dentry, oldname);
2322 if (error)
2323 return error;
2324
2325 DQUOT_INIT(dir);
2326 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002327 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002328 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return error;
2330}
2331
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002332asmlinkage long sys_symlinkat(const char __user *oldname,
2333 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Al Viro2ad94ae2008-07-21 09:32:51 -04002335 int error;
2336 char *from;
2337 char *to;
Dave Hansen6902d922006-09-30 23:29:01 -07002338 struct dentry *dentry;
2339 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04002342 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return PTR_ERR(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002344
2345 error = user_path_parent(newdfd, newname, &nd, &to);
2346 if (error)
Dave Hansen6902d922006-09-30 23:29:01 -07002347 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Dave Hansen6902d922006-09-30 23:29:01 -07002349 dentry = lookup_create(&nd, 0);
2350 error = PTR_ERR(dentry);
2351 if (IS_ERR(dentry))
2352 goto out_unlock;
2353
Dave Hansen75c3f292008-02-15 14:37:45 -08002354 error = mnt_want_write(nd.path.mnt);
2355 if (error)
2356 goto out_dput;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002357 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Dave Hansen75c3f292008-02-15 14:37:45 -08002358 mnt_drop_write(nd.path.mnt);
2359out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002360 dput(dentry);
2361out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002362 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002363 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -07002364 putname(to);
2365out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 putname(from);
2367 return error;
2368}
2369
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002370asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2371{
2372 return sys_symlinkat(oldname, AT_FDCWD, newname);
2373}
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2376{
2377 struct inode *inode = old_dentry->d_inode;
2378 int error;
2379
2380 if (!inode)
2381 return -ENOENT;
2382
Miklos Szeredia95164d2008-07-30 15:08:48 +02002383 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (error)
2385 return error;
2386
2387 if (dir->i_sb != inode->i_sb)
2388 return -EXDEV;
2389
2390 /*
2391 * A link to an append-only or immutable file cannot be created.
2392 */
2393 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2394 return -EPERM;
2395 if (!dir->i_op || !dir->i_op->link)
2396 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002397 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return -EPERM;
2399
2400 error = security_inode_link(old_dentry, dir, new_dentry);
2401 if (error)
2402 return error;
2403
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002404 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 DQUOT_INIT(dir);
2406 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002407 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002408 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002409 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return error;
2411}
2412
2413/*
2414 * Hardlinks are often used in delicate situations. We avoid
2415 * security-related surprises by not following symlinks on the
2416 * newname. --KAB
2417 *
2418 * We don't follow them on the oldname either to be compatible
2419 * with linux 2.0, and to avoid hard-linking to directories
2420 * and other special files. --ADM
2421 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002422asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002423 int newdfd, const char __user *newname,
2424 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 struct dentry *new_dentry;
Al Viro2d8f3032008-07-22 09:59:21 -04002427 struct nameidata nd;
2428 struct path old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 int error;
Al Viro2ad94ae2008-07-21 09:32:51 -04002430 char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002432 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002433 return -EINVAL;
2434
Al Viro2d8f3032008-07-22 09:59:21 -04002435 error = user_path_at(olddfd, oldname,
2436 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2437 &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04002439 return error;
2440
2441 error = user_path_parent(newdfd, newname, &nd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (error)
2443 goto out;
2444 error = -EXDEV;
Al Viro2d8f3032008-07-22 09:59:21 -04002445 if (old_path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto out_release;
2447 new_dentry = lookup_create(&nd, 0);
2448 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002449 if (IS_ERR(new_dentry))
2450 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002451 error = mnt_want_write(nd.path.mnt);
2452 if (error)
2453 goto out_dput;
Al Viro2d8f3032008-07-22 09:59:21 -04002454 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08002455 mnt_drop_write(nd.path.mnt);
2456out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002457 dput(new_dentry);
2458out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002459 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002461 path_put(&nd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002462 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463out:
Al Viro2d8f3032008-07-22 09:59:21 -04002464 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 return error;
2467}
2468
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002469asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2470{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002471 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002472}
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474/*
2475 * The worst of all namespace operations - renaming directory. "Perverted"
2476 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2477 * Problems:
2478 * a) we can get into loop creation. Check is done in is_subdir().
2479 * b) race potential - two innocent renames can create a loop together.
2480 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002481 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 * story.
2483 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002484 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 * whether the target exists). Solution: try to be smart with locking
2486 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002487 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 * move will be locked. Thus we can rank directories by the tree
2489 * (ancestors first) and rank all non-directories after them.
2490 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002491 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 * HOWEVER, it relies on the assumption that any object with ->lookup()
2493 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2494 * we'd better make sure that there's no link(2) for them.
2495 * d) some filesystems don't support opened-but-unlinked directories,
2496 * either because of layout or because they are not ready to deal with
2497 * all cases correctly. The latter will be fixed (taking this sort of
2498 * stuff into VFS), but the former is not going away. Solution: the same
2499 * trick as in rmdir().
2500 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002501 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002503 * ->i_mutex on parents, which works but leads to some truely excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 * locking].
2505 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002506static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2507 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
2509 int error = 0;
2510 struct inode *target;
2511
2512 /*
2513 * If we are going to change the parent - check write permissions,
2514 * we'll need to flip '..'.
2515 */
2516 if (new_dir != old_dir) {
Al Virof419a2e2008-07-22 00:07:17 -04002517 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (error)
2519 return error;
2520 }
2521
2522 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2523 if (error)
2524 return error;
2525
2526 target = new_dentry->d_inode;
2527 if (target) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002528 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 dentry_unhash(new_dentry);
2530 }
2531 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2532 error = -EBUSY;
2533 else
2534 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2535 if (target) {
2536 if (!error)
2537 target->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002538 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (d_unhashed(new_dentry))
2540 d_rehash(new_dentry);
2541 dput(new_dentry);
2542 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002543 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002544 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2545 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return error;
2547}
2548
Adrian Bunk75c96f82005-05-05 16:16:09 -07002549static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2550 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 struct inode *target;
2553 int error;
2554
2555 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2556 if (error)
2557 return error;
2558
2559 dget(new_dentry);
2560 target = new_dentry->d_inode;
2561 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002562 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2564 error = -EBUSY;
2565 else
2566 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2567 if (!error) {
Mark Fasheh349457c2006-09-08 14:22:21 -07002568 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002572 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 dput(new_dentry);
2574 return error;
2575}
2576
2577int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2578 struct inode *new_dir, struct dentry *new_dentry)
2579{
2580 int error;
2581 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Robert Love0eeca282005-07-12 17:06:03 -04002582 const char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 if (old_dentry->d_inode == new_dentry->d_inode)
2585 return 0;
2586
2587 error = may_delete(old_dir, old_dentry, is_dir);
2588 if (error)
2589 return error;
2590
2591 if (!new_dentry->d_inode)
Miklos Szeredia95164d2008-07-30 15:08:48 +02002592 error = may_create(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 else
2594 error = may_delete(new_dir, new_dentry, is_dir);
2595 if (error)
2596 return error;
2597
2598 if (!old_dir->i_op || !old_dir->i_op->rename)
2599 return -EPERM;
2600
2601 DQUOT_INIT(old_dir);
2602 DQUOT_INIT(new_dir);
2603
Robert Love0eeca282005-07-12 17:06:03 -04002604 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (is_dir)
2607 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2608 else
2609 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2610 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -04002611 const char *new_name = old_dentry->d_name.name;
John McCutchan89204c42005-08-15 12:13:28 -04002612 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04002613 new_dentry->d_inode, old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
Robert Love0eeca282005-07-12 17:06:03 -04002615 fsnotify_oldname_free(old_name);
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return error;
2618}
2619
Al Viro2ad94ae2008-07-21 09:32:51 -04002620asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2621 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Al Viro2ad94ae2008-07-21 09:32:51 -04002623 struct dentry *old_dir, *new_dir;
2624 struct dentry *old_dentry, *new_dentry;
2625 struct dentry *trap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 struct nameidata oldnd, newnd;
Al Viro2ad94ae2008-07-21 09:32:51 -04002627 char *from;
2628 char *to;
2629 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Al Viro2ad94ae2008-07-21 09:32:51 -04002631 error = user_path_parent(olddfd, oldname, &oldnd, &from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (error)
2633 goto exit;
2634
Al Viro2ad94ae2008-07-21 09:32:51 -04002635 error = user_path_parent(newdfd, newname, &newnd, &to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (error)
2637 goto exit1;
2638
2639 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08002640 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 goto exit2;
2642
Jan Blunck4ac91372008-02-14 19:34:32 -08002643 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 error = -EBUSY;
2645 if (oldnd.last_type != LAST_NORM)
2646 goto exit2;
2647
Jan Blunck4ac91372008-02-14 19:34:32 -08002648 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (newnd.last_type != LAST_NORM)
2650 goto exit2;
2651
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002652 oldnd.flags &= ~LOOKUP_PARENT;
2653 newnd.flags &= ~LOOKUP_PARENT;
OGAWA Hirofumi4e9ed2f2008-10-16 07:50:29 +09002654 newnd.flags |= LOOKUP_RENAME_TARGET;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09002655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 trap = lock_rename(new_dir, old_dir);
2657
Christoph Hellwig49705b72005-11-08 21:35:06 -08002658 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 error = PTR_ERR(old_dentry);
2660 if (IS_ERR(old_dentry))
2661 goto exit3;
2662 /* source must exist */
2663 error = -ENOENT;
2664 if (!old_dentry->d_inode)
2665 goto exit4;
2666 /* unless the source is a directory trailing slashes give -ENOTDIR */
2667 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2668 error = -ENOTDIR;
2669 if (oldnd.last.name[oldnd.last.len])
2670 goto exit4;
2671 if (newnd.last.name[newnd.last.len])
2672 goto exit4;
2673 }
2674 /* source should not be ancestor of target */
2675 error = -EINVAL;
2676 if (old_dentry == trap)
2677 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08002678 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 error = PTR_ERR(new_dentry);
2680 if (IS_ERR(new_dentry))
2681 goto exit4;
2682 /* target should not be an ancestor of source */
2683 error = -ENOTEMPTY;
2684 if (new_dentry == trap)
2685 goto exit5;
2686
Dave Hansen9079b1e2008-02-15 14:37:49 -08002687 error = mnt_want_write(oldnd.path.mnt);
2688 if (error)
2689 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 error = vfs_rename(old_dir->d_inode, old_dentry,
2691 new_dir->d_inode, new_dentry);
Dave Hansen9079b1e2008-02-15 14:37:49 -08002692 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693exit5:
2694 dput(new_dentry);
2695exit4:
2696 dput(old_dentry);
2697exit3:
2698 unlock_rename(new_dir, old_dir);
2699exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08002700 path_put(&newnd.path);
Al Viro2ad94ae2008-07-21 09:32:51 -04002701 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002703 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 putname(from);
Al Viro2ad94ae2008-07-21 09:32:51 -04002705exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return error;
2707}
2708
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002709asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2710{
2711 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2712}
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2715{
2716 int len;
2717
2718 len = PTR_ERR(link);
2719 if (IS_ERR(link))
2720 goto out;
2721
2722 len = strlen(link);
2723 if (len > (unsigned) buflen)
2724 len = buflen;
2725 if (copy_to_user(buffer, link, len))
2726 len = -EFAULT;
2727out:
2728 return len;
2729}
2730
2731/*
2732 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2733 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2734 * using) it for any given inode is up to filesystem.
2735 */
2736int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2737{
2738 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002739 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07002740 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002743 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07002744 if (IS_ERR(cookie))
2745 return PTR_ERR(cookie);
2746
2747 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2748 if (dentry->d_inode->i_op->put_link)
2749 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2750 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
2753int vfs_follow_link(struct nameidata *nd, const char *link)
2754{
2755 return __vfs_follow_link(nd, link);
2756}
2757
2758/* get the link contents into pagecache */
2759static char *page_getlink(struct dentry * dentry, struct page **ppage)
2760{
2761 struct page * page;
2762 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07002763 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07002765 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 *ppage = page;
2767 return kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2771{
2772 struct page *page = NULL;
2773 char *s = page_getlink(dentry, &page);
2774 int res = vfs_readlink(dentry,buffer,buflen,s);
2775 if (page) {
2776 kunmap(page);
2777 page_cache_release(page);
2778 }
2779 return res;
2780}
2781
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002782void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002784 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002786 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002789void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002791 struct page *page = cookie;
2792
2793 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 kunmap(page);
2795 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797}
2798
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002799int __page_symlink(struct inode *inode, const char *symname, int len,
2800 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
2802 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002803 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002804 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08002805 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 char *kaddr;
2807
NeilBrown7e53cac2006-03-25 03:07:57 -08002808retry:
Nick Pigginafddba42007-10-16 01:25:01 -07002809 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2810 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07002812 goto fail;
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 kaddr = kmap_atomic(page, KM_USER0);
2815 memcpy(kaddr, symname, len-1);
2816 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07002817
2818 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2819 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (err < 0)
2821 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07002822 if (err < len-1)
2823 goto retry;
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 mark_inode_dirty(inode);
2826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827fail:
2828 return err;
2829}
2830
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002831int page_symlink(struct inode *inode, const char *symname, int len)
2832{
2833 return __page_symlink(inode, symname, len,
2834 mapping_gfp_mask(inode->i_mapping));
2835}
2836
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002837const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 .readlink = generic_readlink,
2839 .follow_link = page_follow_link_light,
2840 .put_link = page_put_link,
2841};
2842
Al Viro2d8f3032008-07-22 09:59:21 -04002843EXPORT_SYMBOL(user_path_at);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844EXPORT_SYMBOL(follow_down);
2845EXPORT_SYMBOL(follow_up);
2846EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2847EXPORT_SYMBOL(getname);
2848EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849EXPORT_SYMBOL(lookup_one_len);
2850EXPORT_SYMBOL(page_follow_link_light);
2851EXPORT_SYMBOL(page_put_link);
2852EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002853EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854EXPORT_SYMBOL(page_symlink);
2855EXPORT_SYMBOL(page_symlink_inode_operations);
2856EXPORT_SYMBOL(path_lookup);
Al Virod1811462008-08-02 00:49:18 -04002857EXPORT_SYMBOL(kern_path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002858EXPORT_SYMBOL(vfs_path_lookup);
Al Virof419a2e2008-07-22 00:07:17 -04002859EXPORT_SYMBOL(inode_permission);
Christoph Hellwige4543ed2005-11-08 21:35:04 -08002860EXPORT_SYMBOL(vfs_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08002861EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862EXPORT_SYMBOL(unlock_rename);
2863EXPORT_SYMBOL(vfs_create);
2864EXPORT_SYMBOL(vfs_follow_link);
2865EXPORT_SYMBOL(vfs_link);
2866EXPORT_SYMBOL(vfs_mkdir);
2867EXPORT_SYMBOL(vfs_mknod);
2868EXPORT_SYMBOL(generic_permission);
2869EXPORT_SYMBOL(vfs_readlink);
2870EXPORT_SYMBOL(vfs_rename);
2871EXPORT_SYMBOL(vfs_rmdir);
2872EXPORT_SYMBOL(vfs_symlink);
2873EXPORT_SYMBOL(vfs_unlink);
2874EXPORT_SYMBOL(dentry_unhash);
2875EXPORT_SYMBOL(generic_readlink);