write.c 51 KB

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