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