write.c 38 KB

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