transaction.c 77 KB

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