aops.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  552. int alloc_required;
  553. int error = 0;
  554. struct gfs2_alloc *al;
  555. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  556. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  557. unsigned to = from + len;
  558. struct page *page;
  559. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  560. error = gfs2_glock_nq(&ip->i_gh);
  561. if (unlikely(error))
  562. goto out_uninit;
  563. error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
  564. if (error)
  565. goto out_unlock;
  566. if (alloc_required || gfs2_is_jdata(ip))
  567. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  568. if (alloc_required) {
  569. al = gfs2_alloc_get(ip);
  570. if (!al) {
  571. error = -ENOMEM;
  572. goto out_unlock;
  573. }
  574. error = gfs2_quota_lock_check(ip);
  575. if (error)
  576. goto out_alloc_put;
  577. al->al_requested = data_blocks + ind_blocks;
  578. error = gfs2_inplace_reserve(ip);
  579. if (error)
  580. goto out_qunlock;
  581. }
  582. rblocks = RES_DINODE + ind_blocks;
  583. if (gfs2_is_jdata(ip))
  584. rblocks += data_blocks ? data_blocks : 1;
  585. if (ind_blocks || data_blocks)
  586. rblocks += RES_STATFS + RES_QUOTA;
  587. error = gfs2_trans_begin(sdp, rblocks,
  588. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  589. if (error)
  590. goto out_trans_fail;
  591. error = -ENOMEM;
  592. flags |= AOP_FLAG_NOFS;
  593. page = grab_cache_page_write_begin(mapping, index, flags);
  594. *pagep = page;
  595. if (unlikely(!page))
  596. goto out_endtrans;
  597. if (gfs2_is_stuffed(ip)) {
  598. error = 0;
  599. if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  600. error = gfs2_unstuff_dinode(ip, page);
  601. if (error == 0)
  602. goto prepare_write;
  603. } else if (!PageUptodate(page)) {
  604. error = stuffed_readpage(ip, page);
  605. }
  606. goto out;
  607. }
  608. prepare_write:
  609. error = block_prepare_write(page, from, to, gfs2_block_map);
  610. out:
  611. if (error == 0)
  612. return 0;
  613. page_cache_release(page);
  614. if (pos + len > ip->i_inode.i_size)
  615. vmtruncate(&ip->i_inode, ip->i_inode.i_size);
  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_alloc_put(ip);
  625. }
  626. out_unlock:
  627. gfs2_glock_dq(&ip->i_gh);
  628. out_uninit:
  629. gfs2_holder_uninit(&ip->i_gh);
  630. return error;
  631. }
  632. /**
  633. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  634. * @inode: the rindex inode
  635. */
  636. static void adjust_fs_space(struct inode *inode)
  637. {
  638. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  639. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  640. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  641. u64 fs_total, new_free;
  642. /* Total up the file system space, according to the latest rindex. */
  643. fs_total = gfs2_ri_total(sdp);
  644. spin_lock(&sdp->sd_statfs_spin);
  645. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  646. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  647. else
  648. new_free = 0;
  649. spin_unlock(&sdp->sd_statfs_spin);
  650. fs_warn(sdp, "File system extended by %llu blocks.\n",
  651. (unsigned long long)new_free);
  652. gfs2_statfs_change(sdp, new_free, new_free, 0);
  653. }
  654. /**
  655. * gfs2_stuffed_write_end - Write end for stuffed files
  656. * @inode: The inode
  657. * @dibh: The buffer_head containing the on-disk inode
  658. * @pos: The file position
  659. * @len: The length of the write
  660. * @copied: How much was actually copied by the VFS
  661. * @page: The page
  662. *
  663. * This copies the data from the page into the inode block after
  664. * the inode data structure itself.
  665. *
  666. * Returns: errno
  667. */
  668. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  669. loff_t pos, unsigned len, unsigned copied,
  670. struct page *page)
  671. {
  672. struct gfs2_inode *ip = GFS2_I(inode);
  673. struct gfs2_sbd *sdp = GFS2_SB(inode);
  674. u64 to = pos + copied;
  675. void *kaddr;
  676. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  677. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  678. BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
  679. kaddr = kmap_atomic(page, KM_USER0);
  680. memcpy(buf + pos, kaddr + pos, copied);
  681. memset(kaddr + pos + copied, 0, len - copied);
  682. flush_dcache_page(page);
  683. kunmap_atomic(kaddr, KM_USER0);
  684. if (!PageUptodate(page))
  685. SetPageUptodate(page);
  686. unlock_page(page);
  687. page_cache_release(page);
  688. if (copied) {
  689. if (inode->i_size < to) {
  690. i_size_write(inode, to);
  691. ip->i_disksize = inode->i_size;
  692. }
  693. gfs2_dinode_out(ip, di);
  694. mark_inode_dirty(inode);
  695. }
  696. if (inode == sdp->sd_rindex)
  697. adjust_fs_space(inode);
  698. brelse(dibh);
  699. gfs2_trans_end(sdp);
  700. gfs2_glock_dq(&ip->i_gh);
  701. gfs2_holder_uninit(&ip->i_gh);
  702. return copied;
  703. }
  704. /**
  705. * gfs2_write_end
  706. * @file: The file to write to
  707. * @mapping: The address space to write to
  708. * @pos: The file position
  709. * @len: The length of the data
  710. * @copied:
  711. * @page: The page that has been written
  712. * @fsdata: The fsdata (unused in GFS2)
  713. *
  714. * The main write_end function for GFS2. We have a separate one for
  715. * stuffed files as they are slightly different, otherwise we just
  716. * put our locking around the VFS provided functions.
  717. *
  718. * Returns: errno
  719. */
  720. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  721. loff_t pos, unsigned len, unsigned copied,
  722. struct page *page, void *fsdata)
  723. {
  724. struct inode *inode = page->mapping->host;
  725. struct gfs2_inode *ip = GFS2_I(inode);
  726. struct gfs2_sbd *sdp = GFS2_SB(inode);
  727. struct buffer_head *dibh;
  728. struct gfs2_alloc *al = ip->i_alloc;
  729. unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
  730. unsigned int to = from + len;
  731. int ret;
  732. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
  733. ret = gfs2_meta_inode_buffer(ip, &dibh);
  734. if (unlikely(ret)) {
  735. unlock_page(page);
  736. page_cache_release(page);
  737. goto failed;
  738. }
  739. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  740. if (gfs2_is_stuffed(ip))
  741. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  742. if (!gfs2_is_writeback(ip))
  743. gfs2_page_add_databufs(ip, page, from, to);
  744. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  745. if (ret > 0) {
  746. if (inode->i_size > ip->i_disksize)
  747. ip->i_disksize = inode->i_size;
  748. gfs2_dinode_out(ip, dibh->b_data);
  749. mark_inode_dirty(inode);
  750. }
  751. if (inode == sdp->sd_rindex)
  752. adjust_fs_space(inode);
  753. brelse(dibh);
  754. gfs2_trans_end(sdp);
  755. failed:
  756. if (al) {
  757. gfs2_inplace_release(ip);
  758. gfs2_quota_unlock(ip);
  759. gfs2_alloc_put(ip);
  760. }
  761. gfs2_glock_dq(&ip->i_gh);
  762. gfs2_holder_uninit(&ip->i_gh);
  763. return ret;
  764. }
  765. /**
  766. * gfs2_set_page_dirty - Page dirtying function
  767. * @page: The page to dirty
  768. *
  769. * Returns: 1 if it dirtyed the page, or 0 otherwise
  770. */
  771. static int gfs2_set_page_dirty(struct page *page)
  772. {
  773. SetPageChecked(page);
  774. return __set_page_dirty_buffers(page);
  775. }
  776. /**
  777. * gfs2_bmap - Block map function
  778. * @mapping: Address space info
  779. * @lblock: The block to map
  780. *
  781. * Returns: The disk address for the block or 0 on hole or error
  782. */
  783. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  784. {
  785. struct gfs2_inode *ip = GFS2_I(mapping->host);
  786. struct gfs2_holder i_gh;
  787. sector_t dblock = 0;
  788. int error;
  789. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  790. if (error)
  791. return 0;
  792. if (!gfs2_is_stuffed(ip))
  793. dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
  794. gfs2_glock_dq_uninit(&i_gh);
  795. return dblock;
  796. }
  797. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  798. {
  799. struct gfs2_bufdata *bd;
  800. lock_buffer(bh);
  801. gfs2_log_lock(sdp);
  802. clear_buffer_dirty(bh);
  803. bd = bh->b_private;
  804. if (bd) {
  805. if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
  806. list_del_init(&bd->bd_le.le_list);
  807. else
  808. gfs2_remove_from_journal(bh, current->journal_info, 0);
  809. }
  810. bh->b_bdev = NULL;
  811. clear_buffer_mapped(bh);
  812. clear_buffer_req(bh);
  813. clear_buffer_new(bh);
  814. gfs2_log_unlock(sdp);
  815. unlock_buffer(bh);
  816. }
  817. static void gfs2_invalidatepage(struct page *page, unsigned long offset)
  818. {
  819. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  820. struct buffer_head *bh, *head;
  821. unsigned long pos = 0;
  822. BUG_ON(!PageLocked(page));
  823. if (offset == 0)
  824. ClearPageChecked(page);
  825. if (!page_has_buffers(page))
  826. goto out;
  827. bh = head = page_buffers(page);
  828. do {
  829. if (offset <= pos)
  830. gfs2_discard(sdp, bh);
  831. pos += bh->b_size;
  832. bh = bh->b_this_page;
  833. } while (bh != head);
  834. out:
  835. if (offset == 0)
  836. try_to_release_page(page, 0);
  837. }
  838. /**
  839. * gfs2_ok_for_dio - check that dio is valid on this file
  840. * @ip: The inode
  841. * @rw: READ or WRITE
  842. * @offset: The offset at which we are reading or writing
  843. *
  844. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  845. * 1 (to accept the i/o request)
  846. */
  847. static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
  848. {
  849. /*
  850. * Should we return an error here? I can't see that O_DIRECT for
  851. * a stuffed file makes any sense. For now we'll silently fall
  852. * back to buffered I/O
  853. */
  854. if (gfs2_is_stuffed(ip))
  855. return 0;
  856. if (offset >= i_size_read(&ip->i_inode))
  857. return 0;
  858. return 1;
  859. }
  860. static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
  861. const struct iovec *iov, loff_t offset,
  862. unsigned long nr_segs)
  863. {
  864. struct file *file = iocb->ki_filp;
  865. struct inode *inode = file->f_mapping->host;
  866. struct gfs2_inode *ip = GFS2_I(inode);
  867. struct gfs2_holder gh;
  868. int rv;
  869. /*
  870. * Deferred lock, even if its a write, since we do no allocation
  871. * on this path. All we need change is atime, and this lock mode
  872. * ensures that other nodes have flushed their buffered read caches
  873. * (i.e. their page cache entries for this inode). We do not,
  874. * unfortunately have the option of only flushing a range like
  875. * the VFS does.
  876. */
  877. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
  878. rv = gfs2_glock_nq(&gh);
  879. if (rv)
  880. return rv;
  881. rv = gfs2_ok_for_dio(ip, rw, offset);
  882. if (rv != 1)
  883. goto out; /* dio not valid, fall back to buffered i/o */
  884. rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
  885. iov, offset, nr_segs,
  886. gfs2_get_block_direct, NULL);
  887. out:
  888. gfs2_glock_dq_m(1, &gh);
  889. gfs2_holder_uninit(&gh);
  890. return rv;
  891. }
  892. /**
  893. * gfs2_releasepage - free the metadata associated with a page
  894. * @page: the page that's being released
  895. * @gfp_mask: passed from Linux VFS, ignored by us
  896. *
  897. * Call try_to_free_buffers() if the buffers in this page can be
  898. * released.
  899. *
  900. * Returns: 0
  901. */
  902. int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
  903. {
  904. struct inode *aspace = page->mapping->host;
  905. struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
  906. struct buffer_head *bh, *head;
  907. struct gfs2_bufdata *bd;
  908. if (!page_has_buffers(page))
  909. return 0;
  910. gfs2_log_lock(sdp);
  911. head = bh = page_buffers(page);
  912. do {
  913. if (atomic_read(&bh->b_count))
  914. goto cannot_release;
  915. bd = bh->b_private;
  916. if (bd && bd->bd_ail)
  917. goto cannot_release;
  918. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  919. gfs2_assert_warn(sdp, !buffer_dirty(bh));
  920. bh = bh->b_this_page;
  921. } while(bh != head);
  922. gfs2_log_unlock(sdp);
  923. head = bh = page_buffers(page);
  924. do {
  925. gfs2_log_lock(sdp);
  926. bd = bh->b_private;
  927. if (bd) {
  928. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  929. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  930. if (!list_empty(&bd->bd_le.le_list)) {
  931. if (!buffer_pinned(bh))
  932. list_del_init(&bd->bd_le.le_list);
  933. else
  934. bd = NULL;
  935. }
  936. if (bd)
  937. bd->bd_bh = NULL;
  938. bh->b_private = NULL;
  939. }
  940. gfs2_log_unlock(sdp);
  941. if (bd)
  942. kmem_cache_free(gfs2_bufdata_cachep, bd);
  943. bh = bh->b_this_page;
  944. } while (bh != head);
  945. return try_to_free_buffers(page);
  946. cannot_release:
  947. gfs2_log_unlock(sdp);
  948. return 0;
  949. }
  950. static const struct address_space_operations gfs2_writeback_aops = {
  951. .writepage = gfs2_writeback_writepage,
  952. .writepages = gfs2_writeback_writepages,
  953. .readpage = gfs2_readpage,
  954. .readpages = gfs2_readpages,
  955. .sync_page = block_sync_page,
  956. .write_begin = gfs2_write_begin,
  957. .write_end = gfs2_write_end,
  958. .bmap = gfs2_bmap,
  959. .invalidatepage = gfs2_invalidatepage,
  960. .releasepage = gfs2_releasepage,
  961. .direct_IO = gfs2_direct_IO,
  962. .migratepage = buffer_migrate_page,
  963. .is_partially_uptodate = block_is_partially_uptodate,
  964. };
  965. static const struct address_space_operations gfs2_ordered_aops = {
  966. .writepage = gfs2_ordered_writepage,
  967. .readpage = gfs2_readpage,
  968. .readpages = gfs2_readpages,
  969. .sync_page = block_sync_page,
  970. .write_begin = gfs2_write_begin,
  971. .write_end = gfs2_write_end,
  972. .set_page_dirty = gfs2_set_page_dirty,
  973. .bmap = gfs2_bmap,
  974. .invalidatepage = gfs2_invalidatepage,
  975. .releasepage = gfs2_releasepage,
  976. .direct_IO = gfs2_direct_IO,
  977. .migratepage = buffer_migrate_page,
  978. .is_partially_uptodate = block_is_partially_uptodate,
  979. };
  980. static const struct address_space_operations gfs2_jdata_aops = {
  981. .writepage = gfs2_jdata_writepage,
  982. .writepages = gfs2_jdata_writepages,
  983. .readpage = gfs2_readpage,
  984. .readpages = gfs2_readpages,
  985. .sync_page = block_sync_page,
  986. .write_begin = gfs2_write_begin,
  987. .write_end = gfs2_write_end,
  988. .set_page_dirty = gfs2_set_page_dirty,
  989. .bmap = gfs2_bmap,
  990. .invalidatepage = gfs2_invalidatepage,
  991. .releasepage = gfs2_releasepage,
  992. .is_partially_uptodate = block_is_partially_uptodate,
  993. };
  994. void gfs2_set_aops(struct inode *inode)
  995. {
  996. struct gfs2_inode *ip = GFS2_I(inode);
  997. if (gfs2_is_writeback(ip))
  998. inode->i_mapping->a_ops = &gfs2_writeback_aops;
  999. else if (gfs2_is_ordered(ip))
  1000. inode->i_mapping->a_ops = &gfs2_ordered_aops;
  1001. else if (gfs2_is_jdata(ip))
  1002. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  1003. else
  1004. BUG();
  1005. }