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