transaction.c 62 KB

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