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