transaction.c 80 KB

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