log.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2007 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/gfs2_ondisk.h>
  15. #include <linux/crc32.h>
  16. #include <linux/crc32c.h>
  17. #include <linux/delay.h>
  18. #include <linux/kthread.h>
  19. #include <linux/freezer.h>
  20. #include <linux/bio.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/writeback.h>
  23. #include <linux/list_sort.h>
  24. #include "gfs2.h"
  25. #include "incore.h"
  26. #include "bmap.h"
  27. #include "glock.h"
  28. #include "log.h"
  29. #include "lops.h"
  30. #include "meta_io.h"
  31. #include "util.h"
  32. #include "dir.h"
  33. #include "trace_gfs2.h"
  34. /**
  35. * gfs2_struct2blk - compute stuff
  36. * @sdp: the filesystem
  37. * @nstruct: the number of structures
  38. * @ssize: the size of the structures
  39. *
  40. * Compute the number of log descriptor blocks needed to hold a certain number
  41. * of structures of a certain size.
  42. *
  43. * Returns: the number of blocks needed (minimum is always 1)
  44. */
  45. unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
  46. unsigned int ssize)
  47. {
  48. unsigned int blks;
  49. unsigned int first, second;
  50. blks = 1;
  51. first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
  52. if (nstruct > first) {
  53. second = (sdp->sd_sb.sb_bsize -
  54. sizeof(struct gfs2_meta_header)) / ssize;
  55. blks += DIV_ROUND_UP(nstruct - first, second);
  56. }
  57. return blks;
  58. }
  59. /**
  60. * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
  61. * @mapping: The associated mapping (maybe NULL)
  62. * @bd: The gfs2_bufdata to remove
  63. *
  64. * The ail lock _must_ be held when calling this function
  65. *
  66. */
  67. static void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
  68. {
  69. bd->bd_tr = NULL;
  70. list_del_init(&bd->bd_ail_st_list);
  71. list_del_init(&bd->bd_ail_gl_list);
  72. atomic_dec(&bd->bd_gl->gl_ail_count);
  73. brelse(bd->bd_bh);
  74. }
  75. /**
  76. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  77. * @sdp: the filesystem
  78. * @wbc: The writeback control structure
  79. * @ai: The ail structure
  80. *
  81. */
  82. static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
  83. struct writeback_control *wbc,
  84. struct gfs2_trans *tr,
  85. bool *withdraw)
  86. __releases(&sdp->sd_ail_lock)
  87. __acquires(&sdp->sd_ail_lock)
  88. {
  89. struct gfs2_glock *gl = NULL;
  90. struct address_space *mapping;
  91. struct gfs2_bufdata *bd, *s;
  92. struct buffer_head *bh;
  93. list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
  94. bh = bd->bd_bh;
  95. gfs2_assert(sdp, bd->bd_tr == tr);
  96. if (!buffer_busy(bh)) {
  97. if (!buffer_uptodate(bh) &&
  98. !test_and_set_bit(SDF_AIL1_IO_ERROR,
  99. &sdp->sd_flags)) {
  100. gfs2_io_error_bh(sdp, bh);
  101. *withdraw = true;
  102. }
  103. list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
  104. continue;
  105. }
  106. if (!buffer_dirty(bh))
  107. continue;
  108. if (gl == bd->bd_gl)
  109. continue;
  110. gl = bd->bd_gl;
  111. list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
  112. mapping = bh->b_page->mapping;
  113. if (!mapping)
  114. continue;
  115. spin_unlock(&sdp->sd_ail_lock);
  116. generic_writepages(mapping, wbc);
  117. spin_lock(&sdp->sd_ail_lock);
  118. if (wbc->nr_to_write <= 0)
  119. break;
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * gfs2_ail1_flush - start writeback of some ail1 entries
  126. * @sdp: The super block
  127. * @wbc: The writeback control structure
  128. *
  129. * Writes back some ail1 entries, according to the limits in the
  130. * writeback control structure
  131. */
  132. void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
  133. {
  134. struct list_head *head = &sdp->sd_ail1_list;
  135. struct gfs2_trans *tr;
  136. struct blk_plug plug;
  137. bool withdraw = false;
  138. trace_gfs2_ail_flush(sdp, wbc, 1);
  139. blk_start_plug(&plug);
  140. spin_lock(&sdp->sd_ail_lock);
  141. restart:
  142. list_for_each_entry_reverse(tr, head, tr_list) {
  143. if (wbc->nr_to_write <= 0)
  144. break;
  145. if (gfs2_ail1_start_one(sdp, wbc, tr, &withdraw))
  146. goto restart;
  147. }
  148. spin_unlock(&sdp->sd_ail_lock);
  149. blk_finish_plug(&plug);
  150. if (withdraw)
  151. gfs2_lm_withdraw(sdp, NULL);
  152. trace_gfs2_ail_flush(sdp, wbc, 0);
  153. }
  154. /**
  155. * gfs2_ail1_start - start writeback of all ail1 entries
  156. * @sdp: The superblock
  157. */
  158. static void gfs2_ail1_start(struct gfs2_sbd *sdp)
  159. {
  160. struct writeback_control wbc = {
  161. .sync_mode = WB_SYNC_NONE,
  162. .nr_to_write = LONG_MAX,
  163. .range_start = 0,
  164. .range_end = LLONG_MAX,
  165. };
  166. return gfs2_ail1_flush(sdp, &wbc);
  167. }
  168. /**
  169. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  170. * @sdp: the filesystem
  171. * @ai: the AIL entry
  172. *
  173. */
  174. static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
  175. bool *withdraw)
  176. {
  177. struct gfs2_bufdata *bd, *s;
  178. struct buffer_head *bh;
  179. list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
  180. bd_ail_st_list) {
  181. bh = bd->bd_bh;
  182. gfs2_assert(sdp, bd->bd_tr == tr);
  183. if (buffer_busy(bh))
  184. continue;
  185. if (!buffer_uptodate(bh) &&
  186. !test_and_set_bit(SDF_AIL1_IO_ERROR, &sdp->sd_flags)) {
  187. gfs2_io_error_bh(sdp, bh);
  188. *withdraw = true;
  189. }
  190. list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
  191. }
  192. }
  193. /**
  194. * gfs2_ail1_empty - Try to empty the ail1 lists
  195. * @sdp: The superblock
  196. *
  197. * Tries to empty the ail1 lists, starting with the oldest first
  198. */
  199. static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
  200. {
  201. struct gfs2_trans *tr, *s;
  202. int oldest_tr = 1;
  203. int ret;
  204. bool withdraw = false;
  205. spin_lock(&sdp->sd_ail_lock);
  206. list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
  207. gfs2_ail1_empty_one(sdp, tr, &withdraw);
  208. if (list_empty(&tr->tr_ail1_list) && oldest_tr)
  209. list_move(&tr->tr_list, &sdp->sd_ail2_list);
  210. else
  211. oldest_tr = 0;
  212. }
  213. ret = list_empty(&sdp->sd_ail1_list);
  214. spin_unlock(&sdp->sd_ail_lock);
  215. if (withdraw)
  216. gfs2_lm_withdraw(sdp, "fatal: I/O error(s)\n");
  217. return ret;
  218. }
  219. static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
  220. {
  221. struct gfs2_trans *tr;
  222. struct gfs2_bufdata *bd;
  223. struct buffer_head *bh;
  224. spin_lock(&sdp->sd_ail_lock);
  225. list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
  226. list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
  227. bh = bd->bd_bh;
  228. if (!buffer_locked(bh))
  229. continue;
  230. get_bh(bh);
  231. spin_unlock(&sdp->sd_ail_lock);
  232. wait_on_buffer(bh);
  233. brelse(bh);
  234. return;
  235. }
  236. }
  237. spin_unlock(&sdp->sd_ail_lock);
  238. }
  239. /**
  240. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  241. * @sdp: the filesystem
  242. * @ai: the AIL entry
  243. *
  244. */
  245. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  246. {
  247. struct list_head *head = &tr->tr_ail2_list;
  248. struct gfs2_bufdata *bd;
  249. while (!list_empty(head)) {
  250. bd = list_entry(head->prev, struct gfs2_bufdata,
  251. bd_ail_st_list);
  252. gfs2_assert(sdp, bd->bd_tr == tr);
  253. gfs2_remove_from_ail(bd);
  254. }
  255. }
  256. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  257. {
  258. struct gfs2_trans *tr, *safe;
  259. unsigned int old_tail = sdp->sd_log_tail;
  260. int wrap = (new_tail < old_tail);
  261. int a, b, rm;
  262. spin_lock(&sdp->sd_ail_lock);
  263. list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
  264. a = (old_tail <= tr->tr_first);
  265. b = (tr->tr_first < new_tail);
  266. rm = (wrap) ? (a || b) : (a && b);
  267. if (!rm)
  268. continue;
  269. gfs2_ail2_empty_one(sdp, tr);
  270. list_del(&tr->tr_list);
  271. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
  272. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
  273. kfree(tr);
  274. }
  275. spin_unlock(&sdp->sd_ail_lock);
  276. }
  277. /**
  278. * gfs2_log_release - Release a given number of log blocks
  279. * @sdp: The GFS2 superblock
  280. * @blks: The number of blocks
  281. *
  282. */
  283. void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  284. {
  285. atomic_add(blks, &sdp->sd_log_blks_free);
  286. trace_gfs2_log_blocks(sdp, blks);
  287. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  288. sdp->sd_jdesc->jd_blocks);
  289. up_read(&sdp->sd_log_flush_lock);
  290. }
  291. /**
  292. * gfs2_log_reserve - Make a log reservation
  293. * @sdp: The GFS2 superblock
  294. * @blks: The number of blocks to reserve
  295. *
  296. * Note that we never give out the last few blocks of the journal. Thats
  297. * due to the fact that there is a small number of header blocks
  298. * associated with each log flush. The exact number can't be known until
  299. * flush time, so we ensure that we have just enough free blocks at all
  300. * times to avoid running out during a log flush.
  301. *
  302. * We no longer flush the log here, instead we wake up logd to do that
  303. * for us. To avoid the thundering herd and to ensure that we deal fairly
  304. * with queued waiters, we use an exclusive wait. This means that when we
  305. * get woken with enough journal space to get our reservation, we need to
  306. * wake the next waiter on the list.
  307. *
  308. * Returns: errno
  309. */
  310. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  311. {
  312. int ret = 0;
  313. unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
  314. unsigned wanted = blks + reserved_blks;
  315. DEFINE_WAIT(wait);
  316. int did_wait = 0;
  317. unsigned int free_blocks;
  318. if (gfs2_assert_warn(sdp, blks) ||
  319. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  320. return -EINVAL;
  321. atomic_add(blks, &sdp->sd_log_blks_needed);
  322. retry:
  323. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  324. if (unlikely(free_blocks <= wanted)) {
  325. do {
  326. prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
  327. TASK_UNINTERRUPTIBLE);
  328. wake_up(&sdp->sd_logd_waitq);
  329. did_wait = 1;
  330. if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
  331. io_schedule();
  332. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  333. } while(free_blocks <= wanted);
  334. finish_wait(&sdp->sd_log_waitq, &wait);
  335. }
  336. atomic_inc(&sdp->sd_reserving_log);
  337. if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
  338. free_blocks - blks) != free_blocks) {
  339. if (atomic_dec_and_test(&sdp->sd_reserving_log))
  340. wake_up(&sdp->sd_reserving_log_wait);
  341. goto retry;
  342. }
  343. atomic_sub(blks, &sdp->sd_log_blks_needed);
  344. trace_gfs2_log_blocks(sdp, -blks);
  345. /*
  346. * If we waited, then so might others, wake them up _after_ we get
  347. * our share of the log.
  348. */
  349. if (unlikely(did_wait))
  350. wake_up(&sdp->sd_log_waitq);
  351. down_read(&sdp->sd_log_flush_lock);
  352. if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
  353. gfs2_log_release(sdp, blks);
  354. ret = -EROFS;
  355. }
  356. if (atomic_dec_and_test(&sdp->sd_reserving_log))
  357. wake_up(&sdp->sd_reserving_log_wait);
  358. return ret;
  359. }
  360. /**
  361. * log_distance - Compute distance between two journal blocks
  362. * @sdp: The GFS2 superblock
  363. * @newer: The most recent journal block of the pair
  364. * @older: The older journal block of the pair
  365. *
  366. * Compute the distance (in the journal direction) between two
  367. * blocks in the journal
  368. *
  369. * Returns: the distance in blocks
  370. */
  371. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  372. unsigned int older)
  373. {
  374. int dist;
  375. dist = newer - older;
  376. if (dist < 0)
  377. dist += sdp->sd_jdesc->jd_blocks;
  378. return dist;
  379. }
  380. /**
  381. * calc_reserved - Calculate the number of blocks to reserve when
  382. * refunding a transaction's unused buffers.
  383. * @sdp: The GFS2 superblock
  384. *
  385. * This is complex. We need to reserve room for all our currently used
  386. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  387. * all our journaled data buffers for journaled files (e.g. files in the
  388. * meta_fs like rindex, or files for which chattr +j was done.)
  389. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  390. * will count it as free space (sd_log_blks_free) and corruption will follow.
  391. *
  392. * We can have metadata bufs and jdata bufs in the same journal. So each
  393. * type gets its own log header, for which we need to reserve a block.
  394. * In fact, each type has the potential for needing more than one header
  395. * in cases where we have more buffers than will fit on a journal page.
  396. * Metadata journal entries take up half the space of journaled buffer entries.
  397. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  398. * databuf_limit (251) before they cause a wrap around.
  399. *
  400. * Also, we need to reserve blocks for revoke journal entries and one for an
  401. * overall header for the lot.
  402. *
  403. * Returns: the number of blocks reserved
  404. */
  405. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  406. {
  407. unsigned int reserved = 0;
  408. unsigned int mbuf;
  409. unsigned int dbuf;
  410. struct gfs2_trans *tr = sdp->sd_log_tr;
  411. if (tr) {
  412. mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
  413. dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
  414. reserved = mbuf + dbuf;
  415. /* Account for header blocks */
  416. reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp));
  417. reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp));
  418. }
  419. if (sdp->sd_log_commited_revoke > 0)
  420. reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  421. sizeof(u64));
  422. /* One for the overall header */
  423. if (reserved)
  424. reserved++;
  425. return reserved;
  426. }
  427. static unsigned int current_tail(struct gfs2_sbd *sdp)
  428. {
  429. struct gfs2_trans *tr;
  430. unsigned int tail;
  431. spin_lock(&sdp->sd_ail_lock);
  432. if (list_empty(&sdp->sd_ail1_list)) {
  433. tail = sdp->sd_log_head;
  434. } else {
  435. tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
  436. tr_list);
  437. tail = tr->tr_first;
  438. }
  439. spin_unlock(&sdp->sd_ail_lock);
  440. return tail;
  441. }
  442. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  443. {
  444. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  445. ail2_empty(sdp, new_tail);
  446. atomic_add(dist, &sdp->sd_log_blks_free);
  447. trace_gfs2_log_blocks(sdp, dist);
  448. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  449. sdp->sd_jdesc->jd_blocks);
  450. sdp->sd_log_tail = new_tail;
  451. }
  452. static void log_flush_wait(struct gfs2_sbd *sdp)
  453. {
  454. DEFINE_WAIT(wait);
  455. if (atomic_read(&sdp->sd_log_in_flight)) {
  456. do {
  457. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  458. TASK_UNINTERRUPTIBLE);
  459. if (atomic_read(&sdp->sd_log_in_flight))
  460. io_schedule();
  461. } while(atomic_read(&sdp->sd_log_in_flight));
  462. finish_wait(&sdp->sd_log_flush_wait, &wait);
  463. }
  464. }
  465. static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
  466. {
  467. struct gfs2_inode *ipa, *ipb;
  468. ipa = list_entry(a, struct gfs2_inode, i_ordered);
  469. ipb = list_entry(b, struct gfs2_inode, i_ordered);
  470. if (ipa->i_no_addr < ipb->i_no_addr)
  471. return -1;
  472. if (ipa->i_no_addr > ipb->i_no_addr)
  473. return 1;
  474. return 0;
  475. }
  476. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  477. {
  478. struct gfs2_inode *ip;
  479. LIST_HEAD(written);
  480. spin_lock(&sdp->sd_ordered_lock);
  481. list_sort(NULL, &sdp->sd_log_le_ordered, &ip_cmp);
  482. while (!list_empty(&sdp->sd_log_le_ordered)) {
  483. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  484. if (ip->i_inode.i_mapping->nrpages == 0) {
  485. test_and_clear_bit(GIF_ORDERED, &ip->i_flags);
  486. list_del(&ip->i_ordered);
  487. continue;
  488. }
  489. list_move(&ip->i_ordered, &written);
  490. spin_unlock(&sdp->sd_ordered_lock);
  491. filemap_fdatawrite(ip->i_inode.i_mapping);
  492. spin_lock(&sdp->sd_ordered_lock);
  493. }
  494. list_splice(&written, &sdp->sd_log_le_ordered);
  495. spin_unlock(&sdp->sd_ordered_lock);
  496. }
  497. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  498. {
  499. struct gfs2_inode *ip;
  500. spin_lock(&sdp->sd_ordered_lock);
  501. while (!list_empty(&sdp->sd_log_le_ordered)) {
  502. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  503. list_del(&ip->i_ordered);
  504. WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
  505. if (ip->i_inode.i_mapping->nrpages == 0)
  506. continue;
  507. spin_unlock(&sdp->sd_ordered_lock);
  508. filemap_fdatawait(ip->i_inode.i_mapping);
  509. spin_lock(&sdp->sd_ordered_lock);
  510. }
  511. spin_unlock(&sdp->sd_ordered_lock);
  512. }
  513. void gfs2_ordered_del_inode(struct gfs2_inode *ip)
  514. {
  515. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  516. spin_lock(&sdp->sd_ordered_lock);
  517. if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
  518. list_del(&ip->i_ordered);
  519. spin_unlock(&sdp->sd_ordered_lock);
  520. }
  521. void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  522. {
  523. struct buffer_head *bh = bd->bd_bh;
  524. struct gfs2_glock *gl = bd->bd_gl;
  525. bh->b_private = NULL;
  526. bd->bd_blkno = bh->b_blocknr;
  527. gfs2_remove_from_ail(bd); /* drops ref on bh */
  528. bd->bd_bh = NULL;
  529. bd->bd_ops = &gfs2_revoke_lops;
  530. sdp->sd_log_num_revoke++;
  531. if (atomic_inc_return(&gl->gl_revokes) == 1)
  532. gfs2_glock_hold(gl);
  533. set_bit(GLF_LFLUSH, &gl->gl_flags);
  534. list_add(&bd->bd_list, &sdp->sd_log_le_revoke);
  535. }
  536. void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
  537. {
  538. if (atomic_dec_return(&gl->gl_revokes) == 0) {
  539. clear_bit(GLF_LFLUSH, &gl->gl_flags);
  540. gfs2_glock_queue_put(gl);
  541. }
  542. }
  543. void gfs2_write_revokes(struct gfs2_sbd *sdp)
  544. {
  545. struct gfs2_trans *tr;
  546. struct gfs2_bufdata *bd, *tmp;
  547. int have_revokes = 0;
  548. int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
  549. gfs2_ail1_empty(sdp);
  550. spin_lock(&sdp->sd_ail_lock);
  551. list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
  552. list_for_each_entry(bd, &tr->tr_ail2_list, bd_ail_st_list) {
  553. if (list_empty(&bd->bd_list)) {
  554. have_revokes = 1;
  555. goto done;
  556. }
  557. }
  558. }
  559. done:
  560. spin_unlock(&sdp->sd_ail_lock);
  561. if (have_revokes == 0)
  562. return;
  563. while (sdp->sd_log_num_revoke > max_revokes)
  564. max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
  565. max_revokes -= sdp->sd_log_num_revoke;
  566. if (!sdp->sd_log_num_revoke) {
  567. atomic_dec(&sdp->sd_log_blks_free);
  568. /* If no blocks have been reserved, we need to also
  569. * reserve a block for the header */
  570. if (!sdp->sd_log_blks_reserved)
  571. atomic_dec(&sdp->sd_log_blks_free);
  572. }
  573. gfs2_log_lock(sdp);
  574. spin_lock(&sdp->sd_ail_lock);
  575. list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
  576. list_for_each_entry_safe(bd, tmp, &tr->tr_ail2_list, bd_ail_st_list) {
  577. if (max_revokes == 0)
  578. goto out_of_blocks;
  579. if (!list_empty(&bd->bd_list))
  580. continue;
  581. gfs2_add_revoke(sdp, bd);
  582. max_revokes--;
  583. }
  584. }
  585. out_of_blocks:
  586. spin_unlock(&sdp->sd_ail_lock);
  587. gfs2_log_unlock(sdp);
  588. if (!sdp->sd_log_num_revoke) {
  589. atomic_inc(&sdp->sd_log_blks_free);
  590. if (!sdp->sd_log_blks_reserved)
  591. atomic_inc(&sdp->sd_log_blks_free);
  592. }
  593. }
  594. /**
  595. * write_log_header - Write a journal log header buffer at sd_log_flush_head
  596. * @sdp: The GFS2 superblock
  597. * @jd: journal descriptor of the journal to which we are writing
  598. * @seq: sequence number
  599. * @tail: tail of the log
  600. * @flags: log header flags GFS2_LOG_HEAD_*
  601. * @op_flags: flags to pass to the bio
  602. *
  603. * Returns: the initialized log buffer descriptor
  604. */
  605. void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
  606. u64 seq, u32 tail, u32 flags, int op_flags)
  607. {
  608. struct gfs2_log_header *lh;
  609. u32 hash, crc;
  610. struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  611. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  612. struct timespec64 tv;
  613. struct super_block *sb = sdp->sd_vfs;
  614. u64 addr;
  615. lh = page_address(page);
  616. clear_page(lh);
  617. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  618. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  619. lh->lh_header.__pad0 = cpu_to_be64(0);
  620. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  621. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  622. lh->lh_sequence = cpu_to_be64(seq);
  623. lh->lh_flags = cpu_to_be32(flags);
  624. lh->lh_tail = cpu_to_be32(tail);
  625. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  626. hash = ~crc32(~0, lh, LH_V1_SIZE);
  627. lh->lh_hash = cpu_to_be32(hash);
  628. ktime_get_coarse_real_ts64(&tv);
  629. lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
  630. lh->lh_sec = cpu_to_be64(tv.tv_sec);
  631. addr = gfs2_log_bmap(sdp);
  632. lh->lh_addr = cpu_to_be64(addr);
  633. lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
  634. /* We may only write local statfs, quota, etc., when writing to our
  635. own journal. The values are left 0 when recovering a journal
  636. different from our own. */
  637. if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
  638. lh->lh_statfs_addr =
  639. cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
  640. lh->lh_quota_addr =
  641. cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
  642. spin_lock(&sdp->sd_statfs_spin);
  643. lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
  644. lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
  645. lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
  646. spin_unlock(&sdp->sd_statfs_spin);
  647. }
  648. BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
  649. crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
  650. sb->s_blocksize - LH_V1_SIZE - 4);
  651. lh->lh_crc = cpu_to_be32(crc);
  652. gfs2_log_write(sdp, page, sb->s_blocksize, 0, addr);
  653. gfs2_log_flush_bio(sdp, REQ_OP_WRITE, op_flags);
  654. log_flush_wait(sdp);
  655. }
  656. /**
  657. * log_write_header - Get and initialize a journal header buffer
  658. * @sdp: The GFS2 superblock
  659. * @flags: The log header flags, including log header origin
  660. *
  661. * Returns: the initialized log buffer descriptor
  662. */
  663. static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
  664. {
  665. unsigned int tail;
  666. int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
  667. enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
  668. gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
  669. tail = current_tail(sdp);
  670. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
  671. gfs2_ordered_wait(sdp);
  672. log_flush_wait(sdp);
  673. op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
  674. }
  675. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  676. gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++, tail,
  677. flags, op_flags);
  678. if (sdp->sd_log_tail != tail)
  679. log_pull_tail(sdp, tail);
  680. }
  681. /**
  682. * gfs2_log_flush - flush incore transaction(s)
  683. * @sdp: the filesystem
  684. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  685. * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
  686. *
  687. */
  688. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
  689. {
  690. struct gfs2_trans *tr;
  691. enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
  692. down_write(&sdp->sd_log_flush_lock);
  693. /* Log might have been flushed while we waited for the flush lock */
  694. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  695. up_write(&sdp->sd_log_flush_lock);
  696. return;
  697. }
  698. trace_gfs2_log_flush(sdp, 1, flags);
  699. if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
  700. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  701. sdp->sd_log_flush_head = sdp->sd_log_head;
  702. tr = sdp->sd_log_tr;
  703. if (tr) {
  704. sdp->sd_log_tr = NULL;
  705. tr->tr_first = sdp->sd_log_flush_head;
  706. if (unlikely (state == SFS_FROZEN))
  707. gfs2_assert_withdraw(sdp, !tr->tr_num_buf_new && !tr->tr_num_databuf_new);
  708. }
  709. if (unlikely(state == SFS_FROZEN))
  710. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  711. gfs2_assert_withdraw(sdp,
  712. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  713. gfs2_ordered_write(sdp);
  714. lops_before_commit(sdp, tr);
  715. gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0);
  716. if (sdp->sd_log_head != sdp->sd_log_flush_head) {
  717. log_flush_wait(sdp);
  718. log_write_header(sdp, flags);
  719. } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  720. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  721. trace_gfs2_log_blocks(sdp, -1);
  722. log_write_header(sdp, flags);
  723. }
  724. lops_after_commit(sdp, tr);
  725. gfs2_log_lock(sdp);
  726. sdp->sd_log_head = sdp->sd_log_flush_head;
  727. sdp->sd_log_blks_reserved = 0;
  728. sdp->sd_log_commited_revoke = 0;
  729. spin_lock(&sdp->sd_ail_lock);
  730. if (tr && !list_empty(&tr->tr_ail1_list)) {
  731. list_add(&tr->tr_list, &sdp->sd_ail1_list);
  732. tr = NULL;
  733. }
  734. spin_unlock(&sdp->sd_ail_lock);
  735. gfs2_log_unlock(sdp);
  736. if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
  737. if (!sdp->sd_log_idle) {
  738. for (;;) {
  739. gfs2_ail1_start(sdp);
  740. gfs2_ail1_wait(sdp);
  741. if (gfs2_ail1_empty(sdp))
  742. break;
  743. }
  744. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  745. trace_gfs2_log_blocks(sdp, -1);
  746. log_write_header(sdp, flags);
  747. sdp->sd_log_head = sdp->sd_log_flush_head;
  748. }
  749. if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
  750. GFS2_LOG_HEAD_FLUSH_FREEZE))
  751. gfs2_log_shutdown(sdp);
  752. if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE)
  753. atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
  754. }
  755. trace_gfs2_log_flush(sdp, 0, flags);
  756. up_write(&sdp->sd_log_flush_lock);
  757. kfree(tr);
  758. }
  759. /**
  760. * gfs2_merge_trans - Merge a new transaction into a cached transaction
  761. * @old: Original transaction to be expanded
  762. * @new: New transaction to be merged
  763. */
  764. static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new)
  765. {
  766. struct gfs2_trans *old = sdp->sd_log_tr;
  767. WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
  768. old->tr_num_buf_new += new->tr_num_buf_new;
  769. old->tr_num_databuf_new += new->tr_num_databuf_new;
  770. old->tr_num_buf_rm += new->tr_num_buf_rm;
  771. old->tr_num_databuf_rm += new->tr_num_databuf_rm;
  772. old->tr_num_revoke += new->tr_num_revoke;
  773. old->tr_num_revoke_rm += new->tr_num_revoke_rm;
  774. list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
  775. list_splice_tail_init(&new->tr_buf, &old->tr_buf);
  776. spin_lock(&sdp->sd_ail_lock);
  777. list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list);
  778. list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list);
  779. spin_unlock(&sdp->sd_ail_lock);
  780. }
  781. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  782. {
  783. unsigned int reserved;
  784. unsigned int unused;
  785. unsigned int maxres;
  786. gfs2_log_lock(sdp);
  787. if (sdp->sd_log_tr) {
  788. gfs2_merge_trans(sdp, tr);
  789. } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
  790. gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags));
  791. sdp->sd_log_tr = tr;
  792. set_bit(TR_ATTACHED, &tr->tr_flags);
  793. }
  794. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  795. reserved = calc_reserved(sdp);
  796. maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
  797. gfs2_assert_withdraw(sdp, maxres >= reserved);
  798. unused = maxres - reserved;
  799. atomic_add(unused, &sdp->sd_log_blks_free);
  800. trace_gfs2_log_blocks(sdp, unused);
  801. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  802. sdp->sd_jdesc->jd_blocks);
  803. sdp->sd_log_blks_reserved = reserved;
  804. gfs2_log_unlock(sdp);
  805. }
  806. /**
  807. * gfs2_log_commit - Commit a transaction to the log
  808. * @sdp: the filesystem
  809. * @tr: the transaction
  810. *
  811. * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
  812. * or the total number of used blocks (pinned blocks plus AIL blocks)
  813. * is greater than thresh2.
  814. *
  815. * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
  816. * journal size.
  817. *
  818. * Returns: errno
  819. */
  820. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  821. {
  822. log_refund(sdp, tr);
  823. if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
  824. ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
  825. atomic_read(&sdp->sd_log_thresh2)))
  826. wake_up(&sdp->sd_logd_waitq);
  827. }
  828. /**
  829. * gfs2_log_shutdown - write a shutdown header into a journal
  830. * @sdp: the filesystem
  831. *
  832. */
  833. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  834. {
  835. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  836. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  837. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  838. sdp->sd_log_flush_head = sdp->sd_log_head;
  839. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
  840. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  841. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  842. sdp->sd_log_head = sdp->sd_log_flush_head;
  843. sdp->sd_log_tail = sdp->sd_log_head;
  844. }
  845. static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
  846. {
  847. return (atomic_read(&sdp->sd_log_pinned) +
  848. atomic_read(&sdp->sd_log_blks_needed) >=
  849. atomic_read(&sdp->sd_log_thresh1));
  850. }
  851. static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
  852. {
  853. unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
  854. if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
  855. return 1;
  856. return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
  857. atomic_read(&sdp->sd_log_thresh2);
  858. }
  859. /**
  860. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  861. * @sdp: Pointer to GFS2 superblock
  862. *
  863. * Also, periodically check to make sure that we're using the most recent
  864. * journal index.
  865. */
  866. int gfs2_logd(void *data)
  867. {
  868. struct gfs2_sbd *sdp = data;
  869. unsigned long t = 1;
  870. DEFINE_WAIT(wait);
  871. bool did_flush;
  872. while (!kthread_should_stop()) {
  873. /* Check for errors writing to the journal */
  874. if (sdp->sd_log_error) {
  875. gfs2_lm_withdraw(sdp,
  876. "GFS2: fsid=%s: error %d: "
  877. "withdrawing the file system to "
  878. "prevent further damage.\n",
  879. sdp->sd_fsname, sdp->sd_log_error);
  880. }
  881. did_flush = false;
  882. if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
  883. gfs2_ail1_empty(sdp);
  884. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  885. GFS2_LFC_LOGD_JFLUSH_REQD);
  886. did_flush = true;
  887. }
  888. if (gfs2_ail_flush_reqd(sdp)) {
  889. gfs2_ail1_start(sdp);
  890. gfs2_ail1_wait(sdp);
  891. gfs2_ail1_empty(sdp);
  892. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  893. GFS2_LFC_LOGD_AIL_FLUSH_REQD);
  894. did_flush = true;
  895. }
  896. if (!gfs2_ail_flush_reqd(sdp) || did_flush)
  897. wake_up(&sdp->sd_log_waitq);
  898. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  899. try_to_freeze();
  900. do {
  901. prepare_to_wait(&sdp->sd_logd_waitq, &wait,
  902. TASK_INTERRUPTIBLE);
  903. if (!gfs2_ail_flush_reqd(sdp) &&
  904. !gfs2_jrnl_flush_reqd(sdp) &&
  905. !kthread_should_stop())
  906. t = schedule_timeout(t);
  907. } while(t && !gfs2_ail_flush_reqd(sdp) &&
  908. !gfs2_jrnl_flush_reqd(sdp) &&
  909. !kthread_should_stop());
  910. finish_wait(&sdp->sd_logd_waitq, &wait);
  911. }
  912. return 0;
  913. }