write.c 56 KB

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