transaction.c 75 KB

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