transaction.c 65 KB

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