write.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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. static int nfs_writepage_locked(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) | FLUSH_STABLE | FLUSH_NOWRITEPAGE);
  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. return err;
  253. }
  254. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  255. {
  256. int err;
  257. err = nfs_writepage_locked(page, wbc);
  258. unlock_page(page);
  259. return err;
  260. }
  261. /*
  262. * Note: causes nfs_update_request() to block on the assumption
  263. * that the writeback is generated due to memory pressure.
  264. */
  265. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  266. {
  267. struct backing_dev_info *bdi = mapping->backing_dev_info;
  268. struct inode *inode = mapping->host;
  269. int err;
  270. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  271. err = generic_writepages(mapping, wbc);
  272. if (err)
  273. return err;
  274. while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
  275. if (wbc->nonblocking)
  276. return 0;
  277. nfs_wait_on_write_congestion(mapping, 0);
  278. }
  279. err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
  280. if (err < 0)
  281. goto out;
  282. nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
  283. if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
  284. err = nfs_wait_on_requests(inode, 0, 0);
  285. if (err < 0)
  286. goto out;
  287. }
  288. err = nfs_commit_inode(inode, wb_priority(wbc));
  289. if (err > 0)
  290. err = 0;
  291. out:
  292. clear_bit(BDI_write_congested, &bdi->state);
  293. wake_up_all(&nfs_write_congestion);
  294. congestion_end(WRITE);
  295. return err;
  296. }
  297. /*
  298. * Insert a write request into an inode
  299. */
  300. static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  301. {
  302. struct nfs_inode *nfsi = NFS_I(inode);
  303. int error;
  304. error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
  305. BUG_ON(error == -EEXIST);
  306. if (error)
  307. return error;
  308. if (!nfsi->npages) {
  309. igrab(inode);
  310. nfs_begin_data_update(inode);
  311. if (nfs_have_delegation(inode, FMODE_WRITE))
  312. nfsi->change_attr++;
  313. }
  314. SetPagePrivate(req->wb_page);
  315. set_page_private(req->wb_page, (unsigned long)req);
  316. nfsi->npages++;
  317. atomic_inc(&req->wb_count);
  318. return 0;
  319. }
  320. /*
  321. * Insert a write request into an inode
  322. */
  323. static void nfs_inode_remove_request(struct nfs_page *req)
  324. {
  325. struct inode *inode = req->wb_context->dentry->d_inode;
  326. struct nfs_inode *nfsi = NFS_I(inode);
  327. BUG_ON (!NFS_WBACK_BUSY(req));
  328. spin_lock(&nfsi->req_lock);
  329. set_page_private(req->wb_page, 0);
  330. ClearPagePrivate(req->wb_page);
  331. radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
  332. nfsi->npages--;
  333. if (!nfsi->npages) {
  334. spin_unlock(&nfsi->req_lock);
  335. nfs_end_data_update(inode);
  336. iput(inode);
  337. } else
  338. spin_unlock(&nfsi->req_lock);
  339. nfs_clear_request(req);
  340. nfs_release_request(req);
  341. }
  342. /*
  343. * Add a request to the inode's dirty list.
  344. */
  345. static void
  346. nfs_mark_request_dirty(struct nfs_page *req)
  347. {
  348. struct inode *inode = req->wb_context->dentry->d_inode;
  349. struct nfs_inode *nfsi = NFS_I(inode);
  350. spin_lock(&nfsi->req_lock);
  351. radix_tree_tag_set(&nfsi->nfs_page_tree,
  352. req->wb_index, NFS_PAGE_TAG_DIRTY);
  353. nfs_list_add_request(req, &nfsi->dirty);
  354. nfsi->ndirty++;
  355. spin_unlock(&nfsi->req_lock);
  356. inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
  357. mark_inode_dirty(inode);
  358. }
  359. /*
  360. * Check if a request is dirty
  361. */
  362. static inline int
  363. nfs_dirty_request(struct nfs_page *req)
  364. {
  365. struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
  366. return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
  367. }
  368. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  369. /*
  370. * Add a request to the inode's commit list.
  371. */
  372. static void
  373. nfs_mark_request_commit(struct nfs_page *req)
  374. {
  375. struct inode *inode = req->wb_context->dentry->d_inode;
  376. struct nfs_inode *nfsi = NFS_I(inode);
  377. spin_lock(&nfsi->req_lock);
  378. nfs_list_add_request(req, &nfsi->commit);
  379. nfsi->ncommit++;
  380. spin_unlock(&nfsi->req_lock);
  381. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  382. mark_inode_dirty(inode);
  383. }
  384. #endif
  385. /*
  386. * Wait for a request to complete.
  387. *
  388. * Interruptible by signals only if mounted with intr flag.
  389. */
  390. static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
  391. {
  392. struct nfs_inode *nfsi = NFS_I(inode);
  393. struct nfs_page *req;
  394. unsigned long idx_end, next;
  395. unsigned int res = 0;
  396. int error;
  397. if (npages == 0)
  398. idx_end = ~0;
  399. else
  400. idx_end = idx_start + npages - 1;
  401. next = idx_start;
  402. while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
  403. if (req->wb_index > idx_end)
  404. break;
  405. next = req->wb_index + 1;
  406. BUG_ON(!NFS_WBACK_BUSY(req));
  407. atomic_inc(&req->wb_count);
  408. spin_unlock(&nfsi->req_lock);
  409. error = nfs_wait_on_request(req);
  410. nfs_release_request(req);
  411. spin_lock(&nfsi->req_lock);
  412. if (error < 0)
  413. return error;
  414. res++;
  415. }
  416. return res;
  417. }
  418. static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
  419. {
  420. struct nfs_inode *nfsi = NFS_I(inode);
  421. int ret;
  422. spin_lock(&nfsi->req_lock);
  423. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  424. spin_unlock(&nfsi->req_lock);
  425. return ret;
  426. }
  427. static void nfs_cancel_dirty_list(struct list_head *head)
  428. {
  429. struct nfs_page *req;
  430. while(!list_empty(head)) {
  431. req = nfs_list_entry(head->next);
  432. nfs_list_remove_request(req);
  433. nfs_inode_remove_request(req);
  434. nfs_clear_page_writeback(req);
  435. }
  436. }
  437. static void nfs_cancel_commit_list(struct list_head *head)
  438. {
  439. struct nfs_page *req;
  440. while(!list_empty(head)) {
  441. req = nfs_list_entry(head->next);
  442. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  443. nfs_list_remove_request(req);
  444. nfs_inode_remove_request(req);
  445. nfs_unlock_request(req);
  446. }
  447. }
  448. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  449. /*
  450. * nfs_scan_commit - Scan an inode for commit requests
  451. * @inode: NFS inode to scan
  452. * @dst: destination list
  453. * @idx_start: lower bound of page->index to scan.
  454. * @npages: idx_start + npages sets the upper bound to scan.
  455. *
  456. * Moves requests from the inode's 'commit' request list.
  457. * The requests are *not* checked to ensure that they form a contiguous set.
  458. */
  459. static int
  460. nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  461. {
  462. struct nfs_inode *nfsi = NFS_I(inode);
  463. int res = 0;
  464. if (nfsi->ncommit != 0) {
  465. res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
  466. nfsi->ncommit -= res;
  467. if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
  468. printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
  469. }
  470. return res;
  471. }
  472. #else
  473. static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  474. {
  475. return 0;
  476. }
  477. #endif
  478. static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
  479. {
  480. struct backing_dev_info *bdi = mapping->backing_dev_info;
  481. DEFINE_WAIT(wait);
  482. int ret = 0;
  483. might_sleep();
  484. if (!bdi_write_congested(bdi))
  485. return 0;
  486. nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
  487. if (intr) {
  488. struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
  489. sigset_t oldset;
  490. rpc_clnt_sigmask(clnt, &oldset);
  491. prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
  492. if (bdi_write_congested(bdi)) {
  493. if (signalled())
  494. ret = -ERESTARTSYS;
  495. else
  496. schedule();
  497. }
  498. rpc_clnt_sigunmask(clnt, &oldset);
  499. } else {
  500. prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
  501. if (bdi_write_congested(bdi))
  502. schedule();
  503. }
  504. finish_wait(&nfs_write_congestion, &wait);
  505. return ret;
  506. }
  507. /*
  508. * Try to update any existing write request, or create one if there is none.
  509. * In order to match, the request's credentials must match those of
  510. * the calling process.
  511. *
  512. * Note: Should always be called with the Page Lock held!
  513. */
  514. static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
  515. struct page *page, unsigned int offset, unsigned int bytes)
  516. {
  517. struct inode *inode = page->mapping->host;
  518. struct nfs_inode *nfsi = NFS_I(inode);
  519. struct nfs_page *req, *new = NULL;
  520. unsigned long rqend, end;
  521. end = offset + bytes;
  522. if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
  523. return ERR_PTR(-ERESTARTSYS);
  524. for (;;) {
  525. /* Loop over all inode entries and see if we find
  526. * A request for the page we wish to update
  527. */
  528. spin_lock(&nfsi->req_lock);
  529. req = nfs_page_find_request_locked(page);
  530. if (req) {
  531. if (!nfs_lock_request_dontget(req)) {
  532. int error;
  533. spin_unlock(&nfsi->req_lock);
  534. error = nfs_wait_on_request(req);
  535. nfs_release_request(req);
  536. if (error < 0) {
  537. if (new)
  538. nfs_release_request(new);
  539. return ERR_PTR(error);
  540. }
  541. continue;
  542. }
  543. spin_unlock(&nfsi->req_lock);
  544. if (new)
  545. nfs_release_request(new);
  546. break;
  547. }
  548. if (new) {
  549. int error;
  550. nfs_lock_request_dontget(new);
  551. error = nfs_inode_add_request(inode, new);
  552. if (error) {
  553. spin_unlock(&nfsi->req_lock);
  554. nfs_unlock_request(new);
  555. return ERR_PTR(error);
  556. }
  557. spin_unlock(&nfsi->req_lock);
  558. nfs_mark_request_dirty(new);
  559. return new;
  560. }
  561. spin_unlock(&nfsi->req_lock);
  562. new = nfs_create_request(ctx, inode, page, offset, bytes);
  563. if (IS_ERR(new))
  564. return new;
  565. }
  566. /* We have a request for our page.
  567. * If the creds don't match, or the
  568. * page addresses don't match,
  569. * tell the caller to wait on the conflicting
  570. * request.
  571. */
  572. rqend = req->wb_offset + req->wb_bytes;
  573. if (req->wb_context != ctx
  574. || req->wb_page != page
  575. || !nfs_dirty_request(req)
  576. || offset > rqend || end < req->wb_offset) {
  577. nfs_unlock_request(req);
  578. return ERR_PTR(-EBUSY);
  579. }
  580. /* Okay, the request matches. Update the region */
  581. if (offset < req->wb_offset) {
  582. req->wb_offset = offset;
  583. req->wb_pgbase = offset;
  584. req->wb_bytes = rqend - req->wb_offset;
  585. }
  586. if (end > rqend)
  587. req->wb_bytes = end - req->wb_offset;
  588. return req;
  589. }
  590. int nfs_flush_incompatible(struct file *file, struct page *page)
  591. {
  592. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  593. struct nfs_page *req;
  594. int do_flush, status;
  595. /*
  596. * Look for a request corresponding to this page. If there
  597. * is one, and it belongs to another file, we flush it out
  598. * before we try to copy anything into the page. Do this
  599. * due to the lack of an ACCESS-type call in NFSv2.
  600. * Also do the same if we find a request from an existing
  601. * dropped page.
  602. */
  603. do {
  604. req = nfs_page_find_request(page);
  605. if (req == NULL)
  606. return 0;
  607. do_flush = req->wb_page != page || req->wb_context != ctx
  608. || test_bit(PG_NEED_FLUSH, &req->wb_flags);
  609. nfs_release_request(req);
  610. if (!do_flush)
  611. return 0;
  612. status = nfs_wb_page(page->mapping->host, page);
  613. } while (status == 0);
  614. return status;
  615. }
  616. /*
  617. * Update and possibly write a cached page of an NFS file.
  618. *
  619. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  620. * things with a page scheduled for an RPC call (e.g. invalidate it).
  621. */
  622. int nfs_updatepage(struct file *file, struct page *page,
  623. unsigned int offset, unsigned int count)
  624. {
  625. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  626. struct inode *inode = page->mapping->host;
  627. int status = 0;
  628. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  629. dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
  630. file->f_dentry->d_parent->d_name.name,
  631. file->f_dentry->d_name.name, count,
  632. (long long)(page_offset(page) +offset));
  633. /* If we're not using byte range locks, and we know the page
  634. * is entirely in cache, it may be more efficient to avoid
  635. * fragmenting write requests.
  636. */
  637. if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
  638. count = max(count + offset, nfs_page_length(page));
  639. offset = 0;
  640. }
  641. status = nfs_writepage_setup(ctx, page, offset, count);
  642. dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
  643. status, (long long)i_size_read(inode));
  644. if (status < 0)
  645. ClearPageUptodate(page);
  646. return status;
  647. }
  648. static void nfs_writepage_release(struct nfs_page *req)
  649. {
  650. end_page_writeback(req->wb_page);
  651. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  652. if (!PageError(req->wb_page)) {
  653. if (NFS_NEED_RESCHED(req)) {
  654. nfs_mark_request_dirty(req);
  655. goto out;
  656. } else if (NFS_NEED_COMMIT(req)) {
  657. nfs_mark_request_commit(req);
  658. goto out;
  659. }
  660. }
  661. nfs_inode_remove_request(req);
  662. out:
  663. nfs_clear_commit(req);
  664. nfs_clear_reschedule(req);
  665. #else
  666. nfs_inode_remove_request(req);
  667. #endif
  668. nfs_clear_page_writeback(req);
  669. }
  670. static inline int flush_task_priority(int how)
  671. {
  672. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  673. case FLUSH_HIGHPRI:
  674. return RPC_PRIORITY_HIGH;
  675. case FLUSH_LOWPRI:
  676. return RPC_PRIORITY_LOW;
  677. }
  678. return RPC_PRIORITY_NORMAL;
  679. }
  680. /*
  681. * Set up the argument/result storage required for the RPC call.
  682. */
  683. static void nfs_write_rpcsetup(struct nfs_page *req,
  684. struct nfs_write_data *data,
  685. const struct rpc_call_ops *call_ops,
  686. unsigned int count, unsigned int offset,
  687. int how)
  688. {
  689. struct inode *inode;
  690. int flags;
  691. /* Set up the RPC argument and reply structs
  692. * NB: take care not to mess about with data->commit et al. */
  693. data->req = req;
  694. data->inode = inode = req->wb_context->dentry->d_inode;
  695. data->cred = req->wb_context->cred;
  696. data->args.fh = NFS_FH(inode);
  697. data->args.offset = req_offset(req) + offset;
  698. data->args.pgbase = req->wb_pgbase + offset;
  699. data->args.pages = data->pagevec;
  700. data->args.count = count;
  701. data->args.context = req->wb_context;
  702. data->res.fattr = &data->fattr;
  703. data->res.count = count;
  704. data->res.verf = &data->verf;
  705. nfs_fattr_init(&data->fattr);
  706. /* Set up the initial task struct. */
  707. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  708. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
  709. NFS_PROTO(inode)->write_setup(data, how);
  710. data->task.tk_priority = flush_task_priority(how);
  711. data->task.tk_cookie = (unsigned long)inode;
  712. dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  713. data->task.tk_pid,
  714. inode->i_sb->s_id,
  715. (long long)NFS_FILEID(inode),
  716. count,
  717. (unsigned long long)data->args.offset);
  718. }
  719. static void nfs_execute_write(struct nfs_write_data *data)
  720. {
  721. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  722. sigset_t oldset;
  723. rpc_clnt_sigmask(clnt, &oldset);
  724. rpc_execute(&data->task);
  725. rpc_clnt_sigunmask(clnt, &oldset);
  726. }
  727. /*
  728. * Generate multiple small requests to write out a single
  729. * contiguous dirty area on one page.
  730. */
  731. static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
  732. {
  733. struct nfs_page *req = nfs_list_entry(head->next);
  734. struct page *page = req->wb_page;
  735. struct nfs_write_data *data;
  736. size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
  737. unsigned int offset;
  738. int requests = 0;
  739. LIST_HEAD(list);
  740. nfs_list_remove_request(req);
  741. nbytes = req->wb_bytes;
  742. do {
  743. size_t len = min(nbytes, wsize);
  744. data = nfs_writedata_alloc(len);
  745. if (!data)
  746. goto out_bad;
  747. list_add(&data->pages, &list);
  748. requests++;
  749. nbytes -= len;
  750. } while (nbytes != 0);
  751. atomic_set(&req->wb_complete, requests);
  752. ClearPageError(page);
  753. set_page_writeback(page);
  754. offset = 0;
  755. nbytes = req->wb_bytes;
  756. do {
  757. data = list_entry(list.next, struct nfs_write_data, pages);
  758. list_del_init(&data->pages);
  759. data->pagevec[0] = page;
  760. if (nbytes > wsize) {
  761. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  762. wsize, offset, how);
  763. offset += wsize;
  764. nbytes -= wsize;
  765. } else {
  766. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  767. nbytes, offset, how);
  768. nbytes = 0;
  769. }
  770. nfs_execute_write(data);
  771. } while (nbytes != 0);
  772. return 0;
  773. out_bad:
  774. while (!list_empty(&list)) {
  775. data = list_entry(list.next, struct nfs_write_data, pages);
  776. list_del(&data->pages);
  777. nfs_writedata_release(data);
  778. }
  779. nfs_mark_request_dirty(req);
  780. nfs_clear_page_writeback(req);
  781. return -ENOMEM;
  782. }
  783. /*
  784. * Create an RPC task for the given write request and kick it.
  785. * The page must have been locked by the caller.
  786. *
  787. * It may happen that the page we're passed is not marked dirty.
  788. * This is the case if nfs_updatepage detects a conflicting request
  789. * that has been written but not committed.
  790. */
  791. static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
  792. {
  793. struct nfs_page *req;
  794. struct page **pages;
  795. struct nfs_write_data *data;
  796. unsigned int count;
  797. data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
  798. if (!data)
  799. goto out_bad;
  800. pages = data->pagevec;
  801. count = 0;
  802. while (!list_empty(head)) {
  803. req = nfs_list_entry(head->next);
  804. nfs_list_remove_request(req);
  805. nfs_list_add_request(req, &data->pages);
  806. ClearPageError(req->wb_page);
  807. set_page_writeback(req->wb_page);
  808. *pages++ = req->wb_page;
  809. count += req->wb_bytes;
  810. }
  811. req = nfs_list_entry(data->pages.next);
  812. /* Set up the argument struct */
  813. nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
  814. nfs_execute_write(data);
  815. return 0;
  816. out_bad:
  817. while (!list_empty(head)) {
  818. struct nfs_page *req = nfs_list_entry(head->next);
  819. nfs_list_remove_request(req);
  820. nfs_mark_request_dirty(req);
  821. nfs_clear_page_writeback(req);
  822. }
  823. return -ENOMEM;
  824. }
  825. static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
  826. {
  827. LIST_HEAD(one_request);
  828. int (*flush_one)(struct inode *, struct list_head *, int);
  829. struct nfs_page *req;
  830. int wpages = NFS_SERVER(inode)->wpages;
  831. int wsize = NFS_SERVER(inode)->wsize;
  832. int error;
  833. flush_one = nfs_flush_one;
  834. if (wsize < PAGE_CACHE_SIZE)
  835. flush_one = nfs_flush_multi;
  836. /* For single writes, FLUSH_STABLE is more efficient */
  837. if (npages <= wpages && npages == NFS_I(inode)->npages
  838. && nfs_list_entry(head->next)->wb_bytes <= wsize)
  839. how |= FLUSH_STABLE;
  840. do {
  841. nfs_coalesce_requests(head, &one_request, wpages);
  842. req = nfs_list_entry(one_request.next);
  843. error = flush_one(inode, &one_request, how);
  844. if (error < 0)
  845. goto out_err;
  846. } while (!list_empty(head));
  847. return 0;
  848. out_err:
  849. while (!list_empty(head)) {
  850. req = nfs_list_entry(head->next);
  851. nfs_list_remove_request(req);
  852. nfs_mark_request_dirty(req);
  853. nfs_clear_page_writeback(req);
  854. }
  855. return error;
  856. }
  857. /*
  858. * Handle a write reply that flushed part of a page.
  859. */
  860. static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
  861. {
  862. struct nfs_write_data *data = calldata;
  863. struct nfs_page *req = data->req;
  864. struct page *page = req->wb_page;
  865. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  866. req->wb_context->dentry->d_inode->i_sb->s_id,
  867. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  868. req->wb_bytes,
  869. (long long)req_offset(req));
  870. if (nfs_writeback_done(task, data) != 0)
  871. return;
  872. if (task->tk_status < 0) {
  873. ClearPageUptodate(page);
  874. SetPageError(page);
  875. req->wb_context->error = task->tk_status;
  876. dprintk(", error = %d\n", task->tk_status);
  877. } else {
  878. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  879. if (data->verf.committed < NFS_FILE_SYNC) {
  880. if (!NFS_NEED_COMMIT(req)) {
  881. nfs_defer_commit(req);
  882. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  883. dprintk(" defer commit\n");
  884. } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
  885. nfs_defer_reschedule(req);
  886. dprintk(" server reboot detected\n");
  887. }
  888. } else
  889. #endif
  890. dprintk(" OK\n");
  891. }
  892. if (atomic_dec_and_test(&req->wb_complete))
  893. nfs_writepage_release(req);
  894. }
  895. static const struct rpc_call_ops nfs_write_partial_ops = {
  896. .rpc_call_done = nfs_writeback_done_partial,
  897. .rpc_release = nfs_writedata_release,
  898. };
  899. /*
  900. * Handle a write reply that flushes a whole page.
  901. *
  902. * FIXME: There is an inherent race with invalidate_inode_pages and
  903. * writebacks since the page->count is kept > 1 for as long
  904. * as the page has a write request pending.
  905. */
  906. static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
  907. {
  908. struct nfs_write_data *data = calldata;
  909. struct nfs_page *req;
  910. struct page *page;
  911. if (nfs_writeback_done(task, data) != 0)
  912. return;
  913. /* Update attributes as result of writeback. */
  914. while (!list_empty(&data->pages)) {
  915. req = nfs_list_entry(data->pages.next);
  916. nfs_list_remove_request(req);
  917. page = req->wb_page;
  918. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  919. req->wb_context->dentry->d_inode->i_sb->s_id,
  920. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  921. req->wb_bytes,
  922. (long long)req_offset(req));
  923. if (task->tk_status < 0) {
  924. ClearPageUptodate(page);
  925. SetPageError(page);
  926. req->wb_context->error = task->tk_status;
  927. end_page_writeback(page);
  928. nfs_inode_remove_request(req);
  929. dprintk(", error = %d\n", task->tk_status);
  930. goto next;
  931. }
  932. end_page_writeback(page);
  933. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  934. if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
  935. nfs_inode_remove_request(req);
  936. dprintk(" OK\n");
  937. goto next;
  938. }
  939. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  940. nfs_mark_request_commit(req);
  941. dprintk(" marked for commit\n");
  942. #else
  943. nfs_inode_remove_request(req);
  944. #endif
  945. next:
  946. nfs_clear_page_writeback(req);
  947. }
  948. }
  949. static const struct rpc_call_ops nfs_write_full_ops = {
  950. .rpc_call_done = nfs_writeback_done_full,
  951. .rpc_release = nfs_writedata_release,
  952. };
  953. /*
  954. * This function is called when the WRITE call is complete.
  955. */
  956. int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
  957. {
  958. struct nfs_writeargs *argp = &data->args;
  959. struct nfs_writeres *resp = &data->res;
  960. int status;
  961. dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
  962. task->tk_pid, task->tk_status);
  963. /*
  964. * ->write_done will attempt to use post-op attributes to detect
  965. * conflicting writes by other clients. A strict interpretation
  966. * of close-to-open would allow us to continue caching even if
  967. * another writer had changed the file, but some applications
  968. * depend on tighter cache coherency when writing.
  969. */
  970. status = NFS_PROTO(data->inode)->write_done(task, data);
  971. if (status != 0)
  972. return status;
  973. nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  974. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  975. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  976. /* We tried a write call, but the server did not
  977. * commit data to stable storage even though we
  978. * requested it.
  979. * Note: There is a known bug in Tru64 < 5.0 in which
  980. * the server reports NFS_DATA_SYNC, but performs
  981. * NFS_FILE_SYNC. We therefore implement this checking
  982. * as a dprintk() in order to avoid filling syslog.
  983. */
  984. static unsigned long complain;
  985. if (time_before(complain, jiffies)) {
  986. dprintk("NFS: faulty NFS server %s:"
  987. " (committed = %d) != (stable = %d)\n",
  988. NFS_SERVER(data->inode)->nfs_client->cl_hostname,
  989. resp->verf->committed, argp->stable);
  990. complain = jiffies + 300 * HZ;
  991. }
  992. }
  993. #endif
  994. /* Is this a short write? */
  995. if (task->tk_status >= 0 && resp->count < argp->count) {
  996. static unsigned long complain;
  997. nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
  998. /* Has the server at least made some progress? */
  999. if (resp->count != 0) {
  1000. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1001. if (resp->verf->committed != NFS_UNSTABLE) {
  1002. /* Resend from where the server left off */
  1003. argp->offset += resp->count;
  1004. argp->pgbase += resp->count;
  1005. argp->count -= resp->count;
  1006. } else {
  1007. /* Resend as a stable write in order to avoid
  1008. * headaches in the case of a server crash.
  1009. */
  1010. argp->stable = NFS_FILE_SYNC;
  1011. }
  1012. rpc_restart_call(task);
  1013. return -EAGAIN;
  1014. }
  1015. if (time_before(complain, jiffies)) {
  1016. printk(KERN_WARNING
  1017. "NFS: Server wrote zero bytes, expected %u.\n",
  1018. argp->count);
  1019. complain = jiffies + 300 * HZ;
  1020. }
  1021. /* Can't do anything about it except throw an error. */
  1022. task->tk_status = -EIO;
  1023. }
  1024. return 0;
  1025. }
  1026. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1027. void nfs_commit_release(void *wdata)
  1028. {
  1029. nfs_commit_free(wdata);
  1030. }
  1031. /*
  1032. * Set up the argument/result storage required for the RPC call.
  1033. */
  1034. static void nfs_commit_rpcsetup(struct list_head *head,
  1035. struct nfs_write_data *data,
  1036. int how)
  1037. {
  1038. struct nfs_page *first;
  1039. struct inode *inode;
  1040. int flags;
  1041. /* Set up the RPC argument and reply structs
  1042. * NB: take care not to mess about with data->commit et al. */
  1043. list_splice_init(head, &data->pages);
  1044. first = nfs_list_entry(data->pages.next);
  1045. inode = first->wb_context->dentry->d_inode;
  1046. data->inode = inode;
  1047. data->cred = first->wb_context->cred;
  1048. data->args.fh = NFS_FH(data->inode);
  1049. /* Note: we always request a commit of the entire inode */
  1050. data->args.offset = 0;
  1051. data->args.count = 0;
  1052. data->res.count = 0;
  1053. data->res.fattr = &data->fattr;
  1054. data->res.verf = &data->verf;
  1055. nfs_fattr_init(&data->fattr);
  1056. /* Set up the initial task struct. */
  1057. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1058. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
  1059. NFS_PROTO(inode)->commit_setup(data, how);
  1060. data->task.tk_priority = flush_task_priority(how);
  1061. data->task.tk_cookie = (unsigned long)inode;
  1062. dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
  1063. }
  1064. /*
  1065. * Commit dirty pages
  1066. */
  1067. static int
  1068. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1069. {
  1070. struct nfs_write_data *data;
  1071. struct nfs_page *req;
  1072. data = nfs_commit_alloc();
  1073. if (!data)
  1074. goto out_bad;
  1075. /* Set up the argument struct */
  1076. nfs_commit_rpcsetup(head, data, how);
  1077. nfs_execute_write(data);
  1078. return 0;
  1079. out_bad:
  1080. while (!list_empty(head)) {
  1081. req = nfs_list_entry(head->next);
  1082. nfs_list_remove_request(req);
  1083. nfs_mark_request_commit(req);
  1084. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1085. nfs_clear_page_writeback(req);
  1086. }
  1087. return -ENOMEM;
  1088. }
  1089. /*
  1090. * COMMIT call returned
  1091. */
  1092. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1093. {
  1094. struct nfs_write_data *data = calldata;
  1095. struct nfs_page *req;
  1096. dprintk("NFS: %4d nfs_commit_done (status %d)\n",
  1097. task->tk_pid, task->tk_status);
  1098. /* Call the NFS version-specific code */
  1099. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1100. return;
  1101. while (!list_empty(&data->pages)) {
  1102. req = nfs_list_entry(data->pages.next);
  1103. nfs_list_remove_request(req);
  1104. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1105. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1106. req->wb_context->dentry->d_inode->i_sb->s_id,
  1107. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1108. req->wb_bytes,
  1109. (long long)req_offset(req));
  1110. if (task->tk_status < 0) {
  1111. req->wb_context->error = task->tk_status;
  1112. nfs_inode_remove_request(req);
  1113. dprintk(", error = %d\n", task->tk_status);
  1114. goto next;
  1115. }
  1116. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1117. * returned by the server against all stored verfs. */
  1118. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1119. /* We have a match */
  1120. nfs_inode_remove_request(req);
  1121. dprintk(" OK\n");
  1122. goto next;
  1123. }
  1124. /* We have a mismatch. Write the page again */
  1125. dprintk(" mismatch\n");
  1126. nfs_mark_request_dirty(req);
  1127. next:
  1128. nfs_clear_page_writeback(req);
  1129. }
  1130. }
  1131. static const struct rpc_call_ops nfs_commit_ops = {
  1132. .rpc_call_done = nfs_commit_done,
  1133. .rpc_release = nfs_commit_release,
  1134. };
  1135. #else
  1136. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1137. {
  1138. return 0;
  1139. }
  1140. #endif
  1141. static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
  1142. {
  1143. struct nfs_inode *nfsi = NFS_I(mapping->host);
  1144. LIST_HEAD(head);
  1145. long res;
  1146. spin_lock(&nfsi->req_lock);
  1147. res = nfs_scan_dirty(mapping, wbc, &head);
  1148. spin_unlock(&nfsi->req_lock);
  1149. if (res) {
  1150. int error = nfs_flush_list(mapping->host, &head, res, how);
  1151. if (error < 0)
  1152. return error;
  1153. }
  1154. return res;
  1155. }
  1156. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1157. int nfs_commit_inode(struct inode *inode, int how)
  1158. {
  1159. struct nfs_inode *nfsi = NFS_I(inode);
  1160. LIST_HEAD(head);
  1161. int res;
  1162. spin_lock(&nfsi->req_lock);
  1163. res = nfs_scan_commit(inode, &head, 0, 0);
  1164. spin_unlock(&nfsi->req_lock);
  1165. if (res) {
  1166. int error = nfs_commit_list(inode, &head, how);
  1167. if (error < 0)
  1168. return error;
  1169. }
  1170. return res;
  1171. }
  1172. #endif
  1173. long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
  1174. {
  1175. struct inode *inode = mapping->host;
  1176. struct nfs_inode *nfsi = NFS_I(inode);
  1177. unsigned long idx_start, idx_end;
  1178. unsigned int npages = 0;
  1179. LIST_HEAD(head);
  1180. int nocommit = how & FLUSH_NOCOMMIT;
  1181. long pages, ret;
  1182. /* FIXME */
  1183. if (wbc->range_cyclic)
  1184. idx_start = 0;
  1185. else {
  1186. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  1187. idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1188. if (idx_end > idx_start) {
  1189. unsigned long l_npages = 1 + idx_end - idx_start;
  1190. npages = l_npages;
  1191. if (sizeof(npages) != sizeof(l_npages) &&
  1192. (unsigned long)npages != l_npages)
  1193. npages = 0;
  1194. }
  1195. }
  1196. how &= ~FLUSH_NOCOMMIT;
  1197. spin_lock(&nfsi->req_lock);
  1198. do {
  1199. wbc->pages_skipped = 0;
  1200. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1201. if (ret != 0)
  1202. continue;
  1203. pages = nfs_scan_dirty(mapping, wbc, &head);
  1204. if (pages != 0) {
  1205. spin_unlock(&nfsi->req_lock);
  1206. if (how & FLUSH_INVALIDATE) {
  1207. nfs_cancel_dirty_list(&head);
  1208. ret = pages;
  1209. } else
  1210. ret = nfs_flush_list(inode, &head, pages, how);
  1211. spin_lock(&nfsi->req_lock);
  1212. continue;
  1213. }
  1214. if (wbc->pages_skipped != 0)
  1215. continue;
  1216. if (nocommit)
  1217. break;
  1218. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1219. if (pages == 0) {
  1220. if (wbc->pages_skipped != 0)
  1221. continue;
  1222. break;
  1223. }
  1224. if (how & FLUSH_INVALIDATE) {
  1225. spin_unlock(&nfsi->req_lock);
  1226. nfs_cancel_commit_list(&head);
  1227. ret = pages;
  1228. spin_lock(&nfsi->req_lock);
  1229. continue;
  1230. }
  1231. pages += nfs_scan_commit(inode, &head, 0, 0);
  1232. spin_unlock(&nfsi->req_lock);
  1233. ret = nfs_commit_list(inode, &head, how);
  1234. spin_lock(&nfsi->req_lock);
  1235. } while (ret >= 0);
  1236. spin_unlock(&nfsi->req_lock);
  1237. return ret;
  1238. }
  1239. /*
  1240. * flush the inode to disk.
  1241. */
  1242. int nfs_wb_all(struct inode *inode)
  1243. {
  1244. struct address_space *mapping = inode->i_mapping;
  1245. struct writeback_control wbc = {
  1246. .bdi = mapping->backing_dev_info,
  1247. .sync_mode = WB_SYNC_ALL,
  1248. .nr_to_write = LONG_MAX,
  1249. .range_cyclic = 1,
  1250. };
  1251. int ret;
  1252. ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
  1253. if (ret >= 0)
  1254. return 0;
  1255. return ret;
  1256. }
  1257. int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
  1258. {
  1259. struct writeback_control wbc = {
  1260. .bdi = mapping->backing_dev_info,
  1261. .sync_mode = WB_SYNC_ALL,
  1262. .nr_to_write = LONG_MAX,
  1263. .range_start = range_start,
  1264. .range_end = range_end,
  1265. };
  1266. int ret;
  1267. ret = nfs_sync_mapping_wait(mapping, &wbc, how);
  1268. if (ret >= 0)
  1269. return 0;
  1270. return ret;
  1271. }
  1272. static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
  1273. {
  1274. loff_t range_start = page_offset(page);
  1275. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1276. struct writeback_control wbc = {
  1277. .bdi = page->mapping->backing_dev_info,
  1278. .sync_mode = WB_SYNC_ALL,
  1279. .nr_to_write = LONG_MAX,
  1280. .range_start = range_start,
  1281. .range_end = range_end,
  1282. };
  1283. int ret;
  1284. BUG_ON(!PageLocked(page));
  1285. if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
  1286. ret = nfs_writepage_locked(page, &wbc);
  1287. if (ret < 0)
  1288. goto out;
  1289. }
  1290. ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
  1291. if (ret >= 0)
  1292. return 0;
  1293. out:
  1294. return ret;
  1295. }
  1296. /*
  1297. * Write back all requests on one page - we do this before reading it.
  1298. */
  1299. int nfs_wb_page(struct inode *inode, struct page* page)
  1300. {
  1301. return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
  1302. }
  1303. int nfs_set_page_dirty(struct page *page)
  1304. {
  1305. struct nfs_page *req;
  1306. req = nfs_page_find_request(page);
  1307. if (req != NULL) {
  1308. /* Mark any existing write requests for flushing */
  1309. set_bit(PG_NEED_FLUSH, &req->wb_flags);
  1310. nfs_release_request(req);
  1311. }
  1312. return __set_page_dirty_nobuffers(page);
  1313. }
  1314. int __init nfs_init_writepagecache(void)
  1315. {
  1316. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1317. sizeof(struct nfs_write_data),
  1318. 0, SLAB_HWCACHE_ALIGN,
  1319. NULL, NULL);
  1320. if (nfs_wdata_cachep == NULL)
  1321. return -ENOMEM;
  1322. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1323. nfs_wdata_cachep);
  1324. if (nfs_wdata_mempool == NULL)
  1325. return -ENOMEM;
  1326. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1327. nfs_wdata_cachep);
  1328. if (nfs_commit_mempool == NULL)
  1329. return -ENOMEM;
  1330. return 0;
  1331. }
  1332. void nfs_destroy_writepagecache(void)
  1333. {
  1334. mempool_destroy(nfs_commit_mempool);
  1335. mempool_destroy(nfs_wdata_mempool);
  1336. kmem_cache_destroy(nfs_wdata_cachep);
  1337. }