PageRenderTime 62ms CodeModel.GetById 2ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 0ms

/linux-2.6.21.x/fs/open.c

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