write.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. /*
  2. * linux/fs/nfs/write.c
  3. *
  4. * Write file data over NFS.
  5. *
  6. * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/file.h>
  13. #include <linux/writeback.h>
  14. #include <linux/swap.h>
  15. #include <linux/migrate.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/nfs_fs.h>
  18. #include <linux/nfs_mount.h>
  19. #include <linux/nfs_page.h>
  20. #include <linux/backing-dev.h>
  21. #include <linux/export.h>
  22. #include <asm/uaccess.h>
  23. #include "delegation.h"
  24. #include "internal.h"
  25. #include "iostat.h"
  26. #include "nfs4_fs.h"
  27. #include "fscache.h"
  28. #include "pnfs.h"
  29. #include "nfstrace.h"
  30. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  31. #define MIN_POOL_WRITE (32)
  32. #define MIN_POOL_COMMIT (4)
  33. /*
  34. * Local function declarations
  35. */
  36. static void nfs_redirty_request(struct nfs_page *req);
  37. static const struct rpc_call_ops nfs_commit_ops;
  38. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
  39. static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
  40. static const struct nfs_rw_ops nfs_rw_write_ops;
  41. static void nfs_clear_request_commit(struct nfs_page *req);
  42. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  43. struct inode *inode);
  44. static struct kmem_cache *nfs_wdata_cachep;
  45. static mempool_t *nfs_wdata_mempool;
  46. static struct kmem_cache *nfs_cdata_cachep;
  47. static mempool_t *nfs_commit_mempool;
  48. struct nfs_commit_data *nfs_commitdata_alloc(void)
  49. {
  50. struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
  51. if (p) {
  52. memset(p, 0, sizeof(*p));
  53. INIT_LIST_HEAD(&p->pages);
  54. }
  55. return p;
  56. }
  57. EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
  58. void nfs_commit_free(struct nfs_commit_data *p)
  59. {
  60. mempool_free(p, nfs_commit_mempool);
  61. }
  62. EXPORT_SYMBOL_GPL(nfs_commit_free);
  63. static struct nfs_pgio_header *nfs_writehdr_alloc(void)
  64. {
  65. struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
  66. if (p)
  67. memset(p, 0, sizeof(*p));
  68. return p;
  69. }
  70. static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
  71. {
  72. mempool_free(hdr, nfs_wdata_mempool);
  73. }
  74. static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
  75. {
  76. ctx->error = error;
  77. smp_wmb();
  78. set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  79. }
  80. /*
  81. * nfs_page_search_commits_for_head_request_locked
  82. *
  83. * Search through commit lists on @inode for the head request for @page.
  84. * Must be called while holding the inode (which is cinfo) lock.
  85. *
  86. * Returns the head request if found, or NULL if not found.
  87. */
  88. static struct nfs_page *
  89. nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
  90. struct page *page)
  91. {
  92. struct nfs_page *freq, *t;
  93. struct nfs_commit_info cinfo;
  94. struct inode *inode = &nfsi->vfs_inode;
  95. nfs_init_cinfo_from_inode(&cinfo, inode);
  96. /* search through pnfs commit lists */
  97. freq = pnfs_search_commit_reqs(inode, &cinfo, page);
  98. if (freq)
  99. return freq->wb_head;
  100. /* Linearly search the commit list for the correct request */
  101. list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
  102. if (freq->wb_page == page)
  103. return freq->wb_head;
  104. }
  105. return NULL;
  106. }
  107. /*
  108. * nfs_page_find_head_request_locked - find head request associated with @page
  109. *
  110. * must be called while holding the inode lock.
  111. *
  112. * returns matching head request with reference held, or NULL if not found.
  113. */
  114. static struct nfs_page *
  115. nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page)
  116. {
  117. struct nfs_page *req = NULL;
  118. if (PagePrivate(page))
  119. req = (struct nfs_page *)page_private(page);
  120. else if (unlikely(PageSwapCache(page)))
  121. req = nfs_page_search_commits_for_head_request_locked(nfsi,
  122. page);
  123. if (req) {
  124. WARN_ON_ONCE(req->wb_head != req);
  125. kref_get(&req->wb_kref);
  126. }
  127. return req;
  128. }
  129. /*
  130. * nfs_page_find_head_request - find head request associated with @page
  131. *
  132. * returns matching head request with reference held, or NULL if not found.
  133. */
  134. static struct nfs_page *nfs_page_find_head_request(struct page *page)
  135. {
  136. struct inode *inode = page_file_mapping(page)->host;
  137. struct nfs_page *req = NULL;
  138. spin_lock(&inode->i_lock);
  139. req = nfs_page_find_head_request_locked(NFS_I(inode), page);
  140. spin_unlock(&inode->i_lock);
  141. return req;
  142. }
  143. /* Adjust the file length if we're writing beyond the end */
  144. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  145. {
  146. struct inode *inode = page_file_mapping(page)->host;
  147. loff_t end, i_size;
  148. pgoff_t end_index;
  149. spin_lock(&inode->i_lock);
  150. i_size = i_size_read(inode);
  151. end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
  152. if (i_size > 0 && page_file_index(page) < end_index)
  153. goto out;
  154. end = page_file_offset(page) + ((loff_t)offset+count);
  155. if (i_size >= end)
  156. goto out;
  157. i_size_write(inode, end);
  158. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  159. out:
  160. spin_unlock(&inode->i_lock);
  161. }
  162. /* A writeback failed: mark the page as bad, and invalidate the page cache */
  163. static void nfs_set_pageerror(struct page *page)
  164. {
  165. nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
  166. }
  167. /*
  168. * nfs_page_group_search_locked
  169. * @head - head request of page group
  170. * @page_offset - offset into page
  171. *
  172. * Search page group with head @head to find a request that contains the
  173. * page offset @page_offset.
  174. *
  175. * Returns a pointer to the first matching nfs request, or NULL if no
  176. * match is found.
  177. *
  178. * Must be called with the page group lock held
  179. */
  180. static struct nfs_page *
  181. nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
  182. {
  183. struct nfs_page *req;
  184. WARN_ON_ONCE(head != head->wb_head);
  185. WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
  186. req = head;
  187. do {
  188. if (page_offset >= req->wb_pgbase &&
  189. page_offset < (req->wb_pgbase + req->wb_bytes))
  190. return req;
  191. req = req->wb_this_page;
  192. } while (req != head);
  193. return NULL;
  194. }
  195. /*
  196. * nfs_page_group_covers_page
  197. * @head - head request of page group
  198. *
  199. * Return true if the page group with head @head covers the whole page,
  200. * returns false otherwise
  201. */
  202. static bool nfs_page_group_covers_page(struct nfs_page *req)
  203. {
  204. struct nfs_page *tmp;
  205. unsigned int pos = 0;
  206. unsigned int len = nfs_page_length(req->wb_page);
  207. nfs_page_group_lock(req, false);
  208. do {
  209. tmp = nfs_page_group_search_locked(req->wb_head, pos);
  210. if (tmp) {
  211. /* no way this should happen */
  212. WARN_ON_ONCE(tmp->wb_pgbase != pos);
  213. pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
  214. }
  215. } while (tmp && pos < len);
  216. nfs_page_group_unlock(req);
  217. WARN_ON_ONCE(pos > len);
  218. return pos == len;
  219. }
  220. /* We can set the PG_uptodate flag if we see that a write request
  221. * covers the full page.
  222. */
  223. static void nfs_mark_uptodate(struct nfs_page *req)
  224. {
  225. if (PageUptodate(req->wb_page))
  226. return;
  227. if (!nfs_page_group_covers_page(req))
  228. return;
  229. SetPageUptodate(req->wb_page);
  230. }
  231. static int wb_priority(struct writeback_control *wbc)
  232. {
  233. if (wbc->for_reclaim)
  234. return FLUSH_HIGHPRI | FLUSH_STABLE;
  235. if (wbc->for_kupdate || wbc->for_background)
  236. return FLUSH_LOWPRI | FLUSH_COND_STABLE;
  237. return FLUSH_COND_STABLE;
  238. }
  239. /*
  240. * NFS congestion control
  241. */
  242. int nfs_congestion_kb;
  243. #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
  244. #define NFS_CONGESTION_OFF_THRESH \
  245. (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
  246. static void nfs_set_page_writeback(struct page *page)
  247. {
  248. struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
  249. int ret = test_set_page_writeback(page);
  250. WARN_ON_ONCE(ret != 0);
  251. if (atomic_long_inc_return(&nfss->writeback) >
  252. NFS_CONGESTION_ON_THRESH) {
  253. set_bdi_congested(&nfss->backing_dev_info,
  254. BLK_RW_ASYNC);
  255. }
  256. }
  257. static void nfs_end_page_writeback(struct nfs_page *req)
  258. {
  259. struct inode *inode = page_file_mapping(req->wb_page)->host;
  260. struct nfs_server *nfss = NFS_SERVER(inode);
  261. if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
  262. return;
  263. end_page_writeback(req->wb_page);
  264. if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  265. clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
  266. }
  267. /* nfs_page_group_clear_bits
  268. * @req - an nfs request
  269. * clears all page group related bits from @req
  270. */
  271. static void
  272. nfs_page_group_clear_bits(struct nfs_page *req)
  273. {
  274. clear_bit(PG_TEARDOWN, &req->wb_flags);
  275. clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
  276. clear_bit(PG_UPTODATE, &req->wb_flags);
  277. clear_bit(PG_WB_END, &req->wb_flags);
  278. clear_bit(PG_REMOVE, &req->wb_flags);
  279. }
  280. /*
  281. * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req
  282. *
  283. * this is a helper function for nfs_lock_and_join_requests
  284. *
  285. * @inode - inode associated with request page group, must be holding inode lock
  286. * @head - head request of page group, must be holding head lock
  287. * @req - request that couldn't lock and needs to wait on the req bit lock
  288. * @nonblock - if true, don't actually wait
  289. *
  290. * NOTE: this must be called holding page_group bit lock and inode spin lock
  291. * and BOTH will be released before returning.
  292. *
  293. * returns 0 on success, < 0 on error.
  294. */
  295. static int
  296. nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
  297. struct nfs_page *req, bool nonblock)
  298. __releases(&inode->i_lock)
  299. {
  300. struct nfs_page *tmp;
  301. int ret;
  302. /* relinquish all the locks successfully grabbed this run */
  303. for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
  304. nfs_unlock_request(tmp);
  305. WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
  306. /* grab a ref on the request that will be waited on */
  307. kref_get(&req->wb_kref);
  308. nfs_page_group_unlock(head);
  309. spin_unlock(&inode->i_lock);
  310. /* release ref from nfs_page_find_head_request_locked */
  311. nfs_release_request(head);
  312. if (!nonblock)
  313. ret = nfs_wait_on_request(req);
  314. else
  315. ret = -EAGAIN;
  316. nfs_release_request(req);
  317. return ret;
  318. }
  319. /*
  320. * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
  321. *
  322. * @destroy_list - request list (using wb_this_page) terminated by @old_head
  323. * @old_head - the old head of the list
  324. *
  325. * All subrequests must be locked and removed from all lists, so at this point
  326. * they are only "active" in this function, and possibly in nfs_wait_on_request
  327. * with a reference held by some other context.
  328. */
  329. static void
  330. nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
  331. struct nfs_page *old_head)
  332. {
  333. while (destroy_list) {
  334. struct nfs_page *subreq = destroy_list;
  335. destroy_list = (subreq->wb_this_page == old_head) ?
  336. NULL : subreq->wb_this_page;
  337. WARN_ON_ONCE(old_head != subreq->wb_head);
  338. /* make sure old group is not used */
  339. subreq->wb_head = subreq;
  340. subreq->wb_this_page = subreq;
  341. /* subreq is now totally disconnected from page group or any
  342. * write / commit lists. last chance to wake any waiters */
  343. nfs_unlock_request(subreq);
  344. if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
  345. /* release ref on old head request */
  346. nfs_release_request(old_head);
  347. nfs_page_group_clear_bits(subreq);
  348. /* release the PG_INODE_REF reference */
  349. if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
  350. nfs_release_request(subreq);
  351. else
  352. WARN_ON_ONCE(1);
  353. } else {
  354. WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
  355. /* zombie requests have already released the last
  356. * reference and were waiting on the rest of the
  357. * group to complete. Since it's no longer part of a
  358. * group, simply free the request */
  359. nfs_page_group_clear_bits(subreq);
  360. nfs_free_request(subreq);
  361. }
  362. }
  363. }
  364. /*
  365. * nfs_lock_and_join_requests - join all subreqs to the head req and return
  366. * a locked reference, cancelling any pending
  367. * operations for this page.
  368. *
  369. * @page - the page used to lookup the "page group" of nfs_page structures
  370. * @nonblock - if true, don't block waiting for request locks
  371. *
  372. * This function joins all sub requests to the head request by first
  373. * locking all requests in the group, cancelling any pending operations
  374. * and finally updating the head request to cover the whole range covered by
  375. * the (former) group. All subrequests are removed from any write or commit
  376. * lists, unlinked from the group and destroyed.
  377. *
  378. * Returns a locked, referenced pointer to the head request - which after
  379. * this call is guaranteed to be the only request associated with the page.
  380. * Returns NULL if no requests are found for @page, or a ERR_PTR if an
  381. * error was encountered.
  382. */
  383. static struct nfs_page *
  384. nfs_lock_and_join_requests(struct page *page, bool nonblock)
  385. {
  386. struct inode *inode = page_file_mapping(page)->host;
  387. struct nfs_page *head, *subreq;
  388. struct nfs_page *destroy_list = NULL;
  389. unsigned int total_bytes;
  390. int ret;
  391. try_again:
  392. total_bytes = 0;
  393. WARN_ON_ONCE(destroy_list);
  394. spin_lock(&inode->i_lock);
  395. /*
  396. * A reference is taken only on the head request which acts as a
  397. * reference to the whole page group - the group will not be destroyed
  398. * until the head reference is released.
  399. */
  400. head = nfs_page_find_head_request_locked(NFS_I(inode), page);
  401. if (!head) {
  402. spin_unlock(&inode->i_lock);
  403. return NULL;
  404. }
  405. /* holding inode lock, so always make a non-blocking call to try the
  406. * page group lock */
  407. ret = nfs_page_group_lock(head, true);
  408. if (ret < 0) {
  409. spin_unlock(&inode->i_lock);
  410. if (!nonblock && ret == -EAGAIN) {
  411. nfs_page_group_lock_wait(head);
  412. nfs_release_request(head);
  413. goto try_again;
  414. }
  415. nfs_release_request(head);
  416. return ERR_PTR(ret);
  417. }
  418. /* lock each request in the page group */
  419. subreq = head;
  420. do {
  421. /*
  422. * Subrequests are always contiguous, non overlapping
  423. * and in order. If not, it's a programming error.
  424. */
  425. WARN_ON_ONCE(subreq->wb_offset !=
  426. (head->wb_offset + total_bytes));
  427. /* keep track of how many bytes this group covers */
  428. total_bytes += subreq->wb_bytes;
  429. if (!nfs_lock_request(subreq)) {
  430. /* releases page group bit lock and
  431. * inode spin lock and all references */
  432. ret = nfs_unroll_locks_and_wait(inode, head,
  433. subreq, nonblock);
  434. if (ret == 0)
  435. goto try_again;
  436. return ERR_PTR(ret);
  437. }
  438. subreq = subreq->wb_this_page;
  439. } while (subreq != head);
  440. /* Now that all requests are locked, make sure they aren't on any list.
  441. * Commit list removal accounting is done after locks are dropped */
  442. subreq = head;
  443. do {
  444. nfs_clear_request_commit(subreq);
  445. subreq = subreq->wb_this_page;
  446. } while (subreq != head);
  447. /* unlink subrequests from head, destroy them later */
  448. if (head->wb_this_page != head) {
  449. /* destroy list will be terminated by head */
  450. destroy_list = head->wb_this_page;
  451. head->wb_this_page = head;
  452. /* change head request to cover whole range that
  453. * the former page group covered */
  454. head->wb_bytes = total_bytes;
  455. }
  456. /*
  457. * prepare head request to be added to new pgio descriptor
  458. */
  459. nfs_page_group_clear_bits(head);
  460. /*
  461. * some part of the group was still on the inode list - otherwise
  462. * the group wouldn't be involved in async write.
  463. * grab a reference for the head request, iff it needs one.
  464. */
  465. if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
  466. kref_get(&head->wb_kref);
  467. nfs_page_group_unlock(head);
  468. /* drop lock to clean uprequests on destroy list */
  469. spin_unlock(&inode->i_lock);
  470. nfs_destroy_unlinked_subrequests(destroy_list, head);
  471. /* still holds ref on head from nfs_page_find_head_request_locked
  472. * and still has lock on head from lock loop */
  473. return head;
  474. }
  475. /*
  476. * Find an associated nfs write request, and prepare to flush it out
  477. * May return an error if the user signalled nfs_wait_on_request().
  478. */
  479. static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
  480. struct page *page, bool nonblock)
  481. {
  482. struct nfs_page *req;
  483. int ret = 0;
  484. req = nfs_lock_and_join_requests(page, nonblock);
  485. if (!req)
  486. goto out;
  487. ret = PTR_ERR(req);
  488. if (IS_ERR(req))
  489. goto out;
  490. nfs_set_page_writeback(page);
  491. WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
  492. ret = 0;
  493. if (!nfs_pageio_add_request(pgio, req)) {
  494. nfs_redirty_request(req);
  495. ret = pgio->pg_error;
  496. }
  497. out:
  498. return ret;
  499. }
  500. static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
  501. {
  502. struct inode *inode = page_file_mapping(page)->host;
  503. int ret;
  504. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  505. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  506. nfs_pageio_cond_complete(pgio, page_file_index(page));
  507. ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
  508. if (ret == -EAGAIN) {
  509. redirty_page_for_writepage(wbc, page);
  510. ret = 0;
  511. }
  512. return ret;
  513. }
  514. /*
  515. * Write an mmapped page to the server.
  516. */
  517. static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
  518. {
  519. struct nfs_pageio_descriptor pgio;
  520. int err;
  521. nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
  522. false, &nfs_async_write_completion_ops);
  523. err = nfs_do_writepage(page, wbc, &pgio);
  524. nfs_pageio_complete(&pgio);
  525. if (err < 0)
  526. return err;
  527. if (pgio.pg_error < 0)
  528. return pgio.pg_error;
  529. return 0;
  530. }
  531. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  532. {
  533. int ret;
  534. ret = nfs_writepage_locked(page, wbc);
  535. unlock_page(page);
  536. return ret;
  537. }
  538. static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
  539. {
  540. int ret;
  541. ret = nfs_do_writepage(page, wbc, data);
  542. unlock_page(page);
  543. return ret;
  544. }
  545. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  546. {
  547. struct inode *inode = mapping->host;
  548. unsigned long *bitlock = &NFS_I(inode)->flags;
  549. struct nfs_pageio_descriptor pgio;
  550. int err;
  551. /* Stop dirtying of new pages while we sync */
  552. err = wait_on_bit_lock_action(bitlock, NFS_INO_FLUSHING,
  553. nfs_wait_bit_killable, TASK_KILLABLE);
  554. if (err)
  555. goto out_err;
  556. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  557. nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
  558. &nfs_async_write_completion_ops);
  559. err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
  560. nfs_pageio_complete(&pgio);
  561. clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
  562. smp_mb__after_atomic();
  563. wake_up_bit(bitlock, NFS_INO_FLUSHING);
  564. if (err < 0)
  565. goto out_err;
  566. err = pgio.pg_error;
  567. if (err < 0)
  568. goto out_err;
  569. return 0;
  570. out_err:
  571. return err;
  572. }
  573. /*
  574. * Insert a write request into an inode
  575. */
  576. static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  577. {
  578. struct nfs_inode *nfsi = NFS_I(inode);
  579. WARN_ON_ONCE(req->wb_this_page != req);
  580. /* Lock the request! */
  581. nfs_lock_request(req);
  582. spin_lock(&inode->i_lock);
  583. if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  584. inode->i_version++;
  585. /*
  586. * Swap-space should not get truncated. Hence no need to plug the race
  587. * with invalidate/truncate.
  588. */
  589. if (likely(!PageSwapCache(req->wb_page))) {
  590. set_bit(PG_MAPPED, &req->wb_flags);
  591. SetPagePrivate(req->wb_page);
  592. set_page_private(req->wb_page, (unsigned long)req);
  593. }
  594. nfsi->npages++;
  595. /* this a head request for a page group - mark it as having an
  596. * extra reference so sub groups can follow suit */
  597. WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
  598. kref_get(&req->wb_kref);
  599. spin_unlock(&inode->i_lock);
  600. }
  601. /*
  602. * Remove a write request from an inode
  603. */
  604. static void nfs_inode_remove_request(struct nfs_page *req)
  605. {
  606. struct inode *inode = req->wb_context->dentry->d_inode;
  607. struct nfs_inode *nfsi = NFS_I(inode);
  608. struct nfs_page *head;
  609. if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
  610. head = req->wb_head;
  611. spin_lock(&inode->i_lock);
  612. if (likely(!PageSwapCache(head->wb_page))) {
  613. set_page_private(head->wb_page, 0);
  614. ClearPagePrivate(head->wb_page);
  615. clear_bit(PG_MAPPED, &head->wb_flags);
  616. }
  617. nfsi->npages--;
  618. spin_unlock(&inode->i_lock);
  619. }
  620. if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
  621. nfs_release_request(req);
  622. else
  623. WARN_ON_ONCE(1);
  624. }
  625. static void
  626. nfs_mark_request_dirty(struct nfs_page *req)
  627. {
  628. __set_page_dirty_nobuffers(req->wb_page);
  629. }
  630. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  631. /**
  632. * nfs_request_add_commit_list - add request to a commit list
  633. * @req: pointer to a struct nfs_page
  634. * @dst: commit list head
  635. * @cinfo: holds list lock and accounting info
  636. *
  637. * This sets the PG_CLEAN bit, updates the cinfo count of
  638. * number of outstanding requests requiring a commit as well as
  639. * the MM page stats.
  640. *
  641. * The caller must _not_ hold the cinfo->lock, but must be
  642. * holding the nfs_page lock.
  643. */
  644. void
  645. nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
  646. struct nfs_commit_info *cinfo)
  647. {
  648. set_bit(PG_CLEAN, &(req)->wb_flags);
  649. spin_lock(cinfo->lock);
  650. nfs_list_add_request(req, dst);
  651. cinfo->mds->ncommit++;
  652. spin_unlock(cinfo->lock);
  653. if (!cinfo->dreq) {
  654. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  655. inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
  656. BDI_RECLAIMABLE);
  657. __mark_inode_dirty(req->wb_context->dentry->d_inode,
  658. I_DIRTY_DATASYNC);
  659. }
  660. }
  661. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
  662. /**
  663. * nfs_request_remove_commit_list - Remove request from a commit list
  664. * @req: pointer to a nfs_page
  665. * @cinfo: holds list lock and accounting info
  666. *
  667. * This clears the PG_CLEAN bit, and updates the cinfo's count of
  668. * number of outstanding requests requiring a commit
  669. * It does not update the MM page stats.
  670. *
  671. * The caller _must_ hold the cinfo->lock and the nfs_page lock.
  672. */
  673. void
  674. nfs_request_remove_commit_list(struct nfs_page *req,
  675. struct nfs_commit_info *cinfo)
  676. {
  677. if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
  678. return;
  679. nfs_list_remove_request(req);
  680. cinfo->mds->ncommit--;
  681. }
  682. EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
  683. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  684. struct inode *inode)
  685. {
  686. cinfo->lock = &inode->i_lock;
  687. cinfo->mds = &NFS_I(inode)->commit_info;
  688. cinfo->ds = pnfs_get_ds_info(inode);
  689. cinfo->dreq = NULL;
  690. cinfo->completion_ops = &nfs_commit_completion_ops;
  691. }
  692. void nfs_init_cinfo(struct nfs_commit_info *cinfo,
  693. struct inode *inode,
  694. struct nfs_direct_req *dreq)
  695. {
  696. if (dreq)
  697. nfs_init_cinfo_from_dreq(cinfo, dreq);
  698. else
  699. nfs_init_cinfo_from_inode(cinfo, inode);
  700. }
  701. EXPORT_SYMBOL_GPL(nfs_init_cinfo);
  702. /*
  703. * Add a request to the inode's commit list.
  704. */
  705. void
  706. nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
  707. struct nfs_commit_info *cinfo)
  708. {
  709. if (pnfs_mark_request_commit(req, lseg, cinfo))
  710. return;
  711. nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
  712. }
  713. static void
  714. nfs_clear_page_commit(struct page *page)
  715. {
  716. dec_zone_page_state(page, NR_UNSTABLE_NFS);
  717. dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
  718. }
  719. /* Called holding inode (/cinfo) lock */
  720. static void
  721. nfs_clear_request_commit(struct nfs_page *req)
  722. {
  723. if (test_bit(PG_CLEAN, &req->wb_flags)) {
  724. struct inode *inode = req->wb_context->dentry->d_inode;
  725. struct nfs_commit_info cinfo;
  726. nfs_init_cinfo_from_inode(&cinfo, inode);
  727. if (!pnfs_clear_request_commit(req, &cinfo)) {
  728. nfs_request_remove_commit_list(req, &cinfo);
  729. }
  730. nfs_clear_page_commit(req->wb_page);
  731. }
  732. }
  733. int nfs_write_need_commit(struct nfs_pgio_header *hdr)
  734. {
  735. if (hdr->verf.committed == NFS_DATA_SYNC)
  736. return hdr->lseg == NULL;
  737. return hdr->verf.committed != NFS_FILE_SYNC;
  738. }
  739. #else
  740. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  741. struct inode *inode)
  742. {
  743. }
  744. void nfs_init_cinfo(struct nfs_commit_info *cinfo,
  745. struct inode *inode,
  746. struct nfs_direct_req *dreq)
  747. {
  748. }
  749. void
  750. nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
  751. struct nfs_commit_info *cinfo)
  752. {
  753. }
  754. static void
  755. nfs_clear_request_commit(struct nfs_page *req)
  756. {
  757. }
  758. int nfs_write_need_commit(struct nfs_pgio_header *hdr)
  759. {
  760. return 0;
  761. }
  762. #endif
  763. static void nfs_write_completion(struct nfs_pgio_header *hdr)
  764. {
  765. struct nfs_commit_info cinfo;
  766. unsigned long bytes = 0;
  767. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  768. goto out;
  769. nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
  770. while (!list_empty(&hdr->pages)) {
  771. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  772. bytes += req->wb_bytes;
  773. nfs_list_remove_request(req);
  774. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
  775. (hdr->good_bytes < bytes)) {
  776. nfs_set_pageerror(req->wb_page);
  777. nfs_context_set_write_error(req->wb_context, hdr->error);
  778. goto remove_req;
  779. }
  780. if (nfs_write_need_commit(hdr)) {
  781. memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
  782. nfs_mark_request_commit(req, hdr->lseg, &cinfo);
  783. goto next;
  784. }
  785. remove_req:
  786. nfs_inode_remove_request(req);
  787. next:
  788. nfs_unlock_request(req);
  789. nfs_end_page_writeback(req);
  790. nfs_release_request(req);
  791. }
  792. out:
  793. hdr->release(hdr);
  794. }
  795. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  796. unsigned long
  797. nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
  798. {
  799. return cinfo->mds->ncommit;
  800. }
  801. /* cinfo->lock held by caller */
  802. int
  803. nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
  804. struct nfs_commit_info *cinfo, int max)
  805. {
  806. struct nfs_page *req, *tmp;
  807. int ret = 0;
  808. list_for_each_entry_safe(req, tmp, src, wb_list) {
  809. if (!nfs_lock_request(req))
  810. continue;
  811. kref_get(&req->wb_kref);
  812. if (cond_resched_lock(cinfo->lock))
  813. list_safe_reset_next(req, tmp, wb_list);
  814. nfs_request_remove_commit_list(req, cinfo);
  815. nfs_list_add_request(req, dst);
  816. ret++;
  817. if ((ret == max) && !cinfo->dreq)
  818. break;
  819. }
  820. return ret;
  821. }
  822. /*
  823. * nfs_scan_commit - Scan an inode for commit requests
  824. * @inode: NFS inode to scan
  825. * @dst: mds destination list
  826. * @cinfo: mds and ds lists of reqs ready to commit
  827. *
  828. * Moves requests from the inode's 'commit' request list.
  829. * The requests are *not* checked to ensure that they form a contiguous set.
  830. */
  831. int
  832. nfs_scan_commit(struct inode *inode, struct list_head *dst,
  833. struct nfs_commit_info *cinfo)
  834. {
  835. int ret = 0;
  836. spin_lock(cinfo->lock);
  837. if (cinfo->mds->ncommit > 0) {
  838. const int max = INT_MAX;
  839. ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
  840. cinfo, max);
  841. ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
  842. }
  843. spin_unlock(cinfo->lock);
  844. return ret;
  845. }
  846. #else
  847. unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
  848. {
  849. return 0;
  850. }
  851. int nfs_scan_commit(struct inode *inode, struct list_head *dst,
  852. struct nfs_commit_info *cinfo)
  853. {
  854. return 0;
  855. }
  856. #endif
  857. /*
  858. * Search for an existing write request, and attempt to update
  859. * it to reflect a new dirty region on a given page.
  860. *
  861. * If the attempt fails, then the existing request is flushed out
  862. * to disk.
  863. */
  864. static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
  865. struct page *page,
  866. unsigned int offset,
  867. unsigned int bytes)
  868. {
  869. struct nfs_page *req;
  870. unsigned int rqend;
  871. unsigned int end;
  872. int error;
  873. if (!PagePrivate(page))
  874. return NULL;
  875. end = offset + bytes;
  876. spin_lock(&inode->i_lock);
  877. for (;;) {
  878. req = nfs_page_find_head_request_locked(NFS_I(inode), page);
  879. if (req == NULL)
  880. goto out_unlock;
  881. /* should be handled by nfs_flush_incompatible */
  882. WARN_ON_ONCE(req->wb_head != req);
  883. WARN_ON_ONCE(req->wb_this_page != req);
  884. rqend = req->wb_offset + req->wb_bytes;
  885. /*
  886. * Tell the caller to flush out the request if
  887. * the offsets are non-contiguous.
  888. * Note: nfs_flush_incompatible() will already
  889. * have flushed out requests having wrong owners.
  890. */
  891. if (offset > rqend
  892. || end < req->wb_offset)
  893. goto out_flushme;
  894. if (nfs_lock_request(req))
  895. break;
  896. /* The request is locked, so wait and then retry */
  897. spin_unlock(&inode->i_lock);
  898. error = nfs_wait_on_request(req);
  899. nfs_release_request(req);
  900. if (error != 0)
  901. goto out_err;
  902. spin_lock(&inode->i_lock);
  903. }
  904. /* Okay, the request matches. Update the region */
  905. if (offset < req->wb_offset) {
  906. req->wb_offset = offset;
  907. req->wb_pgbase = offset;
  908. }
  909. if (end > rqend)
  910. req->wb_bytes = end - req->wb_offset;
  911. else
  912. req->wb_bytes = rqend - req->wb_offset;
  913. out_unlock:
  914. if (req)
  915. nfs_clear_request_commit(req);
  916. spin_unlock(&inode->i_lock);
  917. return req;
  918. out_flushme:
  919. spin_unlock(&inode->i_lock);
  920. nfs_release_request(req);
  921. error = nfs_wb_page(inode, page);
  922. out_err:
  923. return ERR_PTR(error);
  924. }
  925. /*
  926. * Try to update an existing write request, or create one if there is none.
  927. *
  928. * Note: Should always be called with the Page Lock held to prevent races
  929. * if we have to add a new request. Also assumes that the caller has
  930. * already called nfs_flush_incompatible() if necessary.
  931. */
  932. static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
  933. struct page *page, unsigned int offset, unsigned int bytes)
  934. {
  935. struct inode *inode = page_file_mapping(page)->host;
  936. struct nfs_page *req;
  937. req = nfs_try_to_update_request(inode, page, offset, bytes);
  938. if (req != NULL)
  939. goto out;
  940. req = nfs_create_request(ctx, page, NULL, offset, bytes);
  941. if (IS_ERR(req))
  942. goto out;
  943. nfs_inode_add_request(inode, req);
  944. out:
  945. return req;
  946. }
  947. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  948. unsigned int offset, unsigned int count)
  949. {
  950. struct nfs_page *req;
  951. req = nfs_setup_write_request(ctx, page, offset, count);
  952. if (IS_ERR(req))
  953. return PTR_ERR(req);
  954. /* Update file length */
  955. nfs_grow_file(page, offset, count);
  956. nfs_mark_uptodate(req);
  957. nfs_mark_request_dirty(req);
  958. nfs_unlock_and_release_request(req);
  959. return 0;
  960. }
  961. int nfs_flush_incompatible(struct file *file, struct page *page)
  962. {
  963. struct nfs_open_context *ctx = nfs_file_open_context(file);
  964. struct nfs_lock_context *l_ctx;
  965. struct nfs_page *req;
  966. int do_flush, status;
  967. /*
  968. * Look for a request corresponding to this page. If there
  969. * is one, and it belongs to another file, we flush it out
  970. * before we try to copy anything into the page. Do this
  971. * due to the lack of an ACCESS-type call in NFSv2.
  972. * Also do the same if we find a request from an existing
  973. * dropped page.
  974. */
  975. do {
  976. req = nfs_page_find_head_request(page);
  977. if (req == NULL)
  978. return 0;
  979. l_ctx = req->wb_lock_context;
  980. do_flush = req->wb_page != page || req->wb_context != ctx;
  981. /* for now, flush if more than 1 request in page_group */
  982. do_flush |= req->wb_this_page != req;
  983. if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
  984. do_flush |= l_ctx->lockowner.l_owner != current->files
  985. || l_ctx->lockowner.l_pid != current->tgid;
  986. }
  987. nfs_release_request(req);
  988. if (!do_flush)
  989. return 0;
  990. status = nfs_wb_page(page_file_mapping(page)->host, page);
  991. } while (status == 0);
  992. return status;
  993. }
  994. /*
  995. * Avoid buffered writes when a open context credential's key would
  996. * expire soon.
  997. *
  998. * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
  999. *
  1000. * Return 0 and set a credential flag which triggers the inode to flush
  1001. * and performs NFS_FILE_SYNC writes if the key will expired within
  1002. * RPC_KEY_EXPIRE_TIMEO.
  1003. */
  1004. int
  1005. nfs_key_timeout_notify(struct file *filp, struct inode *inode)
  1006. {
  1007. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  1008. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  1009. return rpcauth_key_timeout_notify(auth, ctx->cred);
  1010. }
  1011. /*
  1012. * Test if the open context credential key is marked to expire soon.
  1013. */
  1014. bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
  1015. {
  1016. return rpcauth_cred_key_to_expire(ctx->cred);
  1017. }
  1018. /*
  1019. * If the page cache is marked as unsafe or invalid, then we can't rely on
  1020. * the PageUptodate() flag. In this case, we will need to turn off
  1021. * write optimisations that depend on the page contents being correct.
  1022. */
  1023. static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
  1024. {
  1025. struct nfs_inode *nfsi = NFS_I(inode);
  1026. if (nfs_have_delegated_attributes(inode))
  1027. goto out;
  1028. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  1029. return false;
  1030. smp_rmb();
  1031. if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
  1032. return false;
  1033. out:
  1034. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  1035. return false;
  1036. return PageUptodate(page) != 0;
  1037. }
  1038. /* If we know the page is up to date, and we're not using byte range locks (or
  1039. * if we have the whole file locked for writing), it may be more efficient to
  1040. * extend the write to cover the entire page in order to avoid fragmentation
  1041. * inefficiencies.
  1042. *
  1043. * If the file is opened for synchronous writes then we can just skip the rest
  1044. * of the checks.
  1045. */
  1046. static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
  1047. {
  1048. if (file->f_flags & O_DSYNC)
  1049. return 0;
  1050. if (!nfs_write_pageuptodate(page, inode))
  1051. return 0;
  1052. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  1053. return 1;
  1054. if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
  1055. inode->i_flock->fl_end == OFFSET_MAX &&
  1056. inode->i_flock->fl_type != F_RDLCK))
  1057. return 1;
  1058. return 0;
  1059. }
  1060. /*
  1061. * Update and possibly write a cached page of an NFS file.
  1062. *
  1063. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  1064. * things with a page scheduled for an RPC call (e.g. invalidate it).
  1065. */
  1066. int nfs_updatepage(struct file *file, struct page *page,
  1067. unsigned int offset, unsigned int count)
  1068. {
  1069. struct nfs_open_context *ctx = nfs_file_open_context(file);
  1070. struct inode *inode = page_file_mapping(page)->host;
  1071. int status = 0;
  1072. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  1073. dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
  1074. file, count, (long long)(page_file_offset(page) + offset));
  1075. if (nfs_can_extend_write(file, page, inode)) {
  1076. count = max(count + offset, nfs_page_length(page));
  1077. offset = 0;
  1078. }
  1079. status = nfs_writepage_setup(ctx, page, offset, count);
  1080. if (status < 0)
  1081. nfs_set_pageerror(page);
  1082. else
  1083. __set_page_dirty_nobuffers(page);
  1084. dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
  1085. status, (long long)i_size_read(inode));
  1086. return status;
  1087. }
  1088. static int flush_task_priority(int how)
  1089. {
  1090. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  1091. case FLUSH_HIGHPRI:
  1092. return RPC_PRIORITY_HIGH;
  1093. case FLUSH_LOWPRI:
  1094. return RPC_PRIORITY_LOW;
  1095. }
  1096. return RPC_PRIORITY_NORMAL;
  1097. }
  1098. static void nfs_initiate_write(struct nfs_pgio_header *hdr,
  1099. struct rpc_message *msg,
  1100. struct rpc_task_setup *task_setup_data, int how)
  1101. {
  1102. struct inode *inode = hdr->inode;
  1103. int priority = flush_task_priority(how);
  1104. task_setup_data->priority = priority;
  1105. NFS_PROTO(inode)->write_setup(hdr, msg);
  1106. nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
  1107. &task_setup_data->rpc_client, msg, hdr);
  1108. }
  1109. /* If a nfs_flush_* function fails, it should remove reqs from @head and
  1110. * call this on each, which will prepare them to be retried on next
  1111. * writeback using standard nfs.
  1112. */
  1113. static void nfs_redirty_request(struct nfs_page *req)
  1114. {
  1115. nfs_mark_request_dirty(req);
  1116. nfs_unlock_request(req);
  1117. nfs_end_page_writeback(req);
  1118. nfs_release_request(req);
  1119. }
  1120. static void nfs_async_write_error(struct list_head *head)
  1121. {
  1122. struct nfs_page *req;
  1123. while (!list_empty(head)) {
  1124. req = nfs_list_entry(head->next);
  1125. nfs_list_remove_request(req);
  1126. nfs_redirty_request(req);
  1127. }
  1128. }
  1129. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
  1130. .error_cleanup = nfs_async_write_error,
  1131. .completion = nfs_write_completion,
  1132. };
  1133. void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
  1134. struct inode *inode, int ioflags, bool force_mds,
  1135. const struct nfs_pgio_completion_ops *compl_ops)
  1136. {
  1137. struct nfs_server *server = NFS_SERVER(inode);
  1138. const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
  1139. #ifdef CONFIG_NFS_V4_1
  1140. if (server->pnfs_curr_ld && !force_mds)
  1141. pg_ops = server->pnfs_curr_ld->pg_write_ops;
  1142. #endif
  1143. nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
  1144. server->wsize, ioflags);
  1145. }
  1146. EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
  1147. void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
  1148. {
  1149. pgio->pg_ops = &nfs_pgio_rw_ops;
  1150. pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
  1151. }
  1152. EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
  1153. void nfs_commit_prepare(struct rpc_task *task, void *calldata)
  1154. {
  1155. struct nfs_commit_data *data = calldata;
  1156. NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
  1157. }
  1158. static void nfs_writeback_release_common(struct nfs_pgio_header *hdr)
  1159. {
  1160. /* do nothing! */
  1161. }
  1162. /*
  1163. * Special version of should_remove_suid() that ignores capabilities.
  1164. */
  1165. static int nfs_should_remove_suid(const struct inode *inode)
  1166. {
  1167. umode_t mode = inode->i_mode;
  1168. int kill = 0;
  1169. /* suid always must be killed */
  1170. if (unlikely(mode & S_ISUID))
  1171. kill = ATTR_KILL_SUID;
  1172. /*
  1173. * sgid without any exec bits is just a mandatory locking mark; leave
  1174. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1175. */
  1176. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1177. kill |= ATTR_KILL_SGID;
  1178. if (unlikely(kill && S_ISREG(mode)))
  1179. return kill;
  1180. return 0;
  1181. }
  1182. /*
  1183. * This function is called when the WRITE call is complete.
  1184. */
  1185. static int nfs_writeback_done(struct rpc_task *task,
  1186. struct nfs_pgio_header *hdr,
  1187. struct inode *inode)
  1188. {
  1189. int status;
  1190. /*
  1191. * ->write_done will attempt to use post-op attributes to detect
  1192. * conflicting writes by other clients. A strict interpretation
  1193. * of close-to-open would allow us to continue caching even if
  1194. * another writer had changed the file, but some applications
  1195. * depend on tighter cache coherency when writing.
  1196. */
  1197. status = NFS_PROTO(inode)->write_done(task, hdr);
  1198. if (status != 0)
  1199. return status;
  1200. nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
  1201. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  1202. if (hdr->res.verf->committed < hdr->args.stable &&
  1203. task->tk_status >= 0) {
  1204. /* We tried a write call, but the server did not
  1205. * commit data to stable storage even though we
  1206. * requested it.
  1207. * Note: There is a known bug in Tru64 < 5.0 in which
  1208. * the server reports NFS_DATA_SYNC, but performs
  1209. * NFS_FILE_SYNC. We therefore implement this checking
  1210. * as a dprintk() in order to avoid filling syslog.
  1211. */
  1212. static unsigned long complain;
  1213. /* Note this will print the MDS for a DS write */
  1214. if (time_before(complain, jiffies)) {
  1215. dprintk("NFS: faulty NFS server %s:"
  1216. " (committed = %d) != (stable = %d)\n",
  1217. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1218. hdr->res.verf->committed, hdr->args.stable);
  1219. complain = jiffies + 300 * HZ;
  1220. }
  1221. }
  1222. #endif
  1223. /* Deal with the suid/sgid bit corner case */
  1224. if (nfs_should_remove_suid(inode))
  1225. nfs_mark_for_revalidate(inode);
  1226. return 0;
  1227. }
  1228. /*
  1229. * This function is called when the WRITE call is complete.
  1230. */
  1231. static void nfs_writeback_result(struct rpc_task *task,
  1232. struct nfs_pgio_header *hdr)
  1233. {
  1234. struct nfs_pgio_args *argp = &hdr->args;
  1235. struct nfs_pgio_res *resp = &hdr->res;
  1236. if (resp->count < argp->count) {
  1237. static unsigned long complain;
  1238. /* This a short write! */
  1239. nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
  1240. /* Has the server at least made some progress? */
  1241. if (resp->count == 0) {
  1242. if (time_before(complain, jiffies)) {
  1243. printk(KERN_WARNING
  1244. "NFS: Server wrote zero bytes, expected %u.\n",
  1245. argp->count);
  1246. complain = jiffies + 300 * HZ;
  1247. }
  1248. nfs_set_pgio_error(hdr, -EIO, argp->offset);
  1249. task->tk_status = -EIO;
  1250. return;
  1251. }
  1252. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1253. if (resp->verf->committed != NFS_UNSTABLE) {
  1254. /* Resend from where the server left off */
  1255. hdr->mds_offset += resp->count;
  1256. argp->offset += resp->count;
  1257. argp->pgbase += resp->count;
  1258. argp->count -= resp->count;
  1259. } else {
  1260. /* Resend as a stable write in order to avoid
  1261. * headaches in the case of a server crash.
  1262. */
  1263. argp->stable = NFS_FILE_SYNC;
  1264. }
  1265. rpc_restart_call_prepare(task);
  1266. }
  1267. }
  1268. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  1269. static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
  1270. {
  1271. int ret;
  1272. if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
  1273. return 1;
  1274. if (!may_wait)
  1275. return 0;
  1276. ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
  1277. NFS_INO_COMMIT,
  1278. nfs_wait_bit_killable,
  1279. TASK_KILLABLE);
  1280. return (ret < 0) ? ret : 1;
  1281. }
  1282. static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
  1283. {
  1284. clear_bit(NFS_INO_COMMIT, &nfsi->flags);
  1285. smp_mb__after_atomic();
  1286. wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
  1287. }
  1288. void nfs_commitdata_release(struct nfs_commit_data *data)
  1289. {
  1290. put_nfs_open_context(data->context);
  1291. nfs_commit_free(data);
  1292. }
  1293. EXPORT_SYMBOL_GPL(nfs_commitdata_release);
  1294. int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
  1295. const struct rpc_call_ops *call_ops,
  1296. int how, int flags)
  1297. {
  1298. struct rpc_task *task;
  1299. int priority = flush_task_priority(how);
  1300. struct rpc_message msg = {
  1301. .rpc_argp = &data->args,
  1302. .rpc_resp = &data->res,
  1303. .rpc_cred = data->cred,
  1304. };
  1305. struct rpc_task_setup task_setup_data = {
  1306. .task = &data->task,
  1307. .rpc_client = clnt,
  1308. .rpc_message = &msg,
  1309. .callback_ops = call_ops,
  1310. .callback_data = data,
  1311. .workqueue = nfsiod_workqueue,
  1312. .flags = RPC_TASK_ASYNC | flags,
  1313. .priority = priority,
  1314. };
  1315. /* Set up the initial task struct. */
  1316. NFS_PROTO(data->inode)->commit_setup(data, &msg);
  1317. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  1318. nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
  1319. NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
  1320. task = rpc_run_task(&task_setup_data);
  1321. if (IS_ERR(task))
  1322. return PTR_ERR(task);
  1323. if (how & FLUSH_SYNC)
  1324. rpc_wait_for_completion_task(task);
  1325. rpc_put_task(task);
  1326. return 0;
  1327. }
  1328. EXPORT_SYMBOL_GPL(nfs_initiate_commit);
  1329. /*
  1330. * Set up the argument/result storage required for the RPC call.
  1331. */
  1332. void nfs_init_commit(struct nfs_commit_data *data,
  1333. struct list_head *head,
  1334. struct pnfs_layout_segment *lseg,
  1335. struct nfs_commit_info *cinfo)
  1336. {
  1337. struct nfs_page *first = nfs_list_entry(head->next);
  1338. struct inode *inode = first->wb_context->dentry->d_inode;
  1339. /* Set up the RPC argument and reply structs
  1340. * NB: take care not to mess about with data->commit et al. */
  1341. list_splice_init(head, &data->pages);
  1342. data->inode = inode;
  1343. data->cred = first->wb_context->cred;
  1344. data->lseg = lseg; /* reference transferred */
  1345. data->mds_ops = &nfs_commit_ops;
  1346. data->completion_ops = cinfo->completion_ops;
  1347. data->dreq = cinfo->dreq;
  1348. data->args.fh = NFS_FH(data->inode);
  1349. /* Note: we always request a commit of the entire inode */
  1350. data->args.offset = 0;
  1351. data->args.count = 0;
  1352. data->context = get_nfs_open_context(first->wb_context);
  1353. data->res.fattr = &data->fattr;
  1354. data->res.verf = &data->verf;
  1355. nfs_fattr_init(&data->fattr);
  1356. }
  1357. EXPORT_SYMBOL_GPL(nfs_init_commit);
  1358. void nfs_retry_commit(struct list_head *page_list,
  1359. struct pnfs_layout_segment *lseg,
  1360. struct nfs_commit_info *cinfo)
  1361. {
  1362. struct nfs_page *req;
  1363. while (!list_empty(page_list)) {
  1364. req = nfs_list_entry(page_list->next);
  1365. nfs_list_remove_request(req);
  1366. nfs_mark_request_commit(req, lseg, cinfo);
  1367. if (!cinfo->dreq) {
  1368. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1369. dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
  1370. BDI_RECLAIMABLE);
  1371. }
  1372. nfs_unlock_and_release_request(req);
  1373. }
  1374. }
  1375. EXPORT_SYMBOL_GPL(nfs_retry_commit);
  1376. /*
  1377. * Commit dirty pages
  1378. */
  1379. static int
  1380. nfs_commit_list(struct inode *inode, struct list_head *head, int how,
  1381. struct nfs_commit_info *cinfo)
  1382. {
  1383. struct nfs_commit_data *data;
  1384. data = nfs_commitdata_alloc();
  1385. if (!data)
  1386. goto out_bad;
  1387. /* Set up the argument struct */
  1388. nfs_init_commit(data, head, NULL, cinfo);
  1389. atomic_inc(&cinfo->mds->rpcs_out);
  1390. return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
  1391. how, 0);
  1392. out_bad:
  1393. nfs_retry_commit(head, NULL, cinfo);
  1394. cinfo->completion_ops->error_cleanup(NFS_I(inode));
  1395. return -ENOMEM;
  1396. }
  1397. /*
  1398. * COMMIT call returned
  1399. */
  1400. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1401. {
  1402. struct nfs_commit_data *data = calldata;
  1403. dprintk("NFS: %5u nfs_commit_done (status %d)\n",
  1404. task->tk_pid, task->tk_status);
  1405. /* Call the NFS version-specific code */
  1406. NFS_PROTO(data->inode)->commit_done(task, data);
  1407. }
  1408. static void nfs_commit_release_pages(struct nfs_commit_data *data)
  1409. {
  1410. struct nfs_page *req;
  1411. int status = data->task.tk_status;
  1412. struct nfs_commit_info cinfo;
  1413. while (!list_empty(&data->pages)) {
  1414. req = nfs_list_entry(data->pages.next);
  1415. nfs_list_remove_request(req);
  1416. nfs_clear_page_commit(req->wb_page);
  1417. dprintk("NFS: commit (%s/%llu %d@%lld)",
  1418. req->wb_context->dentry->d_sb->s_id,
  1419. (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1420. req->wb_bytes,
  1421. (long long)req_offset(req));
  1422. if (status < 0) {
  1423. nfs_context_set_write_error(req->wb_context, status);
  1424. nfs_inode_remove_request(req);
  1425. dprintk(", error = %d\n", status);
  1426. goto next;
  1427. }
  1428. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1429. * returned by the server against all stored verfs. */
  1430. if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
  1431. /* We have a match */
  1432. nfs_inode_remove_request(req);
  1433. dprintk(" OK\n");
  1434. goto next;
  1435. }
  1436. /* We have a mismatch. Write the page again */
  1437. dprintk(" mismatch\n");
  1438. nfs_mark_request_dirty(req);
  1439. set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
  1440. next:
  1441. nfs_unlock_and_release_request(req);
  1442. }
  1443. nfs_init_cinfo(&cinfo, data->inode, data->dreq);
  1444. if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
  1445. nfs_commit_clear_lock(NFS_I(data->inode));
  1446. }
  1447. static void nfs_commit_release(void *calldata)
  1448. {
  1449. struct nfs_commit_data *data = calldata;
  1450. data->completion_ops->completion(data);
  1451. nfs_commitdata_release(calldata);
  1452. }
  1453. static const struct rpc_call_ops nfs_commit_ops = {
  1454. .rpc_call_prepare = nfs_commit_prepare,
  1455. .rpc_call_done = nfs_commit_done,
  1456. .rpc_release = nfs_commit_release,
  1457. };
  1458. static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
  1459. .completion = nfs_commit_release_pages,
  1460. .error_cleanup = nfs_commit_clear_lock,
  1461. };
  1462. int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
  1463. int how, struct nfs_commit_info *cinfo)
  1464. {
  1465. int status;
  1466. status = pnfs_commit_list(inode, head, how, cinfo);
  1467. if (status == PNFS_NOT_ATTEMPTED)
  1468. status = nfs_commit_list(inode, head, how, cinfo);
  1469. return status;
  1470. }
  1471. int nfs_commit_inode(struct inode *inode, int how)
  1472. {
  1473. LIST_HEAD(head);
  1474. struct nfs_commit_info cinfo;
  1475. int may_wait = how & FLUSH_SYNC;
  1476. int res;
  1477. res = nfs_commit_set_lock(NFS_I(inode), may_wait);
  1478. if (res <= 0)
  1479. goto out_mark_dirty;
  1480. nfs_init_cinfo_from_inode(&cinfo, inode);
  1481. res = nfs_scan_commit(inode, &head, &cinfo);
  1482. if (res) {
  1483. int error;
  1484. error = nfs_generic_commit_list(inode, &head, how, &cinfo);
  1485. if (error < 0)
  1486. return error;
  1487. if (!may_wait)
  1488. goto out_mark_dirty;
  1489. error = wait_on_bit_action(&NFS_I(inode)->flags,
  1490. NFS_INO_COMMIT,
  1491. nfs_wait_bit_killable,
  1492. TASK_KILLABLE);
  1493. if (error < 0)
  1494. return error;
  1495. } else
  1496. nfs_commit_clear_lock(NFS_I(inode));
  1497. return res;
  1498. /* Note: If we exit without ensuring that the commit is complete,
  1499. * we must mark the inode as dirty. Otherwise, future calls to
  1500. * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
  1501. * that the data is on the disk.
  1502. */
  1503. out_mark_dirty:
  1504. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1505. return res;
  1506. }
  1507. static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
  1508. {
  1509. struct nfs_inode *nfsi = NFS_I(inode);
  1510. int flags = FLUSH_SYNC;
  1511. int ret = 0;
  1512. /* no commits means nothing needs to be done */
  1513. if (!nfsi->commit_info.ncommit)
  1514. return ret;
  1515. if (wbc->sync_mode == WB_SYNC_NONE) {
  1516. /* Don't commit yet if this is a non-blocking flush and there
  1517. * are a lot of outstanding writes for this mapping.
  1518. */
  1519. if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
  1520. goto out_mark_dirty;
  1521. /* don't wait for the COMMIT response */
  1522. flags = 0;
  1523. }
  1524. ret = nfs_commit_inode(inode, flags);
  1525. if (ret >= 0) {
  1526. if (wbc->sync_mode == WB_SYNC_NONE) {
  1527. if (ret < wbc->nr_to_write)
  1528. wbc->nr_to_write -= ret;
  1529. else
  1530. wbc->nr_to_write = 0;
  1531. }
  1532. return 0;
  1533. }
  1534. out_mark_dirty:
  1535. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1536. return ret;
  1537. }
  1538. #else
  1539. static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
  1540. {
  1541. return 0;
  1542. }
  1543. #endif
  1544. int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1545. {
  1546. return nfs_commit_unstable_pages(inode, wbc);
  1547. }
  1548. EXPORT_SYMBOL_GPL(nfs_write_inode);
  1549. /*
  1550. * flush the inode to disk.
  1551. */
  1552. int nfs_wb_all(struct inode *inode)
  1553. {
  1554. struct writeback_control wbc = {
  1555. .sync_mode = WB_SYNC_ALL,
  1556. .nr_to_write = LONG_MAX,
  1557. .range_start = 0,
  1558. .range_end = LLONG_MAX,
  1559. };
  1560. int ret;
  1561. trace_nfs_writeback_inode_enter(inode);
  1562. ret = sync_inode(inode, &wbc);
  1563. trace_nfs_writeback_inode_exit(inode, ret);
  1564. return ret;
  1565. }
  1566. EXPORT_SYMBOL_GPL(nfs_wb_all);
  1567. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1568. {
  1569. struct nfs_page *req;
  1570. int ret = 0;
  1571. wait_on_page_writeback(page);
  1572. /* blocking call to cancel all requests and join to a single (head)
  1573. * request */
  1574. req = nfs_lock_and_join_requests(page, false);
  1575. if (IS_ERR(req)) {
  1576. ret = PTR_ERR(req);
  1577. } else if (req) {
  1578. /* all requests from this page have been cancelled by
  1579. * nfs_lock_and_join_requests, so just remove the head
  1580. * request from the inode / page_private pointer and
  1581. * release it */
  1582. nfs_inode_remove_request(req);
  1583. /*
  1584. * In case nfs_inode_remove_request has marked the
  1585. * page as being dirty
  1586. */
  1587. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  1588. nfs_unlock_and_release_request(req);
  1589. }
  1590. return ret;
  1591. }
  1592. /*
  1593. * Write back all requests on one page - we do this before reading it.
  1594. */
  1595. int nfs_wb_page(struct inode *inode, struct page *page)
  1596. {
  1597. loff_t range_start = page_file_offset(page);
  1598. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1599. struct writeback_control wbc = {
  1600. .sync_mode = WB_SYNC_ALL,
  1601. .nr_to_write = 0,
  1602. .range_start = range_start,
  1603. .range_end = range_end,
  1604. };
  1605. int ret;
  1606. trace_nfs_writeback_page_enter(inode);
  1607. for (;;) {
  1608. wait_on_page_writeback(page);
  1609. if (clear_page_dirty_for_io(page)) {
  1610. ret = nfs_writepage_locked(page, &wbc);
  1611. if (ret < 0)
  1612. goto out_error;
  1613. continue;
  1614. }
  1615. ret = 0;
  1616. if (!PagePrivate(page))
  1617. break;
  1618. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1619. if (ret < 0)
  1620. goto out_error;
  1621. }
  1622. out_error:
  1623. trace_nfs_writeback_page_exit(inode, ret);
  1624. return ret;
  1625. }
  1626. #ifdef CONFIG_MIGRATION
  1627. int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
  1628. struct page *page, enum migrate_mode mode)
  1629. {
  1630. /*
  1631. * If PagePrivate is set, then the page is currently associated with
  1632. * an in-progress read or write request. Don't try to migrate it.
  1633. *
  1634. * FIXME: we could do this in principle, but we'll need a way to ensure
  1635. * that we can safely release the inode reference while holding
  1636. * the page lock.
  1637. */
  1638. if (PagePrivate(page))
  1639. return -EBUSY;
  1640. if (!nfs_fscache_release_page(page, GFP_KERNEL))
  1641. return -EBUSY;
  1642. return migrate_page(mapping, newpage, page, mode);
  1643. }
  1644. #endif
  1645. int __init nfs_init_writepagecache(void)
  1646. {
  1647. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1648. sizeof(struct nfs_pgio_header),
  1649. 0, SLAB_HWCACHE_ALIGN,
  1650. NULL);
  1651. if (nfs_wdata_cachep == NULL)
  1652. return -ENOMEM;
  1653. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1654. nfs_wdata_cachep);
  1655. if (nfs_wdata_mempool == NULL)
  1656. goto out_destroy_write_cache;
  1657. nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
  1658. sizeof(struct nfs_commit_data),
  1659. 0, SLAB_HWCACHE_ALIGN,
  1660. NULL);
  1661. if (nfs_cdata_cachep == NULL)
  1662. goto out_destroy_write_mempool;
  1663. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1664. nfs_cdata_cachep);
  1665. if (nfs_commit_mempool == NULL)
  1666. goto out_destroy_commit_cache;
  1667. /*
  1668. * NFS congestion size, scale with available memory.
  1669. *
  1670. * 64MB: 8192k
  1671. * 128MB: 11585k
  1672. * 256MB: 16384k
  1673. * 512MB: 23170k
  1674. * 1GB: 32768k
  1675. * 2GB: 46340k
  1676. * 4GB: 65536k
  1677. * 8GB: 92681k
  1678. * 16GB: 131072k
  1679. *
  1680. * This allows larger machines to have larger/more transfers.
  1681. * Limit the default to 256M
  1682. */
  1683. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1684. if (nfs_congestion_kb > 256*1024)
  1685. nfs_congestion_kb = 256*1024;
  1686. return 0;
  1687. out_destroy_commit_cache:
  1688. kmem_cache_destroy(nfs_cdata_cachep);
  1689. out_destroy_write_mempool:
  1690. mempool_destroy(nfs_wdata_mempool);
  1691. out_destroy_write_cache:
  1692. kmem_cache_destroy(nfs_wdata_cachep);
  1693. return -ENOMEM;
  1694. }
  1695. void nfs_destroy_writepagecache(void)
  1696. {
  1697. mempool_destroy(nfs_commit_mempool);
  1698. kmem_cache_destroy(nfs_cdata_cachep);
  1699. mempool_destroy(nfs_wdata_mempool);
  1700. kmem_cache_destroy(nfs_wdata_cachep);
  1701. }
  1702. static const struct nfs_rw_ops nfs_rw_write_ops = {
  1703. .rw_mode = FMODE_WRITE,
  1704. .rw_alloc_header = nfs_writehdr_alloc,
  1705. .rw_free_header = nfs_writehdr_free,
  1706. .rw_release = nfs_writeback_release_common,
  1707. .rw_done = nfs_writeback_done,
  1708. .rw_result = nfs_writeback_result,
  1709. .rw_initiate = nfs_initiate_write,
  1710. };