aops.c 30 KB

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