write.c 37 KB

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