transaction.c 71 KB

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