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