Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/personality.h> |
| 26 | #include <linux/security.h> |
| 27 | #include <linux/syscalls.h> |
| 28 | #include <linux/mount.h> |
| 29 | #include <linux/audit.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 30 | #include <linux/capability.h> |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 31 | #include <linux/file.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 32 | #include <linux/fcntl.h> |
| 33 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/namei.h> |
| 35 | #include <asm/uaccess.h> |
| 36 | |
| 37 | #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE]) |
| 38 | |
| 39 | /* [Feb-1997 T. Schoebel-Theuer] |
| 40 | * Fundamental changes in the pathname lookup mechanisms (namei) |
| 41 | * were necessary because of omirr. The reason is that omirr needs |
| 42 | * to know the _real_ pathname, not the user-supplied one, in case |
| 43 | * of symlinks (and also when transname replacements occur). |
| 44 | * |
| 45 | * The new code replaces the old recursive symlink resolution with |
| 46 | * an iterative one (in case of non-nested symlink chains). It does |
| 47 | * this with calls to <fs>_follow_link(). |
| 48 | * As a side effect, dir_namei(), _namei() and follow_link() are now |
| 49 | * replaced with a single function lookup_dentry() that can handle all |
| 50 | * the special cases of the former code. |
| 51 | * |
| 52 | * With the new dcache, the pathname is stored at each inode, at least as |
| 53 | * long as the refcount of the inode is positive. As a side effect, the |
| 54 | * size of the dcache depends on the inode cache and thus is dynamic. |
| 55 | * |
| 56 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink |
| 57 | * resolution to correspond with current state of the code. |
| 58 | * |
| 59 | * Note that the symlink resolution is not *completely* iterative. |
| 60 | * There is still a significant amount of tail- and mid- recursion in |
| 61 | * the algorithm. Also, note that <fs>_readlink() is not used in |
| 62 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() |
| 63 | * may return different results than <fs>_follow_link(). Many virtual |
| 64 | * filesystems (including /proc) exhibit this behavior. |
| 65 | */ |
| 66 | |
| 67 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: |
| 68 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL |
| 69 | * and the name already exists in form of a symlink, try to create the new |
| 70 | * name indicated by the symlink. The old code always complained that the |
| 71 | * name already exists, due to not following the symlink even if its target |
| 72 | * is nonexistent. The new semantics affects also mknod() and link() when |
| 73 | * the name is a symlink pointing to a non-existant name. |
| 74 | * |
| 75 | * I don't know which semantics is the right one, since I have no access |
| 76 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" |
| 77 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the |
| 78 | * "old" one. Personally, I think the new semantics is much more logical. |
| 79 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing |
| 80 | * file does succeed in both HP-UX and SunOs, but not in Solaris |
| 81 | * and in the old Linux semantics. |
| 82 | */ |
| 83 | |
| 84 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink |
| 85 | * semantics. See the comments in "open_namei" and "do_link" below. |
| 86 | * |
| 87 | * [10-Sep-98 Alan Modra] Another symlink change. |
| 88 | */ |
| 89 | |
| 90 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: |
| 91 | * inside the path - always follow. |
| 92 | * in the last component in creation/removal/renaming - never follow. |
| 93 | * if LOOKUP_FOLLOW passed - follow. |
| 94 | * if the pathname has trailing slashes - follow. |
| 95 | * otherwise - don't follow. |
| 96 | * (applied in that order). |
| 97 | * |
| 98 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT |
| 99 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. |
| 100 | * During the 2.4 we need to fix the userland stuff depending on it - |
| 101 | * hopefully we will be able to get rid of that wart in 2.5. So far only |
| 102 | * XEmacs seems to be relying on it... |
| 103 | */ |
| 104 | /* |
| 105 | * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 106 | * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * any extra contention... |
| 108 | */ |
| 109 | |
| 110 | /* In order to reduce some races, while at the same time doing additional |
| 111 | * checking and hopefully speeding things up, we copy filenames to the |
| 112 | * kernel data space before using them.. |
| 113 | * |
| 114 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). |
| 115 | * PATH_MAX includes the nul terminator --RR. |
| 116 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 117 | static int do_getname(const char __user *filename, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | int retval; |
| 120 | unsigned long len = PATH_MAX; |
| 121 | |
| 122 | if (!segment_eq(get_fs(), KERNEL_DS)) { |
| 123 | if ((unsigned long) filename >= TASK_SIZE) |
| 124 | return -EFAULT; |
| 125 | if (TASK_SIZE - (unsigned long) filename < PATH_MAX) |
| 126 | len = TASK_SIZE - (unsigned long) filename; |
| 127 | } |
| 128 | |
| 129 | retval = strncpy_from_user(page, filename, len); |
| 130 | if (retval > 0) { |
| 131 | if (retval < len) |
| 132 | return 0; |
| 133 | return -ENAMETOOLONG; |
| 134 | } else if (!retval) |
| 135 | retval = -ENOENT; |
| 136 | return retval; |
| 137 | } |
| 138 | |
| 139 | char * getname(const char __user * filename) |
| 140 | { |
| 141 | char *tmp, *result; |
| 142 | |
| 143 | result = ERR_PTR(-ENOMEM); |
| 144 | tmp = __getname(); |
| 145 | if (tmp) { |
| 146 | int retval = do_getname(filename, tmp); |
| 147 | |
| 148 | result = tmp; |
| 149 | if (retval < 0) { |
| 150 | __putname(tmp); |
| 151 | result = ERR_PTR(retval); |
| 152 | } |
| 153 | } |
| 154 | audit_getname(result); |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | #ifdef CONFIG_AUDITSYSCALL |
| 159 | void putname(const char *name) |
| 160 | { |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 161 | if (unlikely(!audit_dummy_context())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | audit_putname(name); |
| 163 | else |
| 164 | __putname(name); |
| 165 | } |
| 166 | EXPORT_SYMBOL(putname); |
| 167 | #endif |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * generic_permission - check for access rights on a Posix-like filesystem |
| 172 | * @inode: inode to check access rights for |
| 173 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 174 | * @check_acl: optional callback to check for Posix ACLs |
| 175 | * |
| 176 | * Used to check for read/write/execute permissions on a file. |
| 177 | * We use "fsuid" for this, letting us set arbitrary permissions |
| 178 | * for filesystem access without changing the "normal" uids which |
| 179 | * are used for other things.. |
| 180 | */ |
| 181 | int generic_permission(struct inode *inode, int mask, |
| 182 | int (*check_acl)(struct inode *inode, int mask)) |
| 183 | { |
| 184 | umode_t mode = inode->i_mode; |
| 185 | |
| 186 | if (current->fsuid == inode->i_uid) |
| 187 | mode >>= 6; |
| 188 | else { |
| 189 | if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { |
| 190 | int error = check_acl(inode, mask); |
| 191 | if (error == -EACCES) |
| 192 | goto check_capabilities; |
| 193 | else if (error != -EAGAIN) |
| 194 | return error; |
| 195 | } |
| 196 | |
| 197 | if (in_group_p(inode->i_gid)) |
| 198 | mode >>= 3; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * If the DACs are ok we don't need any capability check. |
| 203 | */ |
| 204 | if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)) |
| 205 | return 0; |
| 206 | |
| 207 | check_capabilities: |
| 208 | /* |
| 209 | * Read/write DACs are always overridable. |
| 210 | * Executable DACs are overridable if at least one exec bit is set. |
| 211 | */ |
| 212 | if (!(mask & MAY_EXEC) || |
| 213 | (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode)) |
| 214 | if (capable(CAP_DAC_OVERRIDE)) |
| 215 | return 0; |
| 216 | |
| 217 | /* |
| 218 | * Searching includes executable on directories, else just read. |
| 219 | */ |
| 220 | if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) |
| 221 | if (capable(CAP_DAC_READ_SEARCH)) |
| 222 | return 0; |
| 223 | |
| 224 | return -EACCES; |
| 225 | } |
| 226 | |
| 227 | int permission(struct inode *inode, int mask, struct nameidata *nd) |
| 228 | { |
Trond Myklebust | a343bb7 | 2006-08-22 20:06:03 -0400 | [diff] [blame] | 229 | umode_t mode = inode->i_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | int retval, submask; |
| 231 | |
| 232 | if (mask & MAY_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * Nobody gets write access to a read-only fs. |
| 236 | */ |
| 237 | if (IS_RDONLY(inode) && |
| 238 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
| 239 | return -EROFS; |
| 240 | |
| 241 | /* |
| 242 | * Nobody gets write access to an immutable file. |
| 243 | */ |
| 244 | if (IS_IMMUTABLE(inode)) |
| 245 | return -EACCES; |
| 246 | } |
| 247 | |
| 248 | |
Trond Myklebust | a343bb7 | 2006-08-22 20:06:03 -0400 | [diff] [blame] | 249 | /* |
| 250 | * MAY_EXEC on regular files requires special handling: We override |
Stas Sergeev | 317a40a | 2006-12-06 20:35:25 -0800 | [diff] [blame] | 251 | * filesystem execute permissions if the mode bits aren't set or |
| 252 | * the fs is mounted with the "noexec" flag. |
Trond Myklebust | a343bb7 | 2006-08-22 20:06:03 -0400 | [diff] [blame] | 253 | */ |
Stas Sergeev | 317a40a | 2006-12-06 20:35:25 -0800 | [diff] [blame] | 254 | if ((mask & MAY_EXEC) && S_ISREG(mode) && (!(mode & S_IXUGO) || |
| 255 | (nd && nd->mnt && (nd->mnt->mnt_flags & MNT_NOEXEC)))) |
Trond Myklebust | a343bb7 | 2006-08-22 20:06:03 -0400 | [diff] [blame] | 256 | return -EACCES; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | /* Ordinary permission routines do not understand MAY_APPEND. */ |
| 259 | submask = mask & ~MAY_APPEND; |
| 260 | if (inode->i_op && inode->i_op->permission) |
| 261 | retval = inode->i_op->permission(inode, submask, nd); |
| 262 | else |
| 263 | retval = generic_permission(inode, submask, NULL); |
| 264 | if (retval) |
| 265 | return retval; |
| 266 | |
| 267 | return security_inode_permission(inode, mask, nd); |
| 268 | } |
| 269 | |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 270 | /** |
| 271 | * vfs_permission - check for access rights to a given path |
| 272 | * @nd: lookup result that describes the path |
| 273 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 274 | * |
| 275 | * Used to check for read/write/execute permissions on a path. |
| 276 | * We use "fsuid" for this, letting us set arbitrary permissions |
| 277 | * for filesystem access without changing the "normal" uids which |
| 278 | * are used for other things. |
| 279 | */ |
| 280 | int vfs_permission(struct nameidata *nd, int mask) |
| 281 | { |
| 282 | return permission(nd->dentry->d_inode, mask, nd); |
| 283 | } |
| 284 | |
Christoph Hellwig | 8c744fb | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 285 | /** |
| 286 | * file_permission - check for additional access rights to a given file |
| 287 | * @file: file to check access rights for |
| 288 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 289 | * |
| 290 | * Used to check for read/write/execute permissions on an already opened |
| 291 | * file. |
| 292 | * |
| 293 | * Note: |
| 294 | * Do not use this function in new code. All access checks should |
| 295 | * be done using vfs_permission(). |
| 296 | */ |
| 297 | int file_permission(struct file *file, int mask) |
| 298 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 299 | return permission(file->f_path.dentry->d_inode, mask, NULL); |
Christoph Hellwig | 8c744fb | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | /* |
| 303 | * get_write_access() gets write permission for a file. |
| 304 | * put_write_access() releases this write permission. |
| 305 | * This is used for regular files. |
| 306 | * We cannot support write (and maybe mmap read-write shared) accesses and |
| 307 | * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode |
| 308 | * can have the following values: |
| 309 | * 0: no writers, no VM_DENYWRITE mappings |
| 310 | * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist |
| 311 | * > 0: (i_writecount) users are writing to the file. |
| 312 | * |
| 313 | * Normally we operate on that counter with atomic_{inc,dec} and it's safe |
| 314 | * except for the cases where we don't hold i_writecount yet. Then we need to |
| 315 | * use {get,deny}_write_access() - these functions check the sign and refuse |
| 316 | * to do the change if sign is wrong. Exclusion between them is provided by |
| 317 | * the inode->i_lock spinlock. |
| 318 | */ |
| 319 | |
| 320 | int get_write_access(struct inode * inode) |
| 321 | { |
| 322 | spin_lock(&inode->i_lock); |
| 323 | if (atomic_read(&inode->i_writecount) < 0) { |
| 324 | spin_unlock(&inode->i_lock); |
| 325 | return -ETXTBSY; |
| 326 | } |
| 327 | atomic_inc(&inode->i_writecount); |
| 328 | spin_unlock(&inode->i_lock); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | int deny_write_access(struct file * file) |
| 334 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 335 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | spin_lock(&inode->i_lock); |
| 338 | if (atomic_read(&inode->i_writecount) > 0) { |
| 339 | spin_unlock(&inode->i_lock); |
| 340 | return -ETXTBSY; |
| 341 | } |
| 342 | atomic_dec(&inode->i_writecount); |
| 343 | spin_unlock(&inode->i_lock); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | void path_release(struct nameidata *nd) |
| 349 | { |
| 350 | dput(nd->dentry); |
| 351 | mntput(nd->mnt); |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * umount() mustn't call path_release()/mntput() as that would clear |
| 356 | * mnt_expiry_mark |
| 357 | */ |
| 358 | void path_release_on_umount(struct nameidata *nd) |
| 359 | { |
| 360 | dput(nd->dentry); |
Miklos Szeredi | 751c404 | 2005-07-07 17:57:30 -0700 | [diff] [blame] | 361 | mntput_no_expire(nd->mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 364 | /** |
| 365 | * release_open_intent - free up open intent resources |
| 366 | * @nd: pointer to nameidata |
| 367 | */ |
| 368 | void release_open_intent(struct nameidata *nd) |
| 369 | { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 370 | if (nd->intent.open.file->f_path.dentry == NULL) |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 371 | put_filp(nd->intent.open.file); |
| 372 | else |
| 373 | fput(nd->intent.open.file); |
| 374 | } |
| 375 | |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 376 | static inline struct dentry * |
| 377 | do_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 378 | { |
| 379 | int status = dentry->d_op->d_revalidate(dentry, nd); |
| 380 | if (unlikely(status <= 0)) { |
| 381 | /* |
| 382 | * The dentry failed validation. |
| 383 | * If d_revalidate returned 0 attempt to invalidate |
| 384 | * the dentry otherwise d_revalidate is asking us |
| 385 | * to return a fail status. |
| 386 | */ |
| 387 | if (!status) { |
| 388 | if (!d_invalidate(dentry)) { |
| 389 | dput(dentry); |
| 390 | dentry = NULL; |
| 391 | } |
| 392 | } else { |
| 393 | dput(dentry); |
| 394 | dentry = ERR_PTR(status); |
| 395 | } |
| 396 | } |
| 397 | return dentry; |
| 398 | } |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | /* |
| 401 | * Internal lookup() using the new generic dcache. |
| 402 | * SMP-safe |
| 403 | */ |
| 404 | static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd) |
| 405 | { |
| 406 | struct dentry * dentry = __d_lookup(parent, name); |
| 407 | |
| 408 | /* lockess __d_lookup may fail due to concurrent d_move() |
| 409 | * in some unrelated directory, so try with d_lookup |
| 410 | */ |
| 411 | if (!dentry) |
| 412 | dentry = d_lookup(parent, name); |
| 413 | |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 414 | if (dentry && dentry->d_op && dentry->d_op->d_revalidate) |
| 415 | dentry = do_revalidate(dentry, nd); |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return dentry; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Short-cut version of permission(), for calling by |
| 422 | * path_walk(), when dcache lock is held. Combines parts |
| 423 | * of permission() and generic_permission(), and tests ONLY for |
| 424 | * MAY_EXEC permission. |
| 425 | * |
| 426 | * If appropriate, check DAC only. If not appropriate, or |
| 427 | * short-cut DAC fails, then call permission() to do more |
| 428 | * complete permission check. |
| 429 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 430 | static int exec_permission_lite(struct inode *inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | struct nameidata *nd) |
| 432 | { |
| 433 | umode_t mode = inode->i_mode; |
| 434 | |
| 435 | if (inode->i_op && inode->i_op->permission) |
| 436 | return -EAGAIN; |
| 437 | |
| 438 | if (current->fsuid == inode->i_uid) |
| 439 | mode >>= 6; |
| 440 | else if (in_group_p(inode->i_gid)) |
| 441 | mode >>= 3; |
| 442 | |
| 443 | if (mode & MAY_EXEC) |
| 444 | goto ok; |
| 445 | |
| 446 | if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE)) |
| 447 | goto ok; |
| 448 | |
| 449 | if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE)) |
| 450 | goto ok; |
| 451 | |
| 452 | if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH)) |
| 453 | goto ok; |
| 454 | |
| 455 | return -EACCES; |
| 456 | ok: |
| 457 | return security_inode_permission(inode, MAY_EXEC, nd); |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * This is called when everything else fails, and we actually have |
| 462 | * to go to the low-level filesystem to find out what we should do.. |
| 463 | * |
| 464 | * We get the directory semaphore, and after getting that we also |
| 465 | * make sure that nobody added the entry to the dcache in the meantime.. |
| 466 | * SMP-safe |
| 467 | */ |
| 468 | static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd) |
| 469 | { |
| 470 | struct dentry * result; |
| 471 | struct inode *dir = parent->d_inode; |
| 472 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 473 | mutex_lock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | /* |
| 475 | * First re-do the cached lookup just in case it was created |
| 476 | * while we waited for the directory semaphore.. |
| 477 | * |
| 478 | * FIXME! This could use version numbering or similar to |
| 479 | * avoid unnecessary cache lookups. |
| 480 | * |
| 481 | * The "dcache_lock" is purely to protect the RCU list walker |
| 482 | * from concurrent renames at this point (we mustn't get false |
| 483 | * negatives from the RCU list walk here, unlike the optimistic |
| 484 | * fast walk). |
| 485 | * |
| 486 | * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup |
| 487 | */ |
| 488 | result = d_lookup(parent, name); |
| 489 | if (!result) { |
| 490 | struct dentry * dentry = d_alloc(parent, name); |
| 491 | result = ERR_PTR(-ENOMEM); |
| 492 | if (dentry) { |
| 493 | result = dir->i_op->lookup(dir, dentry, nd); |
| 494 | if (result) |
| 495 | dput(dentry); |
| 496 | else |
| 497 | result = dentry; |
| 498 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 499 | mutex_unlock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | return result; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Uhhuh! Nasty case: the cache was re-populated while |
| 505 | * we waited on the semaphore. Need to revalidate. |
| 506 | */ |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 507 | mutex_unlock(&dir->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | if (result->d_op && result->d_op->d_revalidate) { |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 509 | result = do_revalidate(result, nd); |
| 510 | if (!result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | result = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | return result; |
| 514 | } |
| 515 | |
| 516 | static int __emul_lookup_dentry(const char *, struct nameidata *); |
| 517 | |
| 518 | /* SMP-safe */ |
Arjan van de Ven | f166235 | 2006-01-14 13:21:31 -0800 | [diff] [blame] | 519 | static __always_inline int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | walk_init_root(const char *name, struct nameidata *nd) |
| 521 | { |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 522 | struct fs_struct *fs = current->fs; |
| 523 | |
| 524 | read_lock(&fs->lock); |
| 525 | if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) { |
| 526 | nd->mnt = mntget(fs->altrootmnt); |
| 527 | nd->dentry = dget(fs->altroot); |
| 528 | read_unlock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (__emul_lookup_dentry(name,nd)) |
| 530 | return 0; |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 531 | read_lock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 533 | nd->mnt = mntget(fs->rootmnt); |
| 534 | nd->dentry = dget(fs->root); |
| 535 | read_unlock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | return 1; |
| 537 | } |
| 538 | |
Arjan van de Ven | f166235 | 2006-01-14 13:21:31 -0800 | [diff] [blame] | 539 | static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
| 541 | int res = 0; |
| 542 | char *name; |
| 543 | if (IS_ERR(link)) |
| 544 | goto fail; |
| 545 | |
| 546 | if (*link == '/') { |
| 547 | path_release(nd); |
| 548 | if (!walk_init_root(link, nd)) |
| 549 | /* weird __emul_prefix() stuff did it */ |
| 550 | goto out; |
| 551 | } |
| 552 | res = link_path_walk(link, nd); |
| 553 | out: |
| 554 | if (nd->depth || res || nd->last_type!=LAST_NORM) |
| 555 | return res; |
| 556 | /* |
| 557 | * If it is an iterative symlinks resolution in open_namei() we |
| 558 | * have to copy the last component. And all that crap because of |
| 559 | * bloody create() on broken symlinks. Furrfu... |
| 560 | */ |
| 561 | name = __getname(); |
| 562 | if (unlikely(!name)) { |
| 563 | path_release(nd); |
| 564 | return -ENOMEM; |
| 565 | } |
| 566 | strcpy(name, nd->last.name); |
| 567 | nd->last.name = name; |
| 568 | return 0; |
| 569 | fail: |
| 570 | path_release(nd); |
| 571 | return PTR_ERR(link); |
| 572 | } |
| 573 | |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 574 | static inline void dput_path(struct path *path, struct nameidata *nd) |
| 575 | { |
| 576 | dput(path->dentry); |
| 577 | if (path->mnt != nd->mnt) |
| 578 | mntput(path->mnt); |
| 579 | } |
| 580 | |
| 581 | static inline void path_to_nameidata(struct path *path, struct nameidata *nd) |
| 582 | { |
| 583 | dput(nd->dentry); |
| 584 | if (nd->mnt != path->mnt) |
| 585 | mntput(nd->mnt); |
| 586 | nd->mnt = path->mnt; |
| 587 | nd->dentry = path->dentry; |
| 588 | } |
| 589 | |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 590 | static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd) |
| 591 | { |
| 592 | int error; |
| 593 | void *cookie; |
| 594 | struct dentry *dentry = path->dentry; |
| 595 | |
| 596 | touch_atime(path->mnt, dentry); |
| 597 | nd_set_link(nd, NULL); |
| 598 | |
| 599 | if (path->mnt != nd->mnt) { |
| 600 | path_to_nameidata(path, nd); |
| 601 | dget(dentry); |
| 602 | } |
| 603 | mntget(path->mnt); |
| 604 | cookie = dentry->d_inode->i_op->follow_link(dentry, nd); |
| 605 | error = PTR_ERR(cookie); |
| 606 | if (!IS_ERR(cookie)) { |
| 607 | char *s = nd_get_link(nd); |
| 608 | error = 0; |
| 609 | if (s) |
| 610 | error = __vfs_follow_link(nd, s); |
| 611 | if (dentry->d_inode->i_op->put_link) |
| 612 | dentry->d_inode->i_op->put_link(dentry, nd, cookie); |
| 613 | } |
| 614 | dput(dentry); |
| 615 | mntput(path->mnt); |
| 616 | |
| 617 | return error; |
| 618 | } |
| 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | /* |
| 621 | * This limits recursive symlink follows to 8, while |
| 622 | * limiting consecutive symlinks to 40. |
| 623 | * |
| 624 | * Without that kind of total limit, nasty chains of consecutive |
| 625 | * symlinks can cause almost arbitrarily long lookups. |
| 626 | */ |
Al Viro | 90ebe56 | 2005-06-06 13:35:58 -0700 | [diff] [blame] | 627 | static inline int do_follow_link(struct path *path, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
| 629 | int err = -ELOOP; |
| 630 | if (current->link_count >= MAX_NESTED_LINKS) |
| 631 | goto loop; |
| 632 | if (current->total_link_count >= 40) |
| 633 | goto loop; |
| 634 | BUG_ON(nd->depth >= MAX_NESTED_LINKS); |
| 635 | cond_resched(); |
Al Viro | 90ebe56 | 2005-06-06 13:35:58 -0700 | [diff] [blame] | 636 | err = security_inode_follow_link(path->dentry, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | if (err) |
| 638 | goto loop; |
| 639 | current->link_count++; |
| 640 | current->total_link_count++; |
| 641 | nd->depth++; |
Al Viro | cd4e91d | 2005-06-06 13:36:03 -0700 | [diff] [blame] | 642 | err = __do_follow_link(path, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | current->link_count--; |
| 644 | nd->depth--; |
| 645 | return err; |
| 646 | loop: |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 647 | dput_path(path, nd); |
Al Viro | 839d9f9 | 2005-06-06 13:36:02 -0700 | [diff] [blame] | 648 | path_release(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return err; |
| 650 | } |
| 651 | |
| 652 | int follow_up(struct vfsmount **mnt, struct dentry **dentry) |
| 653 | { |
| 654 | struct vfsmount *parent; |
| 655 | struct dentry *mountpoint; |
| 656 | spin_lock(&vfsmount_lock); |
| 657 | parent=(*mnt)->mnt_parent; |
| 658 | if (parent == *mnt) { |
| 659 | spin_unlock(&vfsmount_lock); |
| 660 | return 0; |
| 661 | } |
| 662 | mntget(parent); |
| 663 | mountpoint=dget((*mnt)->mnt_mountpoint); |
| 664 | spin_unlock(&vfsmount_lock); |
| 665 | dput(*dentry); |
| 666 | *dentry = mountpoint; |
| 667 | mntput(*mnt); |
| 668 | *mnt = parent; |
| 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | /* no need for dcache_lock, as serialization is taken care in |
| 673 | * namespace.c |
| 674 | */ |
Al Viro | 463ffb2 | 2005-06-06 13:36:05 -0700 | [diff] [blame] | 675 | static int __follow_mount(struct path *path) |
| 676 | { |
| 677 | int res = 0; |
| 678 | while (d_mountpoint(path->dentry)) { |
| 679 | struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry); |
| 680 | if (!mounted) |
| 681 | break; |
| 682 | dput(path->dentry); |
| 683 | if (res) |
| 684 | mntput(path->mnt); |
| 685 | path->mnt = mounted; |
| 686 | path->dentry = dget(mounted->mnt_root); |
| 687 | res = 1; |
| 688 | } |
| 689 | return res; |
| 690 | } |
| 691 | |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 692 | static void follow_mount(struct vfsmount **mnt, struct dentry **dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | while (d_mountpoint(*dentry)) { |
| 695 | struct vfsmount *mounted = lookup_mnt(*mnt, *dentry); |
| 696 | if (!mounted) |
| 697 | break; |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 698 | dput(*dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | mntput(*mnt); |
| 700 | *mnt = mounted; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | *dentry = dget(mounted->mnt_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* no need for dcache_lock, as serialization is taken care in |
| 706 | * namespace.c |
| 707 | */ |
Al Viro | e13b210 | 2005-06-06 13:36:06 -0700 | [diff] [blame] | 708 | int follow_down(struct vfsmount **mnt, struct dentry **dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
| 710 | struct vfsmount *mounted; |
| 711 | |
| 712 | mounted = lookup_mnt(*mnt, *dentry); |
| 713 | if (mounted) { |
Al Viro | e13b210 | 2005-06-06 13:36:06 -0700 | [diff] [blame] | 714 | dput(*dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | mntput(*mnt); |
| 716 | *mnt = mounted; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | *dentry = dget(mounted->mnt_root); |
| 718 | return 1; |
| 719 | } |
| 720 | return 0; |
| 721 | } |
| 722 | |
Arjan van de Ven | f166235 | 2006-01-14 13:21:31 -0800 | [diff] [blame] | 723 | static __always_inline void follow_dotdot(struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | { |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 725 | struct fs_struct *fs = current->fs; |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | while(1) { |
| 728 | struct vfsmount *parent; |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 729 | struct dentry *old = nd->dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 731 | read_lock(&fs->lock); |
| 732 | if (nd->dentry == fs->root && |
| 733 | nd->mnt == fs->rootmnt) { |
| 734 | read_unlock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | break; |
| 736 | } |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 737 | read_unlock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | spin_lock(&dcache_lock); |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 739 | if (nd->dentry != nd->mnt->mnt_root) { |
| 740 | nd->dentry = dget(nd->dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | spin_unlock(&dcache_lock); |
| 742 | dput(old); |
| 743 | break; |
| 744 | } |
| 745 | spin_unlock(&dcache_lock); |
| 746 | spin_lock(&vfsmount_lock); |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 747 | parent = nd->mnt->mnt_parent; |
| 748 | if (parent == nd->mnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | spin_unlock(&vfsmount_lock); |
| 750 | break; |
| 751 | } |
| 752 | mntget(parent); |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 753 | nd->dentry = dget(nd->mnt->mnt_mountpoint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | spin_unlock(&vfsmount_lock); |
| 755 | dput(old); |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 756 | mntput(nd->mnt); |
| 757 | nd->mnt = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 759 | follow_mount(&nd->mnt, &nd->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | /* |
| 763 | * It's more convoluted than I'd like it to be, but... it's still fairly |
| 764 | * small and for now I'd prefer to have fast path as straight as possible. |
| 765 | * It _is_ time-critical. |
| 766 | */ |
| 767 | static int do_lookup(struct nameidata *nd, struct qstr *name, |
| 768 | struct path *path) |
| 769 | { |
| 770 | struct vfsmount *mnt = nd->mnt; |
| 771 | struct dentry *dentry = __d_lookup(nd->dentry, name); |
| 772 | |
| 773 | if (!dentry) |
| 774 | goto need_lookup; |
| 775 | if (dentry->d_op && dentry->d_op->d_revalidate) |
| 776 | goto need_revalidate; |
| 777 | done: |
| 778 | path->mnt = mnt; |
| 779 | path->dentry = dentry; |
Al Viro | 634ee70 | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 780 | __follow_mount(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | return 0; |
| 782 | |
| 783 | need_lookup: |
| 784 | dentry = real_lookup(nd->dentry, name, nd); |
| 785 | if (IS_ERR(dentry)) |
| 786 | goto fail; |
| 787 | goto done; |
| 788 | |
| 789 | need_revalidate: |
Ian Kent | bcdc5e0 | 2006-09-27 01:50:44 -0700 | [diff] [blame] | 790 | dentry = do_revalidate(dentry, nd); |
| 791 | if (!dentry) |
| 792 | goto need_lookup; |
| 793 | if (IS_ERR(dentry)) |
| 794 | goto fail; |
| 795 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
| 797 | fail: |
| 798 | return PTR_ERR(dentry); |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Name resolution. |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 803 | * This is the basic name resolution function, turning a pathname into |
| 804 | * the final dentry. We expect 'base' to be positive and a directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | * |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 806 | * Returns 0 and nd will have valid dentry and mnt on success. |
| 807 | * Returns error and drops reference to input namei data on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | */ |
| 809 | static fastcall int __link_path_walk(const char * name, struct nameidata *nd) |
| 810 | { |
| 811 | struct path next; |
| 812 | struct inode *inode; |
| 813 | int err; |
| 814 | unsigned int lookup_flags = nd->flags; |
| 815 | |
| 816 | while (*name=='/') |
| 817 | name++; |
| 818 | if (!*name) |
| 819 | goto return_reval; |
| 820 | |
| 821 | inode = nd->dentry->d_inode; |
| 822 | if (nd->depth) |
Trond Myklebust | f55eab8 | 2006-02-04 23:28:01 -0800 | [diff] [blame] | 823 | lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | /* At this point we know we have a real path component. */ |
| 826 | for(;;) { |
| 827 | unsigned long hash; |
| 828 | struct qstr this; |
| 829 | unsigned int c; |
| 830 | |
Trond Myklebust | cdce5d6 | 2005-10-18 14:20:18 -0700 | [diff] [blame] | 831 | nd->flags |= LOOKUP_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | err = exec_permission_lite(inode, nd); |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 833 | if (err == -EAGAIN) |
| 834 | err = vfs_permission(nd, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (err) |
| 836 | break; |
| 837 | |
| 838 | this.name = name; |
| 839 | c = *(const unsigned char *)name; |
| 840 | |
| 841 | hash = init_name_hash(); |
| 842 | do { |
| 843 | name++; |
| 844 | hash = partial_name_hash(c, hash); |
| 845 | c = *(const unsigned char *)name; |
| 846 | } while (c && (c != '/')); |
| 847 | this.len = name - (const char *) this.name; |
| 848 | this.hash = end_name_hash(hash); |
| 849 | |
| 850 | /* remove trailing slashes? */ |
| 851 | if (!c) |
| 852 | goto last_component; |
| 853 | while (*++name == '/'); |
| 854 | if (!*name) |
| 855 | goto last_with_slashes; |
| 856 | |
| 857 | /* |
| 858 | * "." and ".." are special - ".." especially so because it has |
| 859 | * to be able to know about the current root directory and |
| 860 | * parent relationships. |
| 861 | */ |
| 862 | if (this.name[0] == '.') switch (this.len) { |
| 863 | default: |
| 864 | break; |
| 865 | case 2: |
| 866 | if (this.name[1] != '.') |
| 867 | break; |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 868 | follow_dotdot(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | inode = nd->dentry->d_inode; |
| 870 | /* fallthrough */ |
| 871 | case 1: |
| 872 | continue; |
| 873 | } |
| 874 | /* |
| 875 | * See if the low-level filesystem might want |
| 876 | * to use its own hash.. |
| 877 | */ |
| 878 | if (nd->dentry->d_op && nd->dentry->d_op->d_hash) { |
| 879 | err = nd->dentry->d_op->d_hash(nd->dentry, &this); |
| 880 | if (err < 0) |
| 881 | break; |
| 882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | /* This does the actual lookups.. */ |
| 884 | err = do_lookup(nd, &this, &next); |
| 885 | if (err) |
| 886 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
| 888 | err = -ENOENT; |
| 889 | inode = next.dentry->d_inode; |
| 890 | if (!inode) |
| 891 | goto out_dput; |
| 892 | err = -ENOTDIR; |
| 893 | if (!inode->i_op) |
| 894 | goto out_dput; |
| 895 | |
| 896 | if (inode->i_op->follow_link) { |
Al Viro | 90ebe56 | 2005-06-06 13:35:58 -0700 | [diff] [blame] | 897 | err = do_follow_link(&next, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | if (err) |
| 899 | goto return_err; |
| 900 | err = -ENOENT; |
| 901 | inode = nd->dentry->d_inode; |
| 902 | if (!inode) |
| 903 | break; |
| 904 | err = -ENOTDIR; |
| 905 | if (!inode->i_op) |
| 906 | break; |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 907 | } else |
| 908 | path_to_nameidata(&next, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | err = -ENOTDIR; |
| 910 | if (!inode->i_op->lookup) |
| 911 | break; |
| 912 | continue; |
| 913 | /* here ends the main loop */ |
| 914 | |
| 915 | last_with_slashes: |
| 916 | lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 917 | last_component: |
Trond Myklebust | f55eab8 | 2006-02-04 23:28:01 -0800 | [diff] [blame] | 918 | /* Clear LOOKUP_CONTINUE iff it was previously unset */ |
| 919 | nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | if (lookup_flags & LOOKUP_PARENT) |
| 921 | goto lookup_parent; |
| 922 | if (this.name[0] == '.') switch (this.len) { |
| 923 | default: |
| 924 | break; |
| 925 | case 2: |
| 926 | if (this.name[1] != '.') |
| 927 | break; |
Al Viro | 58c465e | 2005-06-06 13:36:13 -0700 | [diff] [blame] | 928 | follow_dotdot(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | inode = nd->dentry->d_inode; |
| 930 | /* fallthrough */ |
| 931 | case 1: |
| 932 | goto return_reval; |
| 933 | } |
| 934 | if (nd->dentry->d_op && nd->dentry->d_op->d_hash) { |
| 935 | err = nd->dentry->d_op->d_hash(nd->dentry, &this); |
| 936 | if (err < 0) |
| 937 | break; |
| 938 | } |
| 939 | err = do_lookup(nd, &this, &next); |
| 940 | if (err) |
| 941 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | inode = next.dentry->d_inode; |
| 943 | if ((lookup_flags & LOOKUP_FOLLOW) |
| 944 | && inode && inode->i_op && inode->i_op->follow_link) { |
Al Viro | 90ebe56 | 2005-06-06 13:35:58 -0700 | [diff] [blame] | 945 | err = do_follow_link(&next, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | if (err) |
| 947 | goto return_err; |
| 948 | inode = nd->dentry->d_inode; |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 949 | } else |
| 950 | path_to_nameidata(&next, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | err = -ENOENT; |
| 952 | if (!inode) |
| 953 | break; |
| 954 | if (lookup_flags & LOOKUP_DIRECTORY) { |
| 955 | err = -ENOTDIR; |
| 956 | if (!inode->i_op || !inode->i_op->lookup) |
| 957 | break; |
| 958 | } |
| 959 | goto return_base; |
| 960 | lookup_parent: |
| 961 | nd->last = this; |
| 962 | nd->last_type = LAST_NORM; |
| 963 | if (this.name[0] != '.') |
| 964 | goto return_base; |
| 965 | if (this.len == 1) |
| 966 | nd->last_type = LAST_DOT; |
| 967 | else if (this.len == 2 && this.name[1] == '.') |
| 968 | nd->last_type = LAST_DOTDOT; |
| 969 | else |
| 970 | goto return_base; |
| 971 | return_reval: |
| 972 | /* |
| 973 | * We bypassed the ordinary revalidation routines. |
| 974 | * We may need to check the cached dentry for staleness. |
| 975 | */ |
| 976 | if (nd->dentry && nd->dentry->d_sb && |
| 977 | (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { |
| 978 | err = -ESTALE; |
| 979 | /* Note: we do not d_invalidate() */ |
| 980 | if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd)) |
| 981 | break; |
| 982 | } |
| 983 | return_base: |
| 984 | return 0; |
| 985 | out_dput: |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 986 | dput_path(&next, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | break; |
| 988 | } |
| 989 | path_release(nd); |
| 990 | return_err: |
| 991 | return err; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * Wrapper to retry pathname resolution whenever the underlying |
| 996 | * file system returns an ESTALE. |
| 997 | * |
| 998 | * Retry the whole path once, forcing real lookup requests |
| 999 | * instead of relying on the dcache. |
| 1000 | */ |
| 1001 | int fastcall link_path_walk(const char *name, struct nameidata *nd) |
| 1002 | { |
| 1003 | struct nameidata save = *nd; |
| 1004 | int result; |
| 1005 | |
| 1006 | /* make sure the stuff we saved doesn't go away */ |
| 1007 | dget(save.dentry); |
| 1008 | mntget(save.mnt); |
| 1009 | |
| 1010 | result = __link_path_walk(name, nd); |
| 1011 | if (result == -ESTALE) { |
| 1012 | *nd = save; |
| 1013 | dget(nd->dentry); |
| 1014 | mntget(nd->mnt); |
| 1015 | nd->flags |= LOOKUP_REVAL; |
| 1016 | result = __link_path_walk(name, nd); |
| 1017 | } |
| 1018 | |
| 1019 | dput(save.dentry); |
| 1020 | mntput(save.mnt); |
| 1021 | |
| 1022 | return result; |
| 1023 | } |
| 1024 | |
| 1025 | int fastcall path_walk(const char * name, struct nameidata *nd) |
| 1026 | { |
| 1027 | current->total_link_count = 0; |
| 1028 | return link_path_walk(name, nd); |
| 1029 | } |
| 1030 | |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1031 | /* |
| 1032 | * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if |
| 1033 | * everything is done. Returns 0 and drops input nd, if lookup failed; |
| 1034 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | static int __emul_lookup_dentry(const char *name, struct nameidata *nd) |
| 1036 | { |
| 1037 | if (path_walk(name, nd)) |
| 1038 | return 0; /* something went wrong... */ |
| 1039 | |
| 1040 | if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) { |
| 1041 | struct dentry *old_dentry = nd->dentry; |
| 1042 | struct vfsmount *old_mnt = nd->mnt; |
| 1043 | struct qstr last = nd->last; |
| 1044 | int last_type = nd->last_type; |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1045 | struct fs_struct *fs = current->fs; |
| 1046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | /* |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1048 | * NAME was not found in alternate root or it's a directory. |
| 1049 | * Try to find it in the normal root: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | */ |
| 1051 | nd->last_type = LAST_ROOT; |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1052 | read_lock(&fs->lock); |
| 1053 | nd->mnt = mntget(fs->rootmnt); |
| 1054 | nd->dentry = dget(fs->root); |
| 1055 | read_unlock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | if (path_walk(name, nd) == 0) { |
| 1057 | if (nd->dentry->d_inode) { |
| 1058 | dput(old_dentry); |
| 1059 | mntput(old_mnt); |
| 1060 | return 1; |
| 1061 | } |
| 1062 | path_release(nd); |
| 1063 | } |
| 1064 | nd->dentry = old_dentry; |
| 1065 | nd->mnt = old_mnt; |
| 1066 | nd->last = last; |
| 1067 | nd->last_type = last_type; |
| 1068 | } |
| 1069 | return 1; |
| 1070 | } |
| 1071 | |
| 1072 | void set_fs_altroot(void) |
| 1073 | { |
| 1074 | char *emul = __emul_prefix(); |
| 1075 | struct nameidata nd; |
| 1076 | struct vfsmount *mnt = NULL, *oldmnt; |
| 1077 | struct dentry *dentry = NULL, *olddentry; |
| 1078 | int err; |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1079 | struct fs_struct *fs = current->fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | if (!emul) |
| 1082 | goto set_it; |
| 1083 | err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd); |
| 1084 | if (!err) { |
| 1085 | mnt = nd.mnt; |
| 1086 | dentry = nd.dentry; |
| 1087 | } |
| 1088 | set_it: |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1089 | write_lock(&fs->lock); |
| 1090 | oldmnt = fs->altrootmnt; |
| 1091 | olddentry = fs->altroot; |
| 1092 | fs->altrootmnt = mnt; |
| 1093 | fs->altroot = dentry; |
| 1094 | write_unlock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | if (olddentry) { |
| 1096 | dput(olddentry); |
| 1097 | mntput(oldmnt); |
| 1098 | } |
| 1099 | } |
| 1100 | |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1101 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1102 | static int fastcall do_path_lookup(int dfd, const char *name, |
| 1103 | unsigned int flags, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | { |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1105 | int retval = 0; |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1106 | int fput_needed; |
| 1107 | struct file *file; |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1108 | struct fs_struct *fs = current->fs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
| 1110 | nd->last_type = LAST_ROOT; /* if there are only slashes... */ |
| 1111 | nd->flags = flags; |
| 1112 | nd->depth = 0; |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | if (*name=='/') { |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1115 | read_lock(&fs->lock); |
| 1116 | if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) { |
| 1117 | nd->mnt = mntget(fs->altrootmnt); |
| 1118 | nd->dentry = dget(fs->altroot); |
| 1119 | read_unlock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | if (__emul_lookup_dentry(name,nd)) |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1121 | goto out; /* found in altroot */ |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1122 | read_lock(&fs->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1124 | nd->mnt = mntget(fs->rootmnt); |
| 1125 | nd->dentry = dget(fs->root); |
| 1126 | read_unlock(&fs->lock); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1127 | } else if (dfd == AT_FDCWD) { |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1128 | read_lock(&fs->lock); |
| 1129 | nd->mnt = mntget(fs->pwdmnt); |
| 1130 | nd->dentry = dget(fs->pwd); |
| 1131 | read_unlock(&fs->lock); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1132 | } else { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1133 | struct dentry *dentry; |
| 1134 | |
| 1135 | file = fget_light(dfd, &fput_needed); |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1136 | retval = -EBADF; |
| 1137 | if (!file) |
Trond Myklebust | 6d09bb6 | 2006-06-04 02:51:37 -0700 | [diff] [blame] | 1138 | goto out_fail; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1139 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1140 | dentry = file->f_path.dentry; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1141 | |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1142 | retval = -ENOTDIR; |
| 1143 | if (!S_ISDIR(dentry->d_inode->i_mode)) |
Trond Myklebust | 6d09bb6 | 2006-06-04 02:51:37 -0700 | [diff] [blame] | 1144 | goto fput_fail; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1145 | |
| 1146 | retval = file_permission(file, MAY_EXEC); |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1147 | if (retval) |
Trond Myklebust | 6d09bb6 | 2006-06-04 02:51:37 -0700 | [diff] [blame] | 1148 | goto fput_fail; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1149 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1150 | nd->mnt = mntget(file->f_path.mnt); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1151 | nd->dentry = dget(dentry); |
| 1152 | |
| 1153 | fput_light(file, fput_needed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | } |
Josef 'Jeff' Sipek | 2dfdd26 | 2007-05-09 02:33:41 -0700 | [diff] [blame] | 1155 | |
| 1156 | retval = path_walk(name, nd); |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1157 | out: |
Josef 'Jeff' Sipek | 62ce39c | 2007-05-09 02:33:41 -0700 | [diff] [blame] | 1158 | if (unlikely(!retval && !audit_dummy_context() && nd->dentry && |
Suzuki | 3bc8414b | 2006-02-07 12:58:36 -0800 | [diff] [blame] | 1159 | nd->dentry->d_inode)) |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1160 | audit_inode(name, nd->dentry->d_inode); |
Trond Myklebust | 6d09bb6 | 2006-06-04 02:51:37 -0700 | [diff] [blame] | 1161 | out_fail: |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1162 | return retval; |
| 1163 | |
Trond Myklebust | 6d09bb6 | 2006-06-04 02:51:37 -0700 | [diff] [blame] | 1164 | fput_fail: |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1165 | fput_light(file, fput_needed); |
Trond Myklebust | 6d09bb6 | 2006-06-04 02:51:37 -0700 | [diff] [blame] | 1166 | goto out_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1169 | int fastcall path_lookup(const char *name, unsigned int flags, |
| 1170 | struct nameidata *nd) |
| 1171 | { |
| 1172 | return do_path_lookup(AT_FDCWD, name, flags, nd); |
| 1173 | } |
| 1174 | |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame^] | 1175 | /** |
| 1176 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair |
| 1177 | * @dentry: pointer to dentry of the base directory |
| 1178 | * @mnt: pointer to vfs mount of the base directory |
| 1179 | * @name: pointer to file name |
| 1180 | * @flags: lookup flags |
| 1181 | * @nd: pointer to nameidata |
| 1182 | */ |
| 1183 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, |
| 1184 | const char *name, unsigned int flags, |
| 1185 | struct nameidata *nd) |
| 1186 | { |
| 1187 | int retval; |
| 1188 | |
| 1189 | /* same as do_path_lookup */ |
| 1190 | nd->last_type = LAST_ROOT; |
| 1191 | nd->flags = flags; |
| 1192 | nd->depth = 0; |
| 1193 | |
| 1194 | nd->mnt = mntget(mnt); |
| 1195 | nd->dentry = dget(dentry); |
| 1196 | |
| 1197 | retval = path_walk(name, nd); |
| 1198 | if (unlikely(!retval && !audit_dummy_context() && nd->dentry && |
| 1199 | nd->dentry->d_inode)) |
| 1200 | audit_inode(name, nd->dentry->d_inode); |
| 1201 | |
| 1202 | return retval; |
| 1203 | |
| 1204 | } |
| 1205 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1206 | static int __path_lookup_intent_open(int dfd, const char *name, |
| 1207 | unsigned int lookup_flags, struct nameidata *nd, |
| 1208 | int open_flags, int create_mode) |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1209 | { |
| 1210 | struct file *filp = get_empty_filp(); |
| 1211 | int err; |
| 1212 | |
| 1213 | if (filp == NULL) |
| 1214 | return -ENFILE; |
| 1215 | nd->intent.open.file = filp; |
| 1216 | nd->intent.open.flags = open_flags; |
| 1217 | nd->intent.open.create_mode = create_mode; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1218 | err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd); |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1219 | if (IS_ERR(nd->intent.open.file)) { |
| 1220 | if (err == 0) { |
| 1221 | err = PTR_ERR(nd->intent.open.file); |
| 1222 | path_release(nd); |
| 1223 | } |
| 1224 | } else if (err != 0) |
| 1225 | release_open_intent(nd); |
| 1226 | return err; |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * path_lookup_open - lookup a file path with open intent |
Martin Waitz | 7045f37 | 2006-02-01 03:06:57 -0800 | [diff] [blame] | 1231 | * @dfd: the directory to use as base, or AT_FDCWD |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1232 | * @name: pointer to file name |
| 1233 | * @lookup_flags: lookup intent flags |
| 1234 | * @nd: pointer to nameidata |
| 1235 | * @open_flags: open intent flags |
| 1236 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1237 | int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags, |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1238 | struct nameidata *nd, int open_flags) |
| 1239 | { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1240 | return __path_lookup_intent_open(dfd, name, lookup_flags, nd, |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1241 | open_flags, 0); |
| 1242 | } |
| 1243 | |
| 1244 | /** |
| 1245 | * path_lookup_create - lookup a file path with open + create intent |
Martin Waitz | 7045f37 | 2006-02-01 03:06:57 -0800 | [diff] [blame] | 1246 | * @dfd: the directory to use as base, or AT_FDCWD |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1247 | * @name: pointer to file name |
| 1248 | * @lookup_flags: lookup intent flags |
| 1249 | * @nd: pointer to nameidata |
| 1250 | * @open_flags: open intent flags |
| 1251 | * @create_mode: create intent flags |
| 1252 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1253 | static int path_lookup_create(int dfd, const char *name, |
| 1254 | unsigned int lookup_flags, struct nameidata *nd, |
| 1255 | int open_flags, int create_mode) |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1256 | { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1257 | return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE, |
| 1258 | nd, open_flags, create_mode); |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags, |
| 1262 | struct nameidata *nd, int open_flags) |
| 1263 | { |
| 1264 | char *tmp = getname(name); |
| 1265 | int err = PTR_ERR(tmp); |
| 1266 | |
| 1267 | if (!IS_ERR(tmp)) { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1268 | err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0); |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1269 | putname(tmp); |
| 1270 | } |
| 1271 | return err; |
| 1272 | } |
| 1273 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1274 | static inline struct dentry *__lookup_hash_kern(struct qstr *name, struct dentry *base, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | { |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1276 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | struct inode *inode; |
| 1278 | int err; |
| 1279 | |
| 1280 | inode = base->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
| 1282 | /* |
| 1283 | * See if the low-level filesystem might want |
| 1284 | * to use its own hash.. |
| 1285 | */ |
| 1286 | if (base->d_op && base->d_op->d_hash) { |
| 1287 | err = base->d_op->d_hash(base, name); |
| 1288 | dentry = ERR_PTR(err); |
| 1289 | if (err < 0) |
| 1290 | goto out; |
| 1291 | } |
| 1292 | |
| 1293 | dentry = cached_lookup(base, name, nd); |
| 1294 | if (!dentry) { |
| 1295 | struct dentry *new = d_alloc(base, name); |
| 1296 | dentry = ERR_PTR(-ENOMEM); |
| 1297 | if (!new) |
| 1298 | goto out; |
| 1299 | dentry = inode->i_op->lookup(inode, new, nd); |
| 1300 | if (!dentry) |
| 1301 | dentry = new; |
| 1302 | else |
| 1303 | dput(new); |
| 1304 | } |
| 1305 | out: |
| 1306 | return dentry; |
| 1307 | } |
| 1308 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1309 | /* |
| 1310 | * Restricted form of lookup. Doesn't follow links, single-component only, |
| 1311 | * needs parent already locked. Doesn't follow mounts. |
| 1312 | * SMP-safe. |
| 1313 | */ |
| 1314 | static inline struct dentry * __lookup_hash(struct qstr *name, struct dentry *base, struct nameidata *nd) |
| 1315 | { |
| 1316 | struct dentry *dentry; |
| 1317 | struct inode *inode; |
| 1318 | int err; |
| 1319 | |
| 1320 | inode = base->d_inode; |
| 1321 | |
| 1322 | err = permission(inode, MAY_EXEC, nd); |
| 1323 | dentry = ERR_PTR(err); |
| 1324 | if (err) |
| 1325 | goto out; |
| 1326 | |
| 1327 | dentry = __lookup_hash_kern(name, base, nd); |
| 1328 | out: |
| 1329 | return dentry; |
| 1330 | } |
| 1331 | |
Adrian Bunk | a244e16 | 2006-03-31 02:32:11 -0800 | [diff] [blame] | 1332 | static struct dentry *lookup_hash(struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | { |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 1334 | return __lookup_hash(&nd->last, nd->dentry, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* SMP-safe */ |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1338 | static inline int __lookup_one_len(const char *name, struct qstr *this, struct dentry *base, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | { |
| 1340 | unsigned long hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | unsigned int c; |
| 1342 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1343 | this->name = name; |
| 1344 | this->len = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | if (!len) |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1346 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
| 1348 | hash = init_name_hash(); |
| 1349 | while (len--) { |
| 1350 | c = *(const unsigned char *)name++; |
| 1351 | if (c == '/' || c == '\0') |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1352 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | hash = partial_name_hash(c, hash); |
| 1354 | } |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1355 | this->hash = end_name_hash(hash); |
| 1356 | return 0; |
| 1357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1359 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
| 1360 | { |
| 1361 | int err; |
| 1362 | struct qstr this; |
| 1363 | |
| 1364 | err = __lookup_one_len(name, &this, base, len); |
| 1365 | if (err) |
| 1366 | return ERR_PTR(err); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 1367 | return __lookup_hash(&this, base, NULL); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | struct dentry *lookup_one_len_kern(const char *name, struct dentry *base, int len) |
| 1371 | { |
| 1372 | int err; |
| 1373 | struct qstr this; |
| 1374 | |
| 1375 | err = __lookup_one_len(name, &this, base, len); |
| 1376 | if (err) |
| 1377 | return ERR_PTR(err); |
| 1378 | return __lookup_hash_kern(&this, base, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1381 | int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags, |
| 1382 | struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | { |
| 1384 | char *tmp = getname(name); |
| 1385 | int err = PTR_ERR(tmp); |
| 1386 | |
| 1387 | if (!IS_ERR(tmp)) { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1388 | err = do_path_lookup(dfd, tmp, flags, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | putname(tmp); |
| 1390 | } |
| 1391 | return err; |
| 1392 | } |
| 1393 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1394 | int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd) |
| 1395 | { |
| 1396 | return __user_walk_fd(AT_FDCWD, name, flags, nd); |
| 1397 | } |
| 1398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | /* |
| 1400 | * It's inline, so penalty for filesystems that don't use sticky bit is |
| 1401 | * minimal. |
| 1402 | */ |
| 1403 | static inline int check_sticky(struct inode *dir, struct inode *inode) |
| 1404 | { |
| 1405 | if (!(dir->i_mode & S_ISVTX)) |
| 1406 | return 0; |
| 1407 | if (inode->i_uid == current->fsuid) |
| 1408 | return 0; |
| 1409 | if (dir->i_uid == current->fsuid) |
| 1410 | return 0; |
| 1411 | return !capable(CAP_FOWNER); |
| 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * Check whether we can remove a link victim from directory dir, check |
| 1416 | * whether the type of victim is right. |
| 1417 | * 1. We can't do it if dir is read-only (done in permission()) |
| 1418 | * 2. We should have write and exec permissions on dir |
| 1419 | * 3. We can't remove anything from append-only dir |
| 1420 | * 4. We can't do anything with immutable dir (done in permission()) |
| 1421 | * 5. If the sticky bit on dir is set we should either |
| 1422 | * a. be owner of dir, or |
| 1423 | * b. be owner of victim, or |
| 1424 | * c. have CAP_FOWNER capability |
| 1425 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 1426 | * links pointing to it. |
| 1427 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 1428 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 1429 | * 9. We can't remove a root or mountpoint. |
| 1430 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 1431 | * nfs_async_unlink(). |
| 1432 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1433 | static int may_delete(struct inode *dir,struct dentry *victim,int isdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | { |
| 1435 | int error; |
| 1436 | |
| 1437 | if (!victim->d_inode) |
| 1438 | return -ENOENT; |
| 1439 | |
| 1440 | BUG_ON(victim->d_parent->d_inode != dir); |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1441 | audit_inode_child(victim->d_name.name, victim->d_inode, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
| 1443 | error = permission(dir,MAY_WRITE | MAY_EXEC, NULL); |
| 1444 | if (error) |
| 1445 | return error; |
| 1446 | if (IS_APPEND(dir)) |
| 1447 | return -EPERM; |
| 1448 | if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| |
| 1449 | IS_IMMUTABLE(victim->d_inode)) |
| 1450 | return -EPERM; |
| 1451 | if (isdir) { |
| 1452 | if (!S_ISDIR(victim->d_inode->i_mode)) |
| 1453 | return -ENOTDIR; |
| 1454 | if (IS_ROOT(victim)) |
| 1455 | return -EBUSY; |
| 1456 | } else if (S_ISDIR(victim->d_inode->i_mode)) |
| 1457 | return -EISDIR; |
| 1458 | if (IS_DEADDIR(dir)) |
| 1459 | return -ENOENT; |
| 1460 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 1461 | return -EBUSY; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | /* Check whether we can create an object with dentry child in directory |
| 1466 | * dir. |
| 1467 | * 1. We can't do it if child already exists (open has special treatment for |
| 1468 | * this case, but since we are inlined it's OK) |
| 1469 | * 2. We can't do it if dir is read-only (done in permission()) |
| 1470 | * 3. We should have write and exec permissions on dir |
| 1471 | * 4. We can't do it if dir is immutable (done in permission()) |
| 1472 | */ |
| 1473 | static inline int may_create(struct inode *dir, struct dentry *child, |
| 1474 | struct nameidata *nd) |
| 1475 | { |
| 1476 | if (child->d_inode) |
| 1477 | return -EEXIST; |
| 1478 | if (IS_DEADDIR(dir)) |
| 1479 | return -ENOENT; |
| 1480 | return permission(dir,MAY_WRITE | MAY_EXEC, nd); |
| 1481 | } |
| 1482 | |
| 1483 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | * O_DIRECTORY translates into forcing a directory lookup. |
| 1485 | */ |
| 1486 | static inline int lookup_flags(unsigned int f) |
| 1487 | { |
| 1488 | unsigned long retval = LOOKUP_FOLLOW; |
| 1489 | |
| 1490 | if (f & O_NOFOLLOW) |
| 1491 | retval &= ~LOOKUP_FOLLOW; |
| 1492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | if (f & O_DIRECTORY) |
| 1494 | retval |= LOOKUP_DIRECTORY; |
| 1495 | |
| 1496 | return retval; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | * p1 and p2 should be directories on the same fs. |
| 1501 | */ |
| 1502 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) |
| 1503 | { |
| 1504 | struct dentry *p; |
| 1505 | |
| 1506 | if (p1 == p2) { |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1507 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | return NULL; |
| 1509 | } |
| 1510 | |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 1511 | mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | |
| 1513 | for (p = p1; p->d_parent != p; p = p->d_parent) { |
| 1514 | if (p->d_parent == p2) { |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1515 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); |
| 1516 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | return p; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | for (p = p2; p->d_parent != p; p = p->d_parent) { |
| 1522 | if (p->d_parent == p1) { |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1523 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
| 1524 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | return p; |
| 1526 | } |
| 1527 | } |
| 1528 | |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1529 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
| 1530 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | return NULL; |
| 1532 | } |
| 1533 | |
| 1534 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
| 1535 | { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1536 | mutex_unlock(&p1->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | if (p1 != p2) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1538 | mutex_unlock(&p2->d_inode->i_mutex); |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 1539 | mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | int vfs_create(struct inode *dir, struct dentry *dentry, int mode, |
| 1544 | struct nameidata *nd) |
| 1545 | { |
| 1546 | int error = may_create(dir, dentry, nd); |
| 1547 | |
| 1548 | if (error) |
| 1549 | return error; |
| 1550 | |
| 1551 | if (!dir->i_op || !dir->i_op->create) |
| 1552 | return -EACCES; /* shouldn't it be ENOSYS? */ |
| 1553 | mode &= S_IALLUGO; |
| 1554 | mode |= S_IFREG; |
| 1555 | error = security_inode_create(dir, dentry, mode); |
| 1556 | if (error) |
| 1557 | return error; |
| 1558 | DQUOT_INIT(dir); |
| 1559 | error = dir->i_op->create(dir, dentry, mode, nd); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 1560 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 1561 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | return error; |
| 1563 | } |
| 1564 | |
| 1565 | int may_open(struct nameidata *nd, int acc_mode, int flag) |
| 1566 | { |
| 1567 | struct dentry *dentry = nd->dentry; |
| 1568 | struct inode *inode = dentry->d_inode; |
| 1569 | int error; |
| 1570 | |
| 1571 | if (!inode) |
| 1572 | return -ENOENT; |
| 1573 | |
| 1574 | if (S_ISLNK(inode->i_mode)) |
| 1575 | return -ELOOP; |
| 1576 | |
| 1577 | if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE)) |
| 1578 | return -EISDIR; |
| 1579 | |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 1580 | error = vfs_permission(nd, acc_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | if (error) |
| 1582 | return error; |
| 1583 | |
| 1584 | /* |
| 1585 | * FIFO's, sockets and device files are special: they don't |
| 1586 | * actually live on the filesystem itself, and as such you |
| 1587 | * can write to them even if the filesystem is read-only. |
| 1588 | */ |
| 1589 | if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 1590 | flag &= ~O_TRUNC; |
| 1591 | } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) { |
| 1592 | if (nd->mnt->mnt_flags & MNT_NODEV) |
| 1593 | return -EACCES; |
| 1594 | |
| 1595 | flag &= ~O_TRUNC; |
| 1596 | } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE)) |
| 1597 | return -EROFS; |
| 1598 | /* |
| 1599 | * An append-only file must be opened in append mode for writing. |
| 1600 | */ |
| 1601 | if (IS_APPEND(inode)) { |
| 1602 | if ((flag & FMODE_WRITE) && !(flag & O_APPEND)) |
| 1603 | return -EPERM; |
| 1604 | if (flag & O_TRUNC) |
| 1605 | return -EPERM; |
| 1606 | } |
| 1607 | |
| 1608 | /* O_NOATIME can only be set by the owner or superuser */ |
| 1609 | if (flag & O_NOATIME) |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 1610 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | return -EPERM; |
| 1612 | |
| 1613 | /* |
| 1614 | * Ensure there are no outstanding leases on the file. |
| 1615 | */ |
| 1616 | error = break_lease(inode, flag); |
| 1617 | if (error) |
| 1618 | return error; |
| 1619 | |
| 1620 | if (flag & O_TRUNC) { |
| 1621 | error = get_write_access(inode); |
| 1622 | if (error) |
| 1623 | return error; |
| 1624 | |
| 1625 | /* |
| 1626 | * Refuse to truncate files with mandatory locks held on them. |
| 1627 | */ |
| 1628 | error = locks_verify_locked(inode); |
| 1629 | if (!error) { |
| 1630 | DQUOT_INIT(inode); |
| 1631 | |
NeilBrown | 4a30131 | 2006-01-08 01:02:39 -0800 | [diff] [blame] | 1632 | error = do_truncate(dentry, 0, ATTR_MTIME|ATTR_CTIME, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } |
| 1634 | put_write_access(inode); |
| 1635 | if (error) |
| 1636 | return error; |
| 1637 | } else |
| 1638 | if (flag & FMODE_WRITE) |
| 1639 | DQUOT_INIT(inode); |
| 1640 | |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
Dave Hansen | aab520e | 2006-09-30 23:29:02 -0700 | [diff] [blame] | 1644 | static int open_namei_create(struct nameidata *nd, struct path *path, |
| 1645 | int flag, int mode) |
| 1646 | { |
| 1647 | int error; |
| 1648 | struct dentry *dir = nd->dentry; |
| 1649 | |
| 1650 | if (!IS_POSIXACL(dir->d_inode)) |
| 1651 | mode &= ~current->fs->umask; |
| 1652 | error = vfs_create(dir->d_inode, path->dentry, mode, nd); |
| 1653 | mutex_unlock(&dir->d_inode->i_mutex); |
| 1654 | dput(nd->dentry); |
| 1655 | nd->dentry = path->dentry; |
| 1656 | if (error) |
| 1657 | return error; |
| 1658 | /* Don't check for write permission, don't truncate */ |
| 1659 | return may_open(nd, 0, flag & ~O_TRUNC); |
| 1660 | } |
| 1661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | /* |
| 1663 | * open_namei() |
| 1664 | * |
| 1665 | * namei for open - this is in fact almost the whole open-routine. |
| 1666 | * |
| 1667 | * Note that the low bits of "flag" aren't the same as in the open |
| 1668 | * system call - they are 00 - no permissions needed |
| 1669 | * 01 - read permission needed |
| 1670 | * 10 - write permission needed |
| 1671 | * 11 - read/write permissions needed |
| 1672 | * which is a lot more logical, and also allows the "no perm" needed |
| 1673 | * for symlinks (where the permissions are checked later). |
| 1674 | * SMP-safe |
| 1675 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1676 | int open_namei(int dfd, const char *pathname, int flag, |
| 1677 | int mode, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | { |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1679 | int acc_mode, error; |
Al Viro | 4e7506e | 2005-06-06 13:36:00 -0700 | [diff] [blame] | 1680 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | struct dentry *dir; |
| 1682 | int count = 0; |
| 1683 | |
| 1684 | acc_mode = ACC_MODE(flag); |
| 1685 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1686 | /* O_TRUNC implies we need access checks for write permissions */ |
| 1687 | if (flag & O_TRUNC) |
| 1688 | acc_mode |= MAY_WRITE; |
| 1689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | /* Allow the LSM permission hook to distinguish append |
| 1691 | access from general write access. */ |
| 1692 | if (flag & O_APPEND) |
| 1693 | acc_mode |= MAY_APPEND; |
| 1694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | /* |
| 1696 | * The simplest case - just a plain lookup. |
| 1697 | */ |
| 1698 | if (!(flag & O_CREAT)) { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1699 | error = path_lookup_open(dfd, pathname, lookup_flags(flag), |
| 1700 | nd, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | if (error) |
| 1702 | return error; |
| 1703 | goto ok; |
| 1704 | } |
| 1705 | |
| 1706 | /* |
| 1707 | * Create - we need to know the parent. |
| 1708 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1709 | error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | if (error) |
| 1711 | return error; |
| 1712 | |
| 1713 | /* |
| 1714 | * We have the parent and last component. First of all, check |
| 1715 | * that we are not asked to creat(2) an obvious directory - that |
| 1716 | * will not do. |
| 1717 | */ |
| 1718 | error = -EISDIR; |
| 1719 | if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len]) |
| 1720 | goto exit; |
| 1721 | |
| 1722 | dir = nd->dentry; |
| 1723 | nd->flags &= ~LOOKUP_PARENT; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1724 | mutex_lock(&dir->d_inode->i_mutex); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 1725 | path.dentry = lookup_hash(nd); |
Al Viro | d73ffe1 | 2005-06-06 13:36:01 -0700 | [diff] [blame] | 1726 | path.mnt = nd->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | |
| 1728 | do_last: |
Al Viro | 4e7506e | 2005-06-06 13:36:00 -0700 | [diff] [blame] | 1729 | error = PTR_ERR(path.dentry); |
| 1730 | if (IS_ERR(path.dentry)) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1731 | mutex_unlock(&dir->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | goto exit; |
| 1733 | } |
| 1734 | |
Oleg Drokin | 4af4c52 | 2006-03-25 03:06:54 -0800 | [diff] [blame] | 1735 | if (IS_ERR(nd->intent.open.file)) { |
| 1736 | mutex_unlock(&dir->d_inode->i_mutex); |
| 1737 | error = PTR_ERR(nd->intent.open.file); |
| 1738 | goto exit_dput; |
| 1739 | } |
| 1740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | /* Negative dentry, just create the file */ |
Al Viro | 4e7506e | 2005-06-06 13:36:00 -0700 | [diff] [blame] | 1742 | if (!path.dentry->d_inode) { |
Dave Hansen | aab520e | 2006-09-30 23:29:02 -0700 | [diff] [blame] | 1743 | error = open_namei_create(nd, &path, flag, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | if (error) |
| 1745 | goto exit; |
Dave Hansen | aab520e | 2006-09-30 23:29:02 -0700 | [diff] [blame] | 1746 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | /* |
| 1750 | * It already exists. |
| 1751 | */ |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1752 | mutex_unlock(&dir->d_inode->i_mutex); |
Amy Griffis | 4fc03b9 | 2007-02-13 14:15:01 -0500 | [diff] [blame] | 1753 | audit_inode(pathname, path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
| 1755 | error = -EEXIST; |
| 1756 | if (flag & O_EXCL) |
| 1757 | goto exit_dput; |
| 1758 | |
Al Viro | e13b210 | 2005-06-06 13:36:06 -0700 | [diff] [blame] | 1759 | if (__follow_mount(&path)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | error = -ELOOP; |
Al Viro | ba7a4c1 | 2005-06-06 13:36:08 -0700 | [diff] [blame] | 1761 | if (flag & O_NOFOLLOW) |
| 1762 | goto exit_dput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | error = -ENOENT; |
Al Viro | 4e7506e | 2005-06-06 13:36:00 -0700 | [diff] [blame] | 1766 | if (!path.dentry->d_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | goto exit_dput; |
Al Viro | 4e7506e | 2005-06-06 13:36:00 -0700 | [diff] [blame] | 1768 | if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | goto do_link; |
| 1770 | |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 1771 | path_to_nameidata(&path, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | error = -EISDIR; |
Al Viro | 4e7506e | 2005-06-06 13:36:00 -0700 | [diff] [blame] | 1773 | if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | goto exit; |
| 1775 | ok: |
| 1776 | error = may_open(nd, acc_mode, flag); |
| 1777 | if (error) |
| 1778 | goto exit; |
| 1779 | return 0; |
| 1780 | |
| 1781 | exit_dput: |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 1782 | dput_path(&path, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | exit: |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 1784 | if (!IS_ERR(nd->intent.open.file)) |
| 1785 | release_open_intent(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | path_release(nd); |
| 1787 | return error; |
| 1788 | |
| 1789 | do_link: |
| 1790 | error = -ELOOP; |
| 1791 | if (flag & O_NOFOLLOW) |
| 1792 | goto exit_dput; |
| 1793 | /* |
| 1794 | * This is subtle. Instead of calling do_follow_link() we do the |
| 1795 | * thing by hands. The reason is that this way we have zero link_count |
| 1796 | * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT. |
| 1797 | * After that we have the parent and last component, i.e. |
| 1798 | * we are in the same situation as after the first path_walk(). |
| 1799 | * Well, almost - if the last component is normal we get its copy |
| 1800 | * stored in nd->last.name and we will have to putname() it when we |
| 1801 | * are done. Procfs-like symlinks just set LAST_BIND. |
| 1802 | */ |
| 1803 | nd->flags |= LOOKUP_PARENT; |
Al Viro | 4e7506e | 2005-06-06 13:36:00 -0700 | [diff] [blame] | 1804 | error = security_inode_follow_link(path.dentry, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | if (error) |
| 1806 | goto exit_dput; |
Al Viro | cd4e91d | 2005-06-06 13:36:03 -0700 | [diff] [blame] | 1807 | error = __do_follow_link(&path, nd); |
Kirill Korotaev | de45921 | 2006-07-14 00:23:49 -0700 | [diff] [blame] | 1808 | if (error) { |
| 1809 | /* Does someone understand code flow here? Or it is only |
| 1810 | * me so stupid? Anathema to whoever designed this non-sense |
| 1811 | * with "intent.open". |
| 1812 | */ |
| 1813 | release_open_intent(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | return error; |
Kirill Korotaev | de45921 | 2006-07-14 00:23:49 -0700 | [diff] [blame] | 1815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | nd->flags &= ~LOOKUP_PARENT; |
Al Viro | d671d5e | 2005-06-06 13:36:04 -0700 | [diff] [blame] | 1817 | if (nd->last_type == LAST_BIND) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | goto ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | error = -EISDIR; |
| 1820 | if (nd->last_type != LAST_NORM) |
| 1821 | goto exit; |
| 1822 | if (nd->last.name[nd->last.len]) { |
Linus Torvalds | 8298411 | 2005-10-06 21:54:21 -0700 | [diff] [blame] | 1823 | __putname(nd->last.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | goto exit; |
| 1825 | } |
| 1826 | error = -ELOOP; |
| 1827 | if (count++==32) { |
Linus Torvalds | 8298411 | 2005-10-06 21:54:21 -0700 | [diff] [blame] | 1828 | __putname(nd->last.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | goto exit; |
| 1830 | } |
| 1831 | dir = nd->dentry; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1832 | mutex_lock(&dir->d_inode->i_mutex); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 1833 | path.dentry = lookup_hash(nd); |
Al Viro | d671d5e | 2005-06-06 13:36:04 -0700 | [diff] [blame] | 1834 | path.mnt = nd->mnt; |
Linus Torvalds | 8298411 | 2005-10-06 21:54:21 -0700 | [diff] [blame] | 1835 | __putname(nd->last.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | goto do_last; |
| 1837 | } |
| 1838 | |
| 1839 | /** |
| 1840 | * lookup_create - lookup a dentry, creating it if it doesn't exist |
| 1841 | * @nd: nameidata info |
| 1842 | * @is_dir: directory flag |
| 1843 | * |
| 1844 | * Simple function to lookup and return a dentry and create it |
| 1845 | * if it doesn't exist. Is SMP-safe. |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 1846 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1847 | * Returns with nd->dentry->d_inode->i_mutex locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | */ |
| 1849 | struct dentry *lookup_create(struct nameidata *nd, int is_dir) |
| 1850 | { |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 1851 | struct dentry *dentry = ERR_PTR(-EEXIST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1853 | mutex_lock_nested(&nd->dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 1854 | /* |
| 1855 | * Yucky last component or no last component at all? |
| 1856 | * (foo/., foo/.., /////) |
| 1857 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | if (nd->last_type != LAST_NORM) |
| 1859 | goto fail; |
| 1860 | nd->flags &= ~LOOKUP_PARENT; |
ASANO Masahiro | a634904 | 2006-08-22 20:06:02 -0400 | [diff] [blame] | 1861 | nd->flags |= LOOKUP_CREATE; |
| 1862 | nd->intent.open.flags = O_EXCL; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 1863 | |
| 1864 | /* |
| 1865 | * Do the final lookup. |
| 1866 | */ |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 1867 | dentry = lookup_hash(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | if (IS_ERR(dentry)) |
| 1869 | goto fail; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 1870 | |
| 1871 | /* |
| 1872 | * Special case - lookup gave negative, but... we had foo/bar/ |
| 1873 | * From the vfs_mknod() POV we just have a negative dentry - |
| 1874 | * all is fine. Let's be bastards - you had / on the end, you've |
| 1875 | * been asking for (non-existent) directory. -ENOENT for you. |
| 1876 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode) |
| 1878 | goto enoent; |
| 1879 | return dentry; |
| 1880 | enoent: |
| 1881 | dput(dentry); |
| 1882 | dentry = ERR_PTR(-ENOENT); |
| 1883 | fail: |
| 1884 | return dentry; |
| 1885 | } |
Christoph Hellwig | f81a0bf | 2005-05-19 12:26:43 -0700 | [diff] [blame] | 1886 | EXPORT_SYMBOL_GPL(lookup_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | |
| 1888 | int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) |
| 1889 | { |
| 1890 | int error = may_create(dir, dentry, NULL); |
| 1891 | |
| 1892 | if (error) |
| 1893 | return error; |
| 1894 | |
| 1895 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) |
| 1896 | return -EPERM; |
| 1897 | |
| 1898 | if (!dir->i_op || !dir->i_op->mknod) |
| 1899 | return -EPERM; |
| 1900 | |
| 1901 | error = security_inode_mknod(dir, dentry, mode, dev); |
| 1902 | if (error) |
| 1903 | return error; |
| 1904 | |
| 1905 | DQUOT_INIT(dir); |
| 1906 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 1907 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 1908 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | return error; |
| 1910 | } |
| 1911 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1912 | asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode, |
| 1913 | unsigned dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | { |
| 1915 | int error = 0; |
| 1916 | char * tmp; |
| 1917 | struct dentry * dentry; |
| 1918 | struct nameidata nd; |
| 1919 | |
| 1920 | if (S_ISDIR(mode)) |
| 1921 | return -EPERM; |
| 1922 | tmp = getname(filename); |
| 1923 | if (IS_ERR(tmp)) |
| 1924 | return PTR_ERR(tmp); |
| 1925 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1926 | error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | if (error) |
| 1928 | goto out; |
| 1929 | dentry = lookup_create(&nd, 0); |
| 1930 | error = PTR_ERR(dentry); |
| 1931 | |
| 1932 | if (!IS_POSIXACL(nd.dentry->d_inode)) |
| 1933 | mode &= ~current->fs->umask; |
| 1934 | if (!IS_ERR(dentry)) { |
| 1935 | switch (mode & S_IFMT) { |
| 1936 | case 0: case S_IFREG: |
| 1937 | error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd); |
| 1938 | break; |
| 1939 | case S_IFCHR: case S_IFBLK: |
| 1940 | error = vfs_mknod(nd.dentry->d_inode,dentry,mode, |
| 1941 | new_decode_dev(dev)); |
| 1942 | break; |
| 1943 | case S_IFIFO: case S_IFSOCK: |
| 1944 | error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0); |
| 1945 | break; |
| 1946 | case S_IFDIR: |
| 1947 | error = -EPERM; |
| 1948 | break; |
| 1949 | default: |
| 1950 | error = -EINVAL; |
| 1951 | } |
| 1952 | dput(dentry); |
| 1953 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1954 | mutex_unlock(&nd.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | path_release(&nd); |
| 1956 | out: |
| 1957 | putname(tmp); |
| 1958 | |
| 1959 | return error; |
| 1960 | } |
| 1961 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1962 | asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev) |
| 1963 | { |
| 1964 | return sys_mknodat(AT_FDCWD, filename, mode, dev); |
| 1965 | } |
| 1966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1968 | { |
| 1969 | int error = may_create(dir, dentry, NULL); |
| 1970 | |
| 1971 | if (error) |
| 1972 | return error; |
| 1973 | |
| 1974 | if (!dir->i_op || !dir->i_op->mkdir) |
| 1975 | return -EPERM; |
| 1976 | |
| 1977 | mode &= (S_IRWXUGO|S_ISVTX); |
| 1978 | error = security_inode_mkdir(dir, dentry, mode); |
| 1979 | if (error) |
| 1980 | return error; |
| 1981 | |
| 1982 | DQUOT_INIT(dir); |
| 1983 | error = dir->i_op->mkdir(dir, dentry, mode); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 1984 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 1985 | fsnotify_mkdir(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | return error; |
| 1987 | } |
| 1988 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1989 | asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | { |
| 1991 | int error = 0; |
| 1992 | char * tmp; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 1993 | struct dentry *dentry; |
| 1994 | struct nameidata nd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | |
| 1996 | tmp = getname(pathname); |
| 1997 | error = PTR_ERR(tmp); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 1998 | if (IS_ERR(tmp)) |
| 1999 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 2001 | error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd); |
| 2002 | if (error) |
| 2003 | goto out; |
| 2004 | dentry = lookup_create(&nd, 1); |
| 2005 | error = PTR_ERR(dentry); |
| 2006 | if (IS_ERR(dentry)) |
| 2007 | goto out_unlock; |
| 2008 | |
| 2009 | if (!IS_POSIXACL(nd.dentry->d_inode)) |
| 2010 | mode &= ~current->fs->umask; |
| 2011 | error = vfs_mkdir(nd.dentry->d_inode, dentry, mode); |
| 2012 | dput(dentry); |
| 2013 | out_unlock: |
| 2014 | mutex_unlock(&nd.dentry->d_inode->i_mutex); |
| 2015 | path_release(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | out: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 2017 | putname(tmp); |
| 2018 | out_err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | return error; |
| 2020 | } |
| 2021 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2022 | asmlinkage long sys_mkdir(const char __user *pathname, int mode) |
| 2023 | { |
| 2024 | return sys_mkdirat(AT_FDCWD, pathname, mode); |
| 2025 | } |
| 2026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | /* |
| 2028 | * We try to drop the dentry early: we should have |
| 2029 | * a usage count of 2 if we're the only user of this |
| 2030 | * dentry, and if that is true (possibly after pruning |
| 2031 | * the dcache), then we drop the dentry now. |
| 2032 | * |
| 2033 | * A low-level filesystem can, if it choses, legally |
| 2034 | * do a |
| 2035 | * |
| 2036 | * if (!d_unhashed(dentry)) |
| 2037 | * return -EBUSY; |
| 2038 | * |
| 2039 | * if it cannot handle the case of removing a directory |
| 2040 | * that is still in use by something else.. |
| 2041 | */ |
| 2042 | void dentry_unhash(struct dentry *dentry) |
| 2043 | { |
| 2044 | dget(dentry); |
Vasily Averin | dc16842 | 2006-12-06 20:37:07 -0800 | [diff] [blame] | 2045 | shrink_dcache_parent(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | spin_lock(&dcache_lock); |
| 2047 | spin_lock(&dentry->d_lock); |
| 2048 | if (atomic_read(&dentry->d_count) == 2) |
| 2049 | __d_drop(dentry); |
| 2050 | spin_unlock(&dentry->d_lock); |
| 2051 | spin_unlock(&dcache_lock); |
| 2052 | } |
| 2053 | |
| 2054 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 2055 | { |
| 2056 | int error = may_delete(dir, dentry, 1); |
| 2057 | |
| 2058 | if (error) |
| 2059 | return error; |
| 2060 | |
| 2061 | if (!dir->i_op || !dir->i_op->rmdir) |
| 2062 | return -EPERM; |
| 2063 | |
| 2064 | DQUOT_INIT(dir); |
| 2065 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2066 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | dentry_unhash(dentry); |
| 2068 | if (d_mountpoint(dentry)) |
| 2069 | error = -EBUSY; |
| 2070 | else { |
| 2071 | error = security_inode_rmdir(dir, dentry); |
| 2072 | if (!error) { |
| 2073 | error = dir->i_op->rmdir(dir, dentry); |
| 2074 | if (!error) |
| 2075 | dentry->d_inode->i_flags |= S_DEAD; |
| 2076 | } |
| 2077 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2078 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | d_delete(dentry); |
| 2081 | } |
| 2082 | dput(dentry); |
| 2083 | |
| 2084 | return error; |
| 2085 | } |
| 2086 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2087 | static long do_rmdir(int dfd, const char __user *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | { |
| 2089 | int error = 0; |
| 2090 | char * name; |
| 2091 | struct dentry *dentry; |
| 2092 | struct nameidata nd; |
| 2093 | |
| 2094 | name = getname(pathname); |
| 2095 | if(IS_ERR(name)) |
| 2096 | return PTR_ERR(name); |
| 2097 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2098 | error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | if (error) |
| 2100 | goto exit; |
| 2101 | |
| 2102 | switch(nd.last_type) { |
| 2103 | case LAST_DOTDOT: |
| 2104 | error = -ENOTEMPTY; |
| 2105 | goto exit1; |
| 2106 | case LAST_DOT: |
| 2107 | error = -EINVAL; |
| 2108 | goto exit1; |
| 2109 | case LAST_ROOT: |
| 2110 | error = -EBUSY; |
| 2111 | goto exit1; |
| 2112 | } |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 2113 | mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 2114 | dentry = lookup_hash(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 2116 | if (IS_ERR(dentry)) |
| 2117 | goto exit2; |
| 2118 | error = vfs_rmdir(nd.dentry->d_inode, dentry); |
| 2119 | dput(dentry); |
| 2120 | exit2: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2121 | mutex_unlock(&nd.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | exit1: |
| 2123 | path_release(&nd); |
| 2124 | exit: |
| 2125 | putname(name); |
| 2126 | return error; |
| 2127 | } |
| 2128 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2129 | asmlinkage long sys_rmdir(const char __user *pathname) |
| 2130 | { |
| 2131 | return do_rmdir(AT_FDCWD, pathname); |
| 2132 | } |
| 2133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | int vfs_unlink(struct inode *dir, struct dentry *dentry) |
| 2135 | { |
| 2136 | int error = may_delete(dir, dentry, 0); |
| 2137 | |
| 2138 | if (error) |
| 2139 | return error; |
| 2140 | |
| 2141 | if (!dir->i_op || !dir->i_op->unlink) |
| 2142 | return -EPERM; |
| 2143 | |
| 2144 | DQUOT_INIT(dir); |
| 2145 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2146 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | if (d_mountpoint(dentry)) |
| 2148 | error = -EBUSY; |
| 2149 | else { |
| 2150 | error = security_inode_unlink(dir, dentry); |
| 2151 | if (!error) |
| 2152 | error = dir->i_op->unlink(dir, dentry); |
| 2153 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2154 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
| 2156 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ |
| 2157 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { |
| 2158 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 2160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | return error; |
| 2162 | } |
| 2163 | |
| 2164 | /* |
| 2165 | * Make sure that the actual truncation of the file will occur outside its |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2166 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | * writeout happening, and we don't want to prevent access to the directory |
| 2168 | * while waiting on the I/O. |
| 2169 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2170 | static long do_unlinkat(int dfd, const char __user *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | { |
| 2172 | int error = 0; |
| 2173 | char * name; |
| 2174 | struct dentry *dentry; |
| 2175 | struct nameidata nd; |
| 2176 | struct inode *inode = NULL; |
| 2177 | |
| 2178 | name = getname(pathname); |
| 2179 | if(IS_ERR(name)) |
| 2180 | return PTR_ERR(name); |
| 2181 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2182 | error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | if (error) |
| 2184 | goto exit; |
| 2185 | error = -EISDIR; |
| 2186 | if (nd.last_type != LAST_NORM) |
| 2187 | goto exit1; |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 2188 | mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 2189 | dentry = lookup_hash(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | error = PTR_ERR(dentry); |
| 2191 | if (!IS_ERR(dentry)) { |
| 2192 | /* Why not before? Because we want correct error value */ |
| 2193 | if (nd.last.name[nd.last.len]) |
| 2194 | goto slashes; |
| 2195 | inode = dentry->d_inode; |
| 2196 | if (inode) |
| 2197 | atomic_inc(&inode->i_count); |
| 2198 | error = vfs_unlink(nd.dentry->d_inode, dentry); |
| 2199 | exit2: |
| 2200 | dput(dentry); |
| 2201 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2202 | mutex_unlock(&nd.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | if (inode) |
| 2204 | iput(inode); /* truncate the inode here */ |
| 2205 | exit1: |
| 2206 | path_release(&nd); |
| 2207 | exit: |
| 2208 | putname(name); |
| 2209 | return error; |
| 2210 | |
| 2211 | slashes: |
| 2212 | error = !dentry->d_inode ? -ENOENT : |
| 2213 | S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; |
| 2214 | goto exit2; |
| 2215 | } |
| 2216 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2217 | asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag) |
| 2218 | { |
| 2219 | if ((flag & ~AT_REMOVEDIR) != 0) |
| 2220 | return -EINVAL; |
| 2221 | |
| 2222 | if (flag & AT_REMOVEDIR) |
| 2223 | return do_rmdir(dfd, pathname); |
| 2224 | |
| 2225 | return do_unlinkat(dfd, pathname); |
| 2226 | } |
| 2227 | |
| 2228 | asmlinkage long sys_unlink(const char __user *pathname) |
| 2229 | { |
| 2230 | return do_unlinkat(AT_FDCWD, pathname); |
| 2231 | } |
| 2232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode) |
| 2234 | { |
| 2235 | int error = may_create(dir, dentry, NULL); |
| 2236 | |
| 2237 | if (error) |
| 2238 | return error; |
| 2239 | |
| 2240 | if (!dir->i_op || !dir->i_op->symlink) |
| 2241 | return -EPERM; |
| 2242 | |
| 2243 | error = security_inode_symlink(dir, dentry, oldname); |
| 2244 | if (error) |
| 2245 | return error; |
| 2246 | |
| 2247 | DQUOT_INIT(dir); |
| 2248 | error = dir->i_op->symlink(dir, dentry, oldname); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 2249 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 2250 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | return error; |
| 2252 | } |
| 2253 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2254 | asmlinkage long sys_symlinkat(const char __user *oldname, |
| 2255 | int newdfd, const char __user *newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | { |
| 2257 | int error = 0; |
| 2258 | char * from; |
| 2259 | char * to; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 2260 | struct dentry *dentry; |
| 2261 | struct nameidata nd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | |
| 2263 | from = getname(oldname); |
| 2264 | if(IS_ERR(from)) |
| 2265 | return PTR_ERR(from); |
| 2266 | to = getname(newname); |
| 2267 | error = PTR_ERR(to); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 2268 | if (IS_ERR(to)) |
| 2269 | goto out_putname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 2271 | error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd); |
| 2272 | if (error) |
| 2273 | goto out; |
| 2274 | dentry = lookup_create(&nd, 0); |
| 2275 | error = PTR_ERR(dentry); |
| 2276 | if (IS_ERR(dentry)) |
| 2277 | goto out_unlock; |
| 2278 | |
| 2279 | error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO); |
| 2280 | dput(dentry); |
| 2281 | out_unlock: |
| 2282 | mutex_unlock(&nd.dentry->d_inode->i_mutex); |
| 2283 | path_release(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | out: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 2285 | putname(to); |
| 2286 | out_putname: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | putname(from); |
| 2288 | return error; |
| 2289 | } |
| 2290 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2291 | asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname) |
| 2292 | { |
| 2293 | return sys_symlinkat(oldname, AT_FDCWD, newname); |
| 2294 | } |
| 2295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 2297 | { |
| 2298 | struct inode *inode = old_dentry->d_inode; |
| 2299 | int error; |
| 2300 | |
| 2301 | if (!inode) |
| 2302 | return -ENOENT; |
| 2303 | |
| 2304 | error = may_create(dir, new_dentry, NULL); |
| 2305 | if (error) |
| 2306 | return error; |
| 2307 | |
| 2308 | if (dir->i_sb != inode->i_sb) |
| 2309 | return -EXDEV; |
| 2310 | |
| 2311 | /* |
| 2312 | * A link to an append-only or immutable file cannot be created. |
| 2313 | */ |
| 2314 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 2315 | return -EPERM; |
| 2316 | if (!dir->i_op || !dir->i_op->link) |
| 2317 | return -EPERM; |
| 2318 | if (S_ISDIR(old_dentry->d_inode->i_mode)) |
| 2319 | return -EPERM; |
| 2320 | |
| 2321 | error = security_inode_link(old_dentry, dir, new_dentry); |
| 2322 | if (error) |
| 2323 | return error; |
| 2324 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2325 | mutex_lock(&old_dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | DQUOT_INIT(dir); |
| 2327 | error = dir->i_op->link(old_dentry, dir, new_dentry); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2328 | mutex_unlock(&old_dentry->d_inode->i_mutex); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 2329 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 2330 | fsnotify_create(dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | return error; |
| 2332 | } |
| 2333 | |
| 2334 | /* |
| 2335 | * Hardlinks are often used in delicate situations. We avoid |
| 2336 | * security-related surprises by not following symlinks on the |
| 2337 | * newname. --KAB |
| 2338 | * |
| 2339 | * We don't follow them on the oldname either to be compatible |
| 2340 | * with linux 2.0, and to avoid hard-linking to directories |
| 2341 | * and other special files. --ADM |
| 2342 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2343 | asmlinkage long sys_linkat(int olddfd, const char __user *oldname, |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 2344 | int newdfd, const char __user *newname, |
| 2345 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | { |
| 2347 | struct dentry *new_dentry; |
| 2348 | struct nameidata nd, old_nd; |
| 2349 | int error; |
| 2350 | char * to; |
| 2351 | |
Ulrich Drepper | 45c9b11a | 2006-06-25 05:49:11 -0700 | [diff] [blame] | 2352 | if ((flags & ~AT_SYMLINK_FOLLOW) != 0) |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 2353 | return -EINVAL; |
| 2354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | to = getname(newname); |
| 2356 | if (IS_ERR(to)) |
| 2357 | return PTR_ERR(to); |
| 2358 | |
Ulrich Drepper | 45c9b11a | 2006-06-25 05:49:11 -0700 | [diff] [blame] | 2359 | error = __user_walk_fd(olddfd, oldname, |
| 2360 | flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, |
| 2361 | &old_nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | if (error) |
| 2363 | goto exit; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2364 | error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | if (error) |
| 2366 | goto out; |
| 2367 | error = -EXDEV; |
| 2368 | if (old_nd.mnt != nd.mnt) |
| 2369 | goto out_release; |
| 2370 | new_dentry = lookup_create(&nd, 0); |
| 2371 | error = PTR_ERR(new_dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 2372 | if (IS_ERR(new_dentry)) |
| 2373 | goto out_unlock; |
| 2374 | error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry); |
| 2375 | dput(new_dentry); |
| 2376 | out_unlock: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2377 | mutex_unlock(&nd.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | out_release: |
| 2379 | path_release(&nd); |
| 2380 | out: |
| 2381 | path_release(&old_nd); |
| 2382 | exit: |
| 2383 | putname(to); |
| 2384 | |
| 2385 | return error; |
| 2386 | } |
| 2387 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2388 | asmlinkage long sys_link(const char __user *oldname, const char __user *newname) |
| 2389 | { |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 2390 | return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2391 | } |
| 2392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | /* |
| 2394 | * The worst of all namespace operations - renaming directory. "Perverted" |
| 2395 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... |
| 2396 | * Problems: |
| 2397 | * a) we can get into loop creation. Check is done in is_subdir(). |
| 2398 | * b) race potential - two innocent renames can create a loop together. |
| 2399 | * That's where 4.4 screws up. Current fix: serialization on |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 2400 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | * story. |
| 2402 | * c) we have to lock _three_ objects - parents and victim (if it exists). |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2403 | * And that - after we got ->i_mutex on parents (until then we don't know |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | * whether the target exists). Solution: try to be smart with locking |
| 2405 | * order for inodes. We rely on the fact that tree topology may change |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 2406 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | * move will be locked. Thus we can rank directories by the tree |
| 2408 | * (ancestors first) and rank all non-directories after them. |
| 2409 | * That works since everybody except rename does "lock parent, lookup, |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 2410 | * lock child" and rename is under ->s_vfs_rename_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
| 2412 | * has no more than 1 dentry. If "hybrid" objects will ever appear, |
| 2413 | * we'd better make sure that there's no link(2) for them. |
| 2414 | * d) some filesystems don't support opened-but-unlinked directories, |
| 2415 | * either because of layout or because they are not ready to deal with |
| 2416 | * all cases correctly. The latter will be fixed (taking this sort of |
| 2417 | * stuff into VFS), but the former is not going away. Solution: the same |
| 2418 | * trick as in rmdir(). |
| 2419 | * e) conversion from fhandle to dentry may come in the wrong moment - when |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2420 | * we are removing the target. Solution: we will have to grab ->i_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2422 | * ->i_mutex on parents, which works but leads to some truely excessive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | * locking]. |
| 2424 | */ |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 2425 | static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, |
| 2426 | struct inode *new_dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | { |
| 2428 | int error = 0; |
| 2429 | struct inode *target; |
| 2430 | |
| 2431 | /* |
| 2432 | * If we are going to change the parent - check write permissions, |
| 2433 | * we'll need to flip '..'. |
| 2434 | */ |
| 2435 | if (new_dir != old_dir) { |
| 2436 | error = permission(old_dentry->d_inode, MAY_WRITE, NULL); |
| 2437 | if (error) |
| 2438 | return error; |
| 2439 | } |
| 2440 | |
| 2441 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2442 | if (error) |
| 2443 | return error; |
| 2444 | |
| 2445 | target = new_dentry->d_inode; |
| 2446 | if (target) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2447 | mutex_lock(&target->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | dentry_unhash(new_dentry); |
| 2449 | } |
| 2450 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) |
| 2451 | error = -EBUSY; |
| 2452 | else |
| 2453 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2454 | if (target) { |
| 2455 | if (!error) |
| 2456 | target->i_flags |= S_DEAD; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2457 | mutex_unlock(&target->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | if (d_unhashed(new_dentry)) |
| 2459 | d_rehash(new_dentry); |
| 2460 | dput(new_dentry); |
| 2461 | } |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 2462 | if (!error) |
Mark Fasheh | 349457c | 2006-09-08 14:22:21 -0700 | [diff] [blame] | 2463 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) |
| 2464 | d_move(old_dentry,new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | return error; |
| 2466 | } |
| 2467 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 2468 | static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, |
| 2469 | struct inode *new_dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | { |
| 2471 | struct inode *target; |
| 2472 | int error; |
| 2473 | |
| 2474 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2475 | if (error) |
| 2476 | return error; |
| 2477 | |
| 2478 | dget(new_dentry); |
| 2479 | target = new_dentry->d_inode; |
| 2480 | if (target) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2481 | mutex_lock(&target->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) |
| 2483 | error = -EBUSY; |
| 2484 | else |
| 2485 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); |
| 2486 | if (!error) { |
Mark Fasheh | 349457c | 2006-09-08 14:22:21 -0700 | [diff] [blame] | 2487 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | d_move(old_dentry, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | } |
| 2490 | if (target) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2491 | mutex_unlock(&target->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | dput(new_dentry); |
| 2493 | return error; |
| 2494 | } |
| 2495 | |
| 2496 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 2497 | struct inode *new_dir, struct dentry *new_dentry) |
| 2498 | { |
| 2499 | int error; |
| 2500 | int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 2501 | const char *old_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | |
| 2503 | if (old_dentry->d_inode == new_dentry->d_inode) |
| 2504 | return 0; |
| 2505 | |
| 2506 | error = may_delete(old_dir, old_dentry, is_dir); |
| 2507 | if (error) |
| 2508 | return error; |
| 2509 | |
| 2510 | if (!new_dentry->d_inode) |
| 2511 | error = may_create(new_dir, new_dentry, NULL); |
| 2512 | else |
| 2513 | error = may_delete(new_dir, new_dentry, is_dir); |
| 2514 | if (error) |
| 2515 | return error; |
| 2516 | |
| 2517 | if (!old_dir->i_op || !old_dir->i_op->rename) |
| 2518 | return -EPERM; |
| 2519 | |
| 2520 | DQUOT_INIT(old_dir); |
| 2521 | DQUOT_INIT(new_dir); |
| 2522 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 2523 | old_name = fsnotify_oldname_init(old_dentry->d_name.name); |
| 2524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | if (is_dir) |
| 2526 | error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); |
| 2527 | else |
| 2528 | error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); |
| 2529 | if (!error) { |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 2530 | const char *new_name = old_dentry->d_name.name; |
John McCutchan | 89204c4 | 2005-08-15 12:13:28 -0400 | [diff] [blame] | 2531 | fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir, |
| 2532 | new_dentry->d_inode, old_dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 2534 | fsnotify_oldname_free(old_name); |
| 2535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | return error; |
| 2537 | } |
| 2538 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2539 | static int do_rename(int olddfd, const char *oldname, |
| 2540 | int newdfd, const char *newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | { |
| 2542 | int error = 0; |
| 2543 | struct dentry * old_dir, * new_dir; |
| 2544 | struct dentry * old_dentry, *new_dentry; |
| 2545 | struct dentry * trap; |
| 2546 | struct nameidata oldnd, newnd; |
| 2547 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2548 | error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | if (error) |
| 2550 | goto exit; |
| 2551 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2552 | error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | if (error) |
| 2554 | goto exit1; |
| 2555 | |
| 2556 | error = -EXDEV; |
| 2557 | if (oldnd.mnt != newnd.mnt) |
| 2558 | goto exit2; |
| 2559 | |
| 2560 | old_dir = oldnd.dentry; |
| 2561 | error = -EBUSY; |
| 2562 | if (oldnd.last_type != LAST_NORM) |
| 2563 | goto exit2; |
| 2564 | |
| 2565 | new_dir = newnd.dentry; |
| 2566 | if (newnd.last_type != LAST_NORM) |
| 2567 | goto exit2; |
| 2568 | |
| 2569 | trap = lock_rename(new_dir, old_dir); |
| 2570 | |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 2571 | old_dentry = lookup_hash(&oldnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | error = PTR_ERR(old_dentry); |
| 2573 | if (IS_ERR(old_dentry)) |
| 2574 | goto exit3; |
| 2575 | /* source must exist */ |
| 2576 | error = -ENOENT; |
| 2577 | if (!old_dentry->d_inode) |
| 2578 | goto exit4; |
| 2579 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
| 2580 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) { |
| 2581 | error = -ENOTDIR; |
| 2582 | if (oldnd.last.name[oldnd.last.len]) |
| 2583 | goto exit4; |
| 2584 | if (newnd.last.name[newnd.last.len]) |
| 2585 | goto exit4; |
| 2586 | } |
| 2587 | /* source should not be ancestor of target */ |
| 2588 | error = -EINVAL; |
| 2589 | if (old_dentry == trap) |
| 2590 | goto exit4; |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 2591 | new_dentry = lookup_hash(&newnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | error = PTR_ERR(new_dentry); |
| 2593 | if (IS_ERR(new_dentry)) |
| 2594 | goto exit4; |
| 2595 | /* target should not be an ancestor of source */ |
| 2596 | error = -ENOTEMPTY; |
| 2597 | if (new_dentry == trap) |
| 2598 | goto exit5; |
| 2599 | |
| 2600 | error = vfs_rename(old_dir->d_inode, old_dentry, |
| 2601 | new_dir->d_inode, new_dentry); |
| 2602 | exit5: |
| 2603 | dput(new_dentry); |
| 2604 | exit4: |
| 2605 | dput(old_dentry); |
| 2606 | exit3: |
| 2607 | unlock_rename(new_dir, old_dir); |
| 2608 | exit2: |
| 2609 | path_release(&newnd); |
| 2610 | exit1: |
| 2611 | path_release(&oldnd); |
| 2612 | exit: |
| 2613 | return error; |
| 2614 | } |
| 2615 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2616 | asmlinkage long sys_renameat(int olddfd, const char __user *oldname, |
| 2617 | int newdfd, const char __user *newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | { |
| 2619 | int error; |
| 2620 | char * from; |
| 2621 | char * to; |
| 2622 | |
| 2623 | from = getname(oldname); |
| 2624 | if(IS_ERR(from)) |
| 2625 | return PTR_ERR(from); |
| 2626 | to = getname(newname); |
| 2627 | error = PTR_ERR(to); |
| 2628 | if (!IS_ERR(to)) { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2629 | error = do_rename(olddfd, from, newdfd, to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | putname(to); |
| 2631 | } |
| 2632 | putname(from); |
| 2633 | return error; |
| 2634 | } |
| 2635 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2636 | asmlinkage long sys_rename(const char __user *oldname, const char __user *newname) |
| 2637 | { |
| 2638 | return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); |
| 2639 | } |
| 2640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) |
| 2642 | { |
| 2643 | int len; |
| 2644 | |
| 2645 | len = PTR_ERR(link); |
| 2646 | if (IS_ERR(link)) |
| 2647 | goto out; |
| 2648 | |
| 2649 | len = strlen(link); |
| 2650 | if (len > (unsigned) buflen) |
| 2651 | len = buflen; |
| 2652 | if (copy_to_user(buffer, link, len)) |
| 2653 | len = -EFAULT; |
| 2654 | out: |
| 2655 | return len; |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * A helper for ->readlink(). This should be used *ONLY* for symlinks that |
| 2660 | * have ->follow_link() touching nd only in nd_set_link(). Using (or not |
| 2661 | * using) it for any given inode is up to filesystem. |
| 2662 | */ |
| 2663 | int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 2664 | { |
| 2665 | struct nameidata nd; |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2666 | void *cookie; |
| 2667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | nd.depth = 0; |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2669 | cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); |
| 2670 | if (!IS_ERR(cookie)) { |
| 2671 | int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | if (dentry->d_inode->i_op->put_link) |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2673 | dentry->d_inode->i_op->put_link(dentry, &nd, cookie); |
| 2674 | cookie = ERR_PTR(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | } |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2676 | return PTR_ERR(cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | int vfs_follow_link(struct nameidata *nd, const char *link) |
| 2680 | { |
| 2681 | return __vfs_follow_link(nd, link); |
| 2682 | } |
| 2683 | |
| 2684 | /* get the link contents into pagecache */ |
| 2685 | static char *page_getlink(struct dentry * dentry, struct page **ppage) |
| 2686 | { |
| 2687 | struct page * page; |
| 2688 | struct address_space *mapping = dentry->d_inode->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 2689 | page = read_mapping_page(mapping, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | if (IS_ERR(page)) |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 2691 | return (char*)page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | *ppage = page; |
| 2693 | return kmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 2697 | { |
| 2698 | struct page *page = NULL; |
| 2699 | char *s = page_getlink(dentry, &page); |
| 2700 | int res = vfs_readlink(dentry,buffer,buflen,s); |
| 2701 | if (page) { |
| 2702 | kunmap(page); |
| 2703 | page_cache_release(page); |
| 2704 | } |
| 2705 | return res; |
| 2706 | } |
| 2707 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2708 | void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | { |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2710 | struct page *page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | nd_set_link(nd, page_getlink(dentry, &page)); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2712 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2715 | void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | { |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 2717 | struct page *page = cookie; |
| 2718 | |
| 2719 | if (page) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | kunmap(page); |
| 2721 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | } |
| 2723 | } |
| 2724 | |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 2725 | int __page_symlink(struct inode *inode, const char *symname, int len, |
| 2726 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | { |
| 2728 | struct address_space *mapping = inode->i_mapping; |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 2729 | struct page *page; |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 2730 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | char *kaddr; |
| 2732 | |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 2733 | retry: |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 2734 | err = -ENOMEM; |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 2735 | page = find_or_create_page(mapping, 0, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | if (!page) |
| 2737 | goto fail; |
| 2738 | err = mapping->a_ops->prepare_write(NULL, page, 0, len-1); |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 2739 | if (err == AOP_TRUNCATED_PAGE) { |
| 2740 | page_cache_release(page); |
| 2741 | goto retry; |
| 2742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | if (err) |
| 2744 | goto fail_map; |
| 2745 | kaddr = kmap_atomic(page, KM_USER0); |
| 2746 | memcpy(kaddr, symname, len-1); |
| 2747 | kunmap_atomic(kaddr, KM_USER0); |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 2748 | err = mapping->a_ops->commit_write(NULL, page, 0, len-1); |
| 2749 | if (err == AOP_TRUNCATED_PAGE) { |
| 2750 | page_cache_release(page); |
| 2751 | goto retry; |
| 2752 | } |
| 2753 | if (err) |
| 2754 | goto fail_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | /* |
| 2756 | * Notice that we are _not_ going to block here - end of page is |
| 2757 | * unmapped, so this will only try to map the rest of page, see |
| 2758 | * that it is unmapped (typically even will not look into inode - |
| 2759 | * ->i_size will be enough for everything) and zero it out. |
| 2760 | * OTOH it's obviously correct and should make the page up-to-date. |
| 2761 | */ |
| 2762 | if (!PageUptodate(page)) { |
| 2763 | err = mapping->a_ops->readpage(NULL, page); |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 2764 | if (err != AOP_TRUNCATED_PAGE) |
| 2765 | wait_on_page_locked(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | } else { |
| 2767 | unlock_page(page); |
| 2768 | } |
| 2769 | page_cache_release(page); |
| 2770 | if (err < 0) |
| 2771 | goto fail; |
| 2772 | mark_inode_dirty(inode); |
| 2773 | return 0; |
| 2774 | fail_map: |
| 2775 | unlock_page(page); |
| 2776 | page_cache_release(page); |
| 2777 | fail: |
| 2778 | return err; |
| 2779 | } |
| 2780 | |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 2781 | int page_symlink(struct inode *inode, const char *symname, int len) |
| 2782 | { |
| 2783 | return __page_symlink(inode, symname, len, |
| 2784 | mapping_gfp_mask(inode->i_mapping)); |
| 2785 | } |
| 2786 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2787 | const struct inode_operations page_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | .readlink = generic_readlink, |
| 2789 | .follow_link = page_follow_link_light, |
| 2790 | .put_link = page_put_link, |
| 2791 | }; |
| 2792 | |
| 2793 | EXPORT_SYMBOL(__user_walk); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2794 | EXPORT_SYMBOL(__user_walk_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | EXPORT_SYMBOL(follow_down); |
| 2796 | EXPORT_SYMBOL(follow_up); |
| 2797 | EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ |
| 2798 | EXPORT_SYMBOL(getname); |
| 2799 | EXPORT_SYMBOL(lock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | EXPORT_SYMBOL(lookup_one_len); |
| 2801 | EXPORT_SYMBOL(page_follow_link_light); |
| 2802 | EXPORT_SYMBOL(page_put_link); |
| 2803 | EXPORT_SYMBOL(page_readlink); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 2804 | EXPORT_SYMBOL(__page_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | EXPORT_SYMBOL(page_symlink); |
| 2806 | EXPORT_SYMBOL(page_symlink_inode_operations); |
| 2807 | EXPORT_SYMBOL(path_lookup); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame^] | 2808 | EXPORT_SYMBOL(vfs_path_lookup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | EXPORT_SYMBOL(path_release); |
| 2810 | EXPORT_SYMBOL(path_walk); |
| 2811 | EXPORT_SYMBOL(permission); |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 2812 | EXPORT_SYMBOL(vfs_permission); |
Christoph Hellwig | 8c744fb | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 2813 | EXPORT_SYMBOL(file_permission); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | EXPORT_SYMBOL(unlock_rename); |
| 2815 | EXPORT_SYMBOL(vfs_create); |
| 2816 | EXPORT_SYMBOL(vfs_follow_link); |
| 2817 | EXPORT_SYMBOL(vfs_link); |
| 2818 | EXPORT_SYMBOL(vfs_mkdir); |
| 2819 | EXPORT_SYMBOL(vfs_mknod); |
| 2820 | EXPORT_SYMBOL(generic_permission); |
| 2821 | EXPORT_SYMBOL(vfs_readlink); |
| 2822 | EXPORT_SYMBOL(vfs_rename); |
| 2823 | EXPORT_SYMBOL(vfs_rmdir); |
| 2824 | EXPORT_SYMBOL(vfs_symlink); |
| 2825 | EXPORT_SYMBOL(vfs_unlink); |
| 2826 | EXPORT_SYMBOL(dentry_unhash); |
| 2827 | EXPORT_SYMBOL(generic_readlink); |