transaction.c 65 KB

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