aops.c 29 KB

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