transaction.c 65 KB

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