xref: /linux/fs/open.c (revision 87c2ce3b9305b9b723faeedf6e32ef703ec9b33a)
1 /*
2  *  linux/fs/open.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6 
7 #include <linux/string.h>
8 #include <linux/mm.h>
9 #include <linux/utime.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/quotaops.h>
13 #include <linux/fsnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/namei.h>
18 #include <linux/backing-dev.h>
19 #include <linux/security.h>
20 #include <linux/mount.h>
21 #include <linux/vfs.h>
22 #include <asm/uaccess.h>
23 #include <linux/fs.h>
24 #include <linux/personality.h>
25 #include <linux/pagemap.h>
26 #include <linux/syscalls.h>
27 #include <linux/rcupdate.h>
28 
29 #include <asm/unistd.h>
30 
31 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
32 {
33 	int retval = -ENODEV;
34 
35 	if (sb) {
36 		retval = -ENOSYS;
37 		if (sb->s_op->statfs) {
38 			memset(buf, 0, sizeof(*buf));
39 			retval = security_sb_statfs(sb);
40 			if (retval)
41 				return retval;
42 			retval = sb->s_op->statfs(sb, buf);
43 			if (retval == 0 && buf->f_frsize == 0)
44 				buf->f_frsize = buf->f_bsize;
45 		}
46 	}
47 	return retval;
48 }
49 
50 EXPORT_SYMBOL(vfs_statfs);
51 
52 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
53 {
54 	struct kstatfs st;
55 	int retval;
56 
57 	retval = vfs_statfs(sb, &st);
58 	if (retval)
59 		return retval;
60 
61 	if (sizeof(*buf) == sizeof(st))
62 		memcpy(buf, &st, sizeof(st));
63 	else {
64 		if (sizeof buf->f_blocks == 4) {
65 			if ((st.f_blocks | st.f_bfree | st.f_bavail) &
66 			    0xffffffff00000000ULL)
67 				return -EOVERFLOW;
68 			/*
69 			 * f_files and f_ffree may be -1; it's okay to stuff
70 			 * that into 32 bits
71 			 */
72 			if (st.f_files != -1 &&
73 			    (st.f_files & 0xffffffff00000000ULL))
74 				return -EOVERFLOW;
75 			if (st.f_ffree != -1 &&
76 			    (st.f_ffree & 0xffffffff00000000ULL))
77 				return -EOVERFLOW;
78 		}
79 
80 		buf->f_type = st.f_type;
81 		buf->f_bsize = st.f_bsize;
82 		buf->f_blocks = st.f_blocks;
83 		buf->f_bfree = st.f_bfree;
84 		buf->f_bavail = st.f_bavail;
85 		buf->f_files = st.f_files;
86 		buf->f_ffree = st.f_ffree;
87 		buf->f_fsid = st.f_fsid;
88 		buf->f_namelen = st.f_namelen;
89 		buf->f_frsize = st.f_frsize;
90 		memset(buf->f_spare, 0, sizeof(buf->f_spare));
91 	}
92 	return 0;
93 }
94 
95 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
96 {
97 	struct kstatfs st;
98 	int retval;
99 
100 	retval = vfs_statfs(sb, &st);
101 	if (retval)
102 		return retval;
103 
104 	if (sizeof(*buf) == sizeof(st))
105 		memcpy(buf, &st, sizeof(st));
106 	else {
107 		buf->f_type = st.f_type;
108 		buf->f_bsize = st.f_bsize;
109 		buf->f_blocks = st.f_blocks;
110 		buf->f_bfree = st.f_bfree;
111 		buf->f_bavail = st.f_bavail;
112 		buf->f_files = st.f_files;
113 		buf->f_ffree = st.f_ffree;
114 		buf->f_fsid = st.f_fsid;
115 		buf->f_namelen = st.f_namelen;
116 		buf->f_frsize = st.f_frsize;
117 		memset(buf->f_spare, 0, sizeof(buf->f_spare));
118 	}
119 	return 0;
120 }
121 
122 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
123 {
124 	struct nameidata nd;
125 	int error;
126 
127 	error = user_path_walk(path, &nd);
128 	if (!error) {
129 		struct statfs tmp;
130 		error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
131 		if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
132 			error = -EFAULT;
133 		path_release(&nd);
134 	}
135 	return error;
136 }
137 
138 
139 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
140 {
141 	struct nameidata nd;
142 	long error;
143 
144 	if (sz != sizeof(*buf))
145 		return -EINVAL;
146 	error = user_path_walk(path, &nd);
147 	if (!error) {
148 		struct statfs64 tmp;
149 		error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
150 		if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
151 			error = -EFAULT;
152 		path_release(&nd);
153 	}
154 	return error;
155 }
156 
157 
158 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
159 {
160 	struct file * file;
161 	struct statfs tmp;
162 	int error;
163 
164 	error = -EBADF;
165 	file = fget(fd);
166 	if (!file)
167 		goto out;
168 	error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
169 	if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
170 		error = -EFAULT;
171 	fput(file);
172 out:
173 	return error;
174 }
175 
176 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
177 {
178 	struct file * file;
179 	struct statfs64 tmp;
180 	int error;
181 
182 	if (sz != sizeof(*buf))
183 		return -EINVAL;
184 
185 	error = -EBADF;
186 	file = fget(fd);
187 	if (!file)
188 		goto out;
189 	error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
190 	if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
191 		error = -EFAULT;
192 	fput(file);
193 out:
194 	return error;
195 }
196 
197 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
198 	struct file *filp)
199 {
200 	int err;
201 	struct iattr newattrs;
202 
203 	/* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
204 	if (length < 0)
205 		return -EINVAL;
206 
207 	newattrs.ia_size = length;
208 	newattrs.ia_valid = ATTR_SIZE | time_attrs;
209 	if (filp) {
210 		newattrs.ia_file = filp;
211 		newattrs.ia_valid |= ATTR_FILE;
212 	}
213 
214 	mutex_lock(&dentry->d_inode->i_mutex);
215 	err = notify_change(dentry, &newattrs);
216 	mutex_unlock(&dentry->d_inode->i_mutex);
217 	return err;
218 }
219 
220 static long do_sys_truncate(const char __user * path, loff_t length)
221 {
222 	struct nameidata nd;
223 	struct inode * inode;
224 	int error;
225 
226 	error = -EINVAL;
227 	if (length < 0)	/* sorry, but loff_t says... */
228 		goto out;
229 
230 	error = user_path_walk(path, &nd);
231 	if (error)
232 		goto out;
233 	inode = nd.dentry->d_inode;
234 
235 	/* For directories it's -EISDIR, for other non-regulars - -EINVAL */
236 	error = -EISDIR;
237 	if (S_ISDIR(inode->i_mode))
238 		goto dput_and_out;
239 
240 	error = -EINVAL;
241 	if (!S_ISREG(inode->i_mode))
242 		goto dput_and_out;
243 
244 	error = vfs_permission(&nd, MAY_WRITE);
245 	if (error)
246 		goto dput_and_out;
247 
248 	error = -EROFS;
249 	if (IS_RDONLY(inode))
250 		goto dput_and_out;
251 
252 	error = -EPERM;
253 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
254 		goto dput_and_out;
255 
256 	/*
257 	 * Make sure that there are no leases.
258 	 */
259 	error = break_lease(inode, FMODE_WRITE);
260 	if (error)
261 		goto dput_and_out;
262 
263 	error = get_write_access(inode);
264 	if (error)
265 		goto dput_and_out;
266 
267 	error = locks_verify_truncate(inode, NULL, length);
268 	if (!error) {
269 		DQUOT_INIT(inode);
270 		error = do_truncate(nd.dentry, length, 0, NULL);
271 	}
272 	put_write_access(inode);
273 
274 dput_and_out:
275 	path_release(&nd);
276 out:
277 	return error;
278 }
279 
280 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
281 {
282 	/* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
283 	return do_sys_truncate(path, (long)length);
284 }
285 
286 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
287 {
288 	struct inode * inode;
289 	struct dentry *dentry;
290 	struct file * file;
291 	int error;
292 
293 	error = -EINVAL;
294 	if (length < 0)
295 		goto out;
296 	error = -EBADF;
297 	file = fget(fd);
298 	if (!file)
299 		goto out;
300 
301 	/* explicitly opened as large or we are on 64-bit box */
302 	if (file->f_flags & O_LARGEFILE)
303 		small = 0;
304 
305 	dentry = file->f_dentry;
306 	inode = dentry->d_inode;
307 	error = -EINVAL;
308 	if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
309 		goto out_putf;
310 
311 	error = -EINVAL;
312 	/* Cannot ftruncate over 2^31 bytes without large file support */
313 	if (small && length > MAX_NON_LFS)
314 		goto out_putf;
315 
316 	error = -EPERM;
317 	if (IS_APPEND(inode))
318 		goto out_putf;
319 
320 	error = locks_verify_truncate(inode, file, length);
321 	if (!error)
322 		error = do_truncate(dentry, length, 0, file);
323 out_putf:
324 	fput(file);
325 out:
326 	return error;
327 }
328 
329 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
330 {
331 	return do_sys_ftruncate(fd, length, 1);
332 }
333 
334 /* LFS versions of truncate are only needed on 32 bit machines */
335 #if BITS_PER_LONG == 32
336 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
337 {
338 	return do_sys_truncate(path, length);
339 }
340 
341 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
342 {
343 	return do_sys_ftruncate(fd, length, 0);
344 }
345 #endif
346 
347 #ifdef __ARCH_WANT_SYS_UTIME
348 
349 /*
350  * sys_utime() can be implemented in user-level using sys_utimes().
351  * Is this for backwards compatibility?  If so, why not move it
352  * into the appropriate arch directory (for those architectures that
353  * need it).
354  */
355 
356 /* If times==NULL, set access and modification to current time,
357  * must be owner or have write permission.
358  * Else, update from *times, must be owner or super user.
359  */
360 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
361 {
362 	int error;
363 	struct nameidata nd;
364 	struct inode * inode;
365 	struct iattr newattrs;
366 
367 	error = user_path_walk(filename, &nd);
368 	if (error)
369 		goto out;
370 	inode = nd.dentry->d_inode;
371 
372 	error = -EROFS;
373 	if (IS_RDONLY(inode))
374 		goto dput_and_out;
375 
376 	/* Don't worry, the checks are done in inode_change_ok() */
377 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
378 	if (times) {
379 		error = -EPERM;
380 		if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
381 			goto dput_and_out;
382 
383 		error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
384 		newattrs.ia_atime.tv_nsec = 0;
385 		if (!error)
386 			error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
387 		newattrs.ia_mtime.tv_nsec = 0;
388 		if (error)
389 			goto dput_and_out;
390 
391 		newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
392 	} else {
393                 error = -EACCES;
394                 if (IS_IMMUTABLE(inode))
395                         goto dput_and_out;
396 
397 		if (current->fsuid != inode->i_uid &&
398 		    (error = vfs_permission(&nd, MAY_WRITE)) != 0)
399 			goto dput_and_out;
400 	}
401 	mutex_lock(&inode->i_mutex);
402 	error = notify_change(nd.dentry, &newattrs);
403 	mutex_unlock(&inode->i_mutex);
404 dput_and_out:
405 	path_release(&nd);
406 out:
407 	return error;
408 }
409 
410 #endif
411 
412 /* If times==NULL, set access and modification to current time,
413  * must be owner or have write permission.
414  * Else, update from *times, must be owner or super user.
415  */
416 long do_utimes(char __user * filename, struct timeval * times)
417 {
418 	int error;
419 	struct nameidata nd;
420 	struct inode * inode;
421 	struct iattr newattrs;
422 
423 	error = user_path_walk(filename, &nd);
424 
425 	if (error)
426 		goto out;
427 	inode = nd.dentry->d_inode;
428 
429 	error = -EROFS;
430 	if (IS_RDONLY(inode))
431 		goto dput_and_out;
432 
433 	/* Don't worry, the checks are done in inode_change_ok() */
434 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
435 	if (times) {
436 		error = -EPERM;
437                 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
438                         goto dput_and_out;
439 
440 		newattrs.ia_atime.tv_sec = times[0].tv_sec;
441 		newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
442 		newattrs.ia_mtime.tv_sec = times[1].tv_sec;
443 		newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
444 		newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
445 	} else {
446 		error = -EACCES;
447                 if (IS_IMMUTABLE(inode))
448                         goto dput_and_out;
449 
450 		if (current->fsuid != inode->i_uid &&
451 		    (error = vfs_permission(&nd, MAY_WRITE)) != 0)
452 			goto dput_and_out;
453 	}
454 	mutex_lock(&inode->i_mutex);
455 	error = notify_change(nd.dentry, &newattrs);
456 	mutex_unlock(&inode->i_mutex);
457 dput_and_out:
458 	path_release(&nd);
459 out:
460 	return error;
461 }
462 
463 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
464 {
465 	struct timeval times[2];
466 
467 	if (utimes && copy_from_user(&times, utimes, sizeof(times)))
468 		return -EFAULT;
469 	return do_utimes(filename, utimes ? times : NULL);
470 }
471 
472 
473 /*
474  * access() needs to use the real uid/gid, not the effective uid/gid.
475  * We do this by temporarily clearing all FS-related capabilities and
476  * switching the fsuid/fsgid around to the real ones.
477  */
478 asmlinkage long sys_access(const char __user * filename, int mode)
479 {
480 	struct nameidata nd;
481 	int old_fsuid, old_fsgid;
482 	kernel_cap_t old_cap;
483 	int res;
484 
485 	if (mode & ~S_IRWXO)	/* where's F_OK, X_OK, W_OK, R_OK? */
486 		return -EINVAL;
487 
488 	old_fsuid = current->fsuid;
489 	old_fsgid = current->fsgid;
490 	old_cap = current->cap_effective;
491 
492 	current->fsuid = current->uid;
493 	current->fsgid = current->gid;
494 
495 	/*
496 	 * Clear the capabilities if we switch to a non-root user
497 	 *
498 	 * FIXME: There is a race here against sys_capset.  The
499 	 * capabilities can change yet we will restore the old
500 	 * value below.  We should hold task_capabilities_lock,
501 	 * but we cannot because user_path_walk can sleep.
502 	 */
503 	if (current->uid)
504 		cap_clear(current->cap_effective);
505 	else
506 		current->cap_effective = current->cap_permitted;
507 
508 	res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
509 	if (!res) {
510 		res = vfs_permission(&nd, mode);
511 		/* SuS v2 requires we report a read only fs too */
512 		if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
513 		   && !special_file(nd.dentry->d_inode->i_mode))
514 			res = -EROFS;
515 		path_release(&nd);
516 	}
517 
518 	current->fsuid = old_fsuid;
519 	current->fsgid = old_fsgid;
520 	current->cap_effective = old_cap;
521 
522 	return res;
523 }
524 
525 asmlinkage long sys_chdir(const char __user * filename)
526 {
527 	struct nameidata nd;
528 	int error;
529 
530 	error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
531 	if (error)
532 		goto out;
533 
534 	error = vfs_permission(&nd, MAY_EXEC);
535 	if (error)
536 		goto dput_and_out;
537 
538 	set_fs_pwd(current->fs, nd.mnt, nd.dentry);
539 
540 dput_and_out:
541 	path_release(&nd);
542 out:
543 	return error;
544 }
545 
546 asmlinkage long sys_fchdir(unsigned int fd)
547 {
548 	struct file *file;
549 	struct dentry *dentry;
550 	struct inode *inode;
551 	struct vfsmount *mnt;
552 	int error;
553 
554 	error = -EBADF;
555 	file = fget(fd);
556 	if (!file)
557 		goto out;
558 
559 	dentry = file->f_dentry;
560 	mnt = file->f_vfsmnt;
561 	inode = dentry->d_inode;
562 
563 	error = -ENOTDIR;
564 	if (!S_ISDIR(inode->i_mode))
565 		goto out_putf;
566 
567 	error = file_permission(file, MAY_EXEC);
568 	if (!error)
569 		set_fs_pwd(current->fs, mnt, dentry);
570 out_putf:
571 	fput(file);
572 out:
573 	return error;
574 }
575 
576 asmlinkage long sys_chroot(const char __user * filename)
577 {
578 	struct nameidata nd;
579 	int error;
580 
581 	error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
582 	if (error)
583 		goto out;
584 
585 	error = vfs_permission(&nd, MAY_EXEC);
586 	if (error)
587 		goto dput_and_out;
588 
589 	error = -EPERM;
590 	if (!capable(CAP_SYS_CHROOT))
591 		goto dput_and_out;
592 
593 	set_fs_root(current->fs, nd.mnt, nd.dentry);
594 	set_fs_altroot();
595 	error = 0;
596 dput_and_out:
597 	path_release(&nd);
598 out:
599 	return error;
600 }
601 
602 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
603 {
604 	struct inode * inode;
605 	struct dentry * dentry;
606 	struct file * file;
607 	int err = -EBADF;
608 	struct iattr newattrs;
609 
610 	file = fget(fd);
611 	if (!file)
612 		goto out;
613 
614 	dentry = file->f_dentry;
615 	inode = dentry->d_inode;
616 
617 	err = -EROFS;
618 	if (IS_RDONLY(inode))
619 		goto out_putf;
620 	err = -EPERM;
621 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
622 		goto out_putf;
623 	mutex_lock(&inode->i_mutex);
624 	if (mode == (mode_t) -1)
625 		mode = inode->i_mode;
626 	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
627 	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
628 	err = notify_change(dentry, &newattrs);
629 	mutex_unlock(&inode->i_mutex);
630 
631 out_putf:
632 	fput(file);
633 out:
634 	return err;
635 }
636 
637 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
638 {
639 	struct nameidata nd;
640 	struct inode * inode;
641 	int error;
642 	struct iattr newattrs;
643 
644 	error = user_path_walk(filename, &nd);
645 	if (error)
646 		goto out;
647 	inode = nd.dentry->d_inode;
648 
649 	error = -EROFS;
650 	if (IS_RDONLY(inode))
651 		goto dput_and_out;
652 
653 	error = -EPERM;
654 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
655 		goto dput_and_out;
656 
657 	mutex_lock(&inode->i_mutex);
658 	if (mode == (mode_t) -1)
659 		mode = inode->i_mode;
660 	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
661 	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
662 	error = notify_change(nd.dentry, &newattrs);
663 	mutex_unlock(&inode->i_mutex);
664 
665 dput_and_out:
666 	path_release(&nd);
667 out:
668 	return error;
669 }
670 
671 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
672 {
673 	struct inode * inode;
674 	int error;
675 	struct iattr newattrs;
676 
677 	error = -ENOENT;
678 	if (!(inode = dentry->d_inode)) {
679 		printk(KERN_ERR "chown_common: NULL inode\n");
680 		goto out;
681 	}
682 	error = -EROFS;
683 	if (IS_RDONLY(inode))
684 		goto out;
685 	error = -EPERM;
686 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
687 		goto out;
688 	newattrs.ia_valid =  ATTR_CTIME;
689 	if (user != (uid_t) -1) {
690 		newattrs.ia_valid |= ATTR_UID;
691 		newattrs.ia_uid = user;
692 	}
693 	if (group != (gid_t) -1) {
694 		newattrs.ia_valid |= ATTR_GID;
695 		newattrs.ia_gid = group;
696 	}
697 	if (!S_ISDIR(inode->i_mode))
698 		newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
699 	mutex_lock(&inode->i_mutex);
700 	error = notify_change(dentry, &newattrs);
701 	mutex_unlock(&inode->i_mutex);
702 out:
703 	return error;
704 }
705 
706 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
707 {
708 	struct nameidata nd;
709 	int error;
710 
711 	error = user_path_walk(filename, &nd);
712 	if (!error) {
713 		error = chown_common(nd.dentry, user, group);
714 		path_release(&nd);
715 	}
716 	return error;
717 }
718 
719 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
720 {
721 	struct nameidata nd;
722 	int error;
723 
724 	error = user_path_walk_link(filename, &nd);
725 	if (!error) {
726 		error = chown_common(nd.dentry, user, group);
727 		path_release(&nd);
728 	}
729 	return error;
730 }
731 
732 
733 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
734 {
735 	struct file * file;
736 	int error = -EBADF;
737 
738 	file = fget(fd);
739 	if (file) {
740 		error = chown_common(file->f_dentry, user, group);
741 		fput(file);
742 	}
743 	return error;
744 }
745 
746 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
747 					int flags, struct file *f,
748 					int (*open)(struct inode *, struct file *))
749 {
750 	struct inode *inode;
751 	int error;
752 
753 	f->f_flags = flags;
754 	f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
755 				FMODE_PREAD | FMODE_PWRITE;
756 	inode = dentry->d_inode;
757 	if (f->f_mode & FMODE_WRITE) {
758 		error = get_write_access(inode);
759 		if (error)
760 			goto cleanup_file;
761 	}
762 
763 	f->f_mapping = inode->i_mapping;
764 	f->f_dentry = dentry;
765 	f->f_vfsmnt = mnt;
766 	f->f_pos = 0;
767 	f->f_op = fops_get(inode->i_fop);
768 	file_move(f, &inode->i_sb->s_files);
769 
770 	if (!open && f->f_op)
771 		open = f->f_op->open;
772 	if (open) {
773 		error = open(inode, f);
774 		if (error)
775 			goto cleanup_all;
776 	}
777 
778 	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
779 
780 	file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
781 
782 	/* NB: we're sure to have correct a_ops only after f_op->open */
783 	if (f->f_flags & O_DIRECT) {
784 		if (!f->f_mapping->a_ops ||
785 		    ((!f->f_mapping->a_ops->direct_IO) &&
786 		    (!f->f_mapping->a_ops->get_xip_page))) {
787 			fput(f);
788 			f = ERR_PTR(-EINVAL);
789 		}
790 	}
791 
792 	return f;
793 
794 cleanup_all:
795 	fops_put(f->f_op);
796 	if (f->f_mode & FMODE_WRITE)
797 		put_write_access(inode);
798 	file_kill(f);
799 	f->f_dentry = NULL;
800 	f->f_vfsmnt = NULL;
801 cleanup_file:
802 	put_filp(f);
803 	dput(dentry);
804 	mntput(mnt);
805 	return ERR_PTR(error);
806 }
807 
808 /*
809  * Note that while the flag value (low two bits) for sys_open means:
810  *	00 - read-only
811  *	01 - write-only
812  *	10 - read-write
813  *	11 - special
814  * it is changed into
815  *	00 - no permissions needed
816  *	01 - read-permission
817  *	10 - write-permission
818  *	11 - read-write
819  * for the internal routines (ie open_namei()/follow_link() etc). 00 is
820  * used by symlinks.
821  */
822 struct file *filp_open(const char * filename, int flags, int mode)
823 {
824 	int namei_flags, error;
825 	struct nameidata nd;
826 
827 	namei_flags = flags;
828 	if ((namei_flags+1) & O_ACCMODE)
829 		namei_flags++;
830 
831 	error = open_namei(filename, namei_flags, mode, &nd);
832 	if (!error)
833 		return nameidata_to_filp(&nd, flags);
834 
835 	return ERR_PTR(error);
836 }
837 EXPORT_SYMBOL(filp_open);
838 
839 /**
840  * lookup_instantiate_filp - instantiates the open intent filp
841  * @nd: pointer to nameidata
842  * @dentry: pointer to dentry
843  * @open: open callback
844  *
845  * Helper for filesystems that want to use lookup open intents and pass back
846  * a fully instantiated struct file to the caller.
847  * This function is meant to be called from within a filesystem's
848  * lookup method.
849  * Note that in case of error, nd->intent.open.file is destroyed, but the
850  * path information remains valid.
851  * If the open callback is set to NULL, then the standard f_op->open()
852  * filesystem callback is substituted.
853  */
854 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
855 		int (*open)(struct inode *, struct file *))
856 {
857 	if (IS_ERR(nd->intent.open.file))
858 		goto out;
859 	if (IS_ERR(dentry))
860 		goto out_err;
861 	nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
862 					     nd->intent.open.flags - 1,
863 					     nd->intent.open.file,
864 					     open);
865 out:
866 	return nd->intent.open.file;
867 out_err:
868 	release_open_intent(nd);
869 	nd->intent.open.file = (struct file *)dentry;
870 	goto out;
871 }
872 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
873 
874 /**
875  * nameidata_to_filp - convert a nameidata to an open filp.
876  * @nd: pointer to nameidata
877  * @flags: open flags
878  *
879  * Note that this function destroys the original nameidata
880  */
881 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
882 {
883 	struct file *filp;
884 
885 	/* Pick up the filp from the open intent */
886 	filp = nd->intent.open.file;
887 	/* Has the filesystem initialised the file for us? */
888 	if (filp->f_dentry == NULL)
889 		filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
890 	else
891 		path_release(nd);
892 	return filp;
893 }
894 
895 /*
896  * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
897  * error.
898  */
899 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
900 {
901 	int error;
902 	struct file *f;
903 
904 	error = -ENFILE;
905 	f = get_empty_filp();
906 	if (f == NULL) {
907 		dput(dentry);
908 		mntput(mnt);
909 		return ERR_PTR(error);
910 	}
911 
912 	return __dentry_open(dentry, mnt, flags, f, NULL);
913 }
914 EXPORT_SYMBOL(dentry_open);
915 
916 /*
917  * Find an empty file descriptor entry, and mark it busy.
918  */
919 int get_unused_fd(void)
920 {
921 	struct files_struct * files = current->files;
922 	int fd, error;
923 	struct fdtable *fdt;
924 
925   	error = -EMFILE;
926 	spin_lock(&files->file_lock);
927 
928 repeat:
929 	fdt = files_fdtable(files);
930  	fd = find_next_zero_bit(fdt->open_fds->fds_bits,
931 				fdt->max_fdset,
932 				fdt->next_fd);
933 
934 	/*
935 	 * N.B. For clone tasks sharing a files structure, this test
936 	 * will limit the total number of files that can be opened.
937 	 */
938 	if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
939 		goto out;
940 
941 	/* Do we need to expand the fd array or fd set?  */
942 	error = expand_files(files, fd);
943 	if (error < 0)
944 		goto out;
945 
946 	if (error) {
947 		/*
948 	 	 * If we needed to expand the fs array we
949 		 * might have blocked - try again.
950 		 */
951 		error = -EMFILE;
952 		goto repeat;
953 	}
954 
955 	FD_SET(fd, fdt->open_fds);
956 	FD_CLR(fd, fdt->close_on_exec);
957 	fdt->next_fd = fd + 1;
958 #if 1
959 	/* Sanity check */
960 	if (fdt->fd[fd] != NULL) {
961 		printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
962 		fdt->fd[fd] = NULL;
963 	}
964 #endif
965 	error = fd;
966 
967 out:
968 	spin_unlock(&files->file_lock);
969 	return error;
970 }
971 
972 EXPORT_SYMBOL(get_unused_fd);
973 
974 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
975 {
976 	struct fdtable *fdt = files_fdtable(files);
977 	__FD_CLR(fd, fdt->open_fds);
978 	if (fd < fdt->next_fd)
979 		fdt->next_fd = fd;
980 }
981 
982 void fastcall put_unused_fd(unsigned int fd)
983 {
984 	struct files_struct *files = current->files;
985 	spin_lock(&files->file_lock);
986 	__put_unused_fd(files, fd);
987 	spin_unlock(&files->file_lock);
988 }
989 
990 EXPORT_SYMBOL(put_unused_fd);
991 
992 /*
993  * Install a file pointer in the fd array.
994  *
995  * The VFS is full of places where we drop the files lock between
996  * setting the open_fds bitmap and installing the file in the file
997  * array.  At any such point, we are vulnerable to a dup2() race
998  * installing a file in the array before us.  We need to detect this and
999  * fput() the struct file we are about to overwrite in this case.
1000  *
1001  * It should never happen - if we allow dup2() do it, _really_ bad things
1002  * will follow.
1003  */
1004 
1005 void fastcall fd_install(unsigned int fd, struct file * file)
1006 {
1007 	struct files_struct *files = current->files;
1008 	struct fdtable *fdt;
1009 	spin_lock(&files->file_lock);
1010 	fdt = files_fdtable(files);
1011 	BUG_ON(fdt->fd[fd] != NULL);
1012 	rcu_assign_pointer(fdt->fd[fd], file);
1013 	spin_unlock(&files->file_lock);
1014 }
1015 
1016 EXPORT_SYMBOL(fd_install);
1017 
1018 long do_sys_open(const char __user *filename, int flags, int mode)
1019 {
1020 	char *tmp = getname(filename);
1021 	int fd = PTR_ERR(tmp);
1022 
1023 	if (!IS_ERR(tmp)) {
1024 		fd = get_unused_fd();
1025 		if (fd >= 0) {
1026 			struct file *f = filp_open(tmp, flags, mode);
1027 			if (IS_ERR(f)) {
1028 				put_unused_fd(fd);
1029 				fd = PTR_ERR(f);
1030 			} else {
1031 				fsnotify_open(f->f_dentry);
1032 				fd_install(fd, f);
1033 			}
1034 		}
1035 		putname(tmp);
1036 	}
1037 	return fd;
1038 }
1039 
1040 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1041 {
1042 	if (force_o_largefile())
1043 		flags |= O_LARGEFILE;
1044 
1045 	return do_sys_open(filename, flags, mode);
1046 }
1047 EXPORT_SYMBOL_GPL(sys_open);
1048 
1049 #ifndef __alpha__
1050 
1051 /*
1052  * For backward compatibility?  Maybe this should be moved
1053  * into arch/i386 instead?
1054  */
1055 asmlinkage long sys_creat(const char __user * pathname, int mode)
1056 {
1057 	return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1058 }
1059 
1060 #endif
1061 
1062 /*
1063  * "id" is the POSIX thread ID. We use the
1064  * files pointer for this..
1065  */
1066 int filp_close(struct file *filp, fl_owner_t id)
1067 {
1068 	int retval = 0;
1069 
1070 	if (!file_count(filp)) {
1071 		printk(KERN_ERR "VFS: Close: file count is 0\n");
1072 		return 0;
1073 	}
1074 
1075 	if (filp->f_op && filp->f_op->flush)
1076 		retval = filp->f_op->flush(filp);
1077 
1078 	dnotify_flush(filp, id);
1079 	locks_remove_posix(filp, id);
1080 	fput(filp);
1081 	return retval;
1082 }
1083 
1084 EXPORT_SYMBOL(filp_close);
1085 
1086 /*
1087  * Careful here! We test whether the file pointer is NULL before
1088  * releasing the fd. This ensures that one clone task can't release
1089  * an fd while another clone is opening it.
1090  */
1091 asmlinkage long sys_close(unsigned int fd)
1092 {
1093 	struct file * filp;
1094 	struct files_struct *files = current->files;
1095 	struct fdtable *fdt;
1096 
1097 	spin_lock(&files->file_lock);
1098 	fdt = files_fdtable(files);
1099 	if (fd >= fdt->max_fds)
1100 		goto out_unlock;
1101 	filp = fdt->fd[fd];
1102 	if (!filp)
1103 		goto out_unlock;
1104 	rcu_assign_pointer(fdt->fd[fd], NULL);
1105 	FD_CLR(fd, fdt->close_on_exec);
1106 	__put_unused_fd(files, fd);
1107 	spin_unlock(&files->file_lock);
1108 	return filp_close(filp, files);
1109 
1110 out_unlock:
1111 	spin_unlock(&files->file_lock);
1112 	return -EBADF;
1113 }
1114 
1115 EXPORT_SYMBOL(sys_close);
1116 
1117 /*
1118  * This routine simulates a hangup on the tty, to arrange that users
1119  * are given clean terminals at login time.
1120  */
1121 asmlinkage long sys_vhangup(void)
1122 {
1123 	if (capable(CAP_SYS_TTY_CONFIG)) {
1124 		tty_vhangup(current->signal->tty);
1125 		return 0;
1126 	}
1127 	return -EPERM;
1128 }
1129 
1130 /*
1131  * Called when an inode is about to be open.
1132  * We use this to disallow opening large files on 32bit systems if
1133  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1134  * on this flag in sys_open.
1135  */
1136 int generic_file_open(struct inode * inode, struct file * filp)
1137 {
1138 	if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1139 		return -EFBIG;
1140 	return 0;
1141 }
1142 
1143 EXPORT_SYMBOL(generic_file_open);
1144 
1145 /*
1146  * This is used by subsystems that don't want seekable
1147  * file descriptors
1148  */
1149 int nonseekable_open(struct inode *inode, struct file *filp)
1150 {
1151 	filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1152 	return 0;
1153 }
1154 
1155 EXPORT_SYMBOL(nonseekable_open);
1156