transaction.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * linux/fs/jbd2/transaction.c
  4. *
  5. * Written by Stephen C. Tweedie <[email protected]>, 1998
  6. *
  7. * Copyright 1998 Red Hat corp --- All Rights Reserved
  8. *
  9. * Generic filesystem transaction handling code; part of the ext2fs
  10. * journaling system.
  11. *
  12. * This file manages transactions (compound commits managed by the
  13. * journaling code) and handles (individual atomic operations by the
  14. * filesystem).
  15. */
  16. #include <linux/time.h>
  17. #include <linux/fs.h>
  18. #include <linux/jbd2.h>
  19. #include <linux/errno.h>
  20. #include <linux/slab.h>
  21. #include <linux/timer.h>
  22. #include <linux/mm.h>
  23. #include <linux/highmem.h>
  24. #include <linux/hrtimer.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/bug.h>
  27. #include <linux/module.h>
  28. #include <linux/sched/mm.h>
  29. #include <trace/events/jbd2.h>
  30. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
  31. static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
  32. static struct kmem_cache *transaction_cache;
  33. int __init jbd2_journal_init_transaction_cache(void)
  34. {
  35. J_ASSERT(!transaction_cache);
  36. transaction_cache = kmem_cache_create("jbd2_transaction_s",
  37. sizeof(transaction_t),
  38. 0,
  39. SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
  40. NULL);
  41. if (!transaction_cache) {
  42. pr_emerg("JBD2: failed to create transaction cache\n");
  43. return -ENOMEM;
  44. }
  45. return 0;
  46. }
  47. void jbd2_journal_destroy_transaction_cache(void)
  48. {
  49. kmem_cache_destroy(transaction_cache);
  50. transaction_cache = NULL;
  51. }
  52. void jbd2_journal_free_transaction(transaction_t *transaction)
  53. {
  54. if (unlikely(ZERO_OR_NULL_PTR(transaction)))
  55. return;
  56. kmem_cache_free(transaction_cache, transaction);
  57. }
  58. /*
  59. * Base amount of descriptor blocks we reserve for each transaction.
  60. */
  61. static int jbd2_descriptor_blocks_per_trans(journal_t *journal)
  62. {
  63. int tag_space = journal->j_blocksize - sizeof(journal_header_t);
  64. int tags_per_block;
  65. /* Subtract UUID */
  66. tag_space -= 16;
  67. if (jbd2_journal_has_csum_v2or3(journal))
  68. tag_space -= sizeof(struct jbd2_journal_block_tail);
  69. /* Commit code leaves a slack space of 16 bytes at the end of block */
  70. tags_per_block = (tag_space - 16) / journal_tag_bytes(journal);
  71. /*
  72. * Revoke descriptors are accounted separately so we need to reserve
  73. * space for commit block and normal transaction descriptor blocks.
  74. */
  75. return 1 + DIV_ROUND_UP(journal->j_max_transaction_buffers,
  76. tags_per_block);
  77. }
  78. /*
  79. * jbd2_get_transaction: obtain a new transaction_t object.
  80. *
  81. * Simply initialise a new transaction. Initialize it in
  82. * RUNNING state and add it to the current journal (which should not
  83. * have an existing running transaction: we only make a new transaction
  84. * once we have started to commit the old one).
  85. *
  86. * Preconditions:
  87. * The journal MUST be locked. We don't perform atomic mallocs on the
  88. * new transaction and we can't block without protecting against other
  89. * processes trying to touch the journal while it is in transition.
  90. *
  91. */
  92. static void jbd2_get_transaction(journal_t *journal,
  93. transaction_t *transaction)
  94. {
  95. transaction->t_journal = journal;
  96. transaction->t_state = T_RUNNING;
  97. transaction->t_start_time = ktime_get();
  98. transaction->t_tid = journal->j_transaction_sequence++;
  99. transaction->t_expires = jiffies + journal->j_commit_interval;
  100. atomic_set(&transaction->t_updates, 0);
  101. atomic_set(&transaction->t_outstanding_credits,
  102. jbd2_descriptor_blocks_per_trans(journal) +
  103. atomic_read(&journal->j_reserved_credits));
  104. atomic_set(&transaction->t_outstanding_revokes, 0);
  105. atomic_set(&transaction->t_handle_count, 0);
  106. INIT_LIST_HEAD(&transaction->t_inode_list);
  107. INIT_LIST_HEAD(&transaction->t_private_list);
  108. /* Set up the commit timer for the new transaction. */
  109. journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
  110. add_timer(&journal->j_commit_timer);
  111. J_ASSERT(journal->j_running_transaction == NULL);
  112. journal->j_running_transaction = transaction;
  113. transaction->t_max_wait = 0;
  114. transaction->t_start = jiffies;
  115. transaction->t_requested = 0;
  116. }
  117. /*
  118. * Handle management.
  119. *
  120. * A handle_t is an object which represents a single atomic update to a
  121. * filesystem, and which tracks all of the modifications which form part
  122. * of that one update.
  123. */
  124. /*
  125. * Update transaction's maximum wait time, if debugging is enabled.
  126. *
  127. * t_max_wait is carefully updated here with use of atomic compare exchange.
  128. * Note that there could be multiplre threads trying to do this simultaneously
  129. * hence using cmpxchg to avoid any use of locks in this case.
  130. * With this t_max_wait can be updated w/o enabling jbd2_journal_enable_debug.
  131. */
  132. static inline void update_t_max_wait(transaction_t *transaction,
  133. unsigned long ts)
  134. {
  135. unsigned long oldts, newts;
  136. if (time_after(transaction->t_start, ts)) {
  137. newts = jbd2_time_diff(ts, transaction->t_start);
  138. oldts = READ_ONCE(transaction->t_max_wait);
  139. while (oldts < newts)
  140. oldts = cmpxchg(&transaction->t_max_wait, oldts, newts);
  141. }
  142. }
  143. /*
  144. * Wait until running transaction passes to T_FLUSH state and new transaction
  145. * can thus be started. Also starts the commit if needed. The function expects
  146. * running transaction to exist and releases j_state_lock.
  147. */
  148. static void wait_transaction_locked(journal_t *journal)
  149. __releases(journal->j_state_lock)
  150. {
  151. DEFINE_WAIT(wait);
  152. int need_to_start;
  153. tid_t tid = journal->j_running_transaction->t_tid;
  154. prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
  155. TASK_UNINTERRUPTIBLE);
  156. need_to_start = !tid_geq(journal->j_commit_request, tid);
  157. read_unlock(&journal->j_state_lock);
  158. if (need_to_start)
  159. jbd2_log_start_commit(journal, tid);
  160. jbd2_might_wait_for_commit(journal);
  161. schedule();
  162. finish_wait(&journal->j_wait_transaction_locked, &wait);
  163. }
  164. /*
  165. * Wait until running transaction transitions from T_SWITCH to T_FLUSH
  166. * state and new transaction can thus be started. The function releases
  167. * j_state_lock.
  168. */
  169. static void wait_transaction_switching(journal_t *journal)
  170. __releases(journal->j_state_lock)
  171. {
  172. DEFINE_WAIT(wait);
  173. if (WARN_ON(!journal->j_running_transaction ||
  174. journal->j_running_transaction->t_state != T_SWITCH)) {
  175. read_unlock(&journal->j_state_lock);
  176. return;
  177. }
  178. prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
  179. TASK_UNINTERRUPTIBLE);
  180. read_unlock(&journal->j_state_lock);
  181. /*
  182. * We don't call jbd2_might_wait_for_commit() here as there's no
  183. * waiting for outstanding handles happening anymore in T_SWITCH state
  184. * and handling of reserved handles actually relies on that for
  185. * correctness.
  186. */
  187. schedule();
  188. finish_wait(&journal->j_wait_transaction_locked, &wait);
  189. }
  190. static void sub_reserved_credits(journal_t *journal, int blocks)
  191. {
  192. atomic_sub(blocks, &journal->j_reserved_credits);
  193. wake_up(&journal->j_wait_reserved);
  194. }
  195. /*
  196. * Wait until we can add credits for handle to the running transaction. Called
  197. * with j_state_lock held for reading. Returns 0 if handle joined the running
  198. * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
  199. * caller must retry.
  200. *
  201. * Note: because j_state_lock may be dropped depending on the return
  202. * value, we need to fake out sparse so ti doesn't complain about a
  203. * locking imbalance. Callers of add_transaction_credits will need to
  204. * make a similar accomodation.
  205. */
  206. static int add_transaction_credits(journal_t *journal, int blocks,
  207. int rsv_blocks)
  208. __must_hold(&journal->j_state_lock)
  209. {
  210. transaction_t *t = journal->j_running_transaction;
  211. int needed;
  212. int total = blocks + rsv_blocks;
  213. /*
  214. * If the current transaction is locked down for commit, wait
  215. * for the lock to be released.
  216. */
  217. if (t->t_state != T_RUNNING) {
  218. WARN_ON_ONCE(t->t_state >= T_FLUSH);
  219. wait_transaction_locked(journal);
  220. __acquire(&journal->j_state_lock); /* fake out sparse */
  221. return 1;
  222. }
  223. /*
  224. * If there is not enough space left in the log to write all
  225. * potential buffers requested by this operation, we need to
  226. * stall pending a log checkpoint to free some more log space.
  227. */
  228. needed = atomic_add_return(total, &t->t_outstanding_credits);
  229. if (needed > journal->j_max_transaction_buffers) {
  230. /*
  231. * If the current transaction is already too large,
  232. * then start to commit it: we can then go back and
  233. * attach this handle to a new transaction.
  234. */
  235. atomic_sub(total, &t->t_outstanding_credits);
  236. /*
  237. * Is the number of reserved credits in the current transaction too
  238. * big to fit this handle? Wait until reserved credits are freed.
  239. */
  240. if (atomic_read(&journal->j_reserved_credits) + total >
  241. journal->j_max_transaction_buffers) {
  242. read_unlock(&journal->j_state_lock);
  243. jbd2_might_wait_for_commit(journal);
  244. wait_event(journal->j_wait_reserved,
  245. atomic_read(&journal->j_reserved_credits) + total <=
  246. journal->j_max_transaction_buffers);
  247. __acquire(&journal->j_state_lock); /* fake out sparse */
  248. return 1;
  249. }
  250. wait_transaction_locked(journal);
  251. __acquire(&journal->j_state_lock); /* fake out sparse */
  252. return 1;
  253. }
  254. /*
  255. * The commit code assumes that it can get enough log space
  256. * without forcing a checkpoint. This is *critical* for
  257. * correctness: a checkpoint of a buffer which is also
  258. * associated with a committing transaction creates a deadlock,
  259. * so commit simply cannot force through checkpoints.
  260. *
  261. * We must therefore ensure the necessary space in the journal
  262. * *before* starting to dirty potentially checkpointed buffers
  263. * in the new transaction.
  264. */
  265. if (jbd2_log_space_left(journal) < journal->j_max_transaction_buffers) {
  266. atomic_sub(total, &t->t_outstanding_credits);
  267. read_unlock(&journal->j_state_lock);
  268. jbd2_might_wait_for_commit(journal);
  269. write_lock(&journal->j_state_lock);
  270. if (jbd2_log_space_left(journal) <
  271. journal->j_max_transaction_buffers)
  272. __jbd2_log_wait_for_space(journal);
  273. write_unlock(&journal->j_state_lock);
  274. __acquire(&journal->j_state_lock); /* fake out sparse */
  275. return 1;
  276. }
  277. /* No reservation? We are done... */
  278. if (!rsv_blocks)
  279. return 0;
  280. needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
  281. /* We allow at most half of a transaction to be reserved */
  282. if (needed > journal->j_max_transaction_buffers / 2) {
  283. sub_reserved_credits(journal, rsv_blocks);
  284. atomic_sub(total, &t->t_outstanding_credits);
  285. read_unlock(&journal->j_state_lock);
  286. jbd2_might_wait_for_commit(journal);
  287. wait_event(journal->j_wait_reserved,
  288. atomic_read(&journal->j_reserved_credits) + rsv_blocks
  289. <= journal->j_max_transaction_buffers / 2);
  290. __acquire(&journal->j_state_lock); /* fake out sparse */
  291. return 1;
  292. }
  293. return 0;
  294. }
  295. /*
  296. * start_this_handle: Given a handle, deal with any locking or stalling
  297. * needed to make sure that there is enough journal space for the handle
  298. * to begin. Attach the handle to a transaction and set up the
  299. * transaction's buffer credits.
  300. */
  301. static int start_this_handle(journal_t *journal, handle_t *handle,
  302. gfp_t gfp_mask)
  303. {
  304. transaction_t *transaction, *new_transaction = NULL;
  305. int blocks = handle->h_total_credits;
  306. int rsv_blocks = 0;
  307. unsigned long ts = jiffies;
  308. if (handle->h_rsv_handle)
  309. rsv_blocks = handle->h_rsv_handle->h_total_credits;
  310. /*
  311. * Limit the number of reserved credits to 1/2 of maximum transaction
  312. * size and limit the number of total credits to not exceed maximum
  313. * transaction size per operation.
  314. */
  315. if ((rsv_blocks > journal->j_max_transaction_buffers / 2) ||
  316. (rsv_blocks + blocks > journal->j_max_transaction_buffers)) {
  317. printk(KERN_ERR "JBD2: %s wants too many credits "
  318. "credits:%d rsv_credits:%d max:%d\n",
  319. current->comm, blocks, rsv_blocks,
  320. journal->j_max_transaction_buffers);
  321. WARN_ON(1);
  322. return -ENOSPC;
  323. }
  324. alloc_transaction:
  325. /*
  326. * This check is racy but it is just an optimization of allocating new
  327. * transaction early if there are high chances we'll need it. If we
  328. * guess wrong, we'll retry or free unused transaction.
  329. */
  330. if (!data_race(journal->j_running_transaction)) {
  331. /*
  332. * If __GFP_FS is not present, then we may be being called from
  333. * inside the fs writeback layer, so we MUST NOT fail.
  334. */
  335. if ((gfp_mask & __GFP_FS) == 0)
  336. gfp_mask |= __GFP_NOFAIL;
  337. new_transaction = kmem_cache_zalloc(transaction_cache,
  338. gfp_mask);
  339. if (!new_transaction)
  340. return -ENOMEM;
  341. }
  342. jbd2_debug(3, "New handle %p going live.\n", handle);
  343. /*
  344. * We need to hold j_state_lock until t_updates has been incremented,
  345. * for proper journal barrier handling
  346. */
  347. repeat:
  348. read_lock(&journal->j_state_lock);
  349. BUG_ON(journal->j_flags & JBD2_UNMOUNT);
  350. if (is_journal_aborted(journal) ||
  351. (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
  352. read_unlock(&journal->j_state_lock);
  353. jbd2_journal_free_transaction(new_transaction);
  354. return -EROFS;
  355. }
  356. /*
  357. * Wait on the journal's transaction barrier if necessary. Specifically
  358. * we allow reserved handles to proceed because otherwise commit could
  359. * deadlock on page writeback not being able to complete.
  360. */
  361. if (!handle->h_reserved && journal->j_barrier_count) {
  362. read_unlock(&journal->j_state_lock);
  363. wait_event(journal->j_wait_transaction_locked,
  364. journal->j_barrier_count == 0);
  365. goto repeat;
  366. }
  367. if (!journal->j_running_transaction) {
  368. read_unlock(&journal->j_state_lock);
  369. if (!new_transaction)
  370. goto alloc_transaction;
  371. write_lock(&journal->j_state_lock);
  372. if (!journal->j_running_transaction &&
  373. (handle->h_reserved || !journal->j_barrier_count)) {
  374. jbd2_get_transaction(journal, new_transaction);
  375. new_transaction = NULL;
  376. }
  377. write_unlock(&journal->j_state_lock);
  378. goto repeat;
  379. }
  380. transaction = journal->j_running_transaction;
  381. if (!handle->h_reserved) {
  382. /* We may have dropped j_state_lock - restart in that case */
  383. if (add_transaction_credits(journal, blocks, rsv_blocks)) {
  384. /*
  385. * add_transaction_credits releases
  386. * j_state_lock on a non-zero return
  387. */
  388. __release(&journal->j_state_lock);
  389. goto repeat;
  390. }
  391. } else {
  392. /*
  393. * We have handle reserved so we are allowed to join T_LOCKED
  394. * transaction and we don't have to check for transaction size
  395. * and journal space. But we still have to wait while running
  396. * transaction is being switched to a committing one as it
  397. * won't wait for any handles anymore.
  398. */
  399. if (transaction->t_state == T_SWITCH) {
  400. wait_transaction_switching(journal);
  401. goto repeat;
  402. }
  403. sub_reserved_credits(journal, blocks);
  404. handle->h_reserved = 0;
  405. }
  406. /* OK, account for the buffers that this operation expects to
  407. * use and add the handle to the running transaction.
  408. */
  409. update_t_max_wait(transaction, ts);
  410. handle->h_transaction = transaction;
  411. handle->h_requested_credits = blocks;
  412. handle->h_revoke_credits_requested = handle->h_revoke_credits;
  413. handle->h_start_jiffies = jiffies;
  414. atomic_inc(&transaction->t_updates);
  415. atomic_inc(&transaction->t_handle_count);
  416. jbd2_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
  417. handle, blocks,
  418. atomic_read(&transaction->t_outstanding_credits),
  419. jbd2_log_space_left(journal));
  420. read_unlock(&journal->j_state_lock);
  421. current->journal_info = handle;
  422. rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_);
  423. jbd2_journal_free_transaction(new_transaction);
  424. /*
  425. * Ensure that no allocations done while the transaction is open are
  426. * going to recurse back to the fs layer.
  427. */
  428. handle->saved_alloc_context = memalloc_nofs_save();
  429. return 0;
  430. }
  431. /* Allocate a new handle. This should probably be in a slab... */
  432. static handle_t *new_handle(int nblocks)
  433. {
  434. handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
  435. if (!handle)
  436. return NULL;
  437. handle->h_total_credits = nblocks;
  438. handle->h_ref = 1;
  439. return handle;
  440. }
  441. handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
  442. int revoke_records, gfp_t gfp_mask,
  443. unsigned int type, unsigned int line_no)
  444. {
  445. handle_t *handle = journal_current_handle();
  446. int err;
  447. if (!journal)
  448. return ERR_PTR(-EROFS);
  449. if (handle) {
  450. J_ASSERT(handle->h_transaction->t_journal == journal);
  451. handle->h_ref++;
  452. return handle;
  453. }
  454. nblocks += DIV_ROUND_UP(revoke_records,
  455. journal->j_revoke_records_per_block);
  456. handle = new_handle(nblocks);
  457. if (!handle)
  458. return ERR_PTR(-ENOMEM);
  459. if (rsv_blocks) {
  460. handle_t *rsv_handle;
  461. rsv_handle = new_handle(rsv_blocks);
  462. if (!rsv_handle) {
  463. jbd2_free_handle(handle);
  464. return ERR_PTR(-ENOMEM);
  465. }
  466. rsv_handle->h_reserved = 1;
  467. rsv_handle->h_journal = journal;
  468. handle->h_rsv_handle = rsv_handle;
  469. }
  470. handle->h_revoke_credits = revoke_records;
  471. err = start_this_handle(journal, handle, gfp_mask);
  472. if (err < 0) {
  473. if (handle->h_rsv_handle)
  474. jbd2_free_handle(handle->h_rsv_handle);
  475. jbd2_free_handle(handle);
  476. return ERR_PTR(err);
  477. }
  478. handle->h_type = type;
  479. handle->h_line_no = line_no;
  480. trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
  481. handle->h_transaction->t_tid, type,
  482. line_no, nblocks);
  483. return handle;
  484. }
  485. EXPORT_SYMBOL(jbd2__journal_start);
  486. /**
  487. * jbd2_journal_start() - Obtain a new handle.
  488. * @journal: Journal to start transaction on.
  489. * @nblocks: number of block buffer we might modify
  490. *
  491. * We make sure that the transaction can guarantee at least nblocks of
  492. * modified buffers in the log. We block until the log can guarantee
  493. * that much space. Additionally, if rsv_blocks > 0, we also create another
  494. * handle with rsv_blocks reserved blocks in the journal. This handle is
  495. * stored in h_rsv_handle. It is not attached to any particular transaction
  496. * and thus doesn't block transaction commit. If the caller uses this reserved
  497. * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
  498. * on the parent handle will dispose the reserved one. Reserved handle has to
  499. * be converted to a normal handle using jbd2_journal_start_reserved() before
  500. * it can be used.
  501. *
  502. * Return a pointer to a newly allocated handle, or an ERR_PTR() value
  503. * on failure.
  504. */
  505. handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
  506. {
  507. return jbd2__journal_start(journal, nblocks, 0, 0, GFP_NOFS, 0, 0);
  508. }
  509. EXPORT_SYMBOL(jbd2_journal_start);
  510. static void __jbd2_journal_unreserve_handle(handle_t *handle, transaction_t *t)
  511. {
  512. journal_t *journal = handle->h_journal;
  513. WARN_ON(!handle->h_reserved);
  514. sub_reserved_credits(journal, handle->h_total_credits);
  515. if (t)
  516. atomic_sub(handle->h_total_credits, &t->t_outstanding_credits);
  517. }
  518. void jbd2_journal_free_reserved(handle_t *handle)
  519. {
  520. journal_t *journal = handle->h_journal;
  521. /* Get j_state_lock to pin running transaction if it exists */
  522. read_lock(&journal->j_state_lock);
  523. __jbd2_journal_unreserve_handle(handle, journal->j_running_transaction);
  524. read_unlock(&journal->j_state_lock);
  525. jbd2_free_handle(handle);
  526. }
  527. EXPORT_SYMBOL(jbd2_journal_free_reserved);
  528. /**
  529. * jbd2_journal_start_reserved() - start reserved handle
  530. * @handle: handle to start
  531. * @type: for handle statistics
  532. * @line_no: for handle statistics
  533. *
  534. * Start handle that has been previously reserved with jbd2_journal_reserve().
  535. * This attaches @handle to the running transaction (or creates one if there's
  536. * not transaction running). Unlike jbd2_journal_start() this function cannot
  537. * block on journal commit, checkpointing, or similar stuff. It can block on
  538. * memory allocation or frozen journal though.
  539. *
  540. * Return 0 on success, non-zero on error - handle is freed in that case.
  541. */
  542. int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
  543. unsigned int line_no)
  544. {
  545. journal_t *journal = handle->h_journal;
  546. int ret = -EIO;
  547. if (WARN_ON(!handle->h_reserved)) {
  548. /* Someone passed in normal handle? Just stop it. */
  549. jbd2_journal_stop(handle);
  550. return ret;
  551. }
  552. /*
  553. * Usefulness of mixing of reserved and unreserved handles is
  554. * questionable. So far nobody seems to need it so just error out.
  555. */
  556. if (WARN_ON(current->journal_info)) {
  557. jbd2_journal_free_reserved(handle);
  558. return ret;
  559. }
  560. handle->h_journal = NULL;
  561. /*
  562. * GFP_NOFS is here because callers are likely from writeback or
  563. * similarly constrained call sites
  564. */
  565. ret = start_this_handle(journal, handle, GFP_NOFS);
  566. if (ret < 0) {
  567. handle->h_journal = journal;
  568. jbd2_journal_free_reserved(handle);
  569. return ret;
  570. }
  571. handle->h_type = type;
  572. handle->h_line_no = line_no;
  573. trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
  574. handle->h_transaction->t_tid, type,
  575. line_no, handle->h_total_credits);
  576. return 0;
  577. }
  578. EXPORT_SYMBOL(jbd2_journal_start_reserved);
  579. /**
  580. * jbd2_journal_extend() - extend buffer credits.
  581. * @handle: handle to 'extend'
  582. * @nblocks: nr blocks to try to extend by.
  583. * @revoke_records: number of revoke records to try to extend by.
  584. *
  585. * Some transactions, such as large extends and truncates, can be done
  586. * atomically all at once or in several stages. The operation requests
  587. * a credit for a number of buffer modifications in advance, but can
  588. * extend its credit if it needs more.
  589. *
  590. * jbd2_journal_extend tries to give the running handle more buffer credits.
  591. * It does not guarantee that allocation - this is a best-effort only.
  592. * The calling process MUST be able to deal cleanly with a failure to
  593. * extend here.
  594. *
  595. * Return 0 on success, non-zero on failure.
  596. *
  597. * return code < 0 implies an error
  598. * return code > 0 implies normal transaction-full status.
  599. */
  600. int jbd2_journal_extend(handle_t *handle, int nblocks, int revoke_records)
  601. {
  602. transaction_t *transaction = handle->h_transaction;
  603. journal_t *journal;
  604. int result;
  605. int wanted;
  606. if (is_handle_aborted(handle))
  607. return -EROFS;
  608. journal = transaction->t_journal;
  609. result = 1;
  610. read_lock(&journal->j_state_lock);
  611. /* Don't extend a locked-down transaction! */
  612. if (transaction->t_state != T_RUNNING) {
  613. jbd2_debug(3, "denied handle %p %d blocks: "
  614. "transaction not running\n", handle, nblocks);
  615. goto error_out;
  616. }
  617. nblocks += DIV_ROUND_UP(
  618. handle->h_revoke_credits_requested + revoke_records,
  619. journal->j_revoke_records_per_block) -
  620. DIV_ROUND_UP(
  621. handle->h_revoke_credits_requested,
  622. journal->j_revoke_records_per_block);
  623. wanted = atomic_add_return(nblocks,
  624. &transaction->t_outstanding_credits);
  625. if (wanted > journal->j_max_transaction_buffers) {
  626. jbd2_debug(3, "denied handle %p %d blocks: "
  627. "transaction too large\n", handle, nblocks);
  628. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  629. goto error_out;
  630. }
  631. trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
  632. transaction->t_tid,
  633. handle->h_type, handle->h_line_no,
  634. handle->h_total_credits,
  635. nblocks);
  636. handle->h_total_credits += nblocks;
  637. handle->h_requested_credits += nblocks;
  638. handle->h_revoke_credits += revoke_records;
  639. handle->h_revoke_credits_requested += revoke_records;
  640. result = 0;
  641. jbd2_debug(3, "extended handle %p by %d\n", handle, nblocks);
  642. error_out:
  643. read_unlock(&journal->j_state_lock);
  644. return result;
  645. }
  646. static void stop_this_handle(handle_t *handle)
  647. {
  648. transaction_t *transaction = handle->h_transaction;
  649. journal_t *journal = transaction->t_journal;
  650. int revokes;
  651. J_ASSERT(journal_current_handle() == handle);
  652. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  653. current->journal_info = NULL;
  654. /*
  655. * Subtract necessary revoke descriptor blocks from handle credits. We
  656. * take care to account only for revoke descriptor blocks the
  657. * transaction will really need as large sequences of transactions with
  658. * small numbers of revokes are relatively common.
  659. */
  660. revokes = handle->h_revoke_credits_requested - handle->h_revoke_credits;
  661. if (revokes) {
  662. int t_revokes, revoke_descriptors;
  663. int rr_per_blk = journal->j_revoke_records_per_block;
  664. WARN_ON_ONCE(DIV_ROUND_UP(revokes, rr_per_blk)
  665. > handle->h_total_credits);
  666. t_revokes = atomic_add_return(revokes,
  667. &transaction->t_outstanding_revokes);
  668. revoke_descriptors =
  669. DIV_ROUND_UP(t_revokes, rr_per_blk) -
  670. DIV_ROUND_UP(t_revokes - revokes, rr_per_blk);
  671. handle->h_total_credits -= revoke_descriptors;
  672. }
  673. atomic_sub(handle->h_total_credits,
  674. &transaction->t_outstanding_credits);
  675. if (handle->h_rsv_handle)
  676. __jbd2_journal_unreserve_handle(handle->h_rsv_handle,
  677. transaction);
  678. if (atomic_dec_and_test(&transaction->t_updates))
  679. wake_up(&journal->j_wait_updates);
  680. rwsem_release(&journal->j_trans_commit_map, _THIS_IP_);
  681. /*
  682. * Scope of the GFP_NOFS context is over here and so we can restore the
  683. * original alloc context.
  684. */
  685. memalloc_nofs_restore(handle->saved_alloc_context);
  686. }
  687. /**
  688. * jbd2__journal_restart() - restart a handle .
  689. * @handle: handle to restart
  690. * @nblocks: nr credits requested
  691. * @revoke_records: number of revoke record credits requested
  692. * @gfp_mask: memory allocation flags (for start_this_handle)
  693. *
  694. * Restart a handle for a multi-transaction filesystem
  695. * operation.
  696. *
  697. * If the jbd2_journal_extend() call above fails to grant new buffer credits
  698. * to a running handle, a call to jbd2_journal_restart will commit the
  699. * handle's transaction so far and reattach the handle to a new
  700. * transaction capable of guaranteeing the requested number of
  701. * credits. We preserve reserved handle if there's any attached to the
  702. * passed in handle.
  703. */
  704. int jbd2__journal_restart(handle_t *handle, int nblocks, int revoke_records,
  705. gfp_t gfp_mask)
  706. {
  707. transaction_t *transaction = handle->h_transaction;
  708. journal_t *journal;
  709. tid_t tid;
  710. int need_to_start;
  711. int ret;
  712. /* If we've had an abort of any type, don't even think about
  713. * actually doing the restart! */
  714. if (is_handle_aborted(handle))
  715. return 0;
  716. journal = transaction->t_journal;
  717. tid = transaction->t_tid;
  718. /*
  719. * First unlink the handle from its current transaction, and start the
  720. * commit on that.
  721. */
  722. jbd2_debug(2, "restarting handle %p\n", handle);
  723. stop_this_handle(handle);
  724. handle->h_transaction = NULL;
  725. /*
  726. * TODO: If we use READ_ONCE / WRITE_ONCE for j_commit_request we can
  727. * get rid of pointless j_state_lock traffic like this.
  728. */
  729. read_lock(&journal->j_state_lock);
  730. need_to_start = !tid_geq(journal->j_commit_request, tid);
  731. read_unlock(&journal->j_state_lock);
  732. if (need_to_start)
  733. jbd2_log_start_commit(journal, tid);
  734. handle->h_total_credits = nblocks +
  735. DIV_ROUND_UP(revoke_records,
  736. journal->j_revoke_records_per_block);
  737. handle->h_revoke_credits = revoke_records;
  738. ret = start_this_handle(journal, handle, gfp_mask);
  739. trace_jbd2_handle_restart(journal->j_fs_dev->bd_dev,
  740. ret ? 0 : handle->h_transaction->t_tid,
  741. handle->h_type, handle->h_line_no,
  742. handle->h_total_credits);
  743. return ret;
  744. }
  745. EXPORT_SYMBOL(jbd2__journal_restart);
  746. int jbd2_journal_restart(handle_t *handle, int nblocks)
  747. {
  748. return jbd2__journal_restart(handle, nblocks, 0, GFP_NOFS);
  749. }
  750. EXPORT_SYMBOL(jbd2_journal_restart);
  751. /*
  752. * Waits for any outstanding t_updates to finish.
  753. * This is called with write j_state_lock held.
  754. */
  755. void jbd2_journal_wait_updates(journal_t *journal)
  756. {
  757. DEFINE_WAIT(wait);
  758. while (1) {
  759. /*
  760. * Note that the running transaction can get freed under us if
  761. * this transaction is getting committed in
  762. * jbd2_journal_commit_transaction() ->
  763. * jbd2_journal_free_transaction(). This can only happen when we
  764. * release j_state_lock -> schedule() -> acquire j_state_lock.
  765. * Hence we should everytime retrieve new j_running_transaction
  766. * value (after j_state_lock release acquire cycle), else it may
  767. * lead to use-after-free of old freed transaction.
  768. */
  769. transaction_t *transaction = journal->j_running_transaction;
  770. if (!transaction)
  771. break;
  772. prepare_to_wait(&journal->j_wait_updates, &wait,
  773. TASK_UNINTERRUPTIBLE);
  774. if (!atomic_read(&transaction->t_updates)) {
  775. finish_wait(&journal->j_wait_updates, &wait);
  776. break;
  777. }
  778. write_unlock(&journal->j_state_lock);
  779. schedule();
  780. finish_wait(&journal->j_wait_updates, &wait);
  781. write_lock(&journal->j_state_lock);
  782. }
  783. }
  784. /**
  785. * jbd2_journal_lock_updates () - establish a transaction barrier.
  786. * @journal: Journal to establish a barrier on.
  787. *
  788. * This locks out any further updates from being started, and blocks
  789. * until all existing updates have completed, returning only once the
  790. * journal is in a quiescent state with no updates running.
  791. *
  792. * The journal lock should not be held on entry.
  793. */
  794. void jbd2_journal_lock_updates(journal_t *journal)
  795. {
  796. jbd2_might_wait_for_commit(journal);
  797. write_lock(&journal->j_state_lock);
  798. ++journal->j_barrier_count;
  799. /* Wait until there are no reserved handles */
  800. if (atomic_read(&journal->j_reserved_credits)) {
  801. write_unlock(&journal->j_state_lock);
  802. wait_event(journal->j_wait_reserved,
  803. atomic_read(&journal->j_reserved_credits) == 0);
  804. write_lock(&journal->j_state_lock);
  805. }
  806. /* Wait until there are no running t_updates */
  807. jbd2_journal_wait_updates(journal);
  808. write_unlock(&journal->j_state_lock);
  809. /*
  810. * We have now established a barrier against other normal updates, but
  811. * we also need to barrier against other jbd2_journal_lock_updates() calls
  812. * to make sure that we serialise special journal-locked operations
  813. * too.
  814. */
  815. mutex_lock(&journal->j_barrier);
  816. }
  817. /**
  818. * jbd2_journal_unlock_updates () - release barrier
  819. * @journal: Journal to release the barrier on.
  820. *
  821. * Release a transaction barrier obtained with jbd2_journal_lock_updates().
  822. *
  823. * Should be called without the journal lock held.
  824. */
  825. void jbd2_journal_unlock_updates (journal_t *journal)
  826. {
  827. J_ASSERT(journal->j_barrier_count != 0);
  828. mutex_unlock(&journal->j_barrier);
  829. write_lock(&journal->j_state_lock);
  830. --journal->j_barrier_count;
  831. write_unlock(&journal->j_state_lock);
  832. wake_up_all(&journal->j_wait_transaction_locked);
  833. }
  834. static void warn_dirty_buffer(struct buffer_head *bh)
  835. {
  836. printk(KERN_WARNING
  837. "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). "
  838. "There's a risk of filesystem corruption in case of system "
  839. "crash.\n",
  840. bh->b_bdev, (unsigned long long)bh->b_blocknr);
  841. }
  842. /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */
  843. static void jbd2_freeze_jh_data(struct journal_head *jh)
  844. {
  845. struct page *page;
  846. int offset;
  847. char *source;
  848. struct buffer_head *bh = jh2bh(jh);
  849. J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n");
  850. page = bh->b_page;
  851. offset = offset_in_page(bh->b_data);
  852. source = kmap_atomic(page);
  853. /* Fire data frozen trigger just before we copy the data */
  854. jbd2_buffer_frozen_trigger(jh, source + offset, jh->b_triggers);
  855. memcpy(jh->b_frozen_data, source + offset, bh->b_size);
  856. kunmap_atomic(source);
  857. /*
  858. * Now that the frozen data is saved off, we need to store any matching
  859. * triggers.
  860. */
  861. jh->b_frozen_triggers = jh->b_triggers;
  862. }
  863. /*
  864. * If the buffer is already part of the current transaction, then there
  865. * is nothing we need to do. If it is already part of a prior
  866. * transaction which we are still committing to disk, then we need to
  867. * make sure that we do not overwrite the old copy: we do copy-out to
  868. * preserve the copy going to disk. We also account the buffer against
  869. * the handle's metadata buffer credits (unless the buffer is already
  870. * part of the transaction, that is).
  871. *
  872. */
  873. static int
  874. do_get_write_access(handle_t *handle, struct journal_head *jh,
  875. int force_copy)
  876. {
  877. struct buffer_head *bh;
  878. transaction_t *transaction = handle->h_transaction;
  879. journal_t *journal;
  880. int error;
  881. char *frozen_buffer = NULL;
  882. unsigned long start_lock, time_lock;
  883. journal = transaction->t_journal;
  884. jbd2_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
  885. JBUFFER_TRACE(jh, "entry");
  886. repeat:
  887. bh = jh2bh(jh);
  888. /* @@@ Need to check for errors here at some point. */
  889. start_lock = jiffies;
  890. lock_buffer(bh);
  891. spin_lock(&jh->b_state_lock);
  892. /* If it takes too long to lock the buffer, trace it */
  893. time_lock = jbd2_time_diff(start_lock, jiffies);
  894. if (time_lock > HZ/10)
  895. trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
  896. jiffies_to_msecs(time_lock));
  897. /* We now hold the buffer lock so it is safe to query the buffer
  898. * state. Is the buffer dirty?
  899. *
  900. * If so, there are two possibilities. The buffer may be
  901. * non-journaled, and undergoing a quite legitimate writeback.
  902. * Otherwise, it is journaled, and we don't expect dirty buffers
  903. * in that state (the buffers should be marked JBD_Dirty
  904. * instead.) So either the IO is being done under our own
  905. * control and this is a bug, or it's a third party IO such as
  906. * dump(8) (which may leave the buffer scheduled for read ---
  907. * ie. locked but not dirty) or tune2fs (which may actually have
  908. * the buffer dirtied, ugh.) */
  909. if (buffer_dirty(bh) && jh->b_transaction) {
  910. warn_dirty_buffer(bh);
  911. /*
  912. * We need to clean the dirty flag and we must do it under the
  913. * buffer lock to be sure we don't race with running write-out.
  914. */
  915. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  916. clear_buffer_dirty(bh);
  917. /*
  918. * The buffer is going to be added to BJ_Reserved list now and
  919. * nothing guarantees jbd2_journal_dirty_metadata() will be
  920. * ever called for it. So we need to set jbddirty bit here to
  921. * make sure the buffer is dirtied and written out when the
  922. * journaling machinery is done with it.
  923. */
  924. set_buffer_jbddirty(bh);
  925. }
  926. error = -EROFS;
  927. if (is_handle_aborted(handle)) {
  928. spin_unlock(&jh->b_state_lock);
  929. unlock_buffer(bh);
  930. goto out;
  931. }
  932. error = 0;
  933. /*
  934. * The buffer is already part of this transaction if b_transaction or
  935. * b_next_transaction points to it
  936. */
  937. if (jh->b_transaction == transaction ||
  938. jh->b_next_transaction == transaction) {
  939. unlock_buffer(bh);
  940. goto done;
  941. }
  942. /*
  943. * this is the first time this transaction is touching this buffer,
  944. * reset the modified flag
  945. */
  946. jh->b_modified = 0;
  947. /*
  948. * If the buffer is not journaled right now, we need to make sure it
  949. * doesn't get written to disk before the caller actually commits the
  950. * new data
  951. */
  952. if (!jh->b_transaction) {
  953. JBUFFER_TRACE(jh, "no transaction");
  954. J_ASSERT_JH(jh, !jh->b_next_transaction);
  955. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  956. /*
  957. * Make sure all stores to jh (b_modified, b_frozen_data) are
  958. * visible before attaching it to the running transaction.
  959. * Paired with barrier in jbd2_write_access_granted()
  960. */
  961. smp_wmb();
  962. spin_lock(&journal->j_list_lock);
  963. if (test_clear_buffer_dirty(bh)) {
  964. /*
  965. * Execute buffer dirty clearing and jh->b_transaction
  966. * assignment under journal->j_list_lock locked to
  967. * prevent bh being removed from checkpoint list if
  968. * the buffer is in an intermediate state (not dirty
  969. * and jh->b_transaction is NULL).
  970. */
  971. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  972. set_buffer_jbddirty(bh);
  973. }
  974. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  975. spin_unlock(&journal->j_list_lock);
  976. unlock_buffer(bh);
  977. goto done;
  978. }
  979. unlock_buffer(bh);
  980. /*
  981. * If there is already a copy-out version of this buffer, then we don't
  982. * need to make another one
  983. */
  984. if (jh->b_frozen_data) {
  985. JBUFFER_TRACE(jh, "has frozen data");
  986. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  987. goto attach_next;
  988. }
  989. JBUFFER_TRACE(jh, "owned by older transaction");
  990. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  991. J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction);
  992. /*
  993. * There is one case we have to be very careful about. If the
  994. * committing transaction is currently writing this buffer out to disk
  995. * and has NOT made a copy-out, then we cannot modify the buffer
  996. * contents at all right now. The essence of copy-out is that it is
  997. * the extra copy, not the primary copy, which gets journaled. If the
  998. * primary copy is already going to disk then we cannot do copy-out
  999. * here.
  1000. */
  1001. if (buffer_shadow(bh)) {
  1002. JBUFFER_TRACE(jh, "on shadow: sleep");
  1003. spin_unlock(&jh->b_state_lock);
  1004. wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE);
  1005. goto repeat;
  1006. }
  1007. /*
  1008. * Only do the copy if the currently-owning transaction still needs it.
  1009. * If buffer isn't on BJ_Metadata list, the committing transaction is
  1010. * past that stage (here we use the fact that BH_Shadow is set under
  1011. * bh_state lock together with refiling to BJ_Shadow list and at this
  1012. * point we know the buffer doesn't have BH_Shadow set).
  1013. *
  1014. * Subtle point, though: if this is a get_undo_access, then we will be
  1015. * relying on the frozen_data to contain the new value of the
  1016. * committed_data record after the transaction, so we HAVE to force the
  1017. * frozen_data copy in that case.
  1018. */
  1019. if (jh->b_jlist == BJ_Metadata || force_copy) {
  1020. JBUFFER_TRACE(jh, "generate frozen data");
  1021. if (!frozen_buffer) {
  1022. JBUFFER_TRACE(jh, "allocate memory for buffer");
  1023. spin_unlock(&jh->b_state_lock);
  1024. frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size,
  1025. GFP_NOFS | __GFP_NOFAIL);
  1026. goto repeat;
  1027. }
  1028. jh->b_frozen_data = frozen_buffer;
  1029. frozen_buffer = NULL;
  1030. jbd2_freeze_jh_data(jh);
  1031. }
  1032. attach_next:
  1033. /*
  1034. * Make sure all stores to jh (b_modified, b_frozen_data) are visible
  1035. * before attaching it to the running transaction. Paired with barrier
  1036. * in jbd2_write_access_granted()
  1037. */
  1038. smp_wmb();
  1039. jh->b_next_transaction = transaction;
  1040. done:
  1041. spin_unlock(&jh->b_state_lock);
  1042. /*
  1043. * If we are about to journal a buffer, then any revoke pending on it is
  1044. * no longer valid
  1045. */
  1046. jbd2_journal_cancel_revoke(handle, jh);
  1047. out:
  1048. if (unlikely(frozen_buffer)) /* It's usually NULL */
  1049. jbd2_free(frozen_buffer, bh->b_size);
  1050. JBUFFER_TRACE(jh, "exit");
  1051. return error;
  1052. }
  1053. /* Fast check whether buffer is already attached to the required transaction */
  1054. static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh,
  1055. bool undo)
  1056. {
  1057. struct journal_head *jh;
  1058. bool ret = false;
  1059. /* Dirty buffers require special handling... */
  1060. if (buffer_dirty(bh))
  1061. return false;
  1062. /*
  1063. * RCU protects us from dereferencing freed pages. So the checks we do
  1064. * are guaranteed not to oops. However the jh slab object can get freed
  1065. * & reallocated while we work with it. So we have to be careful. When
  1066. * we see jh attached to the running transaction, we know it must stay
  1067. * so until the transaction is committed. Thus jh won't be freed and
  1068. * will be attached to the same bh while we run. However it can
  1069. * happen jh gets freed, reallocated, and attached to the transaction
  1070. * just after we get pointer to it from bh. So we have to be careful
  1071. * and recheck jh still belongs to our bh before we return success.
  1072. */
  1073. rcu_read_lock();
  1074. if (!buffer_jbd(bh))
  1075. goto out;
  1076. /* This should be bh2jh() but that doesn't work with inline functions */
  1077. jh = READ_ONCE(bh->b_private);
  1078. if (!jh)
  1079. goto out;
  1080. /* For undo access buffer must have data copied */
  1081. if (undo && !jh->b_committed_data)
  1082. goto out;
  1083. if (READ_ONCE(jh->b_transaction) != handle->h_transaction &&
  1084. READ_ONCE(jh->b_next_transaction) != handle->h_transaction)
  1085. goto out;
  1086. /*
  1087. * There are two reasons for the barrier here:
  1088. * 1) Make sure to fetch b_bh after we did previous checks so that we
  1089. * detect when jh went through free, realloc, attach to transaction
  1090. * while we were checking. Paired with implicit barrier in that path.
  1091. * 2) So that access to bh done after jbd2_write_access_granted()
  1092. * doesn't get reordered and see inconsistent state of concurrent
  1093. * do_get_write_access().
  1094. */
  1095. smp_mb();
  1096. if (unlikely(jh->b_bh != bh))
  1097. goto out;
  1098. ret = true;
  1099. out:
  1100. rcu_read_unlock();
  1101. return ret;
  1102. }
  1103. /**
  1104. * jbd2_journal_get_write_access() - notify intent to modify a buffer
  1105. * for metadata (not data) update.
  1106. * @handle: transaction to add buffer modifications to
  1107. * @bh: bh to be used for metadata writes
  1108. *
  1109. * Returns: error code or 0 on success.
  1110. *
  1111. * In full data journalling mode the buffer may be of type BJ_AsyncData,
  1112. * because we're ``write()ing`` a buffer which is also part of a shared mapping.
  1113. */
  1114. int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
  1115. {
  1116. struct journal_head *jh;
  1117. int rc;
  1118. if (is_handle_aborted(handle))
  1119. return -EROFS;
  1120. if (jbd2_write_access_granted(handle, bh, false))
  1121. return 0;
  1122. jh = jbd2_journal_add_journal_head(bh);
  1123. /* We do not want to get caught playing with fields which the
  1124. * log thread also manipulates. Make sure that the buffer
  1125. * completes any outstanding IO before proceeding. */
  1126. rc = do_get_write_access(handle, jh, 0);
  1127. jbd2_journal_put_journal_head(jh);
  1128. return rc;
  1129. }
  1130. /*
  1131. * When the user wants to journal a newly created buffer_head
  1132. * (ie. getblk() returned a new buffer and we are going to populate it
  1133. * manually rather than reading off disk), then we need to keep the
  1134. * buffer_head locked until it has been completely filled with new
  1135. * data. In this case, we should be able to make the assertion that
  1136. * the bh is not already part of an existing transaction.
  1137. *
  1138. * The buffer should already be locked by the caller by this point.
  1139. * There is no lock ranking violation: it was a newly created,
  1140. * unlocked buffer beforehand. */
  1141. /**
  1142. * jbd2_journal_get_create_access () - notify intent to use newly created bh
  1143. * @handle: transaction to new buffer to
  1144. * @bh: new buffer.
  1145. *
  1146. * Call this if you create a new bh.
  1147. */
  1148. int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
  1149. {
  1150. transaction_t *transaction = handle->h_transaction;
  1151. journal_t *journal;
  1152. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  1153. int err;
  1154. jbd2_debug(5, "journal_head %p\n", jh);
  1155. err = -EROFS;
  1156. if (is_handle_aborted(handle))
  1157. goto out;
  1158. journal = transaction->t_journal;
  1159. err = 0;
  1160. JBUFFER_TRACE(jh, "entry");
  1161. /*
  1162. * The buffer may already belong to this transaction due to pre-zeroing
  1163. * in the filesystem's new_block code. It may also be on the previous,
  1164. * committing transaction's lists, but it HAS to be in Forget state in
  1165. * that case: the transaction must have deleted the buffer for it to be
  1166. * reused here.
  1167. */
  1168. spin_lock(&jh->b_state_lock);
  1169. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  1170. jh->b_transaction == NULL ||
  1171. (jh->b_transaction == journal->j_committing_transaction &&
  1172. jh->b_jlist == BJ_Forget)));
  1173. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  1174. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  1175. if (jh->b_transaction == NULL) {
  1176. /*
  1177. * Previous jbd2_journal_forget() could have left the buffer
  1178. * with jbddirty bit set because it was being committed. When
  1179. * the commit finished, we've filed the buffer for
  1180. * checkpointing and marked it dirty. Now we are reallocating
  1181. * the buffer so the transaction freeing it must have
  1182. * committed and so it's safe to clear the dirty bit.
  1183. */
  1184. clear_buffer_dirty(jh2bh(jh));
  1185. /* first access by this transaction */
  1186. jh->b_modified = 0;
  1187. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  1188. spin_lock(&journal->j_list_lock);
  1189. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  1190. spin_unlock(&journal->j_list_lock);
  1191. } else if (jh->b_transaction == journal->j_committing_transaction) {
  1192. /* first access by this transaction */
  1193. jh->b_modified = 0;
  1194. JBUFFER_TRACE(jh, "set next transaction");
  1195. spin_lock(&journal->j_list_lock);
  1196. jh->b_next_transaction = transaction;
  1197. spin_unlock(&journal->j_list_lock);
  1198. }
  1199. spin_unlock(&jh->b_state_lock);
  1200. /*
  1201. * akpm: I added this. ext3_alloc_branch can pick up new indirect
  1202. * blocks which contain freed but then revoked metadata. We need
  1203. * to cancel the revoke in case we end up freeing it yet again
  1204. * and the reallocating as data - this would cause a second revoke,
  1205. * which hits an assertion error.
  1206. */
  1207. JBUFFER_TRACE(jh, "cancelling revoke");
  1208. jbd2_journal_cancel_revoke(handle, jh);
  1209. out:
  1210. jbd2_journal_put_journal_head(jh);
  1211. return err;
  1212. }
  1213. /**
  1214. * jbd2_journal_get_undo_access() - Notify intent to modify metadata with
  1215. * non-rewindable consequences
  1216. * @handle: transaction
  1217. * @bh: buffer to undo
  1218. *
  1219. * Sometimes there is a need to distinguish between metadata which has
  1220. * been committed to disk and that which has not. The ext3fs code uses
  1221. * this for freeing and allocating space, we have to make sure that we
  1222. * do not reuse freed space until the deallocation has been committed,
  1223. * since if we overwrote that space we would make the delete
  1224. * un-rewindable in case of a crash.
  1225. *
  1226. * To deal with that, jbd2_journal_get_undo_access requests write access to a
  1227. * buffer for parts of non-rewindable operations such as delete
  1228. * operations on the bitmaps. The journaling code must keep a copy of
  1229. * the buffer's contents prior to the undo_access call until such time
  1230. * as we know that the buffer has definitely been committed to disk.
  1231. *
  1232. * We never need to know which transaction the committed data is part
  1233. * of, buffers touched here are guaranteed to be dirtied later and so
  1234. * will be committed to a new transaction in due course, at which point
  1235. * we can discard the old committed data pointer.
  1236. *
  1237. * Returns error number or 0 on success.
  1238. */
  1239. int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
  1240. {
  1241. int err;
  1242. struct journal_head *jh;
  1243. char *committed_data = NULL;
  1244. if (is_handle_aborted(handle))
  1245. return -EROFS;
  1246. if (jbd2_write_access_granted(handle, bh, true))
  1247. return 0;
  1248. jh = jbd2_journal_add_journal_head(bh);
  1249. JBUFFER_TRACE(jh, "entry");
  1250. /*
  1251. * Do this first --- it can drop the journal lock, so we want to
  1252. * make sure that obtaining the committed_data is done
  1253. * atomically wrt. completion of any outstanding commits.
  1254. */
  1255. err = do_get_write_access(handle, jh, 1);
  1256. if (err)
  1257. goto out;
  1258. repeat:
  1259. if (!jh->b_committed_data)
  1260. committed_data = jbd2_alloc(jh2bh(jh)->b_size,
  1261. GFP_NOFS|__GFP_NOFAIL);
  1262. spin_lock(&jh->b_state_lock);
  1263. if (!jh->b_committed_data) {
  1264. /* Copy out the current buffer contents into the
  1265. * preserved, committed copy. */
  1266. JBUFFER_TRACE(jh, "generate b_committed data");
  1267. if (!committed_data) {
  1268. spin_unlock(&jh->b_state_lock);
  1269. goto repeat;
  1270. }
  1271. jh->b_committed_data = committed_data;
  1272. committed_data = NULL;
  1273. memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
  1274. }
  1275. spin_unlock(&jh->b_state_lock);
  1276. out:
  1277. jbd2_journal_put_journal_head(jh);
  1278. if (unlikely(committed_data))
  1279. jbd2_free(committed_data, bh->b_size);
  1280. return err;
  1281. }
  1282. /**
  1283. * jbd2_journal_set_triggers() - Add triggers for commit writeout
  1284. * @bh: buffer to trigger on
  1285. * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
  1286. *
  1287. * Set any triggers on this journal_head. This is always safe, because
  1288. * triggers for a committing buffer will be saved off, and triggers for
  1289. * a running transaction will match the buffer in that transaction.
  1290. *
  1291. * Call with NULL to clear the triggers.
  1292. */
  1293. void jbd2_journal_set_triggers(struct buffer_head *bh,
  1294. struct jbd2_buffer_trigger_type *type)
  1295. {
  1296. struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
  1297. if (WARN_ON_ONCE(!jh))
  1298. return;
  1299. jh->b_triggers = type;
  1300. jbd2_journal_put_journal_head(jh);
  1301. }
  1302. void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
  1303. struct jbd2_buffer_trigger_type *triggers)
  1304. {
  1305. struct buffer_head *bh = jh2bh(jh);
  1306. if (!triggers || !triggers->t_frozen)
  1307. return;
  1308. triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
  1309. }
  1310. void jbd2_buffer_abort_trigger(struct journal_head *jh,
  1311. struct jbd2_buffer_trigger_type *triggers)
  1312. {
  1313. if (!triggers || !triggers->t_abort)
  1314. return;
  1315. triggers->t_abort(triggers, jh2bh(jh));
  1316. }
  1317. /**
  1318. * jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
  1319. * @handle: transaction to add buffer to.
  1320. * @bh: buffer to mark
  1321. *
  1322. * mark dirty metadata which needs to be journaled as part of the current
  1323. * transaction.
  1324. *
  1325. * The buffer must have previously had jbd2_journal_get_write_access()
  1326. * called so that it has a valid journal_head attached to the buffer
  1327. * head.
  1328. *
  1329. * The buffer is placed on the transaction's metadata list and is marked
  1330. * as belonging to the transaction.
  1331. *
  1332. * Returns error number or 0 on success.
  1333. *
  1334. * Special care needs to be taken if the buffer already belongs to the
  1335. * current committing transaction (in which case we should have frozen
  1336. * data present for that commit). In that case, we don't relink the
  1337. * buffer: that only gets done when the old transaction finally
  1338. * completes its commit.
  1339. */
  1340. int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
  1341. {
  1342. transaction_t *transaction = handle->h_transaction;
  1343. journal_t *journal;
  1344. struct journal_head *jh;
  1345. int ret = 0;
  1346. if (!buffer_jbd(bh))
  1347. return -EUCLEAN;
  1348. /*
  1349. * We don't grab jh reference here since the buffer must be part
  1350. * of the running transaction.
  1351. */
  1352. jh = bh2jh(bh);
  1353. jbd2_debug(5, "journal_head %p\n", jh);
  1354. JBUFFER_TRACE(jh, "entry");
  1355. /*
  1356. * This and the following assertions are unreliable since we may see jh
  1357. * in inconsistent state unless we grab bh_state lock. But this is
  1358. * crucial to catch bugs so let's do a reliable check until the
  1359. * lockless handling is fully proven.
  1360. */
  1361. if (data_race(jh->b_transaction != transaction &&
  1362. jh->b_next_transaction != transaction)) {
  1363. spin_lock(&jh->b_state_lock);
  1364. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  1365. jh->b_next_transaction == transaction);
  1366. spin_unlock(&jh->b_state_lock);
  1367. }
  1368. if (jh->b_modified == 1) {
  1369. /* If it's in our transaction it must be in BJ_Metadata list. */
  1370. if (data_race(jh->b_transaction == transaction &&
  1371. jh->b_jlist != BJ_Metadata)) {
  1372. spin_lock(&jh->b_state_lock);
  1373. if (jh->b_transaction == transaction &&
  1374. jh->b_jlist != BJ_Metadata)
  1375. pr_err("JBD2: assertion failure: h_type=%u "
  1376. "h_line_no=%u block_no=%llu jlist=%u\n",
  1377. handle->h_type, handle->h_line_no,
  1378. (unsigned long long) bh->b_blocknr,
  1379. jh->b_jlist);
  1380. J_ASSERT_JH(jh, jh->b_transaction != transaction ||
  1381. jh->b_jlist == BJ_Metadata);
  1382. spin_unlock(&jh->b_state_lock);
  1383. }
  1384. goto out;
  1385. }
  1386. journal = transaction->t_journal;
  1387. spin_lock(&jh->b_state_lock);
  1388. if (is_handle_aborted(handle)) {
  1389. /*
  1390. * Check journal aborting with @jh->b_state_lock locked,
  1391. * since 'jh->b_transaction' could be replaced with
  1392. * 'jh->b_next_transaction' during old transaction
  1393. * committing if journal aborted, which may fail
  1394. * assertion on 'jh->b_frozen_data == NULL'.
  1395. */
  1396. ret = -EROFS;
  1397. goto out_unlock_bh;
  1398. }
  1399. if (jh->b_modified == 0) {
  1400. /*
  1401. * This buffer's got modified and becoming part
  1402. * of the transaction. This needs to be done
  1403. * once a transaction -bzzz
  1404. */
  1405. if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) {
  1406. ret = -ENOSPC;
  1407. goto out_unlock_bh;
  1408. }
  1409. jh->b_modified = 1;
  1410. handle->h_total_credits--;
  1411. }
  1412. /*
  1413. * fastpath, to avoid expensive locking. If this buffer is already
  1414. * on the running transaction's metadata list there is nothing to do.
  1415. * Nobody can take it off again because there is a handle open.
  1416. * I _think_ we're OK here with SMP barriers - a mistaken decision will
  1417. * result in this test being false, so we go in and take the locks.
  1418. */
  1419. if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
  1420. JBUFFER_TRACE(jh, "fastpath");
  1421. if (unlikely(jh->b_transaction !=
  1422. journal->j_running_transaction)) {
  1423. printk(KERN_ERR "JBD2: %s: "
  1424. "jh->b_transaction (%llu, %p, %u) != "
  1425. "journal->j_running_transaction (%p, %u)\n",
  1426. journal->j_devname,
  1427. (unsigned long long) bh->b_blocknr,
  1428. jh->b_transaction,
  1429. jh->b_transaction ? jh->b_transaction->t_tid : 0,
  1430. journal->j_running_transaction,
  1431. journal->j_running_transaction ?
  1432. journal->j_running_transaction->t_tid : 0);
  1433. ret = -EINVAL;
  1434. }
  1435. goto out_unlock_bh;
  1436. }
  1437. set_buffer_jbddirty(bh);
  1438. /*
  1439. * Metadata already on the current transaction list doesn't
  1440. * need to be filed. Metadata on another transaction's list must
  1441. * be committing, and will be refiled once the commit completes:
  1442. * leave it alone for now.
  1443. */
  1444. if (jh->b_transaction != transaction) {
  1445. JBUFFER_TRACE(jh, "already on other transaction");
  1446. if (unlikely(((jh->b_transaction !=
  1447. journal->j_committing_transaction)) ||
  1448. (jh->b_next_transaction != transaction))) {
  1449. printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
  1450. "bad jh for block %llu: "
  1451. "transaction (%p, %u), "
  1452. "jh->b_transaction (%p, %u), "
  1453. "jh->b_next_transaction (%p, %u), jlist %u\n",
  1454. journal->j_devname,
  1455. (unsigned long long) bh->b_blocknr,
  1456. transaction, transaction->t_tid,
  1457. jh->b_transaction,
  1458. jh->b_transaction ?
  1459. jh->b_transaction->t_tid : 0,
  1460. jh->b_next_transaction,
  1461. jh->b_next_transaction ?
  1462. jh->b_next_transaction->t_tid : 0,
  1463. jh->b_jlist);
  1464. WARN_ON(1);
  1465. ret = -EINVAL;
  1466. }
  1467. /* And this case is illegal: we can't reuse another
  1468. * transaction's data buffer, ever. */
  1469. goto out_unlock_bh;
  1470. }
  1471. /* That test should have eliminated the following case: */
  1472. J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
  1473. JBUFFER_TRACE(jh, "file as BJ_Metadata");
  1474. spin_lock(&journal->j_list_lock);
  1475. __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
  1476. spin_unlock(&journal->j_list_lock);
  1477. out_unlock_bh:
  1478. spin_unlock(&jh->b_state_lock);
  1479. out:
  1480. JBUFFER_TRACE(jh, "exit");
  1481. return ret;
  1482. }
  1483. /**
  1484. * jbd2_journal_forget() - bforget() for potentially-journaled buffers.
  1485. * @handle: transaction handle
  1486. * @bh: bh to 'forget'
  1487. *
  1488. * We can only do the bforget if there are no commits pending against the
  1489. * buffer. If the buffer is dirty in the current running transaction we
  1490. * can safely unlink it.
  1491. *
  1492. * bh may not be a journalled buffer at all - it may be a non-JBD
  1493. * buffer which came off the hashtable. Check for this.
  1494. *
  1495. * Decrements bh->b_count by one.
  1496. *
  1497. * Allow this call even if the handle has aborted --- it may be part of
  1498. * the caller's cleanup after an abort.
  1499. */
  1500. int jbd2_journal_forget(handle_t *handle, struct buffer_head *bh)
  1501. {
  1502. transaction_t *transaction = handle->h_transaction;
  1503. journal_t *journal;
  1504. struct journal_head *jh;
  1505. int drop_reserve = 0;
  1506. int err = 0;
  1507. int was_modified = 0;
  1508. if (is_handle_aborted(handle))
  1509. return -EROFS;
  1510. journal = transaction->t_journal;
  1511. BUFFER_TRACE(bh, "entry");
  1512. jh = jbd2_journal_grab_journal_head(bh);
  1513. if (!jh) {
  1514. __bforget(bh);
  1515. return 0;
  1516. }
  1517. spin_lock(&jh->b_state_lock);
  1518. /* Critical error: attempting to delete a bitmap buffer, maybe?
  1519. * Don't do any jbd operations, and return an error. */
  1520. if (!J_EXPECT_JH(jh, !jh->b_committed_data,
  1521. "inconsistent data on disk")) {
  1522. err = -EIO;
  1523. goto drop;
  1524. }
  1525. /* keep track of whether or not this transaction modified us */
  1526. was_modified = jh->b_modified;
  1527. /*
  1528. * The buffer's going from the transaction, we must drop
  1529. * all references -bzzz
  1530. */
  1531. jh->b_modified = 0;
  1532. if (jh->b_transaction == transaction) {
  1533. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1534. /* If we are forgetting a buffer which is already part
  1535. * of this transaction, then we can just drop it from
  1536. * the transaction immediately. */
  1537. clear_buffer_dirty(bh);
  1538. clear_buffer_jbddirty(bh);
  1539. JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
  1540. /*
  1541. * we only want to drop a reference if this transaction
  1542. * modified the buffer
  1543. */
  1544. if (was_modified)
  1545. drop_reserve = 1;
  1546. /*
  1547. * We are no longer going to journal this buffer.
  1548. * However, the commit of this transaction is still
  1549. * important to the buffer: the delete that we are now
  1550. * processing might obsolete an old log entry, so by
  1551. * committing, we can satisfy the buffer's checkpoint.
  1552. *
  1553. * So, if we have a checkpoint on the buffer, we should
  1554. * now refile the buffer on our BJ_Forget list so that
  1555. * we know to remove the checkpoint after we commit.
  1556. */
  1557. spin_lock(&journal->j_list_lock);
  1558. if (jh->b_cp_transaction) {
  1559. __jbd2_journal_temp_unlink_buffer(jh);
  1560. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1561. } else {
  1562. __jbd2_journal_unfile_buffer(jh);
  1563. jbd2_journal_put_journal_head(jh);
  1564. }
  1565. spin_unlock(&journal->j_list_lock);
  1566. } else if (jh->b_transaction) {
  1567. J_ASSERT_JH(jh, (jh->b_transaction ==
  1568. journal->j_committing_transaction));
  1569. /* However, if the buffer is still owned by a prior
  1570. * (committing) transaction, we can't drop it yet... */
  1571. JBUFFER_TRACE(jh, "belongs to older transaction");
  1572. /* ... but we CAN drop it from the new transaction through
  1573. * marking the buffer as freed and set j_next_transaction to
  1574. * the new transaction, so that not only the commit code
  1575. * knows it should clear dirty bits when it is done with the
  1576. * buffer, but also the buffer can be checkpointed only
  1577. * after the new transaction commits. */
  1578. set_buffer_freed(bh);
  1579. if (!jh->b_next_transaction) {
  1580. spin_lock(&journal->j_list_lock);
  1581. jh->b_next_transaction = transaction;
  1582. spin_unlock(&journal->j_list_lock);
  1583. } else {
  1584. J_ASSERT(jh->b_next_transaction == transaction);
  1585. /*
  1586. * only drop a reference if this transaction modified
  1587. * the buffer
  1588. */
  1589. if (was_modified)
  1590. drop_reserve = 1;
  1591. }
  1592. } else {
  1593. /*
  1594. * Finally, if the buffer is not belongs to any
  1595. * transaction, we can just drop it now if it has no
  1596. * checkpoint.
  1597. */
  1598. spin_lock(&journal->j_list_lock);
  1599. if (!jh->b_cp_transaction) {
  1600. JBUFFER_TRACE(jh, "belongs to none transaction");
  1601. spin_unlock(&journal->j_list_lock);
  1602. goto drop;
  1603. }
  1604. /*
  1605. * Otherwise, if the buffer has been written to disk,
  1606. * it is safe to remove the checkpoint and drop it.
  1607. */
  1608. if (jbd2_journal_try_remove_checkpoint(jh) >= 0) {
  1609. spin_unlock(&journal->j_list_lock);
  1610. goto drop;
  1611. }
  1612. /*
  1613. * The buffer is still not written to disk, we should
  1614. * attach this buffer to current transaction so that the
  1615. * buffer can be checkpointed only after the current
  1616. * transaction commits.
  1617. */
  1618. clear_buffer_dirty(bh);
  1619. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1620. spin_unlock(&journal->j_list_lock);
  1621. }
  1622. drop:
  1623. __brelse(bh);
  1624. spin_unlock(&jh->b_state_lock);
  1625. jbd2_journal_put_journal_head(jh);
  1626. if (drop_reserve) {
  1627. /* no need to reserve log space for this block -bzzz */
  1628. handle->h_total_credits++;
  1629. }
  1630. return err;
  1631. }
  1632. /**
  1633. * jbd2_journal_stop() - complete a transaction
  1634. * @handle: transaction to complete.
  1635. *
  1636. * All done for a particular handle.
  1637. *
  1638. * There is not much action needed here. We just return any remaining
  1639. * buffer credits to the transaction and remove the handle. The only
  1640. * complication is that we need to start a commit operation if the
  1641. * filesystem is marked for synchronous update.
  1642. *
  1643. * jbd2_journal_stop itself will not usually return an error, but it may
  1644. * do so in unusual circumstances. In particular, expect it to
  1645. * return -EIO if a jbd2_journal_abort has been executed since the
  1646. * transaction began.
  1647. */
  1648. int jbd2_journal_stop(handle_t *handle)
  1649. {
  1650. transaction_t *transaction = handle->h_transaction;
  1651. journal_t *journal;
  1652. int err = 0, wait_for_commit = 0;
  1653. tid_t tid;
  1654. pid_t pid;
  1655. if (--handle->h_ref > 0) {
  1656. jbd2_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1657. handle->h_ref);
  1658. if (is_handle_aborted(handle))
  1659. return -EIO;
  1660. return 0;
  1661. }
  1662. if (!transaction) {
  1663. /*
  1664. * Handle is already detached from the transaction so there is
  1665. * nothing to do other than free the handle.
  1666. */
  1667. memalloc_nofs_restore(handle->saved_alloc_context);
  1668. goto free_and_exit;
  1669. }
  1670. journal = transaction->t_journal;
  1671. tid = transaction->t_tid;
  1672. if (is_handle_aborted(handle))
  1673. err = -EIO;
  1674. jbd2_debug(4, "Handle %p going down\n", handle);
  1675. trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
  1676. tid, handle->h_type, handle->h_line_no,
  1677. jiffies - handle->h_start_jiffies,
  1678. handle->h_sync, handle->h_requested_credits,
  1679. (handle->h_requested_credits -
  1680. handle->h_total_credits));
  1681. /*
  1682. * Implement synchronous transaction batching. If the handle
  1683. * was synchronous, don't force a commit immediately. Let's
  1684. * yield and let another thread piggyback onto this
  1685. * transaction. Keep doing that while new threads continue to
  1686. * arrive. It doesn't cost much - we're about to run a commit
  1687. * and sleep on IO anyway. Speeds up many-threaded, many-dir
  1688. * operations by 30x or more...
  1689. *
  1690. * We try and optimize the sleep time against what the
  1691. * underlying disk can do, instead of having a static sleep
  1692. * time. This is useful for the case where our storage is so
  1693. * fast that it is more optimal to go ahead and force a flush
  1694. * and wait for the transaction to be committed than it is to
  1695. * wait for an arbitrary amount of time for new writers to
  1696. * join the transaction. We achieve this by measuring how
  1697. * long it takes to commit a transaction, and compare it with
  1698. * how long this transaction has been running, and if run time
  1699. * < commit time then we sleep for the delta and commit. This
  1700. * greatly helps super fast disks that would see slowdowns as
  1701. * more threads started doing fsyncs.
  1702. *
  1703. * But don't do this if this process was the most recent one
  1704. * to perform a synchronous write. We do this to detect the
  1705. * case where a single process is doing a stream of sync
  1706. * writes. No point in waiting for joiners in that case.
  1707. *
  1708. * Setting max_batch_time to 0 disables this completely.
  1709. */
  1710. pid = current->pid;
  1711. if (handle->h_sync && journal->j_last_sync_writer != pid &&
  1712. journal->j_max_batch_time) {
  1713. u64 commit_time, trans_time;
  1714. journal->j_last_sync_writer = pid;
  1715. read_lock(&journal->j_state_lock);
  1716. commit_time = journal->j_average_commit_time;
  1717. read_unlock(&journal->j_state_lock);
  1718. trans_time = ktime_to_ns(ktime_sub(ktime_get(),
  1719. transaction->t_start_time));
  1720. commit_time = max_t(u64, commit_time,
  1721. 1000*journal->j_min_batch_time);
  1722. commit_time = min_t(u64, commit_time,
  1723. 1000*journal->j_max_batch_time);
  1724. if (trans_time < commit_time) {
  1725. ktime_t expires = ktime_add_ns(ktime_get(),
  1726. commit_time);
  1727. set_current_state(TASK_UNINTERRUPTIBLE);
  1728. schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
  1729. }
  1730. }
  1731. if (handle->h_sync)
  1732. transaction->t_synchronous_commit = 1;
  1733. /*
  1734. * If the handle is marked SYNC, we need to set another commit
  1735. * going! We also want to force a commit if the transaction is too
  1736. * old now.
  1737. */
  1738. if (handle->h_sync ||
  1739. time_after_eq(jiffies, transaction->t_expires)) {
  1740. /* Do this even for aborted journals: an abort still
  1741. * completes the commit thread, it just doesn't write
  1742. * anything to disk. */
  1743. jbd2_debug(2, "transaction too old, requesting commit for "
  1744. "handle %p\n", handle);
  1745. /* This is non-blocking */
  1746. jbd2_log_start_commit(journal, tid);
  1747. /*
  1748. * Special case: JBD2_SYNC synchronous updates require us
  1749. * to wait for the commit to complete.
  1750. */
  1751. if (handle->h_sync && !(current->flags & PF_MEMALLOC))
  1752. wait_for_commit = 1;
  1753. }
  1754. /*
  1755. * Once stop_this_handle() drops t_updates, the transaction could start
  1756. * committing on us and eventually disappear. So we must not
  1757. * dereference transaction pointer again after calling
  1758. * stop_this_handle().
  1759. */
  1760. stop_this_handle(handle);
  1761. if (wait_for_commit)
  1762. err = jbd2_log_wait_commit(journal, tid);
  1763. free_and_exit:
  1764. if (handle->h_rsv_handle)
  1765. jbd2_free_handle(handle->h_rsv_handle);
  1766. jbd2_free_handle(handle);
  1767. return err;
  1768. }
  1769. /*
  1770. *
  1771. * List management code snippets: various functions for manipulating the
  1772. * transaction buffer lists.
  1773. *
  1774. */
  1775. /*
  1776. * Append a buffer to a transaction list, given the transaction's list head
  1777. * pointer.
  1778. *
  1779. * j_list_lock is held.
  1780. *
  1781. * jh->b_state_lock is held.
  1782. */
  1783. static inline void
  1784. __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
  1785. {
  1786. if (!*list) {
  1787. jh->b_tnext = jh->b_tprev = jh;
  1788. *list = jh;
  1789. } else {
  1790. /* Insert at the tail of the list to preserve order */
  1791. struct journal_head *first = *list, *last = first->b_tprev;
  1792. jh->b_tprev = last;
  1793. jh->b_tnext = first;
  1794. last->b_tnext = first->b_tprev = jh;
  1795. }
  1796. }
  1797. /*
  1798. * Remove a buffer from a transaction list, given the transaction's list
  1799. * head pointer.
  1800. *
  1801. * Called with j_list_lock held, and the journal may not be locked.
  1802. *
  1803. * jh->b_state_lock is held.
  1804. */
  1805. static inline void
  1806. __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
  1807. {
  1808. if (*list == jh) {
  1809. *list = jh->b_tnext;
  1810. if (*list == jh)
  1811. *list = NULL;
  1812. }
  1813. jh->b_tprev->b_tnext = jh->b_tnext;
  1814. jh->b_tnext->b_tprev = jh->b_tprev;
  1815. }
  1816. /*
  1817. * Remove a buffer from the appropriate transaction list.
  1818. *
  1819. * Note that this function can *change* the value of
  1820. * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
  1821. * t_reserved_list. If the caller is holding onto a copy of one of these
  1822. * pointers, it could go bad. Generally the caller needs to re-read the
  1823. * pointer from the transaction_t.
  1824. *
  1825. * Called under j_list_lock.
  1826. */
  1827. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
  1828. {
  1829. struct journal_head **list = NULL;
  1830. transaction_t *transaction;
  1831. struct buffer_head *bh = jh2bh(jh);
  1832. lockdep_assert_held(&jh->b_state_lock);
  1833. transaction = jh->b_transaction;
  1834. if (transaction)
  1835. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1836. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1837. if (jh->b_jlist != BJ_None)
  1838. J_ASSERT_JH(jh, transaction != NULL);
  1839. switch (jh->b_jlist) {
  1840. case BJ_None:
  1841. return;
  1842. case BJ_Metadata:
  1843. transaction->t_nr_buffers--;
  1844. J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
  1845. list = &transaction->t_buffers;
  1846. break;
  1847. case BJ_Forget:
  1848. list = &transaction->t_forget;
  1849. break;
  1850. case BJ_Shadow:
  1851. list = &transaction->t_shadow_list;
  1852. break;
  1853. case BJ_Reserved:
  1854. list = &transaction->t_reserved_list;
  1855. break;
  1856. }
  1857. __blist_del_buffer(list, jh);
  1858. jh->b_jlist = BJ_None;
  1859. if (transaction && is_journal_aborted(transaction->t_journal))
  1860. clear_buffer_jbddirty(bh);
  1861. else if (test_clear_buffer_jbddirty(bh))
  1862. mark_buffer_dirty(bh); /* Expose it to the VM */
  1863. }
  1864. /*
  1865. * Remove buffer from all transactions. The caller is responsible for dropping
  1866. * the jh reference that belonged to the transaction.
  1867. *
  1868. * Called with bh_state lock and j_list_lock
  1869. */
  1870. static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
  1871. {
  1872. J_ASSERT_JH(jh, jh->b_transaction != NULL);
  1873. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  1874. __jbd2_journal_temp_unlink_buffer(jh);
  1875. jh->b_transaction = NULL;
  1876. }
  1877. void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
  1878. {
  1879. struct buffer_head *bh = jh2bh(jh);
  1880. /* Get reference so that buffer cannot be freed before we unlock it */
  1881. get_bh(bh);
  1882. spin_lock(&jh->b_state_lock);
  1883. spin_lock(&journal->j_list_lock);
  1884. __jbd2_journal_unfile_buffer(jh);
  1885. spin_unlock(&journal->j_list_lock);
  1886. spin_unlock(&jh->b_state_lock);
  1887. jbd2_journal_put_journal_head(jh);
  1888. __brelse(bh);
  1889. }
  1890. /*
  1891. * Called from jbd2_journal_try_to_free_buffers().
  1892. *
  1893. * Called under jh->b_state_lock
  1894. */
  1895. static void
  1896. __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
  1897. {
  1898. struct journal_head *jh;
  1899. jh = bh2jh(bh);
  1900. if (jh->b_next_transaction != NULL || jh->b_transaction != NULL)
  1901. return;
  1902. spin_lock(&journal->j_list_lock);
  1903. /* Remove written-back checkpointed metadata buffer */
  1904. if (jh->b_cp_transaction != NULL)
  1905. jbd2_journal_try_remove_checkpoint(jh);
  1906. spin_unlock(&journal->j_list_lock);
  1907. return;
  1908. }
  1909. /**
  1910. * jbd2_journal_try_to_free_buffers() - try to free page buffers.
  1911. * @journal: journal for operation
  1912. * @folio: Folio to detach data from.
  1913. *
  1914. * For all the buffers on this page,
  1915. * if they are fully written out ordered data, move them onto BUF_CLEAN
  1916. * so try_to_free_buffers() can reap them.
  1917. *
  1918. * This function returns non-zero if we wish try_to_free_buffers()
  1919. * to be called. We do this if the page is releasable by try_to_free_buffers().
  1920. * We also do it if the page has locked or dirty buffers and the caller wants
  1921. * us to perform sync or async writeout.
  1922. *
  1923. * This complicates JBD locking somewhat. We aren't protected by the
  1924. * BKL here. We wish to remove the buffer from its committing or
  1925. * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
  1926. *
  1927. * This may *change* the value of transaction_t->t_datalist, so anyone
  1928. * who looks at t_datalist needs to lock against this function.
  1929. *
  1930. * Even worse, someone may be doing a jbd2_journal_dirty_data on this
  1931. * buffer. So we need to lock against that. jbd2_journal_dirty_data()
  1932. * will come out of the lock with the buffer dirty, which makes it
  1933. * ineligible for release here.
  1934. *
  1935. * Who else is affected by this? hmm... Really the only contender
  1936. * is do_get_write_access() - it could be looking at the buffer while
  1937. * journal_try_to_free_buffer() is changing its state. But that
  1938. * cannot happen because we never reallocate freed data as metadata
  1939. * while the data is part of a transaction. Yes?
  1940. *
  1941. * Return false on failure, true on success
  1942. */
  1943. bool jbd2_journal_try_to_free_buffers(journal_t *journal, struct folio *folio)
  1944. {
  1945. struct buffer_head *head;
  1946. struct buffer_head *bh;
  1947. bool ret = false;
  1948. J_ASSERT(folio_test_locked(folio));
  1949. head = folio_buffers(folio);
  1950. bh = head;
  1951. do {
  1952. struct journal_head *jh;
  1953. /*
  1954. * We take our own ref against the journal_head here to avoid
  1955. * having to add tons of locking around each instance of
  1956. * jbd2_journal_put_journal_head().
  1957. */
  1958. jh = jbd2_journal_grab_journal_head(bh);
  1959. if (!jh)
  1960. continue;
  1961. spin_lock(&jh->b_state_lock);
  1962. __journal_try_to_free_buffer(journal, bh);
  1963. spin_unlock(&jh->b_state_lock);
  1964. jbd2_journal_put_journal_head(jh);
  1965. if (buffer_jbd(bh))
  1966. goto busy;
  1967. } while ((bh = bh->b_this_page) != head);
  1968. ret = try_to_free_buffers(folio);
  1969. busy:
  1970. return ret;
  1971. }
  1972. /*
  1973. * This buffer is no longer needed. If it is on an older transaction's
  1974. * checkpoint list we need to record it on this transaction's forget list
  1975. * to pin this buffer (and hence its checkpointing transaction) down until
  1976. * this transaction commits. If the buffer isn't on a checkpoint list, we
  1977. * release it.
  1978. * Returns non-zero if JBD no longer has an interest in the buffer.
  1979. *
  1980. * Called under j_list_lock.
  1981. *
  1982. * Called under jh->b_state_lock.
  1983. */
  1984. static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
  1985. {
  1986. int may_free = 1;
  1987. struct buffer_head *bh = jh2bh(jh);
  1988. if (jh->b_cp_transaction) {
  1989. JBUFFER_TRACE(jh, "on running+cp transaction");
  1990. __jbd2_journal_temp_unlink_buffer(jh);
  1991. /*
  1992. * We don't want to write the buffer anymore, clear the
  1993. * bit so that we don't confuse checks in
  1994. * __journal_file_buffer
  1995. */
  1996. clear_buffer_dirty(bh);
  1997. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1998. may_free = 0;
  1999. } else {
  2000. JBUFFER_TRACE(jh, "on running transaction");
  2001. __jbd2_journal_unfile_buffer(jh);
  2002. jbd2_journal_put_journal_head(jh);
  2003. }
  2004. return may_free;
  2005. }
  2006. /*
  2007. * jbd2_journal_invalidate_folio
  2008. *
  2009. * This code is tricky. It has a number of cases to deal with.
  2010. *
  2011. * There are two invariants which this code relies on:
  2012. *
  2013. * i_size must be updated on disk before we start calling invalidate_folio
  2014. * on the data.
  2015. *
  2016. * This is done in ext3 by defining an ext3_setattr method which
  2017. * updates i_size before truncate gets going. By maintaining this
  2018. * invariant, we can be sure that it is safe to throw away any buffers
  2019. * attached to the current transaction: once the transaction commits,
  2020. * we know that the data will not be needed.
  2021. *
  2022. * Note however that we can *not* throw away data belonging to the
  2023. * previous, committing transaction!
  2024. *
  2025. * Any disk blocks which *are* part of the previous, committing
  2026. * transaction (and which therefore cannot be discarded immediately) are
  2027. * not going to be reused in the new running transaction
  2028. *
  2029. * The bitmap committed_data images guarantee this: any block which is
  2030. * allocated in one transaction and removed in the next will be marked
  2031. * as in-use in the committed_data bitmap, so cannot be reused until
  2032. * the next transaction to delete the block commits. This means that
  2033. * leaving committing buffers dirty is quite safe: the disk blocks
  2034. * cannot be reallocated to a different file and so buffer aliasing is
  2035. * not possible.
  2036. *
  2037. *
  2038. * The above applies mainly to ordered data mode. In writeback mode we
  2039. * don't make guarantees about the order in which data hits disk --- in
  2040. * particular we don't guarantee that new dirty data is flushed before
  2041. * transaction commit --- so it is always safe just to discard data
  2042. * immediately in that mode. --sct
  2043. */
  2044. /*
  2045. * The journal_unmap_buffer helper function returns zero if the buffer
  2046. * concerned remains pinned as an anonymous buffer belonging to an older
  2047. * transaction.
  2048. *
  2049. * We're outside-transaction here. Either or both of j_running_transaction
  2050. * and j_committing_transaction may be NULL.
  2051. */
  2052. static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
  2053. int partial_page)
  2054. {
  2055. transaction_t *transaction;
  2056. struct journal_head *jh;
  2057. int may_free = 1;
  2058. BUFFER_TRACE(bh, "entry");
  2059. /*
  2060. * It is safe to proceed here without the j_list_lock because the
  2061. * buffers cannot be stolen by try_to_free_buffers as long as we are
  2062. * holding the page lock. --sct
  2063. */
  2064. jh = jbd2_journal_grab_journal_head(bh);
  2065. if (!jh)
  2066. goto zap_buffer_unlocked;
  2067. /* OK, we have data buffer in journaled mode */
  2068. write_lock(&journal->j_state_lock);
  2069. spin_lock(&jh->b_state_lock);
  2070. spin_lock(&journal->j_list_lock);
  2071. /*
  2072. * We cannot remove the buffer from checkpoint lists until the
  2073. * transaction adding inode to orphan list (let's call it T)
  2074. * is committed. Otherwise if the transaction changing the
  2075. * buffer would be cleaned from the journal before T is
  2076. * committed, a crash will cause that the correct contents of
  2077. * the buffer will be lost. On the other hand we have to
  2078. * clear the buffer dirty bit at latest at the moment when the
  2079. * transaction marking the buffer as freed in the filesystem
  2080. * structures is committed because from that moment on the
  2081. * block can be reallocated and used by a different page.
  2082. * Since the block hasn't been freed yet but the inode has
  2083. * already been added to orphan list, it is safe for us to add
  2084. * the buffer to BJ_Forget list of the newest transaction.
  2085. *
  2086. * Also we have to clear buffer_mapped flag of a truncated buffer
  2087. * because the buffer_head may be attached to the page straddling
  2088. * i_size (can happen only when blocksize < pagesize) and thus the
  2089. * buffer_head can be reused when the file is extended again. So we end
  2090. * up keeping around invalidated buffers attached to transactions'
  2091. * BJ_Forget list just to stop checkpointing code from cleaning up
  2092. * the transaction this buffer was modified in.
  2093. */
  2094. transaction = jh->b_transaction;
  2095. if (transaction == NULL) {
  2096. /* First case: not on any transaction. If it
  2097. * has no checkpoint link, then we can zap it:
  2098. * it's a writeback-mode buffer so we don't care
  2099. * if it hits disk safely. */
  2100. if (!jh->b_cp_transaction) {
  2101. JBUFFER_TRACE(jh, "not on any transaction: zap");
  2102. goto zap_buffer;
  2103. }
  2104. if (!buffer_dirty(bh)) {
  2105. /* bdflush has written it. We can drop it now */
  2106. __jbd2_journal_remove_checkpoint(jh);
  2107. goto zap_buffer;
  2108. }
  2109. /* OK, it must be in the journal but still not
  2110. * written fully to disk: it's metadata or
  2111. * journaled data... */
  2112. if (journal->j_running_transaction) {
  2113. /* ... and once the current transaction has
  2114. * committed, the buffer won't be needed any
  2115. * longer. */
  2116. JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
  2117. may_free = __dispose_buffer(jh,
  2118. journal->j_running_transaction);
  2119. goto zap_buffer;
  2120. } else {
  2121. /* There is no currently-running transaction. So the
  2122. * orphan record which we wrote for this file must have
  2123. * passed into commit. We must attach this buffer to
  2124. * the committing transaction, if it exists. */
  2125. if (journal->j_committing_transaction) {
  2126. JBUFFER_TRACE(jh, "give to committing trans");
  2127. may_free = __dispose_buffer(jh,
  2128. journal->j_committing_transaction);
  2129. goto zap_buffer;
  2130. } else {
  2131. /* The orphan record's transaction has
  2132. * committed. We can cleanse this buffer */
  2133. clear_buffer_jbddirty(bh);
  2134. __jbd2_journal_remove_checkpoint(jh);
  2135. goto zap_buffer;
  2136. }
  2137. }
  2138. } else if (transaction == journal->j_committing_transaction) {
  2139. JBUFFER_TRACE(jh, "on committing transaction");
  2140. /*
  2141. * The buffer is committing, we simply cannot touch
  2142. * it. If the page is straddling i_size we have to wait
  2143. * for commit and try again.
  2144. */
  2145. if (partial_page) {
  2146. spin_unlock(&journal->j_list_lock);
  2147. spin_unlock(&jh->b_state_lock);
  2148. write_unlock(&journal->j_state_lock);
  2149. jbd2_journal_put_journal_head(jh);
  2150. /* Already zapped buffer? Nothing to do... */
  2151. if (!bh->b_bdev)
  2152. return 0;
  2153. return -EBUSY;
  2154. }
  2155. /*
  2156. * OK, buffer won't be reachable after truncate. We just clear
  2157. * b_modified to not confuse transaction credit accounting, and
  2158. * set j_next_transaction to the running transaction (if there
  2159. * is one) and mark buffer as freed so that commit code knows
  2160. * it should clear dirty bits when it is done with the buffer.
  2161. */
  2162. set_buffer_freed(bh);
  2163. if (journal->j_running_transaction && buffer_jbddirty(bh))
  2164. jh->b_next_transaction = journal->j_running_transaction;
  2165. jh->b_modified = 0;
  2166. spin_unlock(&journal->j_list_lock);
  2167. spin_unlock(&jh->b_state_lock);
  2168. write_unlock(&journal->j_state_lock);
  2169. jbd2_journal_put_journal_head(jh);
  2170. return 0;
  2171. } else {
  2172. /* Good, the buffer belongs to the running transaction.
  2173. * We are writing our own transaction's data, not any
  2174. * previous one's, so it is safe to throw it away
  2175. * (remember that we expect the filesystem to have set
  2176. * i_size already for this truncate so recovery will not
  2177. * expose the disk blocks we are discarding here.) */
  2178. J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
  2179. JBUFFER_TRACE(jh, "on running transaction");
  2180. may_free = __dispose_buffer(jh, transaction);
  2181. }
  2182. zap_buffer:
  2183. /*
  2184. * This is tricky. Although the buffer is truncated, it may be reused
  2185. * if blocksize < pagesize and it is attached to the page straddling
  2186. * EOF. Since the buffer might have been added to BJ_Forget list of the
  2187. * running transaction, journal_get_write_access() won't clear
  2188. * b_modified and credit accounting gets confused. So clear b_modified
  2189. * here.
  2190. */
  2191. jh->b_modified = 0;
  2192. spin_unlock(&journal->j_list_lock);
  2193. spin_unlock(&jh->b_state_lock);
  2194. write_unlock(&journal->j_state_lock);
  2195. jbd2_journal_put_journal_head(jh);
  2196. zap_buffer_unlocked:
  2197. clear_buffer_dirty(bh);
  2198. J_ASSERT_BH(bh, !buffer_jbddirty(bh));
  2199. clear_buffer_mapped(bh);
  2200. clear_buffer_req(bh);
  2201. clear_buffer_new(bh);
  2202. clear_buffer_delay(bh);
  2203. clear_buffer_unwritten(bh);
  2204. bh->b_bdev = NULL;
  2205. return may_free;
  2206. }
  2207. /**
  2208. * jbd2_journal_invalidate_folio()
  2209. * @journal: journal to use for flush...
  2210. * @folio: folio to flush
  2211. * @offset: start of the range to invalidate
  2212. * @length: length of the range to invalidate
  2213. *
  2214. * Reap page buffers containing data after in the specified range in page.
  2215. * Can return -EBUSY if buffers are part of the committing transaction and
  2216. * the page is straddling i_size. Caller then has to wait for current commit
  2217. * and try again.
  2218. */
  2219. int jbd2_journal_invalidate_folio(journal_t *journal, struct folio *folio,
  2220. size_t offset, size_t length)
  2221. {
  2222. struct buffer_head *head, *bh, *next;
  2223. unsigned int stop = offset + length;
  2224. unsigned int curr_off = 0;
  2225. int partial_page = (offset || length < folio_size(folio));
  2226. int may_free = 1;
  2227. int ret = 0;
  2228. if (!folio_test_locked(folio))
  2229. BUG();
  2230. head = folio_buffers(folio);
  2231. if (!head)
  2232. return 0;
  2233. BUG_ON(stop > folio_size(folio) || stop < length);
  2234. /* We will potentially be playing with lists other than just the
  2235. * data lists (especially for journaled data mode), so be
  2236. * cautious in our locking. */
  2237. bh = head;
  2238. do {
  2239. unsigned int next_off = curr_off + bh->b_size;
  2240. next = bh->b_this_page;
  2241. if (next_off > stop)
  2242. return 0;
  2243. if (offset <= curr_off) {
  2244. /* This block is wholly outside the truncation point */
  2245. lock_buffer(bh);
  2246. ret = journal_unmap_buffer(journal, bh, partial_page);
  2247. unlock_buffer(bh);
  2248. if (ret < 0)
  2249. return ret;
  2250. may_free &= ret;
  2251. }
  2252. curr_off = next_off;
  2253. bh = next;
  2254. } while (bh != head);
  2255. if (!partial_page) {
  2256. if (may_free && try_to_free_buffers(folio))
  2257. J_ASSERT(!folio_buffers(folio));
  2258. }
  2259. return 0;
  2260. }
  2261. /*
  2262. * File a buffer on the given transaction list.
  2263. */
  2264. void __jbd2_journal_file_buffer(struct journal_head *jh,
  2265. transaction_t *transaction, int jlist)
  2266. {
  2267. struct journal_head **list = NULL;
  2268. int was_dirty = 0;
  2269. struct buffer_head *bh = jh2bh(jh);
  2270. lockdep_assert_held(&jh->b_state_lock);
  2271. assert_spin_locked(&transaction->t_journal->j_list_lock);
  2272. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  2273. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  2274. jh->b_transaction == NULL);
  2275. if (jh->b_transaction && jh->b_jlist == jlist)
  2276. return;
  2277. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  2278. jlist == BJ_Shadow || jlist == BJ_Forget) {
  2279. /*
  2280. * For metadata buffers, we track dirty bit in buffer_jbddirty
  2281. * instead of buffer_dirty. We should not see a dirty bit set
  2282. * here because we clear it in do_get_write_access but e.g.
  2283. * tune2fs can modify the sb and set the dirty bit at any time
  2284. * so we try to gracefully handle that.
  2285. */
  2286. if (buffer_dirty(bh))
  2287. warn_dirty_buffer(bh);
  2288. if (test_clear_buffer_dirty(bh) ||
  2289. test_clear_buffer_jbddirty(bh))
  2290. was_dirty = 1;
  2291. }
  2292. if (jh->b_transaction)
  2293. __jbd2_journal_temp_unlink_buffer(jh);
  2294. else
  2295. jbd2_journal_grab_journal_head(bh);
  2296. jh->b_transaction = transaction;
  2297. switch (jlist) {
  2298. case BJ_None:
  2299. J_ASSERT_JH(jh, !jh->b_committed_data);
  2300. J_ASSERT_JH(jh, !jh->b_frozen_data);
  2301. return;
  2302. case BJ_Metadata:
  2303. transaction->t_nr_buffers++;
  2304. list = &transaction->t_buffers;
  2305. break;
  2306. case BJ_Forget:
  2307. list = &transaction->t_forget;
  2308. break;
  2309. case BJ_Shadow:
  2310. list = &transaction->t_shadow_list;
  2311. break;
  2312. case BJ_Reserved:
  2313. list = &transaction->t_reserved_list;
  2314. break;
  2315. }
  2316. __blist_add_buffer(list, jh);
  2317. jh->b_jlist = jlist;
  2318. if (was_dirty)
  2319. set_buffer_jbddirty(bh);
  2320. }
  2321. void jbd2_journal_file_buffer(struct journal_head *jh,
  2322. transaction_t *transaction, int jlist)
  2323. {
  2324. spin_lock(&jh->b_state_lock);
  2325. spin_lock(&transaction->t_journal->j_list_lock);
  2326. __jbd2_journal_file_buffer(jh, transaction, jlist);
  2327. spin_unlock(&transaction->t_journal->j_list_lock);
  2328. spin_unlock(&jh->b_state_lock);
  2329. }
  2330. /*
  2331. * Remove a buffer from its current buffer list in preparation for
  2332. * dropping it from its current transaction entirely. If the buffer has
  2333. * already started to be used by a subsequent transaction, refile the
  2334. * buffer on that transaction's metadata list.
  2335. *
  2336. * Called under j_list_lock
  2337. * Called under jh->b_state_lock
  2338. *
  2339. * When this function returns true, there's no next transaction to refile to
  2340. * and the caller has to drop jh reference through
  2341. * jbd2_journal_put_journal_head().
  2342. */
  2343. bool __jbd2_journal_refile_buffer(struct journal_head *jh)
  2344. {
  2345. int was_dirty, jlist;
  2346. struct buffer_head *bh = jh2bh(jh);
  2347. lockdep_assert_held(&jh->b_state_lock);
  2348. if (jh->b_transaction)
  2349. assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
  2350. /* If the buffer is now unused, just drop it. */
  2351. if (jh->b_next_transaction == NULL) {
  2352. __jbd2_journal_unfile_buffer(jh);
  2353. return true;
  2354. }
  2355. /*
  2356. * It has been modified by a later transaction: add it to the new
  2357. * transaction's metadata list.
  2358. */
  2359. was_dirty = test_clear_buffer_jbddirty(bh);
  2360. __jbd2_journal_temp_unlink_buffer(jh);
  2361. /*
  2362. * b_transaction must be set, otherwise the new b_transaction won't
  2363. * be holding jh reference
  2364. */
  2365. J_ASSERT_JH(jh, jh->b_transaction != NULL);
  2366. /*
  2367. * We set b_transaction here because b_next_transaction will inherit
  2368. * our jh reference and thus __jbd2_journal_file_buffer() must not
  2369. * take a new one.
  2370. */
  2371. WRITE_ONCE(jh->b_transaction, jh->b_next_transaction);
  2372. WRITE_ONCE(jh->b_next_transaction, NULL);
  2373. if (buffer_freed(bh))
  2374. jlist = BJ_Forget;
  2375. else if (jh->b_modified)
  2376. jlist = BJ_Metadata;
  2377. else
  2378. jlist = BJ_Reserved;
  2379. __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
  2380. J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
  2381. if (was_dirty)
  2382. set_buffer_jbddirty(bh);
  2383. return false;
  2384. }
  2385. /*
  2386. * __jbd2_journal_refile_buffer() with necessary locking added. We take our
  2387. * bh reference so that we can safely unlock bh.
  2388. *
  2389. * The jh and bh may be freed by this call.
  2390. */
  2391. void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
  2392. {
  2393. bool drop;
  2394. spin_lock(&jh->b_state_lock);
  2395. spin_lock(&journal->j_list_lock);
  2396. drop = __jbd2_journal_refile_buffer(jh);
  2397. spin_unlock(&jh->b_state_lock);
  2398. spin_unlock(&journal->j_list_lock);
  2399. if (drop)
  2400. jbd2_journal_put_journal_head(jh);
  2401. }
  2402. /*
  2403. * File inode in the inode list of the handle's transaction
  2404. */
  2405. static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
  2406. unsigned long flags, loff_t start_byte, loff_t end_byte)
  2407. {
  2408. transaction_t *transaction = handle->h_transaction;
  2409. journal_t *journal;
  2410. if (is_handle_aborted(handle))
  2411. return -EROFS;
  2412. journal = transaction->t_journal;
  2413. jbd2_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
  2414. transaction->t_tid);
  2415. spin_lock(&journal->j_list_lock);
  2416. jinode->i_flags |= flags;
  2417. if (jinode->i_dirty_end) {
  2418. jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte);
  2419. jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte);
  2420. } else {
  2421. jinode->i_dirty_start = start_byte;
  2422. jinode->i_dirty_end = end_byte;
  2423. }
  2424. /* Is inode already attached where we need it? */
  2425. if (jinode->i_transaction == transaction ||
  2426. jinode->i_next_transaction == transaction)
  2427. goto done;
  2428. /*
  2429. * We only ever set this variable to 1 so the test is safe. Since
  2430. * t_need_data_flush is likely to be set, we do the test to save some
  2431. * cacheline bouncing
  2432. */
  2433. if (!transaction->t_need_data_flush)
  2434. transaction->t_need_data_flush = 1;
  2435. /* On some different transaction's list - should be
  2436. * the committing one */
  2437. if (jinode->i_transaction) {
  2438. J_ASSERT(jinode->i_next_transaction == NULL);
  2439. J_ASSERT(jinode->i_transaction ==
  2440. journal->j_committing_transaction);
  2441. jinode->i_next_transaction = transaction;
  2442. goto done;
  2443. }
  2444. /* Not on any transaction list... */
  2445. J_ASSERT(!jinode->i_next_transaction);
  2446. jinode->i_transaction = transaction;
  2447. list_add(&jinode->i_list, &transaction->t_inode_list);
  2448. done:
  2449. spin_unlock(&journal->j_list_lock);
  2450. return 0;
  2451. }
  2452. int jbd2_journal_inode_ranged_write(handle_t *handle,
  2453. struct jbd2_inode *jinode, loff_t start_byte, loff_t length)
  2454. {
  2455. return jbd2_journal_file_inode(handle, jinode,
  2456. JI_WRITE_DATA | JI_WAIT_DATA, start_byte,
  2457. start_byte + length - 1);
  2458. }
  2459. int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode,
  2460. loff_t start_byte, loff_t length)
  2461. {
  2462. return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA,
  2463. start_byte, start_byte + length - 1);
  2464. }
  2465. /*
  2466. * File truncate and transaction commit interact with each other in a
  2467. * non-trivial way. If a transaction writing data block A is
  2468. * committing, we cannot discard the data by truncate until we have
  2469. * written them. Otherwise if we crashed after the transaction with
  2470. * write has committed but before the transaction with truncate has
  2471. * committed, we could see stale data in block A. This function is a
  2472. * helper to solve this problem. It starts writeout of the truncated
  2473. * part in case it is in the committing transaction.
  2474. *
  2475. * Filesystem code must call this function when inode is journaled in
  2476. * ordered mode before truncation happens and after the inode has been
  2477. * placed on orphan list with the new inode size. The second condition
  2478. * avoids the race that someone writes new data and we start
  2479. * committing the transaction after this function has been called but
  2480. * before a transaction for truncate is started (and furthermore it
  2481. * allows us to optimize the case where the addition to orphan list
  2482. * happens in the same transaction as write --- we don't have to write
  2483. * any data in such case).
  2484. */
  2485. int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  2486. struct jbd2_inode *jinode,
  2487. loff_t new_size)
  2488. {
  2489. transaction_t *inode_trans, *commit_trans;
  2490. int ret = 0;
  2491. /* This is a quick check to avoid locking if not necessary */
  2492. if (!jinode->i_transaction)
  2493. goto out;
  2494. /* Locks are here just to force reading of recent values, it is
  2495. * enough that the transaction was not committing before we started
  2496. * a transaction adding the inode to orphan list */
  2497. read_lock(&journal->j_state_lock);
  2498. commit_trans = journal->j_committing_transaction;
  2499. read_unlock(&journal->j_state_lock);
  2500. spin_lock(&journal->j_list_lock);
  2501. inode_trans = jinode->i_transaction;
  2502. spin_unlock(&journal->j_list_lock);
  2503. if (inode_trans == commit_trans) {
  2504. ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  2505. new_size, LLONG_MAX);
  2506. if (ret)
  2507. jbd2_journal_abort(journal, ret);
  2508. }
  2509. out:
  2510. return ret;
  2511. }