write.c 40 KB

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