transaction.c 70 KB

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