aops.c 29 KB

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