write.c 37 KB

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