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