transaction.c 66 KB

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