PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/open.c

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