transaction.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  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 transiaction'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. {
  87. #ifdef CONFIG_JBD2_DEBUG
  88. unsigned long ts = jiffies;
  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. if (nblocks > journal->j_max_transaction_buffers) {
  113. printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
  114. current->comm, nblocks,
  115. journal->j_max_transaction_buffers);
  116. return -ENOSPC;
  117. }
  118. alloc_transaction:
  119. if (!journal->j_running_transaction) {
  120. new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
  121. if (!new_transaction) {
  122. /*
  123. * If __GFP_FS is not present, then we may be
  124. * being called from inside the fs writeback
  125. * layer, so we MUST NOT fail. Since
  126. * __GFP_NOFAIL is going away, we will arrange
  127. * to retry the allocation ourselves.
  128. */
  129. if ((gfp_mask & __GFP_FS) == 0) {
  130. congestion_wait(BLK_RW_ASYNC, HZ/50);
  131. goto alloc_transaction;
  132. }
  133. return -ENOMEM;
  134. }
  135. }
  136. jbd_debug(3, "New handle %p going live.\n", handle);
  137. /*
  138. * We need to hold j_state_lock until t_updates has been incremented,
  139. * for proper journal barrier handling
  140. */
  141. repeat:
  142. read_lock(&journal->j_state_lock);
  143. BUG_ON(journal->j_flags & JBD2_UNMOUNT);
  144. if (is_journal_aborted(journal) ||
  145. (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
  146. read_unlock(&journal->j_state_lock);
  147. kfree(new_transaction);
  148. return -EROFS;
  149. }
  150. /* Wait on the journal's transaction barrier if necessary */
  151. if (journal->j_barrier_count) {
  152. read_unlock(&journal->j_state_lock);
  153. wait_event(journal->j_wait_transaction_locked,
  154. journal->j_barrier_count == 0);
  155. goto repeat;
  156. }
  157. if (!journal->j_running_transaction) {
  158. read_unlock(&journal->j_state_lock);
  159. if (!new_transaction)
  160. goto alloc_transaction;
  161. write_lock(&journal->j_state_lock);
  162. if (!journal->j_running_transaction) {
  163. jbd2_get_transaction(journal, new_transaction);
  164. new_transaction = NULL;
  165. }
  166. write_unlock(&journal->j_state_lock);
  167. goto repeat;
  168. }
  169. transaction = journal->j_running_transaction;
  170. /*
  171. * If the current transaction is locked down for commit, wait for the
  172. * lock to be released.
  173. */
  174. if (transaction->t_state == T_LOCKED) {
  175. DEFINE_WAIT(wait);
  176. prepare_to_wait(&journal->j_wait_transaction_locked,
  177. &wait, TASK_UNINTERRUPTIBLE);
  178. read_unlock(&journal->j_state_lock);
  179. schedule();
  180. finish_wait(&journal->j_wait_transaction_locked, &wait);
  181. goto repeat;
  182. }
  183. /*
  184. * If there is not enough space left in the log to write all potential
  185. * buffers requested by this operation, we need to stall pending a log
  186. * checkpoint to free some more log space.
  187. */
  188. needed = atomic_add_return(nblocks,
  189. &transaction->t_outstanding_credits);
  190. if (needed > journal->j_max_transaction_buffers) {
  191. /*
  192. * If the current transaction is already too large, then start
  193. * to commit it: we can then go back and attach this handle to
  194. * a new transaction.
  195. */
  196. DEFINE_WAIT(wait);
  197. jbd_debug(2, "Handle %p starting new commit...\n", handle);
  198. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  199. prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
  200. TASK_UNINTERRUPTIBLE);
  201. tid = transaction->t_tid;
  202. need_to_start = !tid_geq(journal->j_commit_request, tid);
  203. read_unlock(&journal->j_state_lock);
  204. if (need_to_start)
  205. jbd2_log_start_commit(journal, tid);
  206. schedule();
  207. finish_wait(&journal->j_wait_transaction_locked, &wait);
  208. goto repeat;
  209. }
  210. /*
  211. * The commit code assumes that it can get enough log space
  212. * without forcing a checkpoint. This is *critical* for
  213. * correctness: a checkpoint of a buffer which is also
  214. * associated with a committing transaction creates a deadlock,
  215. * so commit simply cannot force through checkpoints.
  216. *
  217. * We must therefore ensure the necessary space in the journal
  218. * *before* starting to dirty potentially checkpointed buffers
  219. * in the new transaction.
  220. *
  221. * The worst part is, any transaction currently committing can
  222. * reduce the free space arbitrarily. Be careful to account for
  223. * those buffers when checkpointing.
  224. */
  225. /*
  226. * @@@ AKPM: This seems rather over-defensive. We're giving commit
  227. * a _lot_ of headroom: 1/4 of the journal plus the size of
  228. * the committing transaction. Really, we only need to give it
  229. * committing_transaction->t_outstanding_credits plus "enough" for
  230. * the log control blocks.
  231. * Also, this test is inconsistent with the matching one in
  232. * jbd2_journal_extend().
  233. */
  234. if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
  235. jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
  236. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  237. read_unlock(&journal->j_state_lock);
  238. write_lock(&journal->j_state_lock);
  239. if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
  240. __jbd2_log_wait_for_space(journal);
  241. write_unlock(&journal->j_state_lock);
  242. goto repeat;
  243. }
  244. /* OK, account for the buffers that this operation expects to
  245. * use and add the handle to the running transaction.
  246. */
  247. update_t_max_wait(transaction);
  248. handle->h_transaction = transaction;
  249. atomic_inc(&transaction->t_updates);
  250. atomic_inc(&transaction->t_handle_count);
  251. jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
  252. handle, nblocks,
  253. atomic_read(&transaction->t_outstanding_credits),
  254. __jbd2_log_space_left(journal));
  255. read_unlock(&journal->j_state_lock);
  256. lock_map_acquire(&handle->h_lockdep_map);
  257. kfree(new_transaction);
  258. return 0;
  259. }
  260. static struct lock_class_key jbd2_handle_key;
  261. /* Allocate a new handle. This should probably be in a slab... */
  262. static handle_t *new_handle(int nblocks)
  263. {
  264. handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
  265. if (!handle)
  266. return NULL;
  267. memset(handle, 0, sizeof(*handle));
  268. handle->h_buffer_credits = nblocks;
  269. handle->h_ref = 1;
  270. lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
  271. &jbd2_handle_key, 0);
  272. return handle;
  273. }
  274. /**
  275. * handle_t *jbd2_journal_start() - Obtain a new handle.
  276. * @journal: Journal to start transaction on.
  277. * @nblocks: number of block buffer we might modify
  278. *
  279. * We make sure that the transaction can guarantee at least nblocks of
  280. * modified buffers in the log. We block until the log can guarantee
  281. * that much space.
  282. *
  283. * This function is visible to journal users (like ext3fs), so is not
  284. * called with the journal already locked.
  285. *
  286. * Return a pointer to a newly allocated handle, or NULL on failure
  287. */
  288. handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
  289. {
  290. handle_t *handle = journal_current_handle();
  291. int err;
  292. if (!journal)
  293. return ERR_PTR(-EROFS);
  294. if (handle) {
  295. J_ASSERT(handle->h_transaction->t_journal == journal);
  296. handle->h_ref++;
  297. return handle;
  298. }
  299. handle = new_handle(nblocks);
  300. if (!handle)
  301. return ERR_PTR(-ENOMEM);
  302. current->journal_info = handle;
  303. err = start_this_handle(journal, handle, gfp_mask);
  304. if (err < 0) {
  305. jbd2_free_handle(handle);
  306. current->journal_info = NULL;
  307. handle = ERR_PTR(err);
  308. }
  309. return handle;
  310. }
  311. EXPORT_SYMBOL(jbd2__journal_start);
  312. handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
  313. {
  314. return jbd2__journal_start(journal, nblocks, GFP_NOFS);
  315. }
  316. EXPORT_SYMBOL(jbd2_journal_start);
  317. /**
  318. * int jbd2_journal_extend() - extend buffer credits.
  319. * @handle: handle to 'extend'
  320. * @nblocks: nr blocks to try to extend by.
  321. *
  322. * Some transactions, such as large extends and truncates, can be done
  323. * atomically all at once or in several stages. The operation requests
  324. * a credit for a number of buffer modications in advance, but can
  325. * extend its credit if it needs more.
  326. *
  327. * jbd2_journal_extend tries to give the running handle more buffer credits.
  328. * It does not guarantee that allocation - this is a best-effort only.
  329. * The calling process MUST be able to deal cleanly with a failure to
  330. * extend here.
  331. *
  332. * Return 0 on success, non-zero on failure.
  333. *
  334. * return code < 0 implies an error
  335. * return code > 0 implies normal transaction-full status.
  336. */
  337. int jbd2_journal_extend(handle_t *handle, int nblocks)
  338. {
  339. transaction_t *transaction = handle->h_transaction;
  340. journal_t *journal = transaction->t_journal;
  341. int result;
  342. int wanted;
  343. result = -EIO;
  344. if (is_handle_aborted(handle))
  345. goto out;
  346. result = 1;
  347. read_lock(&journal->j_state_lock);
  348. /* Don't extend a locked-down transaction! */
  349. if (handle->h_transaction->t_state != T_RUNNING) {
  350. jbd_debug(3, "denied handle %p %d blocks: "
  351. "transaction not running\n", handle, nblocks);
  352. goto error_out;
  353. }
  354. spin_lock(&transaction->t_handle_lock);
  355. wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
  356. if (wanted > journal->j_max_transaction_buffers) {
  357. jbd_debug(3, "denied handle %p %d blocks: "
  358. "transaction too large\n", handle, nblocks);
  359. goto unlock;
  360. }
  361. if (wanted > __jbd2_log_space_left(journal)) {
  362. jbd_debug(3, "denied handle %p %d blocks: "
  363. "insufficient log space\n", handle, nblocks);
  364. goto unlock;
  365. }
  366. handle->h_buffer_credits += nblocks;
  367. atomic_add(nblocks, &transaction->t_outstanding_credits);
  368. result = 0;
  369. jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
  370. unlock:
  371. spin_unlock(&transaction->t_handle_lock);
  372. error_out:
  373. read_unlock(&journal->j_state_lock);
  374. out:
  375. return result;
  376. }
  377. /**
  378. * int jbd2_journal_restart() - restart a handle .
  379. * @handle: handle to restart
  380. * @nblocks: nr credits requested
  381. *
  382. * Restart a handle for a multi-transaction filesystem
  383. * operation.
  384. *
  385. * If the jbd2_journal_extend() call above fails to grant new buffer credits
  386. * to a running handle, a call to jbd2_journal_restart will commit the
  387. * handle's transaction so far and reattach the handle to a new
  388. * transaction capabable of guaranteeing the requested number of
  389. * credits.
  390. */
  391. int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
  392. {
  393. transaction_t *transaction = handle->h_transaction;
  394. journal_t *journal = transaction->t_journal;
  395. tid_t tid;
  396. int need_to_start, ret;
  397. /* If we've had an abort of any type, don't even think about
  398. * actually doing the restart! */
  399. if (is_handle_aborted(handle))
  400. return 0;
  401. /*
  402. * First unlink the handle from its current transaction, and start the
  403. * commit on that.
  404. */
  405. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  406. J_ASSERT(journal_current_handle() == handle);
  407. read_lock(&journal->j_state_lock);
  408. spin_lock(&transaction->t_handle_lock);
  409. atomic_sub(handle->h_buffer_credits,
  410. &transaction->t_outstanding_credits);
  411. if (atomic_dec_and_test(&transaction->t_updates))
  412. wake_up(&journal->j_wait_updates);
  413. spin_unlock(&transaction->t_handle_lock);
  414. jbd_debug(2, "restarting handle %p\n", handle);
  415. tid = transaction->t_tid;
  416. need_to_start = !tid_geq(journal->j_commit_request, tid);
  417. read_unlock(&journal->j_state_lock);
  418. if (need_to_start)
  419. jbd2_log_start_commit(journal, tid);
  420. lock_map_release(&handle->h_lockdep_map);
  421. handle->h_buffer_credits = nblocks;
  422. ret = start_this_handle(journal, handle, gfp_mask);
  423. return ret;
  424. }
  425. EXPORT_SYMBOL(jbd2__journal_restart);
  426. int jbd2_journal_restart(handle_t *handle, int nblocks)
  427. {
  428. return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
  429. }
  430. EXPORT_SYMBOL(jbd2_journal_restart);
  431. /**
  432. * void jbd2_journal_lock_updates () - establish a transaction barrier.
  433. * @journal: Journal to establish a barrier on.
  434. *
  435. * This locks out any further updates from being started, and blocks
  436. * until all existing updates have completed, returning only once the
  437. * journal is in a quiescent state with no updates running.
  438. *
  439. * The journal lock should not be held on entry.
  440. */
  441. void jbd2_journal_lock_updates(journal_t *journal)
  442. {
  443. DEFINE_WAIT(wait);
  444. write_lock(&journal->j_state_lock);
  445. ++journal->j_barrier_count;
  446. /* Wait until there are no running updates */
  447. while (1) {
  448. transaction_t *transaction = journal->j_running_transaction;
  449. if (!transaction)
  450. break;
  451. spin_lock(&transaction->t_handle_lock);
  452. if (!atomic_read(&transaction->t_updates)) {
  453. spin_unlock(&transaction->t_handle_lock);
  454. break;
  455. }
  456. prepare_to_wait(&journal->j_wait_updates, &wait,
  457. TASK_UNINTERRUPTIBLE);
  458. spin_unlock(&transaction->t_handle_lock);
  459. write_unlock(&journal->j_state_lock);
  460. schedule();
  461. finish_wait(&journal->j_wait_updates, &wait);
  462. write_lock(&journal->j_state_lock);
  463. }
  464. write_unlock(&journal->j_state_lock);
  465. /*
  466. * We have now established a barrier against other normal updates, but
  467. * we also need to barrier against other jbd2_journal_lock_updates() calls
  468. * to make sure that we serialise special journal-locked operations
  469. * too.
  470. */
  471. mutex_lock(&journal->j_barrier);
  472. }
  473. /**
  474. * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
  475. * @journal: Journal to release the barrier on.
  476. *
  477. * Release a transaction barrier obtained with jbd2_journal_lock_updates().
  478. *
  479. * Should be called without the journal lock held.
  480. */
  481. void jbd2_journal_unlock_updates (journal_t *journal)
  482. {
  483. J_ASSERT(journal->j_barrier_count != 0);
  484. mutex_unlock(&journal->j_barrier);
  485. write_lock(&journal->j_state_lock);
  486. --journal->j_barrier_count;
  487. write_unlock(&journal->j_state_lock);
  488. wake_up(&journal->j_wait_transaction_locked);
  489. }
  490. static void warn_dirty_buffer(struct buffer_head *bh)
  491. {
  492. char b[BDEVNAME_SIZE];
  493. printk(KERN_WARNING
  494. "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
  495. "There's a risk of filesystem corruption in case of system "
  496. "crash.\n",
  497. bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
  498. }
  499. /*
  500. * If the buffer is already part of the current transaction, then there
  501. * is nothing we need to do. If it is already part of a prior
  502. * transaction which we are still committing to disk, then we need to
  503. * make sure that we do not overwrite the old copy: we do copy-out to
  504. * preserve the copy going to disk. We also account the buffer against
  505. * the handle's metadata buffer credits (unless the buffer is already
  506. * part of the transaction, that is).
  507. *
  508. */
  509. static int
  510. do_get_write_access(handle_t *handle, struct journal_head *jh,
  511. int force_copy)
  512. {
  513. struct buffer_head *bh;
  514. transaction_t *transaction;
  515. journal_t *journal;
  516. int error;
  517. char *frozen_buffer = NULL;
  518. int need_copy = 0;
  519. if (is_handle_aborted(handle))
  520. return -EROFS;
  521. transaction = handle->h_transaction;
  522. journal = transaction->t_journal;
  523. jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
  524. JBUFFER_TRACE(jh, "entry");
  525. repeat:
  526. bh = jh2bh(jh);
  527. /* @@@ Need to check for errors here at some point. */
  528. lock_buffer(bh);
  529. jbd_lock_bh_state(bh);
  530. /* We now hold the buffer lock so it is safe to query the buffer
  531. * state. Is the buffer dirty?
  532. *
  533. * If so, there are two possibilities. The buffer may be
  534. * non-journaled, and undergoing a quite legitimate writeback.
  535. * Otherwise, it is journaled, and we don't expect dirty buffers
  536. * in that state (the buffers should be marked JBD_Dirty
  537. * instead.) So either the IO is being done under our own
  538. * control and this is a bug, or it's a third party IO such as
  539. * dump(8) (which may leave the buffer scheduled for read ---
  540. * ie. locked but not dirty) or tune2fs (which may actually have
  541. * the buffer dirtied, ugh.) */
  542. if (buffer_dirty(bh)) {
  543. /*
  544. * First question: is this buffer already part of the current
  545. * transaction or the existing committing transaction?
  546. */
  547. if (jh->b_transaction) {
  548. J_ASSERT_JH(jh,
  549. jh->b_transaction == transaction ||
  550. jh->b_transaction ==
  551. journal->j_committing_transaction);
  552. if (jh->b_next_transaction)
  553. J_ASSERT_JH(jh, jh->b_next_transaction ==
  554. transaction);
  555. warn_dirty_buffer(bh);
  556. }
  557. /*
  558. * In any case we need to clean the dirty flag and we must
  559. * do it under the buffer lock to be sure we don't race
  560. * with running write-out.
  561. */
  562. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  563. clear_buffer_dirty(bh);
  564. set_buffer_jbddirty(bh);
  565. }
  566. unlock_buffer(bh);
  567. error = -EROFS;
  568. if (is_handle_aborted(handle)) {
  569. jbd_unlock_bh_state(bh);
  570. goto out;
  571. }
  572. error = 0;
  573. /*
  574. * The buffer is already part of this transaction if b_transaction or
  575. * b_next_transaction points to it
  576. */
  577. if (jh->b_transaction == transaction ||
  578. jh->b_next_transaction == transaction)
  579. goto done;
  580. /*
  581. * this is the first time this transaction is touching this buffer,
  582. * reset the modified flag
  583. */
  584. jh->b_modified = 0;
  585. /*
  586. * If there is already a copy-out version of this buffer, then we don't
  587. * need to make another one
  588. */
  589. if (jh->b_frozen_data) {
  590. JBUFFER_TRACE(jh, "has frozen data");
  591. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  592. jh->b_next_transaction = transaction;
  593. goto done;
  594. }
  595. /* Is there data here we need to preserve? */
  596. if (jh->b_transaction && jh->b_transaction != transaction) {
  597. JBUFFER_TRACE(jh, "owned by older transaction");
  598. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  599. J_ASSERT_JH(jh, jh->b_transaction ==
  600. journal->j_committing_transaction);
  601. /* There is one case we have to be very careful about.
  602. * If the committing transaction is currently writing
  603. * this buffer out to disk and has NOT made a copy-out,
  604. * then we cannot modify the buffer contents at all
  605. * right now. The essence of copy-out is that it is the
  606. * extra copy, not the primary copy, which gets
  607. * journaled. If the primary copy is already going to
  608. * disk then we cannot do copy-out here. */
  609. if (jh->b_jlist == BJ_Shadow) {
  610. DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
  611. wait_queue_head_t *wqh;
  612. wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
  613. JBUFFER_TRACE(jh, "on shadow: sleep");
  614. jbd_unlock_bh_state(bh);
  615. /* commit wakes up all shadow buffers after IO */
  616. for ( ; ; ) {
  617. prepare_to_wait(wqh, &wait.wait,
  618. TASK_UNINTERRUPTIBLE);
  619. if (jh->b_jlist != BJ_Shadow)
  620. break;
  621. schedule();
  622. }
  623. finish_wait(wqh, &wait.wait);
  624. goto repeat;
  625. }
  626. /* Only do the copy if the currently-owning transaction
  627. * still needs it. If it is on the Forget list, the
  628. * committing transaction is past that stage. The
  629. * buffer had better remain locked during the kmalloc,
  630. * but that should be true --- we hold the journal lock
  631. * still and the buffer is already on the BUF_JOURNAL
  632. * list so won't be flushed.
  633. *
  634. * Subtle point, though: if this is a get_undo_access,
  635. * then we will be relying on the frozen_data to contain
  636. * the new value of the committed_data record after the
  637. * transaction, so we HAVE to force the frozen_data copy
  638. * in that case. */
  639. if (jh->b_jlist != BJ_Forget || force_copy) {
  640. JBUFFER_TRACE(jh, "generate frozen data");
  641. if (!frozen_buffer) {
  642. JBUFFER_TRACE(jh, "allocate memory for buffer");
  643. jbd_unlock_bh_state(bh);
  644. frozen_buffer =
  645. jbd2_alloc(jh2bh(jh)->b_size,
  646. GFP_NOFS);
  647. if (!frozen_buffer) {
  648. printk(KERN_EMERG
  649. "%s: OOM for frozen_buffer\n",
  650. __func__);
  651. JBUFFER_TRACE(jh, "oom!");
  652. error = -ENOMEM;
  653. jbd_lock_bh_state(bh);
  654. goto done;
  655. }
  656. goto repeat;
  657. }
  658. jh->b_frozen_data = frozen_buffer;
  659. frozen_buffer = NULL;
  660. need_copy = 1;
  661. }
  662. jh->b_next_transaction = transaction;
  663. }
  664. /*
  665. * Finally, if the buffer is not journaled right now, we need to make
  666. * sure it doesn't get written to disk before the caller actually
  667. * commits the new data
  668. */
  669. if (!jh->b_transaction) {
  670. JBUFFER_TRACE(jh, "no transaction");
  671. J_ASSERT_JH(jh, !jh->b_next_transaction);
  672. jh->b_transaction = transaction;
  673. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  674. spin_lock(&journal->j_list_lock);
  675. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  676. spin_unlock(&journal->j_list_lock);
  677. }
  678. done:
  679. if (need_copy) {
  680. struct page *page;
  681. int offset;
  682. char *source;
  683. J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
  684. "Possible IO failure.\n");
  685. page = jh2bh(jh)->b_page;
  686. offset = offset_in_page(jh2bh(jh)->b_data);
  687. source = kmap_atomic(page, KM_USER0);
  688. /* Fire data frozen trigger just before we copy the data */
  689. jbd2_buffer_frozen_trigger(jh, source + offset,
  690. jh->b_triggers);
  691. memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
  692. kunmap_atomic(source, KM_USER0);
  693. /*
  694. * Now that the frozen data is saved off, we need to store
  695. * any matching triggers.
  696. */
  697. jh->b_frozen_triggers = jh->b_triggers;
  698. }
  699. jbd_unlock_bh_state(bh);
  700. /*
  701. * If we are about to journal a buffer, then any revoke pending on it is
  702. * no longer valid
  703. */
  704. jbd2_journal_cancel_revoke(handle, jh);
  705. out:
  706. if (unlikely(frozen_buffer)) /* It's usually NULL */
  707. jbd2_free(frozen_buffer, bh->b_size);
  708. JBUFFER_TRACE(jh, "exit");
  709. return error;
  710. }
  711. /**
  712. * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
  713. * @handle: transaction to add buffer modifications to
  714. * @bh: bh to be used for metadata writes
  715. * @credits: variable that will receive credits for the buffer
  716. *
  717. * Returns an error code or 0 on success.
  718. *
  719. * In full data journalling mode the buffer may be of type BJ_AsyncData,
  720. * because we're write()ing a buffer which is also part of a shared mapping.
  721. */
  722. int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
  723. {
  724. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  725. int rc;
  726. /* We do not want to get caught playing with fields which the
  727. * log thread also manipulates. Make sure that the buffer
  728. * completes any outstanding IO before proceeding. */
  729. rc = do_get_write_access(handle, jh, 0);
  730. jbd2_journal_put_journal_head(jh);
  731. return rc;
  732. }
  733. /*
  734. * When the user wants to journal a newly created buffer_head
  735. * (ie. getblk() returned a new buffer and we are going to populate it
  736. * manually rather than reading off disk), then we need to keep the
  737. * buffer_head locked until it has been completely filled with new
  738. * data. In this case, we should be able to make the assertion that
  739. * the bh is not already part of an existing transaction.
  740. *
  741. * The buffer should already be locked by the caller by this point.
  742. * There is no lock ranking violation: it was a newly created,
  743. * unlocked buffer beforehand. */
  744. /**
  745. * int jbd2_journal_get_create_access () - notify intent to use newly created bh
  746. * @handle: transaction to new buffer to
  747. * @bh: new buffer.
  748. *
  749. * Call this if you create a new bh.
  750. */
  751. int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
  752. {
  753. transaction_t *transaction = handle->h_transaction;
  754. journal_t *journal = transaction->t_journal;
  755. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  756. int err;
  757. jbd_debug(5, "journal_head %p\n", jh);
  758. err = -EROFS;
  759. if (is_handle_aborted(handle))
  760. goto out;
  761. err = 0;
  762. JBUFFER_TRACE(jh, "entry");
  763. /*
  764. * The buffer may already belong to this transaction due to pre-zeroing
  765. * in the filesystem's new_block code. It may also be on the previous,
  766. * committing transaction's lists, but it HAS to be in Forget state in
  767. * that case: the transaction must have deleted the buffer for it to be
  768. * reused here.
  769. */
  770. jbd_lock_bh_state(bh);
  771. spin_lock(&journal->j_list_lock);
  772. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  773. jh->b_transaction == NULL ||
  774. (jh->b_transaction == journal->j_committing_transaction &&
  775. jh->b_jlist == BJ_Forget)));
  776. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  777. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  778. if (jh->b_transaction == NULL) {
  779. /*
  780. * Previous jbd2_journal_forget() could have left the buffer
  781. * with jbddirty bit set because it was being committed. When
  782. * the commit finished, we've filed the buffer for
  783. * checkpointing and marked it dirty. Now we are reallocating
  784. * the buffer so the transaction freeing it must have
  785. * committed and so it's safe to clear the dirty bit.
  786. */
  787. clear_buffer_dirty(jh2bh(jh));
  788. jh->b_transaction = transaction;
  789. /* first access by this transaction */
  790. jh->b_modified = 0;
  791. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  792. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  793. } else if (jh->b_transaction == journal->j_committing_transaction) {
  794. /* first access by this transaction */
  795. jh->b_modified = 0;
  796. JBUFFER_TRACE(jh, "set next transaction");
  797. jh->b_next_transaction = transaction;
  798. }
  799. spin_unlock(&journal->j_list_lock);
  800. jbd_unlock_bh_state(bh);
  801. /*
  802. * akpm: I added this. ext3_alloc_branch can pick up new indirect
  803. * blocks which contain freed but then revoked metadata. We need
  804. * to cancel the revoke in case we end up freeing it yet again
  805. * and the reallocating as data - this would cause a second revoke,
  806. * which hits an assertion error.
  807. */
  808. JBUFFER_TRACE(jh, "cancelling revoke");
  809. jbd2_journal_cancel_revoke(handle, jh);
  810. jbd2_journal_put_journal_head(jh);
  811. out:
  812. return err;
  813. }
  814. /**
  815. * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
  816. * non-rewindable consequences
  817. * @handle: transaction
  818. * @bh: buffer to undo
  819. * @credits: store the number of taken credits here (if not NULL)
  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 commiting 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. /* On some different transaction's list - should be
  1902. * the committing one */
  1903. if (jinode->i_transaction) {
  1904. J_ASSERT(jinode->i_next_transaction == NULL);
  1905. J_ASSERT(jinode->i_transaction ==
  1906. journal->j_committing_transaction);
  1907. jinode->i_next_transaction = transaction;
  1908. goto done;
  1909. }
  1910. /* Not on any transaction list... */
  1911. J_ASSERT(!jinode->i_next_transaction);
  1912. jinode->i_transaction = transaction;
  1913. list_add(&jinode->i_list, &transaction->t_inode_list);
  1914. done:
  1915. spin_unlock(&journal->j_list_lock);
  1916. return 0;
  1917. }
  1918. /*
  1919. * File truncate and transaction commit interact with each other in a
  1920. * non-trivial way. If a transaction writing data block A is
  1921. * committing, we cannot discard the data by truncate until we have
  1922. * written them. Otherwise if we crashed after the transaction with
  1923. * write has committed but before the transaction with truncate has
  1924. * committed, we could see stale data in block A. This function is a
  1925. * helper to solve this problem. It starts writeout of the truncated
  1926. * part in case it is in the committing transaction.
  1927. *
  1928. * Filesystem code must call this function when inode is journaled in
  1929. * ordered mode before truncation happens and after the inode has been
  1930. * placed on orphan list with the new inode size. The second condition
  1931. * avoids the race that someone writes new data and we start
  1932. * committing the transaction after this function has been called but
  1933. * before a transaction for truncate is started (and furthermore it
  1934. * allows us to optimize the case where the addition to orphan list
  1935. * happens in the same transaction as write --- we don't have to write
  1936. * any data in such case).
  1937. */
  1938. int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  1939. struct jbd2_inode *jinode,
  1940. loff_t new_size)
  1941. {
  1942. transaction_t *inode_trans, *commit_trans;
  1943. int ret = 0;
  1944. /* This is a quick check to avoid locking if not necessary */
  1945. if (!jinode->i_transaction)
  1946. goto out;
  1947. /* Locks are here just to force reading of recent values, it is
  1948. * enough that the transaction was not committing before we started
  1949. * a transaction adding the inode to orphan list */
  1950. read_lock(&journal->j_state_lock);
  1951. commit_trans = journal->j_committing_transaction;
  1952. read_unlock(&journal->j_state_lock);
  1953. spin_lock(&journal->j_list_lock);
  1954. inode_trans = jinode->i_transaction;
  1955. spin_unlock(&journal->j_list_lock);
  1956. if (inode_trans == commit_trans) {
  1957. ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  1958. new_size, LLONG_MAX);
  1959. if (ret)
  1960. jbd2_journal_abort(journal, ret);
  1961. }
  1962. out:
  1963. return ret;
  1964. }