transaction.c 66 KB

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