transaction.c 81 KB

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