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