buffered_write.c 16 KB

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