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> | |
22 | #include <linux/quotaops.h> | |
23 | #include <linux/pagemap.h> | |
0eeca283 | 24 | #include <linux/fsnotify.h> |
1da177e4 LT |
25 | #include <linux/personality.h> |
26 | #include <linux/security.h> | |
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> |
1da177e4 LT |
34 | #include <asm/namei.h> |
35 | #include <asm/uaccess.h> | |
36 | ||
37 | #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE]) | |
38 | ||
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 | ||
a02f76c3 | 110 | static int __link_path_walk(const char *name, struct nameidata *nd); |
c4a7808f | 111 | |
1da177e4 LT |
112 | /* In order to reduce some races, while at the same time doing additional |
113 | * checking and hopefully speeding things up, we copy filenames to the | |
114 | * kernel data space before using them.. | |
115 | * | |
116 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). | |
117 | * PATH_MAX includes the nul terminator --RR. | |
118 | */ | |
858119e1 | 119 | static int do_getname(const char __user *filename, char *page) |
1da177e4 LT |
120 | { |
121 | int retval; | |
122 | unsigned long len = PATH_MAX; | |
123 | ||
124 | if (!segment_eq(get_fs(), KERNEL_DS)) { | |
125 | if ((unsigned long) filename >= TASK_SIZE) | |
126 | return -EFAULT; | |
127 | if (TASK_SIZE - (unsigned long) filename < PATH_MAX) | |
128 | len = TASK_SIZE - (unsigned long) filename; | |
129 | } | |
130 | ||
131 | retval = strncpy_from_user(page, filename, len); | |
132 | if (retval > 0) { | |
133 | if (retval < len) | |
134 | return 0; | |
135 | return -ENAMETOOLONG; | |
136 | } else if (!retval) | |
137 | retval = -ENOENT; | |
138 | return retval; | |
139 | } | |
140 | ||
141 | char * getname(const char __user * filename) | |
142 | { | |
143 | char *tmp, *result; | |
144 | ||
145 | result = ERR_PTR(-ENOMEM); | |
146 | tmp = __getname(); | |
147 | if (tmp) { | |
148 | int retval = do_getname(filename, tmp); | |
149 | ||
150 | result = tmp; | |
151 | if (retval < 0) { | |
152 | __putname(tmp); | |
153 | result = ERR_PTR(retval); | |
154 | } | |
155 | } | |
156 | audit_getname(result); | |
157 | return result; | |
158 | } | |
159 | ||
160 | #ifdef CONFIG_AUDITSYSCALL | |
161 | void putname(const char *name) | |
162 | { | |
5ac3a9c2 | 163 | if (unlikely(!audit_dummy_context())) |
1da177e4 LT |
164 | audit_putname(name); |
165 | else | |
166 | __putname(name); | |
167 | } | |
168 | EXPORT_SYMBOL(putname); | |
169 | #endif | |
170 | ||
171 | ||
172 | /** | |
173 | * generic_permission - check for access rights on a Posix-like filesystem | |
174 | * @inode: inode to check access rights for | |
175 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) | |
176 | * @check_acl: optional callback to check for Posix ACLs | |
177 | * | |
178 | * Used to check for read/write/execute permissions on a file. | |
179 | * We use "fsuid" for this, letting us set arbitrary permissions | |
180 | * for filesystem access without changing the "normal" uids which | |
181 | * are used for other things.. | |
182 | */ | |
183 | int generic_permission(struct inode *inode, int mask, | |
184 | int (*check_acl)(struct inode *inode, int mask)) | |
185 | { | |
186 | umode_t mode = inode->i_mode; | |
187 | ||
e6305c43 AV |
188 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
189 | ||
1da177e4 LT |
190 | if (current->fsuid == inode->i_uid) |
191 | mode >>= 6; | |
192 | else { | |
193 | if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { | |
194 | int error = check_acl(inode, mask); | |
195 | if (error == -EACCES) | |
196 | goto check_capabilities; | |
197 | else if (error != -EAGAIN) | |
198 | return error; | |
199 | } | |
200 | ||
201 | if (in_group_p(inode->i_gid)) | |
202 | mode >>= 3; | |
203 | } | |
204 | ||
205 | /* | |
206 | * If the DACs are ok we don't need any capability check. | |
207 | */ | |
e6305c43 | 208 | if ((mask & ~mode) == 0) |
1da177e4 LT |
209 | return 0; |
210 | ||
211 | check_capabilities: | |
212 | /* | |
213 | * Read/write DACs are always overridable. | |
214 | * Executable DACs are overridable if at least one exec bit is set. | |
215 | */ | |
216 | if (!(mask & MAY_EXEC) || | |
217 | (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode)) | |
218 | if (capable(CAP_DAC_OVERRIDE)) | |
219 | return 0; | |
220 | ||
221 | /* | |
222 | * Searching includes executable on directories, else just read. | |
223 | */ | |
224 | if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) | |
225 | if (capable(CAP_DAC_READ_SEARCH)) | |
226 | return 0; | |
227 | ||
228 | return -EACCES; | |
229 | } | |
230 | ||
231 | int permission(struct inode *inode, int mask, struct nameidata *nd) | |
232 | { | |
e6305c43 | 233 | int retval; |
c7eb2667 DH |
234 | struct vfsmount *mnt = NULL; |
235 | ||
236 | if (nd) | |
4ac91378 | 237 | mnt = nd->path.mnt; |
1da177e4 LT |
238 | |
239 | if (mask & MAY_WRITE) { | |
22590e41 | 240 | umode_t mode = inode->i_mode; |
1da177e4 LT |
241 | |
242 | /* | |
243 | * Nobody gets write access to a read-only fs. | |
244 | */ | |
245 | if (IS_RDONLY(inode) && | |
246 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) | |
247 | return -EROFS; | |
248 | ||
249 | /* | |
250 | * Nobody gets write access to an immutable file. | |
251 | */ | |
252 | if (IS_IMMUTABLE(inode)) | |
253 | return -EACCES; | |
254 | } | |
255 | ||
22590e41 MS |
256 | if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { |
257 | /* | |
258 | * MAY_EXEC on regular files is denied if the fs is mounted | |
259 | * with the "noexec" flag. | |
260 | */ | |
c7eb2667 | 261 | if (mnt && (mnt->mnt_flags & MNT_NOEXEC)) |
22590e41 MS |
262 | return -EACCES; |
263 | } | |
a343bb77 | 264 | |
1da177e4 | 265 | /* Ordinary permission routines do not understand MAY_APPEND. */ |
22590e41 | 266 | if (inode->i_op && inode->i_op->permission) { |
e6305c43 AV |
267 | int extra = 0; |
268 | if (nd) { | |
269 | if (nd->flags & LOOKUP_ACCESS) | |
270 | extra |= MAY_ACCESS; | |
e6305c43 AV |
271 | if (nd->flags & LOOKUP_OPEN) |
272 | extra |= MAY_OPEN; | |
273 | } | |
274 | retval = inode->i_op->permission(inode, mask | extra); | |
22590e41 MS |
275 | if (!retval) { |
276 | /* | |
277 | * Exec permission on a regular file is denied if none | |
278 | * of the execute bits are set. | |
279 | * | |
280 | * This check should be done by the ->permission() | |
281 | * method. | |
282 | */ | |
283 | if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) && | |
284 | !(inode->i_mode & S_IXUGO)) | |
285 | return -EACCES; | |
286 | } | |
287 | } else { | |
e6305c43 | 288 | retval = generic_permission(inode, mask, NULL); |
22590e41 | 289 | } |
1da177e4 LT |
290 | if (retval) |
291 | return retval; | |
292 | ||
08ce5f16 SH |
293 | retval = devcgroup_inode_permission(inode, mask); |
294 | if (retval) | |
295 | return retval; | |
296 | ||
e6305c43 AV |
297 | return security_inode_permission(inode, |
298 | mask & (MAY_READ|MAY_WRITE|MAY_EXEC), nd); | |
1da177e4 LT |
299 | } |
300 | ||
e4543edd CH |
301 | /** |
302 | * vfs_permission - check for access rights to a given path | |
303 | * @nd: lookup result that describes the path | |
304 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) | |
305 | * | |
306 | * Used to check for read/write/execute permissions on a path. | |
307 | * We use "fsuid" for this, letting us set arbitrary permissions | |
308 | * for filesystem access without changing the "normal" uids which | |
309 | * are used for other things. | |
310 | */ | |
311 | int vfs_permission(struct nameidata *nd, int mask) | |
312 | { | |
4ac91378 | 313 | return permission(nd->path.dentry->d_inode, mask, nd); |
e4543edd CH |
314 | } |
315 | ||
8c744fb8 CH |
316 | /** |
317 | * file_permission - check for additional access rights to a given file | |
318 | * @file: file to check access rights for | |
319 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) | |
320 | * | |
321 | * Used to check for read/write/execute permissions on an already opened | |
322 | * file. | |
323 | * | |
324 | * Note: | |
325 | * Do not use this function in new code. All access checks should | |
326 | * be done using vfs_permission(). | |
327 | */ | |
328 | int file_permission(struct file *file, int mask) | |
329 | { | |
0f7fc9e4 | 330 | return permission(file->f_path.dentry->d_inode, mask, NULL); |
8c744fb8 CH |
331 | } |
332 | ||
1da177e4 LT |
333 | /* |
334 | * get_write_access() gets write permission for a file. | |
335 | * put_write_access() releases this write permission. | |
336 | * This is used for regular files. | |
337 | * We cannot support write (and maybe mmap read-write shared) accesses and | |
338 | * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode | |
339 | * can have the following values: | |
340 | * 0: no writers, no VM_DENYWRITE mappings | |
341 | * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist | |
342 | * > 0: (i_writecount) users are writing to the file. | |
343 | * | |
344 | * Normally we operate on that counter with atomic_{inc,dec} and it's safe | |
345 | * except for the cases where we don't hold i_writecount yet. Then we need to | |
346 | * use {get,deny}_write_access() - these functions check the sign and refuse | |
347 | * to do the change if sign is wrong. Exclusion between them is provided by | |
348 | * the inode->i_lock spinlock. | |
349 | */ | |
350 | ||
351 | int get_write_access(struct inode * inode) | |
352 | { | |
353 | spin_lock(&inode->i_lock); | |
354 | if (atomic_read(&inode->i_writecount) < 0) { | |
355 | spin_unlock(&inode->i_lock); | |
356 | return -ETXTBSY; | |
357 | } | |
358 | atomic_inc(&inode->i_writecount); | |
359 | spin_unlock(&inode->i_lock); | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
364 | int deny_write_access(struct file * file) | |
365 | { | |
0f7fc9e4 | 366 | struct inode *inode = file->f_path.dentry->d_inode; |
1da177e4 LT |
367 | |
368 | spin_lock(&inode->i_lock); | |
369 | if (atomic_read(&inode->i_writecount) > 0) { | |
370 | spin_unlock(&inode->i_lock); | |
371 | return -ETXTBSY; | |
372 | } | |
373 | atomic_dec(&inode->i_writecount); | |
374 | spin_unlock(&inode->i_lock); | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
5dd784d0 JB |
379 | /** |
380 | * path_get - get a reference to a path | |
381 | * @path: path to get the reference to | |
382 | * | |
383 | * Given a path increment the reference count to the dentry and the vfsmount. | |
384 | */ | |
385 | void path_get(struct path *path) | |
386 | { | |
387 | mntget(path->mnt); | |
388 | dget(path->dentry); | |
389 | } | |
390 | EXPORT_SYMBOL(path_get); | |
391 | ||
1d957f9b JB |
392 | /** |
393 | * path_put - put a reference to a path | |
394 | * @path: path to put the reference to | |
395 | * | |
396 | * Given a path decrement the reference count to the dentry and the vfsmount. | |
397 | */ | |
398 | void path_put(struct path *path) | |
1da177e4 | 399 | { |
1d957f9b JB |
400 | dput(path->dentry); |
401 | mntput(path->mnt); | |
1da177e4 | 402 | } |
1d957f9b | 403 | EXPORT_SYMBOL(path_put); |
1da177e4 | 404 | |
834f2a4a TM |
405 | /** |
406 | * release_open_intent - free up open intent resources | |
407 | * @nd: pointer to nameidata | |
408 | */ | |
409 | void release_open_intent(struct nameidata *nd) | |
410 | { | |
0f7fc9e4 | 411 | if (nd->intent.open.file->f_path.dentry == NULL) |
834f2a4a TM |
412 | put_filp(nd->intent.open.file); |
413 | else | |
414 | fput(nd->intent.open.file); | |
415 | } | |
416 | ||
bcdc5e01 IK |
417 | static inline struct dentry * |
418 | do_revalidate(struct dentry *dentry, struct nameidata *nd) | |
419 | { | |
420 | int status = dentry->d_op->d_revalidate(dentry, nd); | |
421 | if (unlikely(status <= 0)) { | |
422 | /* | |
423 | * The dentry failed validation. | |
424 | * If d_revalidate returned 0 attempt to invalidate | |
425 | * the dentry otherwise d_revalidate is asking us | |
426 | * to return a fail status. | |
427 | */ | |
428 | if (!status) { | |
429 | if (!d_invalidate(dentry)) { | |
430 | dput(dentry); | |
431 | dentry = NULL; | |
432 | } | |
433 | } else { | |
434 | dput(dentry); | |
435 | dentry = ERR_PTR(status); | |
436 | } | |
437 | } | |
438 | return dentry; | |
439 | } | |
440 | ||
1da177e4 LT |
441 | /* |
442 | * Internal lookup() using the new generic dcache. | |
443 | * SMP-safe | |
444 | */ | |
445 | static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd) | |
446 | { | |
447 | struct dentry * dentry = __d_lookup(parent, name); | |
448 | ||
449 | /* lockess __d_lookup may fail due to concurrent d_move() | |
450 | * in some unrelated directory, so try with d_lookup | |
451 | */ | |
452 | if (!dentry) | |
453 | dentry = d_lookup(parent, name); | |
454 | ||
bcdc5e01 IK |
455 | if (dentry && dentry->d_op && dentry->d_op->d_revalidate) |
456 | dentry = do_revalidate(dentry, nd); | |
457 | ||
1da177e4 LT |
458 | return dentry; |
459 | } | |
460 | ||
461 | /* | |
462 | * Short-cut version of permission(), for calling by | |
463 | * path_walk(), when dcache lock is held. Combines parts | |
464 | * of permission() and generic_permission(), and tests ONLY for | |
465 | * MAY_EXEC permission. | |
466 | * | |
467 | * If appropriate, check DAC only. If not appropriate, or | |
468 | * short-cut DAC fails, then call permission() to do more | |
469 | * complete permission check. | |
470 | */ | |
858119e1 | 471 | static int exec_permission_lite(struct inode *inode, |
1da177e4 LT |
472 | struct nameidata *nd) |
473 | { | |
474 | umode_t mode = inode->i_mode; | |
475 | ||
476 | if (inode->i_op && inode->i_op->permission) | |
477 | return -EAGAIN; | |
478 | ||
479 | if (current->fsuid == inode->i_uid) | |
480 | mode >>= 6; | |
481 | else if (in_group_p(inode->i_gid)) | |
482 | mode >>= 3; | |
483 | ||
484 | if (mode & MAY_EXEC) | |
485 | goto ok; | |
486 | ||
487 | if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE)) | |
488 | goto ok; | |
489 | ||
490 | if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE)) | |
491 | goto ok; | |
492 | ||
493 | if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH)) | |
494 | goto ok; | |
495 | ||
496 | return -EACCES; | |
497 | ok: | |
498 | return security_inode_permission(inode, MAY_EXEC, nd); | |
499 | } | |
500 | ||
501 | /* | |
502 | * This is called when everything else fails, and we actually have | |
503 | * to go to the low-level filesystem to find out what we should do.. | |
504 | * | |
505 | * We get the directory semaphore, and after getting that we also | |
506 | * make sure that nobody added the entry to the dcache in the meantime.. | |
507 | * SMP-safe | |
508 | */ | |
509 | static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd) | |
510 | { | |
511 | struct dentry * result; | |
512 | struct inode *dir = parent->d_inode; | |
513 | ||
1b1dcc1b | 514 | mutex_lock(&dir->i_mutex); |
1da177e4 LT |
515 | /* |
516 | * First re-do the cached lookup just in case it was created | |
517 | * while we waited for the directory semaphore.. | |
518 | * | |
519 | * FIXME! This could use version numbering or similar to | |
520 | * avoid unnecessary cache lookups. | |
521 | * | |
522 | * The "dcache_lock" is purely to protect the RCU list walker | |
523 | * from concurrent renames at this point (we mustn't get false | |
524 | * negatives from the RCU list walk here, unlike the optimistic | |
525 | * fast walk). | |
526 | * | |
527 | * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup | |
528 | */ | |
529 | result = d_lookup(parent, name); | |
530 | if (!result) { | |
d70b67c8 MS |
531 | struct dentry *dentry; |
532 | ||
533 | /* Don't create child dentry for a dead directory. */ | |
534 | result = ERR_PTR(-ENOENT); | |
535 | if (IS_DEADDIR(dir)) | |
536 | goto out_unlock; | |
537 | ||
538 | dentry = d_alloc(parent, name); | |
1da177e4 LT |
539 | result = ERR_PTR(-ENOMEM); |
540 | if (dentry) { | |
541 | result = dir->i_op->lookup(dir, dentry, nd); | |
542 | if (result) | |
543 | dput(dentry); | |
544 | else | |
545 | result = dentry; | |
546 | } | |
d70b67c8 | 547 | out_unlock: |
1b1dcc1b | 548 | mutex_unlock(&dir->i_mutex); |
1da177e4 LT |
549 | return result; |
550 | } | |
551 | ||
552 | /* | |
553 | * Uhhuh! Nasty case: the cache was re-populated while | |
554 | * we waited on the semaphore. Need to revalidate. | |
555 | */ | |
1b1dcc1b | 556 | mutex_unlock(&dir->i_mutex); |
1da177e4 | 557 | if (result->d_op && result->d_op->d_revalidate) { |
bcdc5e01 IK |
558 | result = do_revalidate(result, nd); |
559 | if (!result) | |
1da177e4 | 560 | result = ERR_PTR(-ENOENT); |
1da177e4 LT |
561 | } |
562 | return result; | |
563 | } | |
564 | ||
565 | static int __emul_lookup_dentry(const char *, struct nameidata *); | |
566 | ||
567 | /* SMP-safe */ | |
f1662356 | 568 | static __always_inline int |
1da177e4 LT |
569 | walk_init_root(const char *name, struct nameidata *nd) |
570 | { | |
e518ddb7 AM |
571 | struct fs_struct *fs = current->fs; |
572 | ||
573 | read_lock(&fs->lock); | |
6ac08c39 JB |
574 | if (fs->altroot.dentry && !(nd->flags & LOOKUP_NOALT)) { |
575 | nd->path = fs->altroot; | |
576 | path_get(&fs->altroot); | |
e518ddb7 | 577 | read_unlock(&fs->lock); |
1da177e4 LT |
578 | if (__emul_lookup_dentry(name,nd)) |
579 | return 0; | |
e518ddb7 | 580 | read_lock(&fs->lock); |
1da177e4 | 581 | } |
6ac08c39 JB |
582 | nd->path = fs->root; |
583 | path_get(&fs->root); | |
e518ddb7 | 584 | read_unlock(&fs->lock); |
1da177e4 LT |
585 | return 1; |
586 | } | |
587 | ||
a02f76c3 AV |
588 | /* |
589 | * Wrapper to retry pathname resolution whenever the underlying | |
590 | * file system returns an ESTALE. | |
591 | * | |
592 | * Retry the whole path once, forcing real lookup requests | |
593 | * instead of relying on the dcache. | |
594 | */ | |
595 | static __always_inline int link_path_walk(const char *name, struct nameidata *nd) | |
596 | { | |
597 | struct path save = nd->path; | |
598 | int result; | |
599 | ||
600 | /* make sure the stuff we saved doesn't go away */ | |
c8e7f449 | 601 | path_get(&save); |
a02f76c3 AV |
602 | |
603 | result = __link_path_walk(name, nd); | |
604 | if (result == -ESTALE) { | |
605 | /* nd->path had been dropped */ | |
606 | nd->path = save; | |
c8e7f449 | 607 | path_get(&nd->path); |
a02f76c3 AV |
608 | nd->flags |= LOOKUP_REVAL; |
609 | result = __link_path_walk(name, nd); | |
610 | } | |
611 | ||
612 | path_put(&save); | |
613 | ||
614 | return result; | |
615 | } | |
616 | ||
f1662356 | 617 | static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) |
1da177e4 LT |
618 | { |
619 | int res = 0; | |
620 | char *name; | |
621 | if (IS_ERR(link)) | |
622 | goto fail; | |
623 | ||
624 | if (*link == '/') { | |
1d957f9b | 625 | path_put(&nd->path); |
1da177e4 LT |
626 | if (!walk_init_root(link, nd)) |
627 | /* weird __emul_prefix() stuff did it */ | |
628 | goto out; | |
629 | } | |
630 | res = link_path_walk(link, nd); | |
631 | out: | |
632 | if (nd->depth || res || nd->last_type!=LAST_NORM) | |
633 | return res; | |
634 | /* | |
635 | * If it is an iterative symlinks resolution in open_namei() we | |
636 | * have to copy the last component. And all that crap because of | |
637 | * bloody create() on broken symlinks. Furrfu... | |
638 | */ | |
639 | name = __getname(); | |
640 | if (unlikely(!name)) { | |
1d957f9b | 641 | path_put(&nd->path); |
1da177e4 LT |
642 | return -ENOMEM; |
643 | } | |
644 | strcpy(name, nd->last.name); | |
645 | nd->last.name = name; | |
646 | return 0; | |
647 | fail: | |
1d957f9b | 648 | path_put(&nd->path); |
1da177e4 LT |
649 | return PTR_ERR(link); |
650 | } | |
651 | ||
1d957f9b | 652 | static void path_put_conditional(struct path *path, struct nameidata *nd) |
051d3812 IK |
653 | { |
654 | dput(path->dentry); | |
4ac91378 | 655 | if (path->mnt != nd->path.mnt) |
051d3812 IK |
656 | mntput(path->mnt); |
657 | } | |
658 | ||
659 | static inline void path_to_nameidata(struct path *path, struct nameidata *nd) | |
660 | { | |
4ac91378 JB |
661 | dput(nd->path.dentry); |
662 | if (nd->path.mnt != path->mnt) | |
663 | mntput(nd->path.mnt); | |
664 | nd->path.mnt = path->mnt; | |
665 | nd->path.dentry = path->dentry; | |
051d3812 IK |
666 | } |
667 | ||
f1662356 | 668 | static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd) |
1da177e4 LT |
669 | { |
670 | int error; | |
cc314eef | 671 | void *cookie; |
cd4e91d3 | 672 | struct dentry *dentry = path->dentry; |
1da177e4 | 673 | |
d671a1cb | 674 | touch_atime(path->mnt, dentry); |
1da177e4 | 675 | nd_set_link(nd, NULL); |
cd4e91d3 | 676 | |
4ac91378 | 677 | if (path->mnt != nd->path.mnt) { |
051d3812 IK |
678 | path_to_nameidata(path, nd); |
679 | dget(dentry); | |
680 | } | |
681 | mntget(path->mnt); | |
cc314eef LT |
682 | cookie = dentry->d_inode->i_op->follow_link(dentry, nd); |
683 | error = PTR_ERR(cookie); | |
684 | if (!IS_ERR(cookie)) { | |
1da177e4 | 685 | char *s = nd_get_link(nd); |
cc314eef | 686 | error = 0; |
1da177e4 LT |
687 | if (s) |
688 | error = __vfs_follow_link(nd, s); | |
689 | if (dentry->d_inode->i_op->put_link) | |
cc314eef | 690 | dentry->d_inode->i_op->put_link(dentry, nd, cookie); |
1da177e4 | 691 | } |
09da5916 | 692 | path_put(path); |
1da177e4 LT |
693 | |
694 | return error; | |
695 | } | |
696 | ||
697 | /* | |
698 | * This limits recursive symlink follows to 8, while | |
699 | * limiting consecutive symlinks to 40. | |
700 | * | |
701 | * Without that kind of total limit, nasty chains of consecutive | |
702 | * symlinks can cause almost arbitrarily long lookups. | |
703 | */ | |
90ebe565 | 704 | static inline int do_follow_link(struct path *path, struct nameidata *nd) |
1da177e4 LT |
705 | { |
706 | int err = -ELOOP; | |
707 | if (current->link_count >= MAX_NESTED_LINKS) | |
708 | goto loop; | |
709 | if (current->total_link_count >= 40) | |
710 | goto loop; | |
711 | BUG_ON(nd->depth >= MAX_NESTED_LINKS); | |
712 | cond_resched(); | |
90ebe565 | 713 | err = security_inode_follow_link(path->dentry, nd); |
1da177e4 LT |
714 | if (err) |
715 | goto loop; | |
716 | current->link_count++; | |
717 | current->total_link_count++; | |
718 | nd->depth++; | |
cd4e91d3 | 719 | err = __do_follow_link(path, nd); |
839d9f93 AV |
720 | current->link_count--; |
721 | nd->depth--; | |
1da177e4 LT |
722 | return err; |
723 | loop: | |
1d957f9b JB |
724 | path_put_conditional(path, nd); |
725 | path_put(&nd->path); | |
1da177e4 LT |
726 | return err; |
727 | } | |
728 | ||
729 | int follow_up(struct vfsmount **mnt, struct dentry **dentry) | |
730 | { | |
731 | struct vfsmount *parent; | |
732 | struct dentry *mountpoint; | |
733 | spin_lock(&vfsmount_lock); | |
734 | parent=(*mnt)->mnt_parent; | |
735 | if (parent == *mnt) { | |
736 | spin_unlock(&vfsmount_lock); | |
737 | return 0; | |
738 | } | |
739 | mntget(parent); | |
740 | mountpoint=dget((*mnt)->mnt_mountpoint); | |
741 | spin_unlock(&vfsmount_lock); | |
742 | dput(*dentry); | |
743 | *dentry = mountpoint; | |
744 | mntput(*mnt); | |
745 | *mnt = parent; | |
746 | return 1; | |
747 | } | |
748 | ||
749 | /* no need for dcache_lock, as serialization is taken care in | |
750 | * namespace.c | |
751 | */ | |
463ffb2e AV |
752 | static int __follow_mount(struct path *path) |
753 | { | |
754 | int res = 0; | |
755 | while (d_mountpoint(path->dentry)) { | |
756 | struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry); | |
757 | if (!mounted) | |
758 | break; | |
759 | dput(path->dentry); | |
760 | if (res) | |
761 | mntput(path->mnt); | |
762 | path->mnt = mounted; | |
763 | path->dentry = dget(mounted->mnt_root); | |
764 | res = 1; | |
765 | } | |
766 | return res; | |
767 | } | |
768 | ||
58c465eb | 769 | static void follow_mount(struct vfsmount **mnt, struct dentry **dentry) |
1da177e4 | 770 | { |
1da177e4 LT |
771 | while (d_mountpoint(*dentry)) { |
772 | struct vfsmount *mounted = lookup_mnt(*mnt, *dentry); | |
773 | if (!mounted) | |
774 | break; | |
58c465eb | 775 | dput(*dentry); |
1da177e4 LT |
776 | mntput(*mnt); |
777 | *mnt = mounted; | |
1da177e4 | 778 | *dentry = dget(mounted->mnt_root); |
1da177e4 | 779 | } |
1da177e4 LT |
780 | } |
781 | ||
782 | /* no need for dcache_lock, as serialization is taken care in | |
783 | * namespace.c | |
784 | */ | |
e13b210f | 785 | int follow_down(struct vfsmount **mnt, struct dentry **dentry) |
1da177e4 LT |
786 | { |
787 | struct vfsmount *mounted; | |
788 | ||
789 | mounted = lookup_mnt(*mnt, *dentry); | |
790 | if (mounted) { | |
e13b210f | 791 | dput(*dentry); |
1da177e4 LT |
792 | mntput(*mnt); |
793 | *mnt = mounted; | |
1da177e4 LT |
794 | *dentry = dget(mounted->mnt_root); |
795 | return 1; | |
796 | } | |
797 | return 0; | |
798 | } | |
799 | ||
f1662356 | 800 | static __always_inline void follow_dotdot(struct nameidata *nd) |
1da177e4 | 801 | { |
e518ddb7 AM |
802 | struct fs_struct *fs = current->fs; |
803 | ||
1da177e4 LT |
804 | while(1) { |
805 | struct vfsmount *parent; | |
4ac91378 | 806 | struct dentry *old = nd->path.dentry; |
1da177e4 | 807 | |
e518ddb7 | 808 | read_lock(&fs->lock); |
6ac08c39 JB |
809 | if (nd->path.dentry == fs->root.dentry && |
810 | nd->path.mnt == fs->root.mnt) { | |
e518ddb7 | 811 | read_unlock(&fs->lock); |
1da177e4 LT |
812 | break; |
813 | } | |
e518ddb7 | 814 | read_unlock(&fs->lock); |
1da177e4 | 815 | spin_lock(&dcache_lock); |
4ac91378 JB |
816 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
817 | nd->path.dentry = dget(nd->path.dentry->d_parent); | |
1da177e4 LT |
818 | spin_unlock(&dcache_lock); |
819 | dput(old); | |
820 | break; | |
821 | } | |
822 | spin_unlock(&dcache_lock); | |
823 | spin_lock(&vfsmount_lock); | |
4ac91378 JB |
824 | parent = nd->path.mnt->mnt_parent; |
825 | if (parent == nd->path.mnt) { | |
1da177e4 LT |
826 | spin_unlock(&vfsmount_lock); |
827 | break; | |
828 | } | |
829 | mntget(parent); | |
4ac91378 | 830 | nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint); |
1da177e4 LT |
831 | spin_unlock(&vfsmount_lock); |
832 | dput(old); | |
4ac91378 JB |
833 | mntput(nd->path.mnt); |
834 | nd->path.mnt = parent; | |
1da177e4 | 835 | } |
4ac91378 | 836 | follow_mount(&nd->path.mnt, &nd->path.dentry); |
1da177e4 LT |
837 | } |
838 | ||
1da177e4 LT |
839 | /* |
840 | * It's more convoluted than I'd like it to be, but... it's still fairly | |
841 | * small and for now I'd prefer to have fast path as straight as possible. | |
842 | * It _is_ time-critical. | |
843 | */ | |
844 | static int do_lookup(struct nameidata *nd, struct qstr *name, | |
845 | struct path *path) | |
846 | { | |
4ac91378 JB |
847 | struct vfsmount *mnt = nd->path.mnt; |
848 | struct dentry *dentry = __d_lookup(nd->path.dentry, name); | |
1da177e4 LT |
849 | |
850 | if (!dentry) | |
851 | goto need_lookup; | |
852 | if (dentry->d_op && dentry->d_op->d_revalidate) | |
853 | goto need_revalidate; | |
854 | done: | |
855 | path->mnt = mnt; | |
856 | path->dentry = dentry; | |
634ee701 | 857 | __follow_mount(path); |
1da177e4 LT |
858 | return 0; |
859 | ||
860 | need_lookup: | |
4ac91378 | 861 | dentry = real_lookup(nd->path.dentry, name, nd); |
1da177e4 LT |
862 | if (IS_ERR(dentry)) |
863 | goto fail; | |
864 | goto done; | |
865 | ||
866 | need_revalidate: | |
bcdc5e01 IK |
867 | dentry = do_revalidate(dentry, nd); |
868 | if (!dentry) | |
869 | goto need_lookup; | |
870 | if (IS_ERR(dentry)) | |
871 | goto fail; | |
872 | goto done; | |
1da177e4 LT |
873 | |
874 | fail: | |
875 | return PTR_ERR(dentry); | |
876 | } | |
877 | ||
878 | /* | |
879 | * Name resolution. | |
ea3834d9 PM |
880 | * This is the basic name resolution function, turning a pathname into |
881 | * the final dentry. We expect 'base' to be positive and a directory. | |
1da177e4 | 882 | * |
ea3834d9 PM |
883 | * Returns 0 and nd will have valid dentry and mnt on success. |
884 | * Returns error and drops reference to input namei data on failure. | |
1da177e4 | 885 | */ |
fc9b52cd | 886 | static int __link_path_walk(const char *name, struct nameidata *nd) |
1da177e4 LT |
887 | { |
888 | struct path next; | |
889 | struct inode *inode; | |
890 | int err; | |
891 | unsigned int lookup_flags = nd->flags; | |
892 | ||
893 | while (*name=='/') | |
894 | name++; | |
895 | if (!*name) | |
896 | goto return_reval; | |
897 | ||
4ac91378 | 898 | inode = nd->path.dentry->d_inode; |
1da177e4 | 899 | if (nd->depth) |
f55eab82 | 900 | lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); |
1da177e4 LT |
901 | |
902 | /* At this point we know we have a real path component. */ | |
903 | for(;;) { | |
904 | unsigned long hash; | |
905 | struct qstr this; | |
906 | unsigned int c; | |
907 | ||
cdce5d6b | 908 | nd->flags |= LOOKUP_CONTINUE; |
1da177e4 | 909 | err = exec_permission_lite(inode, nd); |
e4543edd CH |
910 | if (err == -EAGAIN) |
911 | err = vfs_permission(nd, MAY_EXEC); | |
1da177e4 LT |
912 | if (err) |
913 | break; | |
914 | ||
915 | this.name = name; | |
916 | c = *(const unsigned char *)name; | |
917 | ||
918 | hash = init_name_hash(); | |
919 | do { | |
920 | name++; | |
921 | hash = partial_name_hash(c, hash); | |
922 | c = *(const unsigned char *)name; | |
923 | } while (c && (c != '/')); | |
924 | this.len = name - (const char *) this.name; | |
925 | this.hash = end_name_hash(hash); | |
926 | ||
927 | /* remove trailing slashes? */ | |
928 | if (!c) | |
929 | goto last_component; | |
930 | while (*++name == '/'); | |
931 | if (!*name) | |
932 | goto last_with_slashes; | |
933 | ||
934 | /* | |
935 | * "." and ".." are special - ".." especially so because it has | |
936 | * to be able to know about the current root directory and | |
937 | * parent relationships. | |
938 | */ | |
939 | if (this.name[0] == '.') switch (this.len) { | |
940 | default: | |
941 | break; | |
942 | case 2: | |
943 | if (this.name[1] != '.') | |
944 | break; | |
58c465eb | 945 | follow_dotdot(nd); |
4ac91378 | 946 | inode = nd->path.dentry->d_inode; |
1da177e4 LT |
947 | /* fallthrough */ |
948 | case 1: | |
949 | continue; | |
950 | } | |
951 | /* | |
952 | * See if the low-level filesystem might want | |
953 | * to use its own hash.. | |
954 | */ | |
4ac91378 JB |
955 | if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) { |
956 | err = nd->path.dentry->d_op->d_hash(nd->path.dentry, | |
957 | &this); | |
1da177e4 LT |
958 | if (err < 0) |
959 | break; | |
960 | } | |
1da177e4 LT |
961 | /* This does the actual lookups.. */ |
962 | err = do_lookup(nd, &this, &next); | |
963 | if (err) | |
964 | break; | |
1da177e4 LT |
965 | |
966 | err = -ENOENT; | |
967 | inode = next.dentry->d_inode; | |
968 | if (!inode) | |
969 | goto out_dput; | |
970 | err = -ENOTDIR; | |
971 | if (!inode->i_op) | |
972 | goto out_dput; | |
973 | ||
974 | if (inode->i_op->follow_link) { | |
90ebe565 | 975 | err = do_follow_link(&next, nd); |
1da177e4 LT |
976 | if (err) |
977 | goto return_err; | |
978 | err = -ENOENT; | |
4ac91378 | 979 | inode = nd->path.dentry->d_inode; |
1da177e4 LT |
980 | if (!inode) |
981 | break; | |
982 | err = -ENOTDIR; | |
983 | if (!inode->i_op) | |
984 | break; | |
09dd17d3 MS |
985 | } else |
986 | path_to_nameidata(&next, nd); | |
1da177e4 LT |
987 | err = -ENOTDIR; |
988 | if (!inode->i_op->lookup) | |
989 | break; | |
990 | continue; | |
991 | /* here ends the main loop */ | |
992 | ||
993 | last_with_slashes: | |
994 | lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
995 | last_component: | |
f55eab82 TM |
996 | /* Clear LOOKUP_CONTINUE iff it was previously unset */ |
997 | nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; | |
1da177e4 LT |
998 | if (lookup_flags & LOOKUP_PARENT) |
999 | goto lookup_parent; | |
1000 | if (this.name[0] == '.') switch (this.len) { | |
1001 | default: | |
1002 | break; | |
1003 | case 2: | |
1004 | if (this.name[1] != '.') | |
1005 | break; | |
58c465eb | 1006 | follow_dotdot(nd); |
4ac91378 | 1007 | inode = nd->path.dentry->d_inode; |
1da177e4 LT |
1008 | /* fallthrough */ |
1009 | case 1: | |
1010 | goto return_reval; | |
1011 | } | |
4ac91378 JB |
1012 | if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) { |
1013 | err = nd->path.dentry->d_op->d_hash(nd->path.dentry, | |
1014 | &this); | |
1da177e4 LT |
1015 | if (err < 0) |
1016 | break; | |
1017 | } | |
1018 | err = do_lookup(nd, &this, &next); | |
1019 | if (err) | |
1020 | break; | |
1da177e4 LT |
1021 | inode = next.dentry->d_inode; |
1022 | if ((lookup_flags & LOOKUP_FOLLOW) | |
1023 | && inode && inode->i_op && inode->i_op->follow_link) { | |
90ebe565 | 1024 | err = do_follow_link(&next, nd); |
1da177e4 LT |
1025 | if (err) |
1026 | goto return_err; | |
4ac91378 | 1027 | inode = nd->path.dentry->d_inode; |
09dd17d3 MS |
1028 | } else |
1029 | path_to_nameidata(&next, nd); | |
1da177e4 LT |
1030 | err = -ENOENT; |
1031 | if (!inode) | |
1032 | break; | |
1033 | if (lookup_flags & LOOKUP_DIRECTORY) { | |
1034 | err = -ENOTDIR; | |
1035 | if (!inode->i_op || !inode->i_op->lookup) | |
1036 | break; | |
1037 | } | |
1038 | goto return_base; | |
1039 | lookup_parent: | |
1040 | nd->last = this; | |
1041 | nd->last_type = LAST_NORM; | |
1042 | if (this.name[0] != '.') | |
1043 | goto return_base; | |
1044 | if (this.len == 1) | |
1045 | nd->last_type = LAST_DOT; | |
1046 | else if (this.len == 2 && this.name[1] == '.') | |
1047 | nd->last_type = LAST_DOTDOT; | |
1048 | else | |
1049 | goto return_base; | |
1050 | return_reval: | |
1051 | /* | |
1052 | * We bypassed the ordinary revalidation routines. | |
1053 | * We may need to check the cached dentry for staleness. | |
1054 | */ | |
4ac91378 JB |
1055 | if (nd->path.dentry && nd->path.dentry->d_sb && |
1056 | (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { | |
1da177e4 LT |
1057 | err = -ESTALE; |
1058 | /* Note: we do not d_invalidate() */ | |
4ac91378 JB |
1059 | if (!nd->path.dentry->d_op->d_revalidate( |
1060 | nd->path.dentry, nd)) | |
1da177e4 LT |
1061 | break; |
1062 | } | |
1063 | return_base: | |
1064 | return 0; | |
1065 | out_dput: | |
1d957f9b | 1066 | path_put_conditional(&next, nd); |
1da177e4 LT |
1067 | break; |
1068 | } | |
1d957f9b | 1069 | path_put(&nd->path); |
1da177e4 LT |
1070 | return_err: |
1071 | return err; | |
1072 | } | |
1073 | ||
fc9b52cd | 1074 | static int path_walk(const char *name, struct nameidata *nd) |
1da177e4 LT |
1075 | { |
1076 | current->total_link_count = 0; | |
1077 | return link_path_walk(name, nd); | |
1078 | } | |
1079 | ||
ea3834d9 PM |
1080 | /* |
1081 | * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if | |
1082 | * everything is done. Returns 0 and drops input nd, if lookup failed; | |
1083 | */ | |
1da177e4 LT |
1084 | static int __emul_lookup_dentry(const char *name, struct nameidata *nd) |
1085 | { | |
1086 | if (path_walk(name, nd)) | |
1087 | return 0; /* something went wrong... */ | |
1088 | ||
4ac91378 JB |
1089 | if (!nd->path.dentry->d_inode || |
1090 | S_ISDIR(nd->path.dentry->d_inode->i_mode)) { | |
09da5916 | 1091 | struct path old_path = nd->path; |
1da177e4 LT |
1092 | struct qstr last = nd->last; |
1093 | int last_type = nd->last_type; | |
e518ddb7 AM |
1094 | struct fs_struct *fs = current->fs; |
1095 | ||
1da177e4 | 1096 | /* |
e518ddb7 AM |
1097 | * NAME was not found in alternate root or it's a directory. |
1098 | * Try to find it in the normal root: | |
1da177e4 LT |
1099 | */ |
1100 | nd->last_type = LAST_ROOT; | |
e518ddb7 | 1101 | read_lock(&fs->lock); |
6ac08c39 JB |
1102 | nd->path = fs->root; |
1103 | path_get(&fs->root); | |
e518ddb7 | 1104 | read_unlock(&fs->lock); |
1da177e4 | 1105 | if (path_walk(name, nd) == 0) { |
4ac91378 | 1106 | if (nd->path.dentry->d_inode) { |
09da5916 | 1107 | path_put(&old_path); |
1da177e4 LT |
1108 | return 1; |
1109 | } | |
1d957f9b | 1110 | path_put(&nd->path); |
1da177e4 | 1111 | } |
09da5916 | 1112 | nd->path = old_path; |
1da177e4 LT |
1113 | nd->last = last; |
1114 | nd->last_type = last_type; | |
1115 | } | |
1116 | return 1; | |
1117 | } | |
1118 | ||
1119 | void set_fs_altroot(void) | |
1120 | { | |
1121 | char *emul = __emul_prefix(); | |
1122 | struct nameidata nd; | |
6ac08c39 | 1123 | struct path path = {}, old_path; |
1da177e4 | 1124 | int err; |
e518ddb7 | 1125 | struct fs_struct *fs = current->fs; |
1da177e4 LT |
1126 | |
1127 | if (!emul) | |
1128 | goto set_it; | |
1129 | err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd); | |
6ac08c39 JB |
1130 | if (!err) |
1131 | path = nd.path; | |
1da177e4 | 1132 | set_it: |
e518ddb7 | 1133 | write_lock(&fs->lock); |
6ac08c39 JB |
1134 | old_path = fs->altroot; |
1135 | fs->altroot = path; | |
e518ddb7 | 1136 | write_unlock(&fs->lock); |
6ac08c39 JB |
1137 | if (old_path.dentry) |
1138 | path_put(&old_path); | |
1da177e4 LT |
1139 | } |
1140 | ||
ea3834d9 | 1141 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
fc9b52cd | 1142 | static int do_path_lookup(int dfd, const char *name, |
5590ff0d | 1143 | unsigned int flags, struct nameidata *nd) |
1da177e4 | 1144 | { |
ea3834d9 | 1145 | int retval = 0; |
170aa3d0 UD |
1146 | int fput_needed; |
1147 | struct file *file; | |
e518ddb7 | 1148 | struct fs_struct *fs = current->fs; |
1da177e4 LT |
1149 | |
1150 | nd->last_type = LAST_ROOT; /* if there are only slashes... */ | |
1151 | nd->flags = flags; | |
1152 | nd->depth = 0; | |
1153 | ||
1da177e4 | 1154 | if (*name=='/') { |
e518ddb7 | 1155 | read_lock(&fs->lock); |
6ac08c39 JB |
1156 | if (fs->altroot.dentry && !(nd->flags & LOOKUP_NOALT)) { |
1157 | nd->path = fs->altroot; | |
1158 | path_get(&fs->altroot); | |
e518ddb7 | 1159 | read_unlock(&fs->lock); |
1da177e4 | 1160 | if (__emul_lookup_dentry(name,nd)) |
ea3834d9 | 1161 | goto out; /* found in altroot */ |
e518ddb7 | 1162 | read_lock(&fs->lock); |
1da177e4 | 1163 | } |
6ac08c39 JB |
1164 | nd->path = fs->root; |
1165 | path_get(&fs->root); | |
e518ddb7 | 1166 | read_unlock(&fs->lock); |
5590ff0d | 1167 | } else if (dfd == AT_FDCWD) { |
e518ddb7 | 1168 | read_lock(&fs->lock); |
6ac08c39 JB |
1169 | nd->path = fs->pwd; |
1170 | path_get(&fs->pwd); | |
e518ddb7 | 1171 | read_unlock(&fs->lock); |
5590ff0d | 1172 | } else { |
5590ff0d UD |
1173 | struct dentry *dentry; |
1174 | ||
1175 | file = fget_light(dfd, &fput_needed); | |
170aa3d0 UD |
1176 | retval = -EBADF; |
1177 | if (!file) | |
6d09bb62 | 1178 | goto out_fail; |
5590ff0d | 1179 | |
0f7fc9e4 | 1180 | dentry = file->f_path.dentry; |
5590ff0d | 1181 | |
170aa3d0 UD |
1182 | retval = -ENOTDIR; |
1183 | if (!S_ISDIR(dentry->d_inode->i_mode)) | |
6d09bb62 | 1184 | goto fput_fail; |
5590ff0d UD |
1185 | |
1186 | retval = file_permission(file, MAY_EXEC); | |
170aa3d0 | 1187 | if (retval) |
6d09bb62 | 1188 | goto fput_fail; |
5590ff0d | 1189 | |
5dd784d0 JB |
1190 | nd->path = file->f_path; |
1191 | path_get(&file->f_path); | |
5590ff0d UD |
1192 | |
1193 | fput_light(file, fput_needed); | |
1da177e4 | 1194 | } |
2dfdd266 JJS |
1195 | |
1196 | retval = path_walk(name, nd); | |
ea3834d9 | 1197 | out: |
4ac91378 JB |
1198 | if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && |
1199 | nd->path.dentry->d_inode)) | |
1200 | audit_inode(name, nd->path.dentry); | |
6d09bb62 | 1201 | out_fail: |
170aa3d0 UD |
1202 | return retval; |
1203 | ||
6d09bb62 | 1204 | fput_fail: |
170aa3d0 | 1205 | fput_light(file, fput_needed); |
6d09bb62 | 1206 | goto out_fail; |
1da177e4 LT |
1207 | } |
1208 | ||
fc9b52cd | 1209 | int path_lookup(const char *name, unsigned int flags, |
5590ff0d UD |
1210 | struct nameidata *nd) |
1211 | { | |
1212 | return do_path_lookup(AT_FDCWD, name, flags, nd); | |
1213 | } | |
1214 | ||
16f18200 JJS |
1215 | /** |
1216 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair | |
1217 | * @dentry: pointer to dentry of the base directory | |
1218 | * @mnt: pointer to vfs mount of the base directory | |
1219 | * @name: pointer to file name | |
1220 | * @flags: lookup flags | |
1221 | * @nd: pointer to nameidata | |
1222 | */ | |
1223 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, | |
1224 | const char *name, unsigned int flags, | |
1225 | struct nameidata *nd) | |
1226 | { | |
1227 | int retval; | |
1228 | ||
1229 | /* same as do_path_lookup */ | |
1230 | nd->last_type = LAST_ROOT; | |
1231 | nd->flags = flags; | |
1232 | nd->depth = 0; | |
1233 | ||
c8e7f449 JB |
1234 | nd->path.dentry = dentry; |
1235 | nd->path.mnt = mnt; | |
1236 | path_get(&nd->path); | |
16f18200 JJS |
1237 | |
1238 | retval = path_walk(name, nd); | |
4ac91378 JB |
1239 | if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && |
1240 | nd->path.dentry->d_inode)) | |
1241 | audit_inode(name, nd->path.dentry); | |
16f18200 JJS |
1242 | |
1243 | return retval; | |
1244 | ||
1245 | } | |
1246 | ||
5590ff0d UD |
1247 | static int __path_lookup_intent_open(int dfd, const char *name, |
1248 | unsigned int lookup_flags, struct nameidata *nd, | |
1249 | int open_flags, int create_mode) | |
834f2a4a TM |
1250 | { |
1251 | struct file *filp = get_empty_filp(); | |
1252 | int err; | |
1253 | ||
1254 | if (filp == NULL) | |
1255 | return -ENFILE; | |
1256 | nd->intent.open.file = filp; | |
1257 | nd->intent.open.flags = open_flags; | |
1258 | nd->intent.open.create_mode = create_mode; | |
5590ff0d | 1259 | err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd); |
834f2a4a TM |
1260 | if (IS_ERR(nd->intent.open.file)) { |
1261 | if (err == 0) { | |
1262 | err = PTR_ERR(nd->intent.open.file); | |
1d957f9b | 1263 | path_put(&nd->path); |
834f2a4a TM |
1264 | } |
1265 | } else if (err != 0) | |
1266 | release_open_intent(nd); | |
1267 | return err; | |
1268 | } | |
1269 | ||
1270 | /** | |
1271 | * path_lookup_open - lookup a file path with open intent | |
7045f37b | 1272 | * @dfd: the directory to use as base, or AT_FDCWD |
834f2a4a TM |
1273 | * @name: pointer to file name |
1274 | * @lookup_flags: lookup intent flags | |
1275 | * @nd: pointer to nameidata | |
1276 | * @open_flags: open intent flags | |
1277 | */ | |
5590ff0d | 1278 | int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags, |
834f2a4a TM |
1279 | struct nameidata *nd, int open_flags) |
1280 | { | |
5590ff0d | 1281 | return __path_lookup_intent_open(dfd, name, lookup_flags, nd, |
834f2a4a TM |
1282 | open_flags, 0); |
1283 | } | |
1284 | ||
1285 | /** | |
1286 | * path_lookup_create - lookup a file path with open + create intent | |
7045f37b | 1287 | * @dfd: the directory to use as base, or AT_FDCWD |
834f2a4a TM |
1288 | * @name: pointer to file name |
1289 | * @lookup_flags: lookup intent flags | |
1290 | * @nd: pointer to nameidata | |
1291 | * @open_flags: open intent flags | |
1292 | * @create_mode: create intent flags | |
1293 | */ | |
5590ff0d UD |
1294 | static int path_lookup_create(int dfd, const char *name, |
1295 | unsigned int lookup_flags, struct nameidata *nd, | |
1296 | int open_flags, int create_mode) | |
834f2a4a | 1297 | { |
5590ff0d UD |
1298 | return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE, |
1299 | nd, open_flags, create_mode); | |
834f2a4a TM |
1300 | } |
1301 | ||
1302 | int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags, | |
1303 | struct nameidata *nd, int open_flags) | |
1304 | { | |
1305 | char *tmp = getname(name); | |
1306 | int err = PTR_ERR(tmp); | |
1307 | ||
1308 | if (!IS_ERR(tmp)) { | |
5590ff0d | 1309 | err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0); |
834f2a4a TM |
1310 | putname(tmp); |
1311 | } | |
1312 | return err; | |
1313 | } | |
1314 | ||
eead1911 CH |
1315 | static struct dentry *__lookup_hash(struct qstr *name, |
1316 | struct dentry *base, struct nameidata *nd) | |
1da177e4 | 1317 | { |
057f6c01 | 1318 | struct dentry *dentry; |
1da177e4 LT |
1319 | struct inode *inode; |
1320 | int err; | |
1321 | ||
1322 | inode = base->d_inode; | |
1da177e4 LT |
1323 | |
1324 | /* | |
1325 | * See if the low-level filesystem might want | |
1326 | * to use its own hash.. | |
1327 | */ | |
1328 | if (base->d_op && base->d_op->d_hash) { | |
1329 | err = base->d_op->d_hash(base, name); | |
1330 | dentry = ERR_PTR(err); | |
1331 | if (err < 0) | |
1332 | goto out; | |
1333 | } | |
1334 | ||
1335 | dentry = cached_lookup(base, name, nd); | |
1336 | if (!dentry) { | |
d70b67c8 MS |
1337 | struct dentry *new; |
1338 | ||
1339 | /* Don't create child dentry for a dead directory. */ | |
1340 | dentry = ERR_PTR(-ENOENT); | |
1341 | if (IS_DEADDIR(inode)) | |
1342 | goto out; | |
1343 | ||
1344 | new = d_alloc(base, name); | |
1da177e4 LT |
1345 | dentry = ERR_PTR(-ENOMEM); |
1346 | if (!new) | |
1347 | goto out; | |
1348 | dentry = inode->i_op->lookup(inode, new, nd); | |
1349 | if (!dentry) | |
1350 | dentry = new; | |
1351 | else | |
1352 | dput(new); | |
1353 | } | |
1354 | out: | |
1355 | return dentry; | |
1356 | } | |
1357 | ||
057f6c01 JM |
1358 | /* |
1359 | * Restricted form of lookup. Doesn't follow links, single-component only, | |
1360 | * needs parent already locked. Doesn't follow mounts. | |
1361 | * SMP-safe. | |
1362 | */ | |
eead1911 | 1363 | static struct dentry *lookup_hash(struct nameidata *nd) |
057f6c01 | 1364 | { |
057f6c01 JM |
1365 | int err; |
1366 | ||
4ac91378 | 1367 | err = permission(nd->path.dentry->d_inode, MAY_EXEC, nd); |
057f6c01 | 1368 | if (err) |
eead1911 | 1369 | return ERR_PTR(err); |
4ac91378 | 1370 | return __lookup_hash(&nd->last, nd->path.dentry, nd); |
1da177e4 LT |
1371 | } |
1372 | ||
eead1911 CH |
1373 | static int __lookup_one_len(const char *name, struct qstr *this, |
1374 | struct dentry *base, int len) | |
1da177e4 LT |
1375 | { |
1376 | unsigned long hash; | |
1da177e4 LT |
1377 | unsigned int c; |
1378 | ||
057f6c01 JM |
1379 | this->name = name; |
1380 | this->len = len; | |
1da177e4 | 1381 | if (!len) |
057f6c01 | 1382 | return -EACCES; |
1da177e4 LT |
1383 | |
1384 | hash = init_name_hash(); | |
1385 | while (len--) { | |
1386 | c = *(const unsigned char *)name++; | |
1387 | if (c == '/' || c == '\0') | |
057f6c01 | 1388 | return -EACCES; |
1da177e4 LT |
1389 | hash = partial_name_hash(c, hash); |
1390 | } | |
057f6c01 JM |
1391 | this->hash = end_name_hash(hash); |
1392 | return 0; | |
1393 | } | |
1da177e4 | 1394 | |
eead1911 | 1395 | /** |
a6b91919 | 1396 | * lookup_one_len - filesystem helper to lookup single pathname component |
eead1911 CH |
1397 | * @name: pathname component to lookup |
1398 | * @base: base directory to lookup from | |
1399 | * @len: maximum length @len should be interpreted to | |
1400 | * | |
a6b91919 RD |
1401 | * Note that this routine is purely a helper for filesystem usage and should |
1402 | * not be called by generic code. Also note that by using this function the | |
eead1911 CH |
1403 | * nameidata argument is passed to the filesystem methods and a filesystem |
1404 | * using this helper needs to be prepared for that. | |
1405 | */ | |
057f6c01 JM |
1406 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
1407 | { | |
1408 | int err; | |
1409 | struct qstr this; | |
1410 | ||
1411 | err = __lookup_one_len(name, &this, base, len); | |
eead1911 CH |
1412 | if (err) |
1413 | return ERR_PTR(err); | |
1414 | ||
1415 | err = permission(base->d_inode, MAY_EXEC, NULL); | |
057f6c01 JM |
1416 | if (err) |
1417 | return ERR_PTR(err); | |
49705b77 | 1418 | return __lookup_hash(&this, base, NULL); |
057f6c01 JM |
1419 | } |
1420 | ||
eead1911 CH |
1421 | /** |
1422 | * lookup_one_noperm - bad hack for sysfs | |
1423 | * @name: pathname component to lookup | |
1424 | * @base: base directory to lookup from | |
1425 | * | |
1426 | * This is a variant of lookup_one_len that doesn't perform any permission | |
1427 | * checks. It's a horrible hack to work around the braindead sysfs | |
1428 | * architecture and should not be used anywhere else. | |
1429 | * | |
1430 | * DON'T USE THIS FUNCTION EVER, thanks. | |
1431 | */ | |
1432 | struct dentry *lookup_one_noperm(const char *name, struct dentry *base) | |
057f6c01 JM |
1433 | { |
1434 | int err; | |
1435 | struct qstr this; | |
1436 | ||
eead1911 | 1437 | err = __lookup_one_len(name, &this, base, strlen(name)); |
057f6c01 JM |
1438 | if (err) |
1439 | return ERR_PTR(err); | |
eead1911 | 1440 | return __lookup_hash(&this, base, NULL); |
1da177e4 LT |
1441 | } |
1442 | ||
fc9b52cd | 1443 | int __user_walk_fd(int dfd, const char __user *name, unsigned flags, |
5590ff0d | 1444 | struct nameidata *nd) |
1da177e4 LT |
1445 | { |
1446 | char *tmp = getname(name); | |
1447 | int err = PTR_ERR(tmp); | |
1448 | ||
1449 | if (!IS_ERR(tmp)) { | |
5590ff0d | 1450 | err = do_path_lookup(dfd, tmp, flags, nd); |
1da177e4 LT |
1451 | putname(tmp); |
1452 | } | |
1453 | return err; | |
1454 | } | |
1455 | ||
fc9b52cd | 1456 | int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd) |
5590ff0d UD |
1457 | { |
1458 | return __user_walk_fd(AT_FDCWD, name, flags, nd); | |
1459 | } | |
1460 | ||
1da177e4 LT |
1461 | /* |
1462 | * It's inline, so penalty for filesystems that don't use sticky bit is | |
1463 | * minimal. | |
1464 | */ | |
1465 | static inline int check_sticky(struct inode *dir, struct inode *inode) | |
1466 | { | |
1467 | if (!(dir->i_mode & S_ISVTX)) | |
1468 | return 0; | |
1469 | if (inode->i_uid == current->fsuid) | |
1470 | return 0; | |
1471 | if (dir->i_uid == current->fsuid) | |
1472 | return 0; | |
1473 | return !capable(CAP_FOWNER); | |
1474 | } | |
1475 | ||
1476 | /* | |
1477 | * Check whether we can remove a link victim from directory dir, check | |
1478 | * whether the type of victim is right. | |
1479 | * 1. We can't do it if dir is read-only (done in permission()) | |
1480 | * 2. We should have write and exec permissions on dir | |
1481 | * 3. We can't remove anything from append-only dir | |
1482 | * 4. We can't do anything with immutable dir (done in permission()) | |
1483 | * 5. If the sticky bit on dir is set we should either | |
1484 | * a. be owner of dir, or | |
1485 | * b. be owner of victim, or | |
1486 | * c. have CAP_FOWNER capability | |
1487 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
1488 | * links pointing to it. | |
1489 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
1490 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
1491 | * 9. We can't remove a root or mountpoint. | |
1492 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | |
1493 | * nfs_async_unlink(). | |
1494 | */ | |
858119e1 | 1495 | static int may_delete(struct inode *dir,struct dentry *victim,int isdir) |
1da177e4 LT |
1496 | { |
1497 | int error; | |
1498 | ||
1499 | if (!victim->d_inode) | |
1500 | return -ENOENT; | |
1501 | ||
1502 | BUG_ON(victim->d_parent->d_inode != dir); | |
5a190ae6 | 1503 | audit_inode_child(victim->d_name.name, victim, dir); |
1da177e4 LT |
1504 | |
1505 | error = permission(dir,MAY_WRITE | MAY_EXEC, NULL); | |
1506 | if (error) | |
1507 | return error; | |
1508 | if (IS_APPEND(dir)) | |
1509 | return -EPERM; | |
1510 | if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| | |
1511 | IS_IMMUTABLE(victim->d_inode)) | |
1512 | return -EPERM; | |
1513 | if (isdir) { | |
1514 | if (!S_ISDIR(victim->d_inode->i_mode)) | |
1515 | return -ENOTDIR; | |
1516 | if (IS_ROOT(victim)) | |
1517 | return -EBUSY; | |
1518 | } else if (S_ISDIR(victim->d_inode->i_mode)) | |
1519 | return -EISDIR; | |
1520 | if (IS_DEADDIR(dir)) | |
1521 | return -ENOENT; | |
1522 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
1523 | return -EBUSY; | |
1524 | return 0; | |
1525 | } | |
1526 | ||
1527 | /* Check whether we can create an object with dentry child in directory | |
1528 | * dir. | |
1529 | * 1. We can't do it if child already exists (open has special treatment for | |
1530 | * this case, but since we are inlined it's OK) | |
1531 | * 2. We can't do it if dir is read-only (done in permission()) | |
1532 | * 3. We should have write and exec permissions on dir | |
1533 | * 4. We can't do it if dir is immutable (done in permission()) | |
1534 | */ | |
1535 | static inline int may_create(struct inode *dir, struct dentry *child, | |
1536 | struct nameidata *nd) | |
1537 | { | |
1538 | if (child->d_inode) | |
1539 | return -EEXIST; | |
1540 | if (IS_DEADDIR(dir)) | |
1541 | return -ENOENT; | |
1542 | return permission(dir,MAY_WRITE | MAY_EXEC, nd); | |
1543 | } | |
1544 | ||
1545 | /* | |
1da177e4 LT |
1546 | * O_DIRECTORY translates into forcing a directory lookup. |
1547 | */ | |
1548 | static inline int lookup_flags(unsigned int f) | |
1549 | { | |
1550 | unsigned long retval = LOOKUP_FOLLOW; | |
1551 | ||
1552 | if (f & O_NOFOLLOW) | |
1553 | retval &= ~LOOKUP_FOLLOW; | |
1554 | ||
1da177e4 LT |
1555 | if (f & O_DIRECTORY) |
1556 | retval |= LOOKUP_DIRECTORY; | |
1557 | ||
1558 | return retval; | |
1559 | } | |
1560 | ||
1561 | /* | |
1562 | * p1 and p2 should be directories on the same fs. | |
1563 | */ | |
1564 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) | |
1565 | { | |
1566 | struct dentry *p; | |
1567 | ||
1568 | if (p1 == p2) { | |
f2eace23 | 1569 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
1da177e4 LT |
1570 | return NULL; |
1571 | } | |
1572 | ||
a11f3a05 | 1573 | mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
1da177e4 LT |
1574 | |
1575 | for (p = p1; p->d_parent != p; p = p->d_parent) { | |
1576 | if (p->d_parent == p2) { | |
f2eace23 IM |
1577 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); |
1578 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); | |
1da177e4 LT |
1579 | return p; |
1580 | } | |
1581 | } | |
1582 | ||
1583 | for (p = p2; p->d_parent != p; p = p->d_parent) { | |
1584 | if (p->d_parent == p1) { | |
f2eace23 IM |
1585 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
1586 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); | |
1da177e4 LT |
1587 | return p; |
1588 | } | |
1589 | } | |
1590 | ||
f2eace23 IM |
1591 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
1592 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); | |
1da177e4 LT |
1593 | return NULL; |
1594 | } | |
1595 | ||
1596 | void unlock_rename(struct dentry *p1, struct dentry *p2) | |
1597 | { | |
1b1dcc1b | 1598 | mutex_unlock(&p1->d_inode->i_mutex); |
1da177e4 | 1599 | if (p1 != p2) { |
1b1dcc1b | 1600 | mutex_unlock(&p2->d_inode->i_mutex); |
a11f3a05 | 1601 | mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
1da177e4 LT |
1602 | } |
1603 | } | |
1604 | ||
1605 | int vfs_create(struct inode *dir, struct dentry *dentry, int mode, | |
1606 | struct nameidata *nd) | |
1607 | { | |
1608 | int error = may_create(dir, dentry, nd); | |
1609 | ||
1610 | if (error) | |
1611 | return error; | |
1612 | ||
1613 | if (!dir->i_op || !dir->i_op->create) | |
1614 | return -EACCES; /* shouldn't it be ENOSYS? */ | |
1615 | mode &= S_IALLUGO; | |
1616 | mode |= S_IFREG; | |
1617 | error = security_inode_create(dir, dentry, mode); | |
1618 | if (error) | |
1619 | return error; | |
1620 | DQUOT_INIT(dir); | |
1621 | error = dir->i_op->create(dir, dentry, mode, nd); | |
a74574aa | 1622 | if (!error) |
f38aa942 | 1623 | fsnotify_create(dir, dentry); |
1da177e4 LT |
1624 | return error; |
1625 | } | |
1626 | ||
1627 | int may_open(struct nameidata *nd, int acc_mode, int flag) | |
1628 | { | |
4ac91378 | 1629 | struct dentry *dentry = nd->path.dentry; |
1da177e4 LT |
1630 | struct inode *inode = dentry->d_inode; |
1631 | int error; | |
1632 | ||
1633 | if (!inode) | |
1634 | return -ENOENT; | |
1635 | ||
1636 | if (S_ISLNK(inode->i_mode)) | |
1637 | return -ELOOP; | |
1638 | ||
974a9f0b | 1639 | if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE)) |
1da177e4 LT |
1640 | return -EISDIR; |
1641 | ||
1da177e4 LT |
1642 | /* |
1643 | * FIFO's, sockets and device files are special: they don't | |
1644 | * actually live on the filesystem itself, and as such you | |
1645 | * can write to them even if the filesystem is read-only. | |
1646 | */ | |
1647 | if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | |
1648 | flag &= ~O_TRUNC; | |
1649 | } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) { | |
4ac91378 | 1650 | if (nd->path.mnt->mnt_flags & MNT_NODEV) |
1da177e4 LT |
1651 | return -EACCES; |
1652 | ||
1653 | flag &= ~O_TRUNC; | |
4a3fd211 | 1654 | } |
b41572e9 DH |
1655 | |
1656 | error = vfs_permission(nd, acc_mode); | |
1657 | if (error) | |
1658 | return error; | |
1da177e4 LT |
1659 | /* |
1660 | * An append-only file must be opened in append mode for writing. | |
1661 | */ | |
1662 | if (IS_APPEND(inode)) { | |
1663 | if ((flag & FMODE_WRITE) && !(flag & O_APPEND)) | |
1664 | return -EPERM; | |
1665 | if (flag & O_TRUNC) | |
1666 | return -EPERM; | |
1667 | } | |
1668 | ||
1669 | /* O_NOATIME can only be set by the owner or superuser */ | |
1670 | if (flag & O_NOATIME) | |
3bd858ab | 1671 | if (!is_owner_or_cap(inode)) |
1da177e4 LT |
1672 | return -EPERM; |
1673 | ||
1674 | /* | |
1675 | * Ensure there are no outstanding leases on the file. | |
1676 | */ | |
1677 | error = break_lease(inode, flag); | |
1678 | if (error) | |
1679 | return error; | |
1680 | ||
1681 | if (flag & O_TRUNC) { | |
1682 | error = get_write_access(inode); | |
1683 | if (error) | |
1684 | return error; | |
1685 | ||
1686 | /* | |
1687 | * Refuse to truncate files with mandatory locks held on them. | |
1688 | */ | |
1689 | error = locks_verify_locked(inode); | |
1690 | if (!error) { | |
1691 | DQUOT_INIT(inode); | |
d139d7ff MS |
1692 | |
1693 | error = do_truncate(dentry, 0, | |
1694 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, | |
1695 | NULL); | |
1da177e4 LT |
1696 | } |
1697 | put_write_access(inode); | |
1698 | if (error) | |
1699 | return error; | |
1700 | } else | |
1701 | if (flag & FMODE_WRITE) | |
1702 | DQUOT_INIT(inode); | |
1703 | ||
1704 | return 0; | |
1705 | } | |
1706 | ||
d57999e1 DH |
1707 | /* |
1708 | * Be careful about ever adding any more callers of this | |
1709 | * function. Its flags must be in the namei format, not | |
1710 | * what get passed to sys_open(). | |
1711 | */ | |
1712 | static int __open_namei_create(struct nameidata *nd, struct path *path, | |
aab520e2 DH |
1713 | int flag, int mode) |
1714 | { | |
1715 | int error; | |
4ac91378 | 1716 | struct dentry *dir = nd->path.dentry; |
aab520e2 DH |
1717 | |
1718 | if (!IS_POSIXACL(dir->d_inode)) | |
1719 | mode &= ~current->fs->umask; | |
1720 | error = vfs_create(dir->d_inode, path->dentry, mode, nd); | |
1721 | mutex_unlock(&dir->d_inode->i_mutex); | |
4ac91378 JB |
1722 | dput(nd->path.dentry); |
1723 | nd->path.dentry = path->dentry; | |
aab520e2 DH |
1724 | if (error) |
1725 | return error; | |
1726 | /* Don't check for write permission, don't truncate */ | |
1727 | return may_open(nd, 0, flag & ~O_TRUNC); | |
1728 | } | |
1729 | ||
d57999e1 DH |
1730 | /* |
1731 | * Note that while the flag value (low two bits) for sys_open means: | |
1732 | * 00 - read-only | |
1733 | * 01 - write-only | |
1734 | * 10 - read-write | |
1735 | * 11 - special | |
1736 | * it is changed into | |
1737 | * 00 - no permissions needed | |
1738 | * 01 - read-permission | |
1739 | * 10 - write-permission | |
1740 | * 11 - read-write | |
1741 | * for the internal routines (ie open_namei()/follow_link() etc) | |
1742 | * This is more logical, and also allows the 00 "no perm needed" | |
1743 | * to be used for symlinks (where the permissions are checked | |
1744 | * later). | |
1745 | * | |
1746 | */ | |
1747 | static inline int open_to_namei_flags(int flag) | |
1748 | { | |
1749 | if ((flag+1) & O_ACCMODE) | |
1750 | flag++; | |
1751 | return flag; | |
1752 | } | |
1753 | ||
4a3fd211 DH |
1754 | static int open_will_write_to_fs(int flag, struct inode *inode) |
1755 | { | |
1756 | /* | |
1757 | * We'll never write to the fs underlying | |
1758 | * a device file. | |
1759 | */ | |
1760 | if (special_file(inode->i_mode)) | |
1761 | return 0; | |
1762 | return (flag & O_TRUNC); | |
1763 | } | |
1764 | ||
1da177e4 | 1765 | /* |
4a3fd211 DH |
1766 | * Note that the low bits of the passed in "open_flag" |
1767 | * are not the same as in the local variable "flag". See | |
1768 | * open_to_namei_flags() for more details. | |
1da177e4 | 1769 | */ |
a70e65df CH |
1770 | struct file *do_filp_open(int dfd, const char *pathname, |
1771 | int open_flag, int mode) | |
1da177e4 | 1772 | { |
4a3fd211 | 1773 | struct file *filp; |
a70e65df | 1774 | struct nameidata nd; |
834f2a4a | 1775 | int acc_mode, error; |
4e7506e4 | 1776 | struct path path; |
1da177e4 LT |
1777 | struct dentry *dir; |
1778 | int count = 0; | |
4a3fd211 | 1779 | int will_write; |
d57999e1 | 1780 | int flag = open_to_namei_flags(open_flag); |
1da177e4 LT |
1781 | |
1782 | acc_mode = ACC_MODE(flag); | |
1783 | ||
834f2a4a TM |
1784 | /* O_TRUNC implies we need access checks for write permissions */ |
1785 | if (flag & O_TRUNC) | |
1786 | acc_mode |= MAY_WRITE; | |
1787 | ||
1da177e4 LT |
1788 | /* Allow the LSM permission hook to distinguish append |
1789 | access from general write access. */ | |
1790 | if (flag & O_APPEND) | |
1791 | acc_mode |= MAY_APPEND; | |
1792 | ||
1da177e4 LT |
1793 | /* |
1794 | * The simplest case - just a plain lookup. | |
1795 | */ | |
1796 | if (!(flag & O_CREAT)) { | |
5590ff0d | 1797 | error = path_lookup_open(dfd, pathname, lookup_flags(flag), |
a70e65df | 1798 | &nd, flag); |
1da177e4 | 1799 | if (error) |
a70e65df | 1800 | return ERR_PTR(error); |
1da177e4 LT |
1801 | goto ok; |
1802 | } | |
1803 | ||
1804 | /* | |
1805 | * Create - we need to know the parent. | |
1806 | */ | |
a70e65df CH |
1807 | error = path_lookup_create(dfd, pathname, LOOKUP_PARENT, |
1808 | &nd, flag, mode); | |
1da177e4 | 1809 | if (error) |
a70e65df | 1810 | return ERR_PTR(error); |
1da177e4 LT |
1811 | |
1812 | /* | |
1813 | * We have the parent and last component. First of all, check | |
1814 | * that we are not asked to creat(2) an obvious directory - that | |
1815 | * will not do. | |
1816 | */ | |
1817 | error = -EISDIR; | |
a70e65df | 1818 | if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len]) |
1da177e4 LT |
1819 | goto exit; |
1820 | ||
a70e65df CH |
1821 | dir = nd.path.dentry; |
1822 | nd.flags &= ~LOOKUP_PARENT; | |
1b1dcc1b | 1823 | mutex_lock(&dir->d_inode->i_mutex); |
a70e65df CH |
1824 | path.dentry = lookup_hash(&nd); |
1825 | path.mnt = nd.path.mnt; | |
1da177e4 LT |
1826 | |
1827 | do_last: | |
4e7506e4 AV |
1828 | error = PTR_ERR(path.dentry); |
1829 | if (IS_ERR(path.dentry)) { | |
1b1dcc1b | 1830 | mutex_unlock(&dir->d_inode->i_mutex); |
1da177e4 LT |
1831 | goto exit; |
1832 | } | |
1833 | ||
a70e65df | 1834 | if (IS_ERR(nd.intent.open.file)) { |
a70e65df | 1835 | error = PTR_ERR(nd.intent.open.file); |
4a3fd211 | 1836 | goto exit_mutex_unlock; |
4af4c52f OD |
1837 | } |
1838 | ||
1da177e4 | 1839 | /* Negative dentry, just create the file */ |
4e7506e4 | 1840 | if (!path.dentry->d_inode) { |
4a3fd211 DH |
1841 | /* |
1842 | * This write is needed to ensure that a | |
1843 | * ro->rw transition does not occur between | |
1844 | * the time when the file is created and when | |
1845 | * a permanent write count is taken through | |
1846 | * the 'struct file' in nameidata_to_filp(). | |
1847 | */ | |
1848 | error = mnt_want_write(nd.path.mnt); | |
1da177e4 | 1849 | if (error) |
4a3fd211 DH |
1850 | goto exit_mutex_unlock; |
1851 | error = __open_namei_create(&nd, &path, flag, mode); | |
1852 | if (error) { | |
1853 | mnt_drop_write(nd.path.mnt); | |
1da177e4 | 1854 | goto exit; |
4a3fd211 DH |
1855 | } |
1856 | filp = nameidata_to_filp(&nd, open_flag); | |
1857 | mnt_drop_write(nd.path.mnt); | |
1858 | return filp; | |
1da177e4 LT |
1859 | } |
1860 | ||
1861 | /* | |
1862 | * It already exists. | |
1863 | */ | |
1b1dcc1b | 1864 | mutex_unlock(&dir->d_inode->i_mutex); |
5a190ae6 | 1865 | audit_inode(pathname, path.dentry); |
1da177e4 LT |
1866 | |
1867 | error = -EEXIST; | |
1868 | if (flag & O_EXCL) | |
1869 | goto exit_dput; | |
1870 | ||
e13b210f | 1871 | if (__follow_mount(&path)) { |
1da177e4 | 1872 | error = -ELOOP; |
ba7a4c1a AV |
1873 | if (flag & O_NOFOLLOW) |
1874 | goto exit_dput; | |
1da177e4 | 1875 | } |
3e2efce0 | 1876 | |
1da177e4 | 1877 | error = -ENOENT; |
4e7506e4 | 1878 | if (!path.dentry->d_inode) |
1da177e4 | 1879 | goto exit_dput; |
4e7506e4 | 1880 | if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link) |
1da177e4 LT |
1881 | goto do_link; |
1882 | ||
a70e65df | 1883 | path_to_nameidata(&path, &nd); |
1da177e4 | 1884 | error = -EISDIR; |
4e7506e4 | 1885 | if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode)) |
1da177e4 LT |
1886 | goto exit; |
1887 | ok: | |
4a3fd211 DH |
1888 | /* |
1889 | * Consider: | |
1890 | * 1. may_open() truncates a file | |
1891 | * 2. a rw->ro mount transition occurs | |
1892 | * 3. nameidata_to_filp() fails due to | |
1893 | * the ro mount. | |
1894 | * That would be inconsistent, and should | |
1895 | * be avoided. Taking this mnt write here | |
1896 | * ensures that (2) can not occur. | |
1897 | */ | |
1898 | will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode); | |
1899 | if (will_write) { | |
1900 | error = mnt_want_write(nd.path.mnt); | |
1901 | if (error) | |
1902 | goto exit; | |
1903 | } | |
a70e65df | 1904 | error = may_open(&nd, acc_mode, flag); |
4a3fd211 DH |
1905 | if (error) { |
1906 | if (will_write) | |
1907 | mnt_drop_write(nd.path.mnt); | |
1da177e4 | 1908 | goto exit; |
4a3fd211 DH |
1909 | } |
1910 | filp = nameidata_to_filp(&nd, open_flag); | |
1911 | /* | |
1912 | * It is now safe to drop the mnt write | |
1913 | * because the filp has had a write taken | |
1914 | * on its behalf. | |
1915 | */ | |
1916 | if (will_write) | |
1917 | mnt_drop_write(nd.path.mnt); | |
1918 | return filp; | |
1da177e4 | 1919 | |
4a3fd211 DH |
1920 | exit_mutex_unlock: |
1921 | mutex_unlock(&dir->d_inode->i_mutex); | |
1da177e4 | 1922 | exit_dput: |
a70e65df | 1923 | path_put_conditional(&path, &nd); |
1da177e4 | 1924 | exit: |
a70e65df CH |
1925 | if (!IS_ERR(nd.intent.open.file)) |
1926 | release_open_intent(&nd); | |
1927 | path_put(&nd.path); | |
1928 | return ERR_PTR(error); | |
1da177e4 LT |
1929 | |
1930 | do_link: | |
1931 | error = -ELOOP; | |
1932 | if (flag & O_NOFOLLOW) | |
1933 | goto exit_dput; | |
1934 | /* | |
1935 | * This is subtle. Instead of calling do_follow_link() we do the | |
1936 | * thing by hands. The reason is that this way we have zero link_count | |
1937 | * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT. | |
1938 | * After that we have the parent and last component, i.e. | |
1939 | * we are in the same situation as after the first path_walk(). | |
1940 | * Well, almost - if the last component is normal we get its copy | |
1941 | * stored in nd->last.name and we will have to putname() it when we | |
1942 | * are done. Procfs-like symlinks just set LAST_BIND. | |
1943 | */ | |
a70e65df CH |
1944 | nd.flags |= LOOKUP_PARENT; |
1945 | error = security_inode_follow_link(path.dentry, &nd); | |
1da177e4 LT |
1946 | if (error) |
1947 | goto exit_dput; | |
a70e65df | 1948 | error = __do_follow_link(&path, &nd); |
de459215 KK |
1949 | if (error) { |
1950 | /* Does someone understand code flow here? Or it is only | |
1951 | * me so stupid? Anathema to whoever designed this non-sense | |
1952 | * with "intent.open". | |
1953 | */ | |
a70e65df CH |
1954 | release_open_intent(&nd); |
1955 | return ERR_PTR(error); | |
de459215 | 1956 | } |
a70e65df CH |
1957 | nd.flags &= ~LOOKUP_PARENT; |
1958 | if (nd.last_type == LAST_BIND) | |
1da177e4 | 1959 | goto ok; |
1da177e4 | 1960 | error = -EISDIR; |
a70e65df | 1961 | if (nd.last_type != LAST_NORM) |
1da177e4 | 1962 | goto exit; |
a70e65df CH |
1963 | if (nd.last.name[nd.last.len]) { |
1964 | __putname(nd.last.name); | |
1da177e4 LT |
1965 | goto exit; |
1966 | } | |
1967 | error = -ELOOP; | |
1968 | if (count++==32) { | |
a70e65df | 1969 | __putname(nd.last.name); |
1da177e4 LT |
1970 | goto exit; |
1971 | } | |
a70e65df | 1972 | dir = nd.path.dentry; |
1b1dcc1b | 1973 | mutex_lock(&dir->d_inode->i_mutex); |
a70e65df CH |
1974 | path.dentry = lookup_hash(&nd); |
1975 | path.mnt = nd.path.mnt; | |
1976 | __putname(nd.last.name); | |
1da177e4 LT |
1977 | goto do_last; |
1978 | } | |
1979 | ||
a70e65df CH |
1980 | /** |
1981 | * filp_open - open file and return file pointer | |
1982 | * | |
1983 | * @filename: path to open | |
1984 | * @flags: open flags as per the open(2) second argument | |
1985 | * @mode: mode for the new file if O_CREAT is set, else ignored | |
1986 | * | |
1987 | * This is the helper to open a file from kernelspace if you really | |
1988 | * have to. But in generally you should not do this, so please move | |
1989 | * along, nothing to see here.. | |
1990 | */ | |
1991 | struct file *filp_open(const char *filename, int flags, int mode) | |
1992 | { | |
1993 | return do_filp_open(AT_FDCWD, filename, flags, mode); | |
1994 | } | |
1995 | EXPORT_SYMBOL(filp_open); | |
1996 | ||
1da177e4 LT |
1997 | /** |
1998 | * lookup_create - lookup a dentry, creating it if it doesn't exist | |
1999 | * @nd: nameidata info | |
2000 | * @is_dir: directory flag | |
2001 | * | |
2002 | * Simple function to lookup and return a dentry and create it | |
2003 | * if it doesn't exist. Is SMP-safe. | |
c663e5d8 | 2004 | * |
4ac91378 | 2005 | * Returns with nd->path.dentry->d_inode->i_mutex locked. |
1da177e4 LT |
2006 | */ |
2007 | struct dentry *lookup_create(struct nameidata *nd, int is_dir) | |
2008 | { | |
c663e5d8 | 2009 | struct dentry *dentry = ERR_PTR(-EEXIST); |
1da177e4 | 2010 | |
4ac91378 | 2011 | mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
c663e5d8 CH |
2012 | /* |
2013 | * Yucky last component or no last component at all? | |
2014 | * (foo/., foo/.., /////) | |
2015 | */ | |
1da177e4 LT |
2016 | if (nd->last_type != LAST_NORM) |
2017 | goto fail; | |
2018 | nd->flags &= ~LOOKUP_PARENT; | |
a634904a AM |
2019 | nd->flags |= LOOKUP_CREATE; |
2020 | nd->intent.open.flags = O_EXCL; | |
c663e5d8 CH |
2021 | |
2022 | /* | |
2023 | * Do the final lookup. | |
2024 | */ | |
49705b77 | 2025 | dentry = lookup_hash(nd); |
1da177e4 LT |
2026 | if (IS_ERR(dentry)) |
2027 | goto fail; | |
c663e5d8 | 2028 | |
e9baf6e5 AV |
2029 | if (dentry->d_inode) |
2030 | goto eexist; | |
c663e5d8 CH |
2031 | /* |
2032 | * Special case - lookup gave negative, but... we had foo/bar/ | |
2033 | * From the vfs_mknod() POV we just have a negative dentry - | |
2034 | * all is fine. Let's be bastards - you had / on the end, you've | |
2035 | * been asking for (non-existent) directory. -ENOENT for you. | |
2036 | */ | |
e9baf6e5 AV |
2037 | if (unlikely(!is_dir && nd->last.name[nd->last.len])) { |
2038 | dput(dentry); | |
2039 | dentry = ERR_PTR(-ENOENT); | |
2040 | } | |
1da177e4 | 2041 | return dentry; |
e9baf6e5 | 2042 | eexist: |
1da177e4 | 2043 | dput(dentry); |
e9baf6e5 | 2044 | dentry = ERR_PTR(-EEXIST); |
1da177e4 LT |
2045 | fail: |
2046 | return dentry; | |
2047 | } | |
f81a0bff | 2048 | EXPORT_SYMBOL_GPL(lookup_create); |
1da177e4 LT |
2049 | |
2050 | int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) | |
2051 | { | |
2052 | int error = may_create(dir, dentry, NULL); | |
2053 | ||
2054 | if (error) | |
2055 | return error; | |
2056 | ||
2057 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) | |
2058 | return -EPERM; | |
2059 | ||
2060 | if (!dir->i_op || !dir->i_op->mknod) | |
2061 | return -EPERM; | |
2062 | ||
08ce5f16 SH |
2063 | error = devcgroup_inode_mknod(mode, dev); |
2064 | if (error) | |
2065 | return error; | |
2066 | ||
1da177e4 LT |
2067 | error = security_inode_mknod(dir, dentry, mode, dev); |
2068 | if (error) | |
2069 | return error; | |
2070 | ||
2071 | DQUOT_INIT(dir); | |
2072 | error = dir->i_op->mknod(dir, dentry, mode, dev); | |
a74574aa | 2073 | if (!error) |
f38aa942 | 2074 | fsnotify_create(dir, dentry); |
1da177e4 LT |
2075 | return error; |
2076 | } | |
2077 | ||
463c3197 DH |
2078 | static int may_mknod(mode_t mode) |
2079 | { | |
2080 | switch (mode & S_IFMT) { | |
2081 | case S_IFREG: | |
2082 | case S_IFCHR: | |
2083 | case S_IFBLK: | |
2084 | case S_IFIFO: | |
2085 | case S_IFSOCK: | |
2086 | case 0: /* zero mode translates to S_IFREG */ | |
2087 | return 0; | |
2088 | case S_IFDIR: | |
2089 | return -EPERM; | |
2090 | default: | |
2091 | return -EINVAL; | |
2092 | } | |
2093 | } | |
2094 | ||
5590ff0d UD |
2095 | asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode, |
2096 | unsigned dev) | |
1da177e4 LT |
2097 | { |
2098 | int error = 0; | |
2099 | char * tmp; | |
2100 | struct dentry * dentry; | |
2101 | struct nameidata nd; | |
2102 | ||
2103 | if (S_ISDIR(mode)) | |
2104 | return -EPERM; | |
2105 | tmp = getname(filename); | |
2106 | if (IS_ERR(tmp)) | |
2107 | return PTR_ERR(tmp); | |
2108 | ||
5590ff0d | 2109 | error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd); |
1da177e4 LT |
2110 | if (error) |
2111 | goto out; | |
2112 | dentry = lookup_create(&nd, 0); | |
463c3197 DH |
2113 | if (IS_ERR(dentry)) { |
2114 | error = PTR_ERR(dentry); | |
2115 | goto out_unlock; | |
2116 | } | |
4ac91378 | 2117 | if (!IS_POSIXACL(nd.path.dentry->d_inode)) |
1da177e4 | 2118 | mode &= ~current->fs->umask; |
463c3197 DH |
2119 | error = may_mknod(mode); |
2120 | if (error) | |
2121 | goto out_dput; | |
2122 | error = mnt_want_write(nd.path.mnt); | |
2123 | if (error) | |
2124 | goto out_dput; | |
2125 | switch (mode & S_IFMT) { | |
1da177e4 | 2126 | case 0: case S_IFREG: |
4ac91378 | 2127 | error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); |
1da177e4 LT |
2128 | break; |
2129 | case S_IFCHR: case S_IFBLK: | |
4ac91378 | 2130 | error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, |
1da177e4 LT |
2131 | new_decode_dev(dev)); |
2132 | break; | |
2133 | case S_IFIFO: case S_IFSOCK: | |
4ac91378 | 2134 | error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); |
1da177e4 | 2135 | break; |
1da177e4 | 2136 | } |
463c3197 DH |
2137 | mnt_drop_write(nd.path.mnt); |
2138 | out_dput: | |
2139 | dput(dentry); | |
2140 | out_unlock: | |
4ac91378 | 2141 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
1d957f9b | 2142 | path_put(&nd.path); |
1da177e4 LT |
2143 | out: |
2144 | putname(tmp); | |
2145 | ||
2146 | return error; | |
2147 | } | |
2148 | ||
5590ff0d UD |
2149 | asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev) |
2150 | { | |
2151 | return sys_mknodat(AT_FDCWD, filename, mode, dev); | |
2152 | } | |
2153 | ||
1da177e4 LT |
2154 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
2155 | { | |
2156 | int error = may_create(dir, dentry, NULL); | |
2157 | ||
2158 | if (error) | |
2159 | return error; | |
2160 | ||
2161 | if (!dir->i_op || !dir->i_op->mkdir) | |
2162 | return -EPERM; | |
2163 | ||
2164 | mode &= (S_IRWXUGO|S_ISVTX); | |
2165 | error = security_inode_mkdir(dir, dentry, mode); | |
2166 | if (error) | |
2167 | return error; | |
2168 | ||
2169 | DQUOT_INIT(dir); | |
2170 | error = dir->i_op->mkdir(dir, dentry, mode); | |
a74574aa | 2171 | if (!error) |
f38aa942 | 2172 | fsnotify_mkdir(dir, dentry); |
1da177e4 LT |
2173 | return error; |
2174 | } | |
2175 | ||
5590ff0d | 2176 | asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode) |
1da177e4 LT |
2177 | { |
2178 | int error = 0; | |
2179 | char * tmp; | |
6902d925 DH |
2180 | struct dentry *dentry; |
2181 | struct nameidata nd; | |
1da177e4 LT |
2182 | |
2183 | tmp = getname(pathname); | |
2184 | error = PTR_ERR(tmp); | |
6902d925 DH |
2185 | if (IS_ERR(tmp)) |
2186 | goto out_err; | |
1da177e4 | 2187 | |
6902d925 DH |
2188 | error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd); |
2189 | if (error) | |
2190 | goto out; | |
2191 | dentry = lookup_create(&nd, 1); | |
2192 | error = PTR_ERR(dentry); | |
2193 | if (IS_ERR(dentry)) | |
2194 | goto out_unlock; | |
1da177e4 | 2195 | |
4ac91378 | 2196 | if (!IS_POSIXACL(nd.path.dentry->d_inode)) |
6902d925 | 2197 | mode &= ~current->fs->umask; |
463c3197 DH |
2198 | error = mnt_want_write(nd.path.mnt); |
2199 | if (error) | |
2200 | goto out_dput; | |
4ac91378 | 2201 | error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); |
463c3197 DH |
2202 | mnt_drop_write(nd.path.mnt); |
2203 | out_dput: | |
6902d925 DH |
2204 | dput(dentry); |
2205 | out_unlock: | |
4ac91378 | 2206 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
1d957f9b | 2207 | path_put(&nd.path); |
6902d925 DH |
2208 | out: |
2209 | putname(tmp); | |
2210 | out_err: | |
1da177e4 LT |
2211 | return error; |
2212 | } | |
2213 | ||
5590ff0d UD |
2214 | asmlinkage long sys_mkdir(const char __user *pathname, int mode) |
2215 | { | |
2216 | return sys_mkdirat(AT_FDCWD, pathname, mode); | |
2217 | } | |
2218 | ||
1da177e4 LT |
2219 | /* |
2220 | * We try to drop the dentry early: we should have | |
2221 | * a usage count of 2 if we're the only user of this | |
2222 | * dentry, and if that is true (possibly after pruning | |
2223 | * the dcache), then we drop the dentry now. | |
2224 | * | |
2225 | * A low-level filesystem can, if it choses, legally | |
2226 | * do a | |
2227 | * | |
2228 | * if (!d_unhashed(dentry)) | |
2229 | * return -EBUSY; | |
2230 | * | |
2231 | * if it cannot handle the case of removing a directory | |
2232 | * that is still in use by something else.. | |
2233 | */ | |
2234 | void dentry_unhash(struct dentry *dentry) | |
2235 | { | |
2236 | dget(dentry); | |
dc168427 | 2237 | shrink_dcache_parent(dentry); |
1da177e4 LT |
2238 | spin_lock(&dcache_lock); |
2239 | spin_lock(&dentry->d_lock); | |
2240 | if (atomic_read(&dentry->d_count) == 2) | |
2241 | __d_drop(dentry); | |
2242 | spin_unlock(&dentry->d_lock); | |
2243 | spin_unlock(&dcache_lock); | |
2244 | } | |
2245 | ||
2246 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) | |
2247 | { | |
2248 | int error = may_delete(dir, dentry, 1); | |
2249 | ||
2250 | if (error) | |
2251 | return error; | |
2252 | ||
2253 | if (!dir->i_op || !dir->i_op->rmdir) | |
2254 | return -EPERM; | |
2255 | ||
2256 | DQUOT_INIT(dir); | |
2257 | ||
1b1dcc1b | 2258 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
2259 | dentry_unhash(dentry); |
2260 | if (d_mountpoint(dentry)) | |
2261 | error = -EBUSY; | |
2262 | else { | |
2263 | error = security_inode_rmdir(dir, dentry); | |
2264 | if (!error) { | |
2265 | error = dir->i_op->rmdir(dir, dentry); | |
2266 | if (!error) | |
2267 | dentry->d_inode->i_flags |= S_DEAD; | |
2268 | } | |
2269 | } | |
1b1dcc1b | 2270 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 | 2271 | if (!error) { |
1da177e4 LT |
2272 | d_delete(dentry); |
2273 | } | |
2274 | dput(dentry); | |
2275 | ||
2276 | return error; | |
2277 | } | |
2278 | ||
5590ff0d | 2279 | static long do_rmdir(int dfd, const char __user *pathname) |
1da177e4 LT |
2280 | { |
2281 | int error = 0; | |
2282 | char * name; | |
2283 | struct dentry *dentry; | |
2284 | struct nameidata nd; | |
2285 | ||
2286 | name = getname(pathname); | |
2287 | if(IS_ERR(name)) | |
2288 | return PTR_ERR(name); | |
2289 | ||
5590ff0d | 2290 | error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd); |
1da177e4 LT |
2291 | if (error) |
2292 | goto exit; | |
2293 | ||
2294 | switch(nd.last_type) { | |
2295 | case LAST_DOTDOT: | |
2296 | error = -ENOTEMPTY; | |
2297 | goto exit1; | |
2298 | case LAST_DOT: | |
2299 | error = -EINVAL; | |
2300 | goto exit1; | |
2301 | case LAST_ROOT: | |
2302 | error = -EBUSY; | |
2303 | goto exit1; | |
2304 | } | |
4ac91378 | 2305 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
49705b77 | 2306 | dentry = lookup_hash(&nd); |
1da177e4 | 2307 | error = PTR_ERR(dentry); |
6902d925 DH |
2308 | if (IS_ERR(dentry)) |
2309 | goto exit2; | |
0622753b DH |
2310 | error = mnt_want_write(nd.path.mnt); |
2311 | if (error) | |
2312 | goto exit3; | |
4ac91378 | 2313 | error = vfs_rmdir(nd.path.dentry->d_inode, dentry); |
0622753b DH |
2314 | mnt_drop_write(nd.path.mnt); |
2315 | exit3: | |
6902d925 DH |
2316 | dput(dentry); |
2317 | exit2: | |
4ac91378 | 2318 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
1da177e4 | 2319 | exit1: |
1d957f9b | 2320 | path_put(&nd.path); |
1da177e4 LT |
2321 | exit: |
2322 | putname(name); | |
2323 | return error; | |
2324 | } | |
2325 | ||
5590ff0d UD |
2326 | asmlinkage long sys_rmdir(const char __user *pathname) |
2327 | { | |
2328 | return do_rmdir(AT_FDCWD, pathname); | |
2329 | } | |
2330 | ||
1da177e4 LT |
2331 | int vfs_unlink(struct inode *dir, struct dentry *dentry) |
2332 | { | |
2333 | int error = may_delete(dir, dentry, 0); | |
2334 | ||
2335 | if (error) | |
2336 | return error; | |
2337 | ||
2338 | if (!dir->i_op || !dir->i_op->unlink) | |
2339 | return -EPERM; | |
2340 | ||
2341 | DQUOT_INIT(dir); | |
2342 | ||
1b1dcc1b | 2343 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
2344 | if (d_mountpoint(dentry)) |
2345 | error = -EBUSY; | |
2346 | else { | |
2347 | error = security_inode_unlink(dir, dentry); | |
2348 | if (!error) | |
2349 | error = dir->i_op->unlink(dir, dentry); | |
2350 | } | |
1b1dcc1b | 2351 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
2352 | |
2353 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ | |
2354 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { | |
ece95912 | 2355 | fsnotify_link_count(dentry->d_inode); |
e234f35c | 2356 | d_delete(dentry); |
1da177e4 | 2357 | } |
0eeca283 | 2358 | |
1da177e4 LT |
2359 | return error; |
2360 | } | |
2361 | ||
2362 | /* | |
2363 | * Make sure that the actual truncation of the file will occur outside its | |
1b1dcc1b | 2364 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
1da177e4 LT |
2365 | * writeout happening, and we don't want to prevent access to the directory |
2366 | * while waiting on the I/O. | |
2367 | */ | |
5590ff0d | 2368 | static long do_unlinkat(int dfd, const char __user *pathname) |
1da177e4 LT |
2369 | { |
2370 | int error = 0; | |
2371 | char * name; | |
2372 | struct dentry *dentry; | |
2373 | struct nameidata nd; | |
2374 | struct inode *inode = NULL; | |
2375 | ||
2376 | name = getname(pathname); | |
2377 | if(IS_ERR(name)) | |
2378 | return PTR_ERR(name); | |
2379 | ||
5590ff0d | 2380 | error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd); |
1da177e4 LT |
2381 | if (error) |
2382 | goto exit; | |
2383 | error = -EISDIR; | |
2384 | if (nd.last_type != LAST_NORM) | |
2385 | goto exit1; | |
4ac91378 | 2386 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
49705b77 | 2387 | dentry = lookup_hash(&nd); |
1da177e4 LT |
2388 | error = PTR_ERR(dentry); |
2389 | if (!IS_ERR(dentry)) { | |
2390 | /* Why not before? Because we want correct error value */ | |
2391 | if (nd.last.name[nd.last.len]) | |
2392 | goto slashes; | |
2393 | inode = dentry->d_inode; | |
2394 | if (inode) | |
2395 | atomic_inc(&inode->i_count); | |
0622753b DH |
2396 | error = mnt_want_write(nd.path.mnt); |
2397 | if (error) | |
2398 | goto exit2; | |
4ac91378 | 2399 | error = vfs_unlink(nd.path.dentry->d_inode, dentry); |
0622753b | 2400 | mnt_drop_write(nd.path.mnt); |
1da177e4 LT |
2401 | exit2: |
2402 | dput(dentry); | |
2403 | } | |
4ac91378 | 2404 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
1da177e4 LT |
2405 | if (inode) |
2406 | iput(inode); /* truncate the inode here */ | |
2407 | exit1: | |
1d957f9b | 2408 | path_put(&nd.path); |
1da177e4 LT |
2409 | exit: |
2410 | putname(name); | |
2411 | return error; | |
2412 | ||
2413 | slashes: | |
2414 | error = !dentry->d_inode ? -ENOENT : | |
2415 | S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; | |
2416 | goto exit2; | |
2417 | } | |
2418 | ||
5590ff0d UD |
2419 | asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag) |
2420 | { | |
2421 | if ((flag & ~AT_REMOVEDIR) != 0) | |
2422 | return -EINVAL; | |
2423 | ||
2424 | if (flag & AT_REMOVEDIR) | |
2425 | return do_rmdir(dfd, pathname); | |
2426 | ||
2427 | return do_unlinkat(dfd, pathname); | |
2428 | } | |
2429 | ||
2430 | asmlinkage long sys_unlink(const char __user *pathname) | |
2431 | { | |
2432 | return do_unlinkat(AT_FDCWD, pathname); | |
2433 | } | |
2434 | ||
db2e747b | 2435 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
1da177e4 LT |
2436 | { |
2437 | int error = may_create(dir, dentry, NULL); | |
2438 | ||
2439 | if (error) | |
2440 | return error; | |
2441 | ||
2442 | if (!dir->i_op || !dir->i_op->symlink) | |
2443 | return -EPERM; | |
2444 | ||
2445 | error = security_inode_symlink(dir, dentry, oldname); | |
2446 | if (error) | |
2447 | return error; | |
2448 | ||
2449 | DQUOT_INIT(dir); | |
2450 | error = dir->i_op->symlink(dir, dentry, oldname); | |
a74574aa | 2451 | if (!error) |
f38aa942 | 2452 | fsnotify_create(dir, dentry); |
1da177e4 LT |
2453 | return error; |
2454 | } | |
2455 | ||
5590ff0d UD |
2456 | asmlinkage long sys_symlinkat(const char __user *oldname, |
2457 | int newdfd, const char __user *newname) | |
1da177e4 LT |
2458 | { |
2459 | int error = 0; | |
2460 | char * from; | |
2461 | char * to; | |
6902d925 DH |
2462 | struct dentry *dentry; |
2463 | struct nameidata nd; | |
1da177e4 LT |
2464 | |
2465 | from = getname(oldname); | |
2466 | if(IS_ERR(from)) | |
2467 | return PTR_ERR(from); | |
2468 | to = getname(newname); | |
2469 | error = PTR_ERR(to); | |
6902d925 DH |
2470 | if (IS_ERR(to)) |
2471 | goto out_putname; | |
1da177e4 | 2472 | |
6902d925 DH |
2473 | error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd); |
2474 | if (error) | |
2475 | goto out; | |
2476 | dentry = lookup_create(&nd, 0); | |
2477 | error = PTR_ERR(dentry); | |
2478 | if (IS_ERR(dentry)) | |
2479 | goto out_unlock; | |
2480 | ||
75c3f29d DH |
2481 | error = mnt_want_write(nd.path.mnt); |
2482 | if (error) | |
2483 | goto out_dput; | |
db2e747b | 2484 | error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); |
75c3f29d DH |
2485 | mnt_drop_write(nd.path.mnt); |
2486 | out_dput: | |
6902d925 DH |
2487 | dput(dentry); |
2488 | out_unlock: | |
4ac91378 | 2489 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
1d957f9b | 2490 | path_put(&nd.path); |
1da177e4 | 2491 | out: |
6902d925 DH |
2492 | putname(to); |
2493 | out_putname: | |
1da177e4 LT |
2494 | putname(from); |
2495 | return error; | |
2496 | } | |
2497 | ||
5590ff0d UD |
2498 | asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname) |
2499 | { | |
2500 | return sys_symlinkat(oldname, AT_FDCWD, newname); | |
2501 | } | |
2502 | ||
1da177e4 LT |
2503 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
2504 | { | |
2505 | struct inode *inode = old_dentry->d_inode; | |
2506 | int error; | |
2507 | ||
2508 | if (!inode) | |
2509 | return -ENOENT; | |
2510 | ||
2511 | error = may_create(dir, new_dentry, NULL); | |
2512 | if (error) | |
2513 | return error; | |
2514 | ||
2515 | if (dir->i_sb != inode->i_sb) | |
2516 | return -EXDEV; | |
2517 | ||
2518 | /* | |
2519 | * A link to an append-only or immutable file cannot be created. | |
2520 | */ | |
2521 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
2522 | return -EPERM; | |
2523 | if (!dir->i_op || !dir->i_op->link) | |
2524 | return -EPERM; | |
7e79eedb | 2525 | if (S_ISDIR(inode->i_mode)) |
1da177e4 LT |
2526 | return -EPERM; |
2527 | ||
2528 | error = security_inode_link(old_dentry, dir, new_dentry); | |
2529 | if (error) | |
2530 | return error; | |
2531 | ||
7e79eedb | 2532 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
2533 | DQUOT_INIT(dir); |
2534 | error = dir->i_op->link(old_dentry, dir, new_dentry); | |
7e79eedb | 2535 | mutex_unlock(&inode->i_mutex); |
e31e14ec | 2536 | if (!error) |
7e79eedb | 2537 | fsnotify_link(dir, inode, new_dentry); |
1da177e4 LT |
2538 | return error; |
2539 | } | |
2540 | ||
2541 | /* | |
2542 | * Hardlinks are often used in delicate situations. We avoid | |
2543 | * security-related surprises by not following symlinks on the | |
2544 | * newname. --KAB | |
2545 | * | |
2546 | * We don't follow them on the oldname either to be compatible | |
2547 | * with linux 2.0, and to avoid hard-linking to directories | |
2548 | * and other special files. --ADM | |
2549 | */ | |
5590ff0d | 2550 | asmlinkage long sys_linkat(int olddfd, const char __user *oldname, |
c04030e1 UD |
2551 | int newdfd, const char __user *newname, |
2552 | int flags) | |
1da177e4 LT |
2553 | { |
2554 | struct dentry *new_dentry; | |
2555 | struct nameidata nd, old_nd; | |
2556 | int error; | |
2557 | char * to; | |
2558 | ||
45c9b11a | 2559 | if ((flags & ~AT_SYMLINK_FOLLOW) != 0) |
c04030e1 UD |
2560 | return -EINVAL; |
2561 | ||
1da177e4 LT |
2562 | to = getname(newname); |
2563 | if (IS_ERR(to)) | |
2564 | return PTR_ERR(to); | |
2565 | ||
45c9b11a UD |
2566 | error = __user_walk_fd(olddfd, oldname, |
2567 | flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, | |
2568 | &old_nd); | |
1da177e4 LT |
2569 | if (error) |
2570 | goto exit; | |
5590ff0d | 2571 | error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd); |
1da177e4 LT |
2572 | if (error) |
2573 | goto out; | |
2574 | error = -EXDEV; | |
4ac91378 | 2575 | if (old_nd.path.mnt != nd.path.mnt) |
1da177e4 LT |
2576 | goto out_release; |
2577 | new_dentry = lookup_create(&nd, 0); | |
2578 | error = PTR_ERR(new_dentry); | |
6902d925 DH |
2579 | if (IS_ERR(new_dentry)) |
2580 | goto out_unlock; | |
75c3f29d DH |
2581 | error = mnt_want_write(nd.path.mnt); |
2582 | if (error) | |
2583 | goto out_dput; | |
4ac91378 | 2584 | error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry); |
75c3f29d DH |
2585 | mnt_drop_write(nd.path.mnt); |
2586 | out_dput: | |
6902d925 DH |
2587 | dput(new_dentry); |
2588 | out_unlock: | |
4ac91378 | 2589 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
1da177e4 | 2590 | out_release: |
1d957f9b | 2591 | path_put(&nd.path); |
1da177e4 | 2592 | out: |
1d957f9b | 2593 | path_put(&old_nd.path); |
1da177e4 LT |
2594 | exit: |
2595 | putname(to); | |
2596 | ||
2597 | return error; | |
2598 | } | |
2599 | ||
5590ff0d UD |
2600 | asmlinkage long sys_link(const char __user *oldname, const char __user *newname) |
2601 | { | |
c04030e1 | 2602 | return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
5590ff0d UD |
2603 | } |
2604 | ||
1da177e4 LT |
2605 | /* |
2606 | * The worst of all namespace operations - renaming directory. "Perverted" | |
2607 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | |
2608 | * Problems: | |
2609 | * a) we can get into loop creation. Check is done in is_subdir(). | |
2610 | * b) race potential - two innocent renames can create a loop together. | |
2611 | * That's where 4.4 screws up. Current fix: serialization on | |
a11f3a05 | 2612 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
1da177e4 LT |
2613 | * story. |
2614 | * c) we have to lock _three_ objects - parents and victim (if it exists). | |
1b1dcc1b | 2615 | * And that - after we got ->i_mutex on parents (until then we don't know |
1da177e4 LT |
2616 | * whether the target exists). Solution: try to be smart with locking |
2617 | * order for inodes. We rely on the fact that tree topology may change | |
a11f3a05 | 2618 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
1da177e4 LT |
2619 | * move will be locked. Thus we can rank directories by the tree |
2620 | * (ancestors first) and rank all non-directories after them. | |
2621 | * That works since everybody except rename does "lock parent, lookup, | |
a11f3a05 | 2622 | * lock child" and rename is under ->s_vfs_rename_mutex. |
1da177e4 LT |
2623 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
2624 | * has no more than 1 dentry. If "hybrid" objects will ever appear, | |
2625 | * we'd better make sure that there's no link(2) for them. | |
2626 | * d) some filesystems don't support opened-but-unlinked directories, | |
2627 | * either because of layout or because they are not ready to deal with | |
2628 | * all cases correctly. The latter will be fixed (taking this sort of | |
2629 | * stuff into VFS), but the former is not going away. Solution: the same | |
2630 | * trick as in rmdir(). | |
2631 | * e) conversion from fhandle to dentry may come in the wrong moment - when | |
1b1dcc1b | 2632 | * we are removing the target. Solution: we will have to grab ->i_mutex |
1da177e4 | 2633 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
1b1dcc1b | 2634 | * ->i_mutex on parents, which works but leads to some truely excessive |
1da177e4 LT |
2635 | * locking]. |
2636 | */ | |
75c96f85 AB |
2637 | static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, |
2638 | struct inode *new_dir, struct dentry *new_dentry) | |
1da177e4 LT |
2639 | { |
2640 | int error = 0; | |
2641 | struct inode *target; | |
2642 | ||
2643 | /* | |
2644 | * If we are going to change the parent - check write permissions, | |
2645 | * we'll need to flip '..'. | |
2646 | */ | |
2647 | if (new_dir != old_dir) { | |
2648 | error = permission(old_dentry->d_inode, MAY_WRITE, NULL); | |
2649 | if (error) | |
2650 | return error; | |
2651 | } | |
2652 | ||
2653 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); | |
2654 | if (error) | |
2655 | return error; | |
2656 | ||
2657 | target = new_dentry->d_inode; | |
2658 | if (target) { | |
1b1dcc1b | 2659 | mutex_lock(&target->i_mutex); |
1da177e4 LT |
2660 | dentry_unhash(new_dentry); |
2661 | } | |
2662 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) | |
2663 | error = -EBUSY; | |
2664 | else | |
2665 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); | |
2666 | if (target) { | |
2667 | if (!error) | |
2668 | target->i_flags |= S_DEAD; | |
1b1dcc1b | 2669 | mutex_unlock(&target->i_mutex); |
1da177e4 LT |
2670 | if (d_unhashed(new_dentry)) |
2671 | d_rehash(new_dentry); | |
2672 | dput(new_dentry); | |
2673 | } | |
e31e14ec | 2674 | if (!error) |
349457cc MF |
2675 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) |
2676 | d_move(old_dentry,new_dentry); | |
1da177e4 LT |
2677 | return error; |
2678 | } | |
2679 | ||
75c96f85 AB |
2680 | static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, |
2681 | struct inode *new_dir, struct dentry *new_dentry) | |
1da177e4 LT |
2682 | { |
2683 | struct inode *target; | |
2684 | int error; | |
2685 | ||
2686 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); | |
2687 | if (error) | |
2688 | return error; | |
2689 | ||
2690 | dget(new_dentry); | |
2691 | target = new_dentry->d_inode; | |
2692 | if (target) | |
1b1dcc1b | 2693 | mutex_lock(&target->i_mutex); |
1da177e4 LT |
2694 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) |
2695 | error = -EBUSY; | |
2696 | else | |
2697 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); | |
2698 | if (!error) { | |
349457cc | 2699 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) |
1da177e4 | 2700 | d_move(old_dentry, new_dentry); |
1da177e4 LT |
2701 | } |
2702 | if (target) | |
1b1dcc1b | 2703 | mutex_unlock(&target->i_mutex); |
1da177e4 LT |
2704 | dput(new_dentry); |
2705 | return error; | |
2706 | } | |
2707 | ||
2708 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |
2709 | struct inode *new_dir, struct dentry *new_dentry) | |
2710 | { | |
2711 | int error; | |
2712 | int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); | |
0eeca283 | 2713 | const char *old_name; |
1da177e4 LT |
2714 | |
2715 | if (old_dentry->d_inode == new_dentry->d_inode) | |
2716 | return 0; | |
2717 | ||
2718 | error = may_delete(old_dir, old_dentry, is_dir); | |
2719 | if (error) | |
2720 | return error; | |
2721 | ||
2722 | if (!new_dentry->d_inode) | |
2723 | error = may_create(new_dir, new_dentry, NULL); | |
2724 | else | |
2725 | error = may_delete(new_dir, new_dentry, is_dir); | |
2726 | if (error) | |
2727 | return error; | |
2728 | ||
2729 | if (!old_dir->i_op || !old_dir->i_op->rename) | |
2730 | return -EPERM; | |
2731 | ||
2732 | DQUOT_INIT(old_dir); | |
2733 | DQUOT_INIT(new_dir); | |
2734 | ||
0eeca283 RL |
2735 | old_name = fsnotify_oldname_init(old_dentry->d_name.name); |
2736 | ||
1da177e4 LT |
2737 | if (is_dir) |
2738 | error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); | |
2739 | else | |
2740 | error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); | |
2741 | if (!error) { | |
0eeca283 | 2742 | const char *new_name = old_dentry->d_name.name; |
89204c40 | 2743 | fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir, |
5a190ae6 | 2744 | new_dentry->d_inode, old_dentry); |
1da177e4 | 2745 | } |
0eeca283 RL |
2746 | fsnotify_oldname_free(old_name); |
2747 | ||
1da177e4 LT |
2748 | return error; |
2749 | } | |
2750 | ||
5590ff0d UD |
2751 | static int do_rename(int olddfd, const char *oldname, |
2752 | int newdfd, const char *newname) | |
1da177e4 LT |
2753 | { |
2754 | int error = 0; | |
2755 | struct dentry * old_dir, * new_dir; | |
2756 | struct dentry * old_dentry, *new_dentry; | |
2757 | struct dentry * trap; | |
2758 | struct nameidata oldnd, newnd; | |
2759 | ||
5590ff0d | 2760 | error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd); |
1da177e4 LT |
2761 | if (error) |
2762 | goto exit; | |
2763 | ||
5590ff0d | 2764 | error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd); |
1da177e4 LT |
2765 | if (error) |
2766 | goto exit1; | |
2767 | ||
2768 | error = -EXDEV; | |
4ac91378 | 2769 | if (oldnd.path.mnt != newnd.path.mnt) |
1da177e4 LT |
2770 | goto exit2; |
2771 | ||
4ac91378 | 2772 | old_dir = oldnd.path.dentry; |
1da177e4 LT |
2773 | error = -EBUSY; |
2774 | if (oldnd.last_type != LAST_NORM) | |
2775 | goto exit2; | |
2776 | ||
4ac91378 | 2777 | new_dir = newnd.path.dentry; |
1da177e4 LT |
2778 | if (newnd.last_type != LAST_NORM) |
2779 | goto exit2; | |
2780 | ||
2781 | trap = lock_rename(new_dir, old_dir); | |
2782 | ||
49705b77 | 2783 | old_dentry = lookup_hash(&oldnd); |
1da177e4 LT |
2784 | error = PTR_ERR(old_dentry); |
2785 | if (IS_ERR(old_dentry)) | |
2786 | goto exit3; | |
2787 | /* source must exist */ | |
2788 | error = -ENOENT; | |
2789 | if (!old_dentry->d_inode) | |
2790 | goto exit4; | |
2791 | /* unless the source is a directory trailing slashes give -ENOTDIR */ | |
2792 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) { | |
2793 | error = -ENOTDIR; | |
2794 | if (oldnd.last.name[oldnd.last.len]) | |
2795 | goto exit4; | |
2796 | if (newnd.last.name[newnd.last.len]) | |
2797 | goto exit4; | |
2798 | } | |
2799 | /* source should not be ancestor of target */ | |
2800 | error = -EINVAL; | |
2801 | if (old_dentry == trap) | |
2802 | goto exit4; | |
49705b77 | 2803 | new_dentry = lookup_hash(&newnd); |
1da177e4 LT |
2804 | error = PTR_ERR(new_dentry); |
2805 | if (IS_ERR(new_dentry)) | |
2806 | goto exit4; | |
2807 | /* target should not be an ancestor of source */ | |
2808 | error = -ENOTEMPTY; | |
2809 | if (new_dentry == trap) | |
2810 | goto exit5; | |
2811 | ||
9079b1eb DH |
2812 | error = mnt_want_write(oldnd.path.mnt); |
2813 | if (error) | |
2814 | goto exit5; | |
1da177e4 LT |
2815 | error = vfs_rename(old_dir->d_inode, old_dentry, |
2816 | new_dir->d_inode, new_dentry); | |
9079b1eb | 2817 | mnt_drop_write(oldnd.path.mnt); |
1da177e4 LT |
2818 | exit5: |
2819 | dput(new_dentry); | |
2820 | exit4: | |
2821 | dput(old_dentry); | |
2822 | exit3: | |
2823 | unlock_rename(new_dir, old_dir); | |
2824 | exit2: | |
1d957f9b | 2825 | path_put(&newnd.path); |
1da177e4 | 2826 | exit1: |
1d957f9b | 2827 | path_put(&oldnd.path); |
1da177e4 LT |
2828 | exit: |
2829 | return error; | |
2830 | } | |
2831 | ||
5590ff0d UD |
2832 | asmlinkage long sys_renameat(int olddfd, const char __user *oldname, |
2833 | int newdfd, const char __user *newname) | |
1da177e4 LT |
2834 | { |
2835 | int error; | |
2836 | char * from; | |
2837 | char * to; | |
2838 | ||
2839 | from = getname(oldname); | |
2840 | if(IS_ERR(from)) | |
2841 | return PTR_ERR(from); | |
2842 | to = getname(newname); | |
2843 | error = PTR_ERR(to); | |
2844 | if (!IS_ERR(to)) { | |
5590ff0d | 2845 | error = do_rename(olddfd, from, newdfd, to); |
1da177e4 LT |
2846 | putname(to); |
2847 | } | |
2848 | putname(from); | |
2849 | return error; | |
2850 | } | |
2851 | ||
5590ff0d UD |
2852 | asmlinkage long sys_rename(const char __user *oldname, const char __user *newname) |
2853 | { | |
2854 | return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); | |
2855 | } | |
2856 | ||
1da177e4 LT |
2857 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) |
2858 | { | |
2859 | int len; | |
2860 | ||
2861 | len = PTR_ERR(link); | |
2862 | if (IS_ERR(link)) | |
2863 | goto out; | |
2864 | ||
2865 | len = strlen(link); | |
2866 | if (len > (unsigned) buflen) | |
2867 | len = buflen; | |
2868 | if (copy_to_user(buffer, link, len)) | |
2869 | len = -EFAULT; | |
2870 | out: | |
2871 | return len; | |
2872 | } | |
2873 | ||
2874 | /* | |
2875 | * A helper for ->readlink(). This should be used *ONLY* for symlinks that | |
2876 | * have ->follow_link() touching nd only in nd_set_link(). Using (or not | |
2877 | * using) it for any given inode is up to filesystem. | |
2878 | */ | |
2879 | int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
2880 | { | |
2881 | struct nameidata nd; | |
cc314eef | 2882 | void *cookie; |
694a1764 | 2883 | int res; |
cc314eef | 2884 | |
1da177e4 | 2885 | nd.depth = 0; |
cc314eef | 2886 | cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); |
694a1764 MS |
2887 | if (IS_ERR(cookie)) |
2888 | return PTR_ERR(cookie); | |
2889 | ||
2890 | res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); | |
2891 | if (dentry->d_inode->i_op->put_link) | |
2892 | dentry->d_inode->i_op->put_link(dentry, &nd, cookie); | |
2893 | return res; | |
1da177e4 LT |
2894 | } |
2895 | ||
2896 | int vfs_follow_link(struct nameidata *nd, const char *link) | |
2897 | { | |
2898 | return __vfs_follow_link(nd, link); | |
2899 | } | |
2900 | ||
2901 | /* get the link contents into pagecache */ | |
2902 | static char *page_getlink(struct dentry * dentry, struct page **ppage) | |
2903 | { | |
2904 | struct page * page; | |
2905 | struct address_space *mapping = dentry->d_inode->i_mapping; | |
090d2b18 | 2906 | page = read_mapping_page(mapping, 0, NULL); |
1da177e4 | 2907 | if (IS_ERR(page)) |
6fe6900e | 2908 | return (char*)page; |
1da177e4 LT |
2909 | *ppage = page; |
2910 | return kmap(page); | |
1da177e4 LT |
2911 | } |
2912 | ||
2913 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
2914 | { | |
2915 | struct page *page = NULL; | |
2916 | char *s = page_getlink(dentry, &page); | |
2917 | int res = vfs_readlink(dentry,buffer,buflen,s); | |
2918 | if (page) { | |
2919 | kunmap(page); | |
2920 | page_cache_release(page); | |
2921 | } | |
2922 | return res; | |
2923 | } | |
2924 | ||
cc314eef | 2925 | void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) |
1da177e4 | 2926 | { |
cc314eef | 2927 | struct page *page = NULL; |
1da177e4 | 2928 | nd_set_link(nd, page_getlink(dentry, &page)); |
cc314eef | 2929 | return page; |
1da177e4 LT |
2930 | } |
2931 | ||
cc314eef | 2932 | void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
1da177e4 | 2933 | { |
cc314eef LT |
2934 | struct page *page = cookie; |
2935 | ||
2936 | if (page) { | |
1da177e4 LT |
2937 | kunmap(page); |
2938 | page_cache_release(page); | |
1da177e4 LT |
2939 | } |
2940 | } | |
2941 | ||
0adb25d2 KK |
2942 | int __page_symlink(struct inode *inode, const char *symname, int len, |
2943 | gfp_t gfp_mask) | |
1da177e4 LT |
2944 | { |
2945 | struct address_space *mapping = inode->i_mapping; | |
0adb25d2 | 2946 | struct page *page; |
afddba49 | 2947 | void *fsdata; |
beb497ab | 2948 | int err; |
1da177e4 LT |
2949 | char *kaddr; |
2950 | ||
7e53cac4 | 2951 | retry: |
afddba49 NP |
2952 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
2953 | AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); | |
1da177e4 | 2954 | if (err) |
afddba49 NP |
2955 | goto fail; |
2956 | ||
1da177e4 LT |
2957 | kaddr = kmap_atomic(page, KM_USER0); |
2958 | memcpy(kaddr, symname, len-1); | |
2959 | kunmap_atomic(kaddr, KM_USER0); | |
afddba49 NP |
2960 | |
2961 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, | |
2962 | page, fsdata); | |
1da177e4 LT |
2963 | if (err < 0) |
2964 | goto fail; | |
afddba49 NP |
2965 | if (err < len-1) |
2966 | goto retry; | |
2967 | ||
1da177e4 LT |
2968 | mark_inode_dirty(inode); |
2969 | return 0; | |
1da177e4 LT |
2970 | fail: |
2971 | return err; | |
2972 | } | |
2973 | ||
0adb25d2 KK |
2974 | int page_symlink(struct inode *inode, const char *symname, int len) |
2975 | { | |
2976 | return __page_symlink(inode, symname, len, | |
2977 | mapping_gfp_mask(inode->i_mapping)); | |
2978 | } | |
2979 | ||
92e1d5be | 2980 | const struct inode_operations page_symlink_inode_operations = { |
1da177e4 LT |
2981 | .readlink = generic_readlink, |
2982 | .follow_link = page_follow_link_light, | |
2983 | .put_link = page_put_link, | |
2984 | }; | |
2985 | ||
2986 | EXPORT_SYMBOL(__user_walk); | |
5590ff0d | 2987 | EXPORT_SYMBOL(__user_walk_fd); |
1da177e4 LT |
2988 | EXPORT_SYMBOL(follow_down); |
2989 | EXPORT_SYMBOL(follow_up); | |
2990 | EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ | |
2991 | EXPORT_SYMBOL(getname); | |
2992 | EXPORT_SYMBOL(lock_rename); | |
1da177e4 LT |
2993 | EXPORT_SYMBOL(lookup_one_len); |
2994 | EXPORT_SYMBOL(page_follow_link_light); | |
2995 | EXPORT_SYMBOL(page_put_link); | |
2996 | EXPORT_SYMBOL(page_readlink); | |
0adb25d2 | 2997 | EXPORT_SYMBOL(__page_symlink); |
1da177e4 LT |
2998 | EXPORT_SYMBOL(page_symlink); |
2999 | EXPORT_SYMBOL(page_symlink_inode_operations); | |
3000 | EXPORT_SYMBOL(path_lookup); | |
16f18200 | 3001 | EXPORT_SYMBOL(vfs_path_lookup); |
1da177e4 | 3002 | EXPORT_SYMBOL(permission); |
e4543edd | 3003 | EXPORT_SYMBOL(vfs_permission); |
8c744fb8 | 3004 | EXPORT_SYMBOL(file_permission); |
1da177e4 LT |
3005 | EXPORT_SYMBOL(unlock_rename); |
3006 | EXPORT_SYMBOL(vfs_create); | |
3007 | EXPORT_SYMBOL(vfs_follow_link); | |
3008 | EXPORT_SYMBOL(vfs_link); | |
3009 | EXPORT_SYMBOL(vfs_mkdir); | |
3010 | EXPORT_SYMBOL(vfs_mknod); | |
3011 | EXPORT_SYMBOL(generic_permission); | |
3012 | EXPORT_SYMBOL(vfs_readlink); | |
3013 | EXPORT_SYMBOL(vfs_rename); | |
3014 | EXPORT_SYMBOL(vfs_rmdir); | |
3015 | EXPORT_SYMBOL(vfs_symlink); | |
3016 | EXPORT_SYMBOL(vfs_unlink); | |
3017 | EXPORT_SYMBOL(dentry_unhash); | |
3018 | EXPORT_SYMBOL(generic_readlink); |