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