transaction.c 70 KB

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