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