transaction.c 70 KB

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