transaction.c 63 KB

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