write.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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
  486. nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
  487. {
  488. struct nfs_inode *nfsi = NFS_I(inode);
  489. struct nfs_page *req;
  490. unsigned long idx_end, next;
  491. unsigned int res = 0;
  492. int error;
  493. if (npages == 0)
  494. idx_end = ~0;
  495. else
  496. idx_end = idx_start + npages - 1;
  497. spin_lock(&nfsi->req_lock);
  498. next = idx_start;
  499. while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
  500. if (req->wb_index > idx_end)
  501. break;
  502. next = req->wb_index + 1;
  503. BUG_ON(!NFS_WBACK_BUSY(req));
  504. atomic_inc(&req->wb_count);
  505. spin_unlock(&nfsi->req_lock);
  506. error = nfs_wait_on_request(req);
  507. nfs_release_request(req);
  508. if (error < 0)
  509. return error;
  510. spin_lock(&nfsi->req_lock);
  511. res++;
  512. }
  513. spin_unlock(&nfsi->req_lock);
  514. return res;
  515. }
  516. /*
  517. * nfs_scan_dirty - Scan an inode for dirty requests
  518. * @inode: NFS inode to scan
  519. * @dst: destination list
  520. * @idx_start: lower bound of page->index to scan.
  521. * @npages: idx_start + npages sets the upper bound to scan.
  522. *
  523. * Moves requests from the inode's dirty page list.
  524. * The requests are *not* checked to ensure that they form a contiguous set.
  525. */
  526. static int
  527. nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  528. {
  529. struct nfs_inode *nfsi = NFS_I(inode);
  530. int res = 0;
  531. if (nfsi->ndirty != 0) {
  532. res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
  533. nfsi->ndirty -= res;
  534. sub_page_state(nr_dirty,res);
  535. if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
  536. printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
  537. }
  538. return res;
  539. }
  540. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  541. /*
  542. * nfs_scan_commit - Scan an inode for commit requests
  543. * @inode: NFS inode to scan
  544. * @dst: destination list
  545. * @idx_start: lower bound of page->index to scan.
  546. * @npages: idx_start + npages sets the upper bound to scan.
  547. *
  548. * Moves requests from the inode's 'commit' request list.
  549. * The requests are *not* checked to ensure that they form a contiguous set.
  550. */
  551. static int
  552. nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  553. {
  554. struct nfs_inode *nfsi = NFS_I(inode);
  555. int res = 0;
  556. if (nfsi->ncommit != 0) {
  557. res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
  558. nfsi->ncommit -= res;
  559. if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
  560. printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
  561. }
  562. return res;
  563. }
  564. #endif
  565. static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
  566. {
  567. struct backing_dev_info *bdi = mapping->backing_dev_info;
  568. DEFINE_WAIT(wait);
  569. int ret = 0;
  570. might_sleep();
  571. if (!bdi_write_congested(bdi))
  572. return 0;
  573. nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
  574. if (intr) {
  575. struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
  576. sigset_t oldset;
  577. rpc_clnt_sigmask(clnt, &oldset);
  578. prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
  579. if (bdi_write_congested(bdi)) {
  580. if (signalled())
  581. ret = -ERESTARTSYS;
  582. else
  583. schedule();
  584. }
  585. rpc_clnt_sigunmask(clnt, &oldset);
  586. } else {
  587. prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
  588. if (bdi_write_congested(bdi))
  589. schedule();
  590. }
  591. finish_wait(&nfs_write_congestion, &wait);
  592. return ret;
  593. }
  594. /*
  595. * Try to update any existing write request, or create one if there is none.
  596. * In order to match, the request's credentials must match those of
  597. * the calling process.
  598. *
  599. * Note: Should always be called with the Page Lock held!
  600. */
  601. static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
  602. struct inode *inode, struct page *page,
  603. unsigned int offset, unsigned int bytes)
  604. {
  605. struct nfs_server *server = NFS_SERVER(inode);
  606. struct nfs_inode *nfsi = NFS_I(inode);
  607. struct nfs_page *req, *new = NULL;
  608. unsigned long rqend, end;
  609. end = offset + bytes;
  610. if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
  611. return ERR_PTR(-ERESTARTSYS);
  612. for (;;) {
  613. /* Loop over all inode entries and see if we find
  614. * A request for the page we wish to update
  615. */
  616. spin_lock(&nfsi->req_lock);
  617. req = _nfs_find_request(inode, page->index);
  618. if (req) {
  619. if (!nfs_lock_request_dontget(req)) {
  620. int error;
  621. spin_unlock(&nfsi->req_lock);
  622. error = nfs_wait_on_request(req);
  623. nfs_release_request(req);
  624. if (error < 0) {
  625. if (new)
  626. nfs_release_request(new);
  627. return ERR_PTR(error);
  628. }
  629. continue;
  630. }
  631. spin_unlock(&nfsi->req_lock);
  632. if (new)
  633. nfs_release_request(new);
  634. break;
  635. }
  636. if (new) {
  637. int error;
  638. nfs_lock_request_dontget(new);
  639. error = nfs_inode_add_request(inode, new);
  640. if (error) {
  641. spin_unlock(&nfsi->req_lock);
  642. nfs_unlock_request(new);
  643. return ERR_PTR(error);
  644. }
  645. spin_unlock(&nfsi->req_lock);
  646. nfs_mark_request_dirty(new);
  647. return new;
  648. }
  649. spin_unlock(&nfsi->req_lock);
  650. new = nfs_create_request(ctx, inode, page, offset, bytes);
  651. if (IS_ERR(new))
  652. return new;
  653. }
  654. /* We have a request for our page.
  655. * If the creds don't match, or the
  656. * page addresses don't match,
  657. * tell the caller to wait on the conflicting
  658. * request.
  659. */
  660. rqend = req->wb_offset + req->wb_bytes;
  661. if (req->wb_context != ctx
  662. || req->wb_page != page
  663. || !nfs_dirty_request(req)
  664. || offset > rqend || end < req->wb_offset) {
  665. nfs_unlock_request(req);
  666. return ERR_PTR(-EBUSY);
  667. }
  668. /* Okay, the request matches. Update the region */
  669. if (offset < req->wb_offset) {
  670. req->wb_offset = offset;
  671. req->wb_pgbase = offset;
  672. req->wb_bytes = rqend - req->wb_offset;
  673. }
  674. if (end > rqend)
  675. req->wb_bytes = end - req->wb_offset;
  676. return req;
  677. }
  678. int nfs_flush_incompatible(struct file *file, struct page *page)
  679. {
  680. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  681. struct inode *inode = page->mapping->host;
  682. struct nfs_page *req;
  683. int status = 0;
  684. /*
  685. * Look for a request corresponding to this page. If there
  686. * is one, and it belongs to another file, we flush it out
  687. * before we try to copy anything into the page. Do this
  688. * due to the lack of an ACCESS-type call in NFSv2.
  689. * Also do the same if we find a request from an existing
  690. * dropped page.
  691. */
  692. req = nfs_find_request(inode, page->index);
  693. if (req) {
  694. if (req->wb_page != page || ctx != req->wb_context)
  695. status = nfs_wb_page(inode, page);
  696. nfs_release_request(req);
  697. }
  698. return (status < 0) ? status : 0;
  699. }
  700. /*
  701. * Update and possibly write a cached page of an NFS file.
  702. *
  703. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  704. * things with a page scheduled for an RPC call (e.g. invalidate it).
  705. */
  706. int nfs_updatepage(struct file *file, struct page *page,
  707. unsigned int offset, unsigned int count)
  708. {
  709. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  710. struct inode *inode = page->mapping->host;
  711. struct nfs_page *req;
  712. int status = 0;
  713. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  714. dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
  715. file->f_dentry->d_parent->d_name.name,
  716. file->f_dentry->d_name.name, count,
  717. (long long)(page_offset(page) +offset));
  718. if (IS_SYNC(inode)) {
  719. status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
  720. if (status > 0) {
  721. if (offset == 0 && status == PAGE_CACHE_SIZE)
  722. SetPageUptodate(page);
  723. return 0;
  724. }
  725. return status;
  726. }
  727. /* If we're not using byte range locks, and we know the page
  728. * is entirely in cache, it may be more efficient to avoid
  729. * fragmenting write requests.
  730. */
  731. if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
  732. loff_t end_offs = i_size_read(inode) - 1;
  733. unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
  734. count += offset;
  735. offset = 0;
  736. if (unlikely(end_offs < 0)) {
  737. /* Do nothing */
  738. } else if (page->index == end_index) {
  739. unsigned int pglen;
  740. pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
  741. if (count < pglen)
  742. count = pglen;
  743. } else if (page->index < end_index)
  744. count = PAGE_CACHE_SIZE;
  745. }
  746. /*
  747. * Try to find an NFS request corresponding to this page
  748. * and update it.
  749. * If the existing request cannot be updated, we must flush
  750. * it out now.
  751. */
  752. do {
  753. req = nfs_update_request(ctx, inode, page, offset, count);
  754. status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
  755. if (status != -EBUSY)
  756. break;
  757. /* Request could not be updated. Flush it out and try again */
  758. status = nfs_wb_page(inode, page);
  759. } while (status >= 0);
  760. if (status < 0)
  761. goto done;
  762. status = 0;
  763. /* Update file length */
  764. nfs_grow_file(page, offset, count);
  765. /* Set the PG_uptodate flag? */
  766. nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
  767. nfs_unlock_request(req);
  768. done:
  769. dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
  770. status, (long long)i_size_read(inode));
  771. if (status < 0)
  772. ClearPageUptodate(page);
  773. return status;
  774. }
  775. static void nfs_writepage_release(struct nfs_page *req)
  776. {
  777. end_page_writeback(req->wb_page);
  778. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  779. if (!PageError(req->wb_page)) {
  780. if (NFS_NEED_RESCHED(req)) {
  781. nfs_mark_request_dirty(req);
  782. goto out;
  783. } else if (NFS_NEED_COMMIT(req)) {
  784. nfs_mark_request_commit(req);
  785. goto out;
  786. }
  787. }
  788. nfs_inode_remove_request(req);
  789. out:
  790. nfs_clear_commit(req);
  791. nfs_clear_reschedule(req);
  792. #else
  793. nfs_inode_remove_request(req);
  794. #endif
  795. nfs_clear_page_writeback(req);
  796. }
  797. static inline int flush_task_priority(int how)
  798. {
  799. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  800. case FLUSH_HIGHPRI:
  801. return RPC_PRIORITY_HIGH;
  802. case FLUSH_LOWPRI:
  803. return RPC_PRIORITY_LOW;
  804. }
  805. return RPC_PRIORITY_NORMAL;
  806. }
  807. /*
  808. * Set up the argument/result storage required for the RPC call.
  809. */
  810. static void nfs_write_rpcsetup(struct nfs_page *req,
  811. struct nfs_write_data *data,
  812. const struct rpc_call_ops *call_ops,
  813. unsigned int count, unsigned int offset,
  814. int how)
  815. {
  816. struct inode *inode;
  817. int flags;
  818. /* Set up the RPC argument and reply structs
  819. * NB: take care not to mess about with data->commit et al. */
  820. data->req = req;
  821. data->inode = inode = req->wb_context->dentry->d_inode;
  822. data->cred = req->wb_context->cred;
  823. data->args.fh = NFS_FH(inode);
  824. data->args.offset = req_offset(req) + offset;
  825. data->args.pgbase = req->wb_pgbase + offset;
  826. data->args.pages = data->pagevec;
  827. data->args.count = count;
  828. data->args.context = req->wb_context;
  829. data->res.fattr = &data->fattr;
  830. data->res.count = count;
  831. data->res.verf = &data->verf;
  832. nfs_fattr_init(&data->fattr);
  833. /* Set up the initial task struct. */
  834. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  835. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
  836. NFS_PROTO(inode)->write_setup(data, how);
  837. data->task.tk_priority = flush_task_priority(how);
  838. data->task.tk_cookie = (unsigned long)inode;
  839. dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  840. data->task.tk_pid,
  841. inode->i_sb->s_id,
  842. (long long)NFS_FILEID(inode),
  843. count,
  844. (unsigned long long)data->args.offset);
  845. }
  846. static void nfs_execute_write(struct nfs_write_data *data)
  847. {
  848. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  849. sigset_t oldset;
  850. rpc_clnt_sigmask(clnt, &oldset);
  851. lock_kernel();
  852. rpc_execute(&data->task);
  853. unlock_kernel();
  854. rpc_clnt_sigunmask(clnt, &oldset);
  855. }
  856. /*
  857. * Generate multiple small requests to write out a single
  858. * contiguous dirty area on one page.
  859. */
  860. static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
  861. {
  862. struct nfs_page *req = nfs_list_entry(head->next);
  863. struct page *page = req->wb_page;
  864. struct nfs_write_data *data;
  865. unsigned int wsize = NFS_SERVER(inode)->wsize;
  866. unsigned int nbytes, offset;
  867. int requests = 0;
  868. LIST_HEAD(list);
  869. nfs_list_remove_request(req);
  870. nbytes = req->wb_bytes;
  871. for (;;) {
  872. data = nfs_writedata_alloc(1);
  873. if (!data)
  874. goto out_bad;
  875. list_add(&data->pages, &list);
  876. requests++;
  877. if (nbytes <= wsize)
  878. break;
  879. nbytes -= wsize;
  880. }
  881. atomic_set(&req->wb_complete, requests);
  882. ClearPageError(page);
  883. set_page_writeback(page);
  884. offset = 0;
  885. nbytes = req->wb_bytes;
  886. do {
  887. data = list_entry(list.next, struct nfs_write_data, pages);
  888. list_del_init(&data->pages);
  889. data->pagevec[0] = page;
  890. if (nbytes > wsize) {
  891. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  892. wsize, offset, how);
  893. offset += wsize;
  894. nbytes -= wsize;
  895. } else {
  896. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  897. nbytes, offset, how);
  898. nbytes = 0;
  899. }
  900. nfs_execute_write(data);
  901. } while (nbytes != 0);
  902. return 0;
  903. out_bad:
  904. while (!list_empty(&list)) {
  905. data = list_entry(list.next, struct nfs_write_data, pages);
  906. list_del(&data->pages);
  907. nfs_writedata_free(data);
  908. }
  909. nfs_mark_request_dirty(req);
  910. nfs_clear_page_writeback(req);
  911. return -ENOMEM;
  912. }
  913. /*
  914. * Create an RPC task for the given write request and kick it.
  915. * The page must have been locked by the caller.
  916. *
  917. * It may happen that the page we're passed is not marked dirty.
  918. * This is the case if nfs_updatepage detects a conflicting request
  919. * that has been written but not committed.
  920. */
  921. static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
  922. {
  923. struct nfs_page *req;
  924. struct page **pages;
  925. struct nfs_write_data *data;
  926. unsigned int count;
  927. if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
  928. return nfs_flush_multi(head, inode, how);
  929. data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
  930. if (!data)
  931. goto out_bad;
  932. pages = data->pagevec;
  933. count = 0;
  934. while (!list_empty(head)) {
  935. req = nfs_list_entry(head->next);
  936. nfs_list_remove_request(req);
  937. nfs_list_add_request(req, &data->pages);
  938. ClearPageError(req->wb_page);
  939. set_page_writeback(req->wb_page);
  940. *pages++ = req->wb_page;
  941. count += req->wb_bytes;
  942. }
  943. req = nfs_list_entry(data->pages.next);
  944. /* Set up the argument struct */
  945. nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
  946. nfs_execute_write(data);
  947. return 0;
  948. out_bad:
  949. while (!list_empty(head)) {
  950. struct nfs_page *req = nfs_list_entry(head->next);
  951. nfs_list_remove_request(req);
  952. nfs_mark_request_dirty(req);
  953. nfs_clear_page_writeback(req);
  954. }
  955. return -ENOMEM;
  956. }
  957. static int
  958. nfs_flush_list(struct list_head *head, int wpages, int how)
  959. {
  960. LIST_HEAD(one_request);
  961. struct nfs_page *req;
  962. int error = 0;
  963. unsigned int pages = 0;
  964. while (!list_empty(head)) {
  965. pages += nfs_coalesce_requests(head, &one_request, wpages);
  966. req = nfs_list_entry(one_request.next);
  967. error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
  968. if (error < 0)
  969. break;
  970. }
  971. if (error >= 0)
  972. return pages;
  973. while (!list_empty(head)) {
  974. req = nfs_list_entry(head->next);
  975. nfs_list_remove_request(req);
  976. nfs_mark_request_dirty(req);
  977. nfs_clear_page_writeback(req);
  978. }
  979. return error;
  980. }
  981. /*
  982. * Handle a write reply that flushed part of a page.
  983. */
  984. static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
  985. {
  986. struct nfs_write_data *data = calldata;
  987. struct nfs_page *req = data->req;
  988. struct page *page = req->wb_page;
  989. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  990. req->wb_context->dentry->d_inode->i_sb->s_id,
  991. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  992. req->wb_bytes,
  993. (long long)req_offset(req));
  994. if (nfs_writeback_done(task, data) != 0)
  995. return;
  996. if (task->tk_status < 0) {
  997. ClearPageUptodate(page);
  998. SetPageError(page);
  999. req->wb_context->error = task->tk_status;
  1000. dprintk(", error = %d\n", task->tk_status);
  1001. } else {
  1002. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1003. if (data->verf.committed < NFS_FILE_SYNC) {
  1004. if (!NFS_NEED_COMMIT(req)) {
  1005. nfs_defer_commit(req);
  1006. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  1007. dprintk(" defer commit\n");
  1008. } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
  1009. nfs_defer_reschedule(req);
  1010. dprintk(" server reboot detected\n");
  1011. }
  1012. } else
  1013. #endif
  1014. dprintk(" OK\n");
  1015. }
  1016. if (atomic_dec_and_test(&req->wb_complete))
  1017. nfs_writepage_release(req);
  1018. }
  1019. static const struct rpc_call_ops nfs_write_partial_ops = {
  1020. .rpc_call_done = nfs_writeback_done_partial,
  1021. .rpc_release = nfs_writedata_release,
  1022. };
  1023. /*
  1024. * Handle a write reply that flushes a whole page.
  1025. *
  1026. * FIXME: There is an inherent race with invalidate_inode_pages and
  1027. * writebacks since the page->count is kept > 1 for as long
  1028. * as the page has a write request pending.
  1029. */
  1030. static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
  1031. {
  1032. struct nfs_write_data *data = calldata;
  1033. struct nfs_page *req;
  1034. struct page *page;
  1035. if (nfs_writeback_done(task, data) != 0)
  1036. return;
  1037. /* Update attributes as result of writeback. */
  1038. while (!list_empty(&data->pages)) {
  1039. req = nfs_list_entry(data->pages.next);
  1040. nfs_list_remove_request(req);
  1041. page = req->wb_page;
  1042. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  1043. req->wb_context->dentry->d_inode->i_sb->s_id,
  1044. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1045. req->wb_bytes,
  1046. (long long)req_offset(req));
  1047. if (task->tk_status < 0) {
  1048. ClearPageUptodate(page);
  1049. SetPageError(page);
  1050. req->wb_context->error = task->tk_status;
  1051. end_page_writeback(page);
  1052. nfs_inode_remove_request(req);
  1053. dprintk(", error = %d\n", task->tk_status);
  1054. goto next;
  1055. }
  1056. end_page_writeback(page);
  1057. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1058. if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
  1059. nfs_inode_remove_request(req);
  1060. dprintk(" OK\n");
  1061. goto next;
  1062. }
  1063. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  1064. nfs_mark_request_commit(req);
  1065. dprintk(" marked for commit\n");
  1066. #else
  1067. nfs_inode_remove_request(req);
  1068. #endif
  1069. next:
  1070. nfs_clear_page_writeback(req);
  1071. }
  1072. }
  1073. static const struct rpc_call_ops nfs_write_full_ops = {
  1074. .rpc_call_done = nfs_writeback_done_full,
  1075. .rpc_release = nfs_writedata_release,
  1076. };
  1077. /*
  1078. * This function is called when the WRITE call is complete.
  1079. */
  1080. int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
  1081. {
  1082. struct nfs_writeargs *argp = &data->args;
  1083. struct nfs_writeres *resp = &data->res;
  1084. int status;
  1085. dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
  1086. task->tk_pid, task->tk_status);
  1087. /* Call the NFS version-specific code */
  1088. status = NFS_PROTO(data->inode)->write_done(task, data);
  1089. if (status != 0)
  1090. return status;
  1091. nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  1092. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1093. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  1094. /* We tried a write call, but the server did not
  1095. * commit data to stable storage even though we
  1096. * requested it.
  1097. * Note: There is a known bug in Tru64 < 5.0 in which
  1098. * the server reports NFS_DATA_SYNC, but performs
  1099. * NFS_FILE_SYNC. We therefore implement this checking
  1100. * as a dprintk() in order to avoid filling syslog.
  1101. */
  1102. static unsigned long complain;
  1103. if (time_before(complain, jiffies)) {
  1104. dprintk("NFS: faulty NFS server %s:"
  1105. " (committed = %d) != (stable = %d)\n",
  1106. NFS_SERVER(data->inode)->hostname,
  1107. resp->verf->committed, argp->stable);
  1108. complain = jiffies + 300 * HZ;
  1109. }
  1110. }
  1111. #endif
  1112. /* Is this a short write? */
  1113. if (task->tk_status >= 0 && resp->count < argp->count) {
  1114. static unsigned long complain;
  1115. nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
  1116. /* Has the server at least made some progress? */
  1117. if (resp->count != 0) {
  1118. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1119. if (resp->verf->committed != NFS_UNSTABLE) {
  1120. /* Resend from where the server left off */
  1121. argp->offset += resp->count;
  1122. argp->pgbase += resp->count;
  1123. argp->count -= resp->count;
  1124. } else {
  1125. /* Resend as a stable write in order to avoid
  1126. * headaches in the case of a server crash.
  1127. */
  1128. argp->stable = NFS_FILE_SYNC;
  1129. }
  1130. rpc_restart_call(task);
  1131. return -EAGAIN;
  1132. }
  1133. if (time_before(complain, jiffies)) {
  1134. printk(KERN_WARNING
  1135. "NFS: Server wrote zero bytes, expected %u.\n",
  1136. argp->count);
  1137. complain = jiffies + 300 * HZ;
  1138. }
  1139. /* Can't do anything about it except throw an error. */
  1140. task->tk_status = -EIO;
  1141. }
  1142. return 0;
  1143. }
  1144. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1145. void nfs_commit_release(void *wdata)
  1146. {
  1147. nfs_commit_free(wdata);
  1148. }
  1149. /*
  1150. * Set up the argument/result storage required for the RPC call.
  1151. */
  1152. static void nfs_commit_rpcsetup(struct list_head *head,
  1153. struct nfs_write_data *data,
  1154. int how)
  1155. {
  1156. struct nfs_page *first;
  1157. struct inode *inode;
  1158. int flags;
  1159. /* Set up the RPC argument and reply structs
  1160. * NB: take care not to mess about with data->commit et al. */
  1161. list_splice_init(head, &data->pages);
  1162. first = nfs_list_entry(data->pages.next);
  1163. inode = first->wb_context->dentry->d_inode;
  1164. data->inode = inode;
  1165. data->cred = first->wb_context->cred;
  1166. data->args.fh = NFS_FH(data->inode);
  1167. /* Note: we always request a commit of the entire inode */
  1168. data->args.offset = 0;
  1169. data->args.count = 0;
  1170. data->res.count = 0;
  1171. data->res.fattr = &data->fattr;
  1172. data->res.verf = &data->verf;
  1173. nfs_fattr_init(&data->fattr);
  1174. /* Set up the initial task struct. */
  1175. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1176. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
  1177. NFS_PROTO(inode)->commit_setup(data, how);
  1178. data->task.tk_priority = flush_task_priority(how);
  1179. data->task.tk_cookie = (unsigned long)inode;
  1180. dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
  1181. }
  1182. /*
  1183. * Commit dirty pages
  1184. */
  1185. static int
  1186. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1187. {
  1188. struct nfs_write_data *data;
  1189. struct nfs_page *req;
  1190. data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
  1191. if (!data)
  1192. goto out_bad;
  1193. /* Set up the argument struct */
  1194. nfs_commit_rpcsetup(head, data, how);
  1195. nfs_execute_write(data);
  1196. return 0;
  1197. out_bad:
  1198. while (!list_empty(head)) {
  1199. req = nfs_list_entry(head->next);
  1200. nfs_list_remove_request(req);
  1201. nfs_mark_request_commit(req);
  1202. nfs_clear_page_writeback(req);
  1203. }
  1204. return -ENOMEM;
  1205. }
  1206. /*
  1207. * COMMIT call returned
  1208. */
  1209. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1210. {
  1211. struct nfs_write_data *data = calldata;
  1212. struct nfs_page *req;
  1213. int res = 0;
  1214. dprintk("NFS: %4d nfs_commit_done (status %d)\n",
  1215. task->tk_pid, task->tk_status);
  1216. /* Call the NFS version-specific code */
  1217. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1218. return;
  1219. while (!list_empty(&data->pages)) {
  1220. req = nfs_list_entry(data->pages.next);
  1221. nfs_list_remove_request(req);
  1222. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1223. req->wb_context->dentry->d_inode->i_sb->s_id,
  1224. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1225. req->wb_bytes,
  1226. (long long)req_offset(req));
  1227. if (task->tk_status < 0) {
  1228. req->wb_context->error = task->tk_status;
  1229. nfs_inode_remove_request(req);
  1230. dprintk(", error = %d\n", task->tk_status);
  1231. goto next;
  1232. }
  1233. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1234. * returned by the server against all stored verfs. */
  1235. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1236. /* We have a match */
  1237. nfs_inode_remove_request(req);
  1238. dprintk(" OK\n");
  1239. goto next;
  1240. }
  1241. /* We have a mismatch. Write the page again */
  1242. dprintk(" mismatch\n");
  1243. nfs_mark_request_dirty(req);
  1244. next:
  1245. nfs_clear_page_writeback(req);
  1246. res++;
  1247. }
  1248. sub_page_state(nr_unstable,res);
  1249. }
  1250. static const struct rpc_call_ops nfs_commit_ops = {
  1251. .rpc_call_done = nfs_commit_done,
  1252. .rpc_release = nfs_commit_release,
  1253. };
  1254. #endif
  1255. static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
  1256. unsigned int npages, int how)
  1257. {
  1258. struct nfs_inode *nfsi = NFS_I(inode);
  1259. LIST_HEAD(head);
  1260. int res,
  1261. error = 0;
  1262. spin_lock(&nfsi->req_lock);
  1263. res = nfs_scan_dirty(inode, &head, idx_start, npages);
  1264. spin_unlock(&nfsi->req_lock);
  1265. if (res) {
  1266. struct nfs_server *server = NFS_SERVER(inode);
  1267. /* For single writes, FLUSH_STABLE is more efficient */
  1268. if (res == nfsi->npages && nfsi->npages <= server->wpages) {
  1269. if (res > 1 || nfs_list_entry(head.next)->wb_bytes <= server->wsize)
  1270. how |= FLUSH_STABLE;
  1271. }
  1272. error = nfs_flush_list(&head, server->wpages, how);
  1273. }
  1274. if (error < 0)
  1275. return error;
  1276. return res;
  1277. }
  1278. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1279. int nfs_commit_inode(struct inode *inode, int how)
  1280. {
  1281. struct nfs_inode *nfsi = NFS_I(inode);
  1282. LIST_HEAD(head);
  1283. int res,
  1284. error = 0;
  1285. spin_lock(&nfsi->req_lock);
  1286. res = nfs_scan_commit(inode, &head, 0, 0);
  1287. spin_unlock(&nfsi->req_lock);
  1288. if (res) {
  1289. error = nfs_commit_list(inode, &head, how);
  1290. if (error < 0)
  1291. return error;
  1292. }
  1293. return res;
  1294. }
  1295. #endif
  1296. int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
  1297. unsigned int npages, int how)
  1298. {
  1299. int nocommit = how & FLUSH_NOCOMMIT;
  1300. int wait = how & FLUSH_WAIT;
  1301. int error;
  1302. how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
  1303. do {
  1304. if (wait) {
  1305. error = nfs_wait_on_requests(inode, idx_start, npages);
  1306. if (error != 0)
  1307. continue;
  1308. }
  1309. error = nfs_flush_inode(inode, idx_start, npages, how);
  1310. if (error != 0)
  1311. continue;
  1312. if (!nocommit)
  1313. error = nfs_commit_inode(inode, how);
  1314. } while (error > 0);
  1315. return error;
  1316. }
  1317. int nfs_init_writepagecache(void)
  1318. {
  1319. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1320. sizeof(struct nfs_write_data),
  1321. 0, SLAB_HWCACHE_ALIGN,
  1322. NULL, NULL);
  1323. if (nfs_wdata_cachep == NULL)
  1324. return -ENOMEM;
  1325. nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
  1326. mempool_alloc_slab,
  1327. mempool_free_slab,
  1328. nfs_wdata_cachep);
  1329. if (nfs_wdata_mempool == NULL)
  1330. return -ENOMEM;
  1331. nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
  1332. mempool_alloc_slab,
  1333. mempool_free_slab,
  1334. nfs_wdata_cachep);
  1335. if (nfs_commit_mempool == NULL)
  1336. return -ENOMEM;
  1337. return 0;
  1338. }
  1339. void nfs_destroy_writepagecache(void)
  1340. {
  1341. mempool_destroy(nfs_commit_mempool);
  1342. mempool_destroy(nfs_wdata_mempool);
  1343. if (kmem_cache_destroy(nfs_wdata_cachep))
  1344. printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
  1345. }