write.c 51 KB

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