transaction.c 69 KB

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