ops_address.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/pagevec.h>
  16. #include <linux/mpage.h>
  17. #include <linux/fs.h>
  18. #include <linux/writeback.h>
  19. #include <linux/swap.h>
  20. #include <linux/gfs2_ondisk.h>
  21. #include <linux/lm_interface.h>
  22. #include <linux/backing-dev.h>
  23. #include "gfs2.h"
  24. #include "incore.h"
  25. #include "bmap.h"
  26. #include "glock.h"
  27. #include "inode.h"
  28. #include "log.h"
  29. #include "meta_io.h"
  30. #include "ops_address.h"
  31. #include "quota.h"
  32. #include "trans.h"
  33. #include "rgrp.h"
  34. #include "super.h"
  35. #include "util.h"
  36. #include "glops.h"
  37. static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  38. unsigned int from, unsigned int to)
  39. {
  40. struct buffer_head *head = page_buffers(page);
  41. unsigned int bsize = head->b_size;
  42. struct buffer_head *bh;
  43. unsigned int start, end;
  44. for (bh = head, start = 0; bh != head || !start;
  45. bh = bh->b_this_page, start = end) {
  46. end = start + bsize;
  47. if (end <= from || start >= to)
  48. continue;
  49. if (gfs2_is_jdata(ip))
  50. set_buffer_uptodate(bh);
  51. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  52. }
  53. }
  54. /**
  55. * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  56. * @inode: The inode
  57. * @lblock: The block number to look up
  58. * @bh_result: The buffer head to return the result in
  59. * @create: Non-zero if we may add block to the file
  60. *
  61. * Returns: errno
  62. */
  63. static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
  64. struct buffer_head *bh_result, int create)
  65. {
  66. int error;
  67. error = gfs2_block_map(inode, lblock, bh_result, 0);
  68. if (error)
  69. return error;
  70. if (!buffer_mapped(bh_result))
  71. return -EIO;
  72. return 0;
  73. }
  74. static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
  75. struct buffer_head *bh_result, int create)
  76. {
  77. return gfs2_block_map(inode, lblock, bh_result, 0);
  78. }
  79. /**
  80. * gfs2_writepage_common - Common bits of writepage
  81. * @page: The page to be written
  82. * @wbc: The writeback control
  83. *
  84. * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
  85. */
  86. static int gfs2_writepage_common(struct page *page,
  87. struct writeback_control *wbc)
  88. {
  89. struct inode *inode = page->mapping->host;
  90. struct gfs2_inode *ip = GFS2_I(inode);
  91. struct gfs2_sbd *sdp = GFS2_SB(inode);
  92. loff_t i_size = i_size_read(inode);
  93. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  94. unsigned offset;
  95. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
  96. goto out;
  97. if (current->journal_info)
  98. goto redirty;
  99. /* Is the page fully outside i_size? (truncate in progress) */
  100. offset = i_size & (PAGE_CACHE_SIZE-1);
  101. if (page->index > end_index || (page->index == end_index && !offset)) {
  102. page->mapping->a_ops->invalidatepage(page, 0);
  103. goto out;
  104. }
  105. return 1;
  106. redirty:
  107. redirty_page_for_writepage(wbc, page);
  108. out:
  109. unlock_page(page);
  110. return 0;
  111. }
  112. /**
  113. * gfs2_writeback_writepage - Write page for writeback mappings
  114. * @page: The page
  115. * @wbc: The writeback control
  116. *
  117. */
  118. static int gfs2_writeback_writepage(struct page *page,
  119. struct writeback_control *wbc)
  120. {
  121. int ret;
  122. ret = gfs2_writepage_common(page, wbc);
  123. if (ret <= 0)
  124. return ret;
  125. ret = mpage_writepage(page, gfs2_get_block_noalloc, wbc);
  126. if (ret == -EAGAIN)
  127. ret = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  128. return ret;
  129. }
  130. /**
  131. * gfs2_ordered_writepage - Write page for ordered data files
  132. * @page: The page to write
  133. * @wbc: The writeback control
  134. *
  135. */
  136. static int gfs2_ordered_writepage(struct page *page,
  137. struct writeback_control *wbc)
  138. {
  139. struct inode *inode = page->mapping->host;
  140. struct gfs2_inode *ip = GFS2_I(inode);
  141. int ret;
  142. ret = gfs2_writepage_common(page, wbc);
  143. if (ret <= 0)
  144. return ret;
  145. if (!page_has_buffers(page)) {
  146. create_empty_buffers(page, inode->i_sb->s_blocksize,
  147. (1 << BH_Dirty)|(1 << BH_Uptodate));
  148. }
  149. gfs2_page_add_databufs(ip, page, 0, inode->i_sb->s_blocksize-1);
  150. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  151. }
  152. /**
  153. * __gfs2_jdata_writepage - The core of jdata writepage
  154. * @page: The page to write
  155. * @wbc: The writeback control
  156. *
  157. * This is shared between writepage and writepages and implements the
  158. * core of the writepage operation. If a transaction is required then
  159. * PageChecked will have been set and the transaction will have
  160. * already been started before this is called.
  161. */
  162. static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  163. {
  164. struct inode *inode = page->mapping->host;
  165. struct gfs2_inode *ip = GFS2_I(inode);
  166. struct gfs2_sbd *sdp = GFS2_SB(inode);
  167. if (PageChecked(page)) {
  168. ClearPageChecked(page);
  169. if (!page_has_buffers(page)) {
  170. create_empty_buffers(page, inode->i_sb->s_blocksize,
  171. (1 << BH_Dirty)|(1 << BH_Uptodate));
  172. }
  173. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
  174. }
  175. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  176. }
  177. /**
  178. * gfs2_jdata_writepage - Write complete page
  179. * @page: Page to write
  180. *
  181. * Returns: errno
  182. *
  183. */
  184. static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  185. {
  186. struct inode *inode = page->mapping->host;
  187. struct gfs2_sbd *sdp = GFS2_SB(inode);
  188. int ret;
  189. int done_trans = 0;
  190. if (PageChecked(page)) {
  191. if (wbc->sync_mode != WB_SYNC_ALL)
  192. goto out_ignore;
  193. ret = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  194. if (ret)
  195. goto out_ignore;
  196. done_trans = 1;
  197. }
  198. ret = gfs2_writepage_common(page, wbc);
  199. if (ret > 0)
  200. ret = __gfs2_jdata_writepage(page, wbc);
  201. if (done_trans)
  202. gfs2_trans_end(sdp);
  203. return ret;
  204. out_ignore:
  205. redirty_page_for_writepage(wbc, page);
  206. unlock_page(page);
  207. return 0;
  208. }
  209. /**
  210. * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
  211. * @mapping: The mapping to write
  212. * @wbc: Write-back control
  213. *
  214. * For the data=writeback case we can already ignore buffer heads
  215. * and write whole extents at once. This is a big reduction in the
  216. * number of I/O requests we send and the bmap calls we make in this case.
  217. */
  218. static int gfs2_writeback_writepages(struct address_space *mapping,
  219. struct writeback_control *wbc)
  220. {
  221. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  222. }
  223. /**
  224. * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
  225. * @mapping: The mapping
  226. * @wbc: The writeback control
  227. * @writepage: The writepage function to call for each page
  228. * @pvec: The vector of pages
  229. * @nr_pages: The number of pages to write
  230. *
  231. * Returns: non-zero if loop should terminate, zero otherwise
  232. */
  233. static int gfs2_write_jdata_pagevec(struct address_space *mapping,
  234. struct writeback_control *wbc,
  235. struct pagevec *pvec,
  236. int nr_pages, pgoff_t end)
  237. {
  238. struct inode *inode = mapping->host;
  239. struct gfs2_sbd *sdp = GFS2_SB(inode);
  240. loff_t i_size = i_size_read(inode);
  241. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  242. unsigned offset = i_size & (PAGE_CACHE_SIZE-1);
  243. unsigned nrblocks = nr_pages * (PAGE_CACHE_SIZE/inode->i_sb->s_blocksize);
  244. struct backing_dev_info *bdi = mapping->backing_dev_info;
  245. int i;
  246. int ret;
  247. ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
  248. if (ret < 0)
  249. return ret;
  250. for(i = 0; i < nr_pages; i++) {
  251. struct page *page = pvec->pages[i];
  252. lock_page(page);
  253. if (unlikely(page->mapping != mapping)) {
  254. unlock_page(page);
  255. continue;
  256. }
  257. if (!wbc->range_cyclic && page->index > end) {
  258. ret = 1;
  259. unlock_page(page);
  260. continue;
  261. }
  262. if (wbc->sync_mode != WB_SYNC_NONE)
  263. wait_on_page_writeback(page);
  264. if (PageWriteback(page) ||
  265. !clear_page_dirty_for_io(page)) {
  266. unlock_page(page);
  267. continue;
  268. }
  269. /* Is the page fully outside i_size? (truncate in progress) */
  270. if (page->index > end_index || (page->index == end_index && !offset)) {
  271. page->mapping->a_ops->invalidatepage(page, 0);
  272. unlock_page(page);
  273. continue;
  274. }
  275. ret = __gfs2_jdata_writepage(page, wbc);
  276. if (ret || (--(wbc->nr_to_write) <= 0))
  277. ret = 1;
  278. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  279. wbc->encountered_congestion = 1;
  280. ret = 1;
  281. }
  282. }
  283. gfs2_trans_end(sdp);
  284. return ret;
  285. }
  286. /**
  287. * gfs2_write_cache_jdata - Like write_cache_pages but different
  288. * @mapping: The mapping to write
  289. * @wbc: The writeback control
  290. * @writepage: The writepage function to call
  291. * @data: The data to pass to writepage
  292. *
  293. * The reason that we use our own function here is that we need to
  294. * start transactions before we grab page locks. This allows us
  295. * to get the ordering right.
  296. */
  297. static int gfs2_write_cache_jdata(struct address_space *mapping,
  298. struct writeback_control *wbc)
  299. {
  300. struct backing_dev_info *bdi = mapping->backing_dev_info;
  301. int ret = 0;
  302. int done = 0;
  303. struct pagevec pvec;
  304. int nr_pages;
  305. pgoff_t index;
  306. pgoff_t end;
  307. int scanned = 0;
  308. int range_whole = 0;
  309. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  310. wbc->encountered_congestion = 1;
  311. return 0;
  312. }
  313. pagevec_init(&pvec, 0);
  314. if (wbc->range_cyclic) {
  315. index = mapping->writeback_index; /* Start from prev offset */
  316. end = -1;
  317. } else {
  318. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  319. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  320. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  321. range_whole = 1;
  322. scanned = 1;
  323. }
  324. retry:
  325. while (!done && (index <= end) &&
  326. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  327. PAGECACHE_TAG_DIRTY,
  328. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  329. scanned = 1;
  330. ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end);
  331. if (ret)
  332. done = 1;
  333. if (ret > 0)
  334. ret = 0;
  335. pagevec_release(&pvec);
  336. cond_resched();
  337. }
  338. if (!scanned && !done) {
  339. /*
  340. * We hit the last page and there is more work to be done: wrap
  341. * back to the start of the file
  342. */
  343. scanned = 1;
  344. index = 0;
  345. goto retry;
  346. }
  347. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  348. mapping->writeback_index = index;
  349. return ret;
  350. }
  351. /**
  352. * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
  353. * @mapping: The mapping to write
  354. * @wbc: The writeback control
  355. *
  356. */
  357. static int gfs2_jdata_writepages(struct address_space *mapping,
  358. struct writeback_control *wbc)
  359. {
  360. struct gfs2_inode *ip = GFS2_I(mapping->host);
  361. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  362. int ret;
  363. ret = gfs2_write_cache_jdata(mapping, wbc);
  364. if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
  365. gfs2_log_flush(sdp, ip->i_gl);
  366. ret = gfs2_write_cache_jdata(mapping, wbc);
  367. }
  368. return ret;
  369. }
  370. /**
  371. * stuffed_readpage - Fill in a Linux page with stuffed file data
  372. * @ip: the inode
  373. * @page: the page
  374. *
  375. * Returns: errno
  376. */
  377. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  378. {
  379. struct buffer_head *dibh;
  380. void *kaddr;
  381. int error;
  382. /*
  383. * Due to the order of unstuffing files and ->fault(), we can be
  384. * asked for a zero page in the case of a stuffed file being extended,
  385. * so we need to supply one here. It doesn't happen often.
  386. */
  387. if (unlikely(page->index)) {
  388. zero_user(page, 0, PAGE_CACHE_SIZE);
  389. SetPageUptodate(page);
  390. return 0;
  391. }
  392. error = gfs2_meta_inode_buffer(ip, &dibh);
  393. if (error)
  394. return error;
  395. kaddr = kmap_atomic(page, KM_USER0);
  396. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  397. ip->i_disksize);
  398. memset(kaddr + ip->i_disksize, 0, PAGE_CACHE_SIZE - ip->i_disksize);
  399. kunmap_atomic(kaddr, KM_USER0);
  400. flush_dcache_page(page);
  401. brelse(dibh);
  402. SetPageUptodate(page);
  403. return 0;
  404. }
  405. /**
  406. * __gfs2_readpage - readpage
  407. * @file: The file to read a page for
  408. * @page: The page to read
  409. *
  410. * This is the core of gfs2's readpage. Its used by the internal file
  411. * reading code as in that case we already hold the glock. Also its
  412. * called by gfs2_readpage() once the required lock has been granted.
  413. *
  414. */
  415. static int __gfs2_readpage(void *file, struct page *page)
  416. {
  417. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  418. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  419. int error;
  420. if (gfs2_is_stuffed(ip)) {
  421. error = stuffed_readpage(ip, page);
  422. unlock_page(page);
  423. } else {
  424. error = mpage_readpage(page, gfs2_block_map);
  425. }
  426. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  427. return -EIO;
  428. return error;
  429. }
  430. /**
  431. * gfs2_readpage - read a page of a file
  432. * @file: The file to read
  433. * @page: The page of the file
  434. *
  435. * This deals with the locking required. We have to unlock and
  436. * relock the page in order to get the locking in the right
  437. * order.
  438. */
  439. static int gfs2_readpage(struct file *file, struct page *page)
  440. {
  441. struct address_space *mapping = page->mapping;
  442. struct gfs2_inode *ip = GFS2_I(mapping->host);
  443. struct gfs2_holder gh;
  444. int error;
  445. unlock_page(page);
  446. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  447. error = gfs2_glock_nq(&gh);
  448. if (unlikely(error))
  449. goto out;
  450. error = AOP_TRUNCATED_PAGE;
  451. lock_page(page);
  452. if (page->mapping == mapping && !PageUptodate(page))
  453. error = __gfs2_readpage(file, page);
  454. else
  455. unlock_page(page);
  456. gfs2_glock_dq(&gh);
  457. out:
  458. gfs2_holder_uninit(&gh);
  459. if (error && error != AOP_TRUNCATED_PAGE)
  460. lock_page(page);
  461. return error;
  462. }
  463. /**
  464. * gfs2_internal_read - read an internal file
  465. * @ip: The gfs2 inode
  466. * @ra_state: The readahead state (or NULL for no readahead)
  467. * @buf: The buffer to fill
  468. * @pos: The file position
  469. * @size: The amount to read
  470. *
  471. */
  472. int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  473. char *buf, loff_t *pos, unsigned size)
  474. {
  475. struct address_space *mapping = ip->i_inode.i_mapping;
  476. unsigned long index = *pos / PAGE_CACHE_SIZE;
  477. unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
  478. unsigned copied = 0;
  479. unsigned amt;
  480. struct page *page;
  481. void *p;
  482. do {
  483. amt = size - copied;
  484. if (offset + size > PAGE_CACHE_SIZE)
  485. amt = PAGE_CACHE_SIZE - offset;
  486. page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
  487. if (IS_ERR(page))
  488. return PTR_ERR(page);
  489. p = kmap_atomic(page, KM_USER0);
  490. memcpy(buf + copied, p + offset, amt);
  491. kunmap_atomic(p, KM_USER0);
  492. mark_page_accessed(page);
  493. page_cache_release(page);
  494. copied += amt;
  495. index++;
  496. offset = 0;
  497. } while(copied < size);
  498. (*pos) += size;
  499. return size;
  500. }
  501. /**
  502. * gfs2_readpages - Read a bunch of pages at once
  503. *
  504. * Some notes:
  505. * 1. This is only for readahead, so we can simply ignore any things
  506. * which are slightly inconvenient (such as locking conflicts between
  507. * the page lock and the glock) and return having done no I/O. Its
  508. * obviously not something we'd want to do on too regular a basis.
  509. * Any I/O we ignore at this time will be done via readpage later.
  510. * 2. We don't handle stuffed files here we let readpage do the honours.
  511. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  512. * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
  513. */
  514. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  515. struct list_head *pages, unsigned nr_pages)
  516. {
  517. struct inode *inode = mapping->host;
  518. struct gfs2_inode *ip = GFS2_I(inode);
  519. struct gfs2_sbd *sdp = GFS2_SB(inode);
  520. struct gfs2_holder gh;
  521. int ret;
  522. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  523. ret = gfs2_glock_nq(&gh);
  524. if (unlikely(ret))
  525. goto out_uninit;
  526. if (!gfs2_is_stuffed(ip))
  527. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
  528. gfs2_glock_dq(&gh);
  529. out_uninit:
  530. gfs2_holder_uninit(&gh);
  531. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  532. ret = -EIO;
  533. return ret;
  534. }
  535. /**
  536. * gfs2_write_begin - Begin to write to a file
  537. * @file: The file to write to
  538. * @mapping: The mapping in which to write
  539. * @pos: The file offset at which to start writing
  540. * @len: Length of the write
  541. * @flags: Various flags
  542. * @pagep: Pointer to return the page
  543. * @fsdata: Pointer to return fs data (unused by GFS2)
  544. *
  545. * Returns: errno
  546. */
  547. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  548. loff_t pos, unsigned len, unsigned flags,
  549. struct page **pagep, void **fsdata)
  550. {
  551. struct gfs2_inode *ip = GFS2_I(mapping->host);
  552. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  553. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  554. int alloc_required;
  555. int error = 0;
  556. struct gfs2_alloc *al;
  557. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  558. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  559. unsigned to = from + len;
  560. struct page *page;
  561. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  562. error = gfs2_glock_nq(&ip->i_gh);
  563. if (unlikely(error))
  564. goto out_uninit;
  565. error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
  566. if (error)
  567. goto out_unlock;
  568. if (alloc_required || gfs2_is_jdata(ip))
  569. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  570. if (alloc_required) {
  571. al = gfs2_alloc_get(ip);
  572. if (!al) {
  573. error = -ENOMEM;
  574. goto out_unlock;
  575. }
  576. error = gfs2_quota_lock_check(ip);
  577. if (error)
  578. goto out_alloc_put;
  579. al->al_requested = data_blocks + ind_blocks;
  580. error = gfs2_inplace_reserve(ip);
  581. if (error)
  582. goto out_qunlock;
  583. }
  584. rblocks = RES_DINODE + ind_blocks;
  585. if (gfs2_is_jdata(ip))
  586. rblocks += data_blocks ? data_blocks : 1;
  587. if (ind_blocks || data_blocks)
  588. rblocks += RES_STATFS + RES_QUOTA;
  589. error = gfs2_trans_begin(sdp, rblocks,
  590. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  591. if (error)
  592. goto out_trans_fail;
  593. error = -ENOMEM;
  594. flags |= AOP_FLAG_NOFS;
  595. page = grab_cache_page_write_begin(mapping, index, flags);
  596. *pagep = page;
  597. if (unlikely(!page))
  598. goto out_endtrans;
  599. if (gfs2_is_stuffed(ip)) {
  600. error = 0;
  601. if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  602. error = gfs2_unstuff_dinode(ip, page);
  603. if (error == 0)
  604. goto prepare_write;
  605. } else if (!PageUptodate(page)) {
  606. error = stuffed_readpage(ip, page);
  607. }
  608. goto out;
  609. }
  610. prepare_write:
  611. error = block_prepare_write(page, from, to, gfs2_block_map);
  612. out:
  613. if (error == 0)
  614. return 0;
  615. page_cache_release(page);
  616. if (pos + len > ip->i_inode.i_size)
  617. vmtruncate(&ip->i_inode, ip->i_inode.i_size);
  618. out_endtrans:
  619. gfs2_trans_end(sdp);
  620. out_trans_fail:
  621. if (alloc_required) {
  622. gfs2_inplace_release(ip);
  623. out_qunlock:
  624. gfs2_quota_unlock(ip);
  625. out_alloc_put:
  626. gfs2_alloc_put(ip);
  627. }
  628. out_unlock:
  629. gfs2_glock_dq(&ip->i_gh);
  630. out_uninit:
  631. gfs2_holder_uninit(&ip->i_gh);
  632. return error;
  633. }
  634. /**
  635. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  636. * @inode: the rindex inode
  637. */
  638. static void adjust_fs_space(struct inode *inode)
  639. {
  640. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  641. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  642. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  643. u64 fs_total, new_free;
  644. /* Total up the file system space, according to the latest rindex. */
  645. fs_total = gfs2_ri_total(sdp);
  646. spin_lock(&sdp->sd_statfs_spin);
  647. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  648. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  649. else
  650. new_free = 0;
  651. spin_unlock(&sdp->sd_statfs_spin);
  652. fs_warn(sdp, "File system extended by %llu blocks.\n",
  653. (unsigned long long)new_free);
  654. gfs2_statfs_change(sdp, new_free, new_free, 0);
  655. }
  656. /**
  657. * gfs2_stuffed_write_end - Write end for stuffed files
  658. * @inode: The inode
  659. * @dibh: The buffer_head containing the on-disk inode
  660. * @pos: The file position
  661. * @len: The length of the write
  662. * @copied: How much was actually copied by the VFS
  663. * @page: The page
  664. *
  665. * This copies the data from the page into the inode block after
  666. * the inode data structure itself.
  667. *
  668. * Returns: errno
  669. */
  670. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  671. loff_t pos, unsigned len, unsigned copied,
  672. struct page *page)
  673. {
  674. struct gfs2_inode *ip = GFS2_I(inode);
  675. struct gfs2_sbd *sdp = GFS2_SB(inode);
  676. u64 to = pos + copied;
  677. void *kaddr;
  678. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  679. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  680. BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
  681. kaddr = kmap_atomic(page, KM_USER0);
  682. memcpy(buf + pos, kaddr + pos, copied);
  683. memset(kaddr + pos + copied, 0, len - copied);
  684. flush_dcache_page(page);
  685. kunmap_atomic(kaddr, KM_USER0);
  686. if (!PageUptodate(page))
  687. SetPageUptodate(page);
  688. unlock_page(page);
  689. page_cache_release(page);
  690. if (inode->i_size < to) {
  691. i_size_write(inode, to);
  692. ip->i_disksize = inode->i_size;
  693. di->di_size = cpu_to_be64(inode->i_size);
  694. mark_inode_dirty(inode);
  695. }
  696. if (inode == sdp->sd_rindex)
  697. adjust_fs_space(inode);
  698. brelse(dibh);
  699. gfs2_trans_end(sdp);
  700. gfs2_glock_dq(&ip->i_gh);
  701. gfs2_holder_uninit(&ip->i_gh);
  702. return copied;
  703. }
  704. /**
  705. * gfs2_write_end
  706. * @file: The file to write to
  707. * @mapping: The address space to write to
  708. * @pos: The file position
  709. * @len: The length of the data
  710. * @copied:
  711. * @page: The page that has been written
  712. * @fsdata: The fsdata (unused in GFS2)
  713. *
  714. * The main write_end function for GFS2. We have a separate one for
  715. * stuffed files as they are slightly different, otherwise we just
  716. * put our locking around the VFS provided functions.
  717. *
  718. * Returns: errno
  719. */
  720. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  721. loff_t pos, unsigned len, unsigned copied,
  722. struct page *page, void *fsdata)
  723. {
  724. struct inode *inode = page->mapping->host;
  725. struct gfs2_inode *ip = GFS2_I(inode);
  726. struct gfs2_sbd *sdp = GFS2_SB(inode);
  727. struct buffer_head *dibh;
  728. struct gfs2_alloc *al = ip->i_alloc;
  729. struct gfs2_dinode *di;
  730. unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
  731. unsigned int to = from + len;
  732. int ret;
  733. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
  734. ret = gfs2_meta_inode_buffer(ip, &dibh);
  735. if (unlikely(ret)) {
  736. unlock_page(page);
  737. page_cache_release(page);
  738. goto failed;
  739. }
  740. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  741. if (gfs2_is_stuffed(ip))
  742. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  743. if (!gfs2_is_writeback(ip))
  744. gfs2_page_add_databufs(ip, page, from, to);
  745. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  746. if (likely(ret >= 0) && (inode->i_size > ip->i_disksize)) {
  747. di = (struct gfs2_dinode *)dibh->b_data;
  748. ip->i_disksize = inode->i_size;
  749. di->di_size = cpu_to_be64(inode->i_size);
  750. mark_inode_dirty(inode);
  751. }
  752. if (inode == sdp->sd_rindex)
  753. adjust_fs_space(inode);
  754. brelse(dibh);
  755. gfs2_trans_end(sdp);
  756. failed:
  757. if (al) {
  758. gfs2_inplace_release(ip);
  759. gfs2_quota_unlock(ip);
  760. gfs2_alloc_put(ip);
  761. }
  762. gfs2_glock_dq(&ip->i_gh);
  763. gfs2_holder_uninit(&ip->i_gh);
  764. return ret;
  765. }
  766. /**
  767. * gfs2_set_page_dirty - Page dirtying function
  768. * @page: The page to dirty
  769. *
  770. * Returns: 1 if it dirtyed the page, or 0 otherwise
  771. */
  772. static int gfs2_set_page_dirty(struct page *page)
  773. {
  774. SetPageChecked(page);
  775. return __set_page_dirty_buffers(page);
  776. }
  777. /**
  778. * gfs2_bmap - Block map function
  779. * @mapping: Address space info
  780. * @lblock: The block to map
  781. *
  782. * Returns: The disk address for the block or 0 on hole or error
  783. */
  784. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  785. {
  786. struct gfs2_inode *ip = GFS2_I(mapping->host);
  787. struct gfs2_holder i_gh;
  788. sector_t dblock = 0;
  789. int error;
  790. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  791. if (error)
  792. return 0;
  793. if (!gfs2_is_stuffed(ip))
  794. dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
  795. gfs2_glock_dq_uninit(&i_gh);
  796. return dblock;
  797. }
  798. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  799. {
  800. struct gfs2_bufdata *bd;
  801. lock_buffer(bh);
  802. gfs2_log_lock(sdp);
  803. clear_buffer_dirty(bh);
  804. bd = bh->b_private;
  805. if (bd) {
  806. if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
  807. list_del_init(&bd->bd_le.le_list);
  808. else
  809. gfs2_remove_from_journal(bh, current->journal_info, 0);
  810. }
  811. bh->b_bdev = NULL;
  812. clear_buffer_mapped(bh);
  813. clear_buffer_req(bh);
  814. clear_buffer_new(bh);
  815. gfs2_log_unlock(sdp);
  816. unlock_buffer(bh);
  817. }
  818. static void gfs2_invalidatepage(struct page *page, unsigned long offset)
  819. {
  820. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  821. struct buffer_head *bh, *head;
  822. unsigned long pos = 0;
  823. BUG_ON(!PageLocked(page));
  824. if (offset == 0)
  825. ClearPageChecked(page);
  826. if (!page_has_buffers(page))
  827. goto out;
  828. bh = head = page_buffers(page);
  829. do {
  830. if (offset <= pos)
  831. gfs2_discard(sdp, bh);
  832. pos += bh->b_size;
  833. bh = bh->b_this_page;
  834. } while (bh != head);
  835. out:
  836. if (offset == 0)
  837. try_to_release_page(page, 0);
  838. }
  839. /**
  840. * gfs2_ok_for_dio - check that dio is valid on this file
  841. * @ip: The inode
  842. * @rw: READ or WRITE
  843. * @offset: The offset at which we are reading or writing
  844. *
  845. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  846. * 1 (to accept the i/o request)
  847. */
  848. static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
  849. {
  850. /*
  851. * Should we return an error here? I can't see that O_DIRECT for
  852. * a stuffed file makes any sense. For now we'll silently fall
  853. * back to buffered I/O
  854. */
  855. if (gfs2_is_stuffed(ip))
  856. return 0;
  857. if (offset >= i_size_read(&ip->i_inode))
  858. return 0;
  859. return 1;
  860. }
  861. static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
  862. const struct iovec *iov, loff_t offset,
  863. unsigned long nr_segs)
  864. {
  865. struct file *file = iocb->ki_filp;
  866. struct inode *inode = file->f_mapping->host;
  867. struct gfs2_inode *ip = GFS2_I(inode);
  868. struct gfs2_holder gh;
  869. int rv;
  870. /*
  871. * Deferred lock, even if its a write, since we do no allocation
  872. * on this path. All we need change is atime, and this lock mode
  873. * ensures that other nodes have flushed their buffered read caches
  874. * (i.e. their page cache entries for this inode). We do not,
  875. * unfortunately have the option of only flushing a range like
  876. * the VFS does.
  877. */
  878. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
  879. rv = gfs2_glock_nq(&gh);
  880. if (rv)
  881. return rv;
  882. rv = gfs2_ok_for_dio(ip, rw, offset);
  883. if (rv != 1)
  884. goto out; /* dio not valid, fall back to buffered i/o */
  885. rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
  886. iov, offset, nr_segs,
  887. gfs2_get_block_direct, NULL);
  888. out:
  889. gfs2_glock_dq_m(1, &gh);
  890. gfs2_holder_uninit(&gh);
  891. return rv;
  892. }
  893. /**
  894. * gfs2_releasepage - free the metadata associated with a page
  895. * @page: the page that's being released
  896. * @gfp_mask: passed from Linux VFS, ignored by us
  897. *
  898. * Call try_to_free_buffers() if the buffers in this page can be
  899. * released.
  900. *
  901. * Returns: 0
  902. */
  903. int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
  904. {
  905. struct inode *aspace = page->mapping->host;
  906. struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
  907. struct buffer_head *bh, *head;
  908. struct gfs2_bufdata *bd;
  909. if (!page_has_buffers(page))
  910. return 0;
  911. gfs2_log_lock(sdp);
  912. head = bh = page_buffers(page);
  913. do {
  914. if (atomic_read(&bh->b_count))
  915. goto cannot_release;
  916. bd = bh->b_private;
  917. if (bd && bd->bd_ail)
  918. goto cannot_release;
  919. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  920. gfs2_assert_warn(sdp, !buffer_dirty(bh));
  921. bh = bh->b_this_page;
  922. } while(bh != head);
  923. gfs2_log_unlock(sdp);
  924. head = bh = page_buffers(page);
  925. do {
  926. gfs2_log_lock(sdp);
  927. bd = bh->b_private;
  928. if (bd) {
  929. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  930. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  931. if (!list_empty(&bd->bd_le.le_list)) {
  932. if (!buffer_pinned(bh))
  933. list_del_init(&bd->bd_le.le_list);
  934. else
  935. bd = NULL;
  936. }
  937. if (bd)
  938. bd->bd_bh = NULL;
  939. bh->b_private = NULL;
  940. }
  941. gfs2_log_unlock(sdp);
  942. if (bd)
  943. kmem_cache_free(gfs2_bufdata_cachep, bd);
  944. bh = bh->b_this_page;
  945. } while (bh != head);
  946. return try_to_free_buffers(page);
  947. cannot_release:
  948. gfs2_log_unlock(sdp);
  949. return 0;
  950. }
  951. static const struct address_space_operations gfs2_writeback_aops = {
  952. .writepage = gfs2_writeback_writepage,
  953. .writepages = gfs2_writeback_writepages,
  954. .readpage = gfs2_readpage,
  955. .readpages = gfs2_readpages,
  956. .sync_page = block_sync_page,
  957. .write_begin = gfs2_write_begin,
  958. .write_end = gfs2_write_end,
  959. .bmap = gfs2_bmap,
  960. .invalidatepage = gfs2_invalidatepage,
  961. .releasepage = gfs2_releasepage,
  962. .direct_IO = gfs2_direct_IO,
  963. .migratepage = buffer_migrate_page,
  964. };
  965. static const struct address_space_operations gfs2_ordered_aops = {
  966. .writepage = gfs2_ordered_writepage,
  967. .readpage = gfs2_readpage,
  968. .readpages = gfs2_readpages,
  969. .sync_page = block_sync_page,
  970. .write_begin = gfs2_write_begin,
  971. .write_end = gfs2_write_end,
  972. .set_page_dirty = gfs2_set_page_dirty,
  973. .bmap = gfs2_bmap,
  974. .invalidatepage = gfs2_invalidatepage,
  975. .releasepage = gfs2_releasepage,
  976. .direct_IO = gfs2_direct_IO,
  977. .migratepage = buffer_migrate_page,
  978. };
  979. static const struct address_space_operations gfs2_jdata_aops = {
  980. .writepage = gfs2_jdata_writepage,
  981. .writepages = gfs2_jdata_writepages,
  982. .readpage = gfs2_readpage,
  983. .readpages = gfs2_readpages,
  984. .sync_page = block_sync_page,
  985. .write_begin = gfs2_write_begin,
  986. .write_end = gfs2_write_end,
  987. .set_page_dirty = gfs2_set_page_dirty,
  988. .bmap = gfs2_bmap,
  989. .invalidatepage = gfs2_invalidatepage,
  990. .releasepage = gfs2_releasepage,
  991. };
  992. void gfs2_set_aops(struct inode *inode)
  993. {
  994. struct gfs2_inode *ip = GFS2_I(inode);
  995. if (gfs2_is_writeback(ip))
  996. inode->i_mapping->a_ops = &gfs2_writeback_aops;
  997. else if (gfs2_is_ordered(ip))
  998. inode->i_mapping->a_ops = &gfs2_ordered_aops;
  999. else if (gfs2_is_jdata(ip))
  1000. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  1001. else
  1002. BUG();
  1003. }