write.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  1. /*
  2. * linux/fs/nfs/write.c
  3. *
  4. * Writing file data over NFS.
  5. *
  6. * We do it like this: When a (user) process wishes to write data to an
  7. * NFS file, a write request is allocated that contains the RPC task data
  8. * plus some info on the page to be written, and added to the inode's
  9. * write chain. If the process writes past the end of the page, an async
  10. * RPC call to write the page is scheduled immediately; otherwise, the call
  11. * is delayed for a few seconds.
  12. *
  13. * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
  14. *
  15. * Write requests are kept on the inode's writeback list. Each entry in
  16. * that list references the page (portion) to be written. When the
  17. * cache timeout has expired, the RPC task is woken up, and tries to
  18. * lock the page. As soon as it manages to do so, the request is moved
  19. * from the writeback list to the writelock list.
  20. *
  21. * Note: we must make sure never to confuse the inode passed in the
  22. * write_page request with the one in page->inode. As far as I understand
  23. * it, these are different when doing a swap-out.
  24. *
  25. * To understand everything that goes on here and in the NFS read code,
  26. * one should be aware that a page is locked in exactly one of the following
  27. * cases:
  28. *
  29. * - A write request is in progress.
  30. * - A user process is in generic_file_write/nfs_update_page
  31. * - A user process is in generic_file_read
  32. *
  33. * Also note that because of the way pages are invalidated in
  34. * nfs_revalidate_inode, the following assertions hold:
  35. *
  36. * - If a page is dirty, there will be no read requests (a page will
  37. * not be re-read unless invalidated by nfs_revalidate_inode).
  38. * - If the page is not uptodate, there will be no pending write
  39. * requests, and no process will be in nfs_update_page.
  40. *
  41. * FIXME: Interaction with the vmscan routines is not optimal yet.
  42. * Either vmscan must be made nfs-savvy, or we need a different page
  43. * reclaim concept that supports something like FS-independent
  44. * buffer_heads with a b_ops-> field.
  45. *
  46. * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
  47. */
  48. #include <linux/types.h>
  49. #include <linux/slab.h>
  50. #include <linux/mm.h>
  51. #include <linux/pagemap.h>
  52. #include <linux/file.h>
  53. #include <linux/writeback.h>
  54. #include <linux/sunrpc/clnt.h>
  55. #include <linux/nfs_fs.h>
  56. #include <linux/nfs_mount.h>
  57. #include <linux/nfs_page.h>
  58. #include <linux/backing-dev.h>
  59. #include <asm/uaccess.h>
  60. #include <linux/smp_lock.h>
  61. #include "delegation.h"
  62. #include "internal.h"
  63. #include "iostat.h"
  64. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  65. #define MIN_POOL_WRITE (32)
  66. #define MIN_POOL_COMMIT (4)
  67. /*
  68. * Local function declarations
  69. */
  70. static struct nfs_page * nfs_update_request(struct nfs_open_context*,
  71. struct page *,
  72. unsigned int, unsigned int);
  73. static int nfs_wait_on_write_congestion(struct address_space *, int);
  74. static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
  75. static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
  76. static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how);
  77. static const struct rpc_call_ops nfs_write_partial_ops;
  78. static const struct rpc_call_ops nfs_write_full_ops;
  79. static const struct rpc_call_ops nfs_commit_ops;
  80. static kmem_cache_t *nfs_wdata_cachep;
  81. static mempool_t *nfs_wdata_mempool;
  82. static mempool_t *nfs_commit_mempool;
  83. static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
  84. struct nfs_write_data *nfs_commit_alloc(void)
  85. {
  86. struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
  87. if (p) {
  88. memset(p, 0, sizeof(*p));
  89. INIT_LIST_HEAD(&p->pages);
  90. }
  91. return p;
  92. }
  93. void nfs_commit_rcu_free(struct rcu_head *head)
  94. {
  95. struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
  96. if (p && (p->pagevec != &p->page_array[0]))
  97. kfree(p->pagevec);
  98. mempool_free(p, nfs_commit_mempool);
  99. }
  100. void nfs_commit_free(struct nfs_write_data *wdata)
  101. {
  102. call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
  103. }
  104. struct nfs_write_data *nfs_writedata_alloc(size_t len)
  105. {
  106. unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  107. struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
  108. if (p) {
  109. memset(p, 0, sizeof(*p));
  110. INIT_LIST_HEAD(&p->pages);
  111. p->npages = pagecount;
  112. if (pagecount <= ARRAY_SIZE(p->page_array))
  113. p->pagevec = p->page_array;
  114. else {
  115. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
  116. if (!p->pagevec) {
  117. mempool_free(p, nfs_wdata_mempool);
  118. p = NULL;
  119. }
  120. }
  121. }
  122. return p;
  123. }
  124. static void nfs_writedata_rcu_free(struct rcu_head *head)
  125. {
  126. struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
  127. if (p && (p->pagevec != &p->page_array[0]))
  128. kfree(p->pagevec);
  129. mempool_free(p, nfs_wdata_mempool);
  130. }
  131. static void nfs_writedata_free(struct nfs_write_data *wdata)
  132. {
  133. call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
  134. }
  135. void nfs_writedata_release(void *wdata)
  136. {
  137. nfs_writedata_free(wdata);
  138. }
  139. static struct nfs_page *nfs_page_find_request_locked(struct page *page)
  140. {
  141. struct nfs_page *req = NULL;
  142. if (PagePrivate(page)) {
  143. req = (struct nfs_page *)page_private(page);
  144. if (req != NULL)
  145. atomic_inc(&req->wb_count);
  146. }
  147. return req;
  148. }
  149. static struct nfs_page *nfs_page_find_request(struct page *page)
  150. {
  151. struct nfs_page *req = NULL;
  152. spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
  153. spin_lock(req_lock);
  154. req = nfs_page_find_request_locked(page);
  155. spin_unlock(req_lock);
  156. return req;
  157. }
  158. /* Adjust the file length if we're writing beyond the end */
  159. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  160. {
  161. struct inode *inode = page->mapping->host;
  162. loff_t end, i_size = i_size_read(inode);
  163. unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
  164. if (i_size > 0 && page->index < end_index)
  165. return;
  166. end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
  167. if (i_size >= end)
  168. return;
  169. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  170. i_size_write(inode, end);
  171. }
  172. /* We can set the PG_uptodate flag if we see that a write request
  173. * covers the full page.
  174. */
  175. static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
  176. {
  177. if (PageUptodate(page))
  178. return;
  179. if (base != 0)
  180. return;
  181. if (count != nfs_page_length(page))
  182. return;
  183. if (count != PAGE_CACHE_SIZE)
  184. memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
  185. SetPageUptodate(page);
  186. }
  187. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  188. unsigned int offset, unsigned int count)
  189. {
  190. struct nfs_page *req;
  191. int ret;
  192. for (;;) {
  193. req = nfs_update_request(ctx, page, offset, count);
  194. if (!IS_ERR(req))
  195. break;
  196. ret = PTR_ERR(req);
  197. if (ret != -EBUSY)
  198. return ret;
  199. ret = nfs_wb_page(page->mapping->host, page);
  200. if (ret != 0)
  201. return ret;
  202. }
  203. /* Update file length */
  204. nfs_grow_file(page, offset, count);
  205. /* Set the PG_uptodate flag? */
  206. nfs_mark_uptodate(page, offset, count);
  207. nfs_unlock_request(req);
  208. return 0;
  209. }
  210. static int wb_priority(struct writeback_control *wbc)
  211. {
  212. if (wbc->for_reclaim)
  213. return FLUSH_HIGHPRI;
  214. if (wbc->for_kupdate)
  215. return FLUSH_LOWPRI;
  216. return 0;
  217. }
  218. /*
  219. * Write an mmapped page to the server.
  220. */
  221. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  222. {
  223. struct nfs_open_context *ctx;
  224. struct inode *inode = page->mapping->host;
  225. struct nfs_page *req;
  226. unsigned offset;
  227. int err = 0;
  228. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  229. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  230. req = nfs_page_find_request(page);
  231. if (req != NULL) {
  232. int flushme = test_bit(PG_NEED_FLUSH, &req->wb_flags);
  233. nfs_release_request(req);
  234. if (!flushme)
  235. goto out;
  236. /* Ensure we've flushed out the invalid write */
  237. nfs_wb_page_priority(inode, page, wb_priority(wbc));
  238. }
  239. offset = nfs_page_length(page);
  240. if (!offset)
  241. goto out;
  242. ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
  243. if (ctx == NULL) {
  244. err = -EBADF;
  245. goto out;
  246. }
  247. err = nfs_writepage_setup(ctx, page, 0, offset);
  248. put_nfs_open_context(ctx);
  249. out:
  250. if (!wbc->for_writepages)
  251. nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
  252. unlock_page(page);
  253. return err;
  254. }
  255. /*
  256. * Note: causes nfs_update_request() to block on the assumption
  257. * that the writeback is generated due to memory pressure.
  258. */
  259. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  260. {
  261. struct backing_dev_info *bdi = mapping->backing_dev_info;
  262. struct inode *inode = mapping->host;
  263. int err;
  264. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  265. err = generic_writepages(mapping, wbc);
  266. if (err)
  267. return err;
  268. while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
  269. if (wbc->nonblocking)
  270. return 0;
  271. nfs_wait_on_write_congestion(mapping, 0);
  272. }
  273. err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
  274. if (err < 0)
  275. goto out;
  276. nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
  277. if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
  278. err = nfs_wait_on_requests(inode, 0, 0);
  279. if (err < 0)
  280. goto out;
  281. }
  282. err = nfs_commit_inode(inode, wb_priority(wbc));
  283. if (err > 0)
  284. err = 0;
  285. out:
  286. clear_bit(BDI_write_congested, &bdi->state);
  287. wake_up_all(&nfs_write_congestion);
  288. congestion_end(WRITE);
  289. return err;
  290. }
  291. /*
  292. * Insert a write request into an inode
  293. */
  294. static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  295. {
  296. struct nfs_inode *nfsi = NFS_I(inode);
  297. int error;
  298. error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
  299. BUG_ON(error == -EEXIST);
  300. if (error)
  301. return error;
  302. if (!nfsi->npages) {
  303. igrab(inode);
  304. nfs_begin_data_update(inode);
  305. if (nfs_have_delegation(inode, FMODE_WRITE))
  306. nfsi->change_attr++;
  307. }
  308. SetPagePrivate(req->wb_page);
  309. set_page_private(req->wb_page, (unsigned long)req);
  310. nfsi->npages++;
  311. atomic_inc(&req->wb_count);
  312. return 0;
  313. }
  314. /*
  315. * Insert a write request into an inode
  316. */
  317. static void nfs_inode_remove_request(struct nfs_page *req)
  318. {
  319. struct inode *inode = req->wb_context->dentry->d_inode;
  320. struct nfs_inode *nfsi = NFS_I(inode);
  321. BUG_ON (!NFS_WBACK_BUSY(req));
  322. spin_lock(&nfsi->req_lock);
  323. set_page_private(req->wb_page, 0);
  324. ClearPagePrivate(req->wb_page);
  325. radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
  326. nfsi->npages--;
  327. if (!nfsi->npages) {
  328. spin_unlock(&nfsi->req_lock);
  329. nfs_end_data_update(inode);
  330. iput(inode);
  331. } else
  332. spin_unlock(&nfsi->req_lock);
  333. nfs_clear_request(req);
  334. nfs_release_request(req);
  335. }
  336. /*
  337. * Add a request to the inode's dirty list.
  338. */
  339. static void
  340. nfs_mark_request_dirty(struct nfs_page *req)
  341. {
  342. struct inode *inode = req->wb_context->dentry->d_inode;
  343. struct nfs_inode *nfsi = NFS_I(inode);
  344. spin_lock(&nfsi->req_lock);
  345. radix_tree_tag_set(&nfsi->nfs_page_tree,
  346. req->wb_index, NFS_PAGE_TAG_DIRTY);
  347. nfs_list_add_request(req, &nfsi->dirty);
  348. nfsi->ndirty++;
  349. spin_unlock(&nfsi->req_lock);
  350. inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
  351. mark_inode_dirty(inode);
  352. }
  353. /*
  354. * Check if a request is dirty
  355. */
  356. static inline int
  357. nfs_dirty_request(struct nfs_page *req)
  358. {
  359. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  360. return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
  361. }
  362. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  363. /*
  364. * Add a request to the inode's commit list.
  365. */
  366. static void
  367. nfs_mark_request_commit(struct nfs_page *req)
  368. {
  369. struct inode *inode = req->wb_context->dentry->d_inode;
  370. struct nfs_inode *nfsi = NFS_I(inode);
  371. spin_lock(&nfsi->req_lock);
  372. nfs_list_add_request(req, &nfsi->commit);
  373. nfsi->ncommit++;
  374. spin_unlock(&nfsi->req_lock);
  375. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  376. mark_inode_dirty(inode);
  377. }
  378. #endif
  379. /*
  380. * Wait for a request to complete.
  381. *
  382. * Interruptible by signals only if mounted with intr flag.
  383. */
  384. static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
  385. {
  386. struct nfs_inode *nfsi = NFS_I(inode);
  387. struct nfs_page *req;
  388. unsigned long idx_end, next;
  389. unsigned int res = 0;
  390. int error;
  391. if (npages == 0)
  392. idx_end = ~0;
  393. else
  394. idx_end = idx_start + npages - 1;
  395. next = idx_start;
  396. while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
  397. if (req->wb_index > idx_end)
  398. break;
  399. next = req->wb_index + 1;
  400. BUG_ON(!NFS_WBACK_BUSY(req));
  401. atomic_inc(&req->wb_count);
  402. spin_unlock(&nfsi->req_lock);
  403. error = nfs_wait_on_request(req);
  404. nfs_release_request(req);
  405. spin_lock(&nfsi->req_lock);
  406. if (error < 0)
  407. return error;
  408. res++;
  409. }
  410. return res;
  411. }
  412. static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
  413. {
  414. struct nfs_inode *nfsi = NFS_I(inode);
  415. int ret;
  416. spin_lock(&nfsi->req_lock);
  417. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  418. spin_unlock(&nfsi->req_lock);
  419. return ret;
  420. }
  421. static void nfs_cancel_dirty_list(struct list_head *head)
  422. {
  423. struct nfs_page *req;
  424. while(!list_empty(head)) {
  425. req = nfs_list_entry(head->next);
  426. nfs_list_remove_request(req);
  427. nfs_inode_remove_request(req);
  428. nfs_clear_page_writeback(req);
  429. }
  430. }
  431. static void nfs_cancel_commit_list(struct list_head *head)
  432. {
  433. struct nfs_page *req;
  434. while(!list_empty(head)) {
  435. req = nfs_list_entry(head->next);
  436. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  437. nfs_list_remove_request(req);
  438. nfs_inode_remove_request(req);
  439. nfs_unlock_request(req);
  440. }
  441. }
  442. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  443. /*
  444. * nfs_scan_commit - Scan an inode for commit requests
  445. * @inode: NFS inode to scan
  446. * @dst: destination list
  447. * @idx_start: lower bound of page->index to scan.
  448. * @npages: idx_start + npages sets the upper bound to scan.
  449. *
  450. * Moves requests from the inode's 'commit' request list.
  451. * The requests are *not* checked to ensure that they form a contiguous set.
  452. */
  453. static int
  454. nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  455. {
  456. struct nfs_inode *nfsi = NFS_I(inode);
  457. int res = 0;
  458. if (nfsi->ncommit != 0) {
  459. res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
  460. nfsi->ncommit -= res;
  461. if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
  462. printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
  463. }
  464. return res;
  465. }
  466. #else
  467. static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  468. {
  469. return 0;
  470. }
  471. #endif
  472. static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
  473. {
  474. struct backing_dev_info *bdi = mapping->backing_dev_info;
  475. DEFINE_WAIT(wait);
  476. int ret = 0;
  477. might_sleep();
  478. if (!bdi_write_congested(bdi))
  479. return 0;
  480. nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
  481. if (intr) {
  482. struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
  483. sigset_t oldset;
  484. rpc_clnt_sigmask(clnt, &oldset);
  485. prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
  486. if (bdi_write_congested(bdi)) {
  487. if (signalled())
  488. ret = -ERESTARTSYS;
  489. else
  490. schedule();
  491. }
  492. rpc_clnt_sigunmask(clnt, &oldset);
  493. } else {
  494. prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
  495. if (bdi_write_congested(bdi))
  496. schedule();
  497. }
  498. finish_wait(&nfs_write_congestion, &wait);
  499. return ret;
  500. }
  501. /*
  502. * Try to update any existing write request, or create one if there is none.
  503. * In order to match, the request's credentials must match those of
  504. * the calling process.
  505. *
  506. * Note: Should always be called with the Page Lock held!
  507. */
  508. static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
  509. struct page *page, unsigned int offset, unsigned int bytes)
  510. {
  511. struct inode *inode = page->mapping->host;
  512. struct nfs_inode *nfsi = NFS_I(inode);
  513. struct nfs_page *req, *new = NULL;
  514. unsigned long rqend, end;
  515. end = offset + bytes;
  516. if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
  517. return ERR_PTR(-ERESTARTSYS);
  518. for (;;) {
  519. /* Loop over all inode entries and see if we find
  520. * A request for the page we wish to update
  521. */
  522. spin_lock(&nfsi->req_lock);
  523. req = nfs_page_find_request_locked(page);
  524. if (req) {
  525. if (!nfs_lock_request_dontget(req)) {
  526. int error;
  527. spin_unlock(&nfsi->req_lock);
  528. error = nfs_wait_on_request(req);
  529. nfs_release_request(req);
  530. if (error < 0) {
  531. if (new)
  532. nfs_release_request(new);
  533. return ERR_PTR(error);
  534. }
  535. continue;
  536. }
  537. spin_unlock(&nfsi->req_lock);
  538. if (new)
  539. nfs_release_request(new);
  540. break;
  541. }
  542. if (new) {
  543. int error;
  544. nfs_lock_request_dontget(new);
  545. error = nfs_inode_add_request(inode, new);
  546. if (error) {
  547. spin_unlock(&nfsi->req_lock);
  548. nfs_unlock_request(new);
  549. return ERR_PTR(error);
  550. }
  551. spin_unlock(&nfsi->req_lock);
  552. nfs_mark_request_dirty(new);
  553. return new;
  554. }
  555. spin_unlock(&nfsi->req_lock);
  556. new = nfs_create_request(ctx, inode, page, offset, bytes);
  557. if (IS_ERR(new))
  558. return new;
  559. }
  560. /* We have a request for our page.
  561. * If the creds don't match, or the
  562. * page addresses don't match,
  563. * tell the caller to wait on the conflicting
  564. * request.
  565. */
  566. rqend = req->wb_offset + req->wb_bytes;
  567. if (req->wb_context != ctx
  568. || req->wb_page != page
  569. || !nfs_dirty_request(req)
  570. || offset > rqend || end < req->wb_offset) {
  571. nfs_unlock_request(req);
  572. return ERR_PTR(-EBUSY);
  573. }
  574. /* Okay, the request matches. Update the region */
  575. if (offset < req->wb_offset) {
  576. req->wb_offset = offset;
  577. req->wb_pgbase = offset;
  578. req->wb_bytes = rqend - req->wb_offset;
  579. }
  580. if (end > rqend)
  581. req->wb_bytes = end - req->wb_offset;
  582. return req;
  583. }
  584. int nfs_flush_incompatible(struct file *file, struct page *page)
  585. {
  586. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  587. struct nfs_page *req;
  588. int do_flush, status;
  589. /*
  590. * Look for a request corresponding to this page. If there
  591. * is one, and it belongs to another file, we flush it out
  592. * before we try to copy anything into the page. Do this
  593. * due to the lack of an ACCESS-type call in NFSv2.
  594. * Also do the same if we find a request from an existing
  595. * dropped page.
  596. */
  597. do {
  598. req = nfs_page_find_request(page);
  599. if (req == NULL)
  600. return 0;
  601. do_flush = req->wb_page != page || req->wb_context != ctx
  602. || test_bit(PG_NEED_FLUSH, &req->wb_flags);
  603. nfs_release_request(req);
  604. if (!do_flush)
  605. return 0;
  606. status = nfs_wb_page(page->mapping->host, page);
  607. } while (status == 0);
  608. return status;
  609. }
  610. /*
  611. * Update and possibly write a cached page of an NFS file.
  612. *
  613. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  614. * things with a page scheduled for an RPC call (e.g. invalidate it).
  615. */
  616. int nfs_updatepage(struct file *file, struct page *page,
  617. unsigned int offset, unsigned int count)
  618. {
  619. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  620. struct inode *inode = page->mapping->host;
  621. int status = 0;
  622. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  623. dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
  624. file->f_dentry->d_parent->d_name.name,
  625. file->f_dentry->d_name.name, count,
  626. (long long)(page_offset(page) +offset));
  627. /* If we're not using byte range locks, and we know the page
  628. * is entirely in cache, it may be more efficient to avoid
  629. * fragmenting write requests.
  630. */
  631. if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
  632. count = max(count + offset, nfs_page_length(page));
  633. offset = 0;
  634. }
  635. status = nfs_writepage_setup(ctx, page, offset, count);
  636. dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
  637. status, (long long)i_size_read(inode));
  638. if (status < 0)
  639. ClearPageUptodate(page);
  640. return status;
  641. }
  642. static void nfs_writepage_release(struct nfs_page *req)
  643. {
  644. end_page_writeback(req->wb_page);
  645. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  646. if (!PageError(req->wb_page)) {
  647. if (NFS_NEED_RESCHED(req)) {
  648. nfs_mark_request_dirty(req);
  649. goto out;
  650. } else if (NFS_NEED_COMMIT(req)) {
  651. nfs_mark_request_commit(req);
  652. goto out;
  653. }
  654. }
  655. nfs_inode_remove_request(req);
  656. out:
  657. nfs_clear_commit(req);
  658. nfs_clear_reschedule(req);
  659. #else
  660. nfs_inode_remove_request(req);
  661. #endif
  662. nfs_clear_page_writeback(req);
  663. }
  664. static inline int flush_task_priority(int how)
  665. {
  666. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  667. case FLUSH_HIGHPRI:
  668. return RPC_PRIORITY_HIGH;
  669. case FLUSH_LOWPRI:
  670. return RPC_PRIORITY_LOW;
  671. }
  672. return RPC_PRIORITY_NORMAL;
  673. }
  674. /*
  675. * Set up the argument/result storage required for the RPC call.
  676. */
  677. static void nfs_write_rpcsetup(struct nfs_page *req,
  678. struct nfs_write_data *data,
  679. const struct rpc_call_ops *call_ops,
  680. unsigned int count, unsigned int offset,
  681. int how)
  682. {
  683. struct inode *inode;
  684. int flags;
  685. /* Set up the RPC argument and reply structs
  686. * NB: take care not to mess about with data->commit et al. */
  687. data->req = req;
  688. data->inode = inode = req->wb_context->dentry->d_inode;
  689. data->cred = req->wb_context->cred;
  690. data->args.fh = NFS_FH(inode);
  691. data->args.offset = req_offset(req) + offset;
  692. data->args.pgbase = req->wb_pgbase + offset;
  693. data->args.pages = data->pagevec;
  694. data->args.count = count;
  695. data->args.context = req->wb_context;
  696. data->res.fattr = &data->fattr;
  697. data->res.count = count;
  698. data->res.verf = &data->verf;
  699. nfs_fattr_init(&data->fattr);
  700. /* Set up the initial task struct. */
  701. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  702. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
  703. NFS_PROTO(inode)->write_setup(data, how);
  704. data->task.tk_priority = flush_task_priority(how);
  705. data->task.tk_cookie = (unsigned long)inode;
  706. dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  707. data->task.tk_pid,
  708. inode->i_sb->s_id,
  709. (long long)NFS_FILEID(inode),
  710. count,
  711. (unsigned long long)data->args.offset);
  712. }
  713. static void nfs_execute_write(struct nfs_write_data *data)
  714. {
  715. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  716. sigset_t oldset;
  717. rpc_clnt_sigmask(clnt, &oldset);
  718. rpc_execute(&data->task);
  719. rpc_clnt_sigunmask(clnt, &oldset);
  720. }
  721. /*
  722. * Generate multiple small requests to write out a single
  723. * contiguous dirty area on one page.
  724. */
  725. static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
  726. {
  727. struct nfs_page *req = nfs_list_entry(head->next);
  728. struct page *page = req->wb_page;
  729. struct nfs_write_data *data;
  730. size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
  731. unsigned int offset;
  732. int requests = 0;
  733. LIST_HEAD(list);
  734. nfs_list_remove_request(req);
  735. nbytes = req->wb_bytes;
  736. do {
  737. size_t len = min(nbytes, wsize);
  738. data = nfs_writedata_alloc(len);
  739. if (!data)
  740. goto out_bad;
  741. list_add(&data->pages, &list);
  742. requests++;
  743. nbytes -= len;
  744. } while (nbytes != 0);
  745. atomic_set(&req->wb_complete, requests);
  746. ClearPageError(page);
  747. set_page_writeback(page);
  748. offset = 0;
  749. nbytes = req->wb_bytes;
  750. do {
  751. data = list_entry(list.next, struct nfs_write_data, pages);
  752. list_del_init(&data->pages);
  753. data->pagevec[0] = page;
  754. if (nbytes > wsize) {
  755. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  756. wsize, offset, how);
  757. offset += wsize;
  758. nbytes -= wsize;
  759. } else {
  760. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  761. nbytes, offset, how);
  762. nbytes = 0;
  763. }
  764. nfs_execute_write(data);
  765. } while (nbytes != 0);
  766. return 0;
  767. out_bad:
  768. while (!list_empty(&list)) {
  769. data = list_entry(list.next, struct nfs_write_data, pages);
  770. list_del(&data->pages);
  771. nfs_writedata_release(data);
  772. }
  773. nfs_mark_request_dirty(req);
  774. nfs_clear_page_writeback(req);
  775. return -ENOMEM;
  776. }
  777. /*
  778. * Create an RPC task for the given write request and kick it.
  779. * The page must have been locked by the caller.
  780. *
  781. * It may happen that the page we're passed is not marked dirty.
  782. * This is the case if nfs_updatepage detects a conflicting request
  783. * that has been written but not committed.
  784. */
  785. static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
  786. {
  787. struct nfs_page *req;
  788. struct page **pages;
  789. struct nfs_write_data *data;
  790. unsigned int count;
  791. data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
  792. if (!data)
  793. goto out_bad;
  794. pages = data->pagevec;
  795. count = 0;
  796. while (!list_empty(head)) {
  797. req = nfs_list_entry(head->next);
  798. nfs_list_remove_request(req);
  799. nfs_list_add_request(req, &data->pages);
  800. ClearPageError(req->wb_page);
  801. set_page_writeback(req->wb_page);
  802. *pages++ = req->wb_page;
  803. count += req->wb_bytes;
  804. }
  805. req = nfs_list_entry(data->pages.next);
  806. /* Set up the argument struct */
  807. nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
  808. nfs_execute_write(data);
  809. return 0;
  810. out_bad:
  811. while (!list_empty(head)) {
  812. struct nfs_page *req = nfs_list_entry(head->next);
  813. nfs_list_remove_request(req);
  814. nfs_mark_request_dirty(req);
  815. nfs_clear_page_writeback(req);
  816. }
  817. return -ENOMEM;
  818. }
  819. static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
  820. {
  821. LIST_HEAD(one_request);
  822. int (*flush_one)(struct inode *, struct list_head *, int);
  823. struct nfs_page *req;
  824. int wpages = NFS_SERVER(inode)->wpages;
  825. int wsize = NFS_SERVER(inode)->wsize;
  826. int error;
  827. flush_one = nfs_flush_one;
  828. if (wsize < PAGE_CACHE_SIZE)
  829. flush_one = nfs_flush_multi;
  830. /* For single writes, FLUSH_STABLE is more efficient */
  831. if (npages <= wpages && npages == NFS_I(inode)->npages
  832. && nfs_list_entry(head->next)->wb_bytes <= wsize)
  833. how |= FLUSH_STABLE;
  834. do {
  835. nfs_coalesce_requests(head, &one_request, wpages);
  836. req = nfs_list_entry(one_request.next);
  837. error = flush_one(inode, &one_request, how);
  838. if (error < 0)
  839. goto out_err;
  840. } while (!list_empty(head));
  841. return 0;
  842. out_err:
  843. while (!list_empty(head)) {
  844. req = nfs_list_entry(head->next);
  845. nfs_list_remove_request(req);
  846. nfs_mark_request_dirty(req);
  847. nfs_clear_page_writeback(req);
  848. }
  849. return error;
  850. }
  851. /*
  852. * Handle a write reply that flushed part of a page.
  853. */
  854. static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
  855. {
  856. struct nfs_write_data *data = calldata;
  857. struct nfs_page *req = data->req;
  858. struct page *page = req->wb_page;
  859. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  860. req->wb_context->dentry->d_inode->i_sb->s_id,
  861. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  862. req->wb_bytes,
  863. (long long)req_offset(req));
  864. if (nfs_writeback_done(task, data) != 0)
  865. return;
  866. if (task->tk_status < 0) {
  867. ClearPageUptodate(page);
  868. SetPageError(page);
  869. req->wb_context->error = task->tk_status;
  870. dprintk(", error = %d\n", task->tk_status);
  871. } else {
  872. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  873. if (data->verf.committed < NFS_FILE_SYNC) {
  874. if (!NFS_NEED_COMMIT(req)) {
  875. nfs_defer_commit(req);
  876. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  877. dprintk(" defer commit\n");
  878. } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
  879. nfs_defer_reschedule(req);
  880. dprintk(" server reboot detected\n");
  881. }
  882. } else
  883. #endif
  884. dprintk(" OK\n");
  885. }
  886. if (atomic_dec_and_test(&req->wb_complete))
  887. nfs_writepage_release(req);
  888. }
  889. static const struct rpc_call_ops nfs_write_partial_ops = {
  890. .rpc_call_done = nfs_writeback_done_partial,
  891. .rpc_release = nfs_writedata_release,
  892. };
  893. /*
  894. * Handle a write reply that flushes a whole page.
  895. *
  896. * FIXME: There is an inherent race with invalidate_inode_pages and
  897. * writebacks since the page->count is kept > 1 for as long
  898. * as the page has a write request pending.
  899. */
  900. static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
  901. {
  902. struct nfs_write_data *data = calldata;
  903. struct nfs_page *req;
  904. struct page *page;
  905. if (nfs_writeback_done(task, data) != 0)
  906. return;
  907. /* Update attributes as result of writeback. */
  908. while (!list_empty(&data->pages)) {
  909. req = nfs_list_entry(data->pages.next);
  910. nfs_list_remove_request(req);
  911. page = req->wb_page;
  912. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  913. req->wb_context->dentry->d_inode->i_sb->s_id,
  914. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  915. req->wb_bytes,
  916. (long long)req_offset(req));
  917. if (task->tk_status < 0) {
  918. ClearPageUptodate(page);
  919. SetPageError(page);
  920. req->wb_context->error = task->tk_status;
  921. end_page_writeback(page);
  922. nfs_inode_remove_request(req);
  923. dprintk(", error = %d\n", task->tk_status);
  924. goto next;
  925. }
  926. end_page_writeback(page);
  927. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  928. if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
  929. nfs_inode_remove_request(req);
  930. dprintk(" OK\n");
  931. goto next;
  932. }
  933. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  934. nfs_mark_request_commit(req);
  935. dprintk(" marked for commit\n");
  936. #else
  937. nfs_inode_remove_request(req);
  938. #endif
  939. next:
  940. nfs_clear_page_writeback(req);
  941. }
  942. }
  943. static const struct rpc_call_ops nfs_write_full_ops = {
  944. .rpc_call_done = nfs_writeback_done_full,
  945. .rpc_release = nfs_writedata_release,
  946. };
  947. /*
  948. * This function is called when the WRITE call is complete.
  949. */
  950. int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
  951. {
  952. struct nfs_writeargs *argp = &data->args;
  953. struct nfs_writeres *resp = &data->res;
  954. int status;
  955. dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
  956. task->tk_pid, task->tk_status);
  957. /*
  958. * ->write_done will attempt to use post-op attributes to detect
  959. * conflicting writes by other clients. A strict interpretation
  960. * of close-to-open would allow us to continue caching even if
  961. * another writer had changed the file, but some applications
  962. * depend on tighter cache coherency when writing.
  963. */
  964. status = NFS_PROTO(data->inode)->write_done(task, data);
  965. if (status != 0)
  966. return status;
  967. nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  968. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  969. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  970. /* We tried a write call, but the server did not
  971. * commit data to stable storage even though we
  972. * requested it.
  973. * Note: There is a known bug in Tru64 < 5.0 in which
  974. * the server reports NFS_DATA_SYNC, but performs
  975. * NFS_FILE_SYNC. We therefore implement this checking
  976. * as a dprintk() in order to avoid filling syslog.
  977. */
  978. static unsigned long complain;
  979. if (time_before(complain, jiffies)) {
  980. dprintk("NFS: faulty NFS server %s:"
  981. " (committed = %d) != (stable = %d)\n",
  982. NFS_SERVER(data->inode)->nfs_client->cl_hostname,
  983. resp->verf->committed, argp->stable);
  984. complain = jiffies + 300 * HZ;
  985. }
  986. }
  987. #endif
  988. /* Is this a short write? */
  989. if (task->tk_status >= 0 && resp->count < argp->count) {
  990. static unsigned long complain;
  991. nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
  992. /* Has the server at least made some progress? */
  993. if (resp->count != 0) {
  994. /* Was this an NFSv2 write or an NFSv3 stable write? */
  995. if (resp->verf->committed != NFS_UNSTABLE) {
  996. /* Resend from where the server left off */
  997. argp->offset += resp->count;
  998. argp->pgbase += resp->count;
  999. argp->count -= resp->count;
  1000. } else {
  1001. /* Resend as a stable write in order to avoid
  1002. * headaches in the case of a server crash.
  1003. */
  1004. argp->stable = NFS_FILE_SYNC;
  1005. }
  1006. rpc_restart_call(task);
  1007. return -EAGAIN;
  1008. }
  1009. if (time_before(complain, jiffies)) {
  1010. printk(KERN_WARNING
  1011. "NFS: Server wrote zero bytes, expected %u.\n",
  1012. argp->count);
  1013. complain = jiffies + 300 * HZ;
  1014. }
  1015. /* Can't do anything about it except throw an error. */
  1016. task->tk_status = -EIO;
  1017. }
  1018. return 0;
  1019. }
  1020. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1021. void nfs_commit_release(void *wdata)
  1022. {
  1023. nfs_commit_free(wdata);
  1024. }
  1025. /*
  1026. * Set up the argument/result storage required for the RPC call.
  1027. */
  1028. static void nfs_commit_rpcsetup(struct list_head *head,
  1029. struct nfs_write_data *data,
  1030. int how)
  1031. {
  1032. struct nfs_page *first;
  1033. struct inode *inode;
  1034. int flags;
  1035. /* Set up the RPC argument and reply structs
  1036. * NB: take care not to mess about with data->commit et al. */
  1037. list_splice_init(head, &data->pages);
  1038. first = nfs_list_entry(data->pages.next);
  1039. inode = first->wb_context->dentry->d_inode;
  1040. data->inode = inode;
  1041. data->cred = first->wb_context->cred;
  1042. data->args.fh = NFS_FH(data->inode);
  1043. /* Note: we always request a commit of the entire inode */
  1044. data->args.offset = 0;
  1045. data->args.count = 0;
  1046. data->res.count = 0;
  1047. data->res.fattr = &data->fattr;
  1048. data->res.verf = &data->verf;
  1049. nfs_fattr_init(&data->fattr);
  1050. /* Set up the initial task struct. */
  1051. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1052. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
  1053. NFS_PROTO(inode)->commit_setup(data, how);
  1054. data->task.tk_priority = flush_task_priority(how);
  1055. data->task.tk_cookie = (unsigned long)inode;
  1056. dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
  1057. }
  1058. /*
  1059. * Commit dirty pages
  1060. */
  1061. static int
  1062. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1063. {
  1064. struct nfs_write_data *data;
  1065. struct nfs_page *req;
  1066. data = nfs_commit_alloc();
  1067. if (!data)
  1068. goto out_bad;
  1069. /* Set up the argument struct */
  1070. nfs_commit_rpcsetup(head, data, how);
  1071. nfs_execute_write(data);
  1072. return 0;
  1073. out_bad:
  1074. while (!list_empty(head)) {
  1075. req = nfs_list_entry(head->next);
  1076. nfs_list_remove_request(req);
  1077. nfs_mark_request_commit(req);
  1078. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1079. nfs_clear_page_writeback(req);
  1080. }
  1081. return -ENOMEM;
  1082. }
  1083. /*
  1084. * COMMIT call returned
  1085. */
  1086. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1087. {
  1088. struct nfs_write_data *data = calldata;
  1089. struct nfs_page *req;
  1090. dprintk("NFS: %4d nfs_commit_done (status %d)\n",
  1091. task->tk_pid, task->tk_status);
  1092. /* Call the NFS version-specific code */
  1093. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1094. return;
  1095. while (!list_empty(&data->pages)) {
  1096. req = nfs_list_entry(data->pages.next);
  1097. nfs_list_remove_request(req);
  1098. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1099. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1100. req->wb_context->dentry->d_inode->i_sb->s_id,
  1101. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1102. req->wb_bytes,
  1103. (long long)req_offset(req));
  1104. if (task->tk_status < 0) {
  1105. req->wb_context->error = task->tk_status;
  1106. nfs_inode_remove_request(req);
  1107. dprintk(", error = %d\n", task->tk_status);
  1108. goto next;
  1109. }
  1110. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1111. * returned by the server against all stored verfs. */
  1112. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1113. /* We have a match */
  1114. nfs_inode_remove_request(req);
  1115. dprintk(" OK\n");
  1116. goto next;
  1117. }
  1118. /* We have a mismatch. Write the page again */
  1119. dprintk(" mismatch\n");
  1120. nfs_mark_request_dirty(req);
  1121. next:
  1122. nfs_clear_page_writeback(req);
  1123. }
  1124. }
  1125. static const struct rpc_call_ops nfs_commit_ops = {
  1126. .rpc_call_done = nfs_commit_done,
  1127. .rpc_release = nfs_commit_release,
  1128. };
  1129. #else
  1130. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1131. {
  1132. return 0;
  1133. }
  1134. #endif
  1135. static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
  1136. {
  1137. struct nfs_inode *nfsi = NFS_I(mapping->host);
  1138. LIST_HEAD(head);
  1139. long res;
  1140. spin_lock(&nfsi->req_lock);
  1141. res = nfs_scan_dirty(mapping, wbc, &head);
  1142. spin_unlock(&nfsi->req_lock);
  1143. if (res) {
  1144. int error = nfs_flush_list(mapping->host, &head, res, how);
  1145. if (error < 0)
  1146. return error;
  1147. }
  1148. return res;
  1149. }
  1150. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1151. int nfs_commit_inode(struct inode *inode, int how)
  1152. {
  1153. struct nfs_inode *nfsi = NFS_I(inode);
  1154. LIST_HEAD(head);
  1155. int res;
  1156. spin_lock(&nfsi->req_lock);
  1157. res = nfs_scan_commit(inode, &head, 0, 0);
  1158. spin_unlock(&nfsi->req_lock);
  1159. if (res) {
  1160. int error = nfs_commit_list(inode, &head, how);
  1161. if (error < 0)
  1162. return error;
  1163. }
  1164. return res;
  1165. }
  1166. #endif
  1167. long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
  1168. {
  1169. struct inode *inode = mapping->host;
  1170. struct nfs_inode *nfsi = NFS_I(inode);
  1171. unsigned long idx_start, idx_end;
  1172. unsigned int npages = 0;
  1173. LIST_HEAD(head);
  1174. int nocommit = how & FLUSH_NOCOMMIT;
  1175. long pages, ret;
  1176. /* FIXME */
  1177. if (wbc->range_cyclic)
  1178. idx_start = 0;
  1179. else {
  1180. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  1181. idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1182. if (idx_end > idx_start) {
  1183. unsigned long l_npages = 1 + idx_end - idx_start;
  1184. npages = l_npages;
  1185. if (sizeof(npages) != sizeof(l_npages) &&
  1186. (unsigned long)npages != l_npages)
  1187. npages = 0;
  1188. }
  1189. }
  1190. how &= ~FLUSH_NOCOMMIT;
  1191. spin_lock(&nfsi->req_lock);
  1192. do {
  1193. wbc->pages_skipped = 0;
  1194. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1195. if (ret != 0)
  1196. continue;
  1197. pages = nfs_scan_dirty(mapping, wbc, &head);
  1198. if (pages != 0) {
  1199. spin_unlock(&nfsi->req_lock);
  1200. if (how & FLUSH_INVALIDATE) {
  1201. nfs_cancel_dirty_list(&head);
  1202. ret = pages;
  1203. } else
  1204. ret = nfs_flush_list(inode, &head, pages, how);
  1205. spin_lock(&nfsi->req_lock);
  1206. continue;
  1207. }
  1208. if (wbc->pages_skipped != 0)
  1209. continue;
  1210. if (nocommit)
  1211. break;
  1212. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1213. if (pages == 0) {
  1214. if (wbc->pages_skipped != 0)
  1215. continue;
  1216. break;
  1217. }
  1218. if (how & FLUSH_INVALIDATE) {
  1219. spin_unlock(&nfsi->req_lock);
  1220. nfs_cancel_commit_list(&head);
  1221. ret = pages;
  1222. spin_lock(&nfsi->req_lock);
  1223. continue;
  1224. }
  1225. pages += nfs_scan_commit(inode, &head, 0, 0);
  1226. spin_unlock(&nfsi->req_lock);
  1227. ret = nfs_commit_list(inode, &head, how);
  1228. spin_lock(&nfsi->req_lock);
  1229. } while (ret >= 0);
  1230. spin_unlock(&nfsi->req_lock);
  1231. return ret;
  1232. }
  1233. /*
  1234. * flush the inode to disk.
  1235. */
  1236. int nfs_wb_all(struct inode *inode)
  1237. {
  1238. struct address_space *mapping = inode->i_mapping;
  1239. struct writeback_control wbc = {
  1240. .bdi = mapping->backing_dev_info,
  1241. .sync_mode = WB_SYNC_ALL,
  1242. .nr_to_write = LONG_MAX,
  1243. .range_cyclic = 1,
  1244. };
  1245. int ret;
  1246. ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
  1247. if (ret >= 0)
  1248. return 0;
  1249. return ret;
  1250. }
  1251. int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
  1252. {
  1253. struct writeback_control wbc = {
  1254. .bdi = mapping->backing_dev_info,
  1255. .sync_mode = WB_SYNC_ALL,
  1256. .nr_to_write = LONG_MAX,
  1257. .range_start = range_start,
  1258. .range_end = range_end,
  1259. };
  1260. int ret;
  1261. ret = nfs_sync_mapping_wait(mapping, &wbc, how);
  1262. if (ret >= 0)
  1263. return 0;
  1264. return ret;
  1265. }
  1266. static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
  1267. {
  1268. loff_t range_start = page_offset(page);
  1269. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1270. return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
  1271. }
  1272. /*
  1273. * Write back all requests on one page - we do this before reading it.
  1274. */
  1275. int nfs_wb_page(struct inode *inode, struct page* page)
  1276. {
  1277. return nfs_wb_page_priority(inode, page, 0);
  1278. }
  1279. int nfs_set_page_dirty(struct page *page)
  1280. {
  1281. struct nfs_page *req;
  1282. req = nfs_page_find_request(page);
  1283. if (req != NULL) {
  1284. /* Mark any existing write requests for flushing */
  1285. set_bit(PG_NEED_FLUSH, &req->wb_flags);
  1286. nfs_release_request(req);
  1287. }
  1288. return __set_page_dirty_nobuffers(page);
  1289. }
  1290. int __init nfs_init_writepagecache(void)
  1291. {
  1292. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1293. sizeof(struct nfs_write_data),
  1294. 0, SLAB_HWCACHE_ALIGN,
  1295. NULL, NULL);
  1296. if (nfs_wdata_cachep == NULL)
  1297. return -ENOMEM;
  1298. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1299. nfs_wdata_cachep);
  1300. if (nfs_wdata_mempool == NULL)
  1301. return -ENOMEM;
  1302. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1303. nfs_wdata_cachep);
  1304. if (nfs_commit_mempool == NULL)
  1305. return -ENOMEM;
  1306. return 0;
  1307. }
  1308. void nfs_destroy_writepagecache(void)
  1309. {
  1310. mempool_destroy(nfs_commit_mempool);
  1311. mempool_destroy(nfs_wdata_mempool);
  1312. kmem_cache_destroy(nfs_wdata_cachep);
  1313. }