write.c 53 KB

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