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