transaction.c 65 KB

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