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