transaction.c 63 KB

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