write.c 41 KB

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