write.c 41 KB

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