aops.c 29 KB

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