transaction.c 80 KB

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