transaction.c 80 KB

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