write.c 43 KB

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