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