PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/open.c

https://bitbucket.org/digetx/picasso-kernel
C | 1084 lines | 778 code | 154 blank | 152 comment | 144 complexity | 52e987725663343208ce66784ec5ee75 MD5 | raw file
Possible License(s): AGPL-1.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/file.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/fsnotify.h>
  11. #include <linux/module.h>
  12. #include <linux/tty.h>
  13. #include <linux/namei.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/capability.h>
  16. #include <linux/securebits.h>
  17. #include <linux/security.h>
  18. #include <linux/mount.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/slab.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. #include <linux/falloc.h>
  29. #include <linux/fs_struct.h>
  30. #include <linux/ima.h>
  31. #include <linux/dnotify.h>
  32. #include "internal.h"
  33. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  34. struct file *filp)
  35. {
  36. int ret;
  37. struct iattr newattrs;
  38. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  39. if (length < 0)
  40. return -EINVAL;
  41. newattrs.ia_size = length;
  42. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  43. if (filp) {
  44. newattrs.ia_file = filp;
  45. newattrs.ia_valid |= ATTR_FILE;
  46. }
  47. /* Remove suid/sgid on truncate too */
  48. ret = should_remove_suid(dentry);
  49. if (ret)
  50. newattrs.ia_valid |= ret | ATTR_FORCE;
  51. mutex_lock(&dentry->d_inode->i_mutex);
  52. ret = notify_change(dentry, &newattrs);
  53. mutex_unlock(&dentry->d_inode->i_mutex);
  54. return ret;
  55. }
  56. long vfs_truncate(struct path *path, loff_t length)
  57. {
  58. struct inode *inode;
  59. long error;
  60. inode = path->dentry->d_inode;
  61. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  62. if (S_ISDIR(inode->i_mode))
  63. return -EISDIR;
  64. if (!S_ISREG(inode->i_mode))
  65. return -EINVAL;
  66. error = mnt_want_write(path->mnt);
  67. if (error)
  68. goto out;
  69. error = inode_permission(inode, MAY_WRITE);
  70. if (error)
  71. goto mnt_drop_write_and_out;
  72. error = -EPERM;
  73. if (IS_APPEND(inode))
  74. goto mnt_drop_write_and_out;
  75. error = get_write_access(inode);
  76. if (error)
  77. goto mnt_drop_write_and_out;
  78. /*
  79. * Make sure that there are no leases. get_write_access() protects
  80. * against the truncate racing with a lease-granting setlease().
  81. */
  82. error = break_lease(inode, O_WRONLY);
  83. if (error)
  84. goto put_write_and_out;
  85. error = locks_verify_truncate(inode, NULL, length);
  86. if (!error)
  87. error = security_path_truncate(path);
  88. if (!error)
  89. error = do_truncate(path->dentry, length, 0, NULL);
  90. put_write_and_out:
  91. put_write_access(inode);
  92. mnt_drop_write_and_out:
  93. mnt_drop_write(path->mnt);
  94. out:
  95. return error;
  96. }
  97. EXPORT_SYMBOL_GPL(vfs_truncate);
  98. static long do_sys_truncate(const char __user *pathname, loff_t length)
  99. {
  100. unsigned int lookup_flags = LOOKUP_FOLLOW;
  101. struct path path;
  102. int error;
  103. if (length < 0) /* sorry, but loff_t says... */
  104. return -EINVAL;
  105. retry:
  106. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  107. if (!error) {
  108. error = vfs_truncate(&path, length);
  109. path_put(&path);
  110. }
  111. if (retry_estale(error, lookup_flags)) {
  112. lookup_flags |= LOOKUP_REVAL;
  113. goto retry;
  114. }
  115. return error;
  116. }
  117. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  118. {
  119. return do_sys_truncate(path, length);
  120. }
  121. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  122. {
  123. struct inode *inode;
  124. struct dentry *dentry;
  125. struct fd f;
  126. int error;
  127. error = -EINVAL;
  128. if (length < 0)
  129. goto out;
  130. error = -EBADF;
  131. f = fdget(fd);
  132. if (!f.file)
  133. goto out;
  134. /* explicitly opened as large or we are on 64-bit box */
  135. if (f.file->f_flags & O_LARGEFILE)
  136. small = 0;
  137. dentry = f.file->f_path.dentry;
  138. inode = dentry->d_inode;
  139. error = -EINVAL;
  140. if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
  141. goto out_putf;
  142. error = -EINVAL;
  143. /* Cannot ftruncate over 2^31 bytes without large file support */
  144. if (small && length > MAX_NON_LFS)
  145. goto out_putf;
  146. error = -EPERM;
  147. if (IS_APPEND(inode))
  148. goto out_putf;
  149. sb_start_write(inode->i_sb);
  150. error = locks_verify_truncate(inode, f.file, length);
  151. if (!error)
  152. error = security_path_truncate(&f.file->f_path);
  153. if (!error)
  154. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
  155. sb_end_write(inode->i_sb);
  156. out_putf:
  157. fdput(f);
  158. out:
  159. return error;
  160. }
  161. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  162. {
  163. long ret = do_sys_ftruncate(fd, length, 1);
  164. /* avoid REGPARM breakage on x86: */
  165. asmlinkage_protect(2, ret, fd, length);
  166. return ret;
  167. }
  168. /* LFS versions of truncate are only needed on 32 bit machines */
  169. #if BITS_PER_LONG == 32
  170. SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
  171. {
  172. return do_sys_truncate(path, length);
  173. }
  174. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  175. asmlinkage long SyS_truncate64(long path, loff_t length)
  176. {
  177. return SYSC_truncate64((const char __user *) path, length);
  178. }
  179. SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
  180. #endif
  181. SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
  182. {
  183. long ret = do_sys_ftruncate(fd, length, 0);
  184. /* avoid REGPARM breakage on x86: */
  185. asmlinkage_protect(2, ret, fd, length);
  186. return ret;
  187. }
  188. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  189. asmlinkage long SyS_ftruncate64(long fd, loff_t length)
  190. {
  191. return SYSC_ftruncate64((unsigned int) fd, length);
  192. }
  193. SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
  194. #endif
  195. #endif /* BITS_PER_LONG == 32 */
  196. int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  197. {
  198. struct inode *inode = file->f_path.dentry->d_inode;
  199. long ret;
  200. if (offset < 0 || len <= 0)
  201. return -EINVAL;
  202. /* Return error if mode is not supported */
  203. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  204. return -EOPNOTSUPP;
  205. /* Punch hole must have keep size set */
  206. if ((mode & FALLOC_FL_PUNCH_HOLE) &&
  207. !(mode & FALLOC_FL_KEEP_SIZE))
  208. return -EOPNOTSUPP;
  209. if (!(file->f_mode & FMODE_WRITE))
  210. return -EBADF;
  211. /* It's not possible punch hole on append only file */
  212. if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode))
  213. return -EPERM;
  214. if (IS_IMMUTABLE(inode))
  215. return -EPERM;
  216. /*
  217. * Revalidate the write permissions, in case security policy has
  218. * changed since the files were opened.
  219. */
  220. ret = security_file_permission(file, MAY_WRITE);
  221. if (ret)
  222. return ret;
  223. if (S_ISFIFO(inode->i_mode))
  224. return -ESPIPE;
  225. /*
  226. * Let individual file system decide if it supports preallocation
  227. * for directories or not.
  228. */
  229. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  230. return -ENODEV;
  231. /* Check for wrap through zero too */
  232. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  233. return -EFBIG;
  234. if (!file->f_op->fallocate)
  235. return -EOPNOTSUPP;
  236. sb_start_write(inode->i_sb);
  237. ret = file->f_op->fallocate(file, mode, offset, len);
  238. sb_end_write(inode->i_sb);
  239. return ret;
  240. }
  241. SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
  242. {
  243. struct fd f = fdget(fd);
  244. int error = -EBADF;
  245. if (f.file) {
  246. error = do_fallocate(f.file, mode, offset, len);
  247. fdput(f);
  248. }
  249. return error;
  250. }
  251. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  252. asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
  253. {
  254. return SYSC_fallocate((int)fd, (int)mode, offset, len);
  255. }
  256. SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
  257. #endif
  258. /*
  259. * access() needs to use the real uid/gid, not the effective uid/gid.
  260. * We do this by temporarily clearing all FS-related capabilities and
  261. * switching the fsuid/fsgid around to the real ones.
  262. */
  263. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  264. {
  265. const struct cred *old_cred;
  266. struct cred *override_cred;
  267. struct path path;
  268. struct inode *inode;
  269. int res;
  270. unsigned int lookup_flags = LOOKUP_FOLLOW;
  271. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  272. return -EINVAL;
  273. override_cred = prepare_creds();
  274. if (!override_cred)
  275. return -ENOMEM;
  276. override_cred->fsuid = override_cred->uid;
  277. override_cred->fsgid = override_cred->gid;
  278. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  279. /* Clear the capabilities if we switch to a non-root user */
  280. kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
  281. if (!uid_eq(override_cred->uid, root_uid))
  282. cap_clear(override_cred->cap_effective);
  283. else
  284. override_cred->cap_effective =
  285. override_cred->cap_permitted;
  286. }
  287. old_cred = override_creds(override_cred);
  288. retry:
  289. res = user_path_at(dfd, filename, lookup_flags, &path);
  290. if (res)
  291. goto out;
  292. inode = path.dentry->d_inode;
  293. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  294. /*
  295. * MAY_EXEC on regular files is denied if the fs is mounted
  296. * with the "noexec" flag.
  297. */
  298. res = -EACCES;
  299. if (path.mnt->mnt_flags & MNT_NOEXEC)
  300. goto out_path_release;
  301. }
  302. res = inode_permission(inode, mode | MAY_ACCESS);
  303. /* SuS v2 requires we report a read only fs too */
  304. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  305. goto out_path_release;
  306. /*
  307. * This is a rare case where using __mnt_is_readonly()
  308. * is OK without a mnt_want/drop_write() pair. Since
  309. * no actual write to the fs is performed here, we do
  310. * not need to telegraph to that to anyone.
  311. *
  312. * By doing this, we accept that this access is
  313. * inherently racy and know that the fs may change
  314. * state before we even see this result.
  315. */
  316. if (__mnt_is_readonly(path.mnt))
  317. res = -EROFS;
  318. out_path_release:
  319. path_put(&path);
  320. if (retry_estale(res, lookup_flags)) {
  321. lookup_flags |= LOOKUP_REVAL;
  322. goto retry;
  323. }
  324. out:
  325. revert_creds(old_cred);
  326. put_cred(override_cred);
  327. return res;
  328. }
  329. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  330. {
  331. return sys_faccessat(AT_FDCWD, filename, mode);
  332. }
  333. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  334. {
  335. struct path path;
  336. int error;
  337. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  338. retry:
  339. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  340. if (error)
  341. goto out;
  342. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  343. if (error)
  344. goto dput_and_out;
  345. set_fs_pwd(current->fs, &path);
  346. dput_and_out:
  347. path_put(&path);
  348. if (retry_estale(error, lookup_flags)) {
  349. lookup_flags |= LOOKUP_REVAL;
  350. goto retry;
  351. }
  352. out:
  353. return error;
  354. }
  355. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  356. {
  357. struct fd f = fdget_raw(fd);
  358. struct inode *inode;
  359. int error = -EBADF;
  360. error = -EBADF;
  361. if (!f.file)
  362. goto out;
  363. inode = f.file->f_path.dentry->d_inode;
  364. error = -ENOTDIR;
  365. if (!S_ISDIR(inode->i_mode))
  366. goto out_putf;
  367. error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
  368. if (!error)
  369. set_fs_pwd(current->fs, &f.file->f_path);
  370. out_putf:
  371. fdput(f);
  372. out:
  373. return error;
  374. }
  375. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  376. {
  377. struct path path;
  378. int error;
  379. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  380. retry:
  381. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  382. if (error)
  383. goto out;
  384. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  385. if (error)
  386. goto dput_and_out;
  387. error = -EPERM;
  388. if (!nsown_capable(CAP_SYS_CHROOT))
  389. goto dput_and_out;
  390. error = security_path_chroot(&path);
  391. if (error)
  392. goto dput_and_out;
  393. set_fs_root(current->fs, &path);
  394. error = 0;
  395. dput_and_out:
  396. path_put(&path);
  397. if (retry_estale(error, lookup_flags)) {
  398. lookup_flags |= LOOKUP_REVAL;
  399. goto retry;
  400. }
  401. out:
  402. return error;
  403. }
  404. static int chmod_common(struct path *path, umode_t mode)
  405. {
  406. struct inode *inode = path->dentry->d_inode;
  407. struct iattr newattrs;
  408. int error;
  409. error = mnt_want_write(path->mnt);
  410. if (error)
  411. return error;
  412. mutex_lock(&inode->i_mutex);
  413. error = security_path_chmod(path, mode);
  414. if (error)
  415. goto out_unlock;
  416. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  417. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  418. error = notify_change(path->dentry, &newattrs);
  419. out_unlock:
  420. mutex_unlock(&inode->i_mutex);
  421. mnt_drop_write(path->mnt);
  422. return error;
  423. }
  424. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  425. {
  426. struct file * file;
  427. int err = -EBADF;
  428. file = fget(fd);
  429. if (file) {
  430. audit_inode(NULL, file->f_path.dentry, 0);
  431. err = chmod_common(&file->f_path, mode);
  432. fput(file);
  433. }
  434. return err;
  435. }
  436. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
  437. {
  438. struct path path;
  439. int error;
  440. unsigned int lookup_flags = LOOKUP_FOLLOW;
  441. retry:
  442. error = user_path_at(dfd, filename, lookup_flags, &path);
  443. if (!error) {
  444. error = chmod_common(&path, mode);
  445. path_put(&path);
  446. if (retry_estale(error, lookup_flags)) {
  447. lookup_flags |= LOOKUP_REVAL;
  448. goto retry;
  449. }
  450. }
  451. return error;
  452. }
  453. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  454. {
  455. return sys_fchmodat(AT_FDCWD, filename, mode);
  456. }
  457. static int chown_common(struct path *path, uid_t user, gid_t group)
  458. {
  459. struct inode *inode = path->dentry->d_inode;
  460. int error;
  461. struct iattr newattrs;
  462. kuid_t uid;
  463. kgid_t gid;
  464. uid = make_kuid(current_user_ns(), user);
  465. gid = make_kgid(current_user_ns(), group);
  466. newattrs.ia_valid = ATTR_CTIME;
  467. if (user != (uid_t) -1) {
  468. if (!uid_valid(uid))
  469. return -EINVAL;
  470. newattrs.ia_valid |= ATTR_UID;
  471. newattrs.ia_uid = uid;
  472. }
  473. if (group != (gid_t) -1) {
  474. if (!gid_valid(gid))
  475. return -EINVAL;
  476. newattrs.ia_valid |= ATTR_GID;
  477. newattrs.ia_gid = gid;
  478. }
  479. if (!S_ISDIR(inode->i_mode))
  480. newattrs.ia_valid |=
  481. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  482. mutex_lock(&inode->i_mutex);
  483. error = security_path_chown(path, uid, gid);
  484. if (!error)
  485. error = notify_change(path->dentry, &newattrs);
  486. mutex_unlock(&inode->i_mutex);
  487. return error;
  488. }
  489. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  490. gid_t, group, int, flag)
  491. {
  492. struct path path;
  493. int error = -EINVAL;
  494. int lookup_flags;
  495. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  496. goto out;
  497. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  498. if (flag & AT_EMPTY_PATH)
  499. lookup_flags |= LOOKUP_EMPTY;
  500. retry:
  501. error = user_path_at(dfd, filename, lookup_flags, &path);
  502. if (error)
  503. goto out;
  504. error = mnt_want_write(path.mnt);
  505. if (error)
  506. goto out_release;
  507. error = chown_common(&path, user, group);
  508. mnt_drop_write(path.mnt);
  509. out_release:
  510. path_put(&path);
  511. if (retry_estale(error, lookup_flags)) {
  512. lookup_flags |= LOOKUP_REVAL;
  513. goto retry;
  514. }
  515. out:
  516. return error;
  517. }
  518. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  519. {
  520. return sys_fchownat(AT_FDCWD, filename, user, group, 0);
  521. }
  522. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  523. {
  524. return sys_fchownat(AT_FDCWD, filename, user, group,
  525. AT_SYMLINK_NOFOLLOW);
  526. }
  527. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  528. {
  529. struct fd f = fdget(fd);
  530. int error = -EBADF;
  531. if (!f.file)
  532. goto out;
  533. error = mnt_want_write_file(f.file);
  534. if (error)
  535. goto out_fput;
  536. audit_inode(NULL, f.file->f_path.dentry, 0);
  537. error = chown_common(&f.file->f_path, user, group);
  538. mnt_drop_write_file(f.file);
  539. out_fput:
  540. fdput(f);
  541. out:
  542. return error;
  543. }
  544. /*
  545. * You have to be very careful that these write
  546. * counts get cleaned up in error cases and
  547. * upon __fput(). This should probably never
  548. * be called outside of __dentry_open().
  549. */
  550. static inline int __get_file_write_access(struct inode *inode,
  551. struct vfsmount *mnt)
  552. {
  553. int error = get_write_access(inode);
  554. if (error)
  555. return error;
  556. error = __mnt_want_write(mnt);
  557. if (error)
  558. put_write_access(inode);
  559. return error;
  560. }
  561. int open_check_o_direct(struct file *f)
  562. {
  563. /* NB: we're sure to have correct a_ops only after f_op->open */
  564. if (f->f_flags & O_DIRECT) {
  565. if (!f->f_mapping->a_ops ||
  566. ((!f->f_mapping->a_ops->direct_IO) &&
  567. (!f->f_mapping->a_ops->get_xip_mem))) {
  568. return -EINVAL;
  569. }
  570. }
  571. return 0;
  572. }
  573. static int do_dentry_open(struct file *f,
  574. int (*open)(struct inode *, struct file *),
  575. const struct cred *cred)
  576. {
  577. static const struct file_operations empty_fops = {};
  578. struct inode *inode;
  579. int error;
  580. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  581. FMODE_PREAD | FMODE_PWRITE;
  582. if (unlikely(f->f_flags & O_PATH))
  583. f->f_mode = FMODE_PATH;
  584. path_get(&f->f_path);
  585. inode = f->f_path.dentry->d_inode;
  586. if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
  587. error = __get_file_write_access(inode, f->f_path.mnt);
  588. if (error)
  589. goto cleanup_file;
  590. file_take_write(f);
  591. }
  592. f->f_mapping = inode->i_mapping;
  593. f->f_pos = 0;
  594. file_sb_list_add(f, inode->i_sb);
  595. if (unlikely(f->f_mode & FMODE_PATH)) {
  596. f->f_op = &empty_fops;
  597. return 0;
  598. }
  599. f->f_op = fops_get(inode->i_fop);
  600. error = security_file_open(f, cred);
  601. if (error)
  602. goto cleanup_all;
  603. error = break_lease(inode, f->f_flags);
  604. if (error)
  605. goto cleanup_all;
  606. if (!open && f->f_op)
  607. open = f->f_op->open;
  608. if (open) {
  609. error = open(inode, f);
  610. if (error)
  611. goto cleanup_all;
  612. }
  613. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  614. i_readcount_inc(inode);
  615. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  616. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  617. return 0;
  618. cleanup_all:
  619. fops_put(f->f_op);
  620. file_sb_list_del(f);
  621. if (f->f_mode & FMODE_WRITE) {
  622. if (!special_file(inode->i_mode)) {
  623. /*
  624. * We don't consider this a real
  625. * mnt_want/drop_write() pair
  626. * because it all happenend right
  627. * here, so just reset the state.
  628. */
  629. put_write_access(inode);
  630. file_reset_write(f);
  631. __mnt_drop_write(f->f_path.mnt);
  632. }
  633. }
  634. cleanup_file:
  635. path_put(&f->f_path);
  636. f->f_path.mnt = NULL;
  637. f->f_path.dentry = NULL;
  638. return error;
  639. }
  640. /**
  641. * finish_open - finish opening a file
  642. * @od: opaque open data
  643. * @dentry: pointer to dentry
  644. * @open: open callback
  645. *
  646. * This can be used to finish opening a file passed to i_op->atomic_open().
  647. *
  648. * If the open callback is set to NULL, then the standard f_op->open()
  649. * filesystem callback is substituted.
  650. */
  651. int finish_open(struct file *file, struct dentry *dentry,
  652. int (*open)(struct inode *, struct file *),
  653. int *opened)
  654. {
  655. int error;
  656. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  657. file->f_path.dentry = dentry;
  658. error = do_dentry_open(file, open, current_cred());
  659. if (!error)
  660. *opened |= FILE_OPENED;
  661. return error;
  662. }
  663. EXPORT_SYMBOL(finish_open);
  664. /**
  665. * finish_no_open - finish ->atomic_open() without opening the file
  666. *
  667. * @od: opaque open data
  668. * @dentry: dentry or NULL (as returned from ->lookup())
  669. *
  670. * This can be used to set the result of a successful lookup in ->atomic_open().
  671. * The filesystem's atomic_open() method shall return NULL after calling this.
  672. */
  673. int finish_no_open(struct file *file, struct dentry *dentry)
  674. {
  675. file->f_path.dentry = dentry;
  676. return 1;
  677. }
  678. EXPORT_SYMBOL(finish_no_open);
  679. struct file *dentry_open(const struct path *path, int flags,
  680. const struct cred *cred)
  681. {
  682. int error;
  683. struct file *f;
  684. validate_creds(cred);
  685. /* We must always pass in a valid mount pointer. */
  686. BUG_ON(!path->mnt);
  687. error = -ENFILE;
  688. f = get_empty_filp();
  689. if (f == NULL)
  690. return ERR_PTR(error);
  691. f->f_flags = flags;
  692. f->f_path = *path;
  693. error = do_dentry_open(f, NULL, cred);
  694. if (!error) {
  695. error = open_check_o_direct(f);
  696. if (error) {
  697. fput(f);
  698. f = ERR_PTR(error);
  699. }
  700. } else {
  701. put_filp(f);
  702. f = ERR_PTR(error);
  703. }
  704. return f;
  705. }
  706. EXPORT_SYMBOL(dentry_open);
  707. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  708. {
  709. int lookup_flags = 0;
  710. int acc_mode;
  711. if (flags & O_CREAT)
  712. op->mode = (mode & S_IALLUGO) | S_IFREG;
  713. else
  714. op->mode = 0;
  715. /* Must never be set by userspace */
  716. flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
  717. /*
  718. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  719. * check for O_DSYNC if the need any syncing at all we enforce it's
  720. * always set instead of having to deal with possibly weird behaviour
  721. * for malicious applications setting only __O_SYNC.
  722. */
  723. if (flags & __O_SYNC)
  724. flags |= O_DSYNC;
  725. /*
  726. * If we have O_PATH in the open flag. Then we
  727. * cannot have anything other than the below set of flags
  728. */
  729. if (flags & O_PATH) {
  730. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  731. acc_mode = 0;
  732. } else {
  733. acc_mode = MAY_OPEN | ACC_MODE(flags);
  734. }
  735. op->open_flag = flags;
  736. /* O_TRUNC implies we need access checks for write permissions */
  737. if (flags & O_TRUNC)
  738. acc_mode |= MAY_WRITE;
  739. /* Allow the LSM permission hook to distinguish append
  740. access from general write access. */
  741. if (flags & O_APPEND)
  742. acc_mode |= MAY_APPEND;
  743. op->acc_mode = acc_mode;
  744. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  745. if (flags & O_CREAT) {
  746. op->intent |= LOOKUP_CREATE;
  747. if (flags & O_EXCL)
  748. op->intent |= LOOKUP_EXCL;
  749. }
  750. if (flags & O_DIRECTORY)
  751. lookup_flags |= LOOKUP_DIRECTORY;
  752. if (!(flags & O_NOFOLLOW))
  753. lookup_flags |= LOOKUP_FOLLOW;
  754. return lookup_flags;
  755. }
  756. /**
  757. * file_open_name - open file and return file pointer
  758. *
  759. * @name: struct filename containing path to open
  760. * @flags: open flags as per the open(2) second argument
  761. * @mode: mode for the new file if O_CREAT is set, else ignored
  762. *
  763. * This is the helper to open a file from kernelspace if you really
  764. * have to. But in generally you should not do this, so please move
  765. * along, nothing to see here..
  766. */
  767. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  768. {
  769. struct open_flags op;
  770. int lookup = build_open_flags(flags, mode, &op);
  771. return do_filp_open(AT_FDCWD, name, &op, lookup);
  772. }
  773. /**
  774. * filp_open - open file and return file pointer
  775. *
  776. * @filename: path to open
  777. * @flags: open flags as per the open(2) second argument
  778. * @mode: mode for the new file if O_CREAT is set, else ignored
  779. *
  780. * This is the helper to open a file from kernelspace if you really
  781. * have to. But in generally you should not do this, so please move
  782. * along, nothing to see here..
  783. */
  784. struct file *filp_open(const char *filename, int flags, umode_t mode)
  785. {
  786. struct filename name = {.name = filename};
  787. return file_open_name(&name, flags, mode);
  788. }
  789. EXPORT_SYMBOL(filp_open);
  790. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  791. const char *filename, int flags)
  792. {
  793. struct open_flags op;
  794. int lookup = build_open_flags(flags, 0, &op);
  795. if (flags & O_CREAT)
  796. return ERR_PTR(-EINVAL);
  797. if (!filename && (flags & O_DIRECTORY))
  798. if (!dentry->d_inode->i_op->lookup)
  799. return ERR_PTR(-ENOTDIR);
  800. return do_file_open_root(dentry, mnt, filename, &op, lookup);
  801. }
  802. EXPORT_SYMBOL(file_open_root);
  803. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  804. {
  805. struct open_flags op;
  806. int lookup = build_open_flags(flags, mode, &op);
  807. struct filename *tmp = getname(filename);
  808. int fd = PTR_ERR(tmp);
  809. if (!IS_ERR(tmp)) {
  810. fd = get_unused_fd_flags(flags);
  811. if (fd >= 0) {
  812. struct file *f = do_filp_open(dfd, tmp, &op, lookup);
  813. if (IS_ERR(f)) {
  814. put_unused_fd(fd);
  815. fd = PTR_ERR(f);
  816. } else {
  817. fsnotify_open(f);
  818. fd_install(fd, f);
  819. }
  820. }
  821. putname(tmp);
  822. }
  823. return fd;
  824. }
  825. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  826. {
  827. long ret;
  828. if (force_o_largefile())
  829. flags |= O_LARGEFILE;
  830. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  831. /* avoid REGPARM breakage on x86: */
  832. asmlinkage_protect(3, ret, filename, flags, mode);
  833. return ret;
  834. }
  835. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  836. umode_t, mode)
  837. {
  838. long ret;
  839. if (force_o_largefile())
  840. flags |= O_LARGEFILE;
  841. ret = do_sys_open(dfd, filename, flags, mode);
  842. /* avoid REGPARM breakage on x86: */
  843. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  844. return ret;
  845. }
  846. #ifndef __alpha__
  847. /*
  848. * For backward compatibility? Maybe this should be moved
  849. * into arch/i386 instead?
  850. */
  851. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  852. {
  853. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  854. }
  855. #endif
  856. /*
  857. * "id" is the POSIX thread ID. We use the
  858. * files pointer for this..
  859. */
  860. int filp_close(struct file *filp, fl_owner_t id)
  861. {
  862. int retval = 0;
  863. if (!file_count(filp)) {
  864. printk(KERN_ERR "VFS: Close: file count is 0\n");
  865. return 0;
  866. }
  867. if (filp->f_op && filp->f_op->flush)
  868. retval = filp->f_op->flush(filp, id);
  869. if (likely(!(filp->f_mode & FMODE_PATH))) {
  870. dnotify_flush(filp, id);
  871. locks_remove_posix(filp, id);
  872. }
  873. fput(filp);
  874. return retval;
  875. }
  876. EXPORT_SYMBOL(filp_close);
  877. /*
  878. * Careful here! We test whether the file pointer is NULL before
  879. * releasing the fd. This ensures that one clone task can't release
  880. * an fd while another clone is opening it.
  881. */
  882. SYSCALL_DEFINE1(close, unsigned int, fd)
  883. {
  884. int retval = __close_fd(current->files, fd);
  885. /* can't restart close syscall because file table entry was cleared */
  886. if (unlikely(retval == -ERESTARTSYS ||
  887. retval == -ERESTARTNOINTR ||
  888. retval == -ERESTARTNOHAND ||
  889. retval == -ERESTART_RESTARTBLOCK))
  890. retval = -EINTR;
  891. return retval;
  892. }
  893. EXPORT_SYMBOL(sys_close);
  894. /*
  895. * This routine simulates a hangup on the tty, to arrange that users
  896. * are given clean terminals at login time.
  897. */
  898. SYSCALL_DEFINE0(vhangup)
  899. {
  900. if (capable(CAP_SYS_TTY_CONFIG)) {
  901. tty_vhangup_self();
  902. return 0;
  903. }
  904. return -EPERM;
  905. }
  906. /*
  907. * Called when an inode is about to be open.
  908. * We use this to disallow opening large files on 32bit systems if
  909. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  910. * on this flag in sys_open.
  911. */
  912. int generic_file_open(struct inode * inode, struct file * filp)
  913. {
  914. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  915. return -EOVERFLOW;
  916. return 0;
  917. }
  918. EXPORT_SYMBOL(generic_file_open);
  919. /*
  920. * This is used by subsystems that don't want seekable
  921. * file descriptors. The function is not supposed to ever fail, the only
  922. * reason it returns an 'int' and not 'void' is so that it can be plugged
  923. * directly into file_operations structure.
  924. */
  925. int nonseekable_open(struct inode *inode, struct file *filp)
  926. {
  927. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  928. return 0;
  929. }
  930. EXPORT_SYMBOL(nonseekable_open);