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> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 18 | #include <linux/export.h> |
David S. Miller | 4469690 | 2012-05-23 20:12:50 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/pagemap.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/personality.h> |
| 26 | #include <linux/security.h> |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 27 | #include <linux/ima.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/syscalls.h> |
| 29 | #include <linux/mount.h> |
| 30 | #include <linux/audit.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 31 | #include <linux/capability.h> |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 32 | #include <linux/file.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 33 | #include <linux/fcntl.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 34 | #include <linux/device_cgroup.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 35 | #include <linux/fs_struct.h> |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 36 | #include <linux/posix_acl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/uaccess.h> |
| 38 | |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 39 | #include "internal.h" |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 40 | #include "mount.h" |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* [Feb-1997 T. Schoebel-Theuer] |
| 43 | * Fundamental changes in the pathname lookup mechanisms (namei) |
| 44 | * were necessary because of omirr. The reason is that omirr needs |
| 45 | * to know the _real_ pathname, not the user-supplied one, in case |
| 46 | * of symlinks (and also when transname replacements occur). |
| 47 | * |
| 48 | * The new code replaces the old recursive symlink resolution with |
| 49 | * an iterative one (in case of non-nested symlink chains). It does |
| 50 | * this with calls to <fs>_follow_link(). |
| 51 | * As a side effect, dir_namei(), _namei() and follow_link() are now |
| 52 | * replaced with a single function lookup_dentry() that can handle all |
| 53 | * the special cases of the former code. |
| 54 | * |
| 55 | * With the new dcache, the pathname is stored at each inode, at least as |
| 56 | * long as the refcount of the inode is positive. As a side effect, the |
| 57 | * size of the dcache depends on the inode cache and thus is dynamic. |
| 58 | * |
| 59 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink |
| 60 | * resolution to correspond with current state of the code. |
| 61 | * |
| 62 | * Note that the symlink resolution is not *completely* iterative. |
| 63 | * There is still a significant amount of tail- and mid- recursion in |
| 64 | * the algorithm. Also, note that <fs>_readlink() is not used in |
| 65 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() |
| 66 | * may return different results than <fs>_follow_link(). Many virtual |
| 67 | * filesystems (including /proc) exhibit this behavior. |
| 68 | */ |
| 69 | |
| 70 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: |
| 71 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL |
| 72 | * and the name already exists in form of a symlink, try to create the new |
| 73 | * name indicated by the symlink. The old code always complained that the |
| 74 | * name already exists, due to not following the symlink even if its target |
| 75 | * is nonexistent. The new semantics affects also mknod() and link() when |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 76 | * the name is a symlink pointing to a non-existent name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | * |
| 78 | * I don't know which semantics is the right one, since I have no access |
| 79 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" |
| 80 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the |
| 81 | * "old" one. Personally, I think the new semantics is much more logical. |
| 82 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing |
| 83 | * file does succeed in both HP-UX and SunOs, but not in Solaris |
| 84 | * and in the old Linux semantics. |
| 85 | */ |
| 86 | |
| 87 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink |
| 88 | * semantics. See the comments in "open_namei" and "do_link" below. |
| 89 | * |
| 90 | * [10-Sep-98 Alan Modra] Another symlink change. |
| 91 | */ |
| 92 | |
| 93 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: |
| 94 | * inside the path - always follow. |
| 95 | * in the last component in creation/removal/renaming - never follow. |
| 96 | * if LOOKUP_FOLLOW passed - follow. |
| 97 | * if the pathname has trailing slashes - follow. |
| 98 | * otherwise - don't follow. |
| 99 | * (applied in that order). |
| 100 | * |
| 101 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT |
| 102 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. |
| 103 | * During the 2.4 we need to fix the userland stuff depending on it - |
| 104 | * hopefully we will be able to get rid of that wart in 2.5. So far only |
| 105 | * XEmacs seems to be relying on it... |
| 106 | */ |
| 107 | /* |
| 108 | * [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] | 109 | * 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] | 110 | * any extra contention... |
| 111 | */ |
| 112 | |
| 113 | /* In order to reduce some races, while at the same time doing additional |
| 114 | * checking and hopefully speeding things up, we copy filenames to the |
| 115 | * kernel data space before using them.. |
| 116 | * |
| 117 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). |
| 118 | * PATH_MAX includes the nul terminator --RR. |
| 119 | */ |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 120 | void final_putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 122 | __putname(name->name); |
| 123 | kfree(name); |
| 124 | } |
| 125 | |
| 126 | static struct filename * |
| 127 | getname_flags(const char __user *filename, int flags, int *empty) |
| 128 | { |
| 129 | struct filename *result, *err; |
| 130 | char *kname; |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 131 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Jeff Layton | 7ac8626 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 133 | result = audit_reusename(filename); |
| 134 | if (result) |
| 135 | return result; |
| 136 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 137 | /* FIXME: create dedicated slabcache? */ |
| 138 | result = kzalloc(sizeof(*result), GFP_KERNEL); |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 139 | if (unlikely(!result)) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 140 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 142 | kname = __getname(); |
| 143 | if (unlikely(!kname)) { |
| 144 | err = ERR_PTR(-ENOMEM); |
| 145 | goto error_free_name; |
| 146 | } |
| 147 | |
| 148 | result->name = kname; |
| 149 | result->uptr = filename; |
| 150 | len = strncpy_from_user(kname, filename, PATH_MAX); |
| 151 | if (unlikely(len < 0)) { |
| 152 | err = ERR_PTR(len); |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 153 | goto error; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 154 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 155 | |
| 156 | /* The empty path is special. */ |
| 157 | if (unlikely(!len)) { |
| 158 | if (empty) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 159 | *empty = 1; |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 160 | err = ERR_PTR(-ENOENT); |
| 161 | if (!(flags & LOOKUP_EMPTY)) |
| 162 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 164 | |
| 165 | err = ERR_PTR(-ENAMETOOLONG); |
| 166 | if (likely(len < PATH_MAX)) { |
| 167 | audit_getname(result); |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | error: |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 172 | __putname(kname); |
| 173 | error_free_name: |
| 174 | kfree(result); |
Linus Torvalds | 3f9f0aa | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 175 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 178 | struct filename * |
| 179 | getname(const char __user * filename) |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 180 | { |
Linus Torvalds | f7493e5 | 2012-03-22 16:10:40 -0700 | [diff] [blame] | 181 | return getname_flags(filename, 0, NULL); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 182 | } |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 183 | EXPORT_SYMBOL(getname); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | #ifdef CONFIG_AUDITSYSCALL |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 186 | void putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 188 | if (unlikely(!audit_dummy_context())) |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 189 | return audit_putname(name); |
| 190 | final_putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | #endif |
| 193 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 194 | static int check_acl(struct inode *inode, int mask) |
| 195 | { |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 196 | #ifdef CONFIG_FS_POSIX_ACL |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 197 | struct posix_acl *acl; |
| 198 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 199 | if (mask & MAY_NOT_BLOCK) { |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 200 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
| 201 | if (!acl) |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 202 | return -EAGAIN; |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 203 | /* no ->get_acl() calls in RCU mode... */ |
| 204 | if (acl == ACL_NOT_CACHED) |
| 205 | return -ECHILD; |
Ari Savolainen | 206b1d0 | 2011-08-06 19:43:07 +0300 | [diff] [blame] | 206 | return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | acl = get_cached_acl(inode, ACL_TYPE_ACCESS); |
| 210 | |
| 211 | /* |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 212 | * A filesystem can force a ACL callback by just never filling the |
| 213 | * ACL cache. But normally you'd fill the cache either at inode |
| 214 | * instantiation time, or on the first ->get_acl call. |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 215 | * |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 216 | * If the filesystem doesn't have a get_acl() function at all, we'll |
| 217 | * just create the negative cache entry. |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 218 | */ |
| 219 | if (acl == ACL_NOT_CACHED) { |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 220 | if (inode->i_op->get_acl) { |
| 221 | acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); |
| 222 | if (IS_ERR(acl)) |
| 223 | return PTR_ERR(acl); |
| 224 | } else { |
| 225 | set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); |
| 226 | return -EAGAIN; |
| 227 | } |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (acl) { |
| 231 | int error = posix_acl_permission(inode, acl, mask); |
| 232 | posix_acl_release(acl); |
| 233 | return error; |
| 234 | } |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 235 | #endif |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 236 | |
| 237 | return -EAGAIN; |
| 238 | } |
| 239 | |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 240 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 241 | * This does the basic permission checking |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 242 | */ |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 243 | static int acl_permission_check(struct inode *inode, int mask) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 244 | { |
Linus Torvalds | 26cf46b | 2011-05-13 11:51:01 -0700 | [diff] [blame] | 245 | unsigned int mode = inode->i_mode; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 246 | |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 247 | if (likely(uid_eq(current_fsuid(), inode->i_uid))) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 248 | mode >>= 6; |
| 249 | else { |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 250 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 251 | int error = check_acl(inode, mask); |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 252 | if (error != -EAGAIN) |
| 253 | return error; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (in_group_p(inode->i_gid)) |
| 257 | mode >>= 3; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * If the DACs are ok we don't need any capability check. |
| 262 | */ |
Al Viro | 9c2c703 | 2011-06-20 19:06:22 -0400 | [diff] [blame] | 263 | if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 264 | return 0; |
| 265 | return -EACCES; |
| 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | /** |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 269 | * generic_permission - check for access rights on a Posix-like filesystem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * @inode: inode to check access rights for |
Andreas Gruenbacher | 8fd90c8 | 2011-10-23 23:13:30 +0530 | [diff] [blame] | 271 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | * |
| 273 | * Used to check for read/write/execute permissions on a file. |
| 274 | * We use "fsuid" for this, letting us set arbitrary permissions |
| 275 | * for filesystem access without changing the "normal" uids which |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 276 | * are used for other things. |
| 277 | * |
| 278 | * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk |
| 279 | * request cannot be satisfied (eg. requires blocking or too much complexity). |
| 280 | * It would then be called again in ref-walk mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | */ |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 282 | int generic_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 284 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 287 | * Do the basic permission checks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | */ |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 289 | ret = acl_permission_check(inode, mask); |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 290 | if (ret != -EACCES) |
| 291 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 293 | if (S_ISDIR(inode->i_mode)) { |
| 294 | /* DACs are overridable for directories */ |
Eric W. Biederman | 1a48e2a | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 295 | if (inode_capable(inode, CAP_DAC_OVERRIDE)) |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 296 | return 0; |
| 297 | if (!(mask & MAY_WRITE)) |
Eric W. Biederman | 1a48e2a | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 298 | if (inode_capable(inode, CAP_DAC_READ_SEARCH)) |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 299 | return 0; |
| 300 | return -EACCES; |
| 301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | /* |
| 303 | * Read/write DACs are always overridable. |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 304 | * Executable DACs are overridable when there is |
| 305 | * at least one exec bit set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | */ |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 307 | if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) |
Eric W. Biederman | 1a48e2a | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 308 | if (inode_capable(inode, CAP_DAC_OVERRIDE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return 0; |
| 310 | |
| 311 | /* |
| 312 | * Searching includes executable on directories, else just read. |
| 313 | */ |
Serge E. Hallyn | 7ea6600 | 2009-12-29 14:50:19 -0600 | [diff] [blame] | 314 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 315 | if (mask == MAY_READ) |
Eric W. Biederman | 1a48e2a | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 316 | if (inode_capable(inode, CAP_DAC_READ_SEARCH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | |
| 319 | return -EACCES; |
| 320 | } |
| 321 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 322 | /* |
| 323 | * We _really_ want to just do "generic_permission()" without |
| 324 | * even looking at the inode->i_op values. So we keep a cache |
| 325 | * flag in inode->i_opflags, that says "this has not special |
| 326 | * permission function, use the fast case". |
| 327 | */ |
| 328 | static inline int do_inode_permission(struct inode *inode, int mask) |
| 329 | { |
| 330 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { |
| 331 | if (likely(inode->i_op->permission)) |
| 332 | return inode->i_op->permission(inode, mask); |
| 333 | |
| 334 | /* This gets set once for the inode lifetime */ |
| 335 | spin_lock(&inode->i_lock); |
| 336 | inode->i_opflags |= IOP_FASTPERM; |
| 337 | spin_unlock(&inode->i_lock); |
| 338 | } |
| 339 | return generic_permission(inode, mask); |
| 340 | } |
| 341 | |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 342 | /** |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 343 | * __inode_permission - Check for access rights to a given inode |
| 344 | * @inode: Inode to check permission on |
| 345 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 346 | * |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 347 | * Check for read/write/execute permissions on an inode. |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 348 | * |
| 349 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 350 | * |
| 351 | * This does not check for a read-only file system. You probably want |
| 352 | * inode_permission(). |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 353 | */ |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 354 | int __inode_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 356 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 358 | if (unlikely(mask & MAY_WRITE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | /* |
| 360 | * Nobody gets write access to an immutable file. |
| 361 | */ |
| 362 | if (IS_IMMUTABLE(inode)) |
| 363 | return -EACCES; |
| 364 | } |
| 365 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 366 | retval = do_inode_permission(inode, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | if (retval) |
| 368 | return retval; |
| 369 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 370 | retval = devcgroup_inode_permission(inode, mask); |
| 371 | if (retval) |
| 372 | return retval; |
| 373 | |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 374 | return security_inode_permission(inode, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Al Viro | f4d6ff8 | 2011-06-19 13:14:21 -0400 | [diff] [blame] | 377 | /** |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 378 | * sb_permission - Check superblock-level permissions |
| 379 | * @sb: Superblock of inode to check permission on |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 380 | * @inode: Inode to check permission on |
David Howells | 0bdaea9 | 2012-06-25 12:55:46 +0100 | [diff] [blame] | 381 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 382 | * |
| 383 | * Separate out file-system wide checks from inode-specific permission checks. |
| 384 | */ |
| 385 | static int sb_permission(struct super_block *sb, struct inode *inode, int mask) |
| 386 | { |
| 387 | if (unlikely(mask & MAY_WRITE)) { |
| 388 | umode_t mode = inode->i_mode; |
| 389 | |
| 390 | /* Nobody gets write access to a read-only fs. */ |
| 391 | if ((sb->s_flags & MS_RDONLY) && |
| 392 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
| 393 | return -EROFS; |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * inode_permission - Check for access rights to a given inode |
| 400 | * @inode: Inode to check permission on |
| 401 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
| 402 | * |
| 403 | * Check for read/write/execute permissions on an inode. We use fs[ug]id for |
| 404 | * this, letting us set arbitrary permissions for filesystem access without |
| 405 | * changing the "normal" UIDs which are used for other things. |
| 406 | * |
| 407 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. |
| 408 | */ |
| 409 | int inode_permission(struct inode *inode, int mask) |
| 410 | { |
| 411 | int retval; |
| 412 | |
| 413 | retval = sb_permission(inode->i_sb, inode, mask); |
| 414 | if (retval) |
| 415 | return retval; |
| 416 | return __inode_permission(inode, mask); |
| 417 | } |
| 418 | |
| 419 | /** |
Jan Blunck | 5dd784d0 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 420 | * path_get - get a reference to a path |
| 421 | * @path: path to get the reference to |
| 422 | * |
| 423 | * Given a path increment the reference count to the dentry and the vfsmount. |
| 424 | */ |
| 425 | void path_get(struct path *path) |
| 426 | { |
| 427 | mntget(path->mnt); |
| 428 | dget(path->dentry); |
| 429 | } |
| 430 | EXPORT_SYMBOL(path_get); |
| 431 | |
| 432 | /** |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 433 | * path_put - put a reference to a path |
| 434 | * @path: path to put the reference to |
| 435 | * |
| 436 | * Given a path decrement the reference count to the dentry and the vfsmount. |
| 437 | */ |
| 438 | void path_put(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 440 | dput(path->dentry); |
| 441 | mntput(path->mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 443 | EXPORT_SYMBOL(path_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 445 | /* |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 446 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 447 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
| 448 | * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab |
| 449 | * normal reference counts on dentries and vfsmounts to transition to rcu-walk |
| 450 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
| 451 | * got stuck, so ref-walk may continue from there. If this is not successful |
| 452 | * (eg. a seqcount has changed), then failure is returned and it's up to caller |
| 453 | * to restart the path walk from the beginning in ref-walk mode. |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 454 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 455 | |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 456 | static inline void lock_rcu_walk(void) |
| 457 | { |
| 458 | br_read_lock(&vfsmount_lock); |
| 459 | rcu_read_lock(); |
| 460 | } |
| 461 | |
| 462 | static inline void unlock_rcu_walk(void) |
| 463 | { |
| 464 | rcu_read_unlock(); |
| 465 | br_read_unlock(&vfsmount_lock); |
| 466 | } |
| 467 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 468 | /** |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 469 | * unlazy_walk - try to switch to ref-walk mode. |
| 470 | * @nd: nameidata pathwalk data |
| 471 | * @dentry: child of nd->path.dentry or NULL |
Randy Dunlap | 3919162 | 2011-01-08 19:36:21 -0800 | [diff] [blame] | 472 | * Returns: 0 on success, -ECHILD on failure |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 473 | * |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 474 | * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry |
| 475 | * for ref-walk mode. @dentry must be a path found by a do_lookup call on |
| 476 | * @nd or NULL. Must be called from rcu-walk context. |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 477 | */ |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 478 | static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 479 | { |
| 480 | struct fs_struct *fs = current->fs; |
| 481 | struct dentry *parent = nd->path.dentry; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 482 | int want_root = 0; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 483 | |
| 484 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 485 | if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { |
| 486 | want_root = 1; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 487 | spin_lock(&fs->lock); |
| 488 | if (nd->root.mnt != fs->root.mnt || |
| 489 | nd->root.dentry != fs->root.dentry) |
| 490 | goto err_root; |
| 491 | } |
| 492 | spin_lock(&parent->d_lock); |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 493 | if (!dentry) { |
| 494 | if (!__d_rcu_to_refcount(parent, nd->seq)) |
| 495 | goto err_parent; |
| 496 | BUG_ON(nd->inode != parent->d_inode); |
| 497 | } else { |
Al Viro | 94c0d4e | 2011-07-12 21:40:23 -0400 | [diff] [blame] | 498 | if (dentry->d_parent != parent) |
| 499 | goto err_parent; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 500 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
| 501 | if (!__d_rcu_to_refcount(dentry, nd->seq)) |
| 502 | goto err_child; |
| 503 | /* |
| 504 | * If the sequence check on the child dentry passed, then |
| 505 | * the child has not been removed from its parent. This |
| 506 | * means the parent dentry must be valid and able to take |
| 507 | * a reference at this point. |
| 508 | */ |
| 509 | BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); |
| 510 | BUG_ON(!parent->d_count); |
| 511 | parent->d_count++; |
| 512 | spin_unlock(&dentry->d_lock); |
| 513 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 514 | spin_unlock(&parent->d_lock); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 515 | if (want_root) { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 516 | path_get(&nd->root); |
| 517 | spin_unlock(&fs->lock); |
| 518 | } |
| 519 | mntget(nd->path.mnt); |
| 520 | |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 521 | unlock_rcu_walk(); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 522 | nd->flags &= ~LOOKUP_RCU; |
| 523 | return 0; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 524 | |
| 525 | err_child: |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 526 | spin_unlock(&dentry->d_lock); |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 527 | err_parent: |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 528 | spin_unlock(&parent->d_lock); |
| 529 | err_root: |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 530 | if (want_root) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 531 | spin_unlock(&fs->lock); |
| 532 | return -ECHILD; |
| 533 | } |
| 534 | |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 535 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 536 | { |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 537 | return dentry->d_op->d_revalidate(dentry, flags); |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 538 | } |
| 539 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 540 | /** |
| 541 | * complete_walk - successful completion of path walk |
| 542 | * @nd: pointer nameidata |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 543 | * |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 544 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
| 545 | * Revalidate the final result, unless we'd already done that during |
| 546 | * the path walk or the filesystem doesn't ask for it. Return 0 on |
| 547 | * success, -error on failure. In case of failure caller does not |
| 548 | * need to drop nd->path. |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 549 | */ |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 550 | static int complete_walk(struct nameidata *nd) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 551 | { |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 552 | struct dentry *dentry = nd->path.dentry; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 553 | int status; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 554 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 555 | if (nd->flags & LOOKUP_RCU) { |
| 556 | nd->flags &= ~LOOKUP_RCU; |
| 557 | if (!(nd->flags & LOOKUP_ROOT)) |
| 558 | nd->root.mnt = NULL; |
| 559 | spin_lock(&dentry->d_lock); |
| 560 | if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { |
| 561 | spin_unlock(&dentry->d_lock); |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 562 | unlock_rcu_walk(); |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 563 | return -ECHILD; |
| 564 | } |
| 565 | BUG_ON(nd->inode != dentry->d_inode); |
| 566 | spin_unlock(&dentry->d_lock); |
| 567 | mntget(nd->path.mnt); |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 568 | unlock_rcu_walk(); |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 569 | } |
| 570 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 571 | if (likely(!(nd->flags & LOOKUP_JUMPED))) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 572 | return 0; |
| 573 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 574 | if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) |
| 575 | return 0; |
| 576 | |
| 577 | if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) |
| 578 | return 0; |
| 579 | |
| 580 | /* Note: we do not d_invalidate() */ |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 581 | status = d_revalidate(dentry, nd->flags); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 582 | if (status > 0) |
| 583 | return 0; |
| 584 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 585 | if (!status) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 586 | status = -ESTALE; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 587 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 588 | path_put(&nd->path); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 589 | return status; |
| 590 | } |
| 591 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 592 | static __always_inline void set_root(struct nameidata *nd) |
| 593 | { |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 594 | if (!nd->root.mnt) |
| 595 | get_fs_root(current->fs, &nd->root); |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 596 | } |
| 597 | |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 598 | static int link_path_walk(const char *, struct nameidata *); |
| 599 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 600 | static __always_inline void set_root_rcu(struct nameidata *nd) |
| 601 | { |
| 602 | if (!nd->root.mnt) { |
| 603 | struct fs_struct *fs = current->fs; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 604 | unsigned seq; |
| 605 | |
| 606 | do { |
| 607 | seq = read_seqcount_begin(&fs->seq); |
| 608 | nd->root = fs->root; |
Tim Chen | c153001 | 2011-04-15 11:39:29 -0700 | [diff] [blame] | 609 | nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 610 | } while (read_seqcount_retry(&fs->seq, seq)); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Arjan van de Ven | f166235 | 2006-01-14 13:21:31 -0800 | [diff] [blame] | 614 | 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] | 615 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 616 | int ret; |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | if (IS_ERR(link)) |
| 619 | goto fail; |
| 620 | |
| 621 | if (*link == '/') { |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 622 | set_root(nd); |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 623 | path_put(&nd->path); |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 624 | nd->path = nd->root; |
| 625 | path_get(&nd->root); |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 626 | nd->flags |= LOOKUP_JUMPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 628 | nd->inode = nd->path.dentry->d_inode; |
Christoph Hellwig | b4091d5 | 2008-11-05 15:07:21 +0100 | [diff] [blame] | 629 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 630 | ret = link_path_walk(link, nd); |
| 631 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | fail: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 633 | path_put(&nd->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | return PTR_ERR(link); |
| 635 | } |
| 636 | |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 637 | static void path_put_conditional(struct path *path, struct nameidata *nd) |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 638 | { |
| 639 | dput(path->dentry); |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 640 | if (path->mnt != nd->path.mnt) |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 641 | mntput(path->mnt); |
| 642 | } |
| 643 | |
Nick Piggin | 7b9337a | 2011-01-14 08:42:43 +0000 | [diff] [blame] | 644 | static inline void path_to_nameidata(const struct path *path, |
| 645 | struct nameidata *nd) |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 646 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 647 | if (!(nd->flags & LOOKUP_RCU)) { |
| 648 | dput(nd->path.dentry); |
| 649 | if (nd->path.mnt != path->mnt) |
| 650 | mntput(nd->path.mnt); |
Huang Shijie | 9a22968 | 2010-04-02 17:37:13 +0800 | [diff] [blame] | 651 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 652 | nd->path.mnt = path->mnt; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 653 | nd->path.dentry = path->dentry; |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 656 | /* |
| 657 | * Helper to directly jump to a known parsed path from ->follow_link, |
| 658 | * caller must have taken a reference to path beforehand. |
| 659 | */ |
| 660 | void nd_jump_link(struct nameidata *nd, struct path *path) |
| 661 | { |
| 662 | path_put(&nd->path); |
| 663 | |
| 664 | nd->path = *path; |
| 665 | nd->inode = nd->path.dentry->d_inode; |
| 666 | nd->flags |= LOOKUP_JUMPED; |
| 667 | |
| 668 | BUG_ON(nd->inode->i_op->follow_link); |
| 669 | } |
| 670 | |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 671 | static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) |
| 672 | { |
| 673 | struct inode *inode = link->dentry->d_inode; |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 674 | if (inode->i_op->put_link) |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 675 | inode->i_op->put_link(link->dentry, nd, cookie); |
| 676 | path_put(link); |
| 677 | } |
| 678 | |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 679 | int sysctl_protected_symlinks __read_mostly = 1; |
| 680 | int sysctl_protected_hardlinks __read_mostly = 1; |
| 681 | |
| 682 | /** |
| 683 | * may_follow_link - Check symlink following for unsafe situations |
| 684 | * @link: The path of the symlink |
Randy Dunlap | 5585263 | 2012-08-18 17:39:25 -0700 | [diff] [blame] | 685 | * @nd: nameidata pathwalk data |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 686 | * |
| 687 | * In the case of the sysctl_protected_symlinks sysctl being enabled, |
| 688 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is |
| 689 | * in a sticky world-writable directory. This is to protect privileged |
| 690 | * processes from failing races against path names that may change out |
| 691 | * from under them by way of other users creating malicious symlinks. |
| 692 | * It will permit symlinks to be followed only when outside a sticky |
| 693 | * world-writable directory, or when the uid of the symlink and follower |
| 694 | * match, or when the directory owner matches the symlink's owner. |
| 695 | * |
| 696 | * Returns 0 if following the symlink is allowed, -ve on error. |
| 697 | */ |
| 698 | static inline int may_follow_link(struct path *link, struct nameidata *nd) |
| 699 | { |
| 700 | const struct inode *inode; |
| 701 | const struct inode *parent; |
| 702 | |
| 703 | if (!sysctl_protected_symlinks) |
| 704 | return 0; |
| 705 | |
| 706 | /* Allowed if owner and follower match. */ |
| 707 | inode = link->dentry->d_inode; |
Eric W. Biederman | 81abe27 | 2012-08-03 09:38:08 -0700 | [diff] [blame] | 708 | if (uid_eq(current_cred()->fsuid, inode->i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 709 | return 0; |
| 710 | |
| 711 | /* Allowed if parent directory not sticky and world-writable. */ |
| 712 | parent = nd->path.dentry->d_inode; |
| 713 | if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
| 714 | return 0; |
| 715 | |
| 716 | /* Allowed if parent directory and link owner match. */ |
Eric W. Biederman | 81abe27 | 2012-08-03 09:38:08 -0700 | [diff] [blame] | 717 | if (uid_eq(parent->i_uid, inode->i_uid)) |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 718 | return 0; |
| 719 | |
Sasha Levin | ffd8d10 | 2012-10-04 19:56:40 -0400 | [diff] [blame] | 720 | audit_log_link_denied("follow_link", link); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 721 | path_put_conditional(link, nd); |
| 722 | path_put(&nd->path); |
| 723 | return -EACCES; |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * safe_hardlink_source - Check for safe hardlink conditions |
| 728 | * @inode: the source inode to hardlink from |
| 729 | * |
| 730 | * Return false if at least one of the following conditions: |
| 731 | * - inode is not a regular file |
| 732 | * - inode is setuid |
| 733 | * - inode is setgid and group-exec |
| 734 | * - access failure for read and write |
| 735 | * |
| 736 | * Otherwise returns true. |
| 737 | */ |
| 738 | static bool safe_hardlink_source(struct inode *inode) |
| 739 | { |
| 740 | umode_t mode = inode->i_mode; |
| 741 | |
| 742 | /* Special files should not get pinned to the filesystem. */ |
| 743 | if (!S_ISREG(mode)) |
| 744 | return false; |
| 745 | |
| 746 | /* Setuid files should not get pinned to the filesystem. */ |
| 747 | if (mode & S_ISUID) |
| 748 | return false; |
| 749 | |
| 750 | /* Executable setgid files should not get pinned to the filesystem. */ |
| 751 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 752 | return false; |
| 753 | |
| 754 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ |
| 755 | if (inode_permission(inode, MAY_READ | MAY_WRITE)) |
| 756 | return false; |
| 757 | |
| 758 | return true; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * may_linkat - Check permissions for creating a hardlink |
| 763 | * @link: the source to hardlink from |
| 764 | * |
| 765 | * Block hardlink when all of: |
| 766 | * - sysctl_protected_hardlinks enabled |
| 767 | * - fsuid does not match inode |
| 768 | * - hardlink source is unsafe (see safe_hardlink_source() above) |
| 769 | * - not CAP_FOWNER |
| 770 | * |
| 771 | * Returns 0 if successful, -ve on error. |
| 772 | */ |
| 773 | static int may_linkat(struct path *link) |
| 774 | { |
| 775 | const struct cred *cred; |
| 776 | struct inode *inode; |
| 777 | |
| 778 | if (!sysctl_protected_hardlinks) |
| 779 | return 0; |
| 780 | |
| 781 | cred = current_cred(); |
| 782 | inode = link->dentry->d_inode; |
| 783 | |
| 784 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
| 785 | * otherwise, it must be a safe source. |
| 786 | */ |
Eric W. Biederman | 81abe27 | 2012-08-03 09:38:08 -0700 | [diff] [blame] | 787 | if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) || |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 788 | capable(CAP_FOWNER)) |
| 789 | return 0; |
| 790 | |
Kees Cook | a51d9ea | 2012-07-25 17:29:08 -0700 | [diff] [blame] | 791 | audit_log_link_denied("linkat", link); |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 792 | return -EPERM; |
| 793 | } |
| 794 | |
Al Viro | def4af3 | 2009-12-26 08:37:05 -0500 | [diff] [blame] | 795 | static __always_inline int |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 796 | follow_link(struct path *link, struct nameidata *nd, void **p) |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 797 | { |
Nick Piggin | 7b9337a | 2011-01-14 08:42:43 +0000 | [diff] [blame] | 798 | struct dentry *dentry = link->dentry; |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 799 | int error; |
| 800 | char *s; |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 801 | |
Al Viro | 844a391 | 2011-02-15 00:38:26 -0500 | [diff] [blame] | 802 | BUG_ON(nd->flags & LOOKUP_RCU); |
| 803 | |
Al Viro | 0e79458 | 2011-03-16 02:45:02 -0400 | [diff] [blame] | 804 | if (link->mnt == nd->path.mnt) |
| 805 | mntget(link->mnt); |
| 806 | |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 807 | error = -ELOOP; |
| 808 | if (unlikely(current->total_link_count >= 40)) |
| 809 | goto out_put_nd_path; |
| 810 | |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 811 | cond_resched(); |
| 812 | current->total_link_count++; |
| 813 | |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 814 | touch_atime(link); |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 815 | nd_set_link(nd, NULL); |
| 816 | |
Al Viro | 36f3b4f | 2011-02-22 21:24:38 -0500 | [diff] [blame] | 817 | error = security_inode_follow_link(link->dentry, nd); |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 818 | if (error) |
| 819 | goto out_put_nd_path; |
Al Viro | 36f3b4f | 2011-02-22 21:24:38 -0500 | [diff] [blame] | 820 | |
Al Viro | 86acdca1 | 2009-12-22 23:45:11 -0500 | [diff] [blame] | 821 | nd->last_type = LAST_BIND; |
Al Viro | def4af3 | 2009-12-26 08:37:05 -0500 | [diff] [blame] | 822 | *p = dentry->d_inode->i_op->follow_link(dentry, nd); |
| 823 | error = PTR_ERR(*p); |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 824 | if (IS_ERR(*p)) |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 825 | goto out_put_nd_path; |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 826 | |
| 827 | error = 0; |
| 828 | s = nd_get_link(nd); |
| 829 | if (s) { |
| 830 | error = __vfs_follow_link(nd, s); |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 831 | if (unlikely(error)) |
| 832 | put_link(nd, link, *p); |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 833 | } |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 834 | |
| 835 | return error; |
| 836 | |
| 837 | out_put_nd_path: |
Arnd Bergmann | 98f6ef6 | 2012-10-11 13:20:00 +0000 | [diff] [blame] | 838 | *p = NULL; |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 839 | path_put(&nd->path); |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 840 | path_put(link); |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 841 | return error; |
| 842 | } |
| 843 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 844 | static int follow_up_rcu(struct path *path) |
| 845 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 846 | struct mount *mnt = real_mount(path->mnt); |
| 847 | struct mount *parent; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 848 | struct dentry *mountpoint; |
| 849 | |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 850 | parent = mnt->mnt_parent; |
| 851 | if (&parent->mnt == path->mnt) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 852 | return 0; |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 853 | mountpoint = mnt->mnt_mountpoint; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 854 | path->dentry = mountpoint; |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 855 | path->mnt = &parent->mnt; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 856 | return 1; |
| 857 | } |
| 858 | |
David Howells | f015f126 | 2012-06-25 12:55:28 +0100 | [diff] [blame] | 859 | /* |
| 860 | * follow_up - Find the mountpoint of path's vfsmount |
| 861 | * |
| 862 | * Given a path, find the mountpoint of its source file system. |
| 863 | * Replace @path with the path of the mountpoint in the parent mount. |
| 864 | * Up is towards /. |
| 865 | * |
| 866 | * Return 1 if we went up a level and 0 if we were already at the |
| 867 | * root. |
| 868 | */ |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 869 | int follow_up(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 871 | struct mount *mnt = real_mount(path->mnt); |
| 872 | struct mount *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | struct dentry *mountpoint; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 874 | |
Andi Kleen | 962830d | 2012-05-08 13:32:02 +0930 | [diff] [blame] | 875 | br_read_lock(&vfsmount_lock); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 876 | parent = mnt->mnt_parent; |
Al Viro | 3c0a616 | 2012-07-18 17:32:50 +0400 | [diff] [blame] | 877 | if (parent == mnt) { |
Andi Kleen | 962830d | 2012-05-08 13:32:02 +0930 | [diff] [blame] | 878 | br_read_unlock(&vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return 0; |
| 880 | } |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 881 | mntget(&parent->mnt); |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 882 | mountpoint = dget(mnt->mnt_mountpoint); |
Andi Kleen | 962830d | 2012-05-08 13:32:02 +0930 | [diff] [blame] | 883 | br_read_unlock(&vfsmount_lock); |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 884 | dput(path->dentry); |
| 885 | path->dentry = mountpoint; |
| 886 | mntput(path->mnt); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 887 | path->mnt = &parent->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | return 1; |
| 889 | } |
| 890 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 891 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 892 | * Perform an automount |
| 893 | * - return -EISDIR to tell follow_managed() to stop and return the path we |
| 894 | * were called with. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | */ |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 896 | static int follow_automount(struct path *path, unsigned flags, |
| 897 | bool *need_mntput) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 898 | { |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 899 | struct vfsmount *mnt; |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 900 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 901 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 902 | if (!path->dentry->d_op || !path->dentry->d_op->d_automount) |
| 903 | return -EREMOTE; |
Al Viro | 463ffb2 | 2005-06-06 13:36:05 -0700 | [diff] [blame] | 904 | |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 905 | /* We don't want to mount if someone's just doing a stat - |
| 906 | * unless they're stat'ing a directory and appended a '/' to |
| 907 | * the name. |
| 908 | * |
| 909 | * We do, however, want to mount if someone wants to open or |
| 910 | * create a file of any type under the mountpoint, wants to |
| 911 | * traverse through the mountpoint or wants to open the |
| 912 | * mounted directory. Also, autofs may mark negative dentries |
| 913 | * as being automount points. These will need the attentions |
| 914 | * of the daemon to instantiate them before they can be used. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 915 | */ |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 916 | if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
Linus Torvalds | d94c177 | 2011-09-26 17:44:55 -0700 | [diff] [blame] | 917 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 918 | path->dentry->d_inode) |
| 919 | return -EISDIR; |
| 920 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 921 | current->total_link_count++; |
| 922 | if (current->total_link_count >= 40) |
| 923 | return -ELOOP; |
| 924 | |
| 925 | mnt = path->dentry->d_op->d_automount(path); |
| 926 | if (IS_ERR(mnt)) { |
| 927 | /* |
| 928 | * The filesystem is allowed to return -EISDIR here to indicate |
| 929 | * it doesn't want to automount. For instance, autofs would do |
| 930 | * this so that its userspace daemon can mount on this dentry. |
| 931 | * |
| 932 | * However, we can only permit this if it's a terminal point in |
| 933 | * the path being looked up; if it wasn't then the remainder of |
| 934 | * the path is inaccessible and we should say so. |
| 935 | */ |
Al Viro | 49084c3 | 2011-06-25 21:59:52 -0400 | [diff] [blame] | 936 | if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 937 | return -EREMOTE; |
| 938 | return PTR_ERR(mnt); |
| 939 | } |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 940 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 941 | if (!mnt) /* mount collision */ |
| 942 | return 0; |
| 943 | |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 944 | if (!*need_mntput) { |
| 945 | /* lock_mount() may release path->mnt on error */ |
| 946 | mntget(path->mnt); |
| 947 | *need_mntput = true; |
| 948 | } |
Al Viro | 19a167a | 2011-01-17 01:35:23 -0500 | [diff] [blame] | 949 | err = finish_automount(mnt, path); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 950 | |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 951 | switch (err) { |
| 952 | case -EBUSY: |
| 953 | /* Someone else made a mount here whilst we were busy */ |
Al Viro | 19a167a | 2011-01-17 01:35:23 -0500 | [diff] [blame] | 954 | return 0; |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 955 | case 0: |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 956 | path_put(path); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 957 | path->mnt = mnt; |
| 958 | path->dentry = dget(mnt->mnt_root); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 959 | return 0; |
Al Viro | 19a167a | 2011-01-17 01:35:23 -0500 | [diff] [blame] | 960 | default: |
| 961 | return err; |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 962 | } |
Al Viro | 19a167a | 2011-01-17 01:35:23 -0500 | [diff] [blame] | 963 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Handle a dentry that is managed in some way. |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 968 | * - Flagged for transit management (autofs) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 969 | * - Flagged as mountpoint |
| 970 | * - Flagged as automount point |
| 971 | * |
| 972 | * This may only be called in refwalk mode. |
| 973 | * |
| 974 | * Serialization is taken care of in namespace.c |
| 975 | */ |
| 976 | static int follow_managed(struct path *path, unsigned flags) |
| 977 | { |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 978 | struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 979 | unsigned managed; |
| 980 | bool need_mntput = false; |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 981 | int ret = 0; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 982 | |
| 983 | /* Given that we're not holding a lock here, we retain the value in a |
| 984 | * local variable for each dentry as we look at it so that we don't see |
| 985 | * the components of that value change under us */ |
| 986 | while (managed = ACCESS_ONCE(path->dentry->d_flags), |
| 987 | managed &= DCACHE_MANAGED_DENTRY, |
| 988 | unlikely(managed != 0)) { |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 989 | /* Allow the filesystem to manage the transit without i_mutex |
| 990 | * being held. */ |
| 991 | if (managed & DCACHE_MANAGE_TRANSIT) { |
| 992 | BUG_ON(!path->dentry->d_op); |
| 993 | BUG_ON(!path->dentry->d_op->d_manage); |
Al Viro | 1aed3e4 | 2011-03-18 09:09:02 -0400 | [diff] [blame] | 994 | ret = path->dentry->d_op->d_manage(path->dentry, false); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 995 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 996 | break; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 997 | } |
| 998 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 999 | /* Transit to a mounted filesystem. */ |
| 1000 | if (managed & DCACHE_MOUNTED) { |
| 1001 | struct vfsmount *mounted = lookup_mnt(path); |
| 1002 | if (mounted) { |
| 1003 | dput(path->dentry); |
| 1004 | if (need_mntput) |
| 1005 | mntput(path->mnt); |
| 1006 | path->mnt = mounted; |
| 1007 | path->dentry = dget(mounted->mnt_root); |
| 1008 | need_mntput = true; |
| 1009 | continue; |
| 1010 | } |
| 1011 | |
| 1012 | /* Something is mounted on this dentry in another |
| 1013 | * namespace and/or whatever was mounted there in this |
| 1014 | * namespace got unmounted before we managed to get the |
| 1015 | * vfsmount_lock */ |
| 1016 | } |
| 1017 | |
| 1018 | /* Handle an automount point */ |
| 1019 | if (managed & DCACHE_NEED_AUTOMOUNT) { |
| 1020 | ret = follow_automount(path, flags, &need_mntput); |
| 1021 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1022 | break; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1023 | continue; |
| 1024 | } |
| 1025 | |
| 1026 | /* We didn't change the current path point */ |
| 1027 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1029 | |
| 1030 | if (need_mntput && path->mnt == mnt) |
| 1031 | mntput(path->mnt); |
| 1032 | if (ret == -EISDIR) |
| 1033 | ret = 0; |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 1034 | return ret < 0 ? ret : need_mntput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1037 | int follow_down_one(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
| 1039 | struct vfsmount *mounted; |
| 1040 | |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 1041 | mounted = lookup_mnt(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | if (mounted) { |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1043 | dput(path->dentry); |
| 1044 | mntput(path->mnt); |
| 1045 | path->mnt = mounted; |
| 1046 | path->dentry = dget(mounted->mnt_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | return 1; |
| 1048 | } |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1052 | static inline bool managed_dentry_might_block(struct dentry *dentry) |
| 1053 | { |
| 1054 | return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && |
| 1055 | dentry->d_op->d_manage(dentry, true) < 0); |
| 1056 | } |
| 1057 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1058 | /* |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1059 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
| 1060 | * we meet a managed dentry that would need blocking. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1061 | */ |
| 1062 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1063 | struct inode **inode) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1064 | { |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1065 | for (;;) { |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 1066 | struct mount *mounted; |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1067 | /* |
| 1068 | * Don't forget we might have a non-mountpoint managed dentry |
| 1069 | * that wants to block transit. |
| 1070 | */ |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1071 | if (unlikely(managed_dentry_might_block(path->dentry))) |
David Howells | ab90911 | 2011-01-14 18:46:51 +0000 | [diff] [blame] | 1072 | return false; |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1073 | |
| 1074 | if (!d_mountpoint(path->dentry)) |
| 1075 | break; |
| 1076 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1077 | mounted = __lookup_mnt(path->mnt, path->dentry, 1); |
| 1078 | if (!mounted) |
| 1079 | break; |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 1080 | path->mnt = &mounted->mnt; |
| 1081 | path->dentry = mounted->mnt.mnt_root; |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 1082 | nd->flags |= LOOKUP_JUMPED; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1083 | nd->seq = read_seqcount_begin(&path->dentry->d_seq); |
Linus Torvalds | 5943026 | 2011-07-18 15:43:29 -0700 | [diff] [blame] | 1084 | /* |
| 1085 | * Update the inode too. We don't need to re-check the |
| 1086 | * dentry sequence number here after this d_inode read, |
| 1087 | * because a mount-point is always pinned. |
| 1088 | */ |
| 1089 | *inode = path->dentry->d_inode; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1090 | } |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1091 | return true; |
| 1092 | } |
| 1093 | |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1094 | static void follow_mount_rcu(struct nameidata *nd) |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1095 | { |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1096 | while (d_mountpoint(nd->path.dentry)) { |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 1097 | struct mount *mounted; |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1098 | mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1099 | if (!mounted) |
| 1100 | break; |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 1101 | nd->path.mnt = &mounted->mnt; |
| 1102 | nd->path.dentry = mounted->mnt.mnt_root; |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1103 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1107 | static int follow_dotdot_rcu(struct nameidata *nd) |
| 1108 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1109 | set_root_rcu(nd); |
| 1110 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1111 | while (1) { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1112 | if (nd->path.dentry == nd->root.dentry && |
| 1113 | nd->path.mnt == nd->root.mnt) { |
| 1114 | break; |
| 1115 | } |
| 1116 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
| 1117 | struct dentry *old = nd->path.dentry; |
| 1118 | struct dentry *parent = old->d_parent; |
| 1119 | unsigned seq; |
| 1120 | |
| 1121 | seq = read_seqcount_begin(&parent->d_seq); |
| 1122 | if (read_seqcount_retry(&old->d_seq, nd->seq)) |
Al Viro | ef7562d | 2011-03-04 14:35:59 -0500 | [diff] [blame] | 1123 | goto failed; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1124 | nd->path.dentry = parent; |
| 1125 | nd->seq = seq; |
| 1126 | break; |
| 1127 | } |
| 1128 | if (!follow_up_rcu(&nd->path)) |
| 1129 | break; |
| 1130 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1131 | } |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1132 | follow_mount_rcu(nd); |
| 1133 | nd->inode = nd->path.dentry->d_inode; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1134 | return 0; |
Al Viro | ef7562d | 2011-03-04 14:35:59 -0500 | [diff] [blame] | 1135 | |
| 1136 | failed: |
| 1137 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1138 | if (!(nd->flags & LOOKUP_ROOT)) |
| 1139 | nd->root.mnt = NULL; |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1140 | unlock_rcu_walk(); |
Al Viro | ef7562d | 2011-03-04 14:35:59 -0500 | [diff] [blame] | 1141 | return -ECHILD; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1142 | } |
| 1143 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1144 | /* |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1145 | * Follow down to the covering mount currently visible to userspace. At each |
| 1146 | * point, the filesystem owning that dentry may be queried as to whether the |
| 1147 | * caller is permitted to proceed or not. |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1148 | */ |
Al Viro | 7cc90cc | 2011-03-18 09:04:20 -0400 | [diff] [blame] | 1149 | int follow_down(struct path *path) |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1150 | { |
| 1151 | unsigned managed; |
| 1152 | int ret; |
| 1153 | |
| 1154 | while (managed = ACCESS_ONCE(path->dentry->d_flags), |
| 1155 | unlikely(managed & DCACHE_MANAGED_DENTRY)) { |
| 1156 | /* Allow the filesystem to manage the transit without i_mutex |
| 1157 | * being held. |
| 1158 | * |
| 1159 | * We indicate to the filesystem if someone is trying to mount |
| 1160 | * something here. This gives autofs the chance to deny anyone |
| 1161 | * other than its daemon the right to mount on its |
| 1162 | * superstructure. |
| 1163 | * |
| 1164 | * The filesystem may sleep at this point. |
| 1165 | */ |
| 1166 | if (managed & DCACHE_MANAGE_TRANSIT) { |
| 1167 | BUG_ON(!path->dentry->d_op); |
| 1168 | BUG_ON(!path->dentry->d_op->d_manage); |
David Howells | ab90911 | 2011-01-14 18:46:51 +0000 | [diff] [blame] | 1169 | ret = path->dentry->d_op->d_manage( |
Al Viro | 1aed3e4 | 2011-03-18 09:09:02 -0400 | [diff] [blame] | 1170 | path->dentry, false); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1171 | if (ret < 0) |
| 1172 | return ret == -EISDIR ? 0 : ret; |
| 1173 | } |
| 1174 | |
| 1175 | /* Transit to a mounted filesystem. */ |
| 1176 | if (managed & DCACHE_MOUNTED) { |
| 1177 | struct vfsmount *mounted = lookup_mnt(path); |
| 1178 | if (!mounted) |
| 1179 | break; |
| 1180 | dput(path->dentry); |
| 1181 | mntput(path->mnt); |
| 1182 | path->mnt = mounted; |
| 1183 | path->dentry = dget(mounted->mnt_root); |
| 1184 | continue; |
| 1185 | } |
| 1186 | |
| 1187 | /* Don't handle automount points here */ |
| 1188 | break; |
| 1189 | } |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1194 | * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() |
| 1195 | */ |
| 1196 | static void follow_mount(struct path *path) |
| 1197 | { |
| 1198 | while (d_mountpoint(path->dentry)) { |
| 1199 | struct vfsmount *mounted = lookup_mnt(path); |
| 1200 | if (!mounted) |
| 1201 | break; |
| 1202 | dput(path->dentry); |
| 1203 | mntput(path->mnt); |
| 1204 | path->mnt = mounted; |
| 1205 | path->dentry = dget(mounted->mnt_root); |
| 1206 | } |
| 1207 | } |
| 1208 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1209 | static void follow_dotdot(struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | { |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 1211 | set_root(nd); |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | while(1) { |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1214 | struct dentry *old = nd->path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 1216 | if (nd->path.dentry == nd->root.dentry && |
| 1217 | nd->path.mnt == nd->root.mnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | break; |
| 1219 | } |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1220 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
Al Viro | 3088dd7 | 2010-01-30 15:47:29 -0500 | [diff] [blame] | 1221 | /* rare case of legitimate dget_parent()... */ |
| 1222 | nd->path.dentry = dget_parent(nd->path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | dput(old); |
| 1224 | break; |
| 1225 | } |
Al Viro | 3088dd7 | 2010-01-30 15:47:29 -0500 | [diff] [blame] | 1226 | if (!follow_up(&nd->path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
Al Viro | 79ed022 | 2009-04-18 13:59:41 -0400 | [diff] [blame] | 1229 | follow_mount(&nd->path); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1230 | nd->inode = nd->path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | /* |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1234 | * This looks up the name in dcache, possibly revalidates the old dentry and |
| 1235 | * allocates a new one if not found or not valid. In the need_lookup argument |
| 1236 | * returns whether i_op->lookup is necessary. |
| 1237 | * |
| 1238 | * dir->d_inode->i_mutex must be held |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1239 | */ |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1240 | static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, |
Al Viro | 201f956 | 2012-06-22 12:42:10 +0400 | [diff] [blame] | 1241 | unsigned int flags, bool *need_lookup) |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1242 | { |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1243 | struct dentry *dentry; |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1244 | int error; |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1245 | |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1246 | *need_lookup = false; |
| 1247 | dentry = d_lookup(dir, name); |
| 1248 | if (dentry) { |
| 1249 | if (d_need_lookup(dentry)) { |
| 1250 | *need_lookup = true; |
| 1251 | } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { |
Al Viro | 201f956 | 2012-06-22 12:42:10 +0400 | [diff] [blame] | 1252 | error = d_revalidate(dentry, flags); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1253 | if (unlikely(error <= 0)) { |
| 1254 | if (error < 0) { |
| 1255 | dput(dentry); |
| 1256 | return ERR_PTR(error); |
| 1257 | } else if (!d_invalidate(dentry)) { |
| 1258 | dput(dentry); |
| 1259 | dentry = NULL; |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | } |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1264 | |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1265 | if (!dentry) { |
| 1266 | dentry = d_alloc(dir, name); |
| 1267 | if (unlikely(!dentry)) |
| 1268 | return ERR_PTR(-ENOMEM); |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1269 | |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1270 | *need_lookup = true; |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1271 | } |
| 1272 | return dentry; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1276 | * Call i_op->lookup on the dentry. The dentry must be negative but may be |
| 1277 | * hashed if it was pouplated with DCACHE_NEED_LOOKUP. |
| 1278 | * |
| 1279 | * dir->d_inode->i_mutex must be held |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1280 | */ |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1281 | static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1282 | unsigned int flags) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1283 | { |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1284 | struct dentry *old; |
| 1285 | |
| 1286 | /* Don't create child dentry for a dead directory. */ |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1287 | if (unlikely(IS_DEADDIR(dir))) { |
Miklos Szeredi | e188dc0 | 2012-02-03 14:25:18 +0100 | [diff] [blame] | 1288 | dput(dentry); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1289 | return ERR_PTR(-ENOENT); |
Miklos Szeredi | e188dc0 | 2012-02-03 14:25:18 +0100 | [diff] [blame] | 1290 | } |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1291 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1292 | old = dir->i_op->lookup(dir, dentry, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1293 | if (unlikely(old)) { |
| 1294 | dput(dentry); |
| 1295 | dentry = old; |
| 1296 | } |
| 1297 | return dentry; |
| 1298 | } |
| 1299 | |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1300 | static struct dentry *__lookup_hash(struct qstr *name, |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1301 | struct dentry *base, unsigned int flags) |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1302 | { |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1303 | bool need_lookup; |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1304 | struct dentry *dentry; |
| 1305 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1306 | dentry = lookup_dcache(name, base, flags, &need_lookup); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1307 | if (!need_lookup) |
| 1308 | return dentry; |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1309 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1310 | return lookup_real(base->d_inode, dentry, flags); |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1311 | } |
| 1312 | |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1313 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | * It's more convoluted than I'd like it to be, but... it's still fairly |
| 1315 | * small and for now I'd prefer to have fast path as straight as possible. |
| 1316 | * It _is_ time-critical. |
| 1317 | */ |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1318 | static int lookup_fast(struct nameidata *nd, struct qstr *name, |
| 1319 | struct path *path, struct inode **inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | { |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1321 | struct vfsmount *mnt = nd->path.mnt; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1322 | struct dentry *dentry, *parent = nd->path.dentry; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1323 | int need_reval = 1; |
| 1324 | int status = 1; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1325 | int err; |
| 1326 | |
Al Viro | 3cac260 | 2009-08-13 18:27:43 +0400 | [diff] [blame] | 1327 | /* |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1328 | * Rename seqlock is not required here because in the off chance |
| 1329 | * of a false negative due to a concurrent rename, we're going to |
| 1330 | * do the non-racy lookup, below. |
| 1331 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1332 | if (nd->flags & LOOKUP_RCU) { |
| 1333 | unsigned seq; |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1334 | dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1335 | if (!dentry) |
| 1336 | goto unlazy; |
| 1337 | |
Linus Torvalds | 12f8ad4 | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1338 | /* |
| 1339 | * This sequence count validates that the inode matches |
| 1340 | * the dentry name information from lookup. |
| 1341 | */ |
| 1342 | *inode = dentry->d_inode; |
| 1343 | if (read_seqcount_retry(&dentry->d_seq, seq)) |
| 1344 | return -ECHILD; |
| 1345 | |
| 1346 | /* |
| 1347 | * This sequence count validates that the parent had no |
| 1348 | * changes while we did the lookup of the dentry above. |
| 1349 | * |
| 1350 | * The memory barrier in read_seqcount_begin of child is |
| 1351 | * enough, we can use __read_seqcount_retry here. |
| 1352 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1353 | if (__read_seqcount_retry(&parent->d_seq, nd->seq)) |
| 1354 | return -ECHILD; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1355 | nd->seq = seq; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1356 | |
Miklos Szeredi | fa4ee15 | 2012-03-26 12:54:19 +0200 | [diff] [blame] | 1357 | if (unlikely(d_need_lookup(dentry))) |
| 1358 | goto unlazy; |
Al Viro | 2464308 | 2011-02-15 01:26:22 -0500 | [diff] [blame] | 1359 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 1360 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1361 | if (unlikely(status <= 0)) { |
| 1362 | if (status != -ECHILD) |
| 1363 | need_reval = 0; |
| 1364 | goto unlazy; |
| 1365 | } |
Al Viro | 2464308 | 2011-02-15 01:26:22 -0500 | [diff] [blame] | 1366 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1367 | path->mnt = mnt; |
| 1368 | path->dentry = dentry; |
Al Viro | d6e9bd2 | 2011-05-27 07:03:15 -0400 | [diff] [blame] | 1369 | if (unlikely(!__follow_mount_rcu(nd, path, inode))) |
| 1370 | goto unlazy; |
| 1371 | if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) |
| 1372 | goto unlazy; |
| 1373 | return 0; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1374 | unlazy: |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 1375 | if (unlazy_walk(nd, dentry)) |
| 1376 | return -ECHILD; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1377 | } else { |
| 1378 | dentry = __d_lookup(parent, name); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1379 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1380 | |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1381 | if (unlikely(!dentry)) |
| 1382 | goto need_lookup; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1383 | |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1384 | if (unlikely(d_need_lookup(dentry))) { |
| 1385 | dput(dentry); |
| 1386 | goto need_lookup; |
Al Viro | 2464308 | 2011-02-15 01:26:22 -0500 | [diff] [blame] | 1387 | } |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1388 | |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1389 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) |
Al Viro | 4ce16ef3 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 1390 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1391 | if (unlikely(status <= 0)) { |
| 1392 | if (status < 0) { |
| 1393 | dput(dentry); |
| 1394 | return status; |
| 1395 | } |
| 1396 | if (!d_invalidate(dentry)) { |
| 1397 | dput(dentry); |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1398 | goto need_lookup; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1399 | } |
| 1400 | } |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1401 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1402 | path->mnt = mnt; |
| 1403 | path->dentry = dentry; |
| 1404 | err = follow_managed(path, nd->flags); |
Ian Kent | 8931221 | 2011-01-18 12:06:10 +0800 | [diff] [blame] | 1405 | if (unlikely(err < 0)) { |
| 1406 | path_put_conditional(path, nd); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1407 | return err; |
Ian Kent | 8931221 | 2011-01-18 12:06:10 +0800 | [diff] [blame] | 1408 | } |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 1409 | if (err) |
| 1410 | nd->flags |= LOOKUP_JUMPED; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1411 | *inode = path->dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | return 0; |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1413 | |
| 1414 | need_lookup: |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1415 | return 1; |
| 1416 | } |
| 1417 | |
| 1418 | /* Fast lookup failed, do it the slow way */ |
| 1419 | static int lookup_slow(struct nameidata *nd, struct qstr *name, |
| 1420 | struct path *path) |
| 1421 | { |
| 1422 | struct dentry *dentry, *parent; |
| 1423 | int err; |
| 1424 | |
| 1425 | parent = nd->path.dentry; |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1426 | BUG_ON(nd->inode != parent->d_inode); |
| 1427 | |
| 1428 | mutex_lock(&parent->d_inode->i_mutex); |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1429 | dentry = __lookup_hash(name, parent, nd->flags); |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1430 | mutex_unlock(&parent->d_inode->i_mutex); |
| 1431 | if (IS_ERR(dentry)) |
| 1432 | return PTR_ERR(dentry); |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1433 | path->mnt = nd->path.mnt; |
| 1434 | path->dentry = dentry; |
| 1435 | err = follow_managed(path, nd->flags); |
| 1436 | if (unlikely(err < 0)) { |
| 1437 | path_put_conditional(path, nd); |
| 1438 | return err; |
| 1439 | } |
| 1440 | if (err) |
| 1441 | nd->flags |= LOOKUP_JUMPED; |
| 1442 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1445 | static inline int may_lookup(struct nameidata *nd) |
| 1446 | { |
| 1447 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | 4ad5abb | 2011-06-20 19:57:03 -0400 | [diff] [blame] | 1448 | int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1449 | if (err != -ECHILD) |
| 1450 | return err; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 1451 | if (unlazy_walk(nd, NULL)) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1452 | return -ECHILD; |
| 1453 | } |
Al Viro | 4ad5abb | 2011-06-20 19:57:03 -0400 | [diff] [blame] | 1454 | return inode_permission(nd->inode, MAY_EXEC); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1455 | } |
| 1456 | |
Al Viro | 9856fa1 | 2011-03-04 14:22:06 -0500 | [diff] [blame] | 1457 | static inline int handle_dots(struct nameidata *nd, int type) |
| 1458 | { |
| 1459 | if (type == LAST_DOTDOT) { |
| 1460 | if (nd->flags & LOOKUP_RCU) { |
| 1461 | if (follow_dotdot_rcu(nd)) |
| 1462 | return -ECHILD; |
| 1463 | } else |
| 1464 | follow_dotdot(nd); |
| 1465 | } |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
Al Viro | 951361f | 2011-03-04 14:44:37 -0500 | [diff] [blame] | 1469 | static void terminate_walk(struct nameidata *nd) |
| 1470 | { |
| 1471 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1472 | path_put(&nd->path); |
| 1473 | } else { |
| 1474 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1475 | if (!(nd->flags & LOOKUP_ROOT)) |
| 1476 | nd->root.mnt = NULL; |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1477 | unlock_rcu_walk(); |
Al Viro | 951361f | 2011-03-04 14:44:37 -0500 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1481 | /* |
| 1482 | * Do we need to follow links? We _really_ want to be able |
| 1483 | * to do this check without having to look at inode->i_op, |
| 1484 | * so we keep a cache of "no, this doesn't need follow_link" |
| 1485 | * for the common case. |
| 1486 | */ |
Linus Torvalds | 7813b94 | 2011-08-07 09:53:20 -0700 | [diff] [blame] | 1487 | static inline int should_follow_link(struct inode *inode, int follow) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1488 | { |
| 1489 | if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { |
| 1490 | if (likely(inode->i_op->follow_link)) |
| 1491 | return follow; |
| 1492 | |
| 1493 | /* This gets set once for the inode lifetime */ |
| 1494 | spin_lock(&inode->i_lock); |
| 1495 | inode->i_opflags |= IOP_NOFOLLOW; |
| 1496 | spin_unlock(&inode->i_lock); |
| 1497 | } |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1501 | static inline int walk_component(struct nameidata *nd, struct path *path, |
| 1502 | struct qstr *name, int type, int follow) |
| 1503 | { |
| 1504 | struct inode *inode; |
| 1505 | int err; |
| 1506 | /* |
| 1507 | * "." and ".." are special - ".." especially so because it has |
| 1508 | * to be able to know about the current root directory and |
| 1509 | * parent relationships. |
| 1510 | */ |
| 1511 | if (unlikely(type != LAST_NORM)) |
| 1512 | return handle_dots(nd, type); |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1513 | err = lookup_fast(nd, name, path, &inode); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1514 | if (unlikely(err)) { |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1515 | if (err < 0) |
| 1516 | goto out_err; |
| 1517 | |
| 1518 | err = lookup_slow(nd, name, path); |
| 1519 | if (err < 0) |
| 1520 | goto out_err; |
| 1521 | |
| 1522 | inode = path->dentry->d_inode; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1523 | } |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1524 | err = -ENOENT; |
| 1525 | if (!inode) |
| 1526 | goto out_path_put; |
| 1527 | |
Linus Torvalds | 7813b94 | 2011-08-07 09:53:20 -0700 | [diff] [blame] | 1528 | if (should_follow_link(inode, follow)) { |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 1529 | if (nd->flags & LOOKUP_RCU) { |
| 1530 | if (unlikely(unlazy_walk(nd, path->dentry))) { |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1531 | err = -ECHILD; |
| 1532 | goto out_err; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 1533 | } |
| 1534 | } |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1535 | BUG_ON(inode != path->dentry->d_inode); |
| 1536 | return 1; |
| 1537 | } |
| 1538 | path_to_nameidata(path, nd); |
| 1539 | nd->inode = inode; |
| 1540 | return 0; |
Miklos Szeredi | 697f514 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1541 | |
| 1542 | out_path_put: |
| 1543 | path_to_nameidata(path, nd); |
| 1544 | out_err: |
| 1545 | terminate_walk(nd); |
| 1546 | return err; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1547 | } |
| 1548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | /* |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1550 | * This limits recursive symlink follows to 8, while |
| 1551 | * limiting consecutive symlinks to 40. |
| 1552 | * |
| 1553 | * Without that kind of total limit, nasty chains of consecutive |
| 1554 | * symlinks can cause almost arbitrarily long lookups. |
| 1555 | */ |
| 1556 | static inline int nested_symlink(struct path *path, struct nameidata *nd) |
| 1557 | { |
| 1558 | int res; |
| 1559 | |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1560 | if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { |
| 1561 | path_put_conditional(path, nd); |
| 1562 | path_put(&nd->path); |
| 1563 | return -ELOOP; |
| 1564 | } |
Erez Zadok | 1a4022f | 2011-05-21 01:19:59 -0400 | [diff] [blame] | 1565 | BUG_ON(nd->depth >= MAX_NESTED_LINKS); |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1566 | |
| 1567 | nd->depth++; |
| 1568 | current->link_count++; |
| 1569 | |
| 1570 | do { |
| 1571 | struct path link = *path; |
| 1572 | void *cookie; |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1573 | |
| 1574 | res = follow_link(&link, nd, &cookie); |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 1575 | if (res) |
| 1576 | break; |
| 1577 | res = walk_component(nd, path, &nd->last, |
| 1578 | nd->last_type, LOOKUP_FOLLOW); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1579 | put_link(nd, &link, cookie); |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1580 | } while (res > 0); |
| 1581 | |
| 1582 | current->link_count--; |
| 1583 | nd->depth--; |
| 1584 | return res; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1588 | * We really don't want to look at inode->i_op->lookup |
| 1589 | * when we don't have to. So we keep a cache bit in |
| 1590 | * the inode ->i_opflags field that says "yes, we can |
| 1591 | * do lookup on this inode". |
| 1592 | */ |
| 1593 | static inline int can_lookup(struct inode *inode) |
| 1594 | { |
| 1595 | if (likely(inode->i_opflags & IOP_LOOKUP)) |
| 1596 | return 1; |
| 1597 | if (likely(!inode->i_op->lookup)) |
| 1598 | return 0; |
| 1599 | |
| 1600 | /* We do this once for the lifetime of the inode */ |
| 1601 | spin_lock(&inode->i_lock); |
| 1602 | inode->i_opflags |= IOP_LOOKUP; |
| 1603 | spin_unlock(&inode->i_lock); |
| 1604 | return 1; |
| 1605 | } |
| 1606 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1607 | /* |
| 1608 | * We can do the critical dentry name comparison and hashing |
| 1609 | * operations one word at a time, but we are limited to: |
| 1610 | * |
| 1611 | * - Architectures with fast unaligned word accesses. We could |
| 1612 | * do a "get_unaligned()" if this helps and is sufficiently |
| 1613 | * fast. |
| 1614 | * |
| 1615 | * - Little-endian machines (so that we can generate the mask |
| 1616 | * of low bytes efficiently). Again, we *could* do a byte |
| 1617 | * swapping load on big-endian architectures if that is not |
| 1618 | * expensive enough to make the optimization worthless. |
| 1619 | * |
| 1620 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
| 1621 | * do not trap on the (extremely unlikely) case of a page |
| 1622 | * crossing operation. |
| 1623 | * |
| 1624 | * - Furthermore, we need an efficient 64-bit compile for the |
| 1625 | * 64-bit case in order to generate the "number of bytes in |
| 1626 | * the final mask". Again, that could be replaced with a |
| 1627 | * efficient population count instruction or similar. |
| 1628 | */ |
| 1629 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
| 1630 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1631 | #include <asm/word-at-a-time.h> |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1632 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1633 | #ifdef CONFIG_64BIT |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1634 | |
| 1635 | static inline unsigned int fold_hash(unsigned long hash) |
| 1636 | { |
| 1637 | hash += hash >> (8*sizeof(int)); |
| 1638 | return hash; |
| 1639 | } |
| 1640 | |
| 1641 | #else /* 32-bit case */ |
| 1642 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1643 | #define fold_hash(x) (x) |
| 1644 | |
| 1645 | #endif |
| 1646 | |
| 1647 | unsigned int full_name_hash(const unsigned char *name, unsigned int len) |
| 1648 | { |
| 1649 | unsigned long a, mask; |
| 1650 | unsigned long hash = 0; |
| 1651 | |
| 1652 | for (;;) { |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 1653 | a = load_unaligned_zeropad(name); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1654 | if (len < sizeof(unsigned long)) |
| 1655 | break; |
| 1656 | hash += a; |
Al Viro | f132c5b | 2012-03-22 21:59:52 +0000 | [diff] [blame] | 1657 | hash *= 9; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1658 | name += sizeof(unsigned long); |
| 1659 | len -= sizeof(unsigned long); |
| 1660 | if (!len) |
| 1661 | goto done; |
| 1662 | } |
| 1663 | mask = ~(~0ul << len*8); |
| 1664 | hash += mask & a; |
| 1665 | done: |
| 1666 | return fold_hash(hash); |
| 1667 | } |
| 1668 | EXPORT_SYMBOL(full_name_hash); |
| 1669 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1670 | /* |
| 1671 | * Calculate the length and hash of the path component, and |
| 1672 | * return the length of the component; |
| 1673 | */ |
| 1674 | static inline unsigned long hash_name(const char *name, unsigned int *hashp) |
| 1675 | { |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 1676 | unsigned long a, b, adata, bdata, mask, hash, len; |
| 1677 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1678 | |
| 1679 | hash = a = 0; |
| 1680 | len = -sizeof(unsigned long); |
| 1681 | do { |
| 1682 | hash = (hash + a) * 9; |
| 1683 | len += sizeof(unsigned long); |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 1684 | a = load_unaligned_zeropad(name+len); |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 1685 | b = a ^ REPEAT_BYTE('/'); |
| 1686 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1687 | |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 1688 | adata = prep_zero_mask(a, adata, &constants); |
| 1689 | bdata = prep_zero_mask(b, bdata, &constants); |
| 1690 | |
| 1691 | mask = create_zero_mask(adata | bdata); |
| 1692 | |
| 1693 | hash += a & zero_bytemask(mask); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1694 | *hashp = fold_hash(hash); |
| 1695 | |
Linus Torvalds | 36126f8 | 2012-05-26 10:43:17 -0700 | [diff] [blame] | 1696 | return len + find_zero(mask); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | #else |
| 1700 | |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 1701 | unsigned int full_name_hash(const unsigned char *name, unsigned int len) |
| 1702 | { |
| 1703 | unsigned long hash = init_name_hash(); |
| 1704 | while (len--) |
| 1705 | hash = partial_name_hash(*name++, hash); |
| 1706 | return end_name_hash(hash); |
| 1707 | } |
Linus Torvalds | ae942ae | 2012-03-02 19:40:57 -0800 | [diff] [blame] | 1708 | EXPORT_SYMBOL(full_name_hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 1709 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1710 | /* |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1711 | * We know there's a real path component here of at least |
| 1712 | * one character. |
| 1713 | */ |
| 1714 | static inline unsigned long hash_name(const char *name, unsigned int *hashp) |
| 1715 | { |
| 1716 | unsigned long hash = init_name_hash(); |
| 1717 | unsigned long len = 0, c; |
| 1718 | |
| 1719 | c = (unsigned char)*name; |
| 1720 | do { |
| 1721 | len++; |
| 1722 | hash = partial_name_hash(c, hash); |
| 1723 | c = (unsigned char)name[len]; |
| 1724 | } while (c && c != '/'); |
| 1725 | *hashp = end_name_hash(hash); |
| 1726 | return len; |
| 1727 | } |
| 1728 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1729 | #endif |
| 1730 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1731 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | * Name resolution. |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1733 | * This is the basic name resolution function, turning a pathname into |
| 1734 | * the final dentry. We expect 'base' to be positive and a directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | * |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1736 | * Returns 0 and nd will have valid dentry and mnt on success. |
| 1737 | * Returns error and drops reference to input namei data on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | */ |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 1739 | static int link_path_walk(const char *name, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | { |
| 1741 | struct path next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
| 1744 | while (*name=='/') |
| 1745 | name++; |
| 1746 | if (!*name) |
Al Viro | 086e183 | 2011-02-22 20:56:27 -0500 | [diff] [blame] | 1747 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | /* At this point we know we have a real path component. */ |
| 1750 | for(;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | struct qstr this; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1752 | long len; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1753 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1755 | err = may_lookup(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | if (err) |
| 1757 | break; |
| 1758 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1759 | len = hash_name(name, &this.hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | this.name = name; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1761 | this.len = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1763 | type = LAST_NORM; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1764 | if (name[0] == '.') switch (len) { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1765 | case 2: |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1766 | if (name[1] == '.') { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1767 | type = LAST_DOTDOT; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 1768 | nd->flags |= LOOKUP_JUMPED; |
| 1769 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1770 | break; |
| 1771 | case 1: |
| 1772 | type = LAST_DOT; |
| 1773 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 1774 | if (likely(type == LAST_NORM)) { |
| 1775 | struct dentry *parent = nd->path.dentry; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 1776 | nd->flags &= ~LOOKUP_JUMPED; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 1777 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
| 1778 | err = parent->d_op->d_hash(parent, nd->inode, |
| 1779 | &this); |
| 1780 | if (err < 0) |
| 1781 | break; |
| 1782 | } |
| 1783 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1784 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1785 | if (!name[len]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | goto last_component; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1787 | /* |
| 1788 | * If it wasn't NUL, we know it was '/'. Skip that |
| 1789 | * slash, and continue until no more slashes. |
| 1790 | */ |
| 1791 | do { |
| 1792 | len++; |
| 1793 | } while (unlikely(name[len] == '/')); |
| 1794 | if (!name[len]) |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1795 | goto last_component; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1796 | name += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1798 | err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); |
| 1799 | if (err < 0) |
| 1800 | return err; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1801 | |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1802 | if (err) { |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1803 | err = nested_symlink(&next, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | if (err) |
Al Viro | a7472ba | 2011-03-04 14:39:30 -0500 | [diff] [blame] | 1805 | return err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1806 | } |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1807 | if (can_lookup(nd->inode)) |
| 1808 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | err = -ENOTDIR; |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1810 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | /* here ends the main loop */ |
| 1812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | last_component: |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1814 | nd->last = this; |
| 1815 | nd->last_type = type; |
Al Viro | 086e183 | 2011-02-22 20:56:27 -0500 | [diff] [blame] | 1816 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | } |
Al Viro | 951361f | 2011-03-04 14:44:37 -0500 | [diff] [blame] | 1818 | terminate_walk(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | return err; |
| 1820 | } |
| 1821 | |
Al Viro | 70e9b35 | 2011-03-05 21:12:22 -0500 | [diff] [blame] | 1822 | static int path_init(int dfd, const char *name, unsigned int flags, |
| 1823 | struct nameidata *nd, struct file **fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | { |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1825 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | |
| 1827 | nd->last_type = LAST_ROOT; /* if there are only slashes... */ |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 1828 | nd->flags = flags | LOOKUP_JUMPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | nd->depth = 0; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1830 | if (flags & LOOKUP_ROOT) { |
| 1831 | struct inode *inode = nd->root.dentry->d_inode; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 1832 | if (*name) { |
| 1833 | if (!inode->i_op->lookup) |
| 1834 | return -ENOTDIR; |
| 1835 | retval = inode_permission(inode, MAY_EXEC); |
| 1836 | if (retval) |
| 1837 | return retval; |
| 1838 | } |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1839 | nd->path = nd->root; |
| 1840 | nd->inode = inode; |
| 1841 | if (flags & LOOKUP_RCU) { |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1842 | lock_rcu_walk(); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1843 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 1844 | } else { |
| 1845 | path_get(&nd->path); |
| 1846 | } |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 1850 | nd->root.mnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | if (*name=='/') { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1853 | if (flags & LOOKUP_RCU) { |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1854 | lock_rcu_walk(); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1855 | set_root_rcu(nd); |
| 1856 | } else { |
| 1857 | set_root(nd); |
| 1858 | path_get(&nd->root); |
| 1859 | } |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 1860 | nd->path = nd->root; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1861 | } else if (dfd == AT_FDCWD) { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1862 | if (flags & LOOKUP_RCU) { |
| 1863 | struct fs_struct *fs = current->fs; |
| 1864 | unsigned seq; |
| 1865 | |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1866 | lock_rcu_walk(); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1867 | |
| 1868 | do { |
| 1869 | seq = read_seqcount_begin(&fs->seq); |
| 1870 | nd->path = fs->pwd; |
| 1871 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 1872 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 1873 | } else { |
| 1874 | get_fs_pwd(current->fs, &nd->path); |
| 1875 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1876 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1877 | struct fd f = fdget_raw(dfd); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1878 | struct dentry *dentry; |
| 1879 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1880 | if (!f.file) |
| 1881 | return -EBADF; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1882 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1883 | dentry = f.file->f_path.dentry; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1884 | |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 1885 | if (*name) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1886 | if (!S_ISDIR(dentry->d_inode->i_mode)) { |
| 1887 | fdput(f); |
| 1888 | return -ENOTDIR; |
| 1889 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1890 | |
Al Viro | 4ad5abb | 2011-06-20 19:57:03 -0400 | [diff] [blame] | 1891 | retval = inode_permission(dentry->d_inode, MAY_EXEC); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1892 | if (retval) { |
| 1893 | fdput(f); |
| 1894 | return retval; |
| 1895 | } |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 1896 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1897 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1898 | nd->path = f.file->f_path; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1899 | if (flags & LOOKUP_RCU) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1900 | if (f.need_put) |
| 1901 | *fp = f.file; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1902 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 32a7991 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1903 | lock_rcu_walk(); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1904 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1905 | path_get(&nd->path); |
| 1906 | fdput(f); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | } |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1909 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1910 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 1911 | return 0; |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 1912 | } |
| 1913 | |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1914 | static inline int lookup_last(struct nameidata *nd, struct path *path) |
| 1915 | { |
| 1916 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) |
| 1917 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 1918 | |
| 1919 | nd->flags &= ~LOOKUP_PARENT; |
| 1920 | return walk_component(nd, path, &nd->last, nd->last_type, |
| 1921 | nd->flags & LOOKUP_FOLLOW); |
| 1922 | } |
| 1923 | |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 1924 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 1925 | static int path_lookupat(int dfd, const char *name, |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 1926 | unsigned int flags, struct nameidata *nd) |
| 1927 | { |
Al Viro | 70e9b35 | 2011-03-05 21:12:22 -0500 | [diff] [blame] | 1928 | struct file *base = NULL; |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1929 | struct path path; |
| 1930 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1931 | |
| 1932 | /* |
| 1933 | * Path walking is largely split up into 2 different synchronisation |
| 1934 | * schemes, rcu-walk and ref-walk (explained in |
| 1935 | * Documentation/filesystems/path-lookup.txt). These share much of the |
| 1936 | * path walk code, but some things particularly setup, cleanup, and |
| 1937 | * following mounts are sufficiently divergent that functions are |
| 1938 | * duplicated. Typically there is a function foo(), and its RCU |
| 1939 | * analogue, foo_rcu(). |
| 1940 | * |
| 1941 | * -ECHILD is the error number of choice (just to avoid clashes) that |
| 1942 | * is returned if some aspect of an rcu-walk fails. Such an error must |
| 1943 | * be handled by restarting a traditional ref-walk (which will always |
| 1944 | * be able to complete). |
| 1945 | */ |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1946 | err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 1947 | |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1948 | if (unlikely(err)) |
| 1949 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 1950 | |
| 1951 | current->total_link_count = 0; |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1952 | err = link_path_walk(name, nd); |
| 1953 | |
| 1954 | if (!err && !(flags & LOOKUP_PARENT)) { |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1955 | err = lookup_last(nd, &path); |
| 1956 | while (err > 0) { |
| 1957 | void *cookie; |
| 1958 | struct path link = path; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 1959 | err = may_follow_link(&link, nd); |
| 1960 | if (unlikely(err)) |
| 1961 | break; |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1962 | nd->flags |= LOOKUP_PARENT; |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1963 | err = follow_link(&link, nd, &cookie); |
Al Viro | 6d7b5aa | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 1964 | if (err) |
| 1965 | break; |
| 1966 | err = lookup_last(nd, &path); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1967 | put_link(nd, &link, cookie); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1968 | } |
| 1969 | } |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 1970 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 1971 | if (!err) |
| 1972 | err = complete_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1973 | |
| 1974 | if (!err && nd->flags & LOOKUP_DIRECTORY) { |
| 1975 | if (!nd->inode->i_op->lookup) { |
| 1976 | path_put(&nd->path); |
Al Viro | bd23a53 | 2011-03-23 09:56:30 -0400 | [diff] [blame] | 1977 | err = -ENOTDIR; |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1978 | } |
| 1979 | } |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 1980 | |
Al Viro | 70e9b35 | 2011-03-05 21:12:22 -0500 | [diff] [blame] | 1981 | if (base) |
| 1982 | fput(base); |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 1983 | |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1984 | if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 1985 | path_put(&nd->root); |
| 1986 | nd->root.mnt = NULL; |
| 1987 | } |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1988 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 1989 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1990 | |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 1991 | static int filename_lookup(int dfd, struct filename *name, |
| 1992 | unsigned int flags, struct nameidata *nd) |
| 1993 | { |
| 1994 | int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd); |
| 1995 | if (unlikely(retval == -ECHILD)) |
| 1996 | retval = path_lookupat(dfd, name->name, flags, nd); |
| 1997 | if (unlikely(retval == -ESTALE)) |
| 1998 | retval = path_lookupat(dfd, name->name, |
| 1999 | flags | LOOKUP_REVAL, nd); |
| 2000 | |
| 2001 | if (likely(!retval)) |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame^] | 2002 | audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2003 | return retval; |
| 2004 | } |
| 2005 | |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2006 | static int do_path_lookup(int dfd, const char *name, |
| 2007 | unsigned int flags, struct nameidata *nd) |
| 2008 | { |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2009 | struct filename filename = { .name = name }; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2010 | |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2011 | return filename_lookup(dfd, &filename, flags, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2014 | /* does lookup, returns the object with parent locked */ |
| 2015 | struct dentry *kern_path_locked(const char *name, struct path *path) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2016 | { |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2017 | struct nameidata nd; |
| 2018 | struct dentry *d; |
| 2019 | int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); |
| 2020 | if (err) |
| 2021 | return ERR_PTR(err); |
| 2022 | if (nd.last_type != LAST_NORM) { |
| 2023 | path_put(&nd.path); |
| 2024 | return ERR_PTR(-EINVAL); |
| 2025 | } |
| 2026 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
Al Viro | 1e0ea00 | 2012-07-22 23:46:21 +0400 | [diff] [blame] | 2027 | d = __lookup_hash(&nd.last, nd.path.dentry, 0); |
Al Viro | 79714f7 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2028 | if (IS_ERR(d)) { |
| 2029 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 2030 | path_put(&nd.path); |
| 2031 | return d; |
| 2032 | } |
| 2033 | *path = nd.path; |
| 2034 | return d; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2035 | } |
| 2036 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2037 | int kern_path(const char *name, unsigned int flags, struct path *path) |
| 2038 | { |
| 2039 | struct nameidata nd; |
| 2040 | int res = do_path_lookup(AT_FDCWD, name, flags, &nd); |
| 2041 | if (!res) |
| 2042 | *path = nd.path; |
| 2043 | return res; |
| 2044 | } |
| 2045 | |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2046 | /** |
| 2047 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair |
| 2048 | * @dentry: pointer to dentry of the base directory |
| 2049 | * @mnt: pointer to vfs mount of the base directory |
| 2050 | * @name: pointer to file name |
| 2051 | * @flags: lookup flags |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2052 | * @path: pointer to struct path to fill |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2053 | */ |
| 2054 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, |
| 2055 | const char *name, unsigned int flags, |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2056 | struct path *path) |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2057 | { |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2058 | struct nameidata nd; |
| 2059 | int err; |
| 2060 | nd.root.dentry = dentry; |
| 2061 | nd.root.mnt = mnt; |
| 2062 | BUG_ON(flags & LOOKUP_PARENT); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2063 | /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2064 | err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); |
| 2065 | if (!err) |
| 2066 | *path = nd.path; |
| 2067 | return err; |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2070 | /* |
| 2071 | * Restricted form of lookup. Doesn't follow links, single-component only, |
| 2072 | * needs parent already locked. Doesn't follow mounts. |
| 2073 | * SMP-safe. |
| 2074 | */ |
Adrian Bunk | a244e16 | 2006-03-31 02:32:11 -0800 | [diff] [blame] | 2075 | static struct dentry *lookup_hash(struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | { |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 2077 | return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2080 | /** |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2081 | * lookup_one_len - filesystem helper to lookup single pathname component |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2082 | * @name: pathname component to lookup |
| 2083 | * @base: base directory to lookup from |
| 2084 | * @len: maximum length @len should be interpreted to |
| 2085 | * |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2086 | * Note that this routine is purely a helper for filesystem usage and should |
| 2087 | * 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] | 2088 | * nameidata argument is passed to the filesystem methods and a filesystem |
| 2089 | * using this helper needs to be prepared for that. |
| 2090 | */ |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2091 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
| 2092 | { |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2093 | struct qstr this; |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2094 | unsigned int c; |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2095 | int err; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2096 | |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2097 | WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); |
| 2098 | |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2099 | this.name = name; |
| 2100 | this.len = len; |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2101 | this.hash = full_name_hash(name, len); |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2102 | if (!len) |
| 2103 | return ERR_PTR(-EACCES); |
| 2104 | |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2105 | while (len--) { |
| 2106 | c = *(const unsigned char *)name++; |
| 2107 | if (c == '/' || c == '\0') |
| 2108 | return ERR_PTR(-EACCES); |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2109 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2110 | /* |
| 2111 | * See if the low-level filesystem might want |
| 2112 | * to use its own hash.. |
| 2113 | */ |
| 2114 | if (base->d_flags & DCACHE_OP_HASH) { |
| 2115 | int err = base->d_op->d_hash(base, base->d_inode, &this); |
| 2116 | if (err < 0) |
| 2117 | return ERR_PTR(err); |
| 2118 | } |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2119 | |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2120 | err = inode_permission(base->d_inode, MAY_EXEC); |
| 2121 | if (err) |
| 2122 | return ERR_PTR(err); |
| 2123 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 2124 | return __lookup_hash(&this, base, 0); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2127 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
| 2128 | struct path *path, int *empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2130 | struct nameidata nd; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2131 | struct filename *tmp = getname_flags(name, flags, empty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | int err = PTR_ERR(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | if (!IS_ERR(tmp)) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2134 | |
| 2135 | BUG_ON(flags & LOOKUP_PARENT); |
| 2136 | |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2137 | err = filename_lookup(dfd, tmp, flags, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | putname(tmp); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2139 | if (!err) |
| 2140 | *path = nd.path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | } |
| 2142 | return err; |
| 2143 | } |
| 2144 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2145 | int user_path_at(int dfd, const char __user *name, unsigned flags, |
| 2146 | struct path *path) |
| 2147 | { |
Linus Torvalds | f7493e5 | 2012-03-22 16:10:40 -0700 | [diff] [blame] | 2148 | return user_path_at_empty(dfd, name, flags, path, NULL); |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2149 | } |
| 2150 | |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2151 | /* |
| 2152 | * NB: most callers don't do anything directly with the reference to the |
| 2153 | * to struct filename, but the nd->last pointer points into the name string |
| 2154 | * allocated by getname. So we must hold the reference to it until all |
| 2155 | * path-walking is complete. |
| 2156 | */ |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2157 | static struct filename * |
| 2158 | user_path_parent(int dfd, const char __user *path, struct nameidata *nd) |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2159 | { |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2160 | struct filename *s = getname(path); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2161 | int error; |
| 2162 | |
| 2163 | if (IS_ERR(s)) |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2164 | return s; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2165 | |
Jeff Layton | 873f1ee | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2166 | error = filename_lookup(dfd, s, LOOKUP_PARENT, nd); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2167 | if (error) { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2168 | putname(s); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2169 | return ERR_PTR(error); |
| 2170 | } |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2171 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2172 | return s; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2173 | } |
| 2174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | /* |
| 2176 | * It's inline, so penalty for filesystems that don't use sticky bit is |
| 2177 | * minimal. |
| 2178 | */ |
| 2179 | static inline int check_sticky(struct inode *dir, struct inode *inode) |
| 2180 | { |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2181 | kuid_t fsuid = current_fsuid(); |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | if (!(dir->i_mode & S_ISVTX)) |
| 2184 | return 0; |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2185 | if (uid_eq(inode->i_uid, fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | return 0; |
Eric W. Biederman | 8e96e3b | 2012-03-03 21:17:15 -0800 | [diff] [blame] | 2187 | if (uid_eq(dir->i_uid, fsuid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | return 0; |
Eric W. Biederman | 1a48e2a | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 2189 | return !inode_capable(inode, CAP_FOWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | * Check whether we can remove a link victim from directory dir, check |
| 2194 | * whether the type of victim is right. |
| 2195 | * 1. We can't do it if dir is read-only (done in permission()) |
| 2196 | * 2. We should have write and exec permissions on dir |
| 2197 | * 3. We can't remove anything from append-only dir |
| 2198 | * 4. We can't do anything with immutable dir (done in permission()) |
| 2199 | * 5. If the sticky bit on dir is set we should either |
| 2200 | * a. be owner of dir, or |
| 2201 | * b. be owner of victim, or |
| 2202 | * c. have CAP_FOWNER capability |
| 2203 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 2204 | * links pointing to it. |
| 2205 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 2206 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 2207 | * 9. We can't remove a root or mountpoint. |
| 2208 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 2209 | * nfs_async_unlink(). |
| 2210 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 2211 | static int may_delete(struct inode *dir,struct dentry *victim,int isdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | { |
| 2213 | int error; |
| 2214 | |
| 2215 | if (!victim->d_inode) |
| 2216 | return -ENOENT; |
| 2217 | |
| 2218 | BUG_ON(victim->d_parent->d_inode != dir); |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 2219 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 2221 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | if (error) |
| 2223 | return error; |
| 2224 | if (IS_APPEND(dir)) |
| 2225 | return -EPERM; |
| 2226 | if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| |
Hugh Dickins | f945454 | 2008-11-19 15:36:38 -0800 | [diff] [blame] | 2227 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | return -EPERM; |
| 2229 | if (isdir) { |
| 2230 | if (!S_ISDIR(victim->d_inode->i_mode)) |
| 2231 | return -ENOTDIR; |
| 2232 | if (IS_ROOT(victim)) |
| 2233 | return -EBUSY; |
| 2234 | } else if (S_ISDIR(victim->d_inode->i_mode)) |
| 2235 | return -EISDIR; |
| 2236 | if (IS_DEADDIR(dir)) |
| 2237 | return -ENOENT; |
| 2238 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 2239 | return -EBUSY; |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | /* Check whether we can create an object with dentry child in directory |
| 2244 | * dir. |
| 2245 | * 1. We can't do it if child already exists (open has special treatment for |
| 2246 | * this case, but since we are inlined it's OK) |
| 2247 | * 2. We can't do it if dir is read-only (done in permission()) |
| 2248 | * 3. We should have write and exec permissions on dir |
| 2249 | * 4. We can't do it if dir is immutable (done in permission()) |
| 2250 | */ |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 2251 | static inline int may_create(struct inode *dir, struct dentry *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | { |
| 2253 | if (child->d_inode) |
| 2254 | return -EEXIST; |
| 2255 | if (IS_DEADDIR(dir)) |
| 2256 | return -ENOENT; |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 2257 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | /* |
| 2261 | * p1 and p2 should be directories on the same fs. |
| 2262 | */ |
| 2263 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) |
| 2264 | { |
| 2265 | struct dentry *p; |
| 2266 | |
| 2267 | if (p1 == p2) { |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 2268 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | return NULL; |
| 2270 | } |
| 2271 | |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 2272 | mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2274 | p = d_ancestor(p2, p1); |
| 2275 | if (p) { |
| 2276 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); |
| 2277 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); |
| 2278 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | } |
| 2280 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2281 | p = d_ancestor(p1, p2); |
| 2282 | if (p) { |
| 2283 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
| 2284 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); |
| 2285 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | } |
| 2287 | |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 2288 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
| 2289 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | return NULL; |
| 2291 | } |
| 2292 | |
| 2293 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
| 2294 | { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2295 | mutex_unlock(&p1->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | if (p1 != p2) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2297 | mutex_unlock(&p2->d_inode->i_mutex); |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 2298 | mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | } |
| 2300 | } |
| 2301 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2302 | int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2303 | bool want_excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 2305 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | if (error) |
| 2307 | return error; |
| 2308 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 2309 | if (!dir->i_op->create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | return -EACCES; /* shouldn't it be ENOSYS? */ |
| 2311 | mode &= S_IALLUGO; |
| 2312 | mode |= S_IFREG; |
| 2313 | error = security_inode_create(dir, dentry, mode); |
| 2314 | if (error) |
| 2315 | return error; |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2316 | error = dir->i_op->create(dir, dentry, mode, want_excl); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 2317 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 2318 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | return error; |
| 2320 | } |
| 2321 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2322 | static int may_open(struct path *path, int acc_mode, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | { |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 2324 | struct dentry *dentry = path->dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | struct inode *inode = dentry->d_inode; |
| 2326 | int error; |
| 2327 | |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2328 | /* O_PATH? */ |
| 2329 | if (!acc_mode) |
| 2330 | return 0; |
| 2331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | if (!inode) |
| 2333 | return -ENOENT; |
| 2334 | |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2335 | switch (inode->i_mode & S_IFMT) { |
| 2336 | case S_IFLNK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | return -ELOOP; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2338 | case S_IFDIR: |
| 2339 | if (acc_mode & MAY_WRITE) |
| 2340 | return -EISDIR; |
| 2341 | break; |
| 2342 | case S_IFBLK: |
| 2343 | case S_IFCHR: |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 2344 | if (path->mnt->mnt_flags & MNT_NODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | return -EACCES; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2346 | /*FALLTHRU*/ |
| 2347 | case S_IFIFO: |
| 2348 | case S_IFSOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | flag &= ~O_TRUNC; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2350 | break; |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 2351 | } |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2352 | |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 2353 | error = inode_permission(inode, acc_mode); |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2354 | if (error) |
| 2355 | return error; |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 2356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | /* |
| 2358 | * An append-only file must be opened in append mode for writing. |
| 2359 | */ |
| 2360 | if (IS_APPEND(inode)) { |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 2361 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2362 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | if (flag & O_TRUNC) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2364 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | /* O_NOATIME can only be set by the owner or superuser */ |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 2368 | if (flag & O_NOATIME && !inode_owner_or_capable(inode)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2369 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | |
J. Bruce Fields | f3c7691e | 2011-09-21 10:58:13 -0400 | [diff] [blame] | 2371 | return 0; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2374 | static int handle_truncate(struct file *filp) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2375 | { |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2376 | struct path *path = &filp->f_path; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2377 | struct inode *inode = path->dentry->d_inode; |
| 2378 | int error = get_write_access(inode); |
| 2379 | if (error) |
| 2380 | return error; |
| 2381 | /* |
| 2382 | * Refuse to truncate files with mandatory locks held on them. |
| 2383 | */ |
| 2384 | error = locks_verify_locked(inode); |
| 2385 | if (!error) |
Tetsuo Handa | ea0d3ab | 2010-06-02 13:24:43 +0900 | [diff] [blame] | 2386 | error = security_path_truncate(path); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2387 | if (!error) { |
| 2388 | error = do_truncate(path->dentry, 0, |
| 2389 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2390 | filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2391 | } |
| 2392 | put_write_access(inode); |
Mimi Zohar | acd0c93 | 2009-09-04 13:08:46 -0400 | [diff] [blame] | 2393 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | } |
| 2395 | |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2396 | static inline int open_to_namei_flags(int flag) |
| 2397 | { |
Al Viro | 8a5e929 | 2011-06-25 19:15:54 -0400 | [diff] [blame] | 2398 | if ((flag & O_ACCMODE) == 3) |
| 2399 | flag--; |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2400 | return flag; |
| 2401 | } |
| 2402 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2403 | static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) |
| 2404 | { |
| 2405 | int error = security_path_mknod(dir, dentry, mode, 0); |
| 2406 | if (error) |
| 2407 | return error; |
| 2408 | |
| 2409 | error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); |
| 2410 | if (error) |
| 2411 | return error; |
| 2412 | |
| 2413 | return security_inode_create(dir->dentry->d_inode, dentry, mode); |
| 2414 | } |
| 2415 | |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2416 | /* |
| 2417 | * Attempt to atomically look up, create and open a file from a negative |
| 2418 | * dentry. |
| 2419 | * |
| 2420 | * Returns 0 if successful. The file will have been created and attached to |
| 2421 | * @file by the filesystem calling finish_open(). |
| 2422 | * |
| 2423 | * Returns 1 if the file was looked up only or didn't need creating. The |
| 2424 | * caller will need to perform the open themselves. @path will have been |
| 2425 | * updated to point to the new dentry. This may be negative. |
| 2426 | * |
| 2427 | * Returns an error code otherwise. |
| 2428 | */ |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2429 | static int atomic_open(struct nameidata *nd, struct dentry *dentry, |
| 2430 | struct path *path, struct file *file, |
| 2431 | const struct open_flags *op, |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2432 | bool got_write, bool need_lookup, |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2433 | int *opened) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2434 | { |
| 2435 | struct inode *dir = nd->path.dentry->d_inode; |
| 2436 | unsigned open_flag = open_to_namei_flags(op->open_flag); |
| 2437 | umode_t mode; |
| 2438 | int error; |
| 2439 | int acc_mode; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2440 | int create_error = 0; |
| 2441 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
| 2442 | |
| 2443 | BUG_ON(dentry->d_inode); |
| 2444 | |
| 2445 | /* Don't create child dentry for a dead directory. */ |
| 2446 | if (unlikely(IS_DEADDIR(dir))) { |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2447 | error = -ENOENT; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2448 | goto out; |
| 2449 | } |
| 2450 | |
Miklos Szeredi | 62b259d | 2012-08-15 13:01:24 +0200 | [diff] [blame] | 2451 | mode = op->mode; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2452 | if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) |
| 2453 | mode &= ~current_umask(); |
| 2454 | |
Al Viro | f8310c5 | 2012-07-30 11:50:30 +0400 | [diff] [blame] | 2455 | if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2456 | open_flag &= ~O_TRUNC; |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2457 | *opened |= FILE_CREATED; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | /* |
| 2461 | * Checking write permission is tricky, bacuse we don't know if we are |
| 2462 | * going to actually need it: O_CREAT opens should work as long as the |
| 2463 | * file exists. But checking existence breaks atomicity. The trick is |
| 2464 | * to check access and if not granted clear O_CREAT from the flags. |
| 2465 | * |
| 2466 | * Another problem is returing the "right" error value (e.g. for an |
| 2467 | * O_EXCL open we want to return EEXIST not EROFS). |
| 2468 | */ |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2469 | if (((open_flag & (O_CREAT | O_TRUNC)) || |
| 2470 | (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { |
| 2471 | if (!(open_flag & O_CREAT)) { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2472 | /* |
| 2473 | * No O_CREATE -> atomicity not a requirement -> fall |
| 2474 | * back to lookup + open |
| 2475 | */ |
| 2476 | goto no_open; |
| 2477 | } else if (open_flag & (O_EXCL | O_TRUNC)) { |
| 2478 | /* Fall back and fail with the right error */ |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2479 | create_error = -EROFS; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2480 | goto no_open; |
| 2481 | } else { |
| 2482 | /* No side effects, safe to clear O_CREAT */ |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2483 | create_error = -EROFS; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2484 | open_flag &= ~O_CREAT; |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | if (open_flag & O_CREAT) { |
Miklos Szeredi | 38227f7 | 2012-08-15 13:01:24 +0200 | [diff] [blame] | 2489 | error = may_o_create(&nd->path, dentry, mode); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2490 | if (error) { |
| 2491 | create_error = error; |
| 2492 | if (open_flag & O_EXCL) |
| 2493 | goto no_open; |
| 2494 | open_flag &= ~O_CREAT; |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | if (nd->flags & LOOKUP_DIRECTORY) |
| 2499 | open_flag |= O_DIRECTORY; |
| 2500 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2501 | file->f_path.dentry = DENTRY_NOT_SET; |
| 2502 | file->f_path.mnt = nd->path.mnt; |
| 2503 | error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2504 | opened); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 2505 | if (error < 0) { |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 2506 | if (create_error && error == -ENOENT) |
| 2507 | error = create_error; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2508 | goto out; |
| 2509 | } |
| 2510 | |
| 2511 | acc_mode = op->acc_mode; |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2512 | if (*opened & FILE_CREATED) { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2513 | fsnotify_create(dir, dentry); |
| 2514 | acc_mode = MAY_OPEN; |
| 2515 | } |
| 2516 | |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 2517 | if (error) { /* returned 1, that is */ |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2518 | if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2519 | error = -EIO; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2520 | goto out; |
| 2521 | } |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2522 | if (file->f_path.dentry) { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2523 | dput(dentry); |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2524 | dentry = file->f_path.dentry; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2525 | } |
Sage Weil | 62b2ce9 | 2012-08-15 13:30:12 -0700 | [diff] [blame] | 2526 | if (create_error && dentry->d_inode == NULL) { |
| 2527 | error = create_error; |
| 2528 | goto out; |
| 2529 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2530 | goto looked_up; |
| 2531 | } |
| 2532 | |
| 2533 | /* |
| 2534 | * We didn't have the inode before the open, so check open permission |
| 2535 | * here. |
| 2536 | */ |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2537 | error = may_open(&file->f_path, acc_mode, open_flag); |
| 2538 | if (error) |
| 2539 | fput(file); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2540 | |
| 2541 | out: |
| 2542 | dput(dentry); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2543 | return error; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2544 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2545 | no_open: |
| 2546 | if (need_lookup) { |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 2547 | dentry = lookup_real(dir, dentry, nd->flags); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2548 | if (IS_ERR(dentry)) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2549 | return PTR_ERR(dentry); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2550 | |
| 2551 | if (create_error) { |
| 2552 | int open_flag = op->open_flag; |
| 2553 | |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2554 | error = create_error; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2555 | if ((open_flag & O_EXCL)) { |
| 2556 | if (!dentry->d_inode) |
| 2557 | goto out; |
| 2558 | } else if (!dentry->d_inode) { |
| 2559 | goto out; |
| 2560 | } else if ((open_flag & O_TRUNC) && |
| 2561 | S_ISREG(dentry->d_inode->i_mode)) { |
| 2562 | goto out; |
| 2563 | } |
| 2564 | /* will fail later, go on to get the right error */ |
| 2565 | } |
| 2566 | } |
| 2567 | looked_up: |
| 2568 | path->dentry = dentry; |
| 2569 | path->mnt = nd->path.mnt; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2570 | return 1; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2571 | } |
| 2572 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2573 | /* |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2574 | * Look up and maybe create and open the last component. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2575 | * |
| 2576 | * Must be called with i_mutex held on parent. |
| 2577 | * |
David Howells | 1acf0af | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2578 | * Returns 0 if the file was successfully atomically created (if necessary) and |
| 2579 | * opened. In this case the file will be returned attached to @file. |
| 2580 | * |
| 2581 | * Returns 1 if the file was not completely opened at this time, though lookups |
| 2582 | * and creations will have been performed and the dentry returned in @path will |
| 2583 | * be positive upon return if O_CREAT was specified. If O_CREAT wasn't |
| 2584 | * specified then a negative dentry may be returned. |
| 2585 | * |
| 2586 | * An error code is returned otherwise. |
| 2587 | * |
| 2588 | * FILE_CREATE will be set in @*opened if the dentry was created and will be |
| 2589 | * cleared otherwise prior to returning. |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2590 | */ |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2591 | static int lookup_open(struct nameidata *nd, struct path *path, |
| 2592 | struct file *file, |
| 2593 | const struct open_flags *op, |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2594 | bool got_write, int *opened) |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2595 | { |
| 2596 | struct dentry *dir = nd->path.dentry; |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2597 | struct inode *dir_inode = dir->d_inode; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2598 | struct dentry *dentry; |
| 2599 | int error; |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2600 | bool need_lookup; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2601 | |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2602 | *opened &= ~FILE_CREATED; |
Al Viro | 201f956 | 2012-06-22 12:42:10 +0400 | [diff] [blame] | 2603 | dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2604 | if (IS_ERR(dentry)) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2605 | return PTR_ERR(dentry); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2606 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2607 | /* Cached positive dentry: will open in f_op->open */ |
| 2608 | if (!need_lookup && dentry->d_inode) |
| 2609 | goto out_no_open; |
| 2610 | |
| 2611 | if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2612 | return atomic_open(nd, dentry, path, file, op, got_write, |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2613 | need_lookup, opened); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2614 | } |
| 2615 | |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2616 | if (need_lookup) { |
| 2617 | BUG_ON(dentry->d_inode); |
| 2618 | |
Al Viro | 72bd866 | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 2619 | dentry = lookup_real(dir_inode, dentry, nd->flags); |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2620 | if (IS_ERR(dentry)) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2621 | return PTR_ERR(dentry); |
Miklos Szeredi | 54ef487 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2622 | } |
| 2623 | |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2624 | /* Negative dentry, just create the file */ |
| 2625 | if (!dentry->d_inode && (op->open_flag & O_CREAT)) { |
| 2626 | umode_t mode = op->mode; |
| 2627 | if (!IS_POSIXACL(dir->d_inode)) |
| 2628 | mode &= ~current_umask(); |
| 2629 | /* |
| 2630 | * This write is needed to ensure that a |
| 2631 | * rw->ro transition does not occur between |
| 2632 | * the time when the file is created and when |
| 2633 | * a permanent write count is taken through |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2634 | * the 'struct file' in finish_open(). |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2635 | */ |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2636 | if (!got_write) { |
| 2637 | error = -EROFS; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2638 | goto out_dput; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2639 | } |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2640 | *opened |= FILE_CREATED; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2641 | error = security_path_mknod(&nd->path, dentry, mode, 0); |
| 2642 | if (error) |
| 2643 | goto out_dput; |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2644 | error = vfs_create(dir->d_inode, dentry, mode, |
| 2645 | nd->flags & LOOKUP_EXCL); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2646 | if (error) |
| 2647 | goto out_dput; |
| 2648 | } |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2649 | out_no_open: |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2650 | path->dentry = dentry; |
| 2651 | path->mnt = nd->path.mnt; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2652 | return 1; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2653 | |
| 2654 | out_dput: |
| 2655 | dput(dentry); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2656 | return error; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | /* |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2660 | * Handle the last step of open() |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2661 | */ |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2662 | static int do_last(struct nameidata *nd, struct path *path, |
| 2663 | struct file *file, const struct open_flags *op, |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 2664 | int *opened, struct filename *name) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2665 | { |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 2666 | struct dentry *dir = nd->path.dentry; |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2667 | int open_flag = op->open_flag; |
Miklos Szeredi | 77d660a | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2668 | bool will_truncate = (open_flag & O_TRUNC) != 0; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2669 | bool got_write = false; |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2670 | int acc_mode = op->acc_mode; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 2671 | struct inode *inode; |
Miklos Szeredi | 77d660a | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2672 | bool symlink_ok = false; |
Miklos Szeredi | 16b1c1c | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2673 | struct path save_parent = { .dentry = NULL, .mnt = NULL }; |
| 2674 | bool retried = false; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2675 | int error; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2676 | |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 2677 | nd->flags &= ~LOOKUP_PARENT; |
| 2678 | nd->flags |= op->intent; |
| 2679 | |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 2680 | switch (nd->last_type) { |
| 2681 | case LAST_DOTDOT: |
Neil Brown | 176306f | 2010-05-24 16:57:56 +1000 | [diff] [blame] | 2682 | case LAST_DOT: |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2683 | error = handle_dots(nd, nd->last_type); |
| 2684 | if (error) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2685 | return error; |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 2686 | /* fallthrough */ |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 2687 | case LAST_ROOT: |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2688 | error = complete_walk(nd); |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2689 | if (error) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2690 | return error; |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame^] | 2691 | audit_inode(name, nd->path.dentry, 0); |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2692 | if (open_flag & O_CREAT) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2693 | error = -EISDIR; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2694 | goto out; |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2695 | } |
Miklos Szeredi | e83db16 | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 2696 | goto finish_open; |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 2697 | case LAST_BIND: |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2698 | error = complete_walk(nd); |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2699 | if (error) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2700 | return error; |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame^] | 2701 | audit_inode(name, dir, 0); |
Miklos Szeredi | e83db16 | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 2702 | goto finish_open; |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 2703 | } |
Al Viro | 67ee3ad | 2009-12-26 07:01:01 -0500 | [diff] [blame] | 2704 | |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2705 | if (!(open_flag & O_CREAT)) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2706 | if (nd->last.name[nd->last.len]) |
| 2707 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2708 | if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) |
Miklos Szeredi | 77d660a | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2709 | symlink_ok = true; |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2710 | /* we _can_ be in RCU mode here */ |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 2711 | error = lookup_fast(nd, &nd->last, path, &inode); |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 2712 | if (likely(!error)) |
| 2713 | goto finish_lookup; |
Miklos Szeredi | a1eb331 | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 2714 | |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 2715 | if (error < 0) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2716 | goto out; |
Miklos Szeredi | 37d7fff | 2012-06-05 15:10:12 +0200 | [diff] [blame] | 2717 | |
Miklos Szeredi | 7157486 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 2718 | BUG_ON(nd->inode != dir->d_inode); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 2719 | } else { |
| 2720 | /* create side of things */ |
| 2721 | /* |
| 2722 | * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED |
| 2723 | * has been cleared when we got to the last component we are |
| 2724 | * about to look up |
| 2725 | */ |
| 2726 | error = complete_walk(nd); |
| 2727 | if (error) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2728 | return error; |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 2729 | |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame^] | 2730 | audit_inode(name, dir, 0); |
Miklos Szeredi | b6183df | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 2731 | error = -EISDIR; |
| 2732 | /* trailing slashes? */ |
| 2733 | if (nd->last.name[nd->last.len]) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2734 | goto out; |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2735 | } |
| 2736 | |
Miklos Szeredi | 16b1c1c | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2737 | retry_lookup: |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2738 | if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
| 2739 | error = mnt_want_write(nd->path.mnt); |
| 2740 | if (!error) |
| 2741 | got_write = true; |
| 2742 | /* |
| 2743 | * do _not_ fail yet - we might not need that or fail with |
| 2744 | * a different error; let lookup_open() decide; we'll be |
| 2745 | * dropping this one anyway. |
| 2746 | */ |
| 2747 | } |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 2748 | mutex_lock(&dir->d_inode->i_mutex); |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2749 | error = lookup_open(nd, path, file, op, got_write, opened); |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2750 | mutex_unlock(&dir->d_inode->i_mutex); |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 2751 | |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2752 | if (error <= 0) { |
| 2753 | if (error) |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2754 | goto out; |
| 2755 | |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2756 | if ((*opened & FILE_CREATED) || |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2757 | !S_ISREG(file->f_path.dentry->d_inode->i_mode)) |
Miklos Szeredi | 77d660a | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2758 | will_truncate = false; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2759 | |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame^] | 2760 | audit_inode(name, file->f_path.dentry, 0); |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2761 | goto opened; |
| 2762 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2763 | |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2764 | if (*opened & FILE_CREATED) { |
Al Viro | 9b44f1b | 2011-03-09 00:17:27 -0500 | [diff] [blame] | 2765 | /* Don't check for write permission, don't truncate */ |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2766 | open_flag &= ~O_TRUNC; |
Miklos Szeredi | 77d660a | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2767 | will_truncate = false; |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2768 | acc_mode = MAY_OPEN; |
Miklos Szeredi | d58ffd3 | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2769 | path_to_nameidata(path, nd); |
Miklos Szeredi | e83db16 | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 2770 | goto finish_open_created; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | /* |
Jeff Layton | 3134f37 | 2012-07-25 10:19:47 -0400 | [diff] [blame] | 2774 | * create/update audit record if it already exists. |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2775 | */ |
Jeff Layton | 3134f37 | 2012-07-25 10:19:47 -0400 | [diff] [blame] | 2776 | if (path->dentry->d_inode) |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame^] | 2777 | audit_inode(name, path->dentry, 0); |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2778 | |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2779 | /* |
| 2780 | * If atomic_open() acquired write access it is dropped now due to |
| 2781 | * possible mount and symlink following (this might be optimized away if |
| 2782 | * necessary...) |
| 2783 | */ |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2784 | if (got_write) { |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2785 | mnt_drop_write(nd->path.mnt); |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2786 | got_write = false; |
Miklos Szeredi | d18e900 | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2789 | error = -EEXIST; |
Al Viro | f8310c5 | 2012-07-30 11:50:30 +0400 | [diff] [blame] | 2790 | if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2791 | goto exit_dput; |
| 2792 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 2793 | error = follow_managed(path, nd->flags); |
| 2794 | if (error < 0) |
| 2795 | goto exit_dput; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2796 | |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 2797 | if (error) |
| 2798 | nd->flags |= LOOKUP_JUMPED; |
| 2799 | |
Miklos Szeredi | decf340 | 2012-05-21 17:30:08 +0200 | [diff] [blame] | 2800 | BUG_ON(nd->flags & LOOKUP_RCU); |
| 2801 | inode = path->dentry->d_inode; |
Miklos Szeredi | 5f5daac | 2012-05-21 17:30:14 +0200 | [diff] [blame] | 2802 | finish_lookup: |
| 2803 | /* we _can_ be in RCU mode here */ |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2804 | error = -ENOENT; |
Miklos Szeredi | 54c33e7 | 2012-05-21 17:30:10 +0200 | [diff] [blame] | 2805 | if (!inode) { |
| 2806 | path_to_nameidata(path, nd); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2807 | goto out; |
Miklos Szeredi | 54c33e7 | 2012-05-21 17:30:10 +0200 | [diff] [blame] | 2808 | } |
Al Viro | 9e67f36 | 2009-12-26 07:04:50 -0500 | [diff] [blame] | 2809 | |
Miklos Szeredi | d45ea86 | 2012-05-21 17:30:09 +0200 | [diff] [blame] | 2810 | if (should_follow_link(inode, !symlink_ok)) { |
| 2811 | if (nd->flags & LOOKUP_RCU) { |
| 2812 | if (unlikely(unlazy_walk(nd, path->dentry))) { |
| 2813 | error = -ECHILD; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2814 | goto out; |
Miklos Szeredi | d45ea86 | 2012-05-21 17:30:09 +0200 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | BUG_ON(inode != path->dentry->d_inode); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2818 | return 1; |
Miklos Szeredi | d45ea86 | 2012-05-21 17:30:09 +0200 | [diff] [blame] | 2819 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2820 | |
Miklos Szeredi | 16b1c1c | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2821 | if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { |
| 2822 | path_to_nameidata(path, nd); |
| 2823 | } else { |
| 2824 | save_parent.dentry = nd->path.dentry; |
| 2825 | save_parent.mnt = mntget(path->mnt); |
| 2826 | nd->path.dentry = path->dentry; |
| 2827 | |
| 2828 | } |
Miklos Szeredi | decf340 | 2012-05-21 17:30:08 +0200 | [diff] [blame] | 2829 | nd->inode = inode; |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 2830 | /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ |
| 2831 | error = complete_walk(nd); |
Miklos Szeredi | 16b1c1c | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2832 | if (error) { |
| 2833 | path_put(&save_parent); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2834 | return error; |
Miklos Szeredi | 16b1c1c | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2835 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2836 | error = -EISDIR; |
Miklos Szeredi | 050ac84 | 2012-05-21 17:30:12 +0200 | [diff] [blame] | 2837 | if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2838 | goto out; |
Miklos Szeredi | af2f554 | 2012-05-21 17:30:11 +0200 | [diff] [blame] | 2839 | error = -ENOTDIR; |
| 2840 | if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2841 | goto out; |
Jeff Layton | adb5c24 | 2012-10-10 16:43:13 -0400 | [diff] [blame^] | 2842 | audit_inode(name, nd->path.dentry, 0); |
Miklos Szeredi | e83db16 | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 2843 | finish_open: |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 2844 | if (!S_ISREG(nd->inode->i_mode)) |
Miklos Szeredi | 77d660a | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2845 | will_truncate = false; |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 2846 | |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2847 | if (will_truncate) { |
| 2848 | error = mnt_want_write(nd->path.mnt); |
| 2849 | if (error) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2850 | goto out; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2851 | got_write = true; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2852 | } |
Miklos Szeredi | e83db16 | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 2853 | finish_open_created: |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2854 | error = may_open(&nd->path, acc_mode, open_flag); |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2855 | if (error) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2856 | goto out; |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2857 | file->f_path.mnt = nd->path.mnt; |
| 2858 | error = finish_open(file, nd->path.dentry, NULL, opened); |
| 2859 | if (error) { |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2860 | if (error == -EOPENSTALE) |
Miklos Szeredi | f60dc3d | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 2861 | goto stale_open; |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2862 | goto out; |
Miklos Szeredi | f60dc3d | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 2863 | } |
Miklos Szeredi | a8277b9 | 2012-06-05 15:10:32 +0200 | [diff] [blame] | 2864 | opened: |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2865 | error = open_check_o_direct(file); |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2866 | if (error) |
| 2867 | goto exit_fput; |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2868 | error = ima_file_check(file, op->acc_mode); |
Miklos Szeredi | aa4caad | 2012-06-05 15:10:28 +0200 | [diff] [blame] | 2869 | if (error) |
| 2870 | goto exit_fput; |
| 2871 | |
| 2872 | if (will_truncate) { |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2873 | error = handle_truncate(file); |
Miklos Szeredi | aa4caad | 2012-06-05 15:10:28 +0200 | [diff] [blame] | 2874 | if (error) |
| 2875 | goto exit_fput; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2876 | } |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2877 | out: |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2878 | if (got_write) |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2879 | mnt_drop_write(nd->path.mnt); |
Miklos Szeredi | 16b1c1c | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2880 | path_put(&save_parent); |
Miklos Szeredi | e276ae6 | 2012-05-21 17:30:06 +0200 | [diff] [blame] | 2881 | terminate_walk(nd); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2882 | return error; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2883 | |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2884 | exit_dput: |
| 2885 | path_put_conditional(path, nd); |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2886 | goto out; |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2887 | exit_fput: |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2888 | fput(file); |
| 2889 | goto out; |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2890 | |
Miklos Szeredi | f60dc3d | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 2891 | stale_open: |
| 2892 | /* If no saved parent or already retried then can't retry */ |
| 2893 | if (!save_parent.dentry || retried) |
| 2894 | goto out; |
| 2895 | |
| 2896 | BUG_ON(save_parent.dentry != dir); |
| 2897 | path_put(&nd->path); |
| 2898 | nd->path = save_parent; |
| 2899 | nd->inode = dir->d_inode; |
| 2900 | save_parent.mnt = NULL; |
| 2901 | save_parent.dentry = NULL; |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2902 | if (got_write) { |
Miklos Szeredi | f60dc3d | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 2903 | mnt_drop_write(nd->path.mnt); |
Al Viro | 64894cf | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2904 | got_write = false; |
Miklos Szeredi | f60dc3d | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 2905 | } |
| 2906 | retried = true; |
| 2907 | goto retry_lookup; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2908 | } |
| 2909 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 2910 | static struct file *path_openat(int dfd, struct filename *pathname, |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2911 | struct nameidata *nd, const struct open_flags *op, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2913 | struct file *base = NULL; |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2914 | struct file *file; |
Al Viro | 9850c05 | 2010-01-13 15:01:15 -0500 | [diff] [blame] | 2915 | struct path path; |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2916 | int opened = 0; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 2917 | int error; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2918 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2919 | file = get_empty_filp(); |
| 2920 | if (!file) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2921 | return ERR_PTR(-ENFILE); |
| 2922 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2923 | file->f_flags = op->open_flag; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2924 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 2925 | error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2926 | if (unlikely(error)) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2927 | goto out; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2928 | |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2929 | current->total_link_count = 0; |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 2930 | error = link_path_walk(pathname->name, nd); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2931 | if (unlikely(error)) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2932 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2934 | error = do_last(nd, &path, file, op, &opened, pathname); |
| 2935 | while (unlikely(error > 0)) { /* trailing symlink */ |
Nick Piggin | 7b9337a | 2011-01-14 08:42:43 +0000 | [diff] [blame] | 2936 | struct path link = path; |
Al Viro | def4af3 | 2009-12-26 08:37:05 -0500 | [diff] [blame] | 2937 | void *cookie; |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 2938 | if (!(nd->flags & LOOKUP_FOLLOW)) { |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2939 | path_put_conditional(&path, nd); |
| 2940 | path_put(&nd->path); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2941 | error = -ELOOP; |
Al Viro | 40b3913 | 2011-03-09 16:22:18 -0500 | [diff] [blame] | 2942 | break; |
| 2943 | } |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 2944 | error = may_follow_link(&link, nd); |
| 2945 | if (unlikely(error)) |
| 2946 | break; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2947 | nd->flags |= LOOKUP_PARENT; |
| 2948 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 2949 | error = follow_link(&link, nd, &cookie); |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 2950 | if (unlikely(error)) |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2951 | break; |
| 2952 | error = do_last(nd, &path, file, op, &opened, pathname); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 2953 | put_link(nd, &link, cookie); |
Al Viro | 806b681 | 2009-12-26 07:16:40 -0500 | [diff] [blame] | 2954 | } |
Al Viro | 10fa8e6 | 2009-12-26 07:09:49 -0500 | [diff] [blame] | 2955 | out: |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2956 | if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) |
| 2957 | path_put(&nd->root); |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2958 | if (base) |
| 2959 | fput(base); |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2960 | if (!(opened & FILE_OPENED)) { |
| 2961 | BUG_ON(!error); |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2962 | put_filp(file); |
Miklos Szeredi | 015c3bb | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2963 | } |
Al Viro | 2675a4e | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2964 | if (unlikely(error)) { |
| 2965 | if (error == -EOPENSTALE) { |
| 2966 | if (flags & LOOKUP_RCU) |
| 2967 | error = -ECHILD; |
| 2968 | else |
| 2969 | error = -ESTALE; |
| 2970 | } |
| 2971 | file = ERR_PTR(error); |
| 2972 | } |
| 2973 | return file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 2976 | struct file *do_filp_open(int dfd, struct filename *pathname, |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 2977 | const struct open_flags *op, int flags) |
| 2978 | { |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2979 | struct nameidata nd; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 2980 | struct file *filp; |
| 2981 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2982 | filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 2983 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2984 | filp = path_openat(dfd, pathname, &nd, op, flags); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 2985 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2986 | filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 2987 | return filp; |
| 2988 | } |
| 2989 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2990 | struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
| 2991 | const char *name, const struct open_flags *op, int flags) |
| 2992 | { |
| 2993 | struct nameidata nd; |
| 2994 | struct file *file; |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 2995 | struct filename filename = { .name = name }; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2996 | |
| 2997 | nd.root.mnt = mnt; |
| 2998 | nd.root.dentry = dentry; |
| 2999 | |
| 3000 | flags |= LOOKUP_ROOT; |
| 3001 | |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 3002 | if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3003 | return ERR_PTR(-ELOOP); |
| 3004 | |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3005 | file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3006 | if (unlikely(file == ERR_PTR(-ECHILD))) |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3007 | file = path_openat(-1, &filename, &nd, op, flags); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3008 | if (unlikely(file == ERR_PTR(-ESTALE))) |
Jeff Layton | 669abf4 | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3009 | file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3010 | return file; |
| 3011 | } |
| 3012 | |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3013 | 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] | 3014 | { |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3015 | struct dentry *dentry = ERR_PTR(-EEXIST); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3016 | struct nameidata nd; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3017 | int err2; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3018 | int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); |
| 3019 | if (error) |
| 3020 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3022 | /* |
| 3023 | * Yucky last component or no last component at all? |
| 3024 | * (foo/., foo/.., /////) |
| 3025 | */ |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3026 | if (nd.last_type != LAST_NORM) |
| 3027 | goto out; |
| 3028 | nd.flags &= ~LOOKUP_PARENT; |
| 3029 | nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3030 | |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3031 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
| 3032 | err2 = mnt_want_write(nd.path.mnt); |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3033 | /* |
| 3034 | * Do the final lookup. |
| 3035 | */ |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3036 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 3037 | dentry = lookup_hash(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | if (IS_ERR(dentry)) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3039 | goto unlock; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3040 | |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3041 | error = -EEXIST; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3042 | if (dentry->d_inode) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3043 | goto fail; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3044 | /* |
| 3045 | * Special case - lookup gave negative, but... we had foo/bar/ |
| 3046 | * From the vfs_mknod() POV we just have a negative dentry - |
| 3047 | * all is fine. Let's be bastards - you had / on the end, you've |
| 3048 | * been asking for (non-existent) directory. -ENOENT for you. |
| 3049 | */ |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3050 | if (unlikely(!is_dir && nd.last.name[nd.last.len])) { |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3051 | error = -ENOENT; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3052 | goto fail; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3053 | } |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3054 | if (unlikely(err2)) { |
| 3055 | error = err2; |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3056 | goto fail; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3057 | } |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3058 | *path = nd.path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | fail: |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3061 | dput(dentry); |
| 3062 | dentry = ERR_PTR(error); |
| 3063 | unlock: |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3064 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3065 | if (!err2) |
| 3066 | mnt_drop_write(nd.path.mnt); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3067 | out: |
| 3068 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | return dentry; |
| 3070 | } |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3071 | EXPORT_SYMBOL(kern_path_create); |
| 3072 | |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3073 | void done_path_create(struct path *path, struct dentry *dentry) |
| 3074 | { |
| 3075 | dput(dentry); |
| 3076 | mutex_unlock(&path->dentry->d_inode->i_mutex); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3077 | mnt_drop_write(path->mnt); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3078 | path_put(path); |
| 3079 | } |
| 3080 | EXPORT_SYMBOL(done_path_create); |
| 3081 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3082 | struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) |
| 3083 | { |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3084 | struct filename *tmp = getname(pathname); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3085 | struct dentry *res; |
| 3086 | if (IS_ERR(tmp)) |
| 3087 | return ERR_CAST(tmp); |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3088 | res = kern_path_create(dfd, tmp->name, path, is_dir); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3089 | putname(tmp); |
| 3090 | return res; |
| 3091 | } |
| 3092 | EXPORT_SYMBOL(user_path_create); |
| 3093 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 3094 | int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3096 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | |
| 3098 | if (error) |
| 3099 | return error; |
| 3100 | |
Eric W. Biederman | 975d6b3 | 2011-11-13 12:16:43 -0800 | [diff] [blame] | 3101 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | return -EPERM; |
| 3103 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3104 | if (!dir->i_op->mknod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | return -EPERM; |
| 3106 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 3107 | error = devcgroup_inode_mknod(mode, dev); |
| 3108 | if (error) |
| 3109 | return error; |
| 3110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | error = security_inode_mknod(dir, dentry, mode, dev); |
| 3112 | if (error) |
| 3113 | return error; |
| 3114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3116 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3117 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | return error; |
| 3119 | } |
| 3120 | |
Al Viro | f69aac0 | 2011-07-26 04:31:40 -0400 | [diff] [blame] | 3121 | static int may_mknod(umode_t mode) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3122 | { |
| 3123 | switch (mode & S_IFMT) { |
| 3124 | case S_IFREG: |
| 3125 | case S_IFCHR: |
| 3126 | case S_IFBLK: |
| 3127 | case S_IFIFO: |
| 3128 | case S_IFSOCK: |
| 3129 | case 0: /* zero mode translates to S_IFREG */ |
| 3130 | return 0; |
| 3131 | case S_IFDIR: |
| 3132 | return -EPERM; |
| 3133 | default: |
| 3134 | return -EINVAL; |
| 3135 | } |
| 3136 | } |
| 3137 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3138 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3139 | unsigned, dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3141 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3142 | struct path path; |
| 3143 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
Al Viro | 8e4bfca | 2012-07-20 01:17:26 +0400 | [diff] [blame] | 3145 | error = may_mknod(mode); |
| 3146 | if (error) |
| 3147 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3149 | dentry = user_path_create(dfd, filename, &path, 0); |
| 3150 | if (IS_ERR(dentry)) |
| 3151 | return PTR_ERR(dentry); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3152 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3153 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3154 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3155 | error = security_path_mknod(&path, dentry, mode, dev); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3156 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3157 | goto out; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3158 | switch (mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | case 0: case S_IFREG: |
Al Viro | 312b63f | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 3160 | error = vfs_create(path.dentry->d_inode,dentry,mode,true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | break; |
| 3162 | case S_IFCHR: case S_IFBLK: |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3163 | error = vfs_mknod(path.dentry->d_inode,dentry,mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | new_decode_dev(dev)); |
| 3165 | break; |
| 3166 | case S_IFIFO: case S_IFSOCK: |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3167 | error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | } |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3170 | out: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3171 | done_path_create(&path, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | return error; |
| 3173 | } |
| 3174 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3175 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3176 | { |
| 3177 | return sys_mknodat(AT_FDCWD, filename, mode, dev); |
| 3178 | } |
| 3179 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 3180 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3182 | int error = may_create(dir, dentry); |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3183 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | |
| 3185 | if (error) |
| 3186 | return error; |
| 3187 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3188 | if (!dir->i_op->mkdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | return -EPERM; |
| 3190 | |
| 3191 | mode &= (S_IRWXUGO|S_ISVTX); |
| 3192 | error = security_inode_mkdir(dir, dentry, mode); |
| 3193 | if (error) |
| 3194 | return error; |
| 3195 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3196 | if (max_links && dir->i_nlink >= max_links) |
| 3197 | return -EMLINK; |
| 3198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | error = dir->i_op->mkdir(dir, dentry, mode); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3200 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3201 | fsnotify_mkdir(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | return error; |
| 3203 | } |
| 3204 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 3205 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | { |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3207 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3208 | struct path path; |
| 3209 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3211 | dentry = user_path_create(dfd, pathname, &path, 1); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3212 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3213 | return PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3214 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3215 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3216 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3217 | error = security_path_mkdir(&path, dentry, mode); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3218 | if (!error) |
| 3219 | error = vfs_mkdir(path.dentry->d_inode, dentry, mode); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3220 | done_path_create(&path, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | return error; |
| 3222 | } |
| 3223 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 3224 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3225 | { |
| 3226 | return sys_mkdirat(AT_FDCWD, pathname, mode); |
| 3227 | } |
| 3228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | /* |
Sage Weil | a71905f | 2011-05-24 13:06:08 -0700 | [diff] [blame] | 3230 | * The dentry_unhash() helper will try to drop the dentry early: we |
J. Bruce Fields | c0d0259 | 2012-02-15 11:48:40 -0500 | [diff] [blame] | 3231 | * should have a usage count of 1 if we're the only user of this |
Sage Weil | a71905f | 2011-05-24 13:06:08 -0700 | [diff] [blame] | 3232 | * dentry, and if that is true (possibly after pruning the dcache), |
| 3233 | * then we drop the dentry now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | * |
| 3235 | * A low-level filesystem can, if it choses, legally |
| 3236 | * do a |
| 3237 | * |
| 3238 | * if (!d_unhashed(dentry)) |
| 3239 | * return -EBUSY; |
| 3240 | * |
| 3241 | * if it cannot handle the case of removing a directory |
| 3242 | * that is still in use by something else.. |
| 3243 | */ |
| 3244 | void dentry_unhash(struct dentry *dentry) |
| 3245 | { |
Vasily Averin | dc16842 | 2006-12-06 20:37:07 -0800 | [diff] [blame] | 3246 | shrink_dcache_parent(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | spin_lock(&dentry->d_lock); |
Sage Weil | 64252c7 | 2011-05-24 13:06:05 -0700 | [diff] [blame] | 3248 | if (dentry->d_count == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | __d_drop(dentry); |
| 3250 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3254 | { |
| 3255 | int error = may_delete(dir, dentry, 1); |
| 3256 | |
| 3257 | if (error) |
| 3258 | return error; |
| 3259 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3260 | if (!dir->i_op->rmdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | return -EPERM; |
| 3262 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3263 | dget(dentry); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3264 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3266 | error = -EBUSY; |
| 3267 | if (d_mountpoint(dentry)) |
| 3268 | goto out; |
| 3269 | |
| 3270 | error = security_inode_rmdir(dir, dentry); |
| 3271 | if (error) |
| 3272 | goto out; |
| 3273 | |
Sage Weil | 3cebde2 | 2011-05-29 21:20:59 -0700 | [diff] [blame] | 3274 | shrink_dcache_parent(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3275 | error = dir->i_op->rmdir(dir, dentry); |
| 3276 | if (error) |
| 3277 | goto out; |
| 3278 | |
| 3279 | dentry->d_inode->i_flags |= S_DEAD; |
| 3280 | dont_mount(dentry); |
| 3281 | |
| 3282 | out: |
| 3283 | mutex_unlock(&dentry->d_inode->i_mutex); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3284 | dput(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3285 | if (!error) |
| 3286 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | return error; |
| 3288 | } |
| 3289 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3290 | static long do_rmdir(int dfd, const char __user *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | { |
| 3292 | int error = 0; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3293 | struct filename *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | struct dentry *dentry; |
| 3295 | struct nameidata nd; |
| 3296 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3297 | name = user_path_parent(dfd, pathname, &nd); |
| 3298 | if (IS_ERR(name)) |
| 3299 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | |
| 3301 | switch(nd.last_type) { |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3302 | case LAST_DOTDOT: |
| 3303 | error = -ENOTEMPTY; |
| 3304 | goto exit1; |
| 3305 | case LAST_DOT: |
| 3306 | error = -EINVAL; |
| 3307 | goto exit1; |
| 3308 | case LAST_ROOT: |
| 3309 | error = -EBUSY; |
| 3310 | goto exit1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | } |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3312 | |
| 3313 | nd.flags &= ~LOOKUP_PARENT; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3314 | error = mnt_want_write(nd.path.mnt); |
| 3315 | if (error) |
| 3316 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3317 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3318 | 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] | 3319 | dentry = lookup_hash(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3321 | if (IS_ERR(dentry)) |
| 3322 | goto exit2; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3323 | if (!dentry->d_inode) { |
| 3324 | error = -ENOENT; |
| 3325 | goto exit3; |
| 3326 | } |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3327 | error = security_path_rmdir(&nd.path, dentry); |
| 3328 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3329 | goto exit3; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3330 | error = vfs_rmdir(nd.path.dentry->d_inode, dentry); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 3331 | exit3: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3332 | dput(dentry); |
| 3333 | exit2: |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3334 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3335 | mnt_drop_write(nd.path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | exit1: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 3337 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | putname(name); |
| 3339 | return error; |
| 3340 | } |
| 3341 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 3342 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3343 | { |
| 3344 | return do_rmdir(AT_FDCWD, pathname); |
| 3345 | } |
| 3346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | int vfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3348 | { |
| 3349 | int error = may_delete(dir, dentry, 0); |
| 3350 | |
| 3351 | if (error) |
| 3352 | return error; |
| 3353 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3354 | if (!dir->i_op->unlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3355 | return -EPERM; |
| 3356 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3357 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | if (d_mountpoint(dentry)) |
| 3359 | error = -EBUSY; |
| 3360 | else { |
| 3361 | error = security_inode_unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3362 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | error = dir->i_op->unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3364 | if (!error) |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 3365 | dont_mount(dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3368 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | |
| 3370 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ |
| 3371 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { |
Jan Kara | ece9591 | 2008-02-06 01:37:13 -0800 | [diff] [blame] | 3372 | fsnotify_link_count(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3374 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | return error; |
| 3377 | } |
| 3378 | |
| 3379 | /* |
| 3380 | * 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] | 3381 | * 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] | 3382 | * writeout happening, and we don't want to prevent access to the directory |
| 3383 | * while waiting on the I/O. |
| 3384 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3385 | static long do_unlinkat(int dfd, const char __user *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3387 | int error; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3388 | struct filename *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | struct dentry *dentry; |
| 3390 | struct nameidata nd; |
| 3391 | struct inode *inode = NULL; |
| 3392 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3393 | name = user_path_parent(dfd, pathname, &nd); |
| 3394 | if (IS_ERR(name)) |
| 3395 | return PTR_ERR(name); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | error = -EISDIR; |
| 3398 | if (nd.last_type != LAST_NORM) |
| 3399 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3400 | |
| 3401 | nd.flags &= ~LOOKUP_PARENT; |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3402 | error = mnt_want_write(nd.path.mnt); |
| 3403 | if (error) |
| 3404 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3405 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3406 | 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] | 3407 | dentry = lookup_hash(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | error = PTR_ERR(dentry); |
| 3409 | if (!IS_ERR(dentry)) { |
| 3410 | /* Why not before? Because we want correct error value */ |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 3411 | if (nd.last.name[nd.last.len]) |
| 3412 | goto slashes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | inode = dentry->d_inode; |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 3414 | if (!inode) |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3415 | goto slashes; |
| 3416 | ihold(inode); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3417 | error = security_path_unlink(&nd.path, dentry); |
| 3418 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3419 | goto exit2; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3420 | error = vfs_unlink(nd.path.dentry->d_inode, dentry); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3421 | exit2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | dput(dentry); |
| 3423 | } |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3424 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | if (inode) |
| 3426 | iput(inode); /* truncate the inode here */ |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3427 | mnt_drop_write(nd.path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | exit1: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 3429 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | putname(name); |
| 3431 | return error; |
| 3432 | |
| 3433 | slashes: |
| 3434 | error = !dentry->d_inode ? -ENOENT : |
| 3435 | S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; |
| 3436 | goto exit2; |
| 3437 | } |
| 3438 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3439 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3440 | { |
| 3441 | if ((flag & ~AT_REMOVEDIR) != 0) |
| 3442 | return -EINVAL; |
| 3443 | |
| 3444 | if (flag & AT_REMOVEDIR) |
| 3445 | return do_rmdir(dfd, pathname); |
| 3446 | |
| 3447 | return do_unlinkat(dfd, pathname); |
| 3448 | } |
| 3449 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 3450 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3451 | { |
| 3452 | return do_unlinkat(AT_FDCWD, pathname); |
| 3453 | } |
| 3454 | |
Miklos Szeredi | db2e747 | 2008-06-24 16:50:16 +0200 | [diff] [blame] | 3455 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | { |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3457 | int error = may_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | |
| 3459 | if (error) |
| 3460 | return error; |
| 3461 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3462 | if (!dir->i_op->symlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | return -EPERM; |
| 3464 | |
| 3465 | error = security_inode_symlink(dir, dentry, oldname); |
| 3466 | if (error) |
| 3467 | return error; |
| 3468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | error = dir->i_op->symlink(dir, dentry, oldname); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3470 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3471 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | return error; |
| 3473 | } |
| 3474 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3475 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
| 3476 | int, newdfd, const char __user *, newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3478 | int error; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3479 | struct filename *from; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3480 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3481 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | |
| 3483 | from = getname(oldname); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3484 | if (IS_ERR(from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | return PTR_ERR(from); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3486 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3487 | dentry = user_path_create(newdfd, newname, &path, 0); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3488 | error = PTR_ERR(dentry); |
| 3489 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3490 | goto out_putname; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3491 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3492 | error = security_path_symlink(&path, dentry, from->name); |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3493 | if (!error) |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3494 | error = vfs_symlink(path.dentry->d_inode, dentry, from->name); |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3495 | done_path_create(&path, dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3496 | out_putname: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | putname(from); |
| 3498 | return error; |
| 3499 | } |
| 3500 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 3501 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3502 | { |
| 3503 | return sys_symlinkat(oldname, AT_FDCWD, newname); |
| 3504 | } |
| 3505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 3507 | { |
| 3508 | struct inode *inode = old_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3509 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3510 | int error; |
| 3511 | |
| 3512 | if (!inode) |
| 3513 | return -ENOENT; |
| 3514 | |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3515 | error = may_create(dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | if (error) |
| 3517 | return error; |
| 3518 | |
| 3519 | if (dir->i_sb != inode->i_sb) |
| 3520 | return -EXDEV; |
| 3521 | |
| 3522 | /* |
| 3523 | * A link to an append-only or immutable file cannot be created. |
| 3524 | */ |
| 3525 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 3526 | return -EPERM; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3527 | if (!dir->i_op->link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | return -EPERM; |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 3529 | if (S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | return -EPERM; |
| 3531 | |
| 3532 | error = security_inode_link(old_dentry, dir, new_dentry); |
| 3533 | if (error) |
| 3534 | return error; |
| 3535 | |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 3536 | mutex_lock(&inode->i_mutex); |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 3537 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
| 3538 | if (inode->i_nlink == 0) |
| 3539 | error = -ENOENT; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3540 | else if (max_links && inode->i_nlink >= max_links) |
| 3541 | error = -EMLINK; |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 3542 | else |
| 3543 | error = dir->i_op->link(old_dentry, dir, new_dentry); |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 3544 | mutex_unlock(&inode->i_mutex); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 3545 | if (!error) |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 3546 | fsnotify_link(dir, inode, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | return error; |
| 3548 | } |
| 3549 | |
| 3550 | /* |
| 3551 | * Hardlinks are often used in delicate situations. We avoid |
| 3552 | * security-related surprises by not following symlinks on the |
| 3553 | * newname. --KAB |
| 3554 | * |
| 3555 | * We don't follow them on the oldname either to be compatible |
| 3556 | * with linux 2.0, and to avoid hard-linking to directories |
| 3557 | * and other special files. --ADM |
| 3558 | */ |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3559 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
| 3560 | int, newdfd, const char __user *, newname, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | { |
| 3562 | struct dentry *new_dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3563 | struct path old_path, new_path; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 3564 | int how = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 3567 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 3568 | return -EINVAL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 3569 | /* |
| 3570 | * To use null names we require CAP_DAC_READ_SEARCH |
| 3571 | * This ensures that not everyone will be able to create |
| 3572 | * handlink using the passed filedescriptor. |
| 3573 | */ |
| 3574 | if (flags & AT_EMPTY_PATH) { |
| 3575 | if (!capable(CAP_DAC_READ_SEARCH)) |
| 3576 | return -ENOENT; |
| 3577 | how = LOOKUP_EMPTY; |
| 3578 | } |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 3579 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 3580 | if (flags & AT_SYMLINK_FOLLOW) |
| 3581 | how |= LOOKUP_FOLLOW; |
| 3582 | |
| 3583 | error = user_path_at(olddfd, oldname, how, &old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | if (error) |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3585 | return error; |
| 3586 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3587 | new_dentry = user_path_create(newdfd, newname, &new_path, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | error = PTR_ERR(new_dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3589 | if (IS_ERR(new_dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3590 | goto out; |
| 3591 | |
| 3592 | error = -EXDEV; |
| 3593 | if (old_path.mnt != new_path.mnt) |
| 3594 | goto out_dput; |
Kees Cook | 800179c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 3595 | error = may_linkat(&old_path); |
| 3596 | if (unlikely(error)) |
| 3597 | goto out_dput; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3598 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3599 | if (error) |
Al Viro | a8104a9 | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3600 | goto out_dput; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3601 | error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 3602 | out_dput: |
Al Viro | 921a165 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3603 | done_path_create(&new_path, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 3605 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | |
| 3607 | return error; |
| 3608 | } |
| 3609 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 3610 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3611 | { |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 3612 | return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3613 | } |
| 3614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | /* |
| 3616 | * The worst of all namespace operations - renaming directory. "Perverted" |
| 3617 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... |
| 3618 | * Problems: |
| 3619 | * a) we can get into loop creation. Check is done in is_subdir(). |
| 3620 | * b) race potential - two innocent renames can create a loop together. |
| 3621 | * 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] | 3622 | * 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] | 3623 | * story. |
| 3624 | * 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] | 3625 | * 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] | 3626 | * whether the target exists). Solution: try to be smart with locking |
| 3627 | * 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] | 3628 | * 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] | 3629 | * move will be locked. Thus we can rank directories by the tree |
| 3630 | * (ancestors first) and rank all non-directories after them. |
| 3631 | * That works since everybody except rename does "lock parent, lookup, |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 3632 | * lock child" and rename is under ->s_vfs_rename_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
| 3634 | * has no more than 1 dentry. If "hybrid" objects will ever appear, |
| 3635 | * 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] | 3636 | * 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] | 3637 | * 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] | 3638 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 3639 | * ->i_mutex on parents, which works but leads to some truly excessive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | * locking]. |
| 3641 | */ |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 3642 | static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, |
| 3643 | struct inode *new_dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | { |
| 3645 | int error = 0; |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3646 | struct inode *target = new_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3647 | unsigned max_links = new_dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | |
| 3649 | /* |
| 3650 | * If we are going to change the parent - check write permissions, |
| 3651 | * we'll need to flip '..'. |
| 3652 | */ |
| 3653 | if (new_dir != old_dir) { |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 3654 | error = inode_permission(old_dentry->d_inode, MAY_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | if (error) |
| 3656 | return error; |
| 3657 | } |
| 3658 | |
| 3659 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 3660 | if (error) |
| 3661 | return error; |
| 3662 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3663 | dget(new_dentry); |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 3664 | if (target) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3665 | mutex_lock(&target->i_mutex); |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3666 | |
| 3667 | error = -EBUSY; |
| 3668 | if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) |
| 3669 | goto out; |
| 3670 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3671 | error = -EMLINK; |
| 3672 | if (max_links && !target && new_dir != old_dir && |
| 3673 | new_dir->i_nlink >= max_links) |
| 3674 | goto out; |
| 3675 | |
Sage Weil | 3cebde2 | 2011-05-29 21:20:59 -0700 | [diff] [blame] | 3676 | if (target) |
| 3677 | shrink_dcache_parent(new_dentry); |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3678 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); |
| 3679 | if (error) |
| 3680 | goto out; |
| 3681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | if (target) { |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3683 | target->i_flags |= S_DEAD; |
| 3684 | dont_mount(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | } |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3686 | out: |
| 3687 | if (target) |
| 3688 | mutex_unlock(&target->i_mutex); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3689 | dput(new_dentry); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 3690 | if (!error) |
Mark Fasheh | 349457c | 2006-09-08 14:22:21 -0700 | [diff] [blame] | 3691 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) |
| 3692 | d_move(old_dentry,new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | return error; |
| 3694 | } |
| 3695 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 3696 | static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, |
| 3697 | struct inode *new_dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | { |
Sage Weil | 51892bb | 2011-05-24 13:06:13 -0700 | [diff] [blame] | 3699 | struct inode *target = new_dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | int error; |
| 3701 | |
| 3702 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 3703 | if (error) |
| 3704 | return error; |
| 3705 | |
| 3706 | dget(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | if (target) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3708 | mutex_lock(&target->i_mutex); |
Sage Weil | 51892bb | 2011-05-24 13:06:13 -0700 | [diff] [blame] | 3709 | |
| 3710 | error = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) |
Sage Weil | 51892bb | 2011-05-24 13:06:13 -0700 | [diff] [blame] | 3712 | goto out; |
| 3713 | |
| 3714 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); |
| 3715 | if (error) |
| 3716 | goto out; |
| 3717 | |
| 3718 | if (target) |
| 3719 | dont_mount(new_dentry); |
| 3720 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) |
| 3721 | d_move(old_dentry, new_dentry); |
| 3722 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | if (target) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3724 | mutex_unlock(&target->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | dput(new_dentry); |
| 3726 | return error; |
| 3727 | } |
| 3728 | |
| 3729 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 3730 | struct inode *new_dir, struct dentry *new_dentry) |
| 3731 | { |
| 3732 | int error; |
| 3733 | int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); |
Eric Paris | 59b0df2 | 2010-02-08 12:53:52 -0500 | [diff] [blame] | 3734 | const unsigned char *old_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | |
| 3736 | if (old_dentry->d_inode == new_dentry->d_inode) |
| 3737 | return 0; |
| 3738 | |
| 3739 | error = may_delete(old_dir, old_dentry, is_dir); |
| 3740 | if (error) |
| 3741 | return error; |
| 3742 | |
| 3743 | if (!new_dentry->d_inode) |
Miklos Szeredi | a95164d | 2008-07-30 15:08:48 +0200 | [diff] [blame] | 3744 | error = may_create(new_dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | else |
| 3746 | error = may_delete(new_dir, new_dentry, is_dir); |
| 3747 | if (error) |
| 3748 | return error; |
| 3749 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3750 | if (!old_dir->i_op->rename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | return -EPERM; |
| 3752 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3753 | old_name = fsnotify_oldname_init(old_dentry->d_name.name); |
| 3754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | if (is_dir) |
| 3756 | error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); |
| 3757 | else |
| 3758 | error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); |
Al Viro | 123df29 | 2009-12-25 04:57:57 -0500 | [diff] [blame] | 3759 | if (!error) |
| 3760 | fsnotify_move(old_dir, new_dir, old_name, is_dir, |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 3761 | new_dentry->d_inode, old_dentry); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3762 | fsnotify_oldname_free(old_name); |
| 3763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | return error; |
| 3765 | } |
| 3766 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3767 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
| 3768 | int, newdfd, const char __user *, newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3770 | struct dentry *old_dir, *new_dir; |
| 3771 | struct dentry *old_dentry, *new_dentry; |
| 3772 | struct dentry *trap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3773 | struct nameidata oldnd, newnd; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3774 | struct filename *from; |
| 3775 | struct filename *to; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3776 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3778 | from = user_path_parent(olddfd, oldname, &oldnd); |
| 3779 | if (IS_ERR(from)) { |
| 3780 | error = PTR_ERR(from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | goto exit; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3784 | to = user_path_parent(newdfd, newname, &newnd); |
| 3785 | if (IS_ERR(to)) { |
| 3786 | error = PTR_ERR(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | goto exit1; |
Jeff Layton | 91a27b2 | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3788 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | |
| 3790 | error = -EXDEV; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3791 | if (oldnd.path.mnt != newnd.path.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | goto exit2; |
| 3793 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3794 | old_dir = oldnd.path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | error = -EBUSY; |
| 3796 | if (oldnd.last_type != LAST_NORM) |
| 3797 | goto exit2; |
| 3798 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3799 | new_dir = newnd.path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3800 | if (newnd.last_type != LAST_NORM) |
| 3801 | goto exit2; |
| 3802 | |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3803 | error = mnt_want_write(oldnd.path.mnt); |
| 3804 | if (error) |
| 3805 | goto exit2; |
| 3806 | |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3807 | oldnd.flags &= ~LOOKUP_PARENT; |
| 3808 | newnd.flags &= ~LOOKUP_PARENT; |
OGAWA Hirofumi | 4e9ed2f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3809 | newnd.flags |= LOOKUP_RENAME_TARGET; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | trap = lock_rename(new_dir, old_dir); |
| 3812 | |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 3813 | old_dentry = lookup_hash(&oldnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | error = PTR_ERR(old_dentry); |
| 3815 | if (IS_ERR(old_dentry)) |
| 3816 | goto exit3; |
| 3817 | /* source must exist */ |
| 3818 | error = -ENOENT; |
| 3819 | if (!old_dentry->d_inode) |
| 3820 | goto exit4; |
| 3821 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
| 3822 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) { |
| 3823 | error = -ENOTDIR; |
| 3824 | if (oldnd.last.name[oldnd.last.len]) |
| 3825 | goto exit4; |
| 3826 | if (newnd.last.name[newnd.last.len]) |
| 3827 | goto exit4; |
| 3828 | } |
| 3829 | /* source should not be ancestor of target */ |
| 3830 | error = -EINVAL; |
| 3831 | if (old_dentry == trap) |
| 3832 | goto exit4; |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 3833 | new_dentry = lookup_hash(&newnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | error = PTR_ERR(new_dentry); |
| 3835 | if (IS_ERR(new_dentry)) |
| 3836 | goto exit4; |
| 3837 | /* target should not be an ancestor of source */ |
| 3838 | error = -ENOTEMPTY; |
| 3839 | if (new_dentry == trap) |
| 3840 | goto exit5; |
| 3841 | |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3842 | error = security_path_rename(&oldnd.path, old_dentry, |
| 3843 | &newnd.path, new_dentry); |
| 3844 | if (error) |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3845 | goto exit5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | error = vfs_rename(old_dir->d_inode, old_dentry, |
| 3847 | new_dir->d_inode, new_dentry); |
| 3848 | exit5: |
| 3849 | dput(new_dentry); |
| 3850 | exit4: |
| 3851 | dput(old_dentry); |
| 3852 | exit3: |
| 3853 | unlock_rename(new_dir, old_dir); |
Jan Kara | c30dabf | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3854 | mnt_drop_write(oldnd.path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | exit2: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 3856 | path_put(&newnd.path); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3857 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | exit1: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 3859 | path_put(&oldnd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | putname(from); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3861 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | return error; |
| 3863 | } |
| 3864 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 3865 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3866 | { |
| 3867 | return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); |
| 3868 | } |
| 3869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) |
| 3871 | { |
| 3872 | int len; |
| 3873 | |
| 3874 | len = PTR_ERR(link); |
| 3875 | if (IS_ERR(link)) |
| 3876 | goto out; |
| 3877 | |
| 3878 | len = strlen(link); |
| 3879 | if (len > (unsigned) buflen) |
| 3880 | len = buflen; |
| 3881 | if (copy_to_user(buffer, link, len)) |
| 3882 | len = -EFAULT; |
| 3883 | out: |
| 3884 | return len; |
| 3885 | } |
| 3886 | |
| 3887 | /* |
| 3888 | * A helper for ->readlink(). This should be used *ONLY* for symlinks that |
| 3889 | * have ->follow_link() touching nd only in nd_set_link(). Using (or not |
| 3890 | * using) it for any given inode is up to filesystem. |
| 3891 | */ |
| 3892 | int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 3893 | { |
| 3894 | struct nameidata nd; |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 3895 | void *cookie; |
Marcin Slusarz | 694a176 | 2008-06-09 16:40:37 -0700 | [diff] [blame] | 3896 | int res; |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 3897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | nd.depth = 0; |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 3899 | cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); |
Marcin Slusarz | 694a176 | 2008-06-09 16:40:37 -0700 | [diff] [blame] | 3900 | if (IS_ERR(cookie)) |
| 3901 | return PTR_ERR(cookie); |
| 3902 | |
| 3903 | res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); |
| 3904 | if (dentry->d_inode->i_op->put_link) |
| 3905 | dentry->d_inode->i_op->put_link(dentry, &nd, cookie); |
| 3906 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | int vfs_follow_link(struct nameidata *nd, const char *link) |
| 3910 | { |
| 3911 | return __vfs_follow_link(nd, link); |
| 3912 | } |
| 3913 | |
| 3914 | /* get the link contents into pagecache */ |
| 3915 | static char *page_getlink(struct dentry * dentry, struct page **ppage) |
| 3916 | { |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 3917 | char *kaddr; |
| 3918 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | struct address_space *mapping = dentry->d_inode->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 3920 | page = read_mapping_page(mapping, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3921 | if (IS_ERR(page)) |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 3922 | return (char*)page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | *ppage = page; |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 3924 | kaddr = kmap(page); |
| 3925 | nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); |
| 3926 | return kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 3930 | { |
| 3931 | struct page *page = NULL; |
| 3932 | char *s = page_getlink(dentry, &page); |
| 3933 | int res = vfs_readlink(dentry,buffer,buflen,s); |
| 3934 | if (page) { |
| 3935 | kunmap(page); |
| 3936 | page_cache_release(page); |
| 3937 | } |
| 3938 | return res; |
| 3939 | } |
| 3940 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 3941 | void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | { |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 3943 | struct page *page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | nd_set_link(nd, page_getlink(dentry, &page)); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 3945 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | } |
| 3947 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 3948 | void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | { |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 3950 | struct page *page = cookie; |
| 3951 | |
| 3952 | if (page) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | kunmap(page); |
| 3954 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | } |
| 3956 | } |
| 3957 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3958 | /* |
| 3959 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS |
| 3960 | */ |
| 3961 | 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] | 3962 | { |
| 3963 | struct address_space *mapping = inode->i_mapping; |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 3964 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3965 | void *fsdata; |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 3966 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | char *kaddr; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3968 | unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; |
| 3969 | if (nofs) |
| 3970 | flags |= AOP_FLAG_NOFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 3972 | retry: |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3973 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3974 | flags, &page, &fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | if (err) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3976 | goto fail; |
| 3977 | |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 3978 | kaddr = kmap_atomic(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | memcpy(kaddr, symname, len-1); |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 3980 | kunmap_atomic(kaddr); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3981 | |
| 3982 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, |
| 3983 | page, fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | if (err < 0) |
| 3985 | goto fail; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 3986 | if (err < len-1) |
| 3987 | goto retry; |
| 3988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | mark_inode_dirty(inode); |
| 3990 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | fail: |
| 3992 | return err; |
| 3993 | } |
| 3994 | |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 3995 | int page_symlink(struct inode *inode, const char *symname, int len) |
| 3996 | { |
| 3997 | return __page_symlink(inode, symname, len, |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 3998 | !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 3999 | } |
| 4000 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 4001 | const struct inode_operations page_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | .readlink = generic_readlink, |
| 4003 | .follow_link = page_follow_link_light, |
| 4004 | .put_link = page_put_link, |
| 4005 | }; |
| 4006 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 4007 | EXPORT_SYMBOL(user_path_at); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 4008 | EXPORT_SYMBOL(follow_down_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | EXPORT_SYMBOL(follow_down); |
| 4010 | EXPORT_SYMBOL(follow_up); |
Al Viro | f6d2ac5 | 2012-08-26 12:55:54 -0400 | [diff] [blame] | 4011 | EXPORT_SYMBOL(get_write_access); /* nfsd */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | EXPORT_SYMBOL(lock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4013 | EXPORT_SYMBOL(lookup_one_len); |
| 4014 | EXPORT_SYMBOL(page_follow_link_light); |
| 4015 | EXPORT_SYMBOL(page_put_link); |
| 4016 | EXPORT_SYMBOL(page_readlink); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4017 | EXPORT_SYMBOL(__page_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | EXPORT_SYMBOL(page_symlink); |
| 4019 | EXPORT_SYMBOL(page_symlink_inode_operations); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 4020 | EXPORT_SYMBOL(kern_path); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 4021 | EXPORT_SYMBOL(vfs_path_lookup); |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 4022 | EXPORT_SYMBOL(inode_permission); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | EXPORT_SYMBOL(unlock_rename); |
| 4024 | EXPORT_SYMBOL(vfs_create); |
| 4025 | EXPORT_SYMBOL(vfs_follow_link); |
| 4026 | EXPORT_SYMBOL(vfs_link); |
| 4027 | EXPORT_SYMBOL(vfs_mkdir); |
| 4028 | EXPORT_SYMBOL(vfs_mknod); |
| 4029 | EXPORT_SYMBOL(generic_permission); |
| 4030 | EXPORT_SYMBOL(vfs_readlink); |
| 4031 | EXPORT_SYMBOL(vfs_rename); |
| 4032 | EXPORT_SYMBOL(vfs_rmdir); |
| 4033 | EXPORT_SYMBOL(vfs_symlink); |
| 4034 | EXPORT_SYMBOL(vfs_unlink); |
| 4035 | EXPORT_SYMBOL(dentry_unhash); |
| 4036 | EXPORT_SYMBOL(generic_readlink); |