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