transaction.c 75 KB

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