write.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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/mpage.h>
  54. #include <linux/writeback.h>
  55. #include <linux/sunrpc/clnt.h>
  56. #include <linux/nfs_fs.h>
  57. #include <linux/nfs_mount.h>
  58. #include <linux/nfs_page.h>
  59. #include <asm/uaccess.h>
  60. #include <linux/smp_lock.h>
  61. #include "delegation.h"
  62. #include "iostat.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 int nfs_wait_on_write_congestion(struct address_space *, int);
  74. static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
  75. static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
  76. unsigned int npages, int how);
  77. static const struct rpc_call_ops nfs_write_partial_ops;
  78. static const struct rpc_call_ops nfs_write_full_ops;
  79. static const struct rpc_call_ops nfs_commit_ops;
  80. static kmem_cache_t *nfs_wdata_cachep;
  81. static mempool_t *nfs_wdata_mempool;
  82. static mempool_t *nfs_commit_mempool;
  83. static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
  84. struct nfs_write_data *nfs_commit_alloc(void)
  85. {
  86. struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
  87. if (p) {
  88. memset(p, 0, sizeof(*p));
  89. INIT_LIST_HEAD(&p->pages);
  90. }
  91. return p;
  92. }
  93. void nfs_commit_free(struct nfs_write_data *p)
  94. {
  95. if (p && (p->pagevec != &p->page_array[0]))
  96. kfree(p->pagevec);
  97. mempool_free(p, nfs_commit_mempool);
  98. }
  99. struct nfs_write_data *nfs_writedata_alloc(size_t len)
  100. {
  101. unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  102. struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
  103. if (p) {
  104. memset(p, 0, sizeof(*p));
  105. INIT_LIST_HEAD(&p->pages);
  106. p->npages = pagecount;
  107. if (pagecount <= ARRAY_SIZE(p->page_array))
  108. p->pagevec = p->page_array;
  109. else {
  110. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
  111. if (!p->pagevec) {
  112. mempool_free(p, nfs_wdata_mempool);
  113. p = NULL;
  114. }
  115. }
  116. }
  117. return p;
  118. }
  119. static void nfs_writedata_free(struct nfs_write_data *p)
  120. {
  121. if (p && (p->pagevec != &p->page_array[0]))
  122. kfree(p->pagevec);
  123. mempool_free(p, nfs_wdata_mempool);
  124. }
  125. void nfs_writedata_release(void *wdata)
  126. {
  127. nfs_writedata_free(wdata);
  128. }
  129. /* Adjust the file length if we're writing beyond the end */
  130. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  131. {
  132. struct inode *inode = page->mapping->host;
  133. loff_t end, i_size = i_size_read(inode);
  134. unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
  135. if (i_size > 0 && page->index < end_index)
  136. return;
  137. end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
  138. if (i_size >= end)
  139. return;
  140. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  141. i_size_write(inode, end);
  142. }
  143. /* We can set the PG_uptodate flag if we see that a write request
  144. * covers the full page.
  145. */
  146. static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
  147. {
  148. loff_t end_offs;
  149. if (PageUptodate(page))
  150. return;
  151. if (base != 0)
  152. return;
  153. if (count == PAGE_CACHE_SIZE) {
  154. SetPageUptodate(page);
  155. return;
  156. }
  157. end_offs = i_size_read(page->mapping->host) - 1;
  158. if (end_offs < 0)
  159. return;
  160. /* Is this the last page? */
  161. if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
  162. return;
  163. /* This is the last page: set PG_uptodate if we cover the entire
  164. * extent of the data, then zero the rest of the page.
  165. */
  166. if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
  167. memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
  168. SetPageUptodate(page);
  169. }
  170. }
  171. /*
  172. * Write a page synchronously.
  173. * Offset is the data offset within the page.
  174. */
  175. static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
  176. struct page *page, unsigned int offset, unsigned int count,
  177. int how)
  178. {
  179. unsigned int wsize = NFS_SERVER(inode)->wsize;
  180. int result, written = 0;
  181. struct nfs_write_data *wdata;
  182. wdata = nfs_writedata_alloc(wsize);
  183. if (!wdata)
  184. return -ENOMEM;
  185. wdata->flags = how;
  186. wdata->cred = ctx->cred;
  187. wdata->inode = inode;
  188. wdata->args.fh = NFS_FH(inode);
  189. wdata->args.context = ctx;
  190. wdata->args.pages = &page;
  191. wdata->args.stable = NFS_FILE_SYNC;
  192. wdata->args.pgbase = offset;
  193. wdata->args.count = wsize;
  194. wdata->res.fattr = &wdata->fattr;
  195. wdata->res.verf = &wdata->verf;
  196. dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
  197. inode->i_sb->s_id,
  198. (long long)NFS_FILEID(inode),
  199. count, (long long)(page_offset(page) + offset));
  200. set_page_writeback(page);
  201. nfs_begin_data_update(inode);
  202. do {
  203. if (count < wsize)
  204. wdata->args.count = count;
  205. wdata->args.offset = page_offset(page) + wdata->args.pgbase;
  206. result = NFS_PROTO(inode)->write(wdata);
  207. if (result < 0) {
  208. /* Must mark the page invalid after I/O error */
  209. ClearPageUptodate(page);
  210. goto io_error;
  211. }
  212. if (result < wdata->args.count)
  213. printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
  214. wdata->args.count, result);
  215. wdata->args.offset += result;
  216. wdata->args.pgbase += result;
  217. written += result;
  218. count -= result;
  219. nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
  220. } while (count);
  221. /* Update file length */
  222. nfs_grow_file(page, offset, written);
  223. /* Set the PG_uptodate flag? */
  224. nfs_mark_uptodate(page, offset, written);
  225. if (PageError(page))
  226. ClearPageError(page);
  227. io_error:
  228. nfs_end_data_update(inode);
  229. end_page_writeback(page);
  230. nfs_writedata_free(wdata);
  231. return written ? written : result;
  232. }
  233. static int nfs_writepage_async(struct nfs_open_context *ctx,
  234. struct inode *inode, struct page *page,
  235. unsigned int offset, unsigned int count)
  236. {
  237. struct nfs_page *req;
  238. req = nfs_update_request(ctx, inode, page, offset, count);
  239. if (IS_ERR(req))
  240. return PTR_ERR(req);
  241. /* Update file length */
  242. nfs_grow_file(page, offset, count);
  243. /* Set the PG_uptodate flag? */
  244. nfs_mark_uptodate(page, offset, count);
  245. nfs_unlock_request(req);
  246. return 0;
  247. }
  248. static int wb_priority(struct writeback_control *wbc)
  249. {
  250. if (wbc->for_reclaim)
  251. return FLUSH_HIGHPRI;
  252. if (wbc->for_kupdate)
  253. return FLUSH_LOWPRI;
  254. return 0;
  255. }
  256. /*
  257. * Write an mmapped page to the server.
  258. */
  259. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  260. {
  261. struct nfs_open_context *ctx;
  262. struct inode *inode = page->mapping->host;
  263. unsigned long end_index;
  264. unsigned offset = PAGE_CACHE_SIZE;
  265. loff_t i_size = i_size_read(inode);
  266. int inode_referenced = 0;
  267. int priority = wb_priority(wbc);
  268. int err;
  269. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  270. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  271. /*
  272. * Note: We need to ensure that we have a reference to the inode
  273. * if we are to do asynchronous writes. If not, waiting
  274. * in nfs_wait_on_request() may deadlock with clear_inode().
  275. *
  276. * If igrab() fails here, then it is in any case safe to
  277. * call nfs_wb_page(), since there will be no pending writes.
  278. */
  279. if (igrab(inode) != 0)
  280. inode_referenced = 1;
  281. end_index = i_size >> PAGE_CACHE_SHIFT;
  282. /* Ensure we've flushed out any previous writes */
  283. nfs_wb_page_priority(inode, page, priority);
  284. /* easy case */
  285. if (page->index < end_index)
  286. goto do_it;
  287. /* things got complicated... */
  288. offset = i_size & (PAGE_CACHE_SIZE-1);
  289. /* OK, are we completely out? */
  290. err = 0; /* potential race with truncate - ignore */
  291. if (page->index >= end_index+1 || !offset)
  292. goto out;
  293. do_it:
  294. ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
  295. if (ctx == NULL) {
  296. err = -EBADF;
  297. goto out;
  298. }
  299. lock_kernel();
  300. if (!IS_SYNC(inode) && inode_referenced) {
  301. err = nfs_writepage_async(ctx, inode, page, 0, offset);
  302. if (!wbc->for_writepages)
  303. nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
  304. } else {
  305. err = nfs_writepage_sync(ctx, inode, page, 0,
  306. offset, priority);
  307. if (err >= 0) {
  308. if (err != offset)
  309. redirty_page_for_writepage(wbc, page);
  310. err = 0;
  311. }
  312. }
  313. unlock_kernel();
  314. put_nfs_open_context(ctx);
  315. out:
  316. unlock_page(page);
  317. if (inode_referenced)
  318. iput(inode);
  319. return err;
  320. }
  321. /*
  322. * Note: causes nfs_update_request() to block on the assumption
  323. * that the writeback is generated due to memory pressure.
  324. */
  325. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  326. {
  327. struct backing_dev_info *bdi = mapping->backing_dev_info;
  328. struct inode *inode = mapping->host;
  329. int err;
  330. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  331. err = generic_writepages(mapping, wbc);
  332. if (err)
  333. return err;
  334. while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
  335. if (wbc->nonblocking)
  336. return 0;
  337. nfs_wait_on_write_congestion(mapping, 0);
  338. }
  339. err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
  340. if (err < 0)
  341. goto out;
  342. nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
  343. wbc->nr_to_write -= err;
  344. if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
  345. err = nfs_wait_on_requests(inode, 0, 0);
  346. if (err < 0)
  347. goto out;
  348. }
  349. err = nfs_commit_inode(inode, wb_priority(wbc));
  350. if (err > 0) {
  351. wbc->nr_to_write -= err;
  352. err = 0;
  353. }
  354. out:
  355. clear_bit(BDI_write_congested, &bdi->state);
  356. wake_up_all(&nfs_write_congestion);
  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. nfs_clear_page_writeback(req);
  528. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  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. /* Call the NFS version-specific code */
  1112. status = NFS_PROTO(data->inode)->write_done(task, data);
  1113. if (status != 0)
  1114. return status;
  1115. nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  1116. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1117. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  1118. /* We tried a write call, but the server did not
  1119. * commit data to stable storage even though we
  1120. * requested it.
  1121. * Note: There is a known bug in Tru64 < 5.0 in which
  1122. * the server reports NFS_DATA_SYNC, but performs
  1123. * NFS_FILE_SYNC. We therefore implement this checking
  1124. * as a dprintk() in order to avoid filling syslog.
  1125. */
  1126. static unsigned long complain;
  1127. if (time_before(complain, jiffies)) {
  1128. dprintk("NFS: faulty NFS server %s:"
  1129. " (committed = %d) != (stable = %d)\n",
  1130. NFS_SERVER(data->inode)->hostname,
  1131. resp->verf->committed, argp->stable);
  1132. complain = jiffies + 300 * HZ;
  1133. }
  1134. }
  1135. #endif
  1136. /* Is this a short write? */
  1137. if (task->tk_status >= 0 && resp->count < argp->count) {
  1138. static unsigned long complain;
  1139. nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
  1140. /* Has the server at least made some progress? */
  1141. if (resp->count != 0) {
  1142. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1143. if (resp->verf->committed != NFS_UNSTABLE) {
  1144. /* Resend from where the server left off */
  1145. argp->offset += resp->count;
  1146. argp->pgbase += resp->count;
  1147. argp->count -= resp->count;
  1148. } else {
  1149. /* Resend as a stable write in order to avoid
  1150. * headaches in the case of a server crash.
  1151. */
  1152. argp->stable = NFS_FILE_SYNC;
  1153. }
  1154. rpc_restart_call(task);
  1155. return -EAGAIN;
  1156. }
  1157. if (time_before(complain, jiffies)) {
  1158. printk(KERN_WARNING
  1159. "NFS: Server wrote zero bytes, expected %u.\n",
  1160. argp->count);
  1161. complain = jiffies + 300 * HZ;
  1162. }
  1163. /* Can't do anything about it except throw an error. */
  1164. task->tk_status = -EIO;
  1165. }
  1166. return 0;
  1167. }
  1168. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1169. void nfs_commit_release(void *wdata)
  1170. {
  1171. nfs_commit_free(wdata);
  1172. }
  1173. /*
  1174. * Set up the argument/result storage required for the RPC call.
  1175. */
  1176. static void nfs_commit_rpcsetup(struct list_head *head,
  1177. struct nfs_write_data *data,
  1178. int how)
  1179. {
  1180. struct nfs_page *first;
  1181. struct inode *inode;
  1182. int flags;
  1183. /* Set up the RPC argument and reply structs
  1184. * NB: take care not to mess about with data->commit et al. */
  1185. list_splice_init(head, &data->pages);
  1186. first = nfs_list_entry(data->pages.next);
  1187. inode = first->wb_context->dentry->d_inode;
  1188. data->inode = inode;
  1189. data->cred = first->wb_context->cred;
  1190. data->args.fh = NFS_FH(data->inode);
  1191. /* Note: we always request a commit of the entire inode */
  1192. data->args.offset = 0;
  1193. data->args.count = 0;
  1194. data->res.count = 0;
  1195. data->res.fattr = &data->fattr;
  1196. data->res.verf = &data->verf;
  1197. nfs_fattr_init(&data->fattr);
  1198. /* Set up the initial task struct. */
  1199. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1200. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
  1201. NFS_PROTO(inode)->commit_setup(data, how);
  1202. data->task.tk_priority = flush_task_priority(how);
  1203. data->task.tk_cookie = (unsigned long)inode;
  1204. dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
  1205. }
  1206. /*
  1207. * Commit dirty pages
  1208. */
  1209. static int
  1210. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1211. {
  1212. struct nfs_write_data *data;
  1213. struct nfs_page *req;
  1214. data = nfs_commit_alloc();
  1215. if (!data)
  1216. goto out_bad;
  1217. /* Set up the argument struct */
  1218. nfs_commit_rpcsetup(head, data, how);
  1219. nfs_execute_write(data);
  1220. return 0;
  1221. out_bad:
  1222. while (!list_empty(head)) {
  1223. req = nfs_list_entry(head->next);
  1224. nfs_list_remove_request(req);
  1225. nfs_mark_request_commit(req);
  1226. nfs_clear_page_writeback(req);
  1227. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1228. }
  1229. return -ENOMEM;
  1230. }
  1231. /*
  1232. * COMMIT call returned
  1233. */
  1234. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1235. {
  1236. struct nfs_write_data *data = calldata;
  1237. struct nfs_page *req;
  1238. dprintk("NFS: %4d nfs_commit_done (status %d)\n",
  1239. task->tk_pid, task->tk_status);
  1240. /* Call the NFS version-specific code */
  1241. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1242. return;
  1243. while (!list_empty(&data->pages)) {
  1244. req = nfs_list_entry(data->pages.next);
  1245. nfs_list_remove_request(req);
  1246. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1247. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1248. req->wb_context->dentry->d_inode->i_sb->s_id,
  1249. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1250. req->wb_bytes,
  1251. (long long)req_offset(req));
  1252. if (task->tk_status < 0) {
  1253. req->wb_context->error = task->tk_status;
  1254. nfs_inode_remove_request(req);
  1255. dprintk(", error = %d\n", task->tk_status);
  1256. goto next;
  1257. }
  1258. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1259. * returned by the server against all stored verfs. */
  1260. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1261. /* We have a match */
  1262. nfs_inode_remove_request(req);
  1263. dprintk(" OK\n");
  1264. goto next;
  1265. }
  1266. /* We have a mismatch. Write the page again */
  1267. dprintk(" mismatch\n");
  1268. nfs_mark_request_dirty(req);
  1269. next:
  1270. nfs_clear_page_writeback(req);
  1271. }
  1272. }
  1273. static const struct rpc_call_ops nfs_commit_ops = {
  1274. .rpc_call_done = nfs_commit_done,
  1275. .rpc_release = nfs_commit_release,
  1276. };
  1277. #else
  1278. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1279. {
  1280. return 0;
  1281. }
  1282. #endif
  1283. static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
  1284. unsigned int npages, int how)
  1285. {
  1286. struct nfs_inode *nfsi = NFS_I(inode);
  1287. LIST_HEAD(head);
  1288. int res;
  1289. spin_lock(&nfsi->req_lock);
  1290. res = nfs_scan_dirty(inode, &head, idx_start, npages);
  1291. spin_unlock(&nfsi->req_lock);
  1292. if (res) {
  1293. int error = nfs_flush_list(inode, &head, res, how);
  1294. if (error < 0)
  1295. return error;
  1296. }
  1297. return res;
  1298. }
  1299. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1300. int nfs_commit_inode(struct inode *inode, int how)
  1301. {
  1302. struct nfs_inode *nfsi = NFS_I(inode);
  1303. LIST_HEAD(head);
  1304. int res;
  1305. spin_lock(&nfsi->req_lock);
  1306. res = nfs_scan_commit(inode, &head, 0, 0);
  1307. spin_unlock(&nfsi->req_lock);
  1308. if (res) {
  1309. int error = nfs_commit_list(inode, &head, how);
  1310. if (error < 0)
  1311. return error;
  1312. }
  1313. return res;
  1314. }
  1315. #endif
  1316. int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
  1317. unsigned int npages, int how)
  1318. {
  1319. struct nfs_inode *nfsi = NFS_I(inode);
  1320. LIST_HEAD(head);
  1321. int nocommit = how & FLUSH_NOCOMMIT;
  1322. int pages, ret;
  1323. how &= ~FLUSH_NOCOMMIT;
  1324. spin_lock(&nfsi->req_lock);
  1325. do {
  1326. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1327. if (ret != 0)
  1328. continue;
  1329. pages = nfs_scan_dirty(inode, &head, idx_start, npages);
  1330. if (pages != 0) {
  1331. spin_unlock(&nfsi->req_lock);
  1332. if (how & FLUSH_INVALIDATE)
  1333. nfs_cancel_dirty_list(&head);
  1334. else
  1335. ret = nfs_flush_list(inode, &head, pages, how);
  1336. spin_lock(&nfsi->req_lock);
  1337. continue;
  1338. }
  1339. if (nocommit)
  1340. break;
  1341. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1342. if (pages == 0)
  1343. break;
  1344. if (how & FLUSH_INVALIDATE) {
  1345. spin_unlock(&nfsi->req_lock);
  1346. nfs_cancel_commit_list(&head);
  1347. spin_lock(&nfsi->req_lock);
  1348. continue;
  1349. }
  1350. pages += nfs_scan_commit(inode, &head, 0, 0);
  1351. spin_unlock(&nfsi->req_lock);
  1352. ret = nfs_commit_list(inode, &head, how);
  1353. spin_lock(&nfsi->req_lock);
  1354. } while (ret >= 0);
  1355. spin_unlock(&nfsi->req_lock);
  1356. return ret;
  1357. }
  1358. int __init nfs_init_writepagecache(void)
  1359. {
  1360. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1361. sizeof(struct nfs_write_data),
  1362. 0, SLAB_HWCACHE_ALIGN,
  1363. NULL, NULL);
  1364. if (nfs_wdata_cachep == NULL)
  1365. return -ENOMEM;
  1366. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1367. nfs_wdata_cachep);
  1368. if (nfs_wdata_mempool == NULL)
  1369. return -ENOMEM;
  1370. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1371. nfs_wdata_cachep);
  1372. if (nfs_commit_mempool == NULL)
  1373. return -ENOMEM;
  1374. return 0;
  1375. }
  1376. void nfs_destroy_writepagecache(void)
  1377. {
  1378. mempool_destroy(nfs_commit_mempool);
  1379. mempool_destroy(nfs_wdata_mempool);
  1380. if (kmem_cache_destroy(nfs_wdata_cachep))
  1381. printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
  1382. }