transaction.c 62 KB

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