transaction.c 66 KB

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