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/dnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/iobuf.h>
18 
19 #include <asm/uaccess.h>
20 
21 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
22 
vfs_statfs(struct super_block * sb,struct statfs * buf)23 int vfs_statfs(struct super_block *sb, struct statfs *buf)
24 {
25 	int retval = -ENODEV;
26 
27 	if (sb) {
28 		retval = -ENOSYS;
29 		if (sb->s_op && sb->s_op->statfs) {
30 			memset(buf, 0, sizeof(struct statfs));
31 			lock_kernel();
32 			retval = sb->s_op->statfs(sb, buf);
33 			unlock_kernel();
34 		}
35 	}
36 	return retval;
37 }
38 
39 
sys_statfs(const char * path,struct statfs * buf)40 asmlinkage long sys_statfs(const char * path, struct statfs * buf)
41 {
42 	struct nameidata nd;
43 	int error;
44 
45 	error = user_path_walk(path, &nd);
46 	if (!error) {
47 		struct statfs tmp;
48 		error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
49 		if (!error && copy_to_user(buf, &tmp, sizeof(struct statfs)))
50 			error = -EFAULT;
51 		path_release(&nd);
52 	}
53 	return error;
54 }
55 
sys_fstatfs(unsigned int fd,struct statfs * buf)56 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs * buf)
57 {
58 	struct file * file;
59 	struct statfs tmp;
60 	int error;
61 
62 	error = -EBADF;
63 	file = fget(fd);
64 	if (!file)
65 		goto out;
66 	error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
67 	if (!error && copy_to_user(buf, &tmp, sizeof(struct statfs)))
68 		error = -EFAULT;
69 	fput(file);
70 out:
71 	return error;
72 }
73 
74 /*
75  * Install a file pointer in the fd array.
76  *
77  * The VFS is full of places where we drop the files lock between
78  * setting the open_fds bitmap and installing the file in the file
79  * array.  At any such point, we are vulnerable to a dup2() race
80  * installing a file in the array before us.  We need to detect this and
81  * fput() the struct file we are about to overwrite in this case.
82  *
83  * It should never happen - if we allow dup2() do it, _really_ bad things
84  * will follow.
85  */
86 
fd_install(unsigned int fd,struct file * file)87 void fd_install(unsigned int fd, struct file * file)
88 {
89 	struct files_struct *files = current->files;
90 
91 	write_lock(&files->file_lock);
92 	if (files->fd[fd])
93 		BUG();
94 	files->fd[fd] = file;
95 	write_unlock(&files->file_lock);
96 }
97 
do_truncate(struct dentry * dentry,loff_t length)98 int do_truncate(struct dentry *dentry, loff_t length)
99 {
100 	struct inode *inode = dentry->d_inode;
101 	int error;
102 	struct iattr newattrs;
103 
104 	/* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
105 	if (length < 0)
106 		return -EINVAL;
107 
108 	down_write(&inode->i_alloc_sem);
109 	down(&inode->i_sem);
110 	newattrs.ia_size = length;
111 	newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
112 	/* Remove suid/sgid on truncate too */
113 	remove_suid(inode);
114 	error = notify_change(dentry, &newattrs);
115 	up(&inode->i_sem);
116 	up_write(&inode->i_alloc_sem);
117 	return error;
118 }
119 
do_sys_truncate(const char * path,loff_t length)120 static inline long do_sys_truncate(const char * path, loff_t length)
121 {
122 	struct nameidata nd;
123 	struct inode * inode;
124 	int error;
125 
126 	error = -EINVAL;
127 	if (length < 0)	/* sorry, but loff_t says... */
128 		goto out;
129 
130 	error = user_path_walk(path, &nd);
131 	if (error)
132 		goto out;
133 	inode = nd.dentry->d_inode;
134 
135 	/* For directories it's -EISDIR, for other non-regulars - -EINVAL */
136 	error = -EISDIR;
137 	if (S_ISDIR(inode->i_mode))
138 		goto dput_and_out;
139 
140 	error = -EINVAL;
141 	if (!S_ISREG(inode->i_mode))
142 		goto dput_and_out;
143 
144 	error = permission(inode,MAY_WRITE);
145 	if (error)
146 		goto dput_and_out;
147 
148 	error = -EROFS;
149 	if (IS_RDONLY(inode))
150 		goto dput_and_out;
151 
152 	error = -EPERM;
153 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
154 		goto dput_and_out;
155 
156 	/*
157 	 * Make sure that there are no leases.
158 	 */
159 	error = get_lease(inode, FMODE_WRITE);
160 	if (error)
161 		goto dput_and_out;
162 
163 	error = get_write_access(inode);
164 	if (error)
165 		goto dput_and_out;
166 
167 	error = locks_verify_truncate(inode, NULL, length);
168 	if (!error) {
169 		DQUOT_INIT(inode);
170 		error = do_truncate(nd.dentry, length);
171 	}
172 	put_write_access(inode);
173 
174 dput_and_out:
175 	path_release(&nd);
176 out:
177 	return error;
178 }
179 
sys_truncate(const char * path,unsigned long length)180 asmlinkage long sys_truncate(const char * path, unsigned long length)
181 {
182 	/* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
183 	return do_sys_truncate(path, (long)length);
184 }
185 
do_sys_ftruncate(unsigned int fd,loff_t length,int small)186 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
187 {
188 	struct inode * inode;
189 	struct dentry *dentry;
190 	struct file * file;
191 	int error;
192 
193 	error = -EINVAL;
194 	if (length < 0)
195 		goto out;
196 	error = -EBADF;
197 	file = fget(fd);
198 	if (!file)
199 		goto out;
200 
201 	/* explicitly opened as large or we are on 64-bit box */
202 	if (file->f_flags & O_LARGEFILE)
203 		small = 0;
204 
205 	dentry = file->f_dentry;
206 	inode = dentry->d_inode;
207 	error = -EINVAL;
208 	if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
209 		goto out_putf;
210 
211 	error = -EINVAL;
212 	/* Cannot ftruncate over 2^31 bytes without large file support */
213 	if (small && length > MAX_NON_LFS)
214 		goto out_putf;
215 
216 	error = -EPERM;
217 	if (IS_APPEND(inode))
218 		goto out_putf;
219 
220 	error = locks_verify_truncate(inode, file, length);
221 	if (!error)
222 		error = do_truncate(dentry, length);
223 out_putf:
224 	fput(file);
225 out:
226 	return error;
227 }
228 
sys_ftruncate(unsigned int fd,unsigned long length)229 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
230 {
231 	return do_sys_ftruncate(fd, length, 1);
232 }
233 
234 /* LFS versions of truncate are only needed on 32 bit machines */
235 #if BITS_PER_LONG == 32
sys_truncate64(const char * path,loff_t length)236 asmlinkage long sys_truncate64(const char * path, loff_t length)
237 {
238 	return do_sys_truncate(path, length);
239 }
240 
sys_ftruncate64(unsigned int fd,loff_t length)241 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
242 {
243 	return do_sys_ftruncate(fd, length, 0);
244 }
245 #endif
246 
247 #if !(defined(__alpha__) || defined(__ia64__))
248 
249 /*
250  * sys_utime() can be implemented in user-level using sys_utimes().
251  * Is this for backwards compatibility?  If so, why not move it
252  * into the appropriate arch directory (for those architectures that
253  * need it).
254  */
255 
256 /* If times==NULL, set access and modification to current time,
257  * must be owner or have write permission.
258  * Else, update from *times, must be owner or super user.
259  */
sys_utime(char * filename,struct utimbuf * times)260 asmlinkage long sys_utime(char * filename, struct utimbuf * times)
261 {
262 	int error;
263 	struct nameidata nd;
264 	struct inode * inode;
265 	struct iattr newattrs;
266 
267 	error = user_path_walk(filename, &nd);
268 	if (error)
269 		goto out;
270 	inode = nd.dentry->d_inode;
271 
272 	error = -EROFS;
273 	if (IS_RDONLY(inode))
274 		goto dput_and_out;
275 
276 	/* Don't worry, the checks are done in inode_change_ok() */
277 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
278 	if (times) {
279 		error = -EPERM;
280 		if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
281 			goto dput_and_out;
282 		error = get_user(newattrs.ia_atime, &times->actime);
283 		if (!error)
284 			error = get_user(newattrs.ia_mtime, &times->modtime);
285 		if (error)
286 			goto dput_and_out;
287 
288 		newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
289 	} else {
290 		error = -EACCES;
291 		if (IS_IMMUTABLE(inode))
292 			goto dput_and_out;
293 		if (current->fsuid != inode->i_uid &&
294 		    (error = permission(inode,MAY_WRITE)) != 0)
295 			goto dput_and_out;
296 	}
297 	error = notify_change(nd.dentry, &newattrs);
298 dput_and_out:
299 	path_release(&nd);
300 out:
301 	return error;
302 }
303 
304 #endif
305 
306 /* If times==NULL, set access and modification to current time,
307  * must be owner or have write permission.
308  * Else, update from *times, must be owner or super user.
309  */
sys_utimes(char * filename,struct timeval * utimes)310 asmlinkage long sys_utimes(char * filename, struct timeval * utimes)
311 {
312 	int error;
313 	struct nameidata nd;
314 	struct inode * inode;
315 	struct iattr newattrs;
316 
317 	error = user_path_walk(filename, &nd);
318 
319 	if (error)
320 		goto out;
321 	inode = nd.dentry->d_inode;
322 
323 	error = -EROFS;
324 	if (IS_RDONLY(inode))
325 		goto dput_and_out;
326 
327 	/* Don't worry, the checks are done in inode_change_ok() */
328 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
329 	if (utimes) {
330 		struct timeval times[2];
331 		error = -EPERM;
332 		if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
333 			goto dput_and_out;
334 		error = -EFAULT;
335 		if (copy_from_user(&times, utimes, sizeof(times)))
336 			goto dput_and_out;
337 		newattrs.ia_atime = times[0].tv_sec;
338 		newattrs.ia_mtime = times[1].tv_sec;
339 		newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
340 	} else {
341 		error = -EACCES;
342 		if (IS_IMMUTABLE(inode))
343 			goto dput_and_out;
344 
345 		if (current->fsuid != inode->i_uid &&
346 		    (error = permission(inode,MAY_WRITE)) != 0)
347 			goto dput_and_out;
348 	}
349 	error = notify_change(nd.dentry, &newattrs);
350 dput_and_out:
351 	path_release(&nd);
352 out:
353 	return error;
354 }
355 
356 /*
357  * access() needs to use the real uid/gid, not the effective uid/gid.
358  * We do this by temporarily clearing all FS-related capabilities and
359  * switching the fsuid/fsgid around to the real ones.
360  */
sys_access(const char * filename,int mode)361 asmlinkage long sys_access(const char * filename, int mode)
362 {
363 	struct nameidata nd;
364 	int old_fsuid, old_fsgid;
365 	kernel_cap_t old_cap;
366 	int res;
367 
368 	if (mode & ~S_IRWXO)	/* where's F_OK, X_OK, W_OK, R_OK? */
369 		return -EINVAL;
370 
371 	old_fsuid = current->fsuid;
372 	old_fsgid = current->fsgid;
373 	old_cap = current->cap_effective;
374 
375 	current->fsuid = current->uid;
376 	current->fsgid = current->gid;
377 
378 	/* Clear the capabilities if we switch to a non-root user */
379 	if (current->uid)
380 		cap_clear(current->cap_effective);
381 	else
382 		current->cap_effective = current->cap_permitted;
383 
384 	res = user_path_walk(filename, &nd);
385 	if (!res) {
386 		res = permission(nd.dentry->d_inode, mode);
387 		/* SuS v2 requires we report a read only fs too */
388 		if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
389 		   && !special_file(nd.dentry->d_inode->i_mode))
390 			res = -EROFS;
391 		path_release(&nd);
392 	}
393 
394 	current->fsuid = old_fsuid;
395 	current->fsgid = old_fsgid;
396 	current->cap_effective = old_cap;
397 
398 	return res;
399 }
400 
sys_chdir(const char * filename)401 asmlinkage long sys_chdir(const char * filename)
402 {
403 	int error;
404 	struct nameidata nd;
405 
406 	error = __user_walk(filename,LOOKUP_POSITIVE|LOOKUP_FOLLOW|LOOKUP_DIRECTORY,&nd);
407 	if (error)
408 		goto out;
409 
410 	error = permission(nd.dentry->d_inode,MAY_EXEC);
411 	if (error)
412 		goto dput_and_out;
413 
414 	set_fs_pwd(current->fs, nd.mnt, nd.dentry);
415 
416 dput_and_out:
417 	path_release(&nd);
418 out:
419 	return error;
420 }
421 
sys_fchdir(unsigned int fd)422 asmlinkage long sys_fchdir(unsigned int fd)
423 {
424 	struct file *file;
425 	struct dentry *dentry;
426 	struct inode *inode;
427 	struct vfsmount *mnt;
428 	int error;
429 
430 	error = -EBADF;
431 	file = fget(fd);
432 	if (!file)
433 		goto out;
434 
435 	dentry = file->f_dentry;
436 	mnt = file->f_vfsmnt;
437 	inode = dentry->d_inode;
438 
439 	error = -ENOTDIR;
440 	if (!S_ISDIR(inode->i_mode))
441 		goto out_putf;
442 
443 	error = permission(inode, MAY_EXEC);
444 	if (!error)
445 		set_fs_pwd(current->fs, mnt, dentry);
446 out_putf:
447 	fput(file);
448 out:
449 	return error;
450 }
451 
sys_chroot(const char * filename)452 asmlinkage long sys_chroot(const char * filename)
453 {
454 	int error;
455 	struct nameidata nd;
456 
457 	error = __user_walk(filename, LOOKUP_POSITIVE | LOOKUP_FOLLOW |
458 		      LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
459 	if (error)
460 		goto out;
461 
462 	error = permission(nd.dentry->d_inode,MAY_EXEC);
463 	if (error)
464 		goto dput_and_out;
465 
466 	error = -EPERM;
467 	if (!capable(CAP_SYS_CHROOT))
468 		goto dput_and_out;
469 
470 	set_fs_root(current->fs, nd.mnt, nd.dentry);
471 	set_fs_altroot();
472 	error = 0;
473 dput_and_out:
474 	path_release(&nd);
475 out:
476 	return error;
477 }
478 
sys_fchmod(unsigned int fd,mode_t mode)479 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
480 {
481 	struct inode * inode;
482 	struct dentry * dentry;
483 	struct file * file;
484 	int err = -EBADF;
485 	struct iattr newattrs;
486 
487 	file = fget(fd);
488 	if (!file)
489 		goto out;
490 
491 	dentry = file->f_dentry;
492 	inode = dentry->d_inode;
493 
494 	err = -EROFS;
495 	if (IS_RDONLY(inode))
496 		goto out_putf;
497 	err = -EPERM;
498 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
499 		goto out_putf;
500 	if (mode == (mode_t) -1)
501 		mode = inode->i_mode;
502 	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
503 	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
504 	err = notify_change(dentry, &newattrs);
505 
506 out_putf:
507 	fput(file);
508 out:
509 	return err;
510 }
511 
sys_chmod(const char * filename,mode_t mode)512 asmlinkage long sys_chmod(const char * filename, mode_t mode)
513 {
514 	struct nameidata nd;
515 	struct inode * inode;
516 	int error;
517 	struct iattr newattrs;
518 
519 	error = user_path_walk(filename, &nd);
520 	if (error)
521 		goto out;
522 	inode = nd.dentry->d_inode;
523 
524 	error = -EROFS;
525 	if (IS_RDONLY(inode))
526 		goto dput_and_out;
527 
528 	error = -EPERM;
529 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
530 		goto dput_and_out;
531 
532 	if (mode == (mode_t) -1)
533 		mode = inode->i_mode;
534 	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
535 	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
536 	error = notify_change(nd.dentry, &newattrs);
537 
538 dput_and_out:
539 	path_release(&nd);
540 out:
541 	return error;
542 }
543 
chown_common(struct dentry * dentry,uid_t user,gid_t group)544 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
545 {
546 	struct inode * inode;
547 	int error;
548 	struct iattr newattrs;
549 
550 	error = -ENOENT;
551 	if (!(inode = dentry->d_inode)) {
552 		printk(KERN_ERR "chown_common: NULL inode\n");
553 		goto out;
554 	}
555 	error = -EROFS;
556 	if (IS_RDONLY(inode))
557 		goto out;
558 	error = -EPERM;
559 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
560 		goto out;
561 	if (user == (uid_t) -1)
562 		user = inode->i_uid;
563 	if (group == (gid_t) -1)
564 		group = inode->i_gid;
565 	newattrs.ia_mode = inode->i_mode;
566 	newattrs.ia_uid = user;
567 	newattrs.ia_gid = group;
568 	newattrs.ia_valid =  ATTR_UID | ATTR_GID | ATTR_CTIME;
569 	/*
570 	 * If the user or group of a non-directory has been changed by a
571 	 * non-root user, remove the setuid bit.
572 	 * 19981026	David C Niemi <niemi@tux.org>
573 	 *
574 	 * Changed this to apply to all users, including root, to avoid
575 	 * some races. This is the behavior we had in 2.0. The check for
576 	 * non-root was definitely wrong for 2.2 anyway, as it should
577 	 * have been using CAP_FSETID rather than fsuid -- 19990830 SD.
578 	 */
579 	if ((inode->i_mode & S_ISUID) == S_ISUID &&
580 		!S_ISDIR(inode->i_mode))
581 	{
582 		newattrs.ia_mode &= ~S_ISUID;
583 		newattrs.ia_valid |= ATTR_MODE;
584 	}
585 	/*
586 	 * Likewise, if the user or group of a non-directory has been changed
587 	 * by a non-root user, remove the setgid bit UNLESS there is no group
588 	 * execute bit (this would be a file marked for mandatory locking).
589 	 * 19981026	David C Niemi <niemi@tux.org>
590 	 *
591 	 * Removed the fsuid check (see the comment above) -- 19990830 SD.
592 	 */
593 	if (((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
594 		&& !S_ISDIR(inode->i_mode))
595 	{
596 		newattrs.ia_mode &= ~S_ISGID;
597 		newattrs.ia_valid |= ATTR_MODE;
598 	}
599 	error = notify_change(dentry, &newattrs);
600 out:
601 	return error;
602 }
603 
sys_chown(const char * filename,uid_t user,gid_t group)604 asmlinkage long sys_chown(const char * filename, uid_t user, gid_t group)
605 {
606 	struct nameidata nd;
607 	int error;
608 
609 	error = user_path_walk(filename, &nd);
610 	if (!error) {
611 		error = chown_common(nd.dentry, user, group);
612 		path_release(&nd);
613 	}
614 	return error;
615 }
616 
sys_lchown(const char * filename,uid_t user,gid_t group)617 asmlinkage long sys_lchown(const char * filename, uid_t user, gid_t group)
618 {
619 	struct nameidata nd;
620 	int error;
621 
622 	error = user_path_walk_link(filename, &nd);
623 	if (!error) {
624 		error = chown_common(nd.dentry, user, group);
625 		path_release(&nd);
626 	}
627 	return error;
628 }
629 
630 
sys_fchown(unsigned int fd,uid_t user,gid_t group)631 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
632 {
633 	struct file * file;
634 	int error = -EBADF;
635 
636 	file = fget(fd);
637 	if (file) {
638 		error = chown_common(file->f_dentry, user, group);
639 		fput(file);
640 	}
641 	return error;
642 }
643 
644 /*
645  * Note that while the flag value (low two bits) for sys_open means:
646  *	00 - read-only
647  *	01 - write-only
648  *	10 - read-write
649  *	11 - special
650  * it is changed into
651  *	00 - no permissions needed
652  *	01 - read-permission
653  *	10 - write-permission
654  *	11 - read-write
655  * for the internal routines (ie open_namei()/follow_link() etc). 00 is
656  * used by symlinks.
657  */
filp_open(const char * filename,int flags,int mode)658 struct file *filp_open(const char * filename, int flags, int mode)
659 {
660 	int namei_flags, error;
661 	struct nameidata nd;
662 
663 	namei_flags = flags;
664 	if ((namei_flags+1) & O_ACCMODE)
665 		namei_flags++;
666 	if (namei_flags & O_TRUNC)
667 		namei_flags |= 2;
668 
669 	error = open_namei(filename, namei_flags, mode, &nd);
670 	if (!error)
671 		return dentry_open(nd.dentry, nd.mnt, flags);
672 
673 	return ERR_PTR(error);
674 }
675 
dentry_open(struct dentry * dentry,struct vfsmount * mnt,int flags)676 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
677 {
678 	struct file * f;
679 	struct inode *inode;
680 	static LIST_HEAD(kill_list);
681 	int error;
682 
683 	error = -ENFILE;
684 	f = get_empty_filp();
685 	if (!f)
686 		goto cleanup_dentry;
687 	f->f_flags = flags;
688 	f->f_mode = (flags+1) & O_ACCMODE;
689 	inode = dentry->d_inode;
690 	if (f->f_mode & FMODE_WRITE) {
691 		error = get_write_access(inode);
692 		if (error)
693 			goto cleanup_file;
694 	}
695 
696 	f->f_dentry = dentry;
697 	f->f_vfsmnt = mnt;
698 	f->f_pos = 0;
699 	f->f_reada = 0;
700 	f->f_op = fops_get(inode->i_fop);
701 	file_move(f, &inode->i_sb->s_files);
702 
703 	/* preallocate kiobuf for O_DIRECT */
704 	f->f_iobuf = NULL;
705 	f->f_iobuf_lock = 0;
706 	if (f->f_flags & O_DIRECT) {
707 		error = alloc_kiovec(1, &f->f_iobuf);
708 		if (error)
709 			goto cleanup_all;
710 	}
711 
712 	if (f->f_op && f->f_op->open) {
713 		error = f->f_op->open(inode,f);
714 		if (error)
715 			goto cleanup_all;
716 	}
717 	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
718 
719 	return f;
720 
721 cleanup_all:
722 	if (f->f_iobuf)
723 		free_kiovec(1, &f->f_iobuf);
724 	fops_put(f->f_op);
725 	if (f->f_mode & FMODE_WRITE)
726 		put_write_access(inode);
727 	file_move(f, &kill_list); /* out of the way.. */
728 	f->f_dentry = NULL;
729 	f->f_vfsmnt = NULL;
730 cleanup_file:
731 	put_filp(f);
732 cleanup_dentry:
733 	dput(dentry);
734 	mntput(mnt);
735 	return ERR_PTR(error);
736 }
737 
738 /*
739  * Find an empty file descriptor entry, and mark it busy.
740  */
get_unused_fd(void)741 int get_unused_fd(void)
742 {
743 	struct files_struct * files = current->files;
744 	int fd, error;
745 
746   	error = -EMFILE;
747 	write_lock(&files->file_lock);
748 
749 repeat:
750  	fd = find_next_zero_bit(files->open_fds,
751 				files->max_fdset,
752 				files->next_fd);
753 
754 	/*
755 	 * N.B. For clone tasks sharing a files structure, this test
756 	 * will limit the total number of files that can be opened.
757 	 */
758 	if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
759 		goto out;
760 
761 	/* Do we need to expand the fdset array? */
762 	if (fd >= files->max_fdset) {
763 		error = expand_fdset(files, fd);
764 		if (!error) {
765 			error = -EMFILE;
766 			goto repeat;
767 		}
768 		goto out;
769 	}
770 
771 	/*
772 	 * Check whether we need to expand the fd array.
773 	 */
774 	if (fd >= files->max_fds) {
775 		error = expand_fd_array(files, fd);
776 		if (!error) {
777 			error = -EMFILE;
778 			goto repeat;
779 		}
780 		goto out;
781 	}
782 
783 	FD_SET(fd, files->open_fds);
784 	FD_CLR(fd, files->close_on_exec);
785 	files->next_fd = fd + 1;
786 #if 1
787 	/* Sanity check */
788 	if (files->fd[fd] != NULL) {
789 		printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
790 		files->fd[fd] = NULL;
791 	}
792 #endif
793 	error = fd;
794 
795 out:
796 	write_unlock(&files->file_lock);
797 	return error;
798 }
799 
sys_open(const char * filename,int flags,int mode)800 asmlinkage long sys_open(const char * filename, int flags, int mode)
801 {
802 	char * tmp;
803 	int fd, error;
804 
805 #if BITS_PER_LONG != 32
806 	flags |= O_LARGEFILE;
807 #endif
808 	tmp = getname(filename);
809 	fd = PTR_ERR(tmp);
810 	if (!IS_ERR(tmp)) {
811 		fd = get_unused_fd();
812 		if (fd >= 0) {
813 			struct file *f = filp_open(tmp, flags, mode);
814 			error = PTR_ERR(f);
815 			if (IS_ERR(f))
816 				goto out_error;
817 			fd_install(fd, f);
818 		}
819 out:
820 		putname(tmp);
821 	}
822 	return fd;
823 
824 out_error:
825 	put_unused_fd(fd);
826 	fd = error;
827 	goto out;
828 }
829 
830 #ifndef __alpha__
831 
832 /*
833  * For backward compatibility?  Maybe this should be moved
834  * into arch/i386 instead?
835  */
sys_creat(const char * pathname,int mode)836 asmlinkage long sys_creat(const char * pathname, int mode)
837 {
838 	return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
839 }
840 
841 #endif
842 
843 /*
844  * "id" is the POSIX thread ID. We use the
845  * files pointer for this..
846  */
filp_close(struct file * filp,fl_owner_t id)847 int filp_close(struct file *filp, fl_owner_t id)
848 {
849 	int retval;
850 
851 	if (!file_count(filp)) {
852 		printk(KERN_ERR "VFS: Close: file count is 0\n");
853 		return 0;
854 	}
855 	retval = 0;
856 	if (filp->f_op && filp->f_op->flush) {
857 		lock_kernel();
858 		retval = filp->f_op->flush(filp);
859 		unlock_kernel();
860 	}
861 	dnotify_flush(filp, id);
862 	locks_remove_posix(filp, id);
863 	fput(filp);
864 	return retval;
865 }
866 
867 /*
868  * Careful here! We test whether the file pointer is NULL before
869  * releasing the fd. This ensures that one clone task can't release
870  * an fd while another clone is opening it.
871  */
sys_close(unsigned int fd)872 asmlinkage long sys_close(unsigned int fd)
873 {
874 	struct file * filp;
875 	struct files_struct *files = current->files;
876 
877 	write_lock(&files->file_lock);
878 	if (fd >= files->max_fds)
879 		goto out_unlock;
880 	filp = files->fd[fd];
881 	if (!filp)
882 		goto out_unlock;
883 	files->fd[fd] = NULL;
884 	FD_CLR(fd, files->close_on_exec);
885 	__put_unused_fd(files, fd);
886 	write_unlock(&files->file_lock);
887 	return filp_close(filp, files);
888 
889 out_unlock:
890 	write_unlock(&files->file_lock);
891 	return -EBADF;
892 }
893 
894 /*
895  * This routine simulates a hangup on the tty, to arrange that users
896  * are given clean terminals at login time.
897  */
sys_vhangup(void)898 asmlinkage long sys_vhangup(void)
899 {
900 	if (capable(CAP_SYS_TTY_CONFIG)) {
901 		tty_vhangup(current->tty);
902 		return 0;
903 	}
904 	return -EPERM;
905 }
906 
907 /*
908  * Called when an inode is about to be open.
909  * We use this to disallow opening RW large files on 32bit systems if
910  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
911  * on this flag in sys_open.
912  */
generic_file_open(struct inode * inode,struct file * filp)913 int generic_file_open(struct inode * inode, struct file * filp)
914 {
915 	if (!(filp->f_flags & O_LARGEFILE) && inode->i_size > MAX_NON_LFS)
916 		return -EFBIG;
917 	return 0;
918 }
919 
920 EXPORT_SYMBOL(generic_file_open);
921