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