buffered_write.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Network filesystem high-level buffered write support.
  3. *
  4. * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/export.h>
  8. #include <linux/fs.h>
  9. #include <linux/mm.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/slab.h>
  12. #include <linux/pagevec.h>
  13. #include "internal.h"
  14. static void __netfs_set_group(struct folio *folio, struct netfs_group *netfs_group)
  15. {
  16. if (netfs_group)
  17. folio_attach_private(folio, netfs_get_group(netfs_group));
  18. }
  19. static void netfs_set_group(struct folio *folio, struct netfs_group *netfs_group)
  20. {
  21. void *priv = folio_get_private(folio);
  22. if (unlikely(priv != netfs_group)) {
  23. if (netfs_group && (!priv || priv == NETFS_FOLIO_COPY_TO_CACHE))
  24. folio_attach_private(folio, netfs_get_group(netfs_group));
  25. else if (!netfs_group && priv == NETFS_FOLIO_COPY_TO_CACHE)
  26. folio_detach_private(folio);
  27. }
  28. }
  29. /*
  30. * Grab a folio for writing and lock it. Attempt to allocate as large a folio
  31. * as possible to hold as much of the remaining length as possible in one go.
  32. */
  33. static struct folio *netfs_grab_folio_for_write(struct address_space *mapping,
  34. loff_t pos, size_t part)
  35. {
  36. pgoff_t index = pos / PAGE_SIZE;
  37. fgf_t fgp_flags = FGP_WRITEBEGIN;
  38. if (mapping_large_folio_support(mapping))
  39. fgp_flags |= fgf_set_order(pos % PAGE_SIZE + part);
  40. return __filemap_get_folio(mapping, index, fgp_flags,
  41. mapping_gfp_mask(mapping));
  42. }
  43. /*
  44. * Update i_size and estimate the update to i_blocks to reflect the additional
  45. * data written into the pagecache until we can find out from the server what
  46. * the values actually are.
  47. */
  48. void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
  49. loff_t pos, size_t copied)
  50. {
  51. loff_t i_size, end = pos + copied;
  52. blkcnt_t add;
  53. size_t gap;
  54. if (end <= i_size_read(inode))
  55. return;
  56. if (ctx->ops->update_i_size) {
  57. ctx->ops->update_i_size(inode, end);
  58. return;
  59. }
  60. spin_lock(&inode->i_lock);
  61. i_size = i_size_read(inode);
  62. if (end > i_size) {
  63. i_size_write(inode, end);
  64. #if IS_ENABLED(CONFIG_FSCACHE)
  65. fscache_update_cookie(ctx->cache, NULL, &end);
  66. #endif
  67. gap = SECTOR_SIZE - (i_size & (SECTOR_SIZE - 1));
  68. if (copied > gap) {
  69. add = DIV_ROUND_UP(copied - gap, SECTOR_SIZE);
  70. inode->i_blocks = min_t(blkcnt_t,
  71. DIV_ROUND_UP(end, SECTOR_SIZE),
  72. inode->i_blocks + add);
  73. }
  74. }
  75. spin_unlock(&inode->i_lock);
  76. }
  77. /**
  78. * netfs_perform_write - Copy data into the pagecache.
  79. * @iocb: The operation parameters
  80. * @iter: The source buffer
  81. * @netfs_group: Grouping for dirty folios (eg. ceph snaps).
  82. *
  83. * Copy data into pagecache folios attached to the inode specified by @iocb.
  84. * The caller must hold appropriate inode locks.
  85. *
  86. * Dirty folios are tagged with a netfs_folio struct if they're not up to date
  87. * to indicate the range modified. Dirty folios may also be tagged with a
  88. * netfs-specific grouping such that data from an old group gets flushed before
  89. * a new one is started.
  90. */
  91. ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
  92. struct netfs_group *netfs_group)
  93. {
  94. struct file *file = iocb->ki_filp;
  95. struct inode *inode = file_inode(file);
  96. struct address_space *mapping = inode->i_mapping;
  97. struct netfs_inode *ctx = netfs_inode(inode);
  98. struct writeback_control wbc = {
  99. .sync_mode = WB_SYNC_NONE,
  100. .for_sync = true,
  101. .nr_to_write = LONG_MAX,
  102. .range_start = iocb->ki_pos,
  103. .range_end = iocb->ki_pos + iter->count,
  104. };
  105. struct netfs_io_request *wreq = NULL;
  106. struct folio *folio = NULL, *writethrough = NULL;
  107. unsigned int bdp_flags = (iocb->ki_flags & IOCB_NOWAIT) ? BDP_ASYNC : 0;
  108. ssize_t written = 0, ret, ret2;
  109. loff_t pos = iocb->ki_pos;
  110. size_t max_chunk = mapping_max_folio_size(mapping);
  111. bool maybe_trouble = false;
  112. if (unlikely(iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC))
  113. ) {
  114. wbc_attach_fdatawrite_inode(&wbc, mapping->host);
  115. ret = filemap_write_and_wait_range(mapping, pos, pos + iter->count);
  116. if (ret < 0) {
  117. wbc_detach_inode(&wbc);
  118. goto out;
  119. }
  120. wreq = netfs_begin_writethrough(iocb, iter->count);
  121. if (IS_ERR(wreq)) {
  122. wbc_detach_inode(&wbc);
  123. ret = PTR_ERR(wreq);
  124. wreq = NULL;
  125. goto out;
  126. }
  127. if (!is_sync_kiocb(iocb))
  128. wreq->iocb = iocb;
  129. netfs_stat(&netfs_n_wh_writethrough);
  130. } else {
  131. netfs_stat(&netfs_n_wh_buffered_write);
  132. }
  133. do {
  134. struct netfs_folio *finfo;
  135. struct netfs_group *group;
  136. unsigned long long fpos;
  137. size_t flen;
  138. size_t offset; /* Offset into pagecache folio */
  139. size_t part; /* Bytes to write to folio */
  140. size_t copied; /* Bytes copied from user */
  141. offset = pos & (max_chunk - 1);
  142. part = min(max_chunk - offset, iov_iter_count(iter));
  143. /* Bring in the user pages that we will copy from _first_ lest
  144. * we hit a nasty deadlock on copying from the same page as
  145. * we're writing to, without it being marked uptodate.
  146. *
  147. * Not only is this an optimisation, but it is also required to
  148. * check that the address is actually valid, when atomic
  149. * usercopies are used below.
  150. *
  151. * We rely on the page being held onto long enough by the LRU
  152. * that we can grab it below if this causes it to be read.
  153. */
  154. ret = -EFAULT;
  155. if (unlikely(fault_in_iov_iter_readable(iter, part) == part))
  156. break;
  157. folio = netfs_grab_folio_for_write(mapping, pos, part);
  158. if (IS_ERR(folio)) {
  159. ret = PTR_ERR(folio);
  160. break;
  161. }
  162. flen = folio_size(folio);
  163. fpos = folio_pos(folio);
  164. offset = pos - fpos;
  165. part = min_t(size_t, flen - offset, part);
  166. /* Wait for writeback to complete. The writeback engine owns
  167. * the info in folio->private and may change it until it
  168. * removes the WB mark.
  169. */
  170. if (folio_get_private(folio) &&
  171. folio_wait_writeback_killable(folio)) {
  172. ret = written ? -EINTR : -ERESTARTSYS;
  173. goto error_folio_unlock;
  174. }
  175. if (signal_pending(current)) {
  176. ret = written ? -EINTR : -ERESTARTSYS;
  177. goto error_folio_unlock;
  178. }
  179. /* Decide how we should modify a folio. We might be attempting
  180. * to do write-streaming, in which case we don't want to a
  181. * local RMW cycle if we can avoid it. If we're doing local
  182. * caching or content crypto, we award that priority over
  183. * avoiding RMW. If the file is open readably, then we also
  184. * assume that we may want to read what we wrote.
  185. */
  186. finfo = netfs_folio_info(folio);
  187. group = netfs_folio_group(folio);
  188. if (unlikely(group != netfs_group) &&
  189. group != NETFS_FOLIO_COPY_TO_CACHE)
  190. goto flush_content;
  191. if (folio_test_uptodate(folio)) {
  192. if (mapping_writably_mapped(mapping))
  193. flush_dcache_folio(folio);
  194. copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
  195. if (unlikely(copied == 0))
  196. goto copy_failed;
  197. netfs_set_group(folio, netfs_group);
  198. trace_netfs_folio(folio, netfs_folio_is_uptodate);
  199. goto copied;
  200. }
  201. /* If the page is above the zero-point then we assume that the
  202. * server would just return a block of zeros or a short read if
  203. * we try to read it.
  204. */
  205. if (fpos >= ctx->zero_point) {
  206. folio_zero_segment(folio, 0, offset);
  207. copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
  208. if (unlikely(copied == 0))
  209. goto copy_failed;
  210. folio_zero_segment(folio, offset + copied, flen);
  211. __netfs_set_group(folio, netfs_group);
  212. folio_mark_uptodate(folio);
  213. trace_netfs_folio(folio, netfs_modify_and_clear);
  214. goto copied;
  215. }
  216. /* See if we can write a whole folio in one go. */
  217. if (!maybe_trouble && offset == 0 && part >= flen) {
  218. copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
  219. if (unlikely(copied == 0))
  220. goto copy_failed;
  221. if (unlikely(copied < part)) {
  222. maybe_trouble = true;
  223. iov_iter_revert(iter, copied);
  224. copied = 0;
  225. folio_unlock(folio);
  226. goto retry;
  227. }
  228. __netfs_set_group(folio, netfs_group);
  229. folio_mark_uptodate(folio);
  230. trace_netfs_folio(folio, netfs_whole_folio_modify);
  231. goto copied;
  232. }
  233. /* We don't want to do a streaming write on a file that loses
  234. * caching service temporarily because the backing store got
  235. * culled and we don't really want to get a streaming write on
  236. * a file that's open for reading as ->read_folio() then has to
  237. * be able to flush it.
  238. */
  239. if ((file->f_mode & FMODE_READ) ||
  240. netfs_is_cache_enabled(ctx)) {
  241. if (finfo) {
  242. netfs_stat(&netfs_n_wh_wstream_conflict);
  243. goto flush_content;
  244. }
  245. ret = netfs_prefetch_for_write(file, folio, offset, part);
  246. if (ret < 0) {
  247. _debug("prefetch = %zd", ret);
  248. goto error_folio_unlock;
  249. }
  250. /* Note that copy-to-cache may have been set. */
  251. copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
  252. if (unlikely(copied == 0))
  253. goto copy_failed;
  254. netfs_set_group(folio, netfs_group);
  255. trace_netfs_folio(folio, netfs_just_prefetch);
  256. goto copied;
  257. }
  258. if (!finfo) {
  259. ret = -EIO;
  260. if (WARN_ON(folio_get_private(folio)))
  261. goto error_folio_unlock;
  262. copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
  263. if (unlikely(copied == 0))
  264. goto copy_failed;
  265. if (offset == 0 && copied == flen) {
  266. __netfs_set_group(folio, netfs_group);
  267. folio_mark_uptodate(folio);
  268. trace_netfs_folio(folio, netfs_streaming_filled_page);
  269. goto copied;
  270. }
  271. finfo = kzalloc_obj(*finfo);
  272. if (!finfo) {
  273. iov_iter_revert(iter, copied);
  274. ret = -ENOMEM;
  275. goto error_folio_unlock;
  276. }
  277. finfo->netfs_group = netfs_get_group(netfs_group);
  278. finfo->dirty_offset = offset;
  279. finfo->dirty_len = copied;
  280. folio_attach_private(folio, (void *)((unsigned long)finfo |
  281. NETFS_FOLIO_INFO));
  282. trace_netfs_folio(folio, netfs_streaming_write);
  283. goto copied;
  284. }
  285. /* We can continue a streaming write only if it continues on
  286. * from the previous. If it overlaps, we must flush lest we
  287. * suffer a partial copy and disjoint dirty regions.
  288. */
  289. if (offset == finfo->dirty_offset + finfo->dirty_len) {
  290. copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
  291. if (unlikely(copied == 0))
  292. goto copy_failed;
  293. finfo->dirty_len += copied;
  294. if (finfo->dirty_offset == 0 && finfo->dirty_len == flen) {
  295. if (finfo->netfs_group)
  296. folio_change_private(folio, finfo->netfs_group);
  297. else
  298. folio_detach_private(folio);
  299. folio_mark_uptodate(folio);
  300. kfree(finfo);
  301. trace_netfs_folio(folio, netfs_streaming_cont_filled_page);
  302. } else {
  303. trace_netfs_folio(folio, netfs_streaming_write_cont);
  304. }
  305. goto copied;
  306. }
  307. /* Incompatible write; flush the folio and try again. */
  308. flush_content:
  309. trace_netfs_folio(folio, netfs_flush_content);
  310. folio_unlock(folio);
  311. folio_put(folio);
  312. ret = filemap_write_and_wait_range(mapping, fpos, fpos + flen - 1);
  313. if (ret < 0)
  314. goto out;
  315. continue;
  316. copied:
  317. flush_dcache_folio(folio);
  318. /* Update the inode size if we moved the EOF marker */
  319. netfs_update_i_size(ctx, inode, pos, copied);
  320. pos += copied;
  321. written += copied;
  322. if (likely(!wreq)) {
  323. folio_mark_dirty(folio);
  324. folio_unlock(folio);
  325. } else {
  326. netfs_advance_writethrough(wreq, &wbc, folio, copied,
  327. offset + copied == flen,
  328. &writethrough);
  329. /* Folio unlocked */
  330. }
  331. retry:
  332. folio_put(folio);
  333. folio = NULL;
  334. ret = balance_dirty_pages_ratelimited_flags(mapping, bdp_flags);
  335. if (unlikely(ret < 0))
  336. break;
  337. cond_resched();
  338. } while (iov_iter_count(iter));
  339. out:
  340. if (likely(written)) {
  341. /* Set indication that ctime and mtime got updated in case
  342. * close is deferred.
  343. */
  344. set_bit(NETFS_ICTX_MODIFIED_ATTR, &ctx->flags);
  345. if (unlikely(ctx->ops->post_modify))
  346. ctx->ops->post_modify(inode);
  347. }
  348. if (unlikely(wreq)) {
  349. ret2 = netfs_end_writethrough(wreq, &wbc, writethrough);
  350. wbc_detach_inode(&wbc);
  351. if (ret2 == -EIOCBQUEUED)
  352. return ret2;
  353. if (ret == 0 && ret2 < 0)
  354. ret = ret2;
  355. }
  356. iocb->ki_pos += written;
  357. _leave(" = %zd [%zd]", written, ret);
  358. return written ? written : ret;
  359. copy_failed:
  360. ret = -EFAULT;
  361. error_folio_unlock:
  362. folio_unlock(folio);
  363. folio_put(folio);
  364. goto out;
  365. }
  366. EXPORT_SYMBOL(netfs_perform_write);
  367. /**
  368. * netfs_buffered_write_iter_locked - write data to a file
  369. * @iocb: IO state structure (file, offset, etc.)
  370. * @from: iov_iter with data to write
  371. * @netfs_group: Grouping for dirty folios (eg. ceph snaps).
  372. *
  373. * This function does all the work needed for actually writing data to a
  374. * file. It does all basic checks, removes SUID from the file, updates
  375. * modification times and calls proper subroutines depending on whether we
  376. * do direct IO or a standard buffered write.
  377. *
  378. * The caller must hold appropriate locks around this function and have called
  379. * generic_write_checks() already. The caller is also responsible for doing
  380. * any necessary syncing afterwards.
  381. *
  382. * This function does *not* take care of syncing data in case of O_SYNC write.
  383. * A caller has to handle it. This is mainly due to the fact that we want to
  384. * avoid syncing under i_rwsem.
  385. *
  386. * Return:
  387. * * number of bytes written, even for truncated writes
  388. * * negative error code if no data has been written at all
  389. */
  390. ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *from,
  391. struct netfs_group *netfs_group)
  392. {
  393. struct file *file = iocb->ki_filp;
  394. ssize_t ret;
  395. trace_netfs_write_iter(iocb, from);
  396. ret = file_remove_privs(file);
  397. if (ret)
  398. return ret;
  399. ret = file_update_time(file);
  400. if (ret)
  401. return ret;
  402. return netfs_perform_write(iocb, from, netfs_group);
  403. }
  404. EXPORT_SYMBOL(netfs_buffered_write_iter_locked);
  405. /**
  406. * netfs_file_write_iter - write data to a file
  407. * @iocb: IO state structure
  408. * @from: iov_iter with data to write
  409. *
  410. * Perform a write to a file, writing into the pagecache if possible and doing
  411. * an unbuffered write instead if not.
  412. *
  413. * Return:
  414. * * Negative error code if no data has been written at all of
  415. * vfs_fsync_range() failed for a synchronous write
  416. * * Number of bytes written, even for truncated writes
  417. */
  418. ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  419. {
  420. struct file *file = iocb->ki_filp;
  421. struct inode *inode = file->f_mapping->host;
  422. struct netfs_inode *ictx = netfs_inode(inode);
  423. ssize_t ret;
  424. _enter("%llx,%zx,%llx", iocb->ki_pos, iov_iter_count(from), i_size_read(inode));
  425. if (!iov_iter_count(from))
  426. return 0;
  427. if ((iocb->ki_flags & IOCB_DIRECT) ||
  428. test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags))
  429. return netfs_unbuffered_write_iter(iocb, from);
  430. ret = netfs_start_io_write(inode);
  431. if (ret < 0)
  432. return ret;
  433. ret = generic_write_checks(iocb, from);
  434. if (ret > 0)
  435. ret = netfs_buffered_write_iter_locked(iocb, from, NULL);
  436. netfs_end_io_write(inode);
  437. if (ret > 0)
  438. ret = generic_write_sync(iocb, ret);
  439. return ret;
  440. }
  441. EXPORT_SYMBOL(netfs_file_write_iter);
  442. /*
  443. * Notification that a previously read-only page is about to become writable.
  444. * The caller indicates the precise page that needs to be written to, but
  445. * we only track group on a per-folio basis, so we block more often than
  446. * we might otherwise.
  447. */
  448. vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_group)
  449. {
  450. struct netfs_group *group;
  451. struct folio *folio = page_folio(vmf->page);
  452. struct file *file = vmf->vma->vm_file;
  453. struct address_space *mapping = file->f_mapping;
  454. struct inode *inode = file_inode(file);
  455. struct netfs_inode *ictx = netfs_inode(inode);
  456. vm_fault_t ret = VM_FAULT_NOPAGE;
  457. int err;
  458. _enter("%lx", folio->index);
  459. sb_start_pagefault(inode->i_sb);
  460. if (folio_lock_killable(folio) < 0)
  461. goto out;
  462. if (folio->mapping != mapping)
  463. goto unlock;
  464. if (folio_wait_writeback_killable(folio) < 0)
  465. goto unlock;
  466. /* Can we see a streaming write here? */
  467. if (WARN_ON(!folio_test_uptodate(folio))) {
  468. ret = VM_FAULT_SIGBUS;
  469. goto unlock;
  470. }
  471. group = netfs_folio_group(folio);
  472. if (group != netfs_group && group != NETFS_FOLIO_COPY_TO_CACHE) {
  473. folio_unlock(folio);
  474. err = filemap_fdatawrite_range(mapping,
  475. folio_pos(folio),
  476. folio_next_pos(folio));
  477. switch (err) {
  478. case 0:
  479. ret = VM_FAULT_RETRY;
  480. goto out;
  481. case -ENOMEM:
  482. ret = VM_FAULT_OOM;
  483. goto out;
  484. default:
  485. ret = VM_FAULT_SIGBUS;
  486. goto out;
  487. }
  488. }
  489. if (folio_test_dirty(folio))
  490. trace_netfs_folio(folio, netfs_folio_trace_mkwrite_plus);
  491. else
  492. trace_netfs_folio(folio, netfs_folio_trace_mkwrite);
  493. netfs_set_group(folio, netfs_group);
  494. file_update_time(file);
  495. set_bit(NETFS_ICTX_MODIFIED_ATTR, &ictx->flags);
  496. if (ictx->ops->post_modify)
  497. ictx->ops->post_modify(inode);
  498. ret = VM_FAULT_LOCKED;
  499. out:
  500. sb_end_pagefault(inode->i_sb);
  501. return ret;
  502. unlock:
  503. folio_unlock(folio);
  504. goto out;
  505. }
  506. EXPORT_SYMBOL(netfs_page_mkwrite);