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