write.c 40 KB

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