aops.c 31 KB

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