transaction.c 62 KB

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