PageRenderTime 534ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/open.c

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