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