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