write.c 56 KB

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