transaction.c 66 KB

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