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