transaction.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  1. /*
  2. * linux/fs/jbd2/transaction.c
  3. *
  4. * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
  5. *
  6. * Copyright 1998 Red Hat corp --- All Rights Reserved
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * Generic filesystem transaction handling code; part of the ext2fs
  13. * journaling system.
  14. *
  15. * This file manages transactions (compound commits managed by the
  16. * journaling code) and handles (individual atomic operations by the
  17. * filesystem).
  18. */
  19. #include <linux/time.h>
  20. #include <linux/fs.h>
  21. #include <linux/jbd2.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/timer.h>
  25. #include <linux/mm.h>
  26. #include <linux/highmem.h>
  27. #include <linux/hrtimer.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/module.h>
  30. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
  31. /*
  32. * jbd2_get_transaction: obtain a new transaction_t object.
  33. *
  34. * Simply allocate and initialise a new transaction. Create it in
  35. * RUNNING state and add it to the current journal (which should not
  36. * have an existing running transaction: we only make a new transaction
  37. * once we have started to commit the old one).
  38. *
  39. * Preconditions:
  40. * The journal MUST be locked. We don't perform atomic mallocs on the
  41. * new transaction and we can't block without protecting against other
  42. * processes trying to touch the journal while it is in transition.
  43. *
  44. */
  45. static transaction_t *
  46. jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
  47. {
  48. transaction->t_journal = journal;
  49. transaction->t_state = T_RUNNING;
  50. transaction->t_start_time = ktime_get();
  51. transaction->t_tid = journal->j_transaction_sequence++;
  52. transaction->t_expires = jiffies + journal->j_commit_interval;
  53. spin_lock_init(&transaction->t_handle_lock);
  54. atomic_set(&transaction->t_updates, 0);
  55. atomic_set(&transaction->t_outstanding_credits, 0);
  56. atomic_set(&transaction->t_handle_count, 0);
  57. INIT_LIST_HEAD(&transaction->t_inode_list);
  58. INIT_LIST_HEAD(&transaction->t_private_list);
  59. /* Set up the commit timer for the new transaction. */
  60. journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
  61. add_timer(&journal->j_commit_timer);
  62. J_ASSERT(journal->j_running_transaction == NULL);
  63. journal->j_running_transaction = transaction;
  64. transaction->t_max_wait = 0;
  65. transaction->t_start = jiffies;
  66. return transaction;
  67. }
  68. /*
  69. * Handle management.
  70. *
  71. * A handle_t is an object which represents a single atomic update to a
  72. * filesystem, and which tracks all of the modifications which form part
  73. * of that one update.
  74. */
  75. /*
  76. * Update transaction's maximum wait time, if debugging is enabled.
  77. *
  78. * In order for t_max_wait to be reliable, it must be protected by a
  79. * lock. But doing so will mean that start_this_handle() can not be
  80. * run in parallel on SMP systems, which limits our scalability. So
  81. * unless debugging is enabled, we no longer update t_max_wait, which
  82. * means that maximum wait time reported by the jbd2_run_stats
  83. * tracepoint will always be zero.
  84. */
  85. static inline void update_t_max_wait(transaction_t *transaction,
  86. unsigned long ts)
  87. {
  88. #ifdef CONFIG_JBD2_DEBUG
  89. if (jbd2_journal_enable_debug &&
  90. time_after(transaction->t_start, ts)) {
  91. ts = jbd2_time_diff(ts, transaction->t_start);
  92. spin_lock(&transaction->t_handle_lock);
  93. if (ts > transaction->t_max_wait)
  94. transaction->t_max_wait = ts;
  95. spin_unlock(&transaction->t_handle_lock);
  96. }
  97. #endif
  98. }
  99. /*
  100. * start_this_handle: Given a handle, deal with any locking or stalling
  101. * needed to make sure that there is enough journal space for the handle
  102. * to begin. Attach the handle to a transaction and set up the
  103. * transaction's buffer credits.
  104. */
  105. static int start_this_handle(journal_t *journal, handle_t *handle,
  106. int gfp_mask)
  107. {
  108. transaction_t *transaction, *new_transaction = NULL;
  109. tid_t tid;
  110. int needed, need_to_start;
  111. int nblocks = handle->h_buffer_credits;
  112. unsigned long ts = jiffies;
  113. if (nblocks > journal->j_max_transaction_buffers) {
  114. printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
  115. current->comm, nblocks,
  116. journal->j_max_transaction_buffers);
  117. return -ENOSPC;
  118. }
  119. alloc_transaction:
  120. if (!journal->j_running_transaction) {
  121. new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
  122. if (!new_transaction) {
  123. /*
  124. * If __GFP_FS is not present, then we may be
  125. * being called from inside the fs writeback
  126. * layer, so we MUST NOT fail. Since
  127. * __GFP_NOFAIL is going away, we will arrange
  128. * to retry the allocation ourselves.
  129. */
  130. if ((gfp_mask & __GFP_FS) == 0) {
  131. congestion_wait(BLK_RW_ASYNC, HZ/50);
  132. goto alloc_transaction;
  133. }
  134. return -ENOMEM;
  135. }
  136. }
  137. jbd_debug(3, "New handle %p going live.\n", handle);
  138. /*
  139. * We need to hold j_state_lock until t_updates has been incremented,
  140. * for proper journal barrier handling
  141. */
  142. repeat:
  143. read_lock(&journal->j_state_lock);
  144. BUG_ON(journal->j_flags & JBD2_UNMOUNT);
  145. if (is_journal_aborted(journal) ||
  146. (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
  147. read_unlock(&journal->j_state_lock);
  148. kfree(new_transaction);
  149. return -EROFS;
  150. }
  151. /* Wait on the journal's transaction barrier if necessary */
  152. if (journal->j_barrier_count) {
  153. read_unlock(&journal->j_state_lock);
  154. wait_event(journal->j_wait_transaction_locked,
  155. journal->j_barrier_count == 0);
  156. goto repeat;
  157. }
  158. if (!journal->j_running_transaction) {
  159. read_unlock(&journal->j_state_lock);
  160. if (!new_transaction)
  161. goto alloc_transaction;
  162. write_lock(&journal->j_state_lock);
  163. if (!journal->j_running_transaction) {
  164. jbd2_get_transaction(journal, new_transaction);
  165. new_transaction = NULL;
  166. }
  167. write_unlock(&journal->j_state_lock);
  168. goto repeat;
  169. }
  170. transaction = journal->j_running_transaction;
  171. /*
  172. * If the current transaction is locked down for commit, wait for the
  173. * lock to be released.
  174. */
  175. if (transaction->t_state == T_LOCKED) {
  176. DEFINE_WAIT(wait);
  177. prepare_to_wait(&journal->j_wait_transaction_locked,
  178. &wait, TASK_UNINTERRUPTIBLE);
  179. read_unlock(&journal->j_state_lock);
  180. schedule();
  181. finish_wait(&journal->j_wait_transaction_locked, &wait);
  182. goto repeat;
  183. }
  184. /*
  185. * If there is not enough space left in the log to write all potential
  186. * buffers requested by this operation, we need to stall pending a log
  187. * checkpoint to free some more log space.
  188. */
  189. needed = atomic_add_return(nblocks,
  190. &transaction->t_outstanding_credits);
  191. if (needed > journal->j_max_transaction_buffers) {
  192. /*
  193. * If the current transaction is already too large, then start
  194. * to commit it: we can then go back and attach this handle to
  195. * a new transaction.
  196. */
  197. DEFINE_WAIT(wait);
  198. jbd_debug(2, "Handle %p starting new commit...\n", handle);
  199. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  200. prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
  201. TASK_UNINTERRUPTIBLE);
  202. tid = transaction->t_tid;
  203. need_to_start = !tid_geq(journal->j_commit_request, tid);
  204. read_unlock(&journal->j_state_lock);
  205. if (need_to_start)
  206. jbd2_log_start_commit(journal, tid);
  207. schedule();
  208. finish_wait(&journal->j_wait_transaction_locked, &wait);
  209. goto repeat;
  210. }
  211. /*
  212. * The commit code assumes that it can get enough log space
  213. * without forcing a checkpoint. This is *critical* for
  214. * correctness: a checkpoint of a buffer which is also
  215. * associated with a committing transaction creates a deadlock,
  216. * so commit simply cannot force through checkpoints.
  217. *
  218. * We must therefore ensure the necessary space in the journal
  219. * *before* starting to dirty potentially checkpointed buffers
  220. * in the new transaction.
  221. *
  222. * The worst part is, any transaction currently committing can
  223. * reduce the free space arbitrarily. Be careful to account for
  224. * those buffers when checkpointing.
  225. */
  226. /*
  227. * @@@ AKPM: This seems rather over-defensive. We're giving commit
  228. * a _lot_ of headroom: 1/4 of the journal plus the size of
  229. * the committing transaction. Really, we only need to give it
  230. * committing_transaction->t_outstanding_credits plus "enough" for
  231. * the log control blocks.
  232. * Also, this test is inconsistent with the matching one in
  233. * jbd2_journal_extend().
  234. */
  235. if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
  236. jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
  237. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  238. read_unlock(&journal->j_state_lock);
  239. write_lock(&journal->j_state_lock);
  240. if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
  241. __jbd2_log_wait_for_space(journal);
  242. write_unlock(&journal->j_state_lock);
  243. goto repeat;
  244. }
  245. /* OK, account for the buffers that this operation expects to
  246. * use and add the handle to the running transaction.
  247. */
  248. update_t_max_wait(transaction, ts);
  249. handle->h_transaction = transaction;
  250. atomic_inc(&transaction->t_updates);
  251. atomic_inc(&transaction->t_handle_count);
  252. jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
  253. handle, nblocks,
  254. atomic_read(&transaction->t_outstanding_credits),
  255. __jbd2_log_space_left(journal));
  256. read_unlock(&journal->j_state_lock);
  257. lock_map_acquire(&handle->h_lockdep_map);
  258. kfree(new_transaction);
  259. return 0;
  260. }
  261. static struct lock_class_key jbd2_handle_key;
  262. /* Allocate a new handle. This should probably be in a slab... */
  263. static handle_t *new_handle(int nblocks)
  264. {
  265. handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
  266. if (!handle)
  267. return NULL;
  268. memset(handle, 0, sizeof(*handle));
  269. handle->h_buffer_credits = nblocks;
  270. handle->h_ref = 1;
  271. lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
  272. &jbd2_handle_key, 0);
  273. return handle;
  274. }
  275. /**
  276. * handle_t *jbd2_journal_start() - Obtain a new handle.
  277. * @journal: Journal to start transaction on.
  278. * @nblocks: number of block buffer we might modify
  279. *
  280. * We make sure that the transaction can guarantee at least nblocks of
  281. * modified buffers in the log. We block until the log can guarantee
  282. * that much space.
  283. *
  284. * This function is visible to journal users (like ext3fs), so is not
  285. * called with the journal already locked.
  286. *
  287. * Return a pointer to a newly allocated handle, or an ERR_PTR() value
  288. * on failure.
  289. */
  290. handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
  291. {
  292. handle_t *handle = journal_current_handle();
  293. int err;
  294. if (!journal)
  295. return ERR_PTR(-EROFS);
  296. if (handle) {
  297. J_ASSERT(handle->h_transaction->t_journal == journal);
  298. handle->h_ref++;
  299. return handle;
  300. }
  301. handle = new_handle(nblocks);
  302. if (!handle)
  303. return ERR_PTR(-ENOMEM);
  304. current->journal_info = handle;
  305. err = start_this_handle(journal, handle, gfp_mask);
  306. if (err < 0) {
  307. jbd2_free_handle(handle);
  308. current->journal_info = NULL;
  309. handle = ERR_PTR(err);
  310. }
  311. return handle;
  312. }
  313. EXPORT_SYMBOL(jbd2__journal_start);
  314. handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
  315. {
  316. return jbd2__journal_start(journal, nblocks, GFP_NOFS);
  317. }
  318. EXPORT_SYMBOL(jbd2_journal_start);
  319. /**
  320. * int jbd2_journal_extend() - extend buffer credits.
  321. * @handle: handle to 'extend'
  322. * @nblocks: nr blocks to try to extend by.
  323. *
  324. * Some transactions, such as large extends and truncates, can be done
  325. * atomically all at once or in several stages. The operation requests
  326. * a credit for a number of buffer modications in advance, but can
  327. * extend its credit if it needs more.
  328. *
  329. * jbd2_journal_extend tries to give the running handle more buffer credits.
  330. * It does not guarantee that allocation - this is a best-effort only.
  331. * The calling process MUST be able to deal cleanly with a failure to
  332. * extend here.
  333. *
  334. * Return 0 on success, non-zero on failure.
  335. *
  336. * return code < 0 implies an error
  337. * return code > 0 implies normal transaction-full status.
  338. */
  339. int jbd2_journal_extend(handle_t *handle, int nblocks)
  340. {
  341. transaction_t *transaction = handle->h_transaction;
  342. journal_t *journal = transaction->t_journal;
  343. int result;
  344. int wanted;
  345. result = -EIO;
  346. if (is_handle_aborted(handle))
  347. goto out;
  348. result = 1;
  349. read_lock(&journal->j_state_lock);
  350. /* Don't extend a locked-down transaction! */
  351. if (handle->h_transaction->t_state != T_RUNNING) {
  352. jbd_debug(3, "denied handle %p %d blocks: "
  353. "transaction not running\n", handle, nblocks);
  354. goto error_out;
  355. }
  356. spin_lock(&transaction->t_handle_lock);
  357. wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
  358. if (wanted > journal->j_max_transaction_buffers) {
  359. jbd_debug(3, "denied handle %p %d blocks: "
  360. "transaction too large\n", handle, nblocks);
  361. goto unlock;
  362. }
  363. if (wanted > __jbd2_log_space_left(journal)) {
  364. jbd_debug(3, "denied handle %p %d blocks: "
  365. "insufficient log space\n", handle, nblocks);
  366. goto unlock;
  367. }
  368. handle->h_buffer_credits += nblocks;
  369. atomic_add(nblocks, &transaction->t_outstanding_credits);
  370. result = 0;
  371. jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
  372. unlock:
  373. spin_unlock(&transaction->t_handle_lock);
  374. error_out:
  375. read_unlock(&journal->j_state_lock);
  376. out:
  377. return result;
  378. }
  379. /**
  380. * int jbd2_journal_restart() - restart a handle .
  381. * @handle: handle to restart
  382. * @nblocks: nr credits requested
  383. *
  384. * Restart a handle for a multi-transaction filesystem
  385. * operation.
  386. *
  387. * If the jbd2_journal_extend() call above fails to grant new buffer credits
  388. * to a running handle, a call to jbd2_journal_restart will commit the
  389. * handle's transaction so far and reattach the handle to a new
  390. * transaction capabable of guaranteeing the requested number of
  391. * credits.
  392. */
  393. int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
  394. {
  395. transaction_t *transaction = handle->h_transaction;
  396. journal_t *journal = transaction->t_journal;
  397. tid_t tid;
  398. int need_to_start, ret;
  399. /* If we've had an abort of any type, don't even think about
  400. * actually doing the restart! */
  401. if (is_handle_aborted(handle))
  402. return 0;
  403. /*
  404. * First unlink the handle from its current transaction, and start the
  405. * commit on that.
  406. */
  407. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  408. J_ASSERT(journal_current_handle() == handle);
  409. read_lock(&journal->j_state_lock);
  410. spin_lock(&transaction->t_handle_lock);
  411. atomic_sub(handle->h_buffer_credits,
  412. &transaction->t_outstanding_credits);
  413. if (atomic_dec_and_test(&transaction->t_updates))
  414. wake_up(&journal->j_wait_updates);
  415. spin_unlock(&transaction->t_handle_lock);
  416. jbd_debug(2, "restarting handle %p\n", handle);
  417. tid = transaction->t_tid;
  418. need_to_start = !tid_geq(journal->j_commit_request, tid);
  419. read_unlock(&journal->j_state_lock);
  420. if (need_to_start)
  421. jbd2_log_start_commit(journal, tid);
  422. lock_map_release(&handle->h_lockdep_map);
  423. handle->h_buffer_credits = nblocks;
  424. ret = start_this_handle(journal, handle, gfp_mask);
  425. return ret;
  426. }
  427. EXPORT_SYMBOL(jbd2__journal_restart);
  428. int jbd2_journal_restart(handle_t *handle, int nblocks)
  429. {
  430. return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
  431. }
  432. EXPORT_SYMBOL(jbd2_journal_restart);
  433. /**
  434. * void jbd2_journal_lock_updates () - establish a transaction barrier.
  435. * @journal: Journal to establish a barrier on.
  436. *
  437. * This locks out any further updates from being started, and blocks
  438. * until all existing updates have completed, returning only once the
  439. * journal is in a quiescent state with no updates running.
  440. *
  441. * The journal lock should not be held on entry.
  442. */
  443. void jbd2_journal_lock_updates(journal_t *journal)
  444. {
  445. DEFINE_WAIT(wait);
  446. write_lock(&journal->j_state_lock);
  447. ++journal->j_barrier_count;
  448. /* Wait until there are no running updates */
  449. while (1) {
  450. transaction_t *transaction = journal->j_running_transaction;
  451. if (!transaction)
  452. break;
  453. spin_lock(&transaction->t_handle_lock);
  454. if (!atomic_read(&transaction->t_updates)) {
  455. spin_unlock(&transaction->t_handle_lock);
  456. break;
  457. }
  458. prepare_to_wait(&journal->j_wait_updates, &wait,
  459. TASK_UNINTERRUPTIBLE);
  460. spin_unlock(&transaction->t_handle_lock);
  461. write_unlock(&journal->j_state_lock);
  462. schedule();
  463. finish_wait(&journal->j_wait_updates, &wait);
  464. write_lock(&journal->j_state_lock);
  465. }
  466. write_unlock(&journal->j_state_lock);
  467. /*
  468. * We have now established a barrier against other normal updates, but
  469. * we also need to barrier against other jbd2_journal_lock_updates() calls
  470. * to make sure that we serialise special journal-locked operations
  471. * too.
  472. */
  473. mutex_lock(&journal->j_barrier);
  474. }
  475. /**
  476. * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
  477. * @journal: Journal to release the barrier on.
  478. *
  479. * Release a transaction barrier obtained with jbd2_journal_lock_updates().
  480. *
  481. * Should be called without the journal lock held.
  482. */
  483. void jbd2_journal_unlock_updates (journal_t *journal)
  484. {
  485. J_ASSERT(journal->j_barrier_count != 0);
  486. mutex_unlock(&journal->j_barrier);
  487. write_lock(&journal->j_state_lock);
  488. --journal->j_barrier_count;
  489. write_unlock(&journal->j_state_lock);
  490. wake_up(&journal->j_wait_transaction_locked);
  491. }
  492. static void warn_dirty_buffer(struct buffer_head *bh)
  493. {
  494. char b[BDEVNAME_SIZE];
  495. printk(KERN_WARNING
  496. "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
  497. "There's a risk of filesystem corruption in case of system "
  498. "crash.\n",
  499. bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
  500. }
  501. /*
  502. * If the buffer is already part of the current transaction, then there
  503. * is nothing we need to do. If it is already part of a prior
  504. * transaction which we are still committing to disk, then we need to
  505. * make sure that we do not overwrite the old copy: we do copy-out to
  506. * preserve the copy going to disk. We also account the buffer against
  507. * the handle's metadata buffer credits (unless the buffer is already
  508. * part of the transaction, that is).
  509. *
  510. */
  511. static int
  512. do_get_write_access(handle_t *handle, struct journal_head *jh,
  513. int force_copy)
  514. {
  515. struct buffer_head *bh;
  516. transaction_t *transaction;
  517. journal_t *journal;
  518. int error;
  519. char *frozen_buffer = NULL;
  520. int need_copy = 0;
  521. if (is_handle_aborted(handle))
  522. return -EROFS;
  523. transaction = handle->h_transaction;
  524. journal = transaction->t_journal;
  525. jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
  526. JBUFFER_TRACE(jh, "entry");
  527. repeat:
  528. bh = jh2bh(jh);
  529. /* @@@ Need to check for errors here at some point. */
  530. lock_buffer(bh);
  531. jbd_lock_bh_state(bh);
  532. /* We now hold the buffer lock so it is safe to query the buffer
  533. * state. Is the buffer dirty?
  534. *
  535. * If so, there are two possibilities. The buffer may be
  536. * non-journaled, and undergoing a quite legitimate writeback.
  537. * Otherwise, it is journaled, and we don't expect dirty buffers
  538. * in that state (the buffers should be marked JBD_Dirty
  539. * instead.) So either the IO is being done under our own
  540. * control and this is a bug, or it's a third party IO such as
  541. * dump(8) (which may leave the buffer scheduled for read ---
  542. * ie. locked but not dirty) or tune2fs (which may actually have
  543. * the buffer dirtied, ugh.) */
  544. if (buffer_dirty(bh)) {
  545. /*
  546. * First question: is this buffer already part of the current
  547. * transaction or the existing committing transaction?
  548. */
  549. if (jh->b_transaction) {
  550. J_ASSERT_JH(jh,
  551. jh->b_transaction == transaction ||
  552. jh->b_transaction ==
  553. journal->j_committing_transaction);
  554. if (jh->b_next_transaction)
  555. J_ASSERT_JH(jh, jh->b_next_transaction ==
  556. transaction);
  557. warn_dirty_buffer(bh);
  558. }
  559. /*
  560. * In any case we need to clean the dirty flag and we must
  561. * do it under the buffer lock to be sure we don't race
  562. * with running write-out.
  563. */
  564. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  565. clear_buffer_dirty(bh);
  566. set_buffer_jbddirty(bh);
  567. }
  568. unlock_buffer(bh);
  569. error = -EROFS;
  570. if (is_handle_aborted(handle)) {
  571. jbd_unlock_bh_state(bh);
  572. goto out;
  573. }
  574. error = 0;
  575. /*
  576. * The buffer is already part of this transaction if b_transaction or
  577. * b_next_transaction points to it
  578. */
  579. if (jh->b_transaction == transaction ||
  580. jh->b_next_transaction == transaction)
  581. goto done;
  582. /*
  583. * this is the first time this transaction is touching this buffer,
  584. * reset the modified flag
  585. */
  586. jh->b_modified = 0;
  587. /*
  588. * If there is already a copy-out version of this buffer, then we don't
  589. * need to make another one
  590. */
  591. if (jh->b_frozen_data) {
  592. JBUFFER_TRACE(jh, "has frozen data");
  593. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  594. jh->b_next_transaction = transaction;
  595. goto done;
  596. }
  597. /* Is there data here we need to preserve? */
  598. if (jh->b_transaction && jh->b_transaction != transaction) {
  599. JBUFFER_TRACE(jh, "owned by older transaction");
  600. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  601. J_ASSERT_JH(jh, jh->b_transaction ==
  602. journal->j_committing_transaction);
  603. /* There is one case we have to be very careful about.
  604. * If the committing transaction is currently writing
  605. * this buffer out to disk and has NOT made a copy-out,
  606. * then we cannot modify the buffer contents at all
  607. * right now. The essence of copy-out is that it is the
  608. * extra copy, not the primary copy, which gets
  609. * journaled. If the primary copy is already going to
  610. * disk then we cannot do copy-out here. */
  611. if (jh->b_jlist == BJ_Shadow) {
  612. DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
  613. wait_queue_head_t *wqh;
  614. wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
  615. JBUFFER_TRACE(jh, "on shadow: sleep");
  616. jbd_unlock_bh_state(bh);
  617. /* commit wakes up all shadow buffers after IO */
  618. for ( ; ; ) {
  619. prepare_to_wait(wqh, &wait.wait,
  620. TASK_UNINTERRUPTIBLE);
  621. if (jh->b_jlist != BJ_Shadow)
  622. break;
  623. schedule();
  624. }
  625. finish_wait(wqh, &wait.wait);
  626. goto repeat;
  627. }
  628. /* Only do the copy if the currently-owning transaction
  629. * still needs it. If it is on the Forget list, the
  630. * committing transaction is past that stage. The
  631. * buffer had better remain locked during the kmalloc,
  632. * but that should be true --- we hold the journal lock
  633. * still and the buffer is already on the BUF_JOURNAL
  634. * list so won't be flushed.
  635. *
  636. * Subtle point, though: if this is a get_undo_access,
  637. * then we will be relying on the frozen_data to contain
  638. * the new value of the committed_data record after the
  639. * transaction, so we HAVE to force the frozen_data copy
  640. * in that case. */
  641. if (jh->b_jlist != BJ_Forget || force_copy) {
  642. JBUFFER_TRACE(jh, "generate frozen data");
  643. if (!frozen_buffer) {
  644. JBUFFER_TRACE(jh, "allocate memory for buffer");
  645. jbd_unlock_bh_state(bh);
  646. frozen_buffer =
  647. jbd2_alloc(jh2bh(jh)->b_size,
  648. GFP_NOFS);
  649. if (!frozen_buffer) {
  650. printk(KERN_EMERG
  651. "%s: OOM for frozen_buffer\n",
  652. __func__);
  653. JBUFFER_TRACE(jh, "oom!");
  654. error = -ENOMEM;
  655. jbd_lock_bh_state(bh);
  656. goto done;
  657. }
  658. goto repeat;
  659. }
  660. jh->b_frozen_data = frozen_buffer;
  661. frozen_buffer = NULL;
  662. need_copy = 1;
  663. }
  664. jh->b_next_transaction = transaction;
  665. }
  666. /*
  667. * Finally, if the buffer is not journaled right now, we need to make
  668. * sure it doesn't get written to disk before the caller actually
  669. * commits the new data
  670. */
  671. if (!jh->b_transaction) {
  672. JBUFFER_TRACE(jh, "no transaction");
  673. J_ASSERT_JH(jh, !jh->b_next_transaction);
  674. jh->b_transaction = transaction;
  675. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  676. spin_lock(&journal->j_list_lock);
  677. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  678. spin_unlock(&journal->j_list_lock);
  679. }
  680. done:
  681. if (need_copy) {
  682. struct page *page;
  683. int offset;
  684. char *source;
  685. J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
  686. "Possible IO failure.\n");
  687. page = jh2bh(jh)->b_page;
  688. offset = offset_in_page(jh2bh(jh)->b_data);
  689. source = kmap_atomic(page, KM_USER0);
  690. /* Fire data frozen trigger just before we copy the data */
  691. jbd2_buffer_frozen_trigger(jh, source + offset,
  692. jh->b_triggers);
  693. memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
  694. kunmap_atomic(source, KM_USER0);
  695. /*
  696. * Now that the frozen data is saved off, we need to store
  697. * any matching triggers.
  698. */
  699. jh->b_frozen_triggers = jh->b_triggers;
  700. }
  701. jbd_unlock_bh_state(bh);
  702. /*
  703. * If we are about to journal a buffer, then any revoke pending on it is
  704. * no longer valid
  705. */
  706. jbd2_journal_cancel_revoke(handle, jh);
  707. out:
  708. if (unlikely(frozen_buffer)) /* It's usually NULL */
  709. jbd2_free(frozen_buffer, bh->b_size);
  710. JBUFFER_TRACE(jh, "exit");
  711. return error;
  712. }
  713. /**
  714. * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
  715. * @handle: transaction to add buffer modifications to
  716. * @bh: bh to be used for metadata writes
  717. *
  718. * Returns an error code or 0 on success.
  719. *
  720. * In full data journalling mode the buffer may be of type BJ_AsyncData,
  721. * because we're write()ing a buffer which is also part of a shared mapping.
  722. */
  723. int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
  724. {
  725. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  726. int rc;
  727. /* We do not want to get caught playing with fields which the
  728. * log thread also manipulates. Make sure that the buffer
  729. * completes any outstanding IO before proceeding. */
  730. rc = do_get_write_access(handle, jh, 0);
  731. jbd2_journal_put_journal_head(jh);
  732. return rc;
  733. }
  734. /*
  735. * When the user wants to journal a newly created buffer_head
  736. * (ie. getblk() returned a new buffer and we are going to populate it
  737. * manually rather than reading off disk), then we need to keep the
  738. * buffer_head locked until it has been completely filled with new
  739. * data. In this case, we should be able to make the assertion that
  740. * the bh is not already part of an existing transaction.
  741. *
  742. * The buffer should already be locked by the caller by this point.
  743. * There is no lock ranking violation: it was a newly created,
  744. * unlocked buffer beforehand. */
  745. /**
  746. * int jbd2_journal_get_create_access () - notify intent to use newly created bh
  747. * @handle: transaction to new buffer to
  748. * @bh: new buffer.
  749. *
  750. * Call this if you create a new bh.
  751. */
  752. int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
  753. {
  754. transaction_t *transaction = handle->h_transaction;
  755. journal_t *journal = transaction->t_journal;
  756. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  757. int err;
  758. jbd_debug(5, "journal_head %p\n", jh);
  759. err = -EROFS;
  760. if (is_handle_aborted(handle))
  761. goto out;
  762. err = 0;
  763. JBUFFER_TRACE(jh, "entry");
  764. /*
  765. * The buffer may already belong to this transaction due to pre-zeroing
  766. * in the filesystem's new_block code. It may also be on the previous,
  767. * committing transaction's lists, but it HAS to be in Forget state in
  768. * that case: the transaction must have deleted the buffer for it to be
  769. * reused here.
  770. */
  771. jbd_lock_bh_state(bh);
  772. spin_lock(&journal->j_list_lock);
  773. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  774. jh->b_transaction == NULL ||
  775. (jh->b_transaction == journal->j_committing_transaction &&
  776. jh->b_jlist == BJ_Forget)));
  777. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  778. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  779. if (jh->b_transaction == NULL) {
  780. /*
  781. * Previous jbd2_journal_forget() could have left the buffer
  782. * with jbddirty bit set because it was being committed. When
  783. * the commit finished, we've filed the buffer for
  784. * checkpointing and marked it dirty. Now we are reallocating
  785. * the buffer so the transaction freeing it must have
  786. * committed and so it's safe to clear the dirty bit.
  787. */
  788. clear_buffer_dirty(jh2bh(jh));
  789. jh->b_transaction = transaction;
  790. /* first access by this transaction */
  791. jh->b_modified = 0;
  792. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  793. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  794. } else if (jh->b_transaction == journal->j_committing_transaction) {
  795. /* first access by this transaction */
  796. jh->b_modified = 0;
  797. JBUFFER_TRACE(jh, "set next transaction");
  798. jh->b_next_transaction = transaction;
  799. }
  800. spin_unlock(&journal->j_list_lock);
  801. jbd_unlock_bh_state(bh);
  802. /*
  803. * akpm: I added this. ext3_alloc_branch can pick up new indirect
  804. * blocks which contain freed but then revoked metadata. We need
  805. * to cancel the revoke in case we end up freeing it yet again
  806. * and the reallocating as data - this would cause a second revoke,
  807. * which hits an assertion error.
  808. */
  809. JBUFFER_TRACE(jh, "cancelling revoke");
  810. jbd2_journal_cancel_revoke(handle, jh);
  811. out:
  812. jbd2_journal_put_journal_head(jh);
  813. return err;
  814. }
  815. /**
  816. * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
  817. * non-rewindable consequences
  818. * @handle: transaction
  819. * @bh: buffer to undo
  820. *
  821. * Sometimes there is a need to distinguish between metadata which has
  822. * been committed to disk and that which has not. The ext3fs code uses
  823. * this for freeing and allocating space, we have to make sure that we
  824. * do not reuse freed space until the deallocation has been committed,
  825. * since if we overwrote that space we would make the delete
  826. * un-rewindable in case of a crash.
  827. *
  828. * To deal with that, jbd2_journal_get_undo_access requests write access to a
  829. * buffer for parts of non-rewindable operations such as delete
  830. * operations on the bitmaps. The journaling code must keep a copy of
  831. * the buffer's contents prior to the undo_access call until such time
  832. * as we know that the buffer has definitely been committed to disk.
  833. *
  834. * We never need to know which transaction the committed data is part
  835. * of, buffers touched here are guaranteed to be dirtied later and so
  836. * will be committed to a new transaction in due course, at which point
  837. * we can discard the old committed data pointer.
  838. *
  839. * Returns error number or 0 on success.
  840. */
  841. int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
  842. {
  843. int err;
  844. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  845. char *committed_data = NULL;
  846. JBUFFER_TRACE(jh, "entry");
  847. /*
  848. * Do this first --- it can drop the journal lock, so we want to
  849. * make sure that obtaining the committed_data is done
  850. * atomically wrt. completion of any outstanding commits.
  851. */
  852. err = do_get_write_access(handle, jh, 1);
  853. if (err)
  854. goto out;
  855. repeat:
  856. if (!jh->b_committed_data) {
  857. committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
  858. if (!committed_data) {
  859. printk(KERN_EMERG "%s: No memory for committed data\n",
  860. __func__);
  861. err = -ENOMEM;
  862. goto out;
  863. }
  864. }
  865. jbd_lock_bh_state(bh);
  866. if (!jh->b_committed_data) {
  867. /* Copy out the current buffer contents into the
  868. * preserved, committed copy. */
  869. JBUFFER_TRACE(jh, "generate b_committed data");
  870. if (!committed_data) {
  871. jbd_unlock_bh_state(bh);
  872. goto repeat;
  873. }
  874. jh->b_committed_data = committed_data;
  875. committed_data = NULL;
  876. memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
  877. }
  878. jbd_unlock_bh_state(bh);
  879. out:
  880. jbd2_journal_put_journal_head(jh);
  881. if (unlikely(committed_data))
  882. jbd2_free(committed_data, bh->b_size);
  883. return err;
  884. }
  885. /**
  886. * void jbd2_journal_set_triggers() - Add triggers for commit writeout
  887. * @bh: buffer to trigger on
  888. * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
  889. *
  890. * Set any triggers on this journal_head. This is always safe, because
  891. * triggers for a committing buffer will be saved off, and triggers for
  892. * a running transaction will match the buffer in that transaction.
  893. *
  894. * Call with NULL to clear the triggers.
  895. */
  896. void jbd2_journal_set_triggers(struct buffer_head *bh,
  897. struct jbd2_buffer_trigger_type *type)
  898. {
  899. struct journal_head *jh = bh2jh(bh);
  900. jh->b_triggers = type;
  901. }
  902. void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
  903. struct jbd2_buffer_trigger_type *triggers)
  904. {
  905. struct buffer_head *bh = jh2bh(jh);
  906. if (!triggers || !triggers->t_frozen)
  907. return;
  908. triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
  909. }
  910. void jbd2_buffer_abort_trigger(struct journal_head *jh,
  911. struct jbd2_buffer_trigger_type *triggers)
  912. {
  913. if (!triggers || !triggers->t_abort)
  914. return;
  915. triggers->t_abort(triggers, jh2bh(jh));
  916. }
  917. /**
  918. * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
  919. * @handle: transaction to add buffer to.
  920. * @bh: buffer to mark
  921. *
  922. * mark dirty metadata which needs to be journaled as part of the current
  923. * transaction.
  924. *
  925. * The buffer is placed on the transaction's metadata list and is marked
  926. * as belonging to the transaction.
  927. *
  928. * Returns error number or 0 on success.
  929. *
  930. * Special care needs to be taken if the buffer already belongs to the
  931. * current committing transaction (in which case we should have frozen
  932. * data present for that commit). In that case, we don't relink the
  933. * buffer: that only gets done when the old transaction finally
  934. * completes its commit.
  935. */
  936. int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
  937. {
  938. transaction_t *transaction = handle->h_transaction;
  939. journal_t *journal = transaction->t_journal;
  940. struct journal_head *jh = bh2jh(bh);
  941. jbd_debug(5, "journal_head %p\n", jh);
  942. JBUFFER_TRACE(jh, "entry");
  943. if (is_handle_aborted(handle))
  944. goto out;
  945. jbd_lock_bh_state(bh);
  946. if (jh->b_modified == 0) {
  947. /*
  948. * This buffer's got modified and becoming part
  949. * of the transaction. This needs to be done
  950. * once a transaction -bzzz
  951. */
  952. jh->b_modified = 1;
  953. J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
  954. handle->h_buffer_credits--;
  955. }
  956. /*
  957. * fastpath, to avoid expensive locking. If this buffer is already
  958. * on the running transaction's metadata list there is nothing to do.
  959. * Nobody can take it off again because there is a handle open.
  960. * I _think_ we're OK here with SMP barriers - a mistaken decision will
  961. * result in this test being false, so we go in and take the locks.
  962. */
  963. if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
  964. JBUFFER_TRACE(jh, "fastpath");
  965. J_ASSERT_JH(jh, jh->b_transaction ==
  966. journal->j_running_transaction);
  967. goto out_unlock_bh;
  968. }
  969. set_buffer_jbddirty(bh);
  970. /*
  971. * Metadata already on the current transaction list doesn't
  972. * need to be filed. Metadata on another transaction's list must
  973. * be committing, and will be refiled once the commit completes:
  974. * leave it alone for now.
  975. */
  976. if (jh->b_transaction != transaction) {
  977. JBUFFER_TRACE(jh, "already on other transaction");
  978. J_ASSERT_JH(jh, jh->b_transaction ==
  979. journal->j_committing_transaction);
  980. J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
  981. /* And this case is illegal: we can't reuse another
  982. * transaction's data buffer, ever. */
  983. goto out_unlock_bh;
  984. }
  985. /* That test should have eliminated the following case: */
  986. J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
  987. JBUFFER_TRACE(jh, "file as BJ_Metadata");
  988. spin_lock(&journal->j_list_lock);
  989. __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
  990. spin_unlock(&journal->j_list_lock);
  991. out_unlock_bh:
  992. jbd_unlock_bh_state(bh);
  993. out:
  994. JBUFFER_TRACE(jh, "exit");
  995. return 0;
  996. }
  997. /*
  998. * jbd2_journal_release_buffer: undo a get_write_access without any buffer
  999. * updates, if the update decided in the end that it didn't need access.
  1000. *
  1001. */
  1002. void
  1003. jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
  1004. {
  1005. BUFFER_TRACE(bh, "entry");
  1006. }
  1007. /**
  1008. * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
  1009. * @handle: transaction handle
  1010. * @bh: bh to 'forget'
  1011. *
  1012. * We can only do the bforget if there are no commits pending against the
  1013. * buffer. If the buffer is dirty in the current running transaction we
  1014. * can safely unlink it.
  1015. *
  1016. * bh may not be a journalled buffer at all - it may be a non-JBD
  1017. * buffer which came off the hashtable. Check for this.
  1018. *
  1019. * Decrements bh->b_count by one.
  1020. *
  1021. * Allow this call even if the handle has aborted --- it may be part of
  1022. * the caller's cleanup after an abort.
  1023. */
  1024. int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
  1025. {
  1026. transaction_t *transaction = handle->h_transaction;
  1027. journal_t *journal = transaction->t_journal;
  1028. struct journal_head *jh;
  1029. int drop_reserve = 0;
  1030. int err = 0;
  1031. int was_modified = 0;
  1032. BUFFER_TRACE(bh, "entry");
  1033. jbd_lock_bh_state(bh);
  1034. spin_lock(&journal->j_list_lock);
  1035. if (!buffer_jbd(bh))
  1036. goto not_jbd;
  1037. jh = bh2jh(bh);
  1038. /* Critical error: attempting to delete a bitmap buffer, maybe?
  1039. * Don't do any jbd operations, and return an error. */
  1040. if (!J_EXPECT_JH(jh, !jh->b_committed_data,
  1041. "inconsistent data on disk")) {
  1042. err = -EIO;
  1043. goto not_jbd;
  1044. }
  1045. /* keep track of wether or not this transaction modified us */
  1046. was_modified = jh->b_modified;
  1047. /*
  1048. * The buffer's going from the transaction, we must drop
  1049. * all references -bzzz
  1050. */
  1051. jh->b_modified = 0;
  1052. if (jh->b_transaction == handle->h_transaction) {
  1053. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1054. /* If we are forgetting a buffer which is already part
  1055. * of this transaction, then we can just drop it from
  1056. * the transaction immediately. */
  1057. clear_buffer_dirty(bh);
  1058. clear_buffer_jbddirty(bh);
  1059. JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
  1060. /*
  1061. * we only want to drop a reference if this transaction
  1062. * modified the buffer
  1063. */
  1064. if (was_modified)
  1065. drop_reserve = 1;
  1066. /*
  1067. * We are no longer going to journal this buffer.
  1068. * However, the commit of this transaction is still
  1069. * important to the buffer: the delete that we are now
  1070. * processing might obsolete an old log entry, so by
  1071. * committing, we can satisfy the buffer's checkpoint.
  1072. *
  1073. * So, if we have a checkpoint on the buffer, we should
  1074. * now refile the buffer on our BJ_Forget list so that
  1075. * we know to remove the checkpoint after we commit.
  1076. */
  1077. if (jh->b_cp_transaction) {
  1078. __jbd2_journal_temp_unlink_buffer(jh);
  1079. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1080. } else {
  1081. __jbd2_journal_unfile_buffer(jh);
  1082. jbd2_journal_remove_journal_head(bh);
  1083. __brelse(bh);
  1084. if (!buffer_jbd(bh)) {
  1085. spin_unlock(&journal->j_list_lock);
  1086. jbd_unlock_bh_state(bh);
  1087. __bforget(bh);
  1088. goto drop;
  1089. }
  1090. }
  1091. } else if (jh->b_transaction) {
  1092. J_ASSERT_JH(jh, (jh->b_transaction ==
  1093. journal->j_committing_transaction));
  1094. /* However, if the buffer is still owned by a prior
  1095. * (committing) transaction, we can't drop it yet... */
  1096. JBUFFER_TRACE(jh, "belongs to older transaction");
  1097. /* ... but we CAN drop it from the new transaction if we
  1098. * have also modified it since the original commit. */
  1099. if (jh->b_next_transaction) {
  1100. J_ASSERT(jh->b_next_transaction == transaction);
  1101. jh->b_next_transaction = NULL;
  1102. /*
  1103. * only drop a reference if this transaction modified
  1104. * the buffer
  1105. */
  1106. if (was_modified)
  1107. drop_reserve = 1;
  1108. }
  1109. }
  1110. not_jbd:
  1111. spin_unlock(&journal->j_list_lock);
  1112. jbd_unlock_bh_state(bh);
  1113. __brelse(bh);
  1114. drop:
  1115. if (drop_reserve) {
  1116. /* no need to reserve log space for this block -bzzz */
  1117. handle->h_buffer_credits++;
  1118. }
  1119. return err;
  1120. }
  1121. /**
  1122. * int jbd2_journal_stop() - complete a transaction
  1123. * @handle: tranaction to complete.
  1124. *
  1125. * All done for a particular handle.
  1126. *
  1127. * There is not much action needed here. We just return any remaining
  1128. * buffer credits to the transaction and remove the handle. The only
  1129. * complication is that we need to start a commit operation if the
  1130. * filesystem is marked for synchronous update.
  1131. *
  1132. * jbd2_journal_stop itself will not usually return an error, but it may
  1133. * do so in unusual circumstances. In particular, expect it to
  1134. * return -EIO if a jbd2_journal_abort has been executed since the
  1135. * transaction began.
  1136. */
  1137. int jbd2_journal_stop(handle_t *handle)
  1138. {
  1139. transaction_t *transaction = handle->h_transaction;
  1140. journal_t *journal = transaction->t_journal;
  1141. int err, wait_for_commit = 0;
  1142. tid_t tid;
  1143. pid_t pid;
  1144. J_ASSERT(journal_current_handle() == handle);
  1145. if (is_handle_aborted(handle))
  1146. err = -EIO;
  1147. else {
  1148. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  1149. err = 0;
  1150. }
  1151. if (--handle->h_ref > 0) {
  1152. jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1153. handle->h_ref);
  1154. return err;
  1155. }
  1156. jbd_debug(4, "Handle %p going down\n", handle);
  1157. /*
  1158. * Implement synchronous transaction batching. If the handle
  1159. * was synchronous, don't force a commit immediately. Let's
  1160. * yield and let another thread piggyback onto this
  1161. * transaction. Keep doing that while new threads continue to
  1162. * arrive. It doesn't cost much - we're about to run a commit
  1163. * and sleep on IO anyway. Speeds up many-threaded, many-dir
  1164. * operations by 30x or more...
  1165. *
  1166. * We try and optimize the sleep time against what the
  1167. * underlying disk can do, instead of having a static sleep
  1168. * time. This is useful for the case where our storage is so
  1169. * fast that it is more optimal to go ahead and force a flush
  1170. * and wait for the transaction to be committed than it is to
  1171. * wait for an arbitrary amount of time for new writers to
  1172. * join the transaction. We achieve this by measuring how
  1173. * long it takes to commit a transaction, and compare it with
  1174. * how long this transaction has been running, and if run time
  1175. * < commit time then we sleep for the delta and commit. This
  1176. * greatly helps super fast disks that would see slowdowns as
  1177. * more threads started doing fsyncs.
  1178. *
  1179. * But don't do this if this process was the most recent one
  1180. * to perform a synchronous write. We do this to detect the
  1181. * case where a single process is doing a stream of sync
  1182. * writes. No point in waiting for joiners in that case.
  1183. */
  1184. pid = current->pid;
  1185. if (handle->h_sync && journal->j_last_sync_writer != pid) {
  1186. u64 commit_time, trans_time;
  1187. journal->j_last_sync_writer = pid;
  1188. read_lock(&journal->j_state_lock);
  1189. commit_time = journal->j_average_commit_time;
  1190. read_unlock(&journal->j_state_lock);
  1191. trans_time = ktime_to_ns(ktime_sub(ktime_get(),
  1192. transaction->t_start_time));
  1193. commit_time = max_t(u64, commit_time,
  1194. 1000*journal->j_min_batch_time);
  1195. commit_time = min_t(u64, commit_time,
  1196. 1000*journal->j_max_batch_time);
  1197. if (trans_time < commit_time) {
  1198. ktime_t expires = ktime_add_ns(ktime_get(),
  1199. commit_time);
  1200. set_current_state(TASK_UNINTERRUPTIBLE);
  1201. schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
  1202. }
  1203. }
  1204. if (handle->h_sync)
  1205. transaction->t_synchronous_commit = 1;
  1206. current->journal_info = NULL;
  1207. atomic_sub(handle->h_buffer_credits,
  1208. &transaction->t_outstanding_credits);
  1209. /*
  1210. * If the handle is marked SYNC, we need to set another commit
  1211. * going! We also want to force a commit if the current
  1212. * transaction is occupying too much of the log, or if the
  1213. * transaction is too old now.
  1214. */
  1215. if (handle->h_sync ||
  1216. (atomic_read(&transaction->t_outstanding_credits) >
  1217. journal->j_max_transaction_buffers) ||
  1218. time_after_eq(jiffies, transaction->t_expires)) {
  1219. /* Do this even for aborted journals: an abort still
  1220. * completes the commit thread, it just doesn't write
  1221. * anything to disk. */
  1222. jbd_debug(2, "transaction too old, requesting commit for "
  1223. "handle %p\n", handle);
  1224. /* This is non-blocking */
  1225. jbd2_log_start_commit(journal, transaction->t_tid);
  1226. /*
  1227. * Special case: JBD2_SYNC synchronous updates require us
  1228. * to wait for the commit to complete.
  1229. */
  1230. if (handle->h_sync && !(current->flags & PF_MEMALLOC))
  1231. wait_for_commit = 1;
  1232. }
  1233. /*
  1234. * Once we drop t_updates, if it goes to zero the transaction
  1235. * could start committing on us and eventually disappear. So
  1236. * once we do this, we must not dereference transaction
  1237. * pointer again.
  1238. */
  1239. tid = transaction->t_tid;
  1240. if (atomic_dec_and_test(&transaction->t_updates)) {
  1241. wake_up(&journal->j_wait_updates);
  1242. if (journal->j_barrier_count)
  1243. wake_up(&journal->j_wait_transaction_locked);
  1244. }
  1245. if (wait_for_commit)
  1246. err = jbd2_log_wait_commit(journal, tid);
  1247. lock_map_release(&handle->h_lockdep_map);
  1248. jbd2_free_handle(handle);
  1249. return err;
  1250. }
  1251. /**
  1252. * int jbd2_journal_force_commit() - force any uncommitted transactions
  1253. * @journal: journal to force
  1254. *
  1255. * For synchronous operations: force any uncommitted transactions
  1256. * to disk. May seem kludgy, but it reuses all the handle batching
  1257. * code in a very simple manner.
  1258. */
  1259. int jbd2_journal_force_commit(journal_t *journal)
  1260. {
  1261. handle_t *handle;
  1262. int ret;
  1263. handle = jbd2_journal_start(journal, 1);
  1264. if (IS_ERR(handle)) {
  1265. ret = PTR_ERR(handle);
  1266. } else {
  1267. handle->h_sync = 1;
  1268. ret = jbd2_journal_stop(handle);
  1269. }
  1270. return ret;
  1271. }
  1272. /*
  1273. *
  1274. * List management code snippets: various functions for manipulating the
  1275. * transaction buffer lists.
  1276. *
  1277. */
  1278. /*
  1279. * Append a buffer to a transaction list, given the transaction's list head
  1280. * pointer.
  1281. *
  1282. * j_list_lock is held.
  1283. *
  1284. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1285. */
  1286. static inline void
  1287. __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
  1288. {
  1289. if (!*list) {
  1290. jh->b_tnext = jh->b_tprev = jh;
  1291. *list = jh;
  1292. } else {
  1293. /* Insert at the tail of the list to preserve order */
  1294. struct journal_head *first = *list, *last = first->b_tprev;
  1295. jh->b_tprev = last;
  1296. jh->b_tnext = first;
  1297. last->b_tnext = first->b_tprev = jh;
  1298. }
  1299. }
  1300. /*
  1301. * Remove a buffer from a transaction list, given the transaction's list
  1302. * head pointer.
  1303. *
  1304. * Called with j_list_lock held, and the journal may not be locked.
  1305. *
  1306. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1307. */
  1308. static inline void
  1309. __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
  1310. {
  1311. if (*list == jh) {
  1312. *list = jh->b_tnext;
  1313. if (*list == jh)
  1314. *list = NULL;
  1315. }
  1316. jh->b_tprev->b_tnext = jh->b_tnext;
  1317. jh->b_tnext->b_tprev = jh->b_tprev;
  1318. }
  1319. /*
  1320. * Remove a buffer from the appropriate transaction list.
  1321. *
  1322. * Note that this function can *change* the value of
  1323. * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
  1324. * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
  1325. * of these pointers, it could go bad. Generally the caller needs to re-read
  1326. * the pointer from the transaction_t.
  1327. *
  1328. * Called under j_list_lock. The journal may not be locked.
  1329. */
  1330. void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
  1331. {
  1332. struct journal_head **list = NULL;
  1333. transaction_t *transaction;
  1334. struct buffer_head *bh = jh2bh(jh);
  1335. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1336. transaction = jh->b_transaction;
  1337. if (transaction)
  1338. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1339. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1340. if (jh->b_jlist != BJ_None)
  1341. J_ASSERT_JH(jh, transaction != NULL);
  1342. switch (jh->b_jlist) {
  1343. case BJ_None:
  1344. return;
  1345. case BJ_Metadata:
  1346. transaction->t_nr_buffers--;
  1347. J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
  1348. list = &transaction->t_buffers;
  1349. break;
  1350. case BJ_Forget:
  1351. list = &transaction->t_forget;
  1352. break;
  1353. case BJ_IO:
  1354. list = &transaction->t_iobuf_list;
  1355. break;
  1356. case BJ_Shadow:
  1357. list = &transaction->t_shadow_list;
  1358. break;
  1359. case BJ_LogCtl:
  1360. list = &transaction->t_log_list;
  1361. break;
  1362. case BJ_Reserved:
  1363. list = &transaction->t_reserved_list;
  1364. break;
  1365. }
  1366. __blist_del_buffer(list, jh);
  1367. jh->b_jlist = BJ_None;
  1368. if (test_clear_buffer_jbddirty(bh))
  1369. mark_buffer_dirty(bh); /* Expose it to the VM */
  1370. }
  1371. void __jbd2_journal_unfile_buffer(struct journal_head *jh)
  1372. {
  1373. __jbd2_journal_temp_unlink_buffer(jh);
  1374. jh->b_transaction = NULL;
  1375. }
  1376. void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
  1377. {
  1378. jbd_lock_bh_state(jh2bh(jh));
  1379. spin_lock(&journal->j_list_lock);
  1380. __jbd2_journal_unfile_buffer(jh);
  1381. spin_unlock(&journal->j_list_lock);
  1382. jbd_unlock_bh_state(jh2bh(jh));
  1383. }
  1384. /*
  1385. * Called from jbd2_journal_try_to_free_buffers().
  1386. *
  1387. * Called under jbd_lock_bh_state(bh)
  1388. */
  1389. static void
  1390. __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
  1391. {
  1392. struct journal_head *jh;
  1393. jh = bh2jh(bh);
  1394. if (buffer_locked(bh) || buffer_dirty(bh))
  1395. goto out;
  1396. if (jh->b_next_transaction != NULL)
  1397. goto out;
  1398. spin_lock(&journal->j_list_lock);
  1399. if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
  1400. /* written-back checkpointed metadata buffer */
  1401. if (jh->b_jlist == BJ_None) {
  1402. JBUFFER_TRACE(jh, "remove from checkpoint list");
  1403. __jbd2_journal_remove_checkpoint(jh);
  1404. jbd2_journal_remove_journal_head(bh);
  1405. __brelse(bh);
  1406. }
  1407. }
  1408. spin_unlock(&journal->j_list_lock);
  1409. out:
  1410. return;
  1411. }
  1412. /**
  1413. * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
  1414. * @journal: journal for operation
  1415. * @page: to try and free
  1416. * @gfp_mask: we use the mask to detect how hard should we try to release
  1417. * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
  1418. * release the buffers.
  1419. *
  1420. *
  1421. * For all the buffers on this page,
  1422. * if they are fully written out ordered data, move them onto BUF_CLEAN
  1423. * so try_to_free_buffers() can reap them.
  1424. *
  1425. * This function returns non-zero if we wish try_to_free_buffers()
  1426. * to be called. We do this if the page is releasable by try_to_free_buffers().
  1427. * We also do it if the page has locked or dirty buffers and the caller wants
  1428. * us to perform sync or async writeout.
  1429. *
  1430. * This complicates JBD locking somewhat. We aren't protected by the
  1431. * BKL here. We wish to remove the buffer from its committing or
  1432. * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
  1433. *
  1434. * This may *change* the value of transaction_t->t_datalist, so anyone
  1435. * who looks at t_datalist needs to lock against this function.
  1436. *
  1437. * Even worse, someone may be doing a jbd2_journal_dirty_data on this
  1438. * buffer. So we need to lock against that. jbd2_journal_dirty_data()
  1439. * will come out of the lock with the buffer dirty, which makes it
  1440. * ineligible for release here.
  1441. *
  1442. * Who else is affected by this? hmm... Really the only contender
  1443. * is do_get_write_access() - it could be looking at the buffer while
  1444. * journal_try_to_free_buffer() is changing its state. But that
  1445. * cannot happen because we never reallocate freed data as metadata
  1446. * while the data is part of a transaction. Yes?
  1447. *
  1448. * Return 0 on failure, 1 on success
  1449. */
  1450. int jbd2_journal_try_to_free_buffers(journal_t *journal,
  1451. struct page *page, gfp_t gfp_mask)
  1452. {
  1453. struct buffer_head *head;
  1454. struct buffer_head *bh;
  1455. int ret = 0;
  1456. J_ASSERT(PageLocked(page));
  1457. head = page_buffers(page);
  1458. bh = head;
  1459. do {
  1460. struct journal_head *jh;
  1461. /*
  1462. * We take our own ref against the journal_head here to avoid
  1463. * having to add tons of locking around each instance of
  1464. * jbd2_journal_remove_journal_head() and
  1465. * jbd2_journal_put_journal_head().
  1466. */
  1467. jh = jbd2_journal_grab_journal_head(bh);
  1468. if (!jh)
  1469. continue;
  1470. jbd_lock_bh_state(bh);
  1471. __journal_try_to_free_buffer(journal, bh);
  1472. jbd2_journal_put_journal_head(jh);
  1473. jbd_unlock_bh_state(bh);
  1474. if (buffer_jbd(bh))
  1475. goto busy;
  1476. } while ((bh = bh->b_this_page) != head);
  1477. ret = try_to_free_buffers(page);
  1478. busy:
  1479. return ret;
  1480. }
  1481. /*
  1482. * This buffer is no longer needed. If it is on an older transaction's
  1483. * checkpoint list we need to record it on this transaction's forget list
  1484. * to pin this buffer (and hence its checkpointing transaction) down until
  1485. * this transaction commits. If the buffer isn't on a checkpoint list, we
  1486. * release it.
  1487. * Returns non-zero if JBD no longer has an interest in the buffer.
  1488. *
  1489. * Called under j_list_lock.
  1490. *
  1491. * Called under jbd_lock_bh_state(bh).
  1492. */
  1493. static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
  1494. {
  1495. int may_free = 1;
  1496. struct buffer_head *bh = jh2bh(jh);
  1497. __jbd2_journal_unfile_buffer(jh);
  1498. if (jh->b_cp_transaction) {
  1499. JBUFFER_TRACE(jh, "on running+cp transaction");
  1500. /*
  1501. * We don't want to write the buffer anymore, clear the
  1502. * bit so that we don't confuse checks in
  1503. * __journal_file_buffer
  1504. */
  1505. clear_buffer_dirty(bh);
  1506. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1507. may_free = 0;
  1508. } else {
  1509. JBUFFER_TRACE(jh, "on running transaction");
  1510. jbd2_journal_remove_journal_head(bh);
  1511. __brelse(bh);
  1512. }
  1513. return may_free;
  1514. }
  1515. /*
  1516. * jbd2_journal_invalidatepage
  1517. *
  1518. * This code is tricky. It has a number of cases to deal with.
  1519. *
  1520. * There are two invariants which this code relies on:
  1521. *
  1522. * i_size must be updated on disk before we start calling invalidatepage on the
  1523. * data.
  1524. *
  1525. * This is done in ext3 by defining an ext3_setattr method which
  1526. * updates i_size before truncate gets going. By maintaining this
  1527. * invariant, we can be sure that it is safe to throw away any buffers
  1528. * attached to the current transaction: once the transaction commits,
  1529. * we know that the data will not be needed.
  1530. *
  1531. * Note however that we can *not* throw away data belonging to the
  1532. * previous, committing transaction!
  1533. *
  1534. * Any disk blocks which *are* part of the previous, committing
  1535. * transaction (and which therefore cannot be discarded immediately) are
  1536. * not going to be reused in the new running transaction
  1537. *
  1538. * The bitmap committed_data images guarantee this: any block which is
  1539. * allocated in one transaction and removed in the next will be marked
  1540. * as in-use in the committed_data bitmap, so cannot be reused until
  1541. * the next transaction to delete the block commits. This means that
  1542. * leaving committing buffers dirty is quite safe: the disk blocks
  1543. * cannot be reallocated to a different file and so buffer aliasing is
  1544. * not possible.
  1545. *
  1546. *
  1547. * The above applies mainly to ordered data mode. In writeback mode we
  1548. * don't make guarantees about the order in which data hits disk --- in
  1549. * particular we don't guarantee that new dirty data is flushed before
  1550. * transaction commit --- so it is always safe just to discard data
  1551. * immediately in that mode. --sct
  1552. */
  1553. /*
  1554. * The journal_unmap_buffer helper function returns zero if the buffer
  1555. * concerned remains pinned as an anonymous buffer belonging to an older
  1556. * transaction.
  1557. *
  1558. * We're outside-transaction here. Either or both of j_running_transaction
  1559. * and j_committing_transaction may be NULL.
  1560. */
  1561. static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
  1562. {
  1563. transaction_t *transaction;
  1564. struct journal_head *jh;
  1565. int may_free = 1;
  1566. int ret;
  1567. BUFFER_TRACE(bh, "entry");
  1568. /*
  1569. * It is safe to proceed here without the j_list_lock because the
  1570. * buffers cannot be stolen by try_to_free_buffers as long as we are
  1571. * holding the page lock. --sct
  1572. */
  1573. if (!buffer_jbd(bh))
  1574. goto zap_buffer_unlocked;
  1575. /* OK, we have data buffer in journaled mode */
  1576. write_lock(&journal->j_state_lock);
  1577. jbd_lock_bh_state(bh);
  1578. spin_lock(&journal->j_list_lock);
  1579. jh = jbd2_journal_grab_journal_head(bh);
  1580. if (!jh)
  1581. goto zap_buffer_no_jh;
  1582. /*
  1583. * We cannot remove the buffer from checkpoint lists until the
  1584. * transaction adding inode to orphan list (let's call it T)
  1585. * is committed. Otherwise if the transaction changing the
  1586. * buffer would be cleaned from the journal before T is
  1587. * committed, a crash will cause that the correct contents of
  1588. * the buffer will be lost. On the other hand we have to
  1589. * clear the buffer dirty bit at latest at the moment when the
  1590. * transaction marking the buffer as freed in the filesystem
  1591. * structures is committed because from that moment on the
  1592. * buffer can be reallocated and used by a different page.
  1593. * Since the block hasn't been freed yet but the inode has
  1594. * already been added to orphan list, it is safe for us to add
  1595. * the buffer to BJ_Forget list of the newest transaction.
  1596. */
  1597. transaction = jh->b_transaction;
  1598. if (transaction == NULL) {
  1599. /* First case: not on any transaction. If it
  1600. * has no checkpoint link, then we can zap it:
  1601. * it's a writeback-mode buffer so we don't care
  1602. * if it hits disk safely. */
  1603. if (!jh->b_cp_transaction) {
  1604. JBUFFER_TRACE(jh, "not on any transaction: zap");
  1605. goto zap_buffer;
  1606. }
  1607. if (!buffer_dirty(bh)) {
  1608. /* bdflush has written it. We can drop it now */
  1609. goto zap_buffer;
  1610. }
  1611. /* OK, it must be in the journal but still not
  1612. * written fully to disk: it's metadata or
  1613. * journaled data... */
  1614. if (journal->j_running_transaction) {
  1615. /* ... and once the current transaction has
  1616. * committed, the buffer won't be needed any
  1617. * longer. */
  1618. JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
  1619. ret = __dispose_buffer(jh,
  1620. journal->j_running_transaction);
  1621. jbd2_journal_put_journal_head(jh);
  1622. spin_unlock(&journal->j_list_lock);
  1623. jbd_unlock_bh_state(bh);
  1624. write_unlock(&journal->j_state_lock);
  1625. return ret;
  1626. } else {
  1627. /* There is no currently-running transaction. So the
  1628. * orphan record which we wrote for this file must have
  1629. * passed into commit. We must attach this buffer to
  1630. * the committing transaction, if it exists. */
  1631. if (journal->j_committing_transaction) {
  1632. JBUFFER_TRACE(jh, "give to committing trans");
  1633. ret = __dispose_buffer(jh,
  1634. journal->j_committing_transaction);
  1635. jbd2_journal_put_journal_head(jh);
  1636. spin_unlock(&journal->j_list_lock);
  1637. jbd_unlock_bh_state(bh);
  1638. write_unlock(&journal->j_state_lock);
  1639. return ret;
  1640. } else {
  1641. /* The orphan record's transaction has
  1642. * committed. We can cleanse this buffer */
  1643. clear_buffer_jbddirty(bh);
  1644. goto zap_buffer;
  1645. }
  1646. }
  1647. } else if (transaction == journal->j_committing_transaction) {
  1648. JBUFFER_TRACE(jh, "on committing transaction");
  1649. /*
  1650. * The buffer is committing, we simply cannot touch
  1651. * it. So we just set j_next_transaction to the
  1652. * running transaction (if there is one) and mark
  1653. * buffer as freed so that commit code knows it should
  1654. * clear dirty bits when it is done with the buffer.
  1655. */
  1656. set_buffer_freed(bh);
  1657. if (journal->j_running_transaction && buffer_jbddirty(bh))
  1658. jh->b_next_transaction = journal->j_running_transaction;
  1659. jbd2_journal_put_journal_head(jh);
  1660. spin_unlock(&journal->j_list_lock);
  1661. jbd_unlock_bh_state(bh);
  1662. write_unlock(&journal->j_state_lock);
  1663. return 0;
  1664. } else {
  1665. /* Good, the buffer belongs to the running transaction.
  1666. * We are writing our own transaction's data, not any
  1667. * previous one's, so it is safe to throw it away
  1668. * (remember that we expect the filesystem to have set
  1669. * i_size already for this truncate so recovery will not
  1670. * expose the disk blocks we are discarding here.) */
  1671. J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
  1672. JBUFFER_TRACE(jh, "on running transaction");
  1673. may_free = __dispose_buffer(jh, transaction);
  1674. }
  1675. zap_buffer:
  1676. jbd2_journal_put_journal_head(jh);
  1677. zap_buffer_no_jh:
  1678. spin_unlock(&journal->j_list_lock);
  1679. jbd_unlock_bh_state(bh);
  1680. write_unlock(&journal->j_state_lock);
  1681. zap_buffer_unlocked:
  1682. clear_buffer_dirty(bh);
  1683. J_ASSERT_BH(bh, !buffer_jbddirty(bh));
  1684. clear_buffer_mapped(bh);
  1685. clear_buffer_req(bh);
  1686. clear_buffer_new(bh);
  1687. bh->b_bdev = NULL;
  1688. return may_free;
  1689. }
  1690. /**
  1691. * void jbd2_journal_invalidatepage()
  1692. * @journal: journal to use for flush...
  1693. * @page: page to flush
  1694. * @offset: length of page to invalidate.
  1695. *
  1696. * Reap page buffers containing data after offset in page.
  1697. *
  1698. */
  1699. void jbd2_journal_invalidatepage(journal_t *journal,
  1700. struct page *page,
  1701. unsigned long offset)
  1702. {
  1703. struct buffer_head *head, *bh, *next;
  1704. unsigned int curr_off = 0;
  1705. int may_free = 1;
  1706. if (!PageLocked(page))
  1707. BUG();
  1708. if (!page_has_buffers(page))
  1709. return;
  1710. /* We will potentially be playing with lists other than just the
  1711. * data lists (especially for journaled data mode), so be
  1712. * cautious in our locking. */
  1713. head = bh = page_buffers(page);
  1714. do {
  1715. unsigned int next_off = curr_off + bh->b_size;
  1716. next = bh->b_this_page;
  1717. if (offset <= curr_off) {
  1718. /* This block is wholly outside the truncation point */
  1719. lock_buffer(bh);
  1720. may_free &= journal_unmap_buffer(journal, bh);
  1721. unlock_buffer(bh);
  1722. }
  1723. curr_off = next_off;
  1724. bh = next;
  1725. } while (bh != head);
  1726. if (!offset) {
  1727. if (may_free && try_to_free_buffers(page))
  1728. J_ASSERT(!page_has_buffers(page));
  1729. }
  1730. }
  1731. /*
  1732. * File a buffer on the given transaction list.
  1733. */
  1734. void __jbd2_journal_file_buffer(struct journal_head *jh,
  1735. transaction_t *transaction, int jlist)
  1736. {
  1737. struct journal_head **list = NULL;
  1738. int was_dirty = 0;
  1739. struct buffer_head *bh = jh2bh(jh);
  1740. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1741. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1742. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1743. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  1744. jh->b_transaction == NULL);
  1745. if (jh->b_transaction && jh->b_jlist == jlist)
  1746. return;
  1747. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  1748. jlist == BJ_Shadow || jlist == BJ_Forget) {
  1749. /*
  1750. * For metadata buffers, we track dirty bit in buffer_jbddirty
  1751. * instead of buffer_dirty. We should not see a dirty bit set
  1752. * here because we clear it in do_get_write_access but e.g.
  1753. * tune2fs can modify the sb and set the dirty bit at any time
  1754. * so we try to gracefully handle that.
  1755. */
  1756. if (buffer_dirty(bh))
  1757. warn_dirty_buffer(bh);
  1758. if (test_clear_buffer_dirty(bh) ||
  1759. test_clear_buffer_jbddirty(bh))
  1760. was_dirty = 1;
  1761. }
  1762. if (jh->b_transaction)
  1763. __jbd2_journal_temp_unlink_buffer(jh);
  1764. jh->b_transaction = transaction;
  1765. switch (jlist) {
  1766. case BJ_None:
  1767. J_ASSERT_JH(jh, !jh->b_committed_data);
  1768. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1769. return;
  1770. case BJ_Metadata:
  1771. transaction->t_nr_buffers++;
  1772. list = &transaction->t_buffers;
  1773. break;
  1774. case BJ_Forget:
  1775. list = &transaction->t_forget;
  1776. break;
  1777. case BJ_IO:
  1778. list = &transaction->t_iobuf_list;
  1779. break;
  1780. case BJ_Shadow:
  1781. list = &transaction->t_shadow_list;
  1782. break;
  1783. case BJ_LogCtl:
  1784. list = &transaction->t_log_list;
  1785. break;
  1786. case BJ_Reserved:
  1787. list = &transaction->t_reserved_list;
  1788. break;
  1789. }
  1790. __blist_add_buffer(list, jh);
  1791. jh->b_jlist = jlist;
  1792. if (was_dirty)
  1793. set_buffer_jbddirty(bh);
  1794. }
  1795. void jbd2_journal_file_buffer(struct journal_head *jh,
  1796. transaction_t *transaction, int jlist)
  1797. {
  1798. jbd_lock_bh_state(jh2bh(jh));
  1799. spin_lock(&transaction->t_journal->j_list_lock);
  1800. __jbd2_journal_file_buffer(jh, transaction, jlist);
  1801. spin_unlock(&transaction->t_journal->j_list_lock);
  1802. jbd_unlock_bh_state(jh2bh(jh));
  1803. }
  1804. /*
  1805. * Remove a buffer from its current buffer list in preparation for
  1806. * dropping it from its current transaction entirely. If the buffer has
  1807. * already started to be used by a subsequent transaction, refile the
  1808. * buffer on that transaction's metadata list.
  1809. *
  1810. * Called under journal->j_list_lock
  1811. *
  1812. * Called under jbd_lock_bh_state(jh2bh(jh))
  1813. */
  1814. void __jbd2_journal_refile_buffer(struct journal_head *jh)
  1815. {
  1816. int was_dirty, jlist;
  1817. struct buffer_head *bh = jh2bh(jh);
  1818. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1819. if (jh->b_transaction)
  1820. assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
  1821. /* If the buffer is now unused, just drop it. */
  1822. if (jh->b_next_transaction == NULL) {
  1823. __jbd2_journal_unfile_buffer(jh);
  1824. return;
  1825. }
  1826. /*
  1827. * It has been modified by a later transaction: add it to the new
  1828. * transaction's metadata list.
  1829. */
  1830. was_dirty = test_clear_buffer_jbddirty(bh);
  1831. __jbd2_journal_temp_unlink_buffer(jh);
  1832. jh->b_transaction = jh->b_next_transaction;
  1833. jh->b_next_transaction = NULL;
  1834. if (buffer_freed(bh))
  1835. jlist = BJ_Forget;
  1836. else if (jh->b_modified)
  1837. jlist = BJ_Metadata;
  1838. else
  1839. jlist = BJ_Reserved;
  1840. __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
  1841. J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
  1842. if (was_dirty)
  1843. set_buffer_jbddirty(bh);
  1844. }
  1845. /*
  1846. * For the unlocked version of this call, also make sure that any
  1847. * hanging journal_head is cleaned up if necessary.
  1848. *
  1849. * __jbd2_journal_refile_buffer is usually called as part of a single locked
  1850. * operation on a buffer_head, in which the caller is probably going to
  1851. * be hooking the journal_head onto other lists. In that case it is up
  1852. * to the caller to remove the journal_head if necessary. For the
  1853. * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
  1854. * doing anything else to the buffer so we need to do the cleanup
  1855. * ourselves to avoid a jh leak.
  1856. *
  1857. * *** The journal_head may be freed by this call! ***
  1858. */
  1859. void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
  1860. {
  1861. struct buffer_head *bh = jh2bh(jh);
  1862. jbd_lock_bh_state(bh);
  1863. spin_lock(&journal->j_list_lock);
  1864. __jbd2_journal_refile_buffer(jh);
  1865. jbd_unlock_bh_state(bh);
  1866. jbd2_journal_remove_journal_head(bh);
  1867. spin_unlock(&journal->j_list_lock);
  1868. __brelse(bh);
  1869. }
  1870. /*
  1871. * File inode in the inode list of the handle's transaction
  1872. */
  1873. int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
  1874. {
  1875. transaction_t *transaction = handle->h_transaction;
  1876. journal_t *journal = transaction->t_journal;
  1877. if (is_handle_aborted(handle))
  1878. return -EIO;
  1879. jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
  1880. transaction->t_tid);
  1881. /*
  1882. * First check whether inode isn't already on the transaction's
  1883. * lists without taking the lock. Note that this check is safe
  1884. * without the lock as we cannot race with somebody removing inode
  1885. * from the transaction. The reason is that we remove inode from the
  1886. * transaction only in journal_release_jbd_inode() and when we commit
  1887. * the transaction. We are guarded from the first case by holding
  1888. * a reference to the inode. We are safe against the second case
  1889. * because if jinode->i_transaction == transaction, commit code
  1890. * cannot touch the transaction because we hold reference to it,
  1891. * and if jinode->i_next_transaction == transaction, commit code
  1892. * will only file the inode where we want it.
  1893. */
  1894. if (jinode->i_transaction == transaction ||
  1895. jinode->i_next_transaction == transaction)
  1896. return 0;
  1897. spin_lock(&journal->j_list_lock);
  1898. if (jinode->i_transaction == transaction ||
  1899. jinode->i_next_transaction == transaction)
  1900. goto done;
  1901. /*
  1902. * We only ever set this variable to 1 so the test is safe. Since
  1903. * t_need_data_flush is likely to be set, we do the test to save some
  1904. * cacheline bouncing
  1905. */
  1906. if (!transaction->t_need_data_flush)
  1907. transaction->t_need_data_flush = 1;
  1908. /* On some different transaction's list - should be
  1909. * the committing one */
  1910. if (jinode->i_transaction) {
  1911. J_ASSERT(jinode->i_next_transaction == NULL);
  1912. J_ASSERT(jinode->i_transaction ==
  1913. journal->j_committing_transaction);
  1914. jinode->i_next_transaction = transaction;
  1915. goto done;
  1916. }
  1917. /* Not on any transaction list... */
  1918. J_ASSERT(!jinode->i_next_transaction);
  1919. jinode->i_transaction = transaction;
  1920. list_add(&jinode->i_list, &transaction->t_inode_list);
  1921. done:
  1922. spin_unlock(&journal->j_list_lock);
  1923. return 0;
  1924. }
  1925. /*
  1926. * File truncate and transaction commit interact with each other in a
  1927. * non-trivial way. If a transaction writing data block A is
  1928. * committing, we cannot discard the data by truncate until we have
  1929. * written them. Otherwise if we crashed after the transaction with
  1930. * write has committed but before the transaction with truncate has
  1931. * committed, we could see stale data in block A. This function is a
  1932. * helper to solve this problem. It starts writeout of the truncated
  1933. * part in case it is in the committing transaction.
  1934. *
  1935. * Filesystem code must call this function when inode is journaled in
  1936. * ordered mode before truncation happens and after the inode has been
  1937. * placed on orphan list with the new inode size. The second condition
  1938. * avoids the race that someone writes new data and we start
  1939. * committing the transaction after this function has been called but
  1940. * before a transaction for truncate is started (and furthermore it
  1941. * allows us to optimize the case where the addition to orphan list
  1942. * happens in the same transaction as write --- we don't have to write
  1943. * any data in such case).
  1944. */
  1945. int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  1946. struct jbd2_inode *jinode,
  1947. loff_t new_size)
  1948. {
  1949. transaction_t *inode_trans, *commit_trans;
  1950. int ret = 0;
  1951. /* This is a quick check to avoid locking if not necessary */
  1952. if (!jinode->i_transaction)
  1953. goto out;
  1954. /* Locks are here just to force reading of recent values, it is
  1955. * enough that the transaction was not committing before we started
  1956. * a transaction adding the inode to orphan list */
  1957. read_lock(&journal->j_state_lock);
  1958. commit_trans = journal->j_committing_transaction;
  1959. read_unlock(&journal->j_state_lock);
  1960. spin_lock(&journal->j_list_lock);
  1961. inode_trans = jinode->i_transaction;
  1962. spin_unlock(&journal->j_list_lock);
  1963. if (inode_trans == commit_trans) {
  1964. ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  1965. new_size, LLONG_MAX);
  1966. if (ret)
  1967. jbd2_journal_abort(journal, ret);
  1968. }
  1969. out:
  1970. return ret;
  1971. }