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