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