file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Red Hat, Inc.
  4. */
  5. #include <linux/cred.h>
  6. #include <linux/file.h>
  7. #include <linux/mount.h>
  8. #include <linux/xattr.h>
  9. #include <linux/uio.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/splice.h>
  12. #include <linux/security.h>
  13. #include <linux/mm.h>
  14. #include <linux/fs.h>
  15. #include "overlayfs.h"
  16. #define OVL_IOCB_MASK (IOCB_DSYNC | IOCB_HIPRI | IOCB_NOWAIT | IOCB_SYNC)
  17. struct ovl_aio_req {
  18. struct kiocb iocb;
  19. refcount_t ref;
  20. struct kiocb *orig_iocb;
  21. struct fd fd;
  22. };
  23. static struct kmem_cache *ovl_aio_request_cachep;
  24. static char ovl_whatisit(struct inode *inode, struct inode *realinode)
  25. {
  26. if (realinode != ovl_inode_upper(inode))
  27. return 'l';
  28. if (ovl_has_upperdata(inode))
  29. return 'u';
  30. else
  31. return 'm';
  32. }
  33. /* No atime modificaton nor notify on underlying */
  34. #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
  35. static struct file *ovl_open_realfile(const struct file *file,
  36. struct inode *realinode)
  37. {
  38. struct inode *inode = file_inode(file);
  39. struct file *realfile;
  40. const struct cred *old_cred;
  41. int flags = file->f_flags | OVL_OPEN_FLAGS;
  42. int acc_mode = ACC_MODE(flags);
  43. int err;
  44. if (flags & O_APPEND)
  45. acc_mode |= MAY_APPEND;
  46. old_cred = ovl_override_creds(inode->i_sb);
  47. err = inode_permission(realinode, MAY_OPEN | acc_mode);
  48. if (err) {
  49. realfile = ERR_PTR(err);
  50. } else if (old_cred && !inode_owner_or_capable(realinode)) {
  51. realfile = ERR_PTR(-EPERM);
  52. } else {
  53. realfile = open_with_fake_path(&file->f_path, flags, realinode,
  54. current_cred());
  55. }
  56. ovl_revert_creds(inode->i_sb, old_cred);
  57. pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
  58. file, file, ovl_whatisit(inode, realinode), file->f_flags,
  59. realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
  60. return realfile;
  61. }
  62. #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
  63. static int ovl_change_flags(struct file *file, unsigned int flags)
  64. {
  65. struct inode *inode = file_inode(file);
  66. int err;
  67. flags |= OVL_OPEN_FLAGS;
  68. /* If some flag changed that cannot be changed then something's amiss */
  69. if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
  70. return -EIO;
  71. flags &= OVL_SETFL_MASK;
  72. if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
  73. return -EPERM;
  74. if (flags & O_DIRECT) {
  75. if (!file->f_mapping->a_ops ||
  76. !file->f_mapping->a_ops->direct_IO)
  77. return -EINVAL;
  78. }
  79. if (file->f_op->check_flags) {
  80. err = file->f_op->check_flags(flags);
  81. if (err)
  82. return err;
  83. }
  84. spin_lock(&file->f_lock);
  85. file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
  86. spin_unlock(&file->f_lock);
  87. return 0;
  88. }
  89. static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
  90. bool allow_meta)
  91. {
  92. struct inode *inode = file_inode(file);
  93. struct inode *realinode;
  94. real->flags = 0;
  95. real->file = file->private_data;
  96. if (allow_meta)
  97. realinode = ovl_inode_real(inode);
  98. else
  99. realinode = ovl_inode_realdata(inode);
  100. /* Has it been copied up since we'd opened it? */
  101. if (unlikely(file_inode(real->file) != realinode)) {
  102. real->flags = FDPUT_FPUT;
  103. real->file = ovl_open_realfile(file, realinode);
  104. return PTR_ERR_OR_ZERO(real->file);
  105. }
  106. /* Did the flags change since open? */
  107. if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
  108. return ovl_change_flags(real->file, file->f_flags);
  109. return 0;
  110. }
  111. static int ovl_real_fdget(const struct file *file, struct fd *real)
  112. {
  113. if (d_is_dir(file_dentry(file))) {
  114. real->flags = 0;
  115. real->file = ovl_dir_real_file(file, false);
  116. return PTR_ERR_OR_ZERO(real->file);
  117. }
  118. return ovl_real_fdget_meta(file, real, false);
  119. }
  120. static int ovl_open(struct inode *inode, struct file *file)
  121. {
  122. struct file *realfile;
  123. int err;
  124. err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
  125. if (err)
  126. return err;
  127. /* No longer need these flags, so don't pass them on to underlying fs */
  128. file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  129. realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
  130. if (IS_ERR(realfile))
  131. return PTR_ERR(realfile);
  132. file->private_data = realfile;
  133. return 0;
  134. }
  135. static int ovl_release(struct inode *inode, struct file *file)
  136. {
  137. fput(file->private_data);
  138. return 0;
  139. }
  140. static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
  141. {
  142. struct inode *inode = file_inode(file);
  143. struct fd real;
  144. const struct cred *old_cred;
  145. loff_t ret;
  146. /*
  147. * The two special cases below do not need to involve real fs,
  148. * so we can optimizing concurrent callers.
  149. */
  150. if (offset == 0) {
  151. if (whence == SEEK_CUR)
  152. return file->f_pos;
  153. if (whence == SEEK_SET)
  154. return vfs_setpos(file, 0, 0);
  155. }
  156. ret = ovl_real_fdget(file, &real);
  157. if (ret)
  158. return ret;
  159. /*
  160. * Overlay file f_pos is the master copy that is preserved
  161. * through copy up and modified on read/write, but only real
  162. * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
  163. * limitations that are more strict than ->s_maxbytes for specific
  164. * files, so we use the real file to perform seeks.
  165. */
  166. ovl_inode_lock(inode);
  167. real.file->f_pos = file->f_pos;
  168. old_cred = ovl_override_creds(inode->i_sb);
  169. ret = vfs_llseek(real.file, offset, whence);
  170. ovl_revert_creds(inode->i_sb, old_cred);
  171. file->f_pos = real.file->f_pos;
  172. ovl_inode_unlock(inode);
  173. fdput(real);
  174. return ret;
  175. }
  176. static void ovl_file_accessed(struct file *file)
  177. {
  178. struct inode *inode, *upperinode;
  179. if (file->f_flags & O_NOATIME)
  180. return;
  181. inode = file_inode(file);
  182. upperinode = ovl_inode_upper(inode);
  183. if (!upperinode)
  184. return;
  185. if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
  186. !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
  187. inode->i_mtime = upperinode->i_mtime;
  188. inode->i_ctime = upperinode->i_ctime;
  189. }
  190. touch_atime(&file->f_path);
  191. }
  192. static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
  193. {
  194. if (refcount_dec_and_test(&aio_req->ref)) {
  195. fdput(aio_req->fd);
  196. kmem_cache_free(ovl_aio_request_cachep, aio_req);
  197. }
  198. }
  199. static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
  200. {
  201. struct kiocb *iocb = &aio_req->iocb;
  202. struct kiocb *orig_iocb = aio_req->orig_iocb;
  203. if (iocb->ki_flags & IOCB_WRITE) {
  204. struct inode *inode = file_inode(orig_iocb->ki_filp);
  205. /* Actually acquired in ovl_write_iter() */
  206. __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
  207. SB_FREEZE_WRITE);
  208. file_end_write(iocb->ki_filp);
  209. ovl_copyattr(ovl_inode_real(inode), inode);
  210. }
  211. orig_iocb->ki_pos = iocb->ki_pos;
  212. ovl_aio_put(aio_req);
  213. }
  214. static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2)
  215. {
  216. struct ovl_aio_req *aio_req = container_of(iocb,
  217. struct ovl_aio_req, iocb);
  218. struct kiocb *orig_iocb = aio_req->orig_iocb;
  219. ovl_aio_cleanup_handler(aio_req);
  220. orig_iocb->ki_complete(orig_iocb, res, res2);
  221. }
  222. static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  223. {
  224. struct file *file = iocb->ki_filp;
  225. struct fd real;
  226. const struct cred *old_cred;
  227. ssize_t ret;
  228. if (!iov_iter_count(iter))
  229. return 0;
  230. ret = ovl_real_fdget(file, &real);
  231. if (ret)
  232. return ret;
  233. ret = -EINVAL;
  234. if (iocb->ki_flags & IOCB_DIRECT &&
  235. (!real.file->f_mapping->a_ops ||
  236. !real.file->f_mapping->a_ops->direct_IO))
  237. goto out_fdput;
  238. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  239. if (is_sync_kiocb(iocb)) {
  240. ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
  241. iocb_to_rw_flags(iocb->ki_flags,
  242. OVL_IOCB_MASK));
  243. } else {
  244. struct ovl_aio_req *aio_req;
  245. ret = -ENOMEM;
  246. aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
  247. if (!aio_req)
  248. goto out;
  249. aio_req->fd = real;
  250. real.flags = 0;
  251. aio_req->orig_iocb = iocb;
  252. kiocb_clone(&aio_req->iocb, iocb, real.file);
  253. aio_req->iocb.ki_complete = ovl_aio_rw_complete;
  254. refcount_set(&aio_req->ref, 2);
  255. ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
  256. ovl_aio_put(aio_req);
  257. if (ret != -EIOCBQUEUED)
  258. ovl_aio_cleanup_handler(aio_req);
  259. }
  260. out:
  261. ovl_revert_creds(file_inode(file)->i_sb, old_cred);
  262. ovl_file_accessed(file);
  263. out_fdput:
  264. fdput(real);
  265. return ret;
  266. }
  267. static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
  268. {
  269. struct file *file = iocb->ki_filp;
  270. struct inode *inode = file_inode(file);
  271. struct fd real;
  272. const struct cred *old_cred;
  273. ssize_t ret;
  274. int ifl = iocb->ki_flags;
  275. if (!iov_iter_count(iter))
  276. return 0;
  277. inode_lock(inode);
  278. /* Update mode */
  279. ovl_copyattr(ovl_inode_real(inode), inode);
  280. ret = file_remove_privs(file);
  281. if (ret)
  282. goto out_unlock;
  283. ret = ovl_real_fdget(file, &real);
  284. if (ret)
  285. goto out_unlock;
  286. ret = -EINVAL;
  287. if (iocb->ki_flags & IOCB_DIRECT &&
  288. (!real.file->f_mapping->a_ops ||
  289. !real.file->f_mapping->a_ops->direct_IO))
  290. goto out_fdput;
  291. if (!ovl_should_sync(OVL_FS(inode->i_sb)))
  292. ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
  293. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  294. if (is_sync_kiocb(iocb)) {
  295. file_start_write(real.file);
  296. ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
  297. iocb_to_rw_flags(ifl, OVL_IOCB_MASK));
  298. file_end_write(real.file);
  299. /* Update size */
  300. ovl_copyattr(ovl_inode_real(inode), inode);
  301. } else {
  302. struct ovl_aio_req *aio_req;
  303. ret = -ENOMEM;
  304. aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
  305. if (!aio_req)
  306. goto out;
  307. file_start_write(real.file);
  308. /* Pacify lockdep, same trick as done in aio_write() */
  309. __sb_writers_release(file_inode(real.file)->i_sb,
  310. SB_FREEZE_WRITE);
  311. aio_req->fd = real;
  312. real.flags = 0;
  313. aio_req->orig_iocb = iocb;
  314. kiocb_clone(&aio_req->iocb, iocb, real.file);
  315. aio_req->iocb.ki_flags = ifl;
  316. aio_req->iocb.ki_complete = ovl_aio_rw_complete;
  317. refcount_set(&aio_req->ref, 2);
  318. ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
  319. ovl_aio_put(aio_req);
  320. if (ret != -EIOCBQUEUED)
  321. ovl_aio_cleanup_handler(aio_req);
  322. }
  323. out:
  324. ovl_revert_creds(file_inode(file)->i_sb, old_cred);
  325. out_fdput:
  326. fdput(real);
  327. out_unlock:
  328. inode_unlock(inode);
  329. return ret;
  330. }
  331. /*
  332. * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
  333. * due to lock order inversion between pipe->mutex in iter_file_splice_write()
  334. * and file_start_write(real.file) in ovl_write_iter().
  335. *
  336. * So do everything ovl_write_iter() does and call iter_file_splice_write() on
  337. * the real file.
  338. */
  339. static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
  340. loff_t *ppos, size_t len, unsigned int flags)
  341. {
  342. struct fd real;
  343. const struct cred *old_cred;
  344. struct inode *inode = file_inode(out);
  345. struct inode *realinode = ovl_inode_real(inode);
  346. ssize_t ret;
  347. inode_lock(inode);
  348. /* Update mode */
  349. ovl_copyattr(realinode, inode);
  350. ret = file_remove_privs(out);
  351. if (ret)
  352. goto out_unlock;
  353. ret = ovl_real_fdget(out, &real);
  354. if (ret)
  355. goto out_unlock;
  356. old_cred = ovl_override_creds(inode->i_sb);
  357. file_start_write(real.file);
  358. ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
  359. file_end_write(real.file);
  360. /* Update size */
  361. ovl_copyattr(realinode, inode);
  362. ovl_revert_creds(inode->i_sb, old_cred);
  363. fdput(real);
  364. out_unlock:
  365. inode_unlock(inode);
  366. return ret;
  367. }
  368. static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  369. {
  370. struct fd real;
  371. const struct cred *old_cred;
  372. int ret;
  373. ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
  374. if (ret <= 0)
  375. return ret;
  376. ret = ovl_real_fdget_meta(file, &real, !datasync);
  377. if (ret)
  378. return ret;
  379. /* Don't sync lower file for fear of receiving EROFS error */
  380. if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
  381. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  382. ret = vfs_fsync_range(real.file, start, end, datasync);
  383. ovl_revert_creds(file_inode(file)->i_sb, old_cred);
  384. }
  385. fdput(real);
  386. return ret;
  387. }
  388. static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
  389. {
  390. struct file *realfile = file->private_data;
  391. const struct cred *old_cred;
  392. int ret;
  393. if (!realfile->f_op->mmap)
  394. return -ENODEV;
  395. if (WARN_ON(file != vma->vm_file))
  396. return -EIO;
  397. vma->vm_file = get_file(realfile);
  398. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  399. ret = call_mmap(vma->vm_file, vma);
  400. ovl_revert_creds(file_inode(file)->i_sb, old_cred);
  401. if (ret) {
  402. /* Drop reference count from new vm_file value */
  403. fput(realfile);
  404. } else {
  405. /* Drop reference count from previous vm_file value */
  406. fput(file);
  407. }
  408. ovl_file_accessed(file);
  409. return ret;
  410. }
  411. static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  412. {
  413. struct inode *inode = file_inode(file);
  414. struct fd real;
  415. const struct cred *old_cred;
  416. int ret;
  417. ret = ovl_real_fdget(file, &real);
  418. if (ret)
  419. return ret;
  420. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  421. ret = vfs_fallocate(real.file, mode, offset, len);
  422. ovl_revert_creds(file_inode(file)->i_sb, old_cred);
  423. /* Update size */
  424. ovl_copyattr(ovl_inode_real(inode), inode);
  425. fdput(real);
  426. return ret;
  427. }
  428. static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  429. {
  430. struct fd real;
  431. const struct cred *old_cred;
  432. int ret;
  433. ret = ovl_real_fdget(file, &real);
  434. if (ret)
  435. return ret;
  436. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  437. ret = vfs_fadvise(real.file, offset, len, advice);
  438. ovl_revert_creds(file_inode(file)->i_sb, old_cred);
  439. fdput(real);
  440. return ret;
  441. }
  442. static long ovl_real_ioctl(struct file *file, unsigned int cmd,
  443. unsigned long arg)
  444. {
  445. struct fd real;
  446. long ret;
  447. ret = ovl_real_fdget(file, &real);
  448. if (ret)
  449. return ret;
  450. ret = security_file_ioctl(real.file, cmd, arg);
  451. if (!ret) {
  452. /*
  453. * Don't override creds, since we currently can't safely check
  454. * permissions before doing so.
  455. */
  456. ret = vfs_ioctl(real.file, cmd, arg);
  457. }
  458. fdput(real);
  459. return ret;
  460. }
  461. static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
  462. unsigned long arg)
  463. {
  464. long ret;
  465. struct inode *inode = file_inode(file);
  466. if (!inode_owner_or_capable(inode))
  467. return -EACCES;
  468. ret = mnt_want_write_file(file);
  469. if (ret)
  470. return ret;
  471. inode_lock(inode);
  472. /*
  473. * Prevent copy up if immutable and has no CAP_LINUX_IMMUTABLE
  474. * capability.
  475. */
  476. ret = -EPERM;
  477. if (!ovl_has_upperdata(inode) && IS_IMMUTABLE(inode) &&
  478. !capable(CAP_LINUX_IMMUTABLE))
  479. goto unlock;
  480. ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
  481. if (ret)
  482. goto unlock;
  483. ret = ovl_real_ioctl(file, cmd, arg);
  484. ovl_copyflags(ovl_inode_real(inode), inode);
  485. unlock:
  486. inode_unlock(inode);
  487. mnt_drop_write_file(file);
  488. return ret;
  489. }
  490. long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  491. {
  492. long ret;
  493. switch (cmd) {
  494. case FS_IOC_GETFLAGS:
  495. case FS_IOC_FSGETXATTR:
  496. ret = ovl_real_ioctl(file, cmd, arg);
  497. break;
  498. case FS_IOC_FSSETXATTR:
  499. case FS_IOC_SETFLAGS:
  500. ret = ovl_ioctl_set_flags(file, cmd, arg);
  501. break;
  502. default:
  503. ret = -ENOTTY;
  504. }
  505. return ret;
  506. }
  507. #ifdef CONFIG_COMPAT
  508. long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  509. {
  510. switch (cmd) {
  511. case FS_IOC32_GETFLAGS:
  512. cmd = FS_IOC_GETFLAGS;
  513. break;
  514. case FS_IOC32_SETFLAGS:
  515. cmd = FS_IOC_SETFLAGS;
  516. break;
  517. default:
  518. return -ENOIOCTLCMD;
  519. }
  520. return ovl_ioctl(file, cmd, arg);
  521. }
  522. #endif
  523. enum ovl_copyop {
  524. OVL_COPY,
  525. OVL_CLONE,
  526. OVL_DEDUPE,
  527. };
  528. static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
  529. struct file *file_out, loff_t pos_out,
  530. loff_t len, unsigned int flags, enum ovl_copyop op)
  531. {
  532. struct inode *inode_out = file_inode(file_out);
  533. struct fd real_in, real_out;
  534. const struct cred *old_cred;
  535. loff_t ret;
  536. ret = ovl_real_fdget(file_out, &real_out);
  537. if (ret)
  538. return ret;
  539. ret = ovl_real_fdget(file_in, &real_in);
  540. if (ret) {
  541. fdput(real_out);
  542. return ret;
  543. }
  544. old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
  545. switch (op) {
  546. case OVL_COPY:
  547. ret = vfs_copy_file_range(real_in.file, pos_in,
  548. real_out.file, pos_out, len, flags);
  549. break;
  550. case OVL_CLONE:
  551. ret = vfs_clone_file_range(real_in.file, pos_in,
  552. real_out.file, pos_out, len, flags);
  553. break;
  554. case OVL_DEDUPE:
  555. ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
  556. real_out.file, pos_out, len,
  557. flags);
  558. break;
  559. }
  560. ovl_revert_creds(file_inode(file_out)->i_sb, old_cred);
  561. /* Update size */
  562. ovl_copyattr(ovl_inode_real(inode_out), inode_out);
  563. fdput(real_in);
  564. fdput(real_out);
  565. return ret;
  566. }
  567. static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
  568. struct file *file_out, loff_t pos_out,
  569. size_t len, unsigned int flags)
  570. {
  571. return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
  572. OVL_COPY);
  573. }
  574. static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
  575. struct file *file_out, loff_t pos_out,
  576. loff_t len, unsigned int remap_flags)
  577. {
  578. enum ovl_copyop op;
  579. if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
  580. return -EINVAL;
  581. if (remap_flags & REMAP_FILE_DEDUP)
  582. op = OVL_DEDUPE;
  583. else
  584. op = OVL_CLONE;
  585. /*
  586. * Don't copy up because of a dedupe request, this wouldn't make sense
  587. * most of the time (data would be duplicated instead of deduplicated).
  588. */
  589. if (op == OVL_DEDUPE &&
  590. (!ovl_inode_upper(file_inode(file_in)) ||
  591. !ovl_inode_upper(file_inode(file_out))))
  592. return -EPERM;
  593. return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
  594. remap_flags, op);
  595. }
  596. const struct file_operations ovl_file_operations = {
  597. .open = ovl_open,
  598. .release = ovl_release,
  599. .llseek = ovl_llseek,
  600. .read_iter = ovl_read_iter,
  601. .write_iter = ovl_write_iter,
  602. .fsync = ovl_fsync,
  603. .mmap = ovl_mmap,
  604. .fallocate = ovl_fallocate,
  605. .fadvise = ovl_fadvise,
  606. .unlocked_ioctl = ovl_ioctl,
  607. #ifdef CONFIG_COMPAT
  608. .compat_ioctl = ovl_compat_ioctl,
  609. #endif
  610. .splice_read = generic_file_splice_read,
  611. .splice_write = ovl_splice_write,
  612. .copy_file_range = ovl_copy_file_range,
  613. .remap_file_range = ovl_remap_file_range,
  614. };
  615. int __init ovl_aio_request_cache_init(void)
  616. {
  617. ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
  618. sizeof(struct ovl_aio_req),
  619. 0, SLAB_HWCACHE_ALIGN, NULL);
  620. if (!ovl_aio_request_cachep)
  621. return -ENOMEM;
  622. return 0;
  623. }
  624. void ovl_aio_request_cache_destroy(void)
  625. {
  626. kmem_cache_destroy(ovl_aio_request_cachep);
  627. }