file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * Copyright (C) 2017 Red Hat, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. */
  8. #include <linux/cred.h>
  9. #include <linux/file.h>
  10. #include <linux/mount.h>
  11. #include <linux/xattr.h>
  12. #include <linux/uio.h>
  13. #include "overlayfs.h"
  14. static char ovl_whatisit(struct inode *inode, struct inode *realinode)
  15. {
  16. if (realinode != ovl_inode_upper(inode))
  17. return 'l';
  18. if (ovl_has_upperdata(inode))
  19. return 'u';
  20. else
  21. return 'm';
  22. }
  23. static struct file *ovl_open_realfile(const struct file *file,
  24. struct inode *realinode)
  25. {
  26. struct inode *inode = file_inode(file);
  27. struct file *realfile;
  28. const struct cred *old_cred;
  29. old_cred = ovl_override_creds(inode->i_sb);
  30. realfile = open_with_fake_path(&file->f_path, file->f_flags | O_NOATIME,
  31. realinode, current_cred());
  32. revert_creds(old_cred);
  33. pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
  34. file, file, ovl_whatisit(inode, realinode), file->f_flags,
  35. realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
  36. return realfile;
  37. }
  38. #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
  39. static int ovl_change_flags(struct file *file, unsigned int flags)
  40. {
  41. struct inode *inode = file_inode(file);
  42. int err;
  43. /* No atime modificaton on underlying */
  44. flags |= O_NOATIME;
  45. /* If some flag changed that cannot be changed then something's amiss */
  46. if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
  47. return -EIO;
  48. flags &= OVL_SETFL_MASK;
  49. if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
  50. return -EPERM;
  51. if (flags & O_DIRECT) {
  52. if (!file->f_mapping->a_ops ||
  53. !file->f_mapping->a_ops->direct_IO)
  54. return -EINVAL;
  55. }
  56. if (file->f_op->check_flags) {
  57. err = file->f_op->check_flags(flags);
  58. if (err)
  59. return err;
  60. }
  61. spin_lock(&file->f_lock);
  62. file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
  63. spin_unlock(&file->f_lock);
  64. return 0;
  65. }
  66. static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
  67. bool allow_meta)
  68. {
  69. struct inode *inode = file_inode(file);
  70. struct inode *realinode;
  71. real->flags = 0;
  72. real->file = file->private_data;
  73. if (allow_meta)
  74. realinode = ovl_inode_real(inode);
  75. else
  76. realinode = ovl_inode_realdata(inode);
  77. /* Has it been copied up since we'd opened it? */
  78. if (unlikely(file_inode(real->file) != realinode)) {
  79. real->flags = FDPUT_FPUT;
  80. real->file = ovl_open_realfile(file, realinode);
  81. return PTR_ERR_OR_ZERO(real->file);
  82. }
  83. /* Did the flags change since open? */
  84. if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME))
  85. return ovl_change_flags(real->file, file->f_flags);
  86. return 0;
  87. }
  88. static int ovl_real_fdget(const struct file *file, struct fd *real)
  89. {
  90. return ovl_real_fdget_meta(file, real, false);
  91. }
  92. static int ovl_open(struct inode *inode, struct file *file)
  93. {
  94. struct dentry *dentry = file_dentry(file);
  95. struct file *realfile;
  96. int err;
  97. err = ovl_open_maybe_copy_up(dentry, file->f_flags);
  98. if (err)
  99. return err;
  100. /* No longer need these flags, so don't pass them on to underlying fs */
  101. file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  102. realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
  103. if (IS_ERR(realfile))
  104. return PTR_ERR(realfile);
  105. file->private_data = realfile;
  106. return 0;
  107. }
  108. static int ovl_release(struct inode *inode, struct file *file)
  109. {
  110. fput(file->private_data);
  111. return 0;
  112. }
  113. static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
  114. {
  115. struct inode *realinode = ovl_inode_real(file_inode(file));
  116. return generic_file_llseek_size(file, offset, whence,
  117. realinode->i_sb->s_maxbytes,
  118. i_size_read(realinode));
  119. }
  120. static void ovl_file_accessed(struct file *file)
  121. {
  122. struct inode *inode, *upperinode;
  123. if (file->f_flags & O_NOATIME)
  124. return;
  125. inode = file_inode(file);
  126. upperinode = ovl_inode_upper(inode);
  127. if (!upperinode)
  128. return;
  129. if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
  130. !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
  131. inode->i_mtime = upperinode->i_mtime;
  132. inode->i_ctime = upperinode->i_ctime;
  133. }
  134. touch_atime(&file->f_path);
  135. }
  136. static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb)
  137. {
  138. int ifl = iocb->ki_flags;
  139. rwf_t flags = 0;
  140. if (ifl & IOCB_NOWAIT)
  141. flags |= RWF_NOWAIT;
  142. if (ifl & IOCB_HIPRI)
  143. flags |= RWF_HIPRI;
  144. if (ifl & IOCB_DSYNC)
  145. flags |= RWF_DSYNC;
  146. if (ifl & IOCB_SYNC)
  147. flags |= RWF_SYNC;
  148. return flags;
  149. }
  150. static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  151. {
  152. struct file *file = iocb->ki_filp;
  153. struct fd real;
  154. const struct cred *old_cred;
  155. ssize_t ret;
  156. if (!iov_iter_count(iter))
  157. return 0;
  158. ret = ovl_real_fdget(file, &real);
  159. if (ret)
  160. return ret;
  161. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  162. ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
  163. ovl_iocb_to_rwf(iocb));
  164. revert_creds(old_cred);
  165. ovl_file_accessed(file);
  166. fdput(real);
  167. return ret;
  168. }
  169. static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
  170. {
  171. struct file *file = iocb->ki_filp;
  172. struct inode *inode = file_inode(file);
  173. struct fd real;
  174. const struct cred *old_cred;
  175. ssize_t ret;
  176. if (!iov_iter_count(iter))
  177. return 0;
  178. inode_lock(inode);
  179. /* Update mode */
  180. ovl_copyattr(ovl_inode_real(inode), inode);
  181. ret = file_remove_privs(file);
  182. if (ret)
  183. goto out_unlock;
  184. ret = ovl_real_fdget(file, &real);
  185. if (ret)
  186. goto out_unlock;
  187. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  188. file_start_write(real.file);
  189. ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
  190. ovl_iocb_to_rwf(iocb));
  191. file_end_write(real.file);
  192. revert_creds(old_cred);
  193. /* Update size */
  194. ovl_copyattr(ovl_inode_real(inode), inode);
  195. fdput(real);
  196. out_unlock:
  197. inode_unlock(inode);
  198. return ret;
  199. }
  200. static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  201. {
  202. struct fd real;
  203. const struct cred *old_cred;
  204. int ret;
  205. ret = ovl_real_fdget_meta(file, &real, !datasync);
  206. if (ret)
  207. return ret;
  208. /* Don't sync lower file for fear of receiving EROFS error */
  209. if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
  210. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  211. ret = vfs_fsync_range(real.file, start, end, datasync);
  212. revert_creds(old_cred);
  213. }
  214. fdput(real);
  215. return ret;
  216. }
  217. static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
  218. {
  219. struct file *realfile = file->private_data;
  220. const struct cred *old_cred;
  221. int ret;
  222. if (!realfile->f_op->mmap)
  223. return -ENODEV;
  224. if (WARN_ON(file != vma->vm_file))
  225. return -EIO;
  226. vma->vm_file = get_file(realfile);
  227. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  228. ret = call_mmap(vma->vm_file, vma);
  229. revert_creds(old_cred);
  230. if (ret) {
  231. /* Drop reference count from new vm_file value */
  232. fput(realfile);
  233. } else {
  234. /* Drop reference count from previous vm_file value */
  235. fput(file);
  236. }
  237. ovl_file_accessed(file);
  238. return ret;
  239. }
  240. static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  241. {
  242. struct inode *inode = file_inode(file);
  243. struct fd real;
  244. const struct cred *old_cred;
  245. int ret;
  246. ret = ovl_real_fdget(file, &real);
  247. if (ret)
  248. return ret;
  249. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  250. ret = vfs_fallocate(real.file, mode, offset, len);
  251. revert_creds(old_cred);
  252. /* Update size */
  253. ovl_copyattr(ovl_inode_real(inode), inode);
  254. fdput(real);
  255. return ret;
  256. }
  257. static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  258. {
  259. struct fd real;
  260. const struct cred *old_cred;
  261. int ret;
  262. ret = ovl_real_fdget(file, &real);
  263. if (ret)
  264. return ret;
  265. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  266. ret = vfs_fadvise(real.file, offset, len, advice);
  267. revert_creds(old_cred);
  268. fdput(real);
  269. return ret;
  270. }
  271. static long ovl_real_ioctl(struct file *file, unsigned int cmd,
  272. unsigned long arg)
  273. {
  274. struct fd real;
  275. const struct cred *old_cred;
  276. long ret;
  277. ret = ovl_real_fdget(file, &real);
  278. if (ret)
  279. return ret;
  280. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  281. ret = vfs_ioctl(real.file, cmd, arg);
  282. revert_creds(old_cred);
  283. fdput(real);
  284. return ret;
  285. }
  286. static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  287. {
  288. long ret;
  289. struct inode *inode = file_inode(file);
  290. switch (cmd) {
  291. case FS_IOC_GETFLAGS:
  292. ret = ovl_real_ioctl(file, cmd, arg);
  293. break;
  294. case FS_IOC_SETFLAGS:
  295. if (!inode_owner_or_capable(inode))
  296. return -EACCES;
  297. ret = mnt_want_write_file(file);
  298. if (ret)
  299. return ret;
  300. ret = ovl_copy_up_with_data(file_dentry(file));
  301. if (!ret) {
  302. ret = ovl_real_ioctl(file, cmd, arg);
  303. inode_lock(inode);
  304. ovl_copyflags(ovl_inode_real(inode), inode);
  305. inode_unlock(inode);
  306. }
  307. mnt_drop_write_file(file);
  308. break;
  309. default:
  310. ret = -ENOTTY;
  311. }
  312. return ret;
  313. }
  314. static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
  315. unsigned long arg)
  316. {
  317. switch (cmd) {
  318. case FS_IOC32_GETFLAGS:
  319. cmd = FS_IOC_GETFLAGS;
  320. break;
  321. case FS_IOC32_SETFLAGS:
  322. cmd = FS_IOC_SETFLAGS;
  323. break;
  324. default:
  325. return -ENOIOCTLCMD;
  326. }
  327. return ovl_ioctl(file, cmd, arg);
  328. }
  329. enum ovl_copyop {
  330. OVL_COPY,
  331. OVL_CLONE,
  332. OVL_DEDUPE,
  333. };
  334. static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
  335. struct file *file_out, loff_t pos_out,
  336. loff_t len, unsigned int flags, enum ovl_copyop op)
  337. {
  338. struct inode *inode_out = file_inode(file_out);
  339. struct fd real_in, real_out;
  340. const struct cred *old_cred;
  341. loff_t ret;
  342. ret = ovl_real_fdget(file_out, &real_out);
  343. if (ret)
  344. return ret;
  345. ret = ovl_real_fdget(file_in, &real_in);
  346. if (ret) {
  347. fdput(real_out);
  348. return ret;
  349. }
  350. old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
  351. switch (op) {
  352. case OVL_COPY:
  353. ret = vfs_copy_file_range(real_in.file, pos_in,
  354. real_out.file, pos_out, len, flags);
  355. break;
  356. case OVL_CLONE:
  357. ret = vfs_clone_file_range(real_in.file, pos_in,
  358. real_out.file, pos_out, len, flags);
  359. break;
  360. case OVL_DEDUPE:
  361. ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
  362. real_out.file, pos_out, len,
  363. flags);
  364. break;
  365. }
  366. revert_creds(old_cred);
  367. /* Update size */
  368. ovl_copyattr(ovl_inode_real(inode_out), inode_out);
  369. fdput(real_in);
  370. fdput(real_out);
  371. return ret;
  372. }
  373. static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
  374. struct file *file_out, loff_t pos_out,
  375. size_t len, unsigned int flags)
  376. {
  377. return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
  378. OVL_COPY);
  379. }
  380. static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
  381. struct file *file_out, loff_t pos_out,
  382. loff_t len, unsigned int remap_flags)
  383. {
  384. enum ovl_copyop op;
  385. if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
  386. return -EINVAL;
  387. if (remap_flags & REMAP_FILE_DEDUP)
  388. op = OVL_DEDUPE;
  389. else
  390. op = OVL_CLONE;
  391. /*
  392. * Don't copy up because of a dedupe request, this wouldn't make sense
  393. * most of the time (data would be duplicated instead of deduplicated).
  394. */
  395. if (op == OVL_DEDUPE &&
  396. (!ovl_inode_upper(file_inode(file_in)) ||
  397. !ovl_inode_upper(file_inode(file_out))))
  398. return -EPERM;
  399. return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
  400. remap_flags, op);
  401. }
  402. const struct file_operations ovl_file_operations = {
  403. .open = ovl_open,
  404. .release = ovl_release,
  405. .llseek = ovl_llseek,
  406. .read_iter = ovl_read_iter,
  407. .write_iter = ovl_write_iter,
  408. .fsync = ovl_fsync,
  409. .mmap = ovl_mmap,
  410. .fallocate = ovl_fallocate,
  411. .fadvise = ovl_fadvise,
  412. .unlocked_ioctl = ovl_ioctl,
  413. .compat_ioctl = ovl_compat_ioctl,
  414. .copy_file_range = ovl_copy_file_range,
  415. .remap_file_range = ovl_remap_file_range,
  416. };