transaction.c 79 KB

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