aops.c 29 KB

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