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