write.c 40 KB

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