write.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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_zone_page_state(req->wb_page, NR_FILE_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_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  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. static void nfs_cancel_requests(struct list_head *head)
  519. {
  520. struct nfs_page *req;
  521. while(!list_empty(head)) {
  522. req = nfs_list_entry(head->next);
  523. nfs_list_remove_request(req);
  524. nfs_inode_remove_request(req);
  525. nfs_clear_page_writeback(req);
  526. }
  527. }
  528. /*
  529. * nfs_scan_dirty - Scan an inode for dirty requests
  530. * @inode: NFS inode to scan
  531. * @dst: destination list
  532. * @idx_start: lower bound of page->index to scan.
  533. * @npages: idx_start + npages sets the upper bound to scan.
  534. *
  535. * Moves requests from the inode's dirty page list.
  536. * The requests are *not* checked to ensure that they form a contiguous set.
  537. */
  538. static int
  539. nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  540. {
  541. struct nfs_inode *nfsi = NFS_I(inode);
  542. int res = 0;
  543. if (nfsi->ndirty != 0) {
  544. res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
  545. nfsi->ndirty -= res;
  546. if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
  547. printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
  548. }
  549. return res;
  550. }
  551. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  552. /*
  553. * nfs_scan_commit - Scan an inode for commit requests
  554. * @inode: NFS inode to scan
  555. * @dst: destination list
  556. * @idx_start: lower bound of page->index to scan.
  557. * @npages: idx_start + npages sets the upper bound to scan.
  558. *
  559. * Moves requests from the inode's 'commit' request list.
  560. * The requests are *not* checked to ensure that they form a contiguous set.
  561. */
  562. static int
  563. nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  564. {
  565. struct nfs_inode *nfsi = NFS_I(inode);
  566. int res = 0;
  567. if (nfsi->ncommit != 0) {
  568. res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
  569. nfsi->ncommit -= res;
  570. if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
  571. printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
  572. }
  573. return res;
  574. }
  575. #else
  576. static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  577. {
  578. return 0;
  579. }
  580. #endif
  581. static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
  582. {
  583. struct backing_dev_info *bdi = mapping->backing_dev_info;
  584. DEFINE_WAIT(wait);
  585. int ret = 0;
  586. might_sleep();
  587. if (!bdi_write_congested(bdi))
  588. return 0;
  589. nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
  590. if (intr) {
  591. struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
  592. sigset_t oldset;
  593. rpc_clnt_sigmask(clnt, &oldset);
  594. prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
  595. if (bdi_write_congested(bdi)) {
  596. if (signalled())
  597. ret = -ERESTARTSYS;
  598. else
  599. schedule();
  600. }
  601. rpc_clnt_sigunmask(clnt, &oldset);
  602. } else {
  603. prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
  604. if (bdi_write_congested(bdi))
  605. schedule();
  606. }
  607. finish_wait(&nfs_write_congestion, &wait);
  608. return ret;
  609. }
  610. /*
  611. * Try to update any existing write request, or create one if there is none.
  612. * In order to match, the request's credentials must match those of
  613. * the calling process.
  614. *
  615. * Note: Should always be called with the Page Lock held!
  616. */
  617. static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
  618. struct inode *inode, struct page *page,
  619. unsigned int offset, unsigned int bytes)
  620. {
  621. struct nfs_server *server = NFS_SERVER(inode);
  622. struct nfs_inode *nfsi = NFS_I(inode);
  623. struct nfs_page *req, *new = NULL;
  624. unsigned long rqend, end;
  625. end = offset + bytes;
  626. if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
  627. return ERR_PTR(-ERESTARTSYS);
  628. for (;;) {
  629. /* Loop over all inode entries and see if we find
  630. * A request for the page we wish to update
  631. */
  632. spin_lock(&nfsi->req_lock);
  633. req = _nfs_find_request(inode, page->index);
  634. if (req) {
  635. if (!nfs_lock_request_dontget(req)) {
  636. int error;
  637. spin_unlock(&nfsi->req_lock);
  638. error = nfs_wait_on_request(req);
  639. nfs_release_request(req);
  640. if (error < 0) {
  641. if (new)
  642. nfs_release_request(new);
  643. return ERR_PTR(error);
  644. }
  645. continue;
  646. }
  647. spin_unlock(&nfsi->req_lock);
  648. if (new)
  649. nfs_release_request(new);
  650. break;
  651. }
  652. if (new) {
  653. int error;
  654. nfs_lock_request_dontget(new);
  655. error = nfs_inode_add_request(inode, new);
  656. if (error) {
  657. spin_unlock(&nfsi->req_lock);
  658. nfs_unlock_request(new);
  659. return ERR_PTR(error);
  660. }
  661. spin_unlock(&nfsi->req_lock);
  662. nfs_mark_request_dirty(new);
  663. return new;
  664. }
  665. spin_unlock(&nfsi->req_lock);
  666. new = nfs_create_request(ctx, inode, page, offset, bytes);
  667. if (IS_ERR(new))
  668. return new;
  669. }
  670. /* We have a request for our page.
  671. * If the creds don't match, or the
  672. * page addresses don't match,
  673. * tell the caller to wait on the conflicting
  674. * request.
  675. */
  676. rqend = req->wb_offset + req->wb_bytes;
  677. if (req->wb_context != ctx
  678. || req->wb_page != page
  679. || !nfs_dirty_request(req)
  680. || offset > rqend || end < req->wb_offset) {
  681. nfs_unlock_request(req);
  682. return ERR_PTR(-EBUSY);
  683. }
  684. /* Okay, the request matches. Update the region */
  685. if (offset < req->wb_offset) {
  686. req->wb_offset = offset;
  687. req->wb_pgbase = offset;
  688. req->wb_bytes = rqend - req->wb_offset;
  689. }
  690. if (end > rqend)
  691. req->wb_bytes = end - req->wb_offset;
  692. return req;
  693. }
  694. int nfs_flush_incompatible(struct file *file, struct page *page)
  695. {
  696. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  697. struct inode *inode = page->mapping->host;
  698. struct nfs_page *req;
  699. int status = 0;
  700. /*
  701. * Look for a request corresponding to this page. If there
  702. * is one, and it belongs to another file, we flush it out
  703. * before we try to copy anything into the page. Do this
  704. * due to the lack of an ACCESS-type call in NFSv2.
  705. * Also do the same if we find a request from an existing
  706. * dropped page.
  707. */
  708. req = nfs_find_request(inode, page->index);
  709. if (req) {
  710. if (req->wb_page != page || ctx != req->wb_context)
  711. status = nfs_wb_page(inode, page);
  712. nfs_release_request(req);
  713. }
  714. return (status < 0) ? status : 0;
  715. }
  716. /*
  717. * Update and possibly write a cached page of an NFS file.
  718. *
  719. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  720. * things with a page scheduled for an RPC call (e.g. invalidate it).
  721. */
  722. int nfs_updatepage(struct file *file, struct page *page,
  723. unsigned int offset, unsigned int count)
  724. {
  725. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  726. struct inode *inode = page->mapping->host;
  727. struct nfs_page *req;
  728. int status = 0;
  729. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  730. dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
  731. file->f_dentry->d_parent->d_name.name,
  732. file->f_dentry->d_name.name, count,
  733. (long long)(page_offset(page) +offset));
  734. if (IS_SYNC(inode)) {
  735. status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
  736. if (status > 0) {
  737. if (offset == 0 && status == PAGE_CACHE_SIZE)
  738. SetPageUptodate(page);
  739. return 0;
  740. }
  741. return status;
  742. }
  743. /* If we're not using byte range locks, and we know the page
  744. * is entirely in cache, it may be more efficient to avoid
  745. * fragmenting write requests.
  746. */
  747. if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
  748. loff_t end_offs = i_size_read(inode) - 1;
  749. unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
  750. count += offset;
  751. offset = 0;
  752. if (unlikely(end_offs < 0)) {
  753. /* Do nothing */
  754. } else if (page->index == end_index) {
  755. unsigned int pglen;
  756. pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
  757. if (count < pglen)
  758. count = pglen;
  759. } else if (page->index < end_index)
  760. count = PAGE_CACHE_SIZE;
  761. }
  762. /*
  763. * Try to find an NFS request corresponding to this page
  764. * and update it.
  765. * If the existing request cannot be updated, we must flush
  766. * it out now.
  767. */
  768. do {
  769. req = nfs_update_request(ctx, inode, page, offset, count);
  770. status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
  771. if (status != -EBUSY)
  772. break;
  773. /* Request could not be updated. Flush it out and try again */
  774. status = nfs_wb_page(inode, page);
  775. } while (status >= 0);
  776. if (status < 0)
  777. goto done;
  778. status = 0;
  779. /* Update file length */
  780. nfs_grow_file(page, offset, count);
  781. /* Set the PG_uptodate flag? */
  782. nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
  783. nfs_unlock_request(req);
  784. done:
  785. dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
  786. status, (long long)i_size_read(inode));
  787. if (status < 0)
  788. ClearPageUptodate(page);
  789. return status;
  790. }
  791. static void nfs_writepage_release(struct nfs_page *req)
  792. {
  793. end_page_writeback(req->wb_page);
  794. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  795. if (!PageError(req->wb_page)) {
  796. if (NFS_NEED_RESCHED(req)) {
  797. nfs_mark_request_dirty(req);
  798. goto out;
  799. } else if (NFS_NEED_COMMIT(req)) {
  800. nfs_mark_request_commit(req);
  801. goto out;
  802. }
  803. }
  804. nfs_inode_remove_request(req);
  805. out:
  806. nfs_clear_commit(req);
  807. nfs_clear_reschedule(req);
  808. #else
  809. nfs_inode_remove_request(req);
  810. #endif
  811. nfs_clear_page_writeback(req);
  812. }
  813. static inline int flush_task_priority(int how)
  814. {
  815. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  816. case FLUSH_HIGHPRI:
  817. return RPC_PRIORITY_HIGH;
  818. case FLUSH_LOWPRI:
  819. return RPC_PRIORITY_LOW;
  820. }
  821. return RPC_PRIORITY_NORMAL;
  822. }
  823. /*
  824. * Set up the argument/result storage required for the RPC call.
  825. */
  826. static void nfs_write_rpcsetup(struct nfs_page *req,
  827. struct nfs_write_data *data,
  828. const struct rpc_call_ops *call_ops,
  829. unsigned int count, unsigned int offset,
  830. int how)
  831. {
  832. struct inode *inode;
  833. int flags;
  834. /* Set up the RPC argument and reply structs
  835. * NB: take care not to mess about with data->commit et al. */
  836. data->req = req;
  837. data->inode = inode = req->wb_context->dentry->d_inode;
  838. data->cred = req->wb_context->cred;
  839. data->args.fh = NFS_FH(inode);
  840. data->args.offset = req_offset(req) + offset;
  841. data->args.pgbase = req->wb_pgbase + offset;
  842. data->args.pages = data->pagevec;
  843. data->args.count = count;
  844. data->args.context = req->wb_context;
  845. data->res.fattr = &data->fattr;
  846. data->res.count = count;
  847. data->res.verf = &data->verf;
  848. nfs_fattr_init(&data->fattr);
  849. /* Set up the initial task struct. */
  850. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  851. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
  852. NFS_PROTO(inode)->write_setup(data, how);
  853. data->task.tk_priority = flush_task_priority(how);
  854. data->task.tk_cookie = (unsigned long)inode;
  855. dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  856. data->task.tk_pid,
  857. inode->i_sb->s_id,
  858. (long long)NFS_FILEID(inode),
  859. count,
  860. (unsigned long long)data->args.offset);
  861. }
  862. static void nfs_execute_write(struct nfs_write_data *data)
  863. {
  864. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  865. sigset_t oldset;
  866. rpc_clnt_sigmask(clnt, &oldset);
  867. lock_kernel();
  868. rpc_execute(&data->task);
  869. unlock_kernel();
  870. rpc_clnt_sigunmask(clnt, &oldset);
  871. }
  872. /*
  873. * Generate multiple small requests to write out a single
  874. * contiguous dirty area on one page.
  875. */
  876. static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
  877. {
  878. struct nfs_page *req = nfs_list_entry(head->next);
  879. struct page *page = req->wb_page;
  880. struct nfs_write_data *data;
  881. unsigned int wsize = NFS_SERVER(inode)->wsize;
  882. unsigned int nbytes, offset;
  883. int requests = 0;
  884. LIST_HEAD(list);
  885. nfs_list_remove_request(req);
  886. nbytes = req->wb_bytes;
  887. for (;;) {
  888. data = nfs_writedata_alloc(1);
  889. if (!data)
  890. goto out_bad;
  891. list_add(&data->pages, &list);
  892. requests++;
  893. if (nbytes <= wsize)
  894. break;
  895. nbytes -= wsize;
  896. }
  897. atomic_set(&req->wb_complete, requests);
  898. ClearPageError(page);
  899. set_page_writeback(page);
  900. offset = 0;
  901. nbytes = req->wb_bytes;
  902. do {
  903. data = list_entry(list.next, struct nfs_write_data, pages);
  904. list_del_init(&data->pages);
  905. data->pagevec[0] = page;
  906. if (nbytes > wsize) {
  907. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  908. wsize, offset, how);
  909. offset += wsize;
  910. nbytes -= wsize;
  911. } else {
  912. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  913. nbytes, offset, how);
  914. nbytes = 0;
  915. }
  916. nfs_execute_write(data);
  917. } while (nbytes != 0);
  918. return 0;
  919. out_bad:
  920. while (!list_empty(&list)) {
  921. data = list_entry(list.next, struct nfs_write_data, pages);
  922. list_del(&data->pages);
  923. nfs_writedata_free(data);
  924. }
  925. nfs_mark_request_dirty(req);
  926. nfs_clear_page_writeback(req);
  927. return -ENOMEM;
  928. }
  929. /*
  930. * Create an RPC task for the given write request and kick it.
  931. * The page must have been locked by the caller.
  932. *
  933. * It may happen that the page we're passed is not marked dirty.
  934. * This is the case if nfs_updatepage detects a conflicting request
  935. * that has been written but not committed.
  936. */
  937. static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
  938. {
  939. struct nfs_page *req;
  940. struct page **pages;
  941. struct nfs_write_data *data;
  942. unsigned int count;
  943. data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
  944. if (!data)
  945. goto out_bad;
  946. pages = data->pagevec;
  947. count = 0;
  948. while (!list_empty(head)) {
  949. req = nfs_list_entry(head->next);
  950. nfs_list_remove_request(req);
  951. nfs_list_add_request(req, &data->pages);
  952. ClearPageError(req->wb_page);
  953. set_page_writeback(req->wb_page);
  954. *pages++ = req->wb_page;
  955. count += req->wb_bytes;
  956. }
  957. req = nfs_list_entry(data->pages.next);
  958. /* Set up the argument struct */
  959. nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
  960. nfs_execute_write(data);
  961. return 0;
  962. out_bad:
  963. while (!list_empty(head)) {
  964. struct nfs_page *req = nfs_list_entry(head->next);
  965. nfs_list_remove_request(req);
  966. nfs_mark_request_dirty(req);
  967. nfs_clear_page_writeback(req);
  968. }
  969. return -ENOMEM;
  970. }
  971. static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
  972. {
  973. LIST_HEAD(one_request);
  974. int (*flush_one)(struct inode *, struct list_head *, int);
  975. struct nfs_page *req;
  976. int wpages = NFS_SERVER(inode)->wpages;
  977. int wsize = NFS_SERVER(inode)->wsize;
  978. int error;
  979. flush_one = nfs_flush_one;
  980. if (wsize < PAGE_CACHE_SIZE)
  981. flush_one = nfs_flush_multi;
  982. /* For single writes, FLUSH_STABLE is more efficient */
  983. if (npages <= wpages && npages == NFS_I(inode)->npages
  984. && nfs_list_entry(head->next)->wb_bytes <= wsize)
  985. how |= FLUSH_STABLE;
  986. do {
  987. nfs_coalesce_requests(head, &one_request, wpages);
  988. req = nfs_list_entry(one_request.next);
  989. error = flush_one(inode, &one_request, how);
  990. if (error < 0)
  991. goto out_err;
  992. } while (!list_empty(head));
  993. return 0;
  994. out_err:
  995. while (!list_empty(head)) {
  996. req = nfs_list_entry(head->next);
  997. nfs_list_remove_request(req);
  998. nfs_mark_request_dirty(req);
  999. nfs_clear_page_writeback(req);
  1000. }
  1001. return error;
  1002. }
  1003. /*
  1004. * Handle a write reply that flushed part of a page.
  1005. */
  1006. static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
  1007. {
  1008. struct nfs_write_data *data = calldata;
  1009. struct nfs_page *req = data->req;
  1010. struct page *page = req->wb_page;
  1011. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  1012. req->wb_context->dentry->d_inode->i_sb->s_id,
  1013. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1014. req->wb_bytes,
  1015. (long long)req_offset(req));
  1016. if (nfs_writeback_done(task, data) != 0)
  1017. return;
  1018. if (task->tk_status < 0) {
  1019. ClearPageUptodate(page);
  1020. SetPageError(page);
  1021. req->wb_context->error = task->tk_status;
  1022. dprintk(", error = %d\n", task->tk_status);
  1023. } else {
  1024. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1025. if (data->verf.committed < NFS_FILE_SYNC) {
  1026. if (!NFS_NEED_COMMIT(req)) {
  1027. nfs_defer_commit(req);
  1028. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  1029. dprintk(" defer commit\n");
  1030. } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
  1031. nfs_defer_reschedule(req);
  1032. dprintk(" server reboot detected\n");
  1033. }
  1034. } else
  1035. #endif
  1036. dprintk(" OK\n");
  1037. }
  1038. if (atomic_dec_and_test(&req->wb_complete))
  1039. nfs_writepage_release(req);
  1040. }
  1041. static const struct rpc_call_ops nfs_write_partial_ops = {
  1042. .rpc_call_done = nfs_writeback_done_partial,
  1043. .rpc_release = nfs_writedata_release,
  1044. };
  1045. /*
  1046. * Handle a write reply that flushes a whole page.
  1047. *
  1048. * FIXME: There is an inherent race with invalidate_inode_pages and
  1049. * writebacks since the page->count is kept > 1 for as long
  1050. * as the page has a write request pending.
  1051. */
  1052. static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
  1053. {
  1054. struct nfs_write_data *data = calldata;
  1055. struct nfs_page *req;
  1056. struct page *page;
  1057. if (nfs_writeback_done(task, data) != 0)
  1058. return;
  1059. /* Update attributes as result of writeback. */
  1060. while (!list_empty(&data->pages)) {
  1061. req = nfs_list_entry(data->pages.next);
  1062. nfs_list_remove_request(req);
  1063. page = req->wb_page;
  1064. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  1065. req->wb_context->dentry->d_inode->i_sb->s_id,
  1066. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1067. req->wb_bytes,
  1068. (long long)req_offset(req));
  1069. if (task->tk_status < 0) {
  1070. ClearPageUptodate(page);
  1071. SetPageError(page);
  1072. req->wb_context->error = task->tk_status;
  1073. end_page_writeback(page);
  1074. nfs_inode_remove_request(req);
  1075. dprintk(", error = %d\n", task->tk_status);
  1076. goto next;
  1077. }
  1078. end_page_writeback(page);
  1079. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1080. if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
  1081. nfs_inode_remove_request(req);
  1082. dprintk(" OK\n");
  1083. goto next;
  1084. }
  1085. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  1086. nfs_mark_request_commit(req);
  1087. dprintk(" marked for commit\n");
  1088. #else
  1089. nfs_inode_remove_request(req);
  1090. #endif
  1091. next:
  1092. nfs_clear_page_writeback(req);
  1093. }
  1094. }
  1095. static const struct rpc_call_ops nfs_write_full_ops = {
  1096. .rpc_call_done = nfs_writeback_done_full,
  1097. .rpc_release = nfs_writedata_release,
  1098. };
  1099. /*
  1100. * This function is called when the WRITE call is complete.
  1101. */
  1102. int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
  1103. {
  1104. struct nfs_writeargs *argp = &data->args;
  1105. struct nfs_writeres *resp = &data->res;
  1106. int status;
  1107. dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
  1108. task->tk_pid, task->tk_status);
  1109. /* Call the NFS version-specific code */
  1110. status = NFS_PROTO(data->inode)->write_done(task, data);
  1111. if (status != 0)
  1112. return status;
  1113. nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  1114. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1115. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  1116. /* We tried a write call, but the server did not
  1117. * commit data to stable storage even though we
  1118. * requested it.
  1119. * Note: There is a known bug in Tru64 < 5.0 in which
  1120. * the server reports NFS_DATA_SYNC, but performs
  1121. * NFS_FILE_SYNC. We therefore implement this checking
  1122. * as a dprintk() in order to avoid filling syslog.
  1123. */
  1124. static unsigned long complain;
  1125. if (time_before(complain, jiffies)) {
  1126. dprintk("NFS: faulty NFS server %s:"
  1127. " (committed = %d) != (stable = %d)\n",
  1128. NFS_SERVER(data->inode)->hostname,
  1129. resp->verf->committed, argp->stable);
  1130. complain = jiffies + 300 * HZ;
  1131. }
  1132. }
  1133. #endif
  1134. /* Is this a short write? */
  1135. if (task->tk_status >= 0 && resp->count < argp->count) {
  1136. static unsigned long complain;
  1137. nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
  1138. /* Has the server at least made some progress? */
  1139. if (resp->count != 0) {
  1140. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1141. if (resp->verf->committed != NFS_UNSTABLE) {
  1142. /* Resend from where the server left off */
  1143. argp->offset += resp->count;
  1144. argp->pgbase += resp->count;
  1145. argp->count -= resp->count;
  1146. } else {
  1147. /* Resend as a stable write in order to avoid
  1148. * headaches in the case of a server crash.
  1149. */
  1150. argp->stable = NFS_FILE_SYNC;
  1151. }
  1152. rpc_restart_call(task);
  1153. return -EAGAIN;
  1154. }
  1155. if (time_before(complain, jiffies)) {
  1156. printk(KERN_WARNING
  1157. "NFS: Server wrote zero bytes, expected %u.\n",
  1158. argp->count);
  1159. complain = jiffies + 300 * HZ;
  1160. }
  1161. /* Can't do anything about it except throw an error. */
  1162. task->tk_status = -EIO;
  1163. }
  1164. return 0;
  1165. }
  1166. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1167. void nfs_commit_release(void *wdata)
  1168. {
  1169. nfs_commit_free(wdata);
  1170. }
  1171. /*
  1172. * Set up the argument/result storage required for the RPC call.
  1173. */
  1174. static void nfs_commit_rpcsetup(struct list_head *head,
  1175. struct nfs_write_data *data,
  1176. int how)
  1177. {
  1178. struct nfs_page *first;
  1179. struct inode *inode;
  1180. int flags;
  1181. /* Set up the RPC argument and reply structs
  1182. * NB: take care not to mess about with data->commit et al. */
  1183. list_splice_init(head, &data->pages);
  1184. first = nfs_list_entry(data->pages.next);
  1185. inode = first->wb_context->dentry->d_inode;
  1186. data->inode = inode;
  1187. data->cred = first->wb_context->cred;
  1188. data->args.fh = NFS_FH(data->inode);
  1189. /* Note: we always request a commit of the entire inode */
  1190. data->args.offset = 0;
  1191. data->args.count = 0;
  1192. data->res.count = 0;
  1193. data->res.fattr = &data->fattr;
  1194. data->res.verf = &data->verf;
  1195. nfs_fattr_init(&data->fattr);
  1196. /* Set up the initial task struct. */
  1197. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1198. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
  1199. NFS_PROTO(inode)->commit_setup(data, how);
  1200. data->task.tk_priority = flush_task_priority(how);
  1201. data->task.tk_cookie = (unsigned long)inode;
  1202. dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
  1203. }
  1204. /*
  1205. * Commit dirty pages
  1206. */
  1207. static int
  1208. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1209. {
  1210. struct nfs_write_data *data;
  1211. struct nfs_page *req;
  1212. data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
  1213. if (!data)
  1214. goto out_bad;
  1215. /* Set up the argument struct */
  1216. nfs_commit_rpcsetup(head, data, how);
  1217. nfs_execute_write(data);
  1218. return 0;
  1219. out_bad:
  1220. while (!list_empty(head)) {
  1221. req = nfs_list_entry(head->next);
  1222. nfs_list_remove_request(req);
  1223. nfs_mark_request_commit(req);
  1224. nfs_clear_page_writeback(req);
  1225. }
  1226. return -ENOMEM;
  1227. }
  1228. /*
  1229. * COMMIT call returned
  1230. */
  1231. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1232. {
  1233. struct nfs_write_data *data = calldata;
  1234. struct nfs_page *req;
  1235. dprintk("NFS: %4d nfs_commit_done (status %d)\n",
  1236. task->tk_pid, task->tk_status);
  1237. /* Call the NFS version-specific code */
  1238. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1239. return;
  1240. while (!list_empty(&data->pages)) {
  1241. req = nfs_list_entry(data->pages.next);
  1242. nfs_list_remove_request(req);
  1243. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1244. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1245. req->wb_context->dentry->d_inode->i_sb->s_id,
  1246. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1247. req->wb_bytes,
  1248. (long long)req_offset(req));
  1249. if (task->tk_status < 0) {
  1250. req->wb_context->error = task->tk_status;
  1251. nfs_inode_remove_request(req);
  1252. dprintk(", error = %d\n", task->tk_status);
  1253. goto next;
  1254. }
  1255. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1256. * returned by the server against all stored verfs. */
  1257. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1258. /* We have a match */
  1259. nfs_inode_remove_request(req);
  1260. dprintk(" OK\n");
  1261. goto next;
  1262. }
  1263. /* We have a mismatch. Write the page again */
  1264. dprintk(" mismatch\n");
  1265. nfs_mark_request_dirty(req);
  1266. next:
  1267. nfs_clear_page_writeback(req);
  1268. }
  1269. }
  1270. static const struct rpc_call_ops nfs_commit_ops = {
  1271. .rpc_call_done = nfs_commit_done,
  1272. .rpc_release = nfs_commit_release,
  1273. };
  1274. #else
  1275. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1276. {
  1277. return 0;
  1278. }
  1279. #endif
  1280. static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
  1281. unsigned int npages, int how)
  1282. {
  1283. struct nfs_inode *nfsi = NFS_I(inode);
  1284. LIST_HEAD(head);
  1285. int res;
  1286. spin_lock(&nfsi->req_lock);
  1287. res = nfs_scan_dirty(inode, &head, idx_start, npages);
  1288. spin_unlock(&nfsi->req_lock);
  1289. if (res) {
  1290. int error = nfs_flush_list(inode, &head, res, how);
  1291. if (error < 0)
  1292. return error;
  1293. }
  1294. return res;
  1295. }
  1296. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1297. int nfs_commit_inode(struct inode *inode, int how)
  1298. {
  1299. struct nfs_inode *nfsi = NFS_I(inode);
  1300. LIST_HEAD(head);
  1301. int res;
  1302. spin_lock(&nfsi->req_lock);
  1303. res = nfs_scan_commit(inode, &head, 0, 0);
  1304. spin_unlock(&nfsi->req_lock);
  1305. if (res) {
  1306. int error = nfs_commit_list(inode, &head, how);
  1307. if (error < 0)
  1308. return error;
  1309. }
  1310. return res;
  1311. }
  1312. #endif
  1313. int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
  1314. unsigned int npages, int how)
  1315. {
  1316. struct nfs_inode *nfsi = NFS_I(inode);
  1317. LIST_HEAD(head);
  1318. int nocommit = how & FLUSH_NOCOMMIT;
  1319. int pages, ret;
  1320. how &= ~FLUSH_NOCOMMIT;
  1321. spin_lock(&nfsi->req_lock);
  1322. do {
  1323. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1324. if (ret != 0)
  1325. continue;
  1326. pages = nfs_scan_dirty(inode, &head, idx_start, npages);
  1327. if (pages != 0) {
  1328. spin_unlock(&nfsi->req_lock);
  1329. if (how & FLUSH_INVALIDATE)
  1330. nfs_cancel_requests(&head);
  1331. else
  1332. ret = nfs_flush_list(inode, &head, pages, how);
  1333. spin_lock(&nfsi->req_lock);
  1334. continue;
  1335. }
  1336. if (nocommit)
  1337. break;
  1338. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1339. if (pages == 0)
  1340. break;
  1341. if (how & FLUSH_INVALIDATE) {
  1342. spin_unlock(&nfsi->req_lock);
  1343. nfs_cancel_requests(&head);
  1344. spin_lock(&nfsi->req_lock);
  1345. continue;
  1346. }
  1347. pages += nfs_scan_commit(inode, &head, 0, 0);
  1348. spin_unlock(&nfsi->req_lock);
  1349. ret = nfs_commit_list(inode, &head, how);
  1350. spin_lock(&nfsi->req_lock);
  1351. } while (ret >= 0);
  1352. spin_unlock(&nfsi->req_lock);
  1353. return ret;
  1354. }
  1355. int __init nfs_init_writepagecache(void)
  1356. {
  1357. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1358. sizeof(struct nfs_write_data),
  1359. 0, SLAB_HWCACHE_ALIGN,
  1360. NULL, NULL);
  1361. if (nfs_wdata_cachep == NULL)
  1362. return -ENOMEM;
  1363. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1364. nfs_wdata_cachep);
  1365. if (nfs_wdata_mempool == NULL)
  1366. return -ENOMEM;
  1367. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1368. nfs_wdata_cachep);
  1369. if (nfs_commit_mempool == NULL)
  1370. return -ENOMEM;
  1371. return 0;
  1372. }
  1373. void nfs_destroy_writepagecache(void)
  1374. {
  1375. mempool_destroy(nfs_commit_mempool);
  1376. mempool_destroy(nfs_wdata_mempool);
  1377. if (kmem_cache_destroy(nfs_wdata_cachep))
  1378. printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
  1379. }