transaction.c 66 KB

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