transaction.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  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. static void __journal_temp_unlink_buffer(struct journal_head *jh);
  28. /*
  29. * get_transaction: obtain a new transaction_t object.
  30. *
  31. * Simply allocate and initialise a new transaction. Create it in
  32. * RUNNING state and add it to the current journal (which should not
  33. * have an existing running transaction: we only make a new transaction
  34. * once we have started to commit the old one).
  35. *
  36. * Preconditions:
  37. * The journal MUST be locked. We don't perform atomic mallocs on the
  38. * new transaction and we can't block without protecting against other
  39. * processes trying to touch the journal while it is in transition.
  40. *
  41. * Called under j_state_lock
  42. */
  43. static transaction_t *
  44. get_transaction(journal_t *journal, transaction_t *transaction)
  45. {
  46. transaction->t_journal = journal;
  47. transaction->t_state = T_RUNNING;
  48. transaction->t_tid = journal->j_transaction_sequence++;
  49. transaction->t_expires = jiffies + journal->j_commit_interval;
  50. spin_lock_init(&transaction->t_handle_lock);
  51. /* Set up the commit timer for the new transaction. */
  52. journal->j_commit_timer.expires = round_jiffies(transaction->t_expires);
  53. add_timer(&journal->j_commit_timer);
  54. J_ASSERT(journal->j_running_transaction == NULL);
  55. journal->j_running_transaction = transaction;
  56. return transaction;
  57. }
  58. /*
  59. * Handle management.
  60. *
  61. * A handle_t is an object which represents a single atomic update to a
  62. * filesystem, and which tracks all of the modifications which form part
  63. * of that one update.
  64. */
  65. /*
  66. * start_this_handle: Given a handle, deal with any locking or stalling
  67. * needed to make sure that there is enough journal space for the handle
  68. * to begin. Attach the handle to a transaction and set up the
  69. * transaction's buffer credits.
  70. */
  71. static int start_this_handle(journal_t *journal, handle_t *handle)
  72. {
  73. transaction_t *transaction;
  74. int needed;
  75. int nblocks = handle->h_buffer_credits;
  76. transaction_t *new_transaction = NULL;
  77. int ret = 0;
  78. if (nblocks > journal->j_max_transaction_buffers) {
  79. printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
  80. current->comm, nblocks,
  81. journal->j_max_transaction_buffers);
  82. ret = -ENOSPC;
  83. goto out;
  84. }
  85. alloc_transaction:
  86. if (!journal->j_running_transaction) {
  87. new_transaction = kzalloc(sizeof(*new_transaction),
  88. GFP_NOFS|__GFP_NOFAIL);
  89. if (!new_transaction) {
  90. ret = -ENOMEM;
  91. goto out;
  92. }
  93. }
  94. jbd_debug(3, "New handle %p going live.\n", handle);
  95. repeat:
  96. /*
  97. * We need to hold j_state_lock until t_updates has been incremented,
  98. * for proper journal barrier handling
  99. */
  100. spin_lock(&journal->j_state_lock);
  101. repeat_locked:
  102. if (is_journal_aborted(journal) ||
  103. (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
  104. spin_unlock(&journal->j_state_lock);
  105. ret = -EROFS;
  106. goto out;
  107. }
  108. /* Wait on the journal's transaction barrier if necessary */
  109. if (journal->j_barrier_count) {
  110. spin_unlock(&journal->j_state_lock);
  111. wait_event(journal->j_wait_transaction_locked,
  112. journal->j_barrier_count == 0);
  113. goto repeat;
  114. }
  115. if (!journal->j_running_transaction) {
  116. if (!new_transaction) {
  117. spin_unlock(&journal->j_state_lock);
  118. goto alloc_transaction;
  119. }
  120. get_transaction(journal, new_transaction);
  121. new_transaction = NULL;
  122. }
  123. transaction = journal->j_running_transaction;
  124. /*
  125. * If the current transaction is locked down for commit, wait for the
  126. * lock to be released.
  127. */
  128. if (transaction->t_state == T_LOCKED) {
  129. DEFINE_WAIT(wait);
  130. prepare_to_wait(&journal->j_wait_transaction_locked,
  131. &wait, TASK_UNINTERRUPTIBLE);
  132. spin_unlock(&journal->j_state_lock);
  133. schedule();
  134. finish_wait(&journal->j_wait_transaction_locked, &wait);
  135. goto repeat;
  136. }
  137. /*
  138. * If there is not enough space left in the log to write all potential
  139. * buffers requested by this operation, we need to stall pending a log
  140. * checkpoint to free some more log space.
  141. */
  142. spin_lock(&transaction->t_handle_lock);
  143. needed = transaction->t_outstanding_credits + nblocks;
  144. if (needed > journal->j_max_transaction_buffers) {
  145. /*
  146. * If the current transaction is already too large, then start
  147. * to commit it: we can then go back and attach this handle to
  148. * a new transaction.
  149. */
  150. DEFINE_WAIT(wait);
  151. jbd_debug(2, "Handle %p starting new commit...\n", handle);
  152. spin_unlock(&transaction->t_handle_lock);
  153. prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
  154. TASK_UNINTERRUPTIBLE);
  155. __log_start_commit(journal, transaction->t_tid);
  156. spin_unlock(&journal->j_state_lock);
  157. schedule();
  158. finish_wait(&journal->j_wait_transaction_locked, &wait);
  159. goto repeat;
  160. }
  161. /*
  162. * The commit code assumes that it can get enough log space
  163. * without forcing a checkpoint. This is *critical* for
  164. * correctness: a checkpoint of a buffer which is also
  165. * associated with a committing transaction creates a deadlock,
  166. * so commit simply cannot force through checkpoints.
  167. *
  168. * We must therefore ensure the necessary space in the journal
  169. * *before* starting to dirty potentially checkpointed buffers
  170. * in the new transaction.
  171. *
  172. * The worst part is, any transaction currently committing can
  173. * reduce the free space arbitrarily. Be careful to account for
  174. * those buffers when checkpointing.
  175. */
  176. /*
  177. * @@@ AKPM: This seems rather over-defensive. We're giving commit
  178. * a _lot_ of headroom: 1/4 of the journal plus the size of
  179. * the committing transaction. Really, we only need to give it
  180. * committing_transaction->t_outstanding_credits plus "enough" for
  181. * the log control blocks.
  182. * Also, this test is inconsitent with the matching one in
  183. * journal_extend().
  184. */
  185. if (__log_space_left(journal) < jbd_space_needed(journal)) {
  186. jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
  187. spin_unlock(&transaction->t_handle_lock);
  188. __log_wait_for_space(journal);
  189. goto repeat_locked;
  190. }
  191. /* OK, account for the buffers that this operation expects to
  192. * use and add the handle to the running transaction. */
  193. handle->h_transaction = transaction;
  194. transaction->t_outstanding_credits += nblocks;
  195. transaction->t_updates++;
  196. transaction->t_handle_count++;
  197. jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
  198. handle, nblocks, transaction->t_outstanding_credits,
  199. __log_space_left(journal));
  200. spin_unlock(&transaction->t_handle_lock);
  201. spin_unlock(&journal->j_state_lock);
  202. out:
  203. if (unlikely(new_transaction)) /* It's usually NULL */
  204. kfree(new_transaction);
  205. return ret;
  206. }
  207. static struct lock_class_key jbd_handle_key;
  208. /* Allocate a new handle. This should probably be in a slab... */
  209. static handle_t *new_handle(int nblocks)
  210. {
  211. handle_t *handle = jbd_alloc_handle(GFP_NOFS);
  212. if (!handle)
  213. return NULL;
  214. memset(handle, 0, sizeof(*handle));
  215. handle->h_buffer_credits = nblocks;
  216. handle->h_ref = 1;
  217. lockdep_init_map(&handle->h_lockdep_map, "jbd_handle", &jbd_handle_key, 0);
  218. return handle;
  219. }
  220. /**
  221. * handle_t *journal_start() - Obtain a new handle.
  222. * @journal: Journal to start transaction on.
  223. * @nblocks: number of block buffer we might modify
  224. *
  225. * We make sure that the transaction can guarantee at least nblocks of
  226. * modified buffers in the log. We block until the log can guarantee
  227. * that much space.
  228. *
  229. * This function is visible to journal users (like ext3fs), so is not
  230. * called with the journal already locked.
  231. *
  232. * Return a pointer to a newly allocated handle, or NULL on failure
  233. */
  234. handle_t *journal_start(journal_t *journal, int nblocks)
  235. {
  236. handle_t *handle = journal_current_handle();
  237. int err;
  238. if (!journal)
  239. return ERR_PTR(-EROFS);
  240. if (handle) {
  241. J_ASSERT(handle->h_transaction->t_journal == journal);
  242. handle->h_ref++;
  243. return handle;
  244. }
  245. handle = new_handle(nblocks);
  246. if (!handle)
  247. return ERR_PTR(-ENOMEM);
  248. current->journal_info = handle;
  249. err = start_this_handle(journal, handle);
  250. if (err < 0) {
  251. jbd_free_handle(handle);
  252. current->journal_info = NULL;
  253. handle = ERR_PTR(err);
  254. goto out;
  255. }
  256. lock_map_acquire(&handle->h_lockdep_map);
  257. out:
  258. return handle;
  259. }
  260. /**
  261. * int journal_extend() - extend buffer credits.
  262. * @handle: handle to 'extend'
  263. * @nblocks: nr blocks to try to extend by.
  264. *
  265. * Some transactions, such as large extends and truncates, can be done
  266. * atomically all at once or in several stages. The operation requests
  267. * a credit for a number of buffer modications in advance, but can
  268. * extend its credit if it needs more.
  269. *
  270. * journal_extend tries to give the running handle more buffer credits.
  271. * It does not guarantee that allocation - this is a best-effort only.
  272. * The calling process MUST be able to deal cleanly with a failure to
  273. * extend here.
  274. *
  275. * Return 0 on success, non-zero on failure.
  276. *
  277. * return code < 0 implies an error
  278. * return code > 0 implies normal transaction-full status.
  279. */
  280. int journal_extend(handle_t *handle, int nblocks)
  281. {
  282. transaction_t *transaction = handle->h_transaction;
  283. journal_t *journal = transaction->t_journal;
  284. int result;
  285. int wanted;
  286. result = -EIO;
  287. if (is_handle_aborted(handle))
  288. goto out;
  289. result = 1;
  290. spin_lock(&journal->j_state_lock);
  291. /* Don't extend a locked-down transaction! */
  292. if (handle->h_transaction->t_state != T_RUNNING) {
  293. jbd_debug(3, "denied handle %p %d blocks: "
  294. "transaction not running\n", handle, nblocks);
  295. goto error_out;
  296. }
  297. spin_lock(&transaction->t_handle_lock);
  298. wanted = transaction->t_outstanding_credits + nblocks;
  299. if (wanted > journal->j_max_transaction_buffers) {
  300. jbd_debug(3, "denied handle %p %d blocks: "
  301. "transaction too large\n", handle, nblocks);
  302. goto unlock;
  303. }
  304. if (wanted > __log_space_left(journal)) {
  305. jbd_debug(3, "denied handle %p %d blocks: "
  306. "insufficient log space\n", handle, nblocks);
  307. goto unlock;
  308. }
  309. handle->h_buffer_credits += nblocks;
  310. transaction->t_outstanding_credits += nblocks;
  311. result = 0;
  312. jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
  313. unlock:
  314. spin_unlock(&transaction->t_handle_lock);
  315. error_out:
  316. spin_unlock(&journal->j_state_lock);
  317. out:
  318. return result;
  319. }
  320. /**
  321. * int journal_restart() - restart a handle.
  322. * @handle: handle to restart
  323. * @nblocks: nr credits requested
  324. *
  325. * Restart a handle for a multi-transaction filesystem
  326. * operation.
  327. *
  328. * If the journal_extend() call above fails to grant new buffer credits
  329. * to a running handle, a call to journal_restart will commit the
  330. * handle's transaction so far and reattach the handle to a new
  331. * transaction capabable of guaranteeing the requested number of
  332. * credits.
  333. */
  334. int journal_restart(handle_t *handle, int nblocks)
  335. {
  336. transaction_t *transaction = handle->h_transaction;
  337. journal_t *journal = transaction->t_journal;
  338. int ret;
  339. /* If we've had an abort of any type, don't even think about
  340. * actually doing the restart! */
  341. if (is_handle_aborted(handle))
  342. return 0;
  343. /*
  344. * First unlink the handle from its current transaction, and start the
  345. * commit on that.
  346. */
  347. J_ASSERT(transaction->t_updates > 0);
  348. J_ASSERT(journal_current_handle() == handle);
  349. spin_lock(&journal->j_state_lock);
  350. spin_lock(&transaction->t_handle_lock);
  351. transaction->t_outstanding_credits -= handle->h_buffer_credits;
  352. transaction->t_updates--;
  353. if (!transaction->t_updates)
  354. wake_up(&journal->j_wait_updates);
  355. spin_unlock(&transaction->t_handle_lock);
  356. jbd_debug(2, "restarting handle %p\n", handle);
  357. __log_start_commit(journal, transaction->t_tid);
  358. spin_unlock(&journal->j_state_lock);
  359. handle->h_buffer_credits = nblocks;
  360. ret = start_this_handle(journal, handle);
  361. return ret;
  362. }
  363. /**
  364. * void journal_lock_updates () - establish a transaction barrier.
  365. * @journal: Journal to establish a barrier on.
  366. *
  367. * This locks out any further updates from being started, and blocks
  368. * until all existing updates have completed, returning only once the
  369. * journal is in a quiescent state with no updates running.
  370. *
  371. * The journal lock should not be held on entry.
  372. */
  373. void journal_lock_updates(journal_t *journal)
  374. {
  375. DEFINE_WAIT(wait);
  376. spin_lock(&journal->j_state_lock);
  377. ++journal->j_barrier_count;
  378. /* Wait until there are no running updates */
  379. while (1) {
  380. transaction_t *transaction = journal->j_running_transaction;
  381. if (!transaction)
  382. break;
  383. spin_lock(&transaction->t_handle_lock);
  384. if (!transaction->t_updates) {
  385. spin_unlock(&transaction->t_handle_lock);
  386. break;
  387. }
  388. prepare_to_wait(&journal->j_wait_updates, &wait,
  389. TASK_UNINTERRUPTIBLE);
  390. spin_unlock(&transaction->t_handle_lock);
  391. spin_unlock(&journal->j_state_lock);
  392. schedule();
  393. finish_wait(&journal->j_wait_updates, &wait);
  394. spin_lock(&journal->j_state_lock);
  395. }
  396. spin_unlock(&journal->j_state_lock);
  397. /*
  398. * We have now established a barrier against other normal updates, but
  399. * we also need to barrier against other journal_lock_updates() calls
  400. * to make sure that we serialise special journal-locked operations
  401. * too.
  402. */
  403. mutex_lock(&journal->j_barrier);
  404. }
  405. /**
  406. * void journal_unlock_updates (journal_t* journal) - release barrier
  407. * @journal: Journal to release the barrier on.
  408. *
  409. * Release a transaction barrier obtained with journal_lock_updates().
  410. *
  411. * Should be called without the journal lock held.
  412. */
  413. void journal_unlock_updates (journal_t *journal)
  414. {
  415. J_ASSERT(journal->j_barrier_count != 0);
  416. mutex_unlock(&journal->j_barrier);
  417. spin_lock(&journal->j_state_lock);
  418. --journal->j_barrier_count;
  419. spin_unlock(&journal->j_state_lock);
  420. wake_up(&journal->j_wait_transaction_locked);
  421. }
  422. /*
  423. * Report any unexpected dirty buffers which turn up. Normally those
  424. * indicate an error, but they can occur if the user is running (say)
  425. * tune2fs to modify the live filesystem, so we need the option of
  426. * continuing as gracefully as possible. #
  427. *
  428. * The caller should already hold the journal lock and
  429. * j_list_lock spinlock: most callers will need those anyway
  430. * in order to probe the buffer's journaling state safely.
  431. */
  432. static void jbd_unexpected_dirty_buffer(struct journal_head *jh)
  433. {
  434. int jlist;
  435. /* If this buffer is one which might reasonably be dirty
  436. * --- ie. data, or not part of this journal --- then
  437. * we're OK to leave it alone, but otherwise we need to
  438. * move the dirty bit to the journal's own internal
  439. * JBDDirty bit. */
  440. jlist = jh->b_jlist;
  441. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  442. jlist == BJ_Shadow || jlist == BJ_Forget) {
  443. struct buffer_head *bh = jh2bh(jh);
  444. if (test_clear_buffer_dirty(bh))
  445. set_buffer_jbddirty(bh);
  446. }
  447. }
  448. /*
  449. * If the buffer is already part of the current transaction, then there
  450. * is nothing we need to do. If it is already part of a prior
  451. * transaction which we are still committing to disk, then we need to
  452. * make sure that we do not overwrite the old copy: we do copy-out to
  453. * preserve the copy going to disk. We also account the buffer against
  454. * the handle's metadata buffer credits (unless the buffer is already
  455. * part of the transaction, that is).
  456. *
  457. */
  458. static int
  459. do_get_write_access(handle_t *handle, struct journal_head *jh,
  460. int force_copy)
  461. {
  462. struct buffer_head *bh;
  463. transaction_t *transaction;
  464. journal_t *journal;
  465. int error;
  466. char *frozen_buffer = NULL;
  467. int need_copy = 0;
  468. if (is_handle_aborted(handle))
  469. return -EROFS;
  470. transaction = handle->h_transaction;
  471. journal = transaction->t_journal;
  472. jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
  473. JBUFFER_TRACE(jh, "entry");
  474. repeat:
  475. bh = jh2bh(jh);
  476. /* @@@ Need to check for errors here at some point. */
  477. lock_buffer(bh);
  478. jbd_lock_bh_state(bh);
  479. /* We now hold the buffer lock so it is safe to query the buffer
  480. * state. Is the buffer dirty?
  481. *
  482. * If so, there are two possibilities. The buffer may be
  483. * non-journaled, and undergoing a quite legitimate writeback.
  484. * Otherwise, it is journaled, and we don't expect dirty buffers
  485. * in that state (the buffers should be marked JBD_Dirty
  486. * instead.) So either the IO is being done under our own
  487. * control and this is a bug, or it's a third party IO such as
  488. * dump(8) (which may leave the buffer scheduled for read ---
  489. * ie. locked but not dirty) or tune2fs (which may actually have
  490. * the buffer dirtied, ugh.) */
  491. if (buffer_dirty(bh)) {
  492. /*
  493. * First question: is this buffer already part of the current
  494. * transaction or the existing committing transaction?
  495. */
  496. if (jh->b_transaction) {
  497. J_ASSERT_JH(jh,
  498. jh->b_transaction == transaction ||
  499. jh->b_transaction ==
  500. journal->j_committing_transaction);
  501. if (jh->b_next_transaction)
  502. J_ASSERT_JH(jh, jh->b_next_transaction ==
  503. transaction);
  504. }
  505. /*
  506. * In any case we need to clean the dirty flag and we must
  507. * do it under the buffer lock to be sure we don't race
  508. * with running write-out.
  509. */
  510. JBUFFER_TRACE(jh, "Unexpected dirty buffer");
  511. jbd_unexpected_dirty_buffer(jh);
  512. }
  513. unlock_buffer(bh);
  514. error = -EROFS;
  515. if (is_handle_aborted(handle)) {
  516. jbd_unlock_bh_state(bh);
  517. goto out;
  518. }
  519. error = 0;
  520. /*
  521. * The buffer is already part of this transaction if b_transaction or
  522. * b_next_transaction points to it
  523. */
  524. if (jh->b_transaction == transaction ||
  525. jh->b_next_transaction == transaction)
  526. goto done;
  527. /*
  528. * this is the first time this transaction is touching this buffer,
  529. * reset the modified flag
  530. */
  531. jh->b_modified = 0;
  532. /*
  533. * If there is already a copy-out version of this buffer, then we don't
  534. * need to make another one
  535. */
  536. if (jh->b_frozen_data) {
  537. JBUFFER_TRACE(jh, "has frozen data");
  538. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  539. jh->b_next_transaction = transaction;
  540. goto done;
  541. }
  542. /* Is there data here we need to preserve? */
  543. if (jh->b_transaction && jh->b_transaction != transaction) {
  544. JBUFFER_TRACE(jh, "owned by older transaction");
  545. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  546. J_ASSERT_JH(jh, jh->b_transaction ==
  547. journal->j_committing_transaction);
  548. /* There is one case we have to be very careful about.
  549. * If the committing transaction is currently writing
  550. * this buffer out to disk and has NOT made a copy-out,
  551. * then we cannot modify the buffer contents at all
  552. * right now. The essence of copy-out is that it is the
  553. * extra copy, not the primary copy, which gets
  554. * journaled. If the primary copy is already going to
  555. * disk then we cannot do copy-out here. */
  556. if (jh->b_jlist == BJ_Shadow) {
  557. DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
  558. wait_queue_head_t *wqh;
  559. wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
  560. JBUFFER_TRACE(jh, "on shadow: sleep");
  561. jbd_unlock_bh_state(bh);
  562. /* commit wakes up all shadow buffers after IO */
  563. for ( ; ; ) {
  564. prepare_to_wait(wqh, &wait.wait,
  565. TASK_UNINTERRUPTIBLE);
  566. if (jh->b_jlist != BJ_Shadow)
  567. break;
  568. schedule();
  569. }
  570. finish_wait(wqh, &wait.wait);
  571. goto repeat;
  572. }
  573. /* Only do the copy if the currently-owning transaction
  574. * still needs it. If it is on the Forget list, the
  575. * committing transaction is past that stage. The
  576. * buffer had better remain locked during the kmalloc,
  577. * but that should be true --- we hold the journal lock
  578. * still and the buffer is already on the BUF_JOURNAL
  579. * list so won't be flushed.
  580. *
  581. * Subtle point, though: if this is a get_undo_access,
  582. * then we will be relying on the frozen_data to contain
  583. * the new value of the committed_data record after the
  584. * transaction, so we HAVE to force the frozen_data copy
  585. * in that case. */
  586. if (jh->b_jlist != BJ_Forget || force_copy) {
  587. JBUFFER_TRACE(jh, "generate frozen data");
  588. if (!frozen_buffer) {
  589. JBUFFER_TRACE(jh, "allocate memory for buffer");
  590. jbd_unlock_bh_state(bh);
  591. frozen_buffer =
  592. jbd_alloc(jh2bh(jh)->b_size,
  593. GFP_NOFS);
  594. if (!frozen_buffer) {
  595. printk(KERN_EMERG
  596. "%s: OOM for frozen_buffer\n",
  597. __func__);
  598. JBUFFER_TRACE(jh, "oom!");
  599. error = -ENOMEM;
  600. jbd_lock_bh_state(bh);
  601. goto done;
  602. }
  603. goto repeat;
  604. }
  605. jh->b_frozen_data = frozen_buffer;
  606. frozen_buffer = NULL;
  607. need_copy = 1;
  608. }
  609. jh->b_next_transaction = transaction;
  610. }
  611. /*
  612. * Finally, if the buffer is not journaled right now, we need to make
  613. * sure it doesn't get written to disk before the caller actually
  614. * commits the new data
  615. */
  616. if (!jh->b_transaction) {
  617. JBUFFER_TRACE(jh, "no transaction");
  618. J_ASSERT_JH(jh, !jh->b_next_transaction);
  619. jh->b_transaction = transaction;
  620. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  621. spin_lock(&journal->j_list_lock);
  622. __journal_file_buffer(jh, transaction, BJ_Reserved);
  623. spin_unlock(&journal->j_list_lock);
  624. }
  625. done:
  626. if (need_copy) {
  627. struct page *page;
  628. int offset;
  629. char *source;
  630. J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
  631. "Possible IO failure.\n");
  632. page = jh2bh(jh)->b_page;
  633. offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
  634. source = kmap_atomic(page, KM_USER0);
  635. memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
  636. kunmap_atomic(source, KM_USER0);
  637. }
  638. jbd_unlock_bh_state(bh);
  639. /*
  640. * If we are about to journal a buffer, then any revoke pending on it is
  641. * no longer valid
  642. */
  643. journal_cancel_revoke(handle, jh);
  644. out:
  645. if (unlikely(frozen_buffer)) /* It's usually NULL */
  646. jbd_free(frozen_buffer, bh->b_size);
  647. JBUFFER_TRACE(jh, "exit");
  648. return error;
  649. }
  650. /**
  651. * int journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
  652. * @handle: transaction to add buffer modifications to
  653. * @bh: bh to be used for metadata writes
  654. * @credits: variable that will receive credits for the buffer
  655. *
  656. * Returns an error code or 0 on success.
  657. *
  658. * In full data journalling mode the buffer may be of type BJ_AsyncData,
  659. * because we're write()ing a buffer which is also part of a shared mapping.
  660. */
  661. int journal_get_write_access(handle_t *handle, struct buffer_head *bh)
  662. {
  663. struct journal_head *jh = journal_add_journal_head(bh);
  664. int rc;
  665. /* We do not want to get caught playing with fields which the
  666. * log thread also manipulates. Make sure that the buffer
  667. * completes any outstanding IO before proceeding. */
  668. rc = do_get_write_access(handle, jh, 0);
  669. journal_put_journal_head(jh);
  670. return rc;
  671. }
  672. /*
  673. * When the user wants to journal a newly created buffer_head
  674. * (ie. getblk() returned a new buffer and we are going to populate it
  675. * manually rather than reading off disk), then we need to keep the
  676. * buffer_head locked until it has been completely filled with new
  677. * data. In this case, we should be able to make the assertion that
  678. * the bh is not already part of an existing transaction.
  679. *
  680. * The buffer should already be locked by the caller by this point.
  681. * There is no lock ranking violation: it was a newly created,
  682. * unlocked buffer beforehand. */
  683. /**
  684. * int journal_get_create_access () - notify intent to use newly created bh
  685. * @handle: transaction to new buffer to
  686. * @bh: new buffer.
  687. *
  688. * Call this if you create a new bh.
  689. */
  690. int journal_get_create_access(handle_t *handle, struct buffer_head *bh)
  691. {
  692. transaction_t *transaction = handle->h_transaction;
  693. journal_t *journal = transaction->t_journal;
  694. struct journal_head *jh = journal_add_journal_head(bh);
  695. int err;
  696. jbd_debug(5, "journal_head %p\n", jh);
  697. err = -EROFS;
  698. if (is_handle_aborted(handle))
  699. goto out;
  700. err = 0;
  701. JBUFFER_TRACE(jh, "entry");
  702. /*
  703. * The buffer may already belong to this transaction due to pre-zeroing
  704. * in the filesystem's new_block code. It may also be on the previous,
  705. * committing transaction's lists, but it HAS to be in Forget state in
  706. * that case: the transaction must have deleted the buffer for it to be
  707. * reused here.
  708. */
  709. jbd_lock_bh_state(bh);
  710. spin_lock(&journal->j_list_lock);
  711. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  712. jh->b_transaction == NULL ||
  713. (jh->b_transaction == journal->j_committing_transaction &&
  714. jh->b_jlist == BJ_Forget)));
  715. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  716. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  717. if (jh->b_transaction == NULL) {
  718. jh->b_transaction = transaction;
  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. journal_put_journal_head(jh);
  741. out:
  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. if (jh->b_transaction != NULL) {
  949. JBUFFER_TRACE(jh, "unfile from commit");
  950. __journal_temp_unlink_buffer(jh);
  951. /* It still points to the committing
  952. * transaction; move it to this one so
  953. * that the refile assert checks are
  954. * happy. */
  955. jh->b_transaction = handle->h_transaction;
  956. }
  957. /* The buffer will be refiled below */
  958. }
  959. /*
  960. * Special case --- the buffer might actually have been
  961. * allocated and then immediately deallocated in the previous,
  962. * committing transaction, so might still be left on that
  963. * transaction's metadata lists.
  964. */
  965. if (jh->b_jlist != BJ_SyncData && jh->b_jlist != BJ_Locked) {
  966. JBUFFER_TRACE(jh, "not on correct data list: unfile");
  967. J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow);
  968. __journal_temp_unlink_buffer(jh);
  969. jh->b_transaction = handle->h_transaction;
  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. journal_remove_journal_head(bh);
  1155. __brelse(bh);
  1156. if (!buffer_jbd(bh)) {
  1157. spin_unlock(&journal->j_list_lock);
  1158. jbd_unlock_bh_state(bh);
  1159. __bforget(bh);
  1160. goto drop;
  1161. }
  1162. }
  1163. } else if (jh->b_transaction) {
  1164. J_ASSERT_JH(jh, (jh->b_transaction ==
  1165. journal->j_committing_transaction));
  1166. /* However, if the buffer is still owned by a prior
  1167. * (committing) transaction, we can't drop it yet... */
  1168. JBUFFER_TRACE(jh, "belongs to older transaction");
  1169. /* ... but we CAN drop it from the new transaction if we
  1170. * have also modified it since the original commit. */
  1171. if (jh->b_next_transaction) {
  1172. J_ASSERT(jh->b_next_transaction == transaction);
  1173. jh->b_next_transaction = NULL;
  1174. /*
  1175. * only drop a reference if this transaction modified
  1176. * the buffer
  1177. */
  1178. if (was_modified)
  1179. drop_reserve = 1;
  1180. }
  1181. }
  1182. not_jbd:
  1183. spin_unlock(&journal->j_list_lock);
  1184. jbd_unlock_bh_state(bh);
  1185. __brelse(bh);
  1186. drop:
  1187. if (drop_reserve) {
  1188. /* no need to reserve log space for this block -bzzz */
  1189. handle->h_buffer_credits++;
  1190. }
  1191. return err;
  1192. }
  1193. /**
  1194. * int journal_stop() - complete a transaction
  1195. * @handle: tranaction to complete.
  1196. *
  1197. * All done for a particular handle.
  1198. *
  1199. * There is not much action needed here. We just return any remaining
  1200. * buffer credits to the transaction and remove the handle. The only
  1201. * complication is that we need to start a commit operation if the
  1202. * filesystem is marked for synchronous update.
  1203. *
  1204. * journal_stop itself will not usually return an error, but it may
  1205. * do so in unusual circumstances. In particular, expect it to
  1206. * return -EIO if a journal_abort has been executed since the
  1207. * transaction began.
  1208. */
  1209. int journal_stop(handle_t *handle)
  1210. {
  1211. transaction_t *transaction = handle->h_transaction;
  1212. journal_t *journal = transaction->t_journal;
  1213. int old_handle_count, err;
  1214. pid_t pid;
  1215. J_ASSERT(journal_current_handle() == handle);
  1216. if (is_handle_aborted(handle))
  1217. err = -EIO;
  1218. else {
  1219. J_ASSERT(transaction->t_updates > 0);
  1220. err = 0;
  1221. }
  1222. if (--handle->h_ref > 0) {
  1223. jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1224. handle->h_ref);
  1225. return err;
  1226. }
  1227. jbd_debug(4, "Handle %p going down\n", handle);
  1228. /*
  1229. * Implement synchronous transaction batching. If the handle
  1230. * was synchronous, don't force a commit immediately. Let's
  1231. * yield and let another thread piggyback onto this transaction.
  1232. * Keep doing that while new threads continue to arrive.
  1233. * It doesn't cost much - we're about to run a commit and sleep
  1234. * on IO anyway. Speeds up many-threaded, many-dir operations
  1235. * by 30x or more...
  1236. *
  1237. * But don't do this if this process was the most recent one to
  1238. * perform a synchronous write. We do this to detect the case where a
  1239. * single process is doing a stream of sync writes. No point in waiting
  1240. * for joiners in that case.
  1241. */
  1242. pid = current->pid;
  1243. if (handle->h_sync && journal->j_last_sync_writer != pid) {
  1244. journal->j_last_sync_writer = pid;
  1245. do {
  1246. old_handle_count = transaction->t_handle_count;
  1247. schedule_timeout_uninterruptible(1);
  1248. } while (old_handle_count != transaction->t_handle_count);
  1249. }
  1250. current->journal_info = NULL;
  1251. spin_lock(&journal->j_state_lock);
  1252. spin_lock(&transaction->t_handle_lock);
  1253. transaction->t_outstanding_credits -= handle->h_buffer_credits;
  1254. transaction->t_updates--;
  1255. if (!transaction->t_updates) {
  1256. wake_up(&journal->j_wait_updates);
  1257. if (journal->j_barrier_count)
  1258. wake_up(&journal->j_wait_transaction_locked);
  1259. }
  1260. /*
  1261. * If the handle is marked SYNC, we need to set another commit
  1262. * going! We also want to force a commit if the current
  1263. * transaction is occupying too much of the log, or if the
  1264. * transaction is too old now.
  1265. */
  1266. if (handle->h_sync ||
  1267. transaction->t_outstanding_credits >
  1268. journal->j_max_transaction_buffers ||
  1269. time_after_eq(jiffies, transaction->t_expires)) {
  1270. /* Do this even for aborted journals: an abort still
  1271. * completes the commit thread, it just doesn't write
  1272. * anything to disk. */
  1273. tid_t tid = transaction->t_tid;
  1274. spin_unlock(&transaction->t_handle_lock);
  1275. jbd_debug(2, "transaction too old, requesting commit for "
  1276. "handle %p\n", handle);
  1277. /* This is non-blocking */
  1278. __log_start_commit(journal, transaction->t_tid);
  1279. spin_unlock(&journal->j_state_lock);
  1280. /*
  1281. * Special case: JFS_SYNC synchronous updates require us
  1282. * to wait for the commit to complete.
  1283. */
  1284. if (handle->h_sync && !(current->flags & PF_MEMALLOC))
  1285. err = log_wait_commit(journal, tid);
  1286. } else {
  1287. spin_unlock(&transaction->t_handle_lock);
  1288. spin_unlock(&journal->j_state_lock);
  1289. }
  1290. lock_map_release(&handle->h_lockdep_map);
  1291. jbd_free_handle(handle);
  1292. return err;
  1293. }
  1294. /**
  1295. * int journal_force_commit() - force any uncommitted transactions
  1296. * @journal: journal to force
  1297. *
  1298. * For synchronous operations: force any uncommitted transactions
  1299. * to disk. May seem kludgy, but it reuses all the handle batching
  1300. * code in a very simple manner.
  1301. */
  1302. int journal_force_commit(journal_t *journal)
  1303. {
  1304. handle_t *handle;
  1305. int ret;
  1306. handle = journal_start(journal, 1);
  1307. if (IS_ERR(handle)) {
  1308. ret = PTR_ERR(handle);
  1309. } else {
  1310. handle->h_sync = 1;
  1311. ret = journal_stop(handle);
  1312. }
  1313. return ret;
  1314. }
  1315. /*
  1316. *
  1317. * List management code snippets: various functions for manipulating the
  1318. * transaction buffer lists.
  1319. *
  1320. */
  1321. /*
  1322. * Append a buffer to a transaction list, given the transaction's list head
  1323. * pointer.
  1324. *
  1325. * j_list_lock is held.
  1326. *
  1327. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1328. */
  1329. static inline void
  1330. __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
  1331. {
  1332. if (!*list) {
  1333. jh->b_tnext = jh->b_tprev = jh;
  1334. *list = jh;
  1335. } else {
  1336. /* Insert at the tail of the list to preserve order */
  1337. struct journal_head *first = *list, *last = first->b_tprev;
  1338. jh->b_tprev = last;
  1339. jh->b_tnext = first;
  1340. last->b_tnext = first->b_tprev = jh;
  1341. }
  1342. }
  1343. /*
  1344. * Remove a buffer from a transaction list, given the transaction's list
  1345. * head pointer.
  1346. *
  1347. * Called with j_list_lock held, and the journal may not be locked.
  1348. *
  1349. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1350. */
  1351. static inline void
  1352. __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
  1353. {
  1354. if (*list == jh) {
  1355. *list = jh->b_tnext;
  1356. if (*list == jh)
  1357. *list = NULL;
  1358. }
  1359. jh->b_tprev->b_tnext = jh->b_tnext;
  1360. jh->b_tnext->b_tprev = jh->b_tprev;
  1361. }
  1362. /*
  1363. * Remove a buffer from the appropriate transaction list.
  1364. *
  1365. * Note that this function can *change* the value of
  1366. * bh->b_transaction->t_sync_datalist, t_buffers, t_forget,
  1367. * t_iobuf_list, t_shadow_list, t_log_list or t_reserved_list. If the caller
  1368. * is holding onto a copy of one of thee pointers, it could go bad.
  1369. * Generally the caller needs to re-read the pointer from the transaction_t.
  1370. *
  1371. * Called under j_list_lock. The journal may not be locked.
  1372. */
  1373. static void __journal_temp_unlink_buffer(struct journal_head *jh)
  1374. {
  1375. struct journal_head **list = NULL;
  1376. transaction_t *transaction;
  1377. struct buffer_head *bh = jh2bh(jh);
  1378. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1379. transaction = jh->b_transaction;
  1380. if (transaction)
  1381. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1382. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1383. if (jh->b_jlist != BJ_None)
  1384. J_ASSERT_JH(jh, transaction != NULL);
  1385. switch (jh->b_jlist) {
  1386. case BJ_None:
  1387. return;
  1388. case BJ_SyncData:
  1389. list = &transaction->t_sync_datalist;
  1390. break;
  1391. case BJ_Metadata:
  1392. transaction->t_nr_buffers--;
  1393. J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
  1394. list = &transaction->t_buffers;
  1395. break;
  1396. case BJ_Forget:
  1397. list = &transaction->t_forget;
  1398. break;
  1399. case BJ_IO:
  1400. list = &transaction->t_iobuf_list;
  1401. break;
  1402. case BJ_Shadow:
  1403. list = &transaction->t_shadow_list;
  1404. break;
  1405. case BJ_LogCtl:
  1406. list = &transaction->t_log_list;
  1407. break;
  1408. case BJ_Reserved:
  1409. list = &transaction->t_reserved_list;
  1410. break;
  1411. case BJ_Locked:
  1412. list = &transaction->t_locked_list;
  1413. break;
  1414. }
  1415. __blist_del_buffer(list, jh);
  1416. jh->b_jlist = BJ_None;
  1417. if (test_clear_buffer_jbddirty(bh))
  1418. mark_buffer_dirty(bh); /* Expose it to the VM */
  1419. }
  1420. void __journal_unfile_buffer(struct journal_head *jh)
  1421. {
  1422. __journal_temp_unlink_buffer(jh);
  1423. jh->b_transaction = NULL;
  1424. }
  1425. void journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
  1426. {
  1427. jbd_lock_bh_state(jh2bh(jh));
  1428. spin_lock(&journal->j_list_lock);
  1429. __journal_unfile_buffer(jh);
  1430. spin_unlock(&journal->j_list_lock);
  1431. jbd_unlock_bh_state(jh2bh(jh));
  1432. }
  1433. /*
  1434. * Called from journal_try_to_free_buffers().
  1435. *
  1436. * Called under jbd_lock_bh_state(bh)
  1437. */
  1438. static void
  1439. __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
  1440. {
  1441. struct journal_head *jh;
  1442. jh = bh2jh(bh);
  1443. if (buffer_locked(bh) || buffer_dirty(bh))
  1444. goto out;
  1445. if (jh->b_next_transaction != NULL)
  1446. goto out;
  1447. spin_lock(&journal->j_list_lock);
  1448. if (jh->b_transaction != NULL && jh->b_cp_transaction == NULL) {
  1449. if (jh->b_jlist == BJ_SyncData || jh->b_jlist == BJ_Locked) {
  1450. /* A written-back ordered data buffer */
  1451. JBUFFER_TRACE(jh, "release data");
  1452. __journal_unfile_buffer(jh);
  1453. journal_remove_journal_head(bh);
  1454. __brelse(bh);
  1455. }
  1456. } else if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
  1457. /* written-back checkpointed metadata buffer */
  1458. if (jh->b_jlist == BJ_None) {
  1459. JBUFFER_TRACE(jh, "remove from checkpoint list");
  1460. __journal_remove_checkpoint(jh);
  1461. journal_remove_journal_head(bh);
  1462. __brelse(bh);
  1463. }
  1464. }
  1465. spin_unlock(&journal->j_list_lock);
  1466. out:
  1467. return;
  1468. }
  1469. /*
  1470. * journal_try_to_free_buffers() could race with journal_commit_transaction()
  1471. * The latter might still hold the a count on buffers when inspecting
  1472. * them on t_syncdata_list or t_locked_list.
  1473. *
  1474. * journal_try_to_free_buffers() will call this function to
  1475. * wait for the current transaction to finish syncing data buffers, before
  1476. * tryinf to free that buffer.
  1477. *
  1478. * Called with journal->j_state_lock held.
  1479. */
  1480. static void journal_wait_for_transaction_sync_data(journal_t *journal)
  1481. {
  1482. transaction_t *transaction = NULL;
  1483. tid_t tid;
  1484. spin_lock(&journal->j_state_lock);
  1485. transaction = journal->j_committing_transaction;
  1486. if (!transaction) {
  1487. spin_unlock(&journal->j_state_lock);
  1488. return;
  1489. }
  1490. tid = transaction->t_tid;
  1491. spin_unlock(&journal->j_state_lock);
  1492. log_wait_commit(journal, tid);
  1493. }
  1494. /**
  1495. * int journal_try_to_free_buffers() - try to free page buffers.
  1496. * @journal: journal for operation
  1497. * @page: to try and free
  1498. * @gfp_mask: we use the mask to detect how hard should we try to release
  1499. * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
  1500. * release the buffers.
  1501. *
  1502. *
  1503. * For all the buffers on this page,
  1504. * if they are fully written out ordered data, move them onto BUF_CLEAN
  1505. * so try_to_free_buffers() can reap them.
  1506. *
  1507. * This function returns non-zero if we wish try_to_free_buffers()
  1508. * to be called. We do this if the page is releasable by try_to_free_buffers().
  1509. * We also do it if the page has locked or dirty buffers and the caller wants
  1510. * us to perform sync or async writeout.
  1511. *
  1512. * This complicates JBD locking somewhat. We aren't protected by the
  1513. * BKL here. We wish to remove the buffer from its committing or
  1514. * running transaction's ->t_datalist via __journal_unfile_buffer.
  1515. *
  1516. * This may *change* the value of transaction_t->t_datalist, so anyone
  1517. * who looks at t_datalist needs to lock against this function.
  1518. *
  1519. * Even worse, someone may be doing a journal_dirty_data on this
  1520. * buffer. So we need to lock against that. journal_dirty_data()
  1521. * will come out of the lock with the buffer dirty, which makes it
  1522. * ineligible for release here.
  1523. *
  1524. * Who else is affected by this? hmm... Really the only contender
  1525. * is do_get_write_access() - it could be looking at the buffer while
  1526. * journal_try_to_free_buffer() is changing its state. But that
  1527. * cannot happen because we never reallocate freed data as metadata
  1528. * while the data is part of a transaction. Yes?
  1529. *
  1530. * Return 0 on failure, 1 on success
  1531. */
  1532. int journal_try_to_free_buffers(journal_t *journal,
  1533. struct page *page, gfp_t gfp_mask)
  1534. {
  1535. struct buffer_head *head;
  1536. struct buffer_head *bh;
  1537. int ret = 0;
  1538. J_ASSERT(PageLocked(page));
  1539. head = page_buffers(page);
  1540. bh = head;
  1541. do {
  1542. struct journal_head *jh;
  1543. /*
  1544. * We take our own ref against the journal_head here to avoid
  1545. * having to add tons of locking around each instance of
  1546. * journal_remove_journal_head() and journal_put_journal_head().
  1547. */
  1548. jh = journal_grab_journal_head(bh);
  1549. if (!jh)
  1550. continue;
  1551. jbd_lock_bh_state(bh);
  1552. __journal_try_to_free_buffer(journal, bh);
  1553. journal_put_journal_head(jh);
  1554. jbd_unlock_bh_state(bh);
  1555. if (buffer_jbd(bh))
  1556. goto busy;
  1557. } while ((bh = bh->b_this_page) != head);
  1558. ret = try_to_free_buffers(page);
  1559. /*
  1560. * There are a number of places where journal_try_to_free_buffers()
  1561. * could race with journal_commit_transaction(), the later still
  1562. * holds the reference to the buffers to free while processing them.
  1563. * try_to_free_buffers() failed to free those buffers. Some of the
  1564. * caller of releasepage() request page buffers to be dropped, otherwise
  1565. * treat the fail-to-free as errors (such as generic_file_direct_IO())
  1566. *
  1567. * So, if the caller of try_to_release_page() wants the synchronous
  1568. * behaviour(i.e make sure buffers are dropped upon return),
  1569. * let's wait for the current transaction to finish flush of
  1570. * dirty data buffers, then try to free those buffers again,
  1571. * with the journal locked.
  1572. */
  1573. if (ret == 0 && (gfp_mask & __GFP_WAIT) && (gfp_mask & __GFP_FS)) {
  1574. journal_wait_for_transaction_sync_data(journal);
  1575. ret = try_to_free_buffers(page);
  1576. }
  1577. busy:
  1578. return ret;
  1579. }
  1580. /*
  1581. * This buffer is no longer needed. If it is on an older transaction's
  1582. * checkpoint list we need to record it on this transaction's forget list
  1583. * to pin this buffer (and hence its checkpointing transaction) down until
  1584. * this transaction commits. If the buffer isn't on a checkpoint list, we
  1585. * release it.
  1586. * Returns non-zero if JBD no longer has an interest in the buffer.
  1587. *
  1588. * Called under j_list_lock.
  1589. *
  1590. * Called under jbd_lock_bh_state(bh).
  1591. */
  1592. static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
  1593. {
  1594. int may_free = 1;
  1595. struct buffer_head *bh = jh2bh(jh);
  1596. __journal_unfile_buffer(jh);
  1597. if (jh->b_cp_transaction) {
  1598. JBUFFER_TRACE(jh, "on running+cp transaction");
  1599. __journal_file_buffer(jh, transaction, BJ_Forget);
  1600. clear_buffer_jbddirty(bh);
  1601. may_free = 0;
  1602. } else {
  1603. JBUFFER_TRACE(jh, "on running transaction");
  1604. journal_remove_journal_head(bh);
  1605. __brelse(bh);
  1606. }
  1607. return may_free;
  1608. }
  1609. /*
  1610. * journal_invalidatepage
  1611. *
  1612. * This code is tricky. It has a number of cases to deal with.
  1613. *
  1614. * There are two invariants which this code relies on:
  1615. *
  1616. * i_size must be updated on disk before we start calling invalidatepage on the
  1617. * data.
  1618. *
  1619. * This is done in ext3 by defining an ext3_setattr method which
  1620. * updates i_size before truncate gets going. By maintaining this
  1621. * invariant, we can be sure that it is safe to throw away any buffers
  1622. * attached to the current transaction: once the transaction commits,
  1623. * we know that the data will not be needed.
  1624. *
  1625. * Note however that we can *not* throw away data belonging to the
  1626. * previous, committing transaction!
  1627. *
  1628. * Any disk blocks which *are* part of the previous, committing
  1629. * transaction (and which therefore cannot be discarded immediately) are
  1630. * not going to be reused in the new running transaction
  1631. *
  1632. * The bitmap committed_data images guarantee this: any block which is
  1633. * allocated in one transaction and removed in the next will be marked
  1634. * as in-use in the committed_data bitmap, so cannot be reused until
  1635. * the next transaction to delete the block commits. This means that
  1636. * leaving committing buffers dirty is quite safe: the disk blocks
  1637. * cannot be reallocated to a different file and so buffer aliasing is
  1638. * not possible.
  1639. *
  1640. *
  1641. * The above applies mainly to ordered data mode. In writeback mode we
  1642. * don't make guarantees about the order in which data hits disk --- in
  1643. * particular we don't guarantee that new dirty data is flushed before
  1644. * transaction commit --- so it is always safe just to discard data
  1645. * immediately in that mode. --sct
  1646. */
  1647. /*
  1648. * The journal_unmap_buffer helper function returns zero if the buffer
  1649. * concerned remains pinned as an anonymous buffer belonging to an older
  1650. * transaction.
  1651. *
  1652. * We're outside-transaction here. Either or both of j_running_transaction
  1653. * and j_committing_transaction may be NULL.
  1654. */
  1655. static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
  1656. {
  1657. transaction_t *transaction;
  1658. struct journal_head *jh;
  1659. int may_free = 1;
  1660. int ret;
  1661. BUFFER_TRACE(bh, "entry");
  1662. /*
  1663. * It is safe to proceed here without the j_list_lock because the
  1664. * buffers cannot be stolen by try_to_free_buffers as long as we are
  1665. * holding the page lock. --sct
  1666. */
  1667. if (!buffer_jbd(bh))
  1668. goto zap_buffer_unlocked;
  1669. spin_lock(&journal->j_state_lock);
  1670. jbd_lock_bh_state(bh);
  1671. spin_lock(&journal->j_list_lock);
  1672. jh = journal_grab_journal_head(bh);
  1673. if (!jh)
  1674. goto zap_buffer_no_jh;
  1675. transaction = jh->b_transaction;
  1676. if (transaction == NULL) {
  1677. /* First case: not on any transaction. If it
  1678. * has no checkpoint link, then we can zap it:
  1679. * it's a writeback-mode buffer so we don't care
  1680. * if it hits disk safely. */
  1681. if (!jh->b_cp_transaction) {
  1682. JBUFFER_TRACE(jh, "not on any transaction: zap");
  1683. goto zap_buffer;
  1684. }
  1685. if (!buffer_dirty(bh)) {
  1686. /* bdflush has written it. We can drop it now */
  1687. goto zap_buffer;
  1688. }
  1689. /* OK, it must be in the journal but still not
  1690. * written fully to disk: it's metadata or
  1691. * journaled data... */
  1692. if (journal->j_running_transaction) {
  1693. /* ... and once the current transaction has
  1694. * committed, the buffer won't be needed any
  1695. * longer. */
  1696. JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
  1697. ret = __dispose_buffer(jh,
  1698. journal->j_running_transaction);
  1699. journal_put_journal_head(jh);
  1700. spin_unlock(&journal->j_list_lock);
  1701. jbd_unlock_bh_state(bh);
  1702. spin_unlock(&journal->j_state_lock);
  1703. return ret;
  1704. } else {
  1705. /* There is no currently-running transaction. So the
  1706. * orphan record which we wrote for this file must have
  1707. * passed into commit. We must attach this buffer to
  1708. * the committing transaction, if it exists. */
  1709. if (journal->j_committing_transaction) {
  1710. JBUFFER_TRACE(jh, "give to committing trans");
  1711. ret = __dispose_buffer(jh,
  1712. journal->j_committing_transaction);
  1713. journal_put_journal_head(jh);
  1714. spin_unlock(&journal->j_list_lock);
  1715. jbd_unlock_bh_state(bh);
  1716. spin_unlock(&journal->j_state_lock);
  1717. return ret;
  1718. } else {
  1719. /* The orphan record's transaction has
  1720. * committed. We can cleanse this buffer */
  1721. clear_buffer_jbddirty(bh);
  1722. goto zap_buffer;
  1723. }
  1724. }
  1725. } else if (transaction == journal->j_committing_transaction) {
  1726. JBUFFER_TRACE(jh, "on committing transaction");
  1727. if (jh->b_jlist == BJ_Locked) {
  1728. /*
  1729. * The buffer is on the committing transaction's locked
  1730. * list. We have the buffer locked, so I/O has
  1731. * completed. So we can nail the buffer now.
  1732. */
  1733. may_free = __dispose_buffer(jh, transaction);
  1734. goto zap_buffer;
  1735. }
  1736. /*
  1737. * If it is committing, we simply cannot touch it. We
  1738. * can remove it's next_transaction pointer from the
  1739. * running transaction if that is set, but nothing
  1740. * else. */
  1741. set_buffer_freed(bh);
  1742. if (jh->b_next_transaction) {
  1743. J_ASSERT(jh->b_next_transaction ==
  1744. journal->j_running_transaction);
  1745. jh->b_next_transaction = NULL;
  1746. }
  1747. journal_put_journal_head(jh);
  1748. spin_unlock(&journal->j_list_lock);
  1749. jbd_unlock_bh_state(bh);
  1750. spin_unlock(&journal->j_state_lock);
  1751. return 0;
  1752. } else {
  1753. /* Good, the buffer belongs to the running transaction.
  1754. * We are writing our own transaction's data, not any
  1755. * previous one's, so it is safe to throw it away
  1756. * (remember that we expect the filesystem to have set
  1757. * i_size already for this truncate so recovery will not
  1758. * expose the disk blocks we are discarding here.) */
  1759. J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
  1760. JBUFFER_TRACE(jh, "on running transaction");
  1761. may_free = __dispose_buffer(jh, transaction);
  1762. }
  1763. zap_buffer:
  1764. journal_put_journal_head(jh);
  1765. zap_buffer_no_jh:
  1766. spin_unlock(&journal->j_list_lock);
  1767. jbd_unlock_bh_state(bh);
  1768. spin_unlock(&journal->j_state_lock);
  1769. zap_buffer_unlocked:
  1770. clear_buffer_dirty(bh);
  1771. J_ASSERT_BH(bh, !buffer_jbddirty(bh));
  1772. clear_buffer_mapped(bh);
  1773. clear_buffer_req(bh);
  1774. clear_buffer_new(bh);
  1775. bh->b_bdev = NULL;
  1776. return may_free;
  1777. }
  1778. /**
  1779. * void journal_invalidatepage() - invalidate a journal page
  1780. * @journal: journal to use for flush
  1781. * @page: page to flush
  1782. * @offset: length of page to invalidate.
  1783. *
  1784. * Reap page buffers containing data after offset in page.
  1785. */
  1786. void journal_invalidatepage(journal_t *journal,
  1787. struct page *page,
  1788. unsigned long offset)
  1789. {
  1790. struct buffer_head *head, *bh, *next;
  1791. unsigned int curr_off = 0;
  1792. int may_free = 1;
  1793. if (!PageLocked(page))
  1794. BUG();
  1795. if (!page_has_buffers(page))
  1796. return;
  1797. /* We will potentially be playing with lists other than just the
  1798. * data lists (especially for journaled data mode), so be
  1799. * cautious in our locking. */
  1800. head = bh = page_buffers(page);
  1801. do {
  1802. unsigned int next_off = curr_off + bh->b_size;
  1803. next = bh->b_this_page;
  1804. if (offset <= curr_off) {
  1805. /* This block is wholly outside the truncation point */
  1806. lock_buffer(bh);
  1807. may_free &= journal_unmap_buffer(journal, bh);
  1808. unlock_buffer(bh);
  1809. }
  1810. curr_off = next_off;
  1811. bh = next;
  1812. } while (bh != head);
  1813. if (!offset) {
  1814. if (may_free && try_to_free_buffers(page))
  1815. J_ASSERT(!page_has_buffers(page));
  1816. }
  1817. }
  1818. /*
  1819. * File a buffer on the given transaction list.
  1820. */
  1821. void __journal_file_buffer(struct journal_head *jh,
  1822. transaction_t *transaction, int jlist)
  1823. {
  1824. struct journal_head **list = NULL;
  1825. int was_dirty = 0;
  1826. struct buffer_head *bh = jh2bh(jh);
  1827. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1828. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1829. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1830. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  1831. jh->b_transaction == NULL);
  1832. if (jh->b_transaction && jh->b_jlist == jlist)
  1833. return;
  1834. /* The following list of buffer states needs to be consistent
  1835. * with __jbd_unexpected_dirty_buffer()'s handling of dirty
  1836. * state. */
  1837. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  1838. jlist == BJ_Shadow || jlist == BJ_Forget) {
  1839. if (test_clear_buffer_dirty(bh) ||
  1840. test_clear_buffer_jbddirty(bh))
  1841. was_dirty = 1;
  1842. }
  1843. if (jh->b_transaction)
  1844. __journal_temp_unlink_buffer(jh);
  1845. jh->b_transaction = transaction;
  1846. switch (jlist) {
  1847. case BJ_None:
  1848. J_ASSERT_JH(jh, !jh->b_committed_data);
  1849. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1850. return;
  1851. case BJ_SyncData:
  1852. list = &transaction->t_sync_datalist;
  1853. break;
  1854. case BJ_Metadata:
  1855. transaction->t_nr_buffers++;
  1856. list = &transaction->t_buffers;
  1857. break;
  1858. case BJ_Forget:
  1859. list = &transaction->t_forget;
  1860. break;
  1861. case BJ_IO:
  1862. list = &transaction->t_iobuf_list;
  1863. break;
  1864. case BJ_Shadow:
  1865. list = &transaction->t_shadow_list;
  1866. break;
  1867. case BJ_LogCtl:
  1868. list = &transaction->t_log_list;
  1869. break;
  1870. case BJ_Reserved:
  1871. list = &transaction->t_reserved_list;
  1872. break;
  1873. case BJ_Locked:
  1874. list = &transaction->t_locked_list;
  1875. break;
  1876. }
  1877. __blist_add_buffer(list, jh);
  1878. jh->b_jlist = jlist;
  1879. if (was_dirty)
  1880. set_buffer_jbddirty(bh);
  1881. }
  1882. void journal_file_buffer(struct journal_head *jh,
  1883. transaction_t *transaction, int jlist)
  1884. {
  1885. jbd_lock_bh_state(jh2bh(jh));
  1886. spin_lock(&transaction->t_journal->j_list_lock);
  1887. __journal_file_buffer(jh, transaction, jlist);
  1888. spin_unlock(&transaction->t_journal->j_list_lock);
  1889. jbd_unlock_bh_state(jh2bh(jh));
  1890. }
  1891. /*
  1892. * Remove a buffer from its current buffer list in preparation for
  1893. * dropping it from its current transaction entirely. If the buffer has
  1894. * already started to be used by a subsequent transaction, refile the
  1895. * buffer on that transaction's metadata list.
  1896. *
  1897. * Called under journal->j_list_lock
  1898. *
  1899. * Called under jbd_lock_bh_state(jh2bh(jh))
  1900. */
  1901. void __journal_refile_buffer(struct journal_head *jh)
  1902. {
  1903. int was_dirty;
  1904. struct buffer_head *bh = jh2bh(jh);
  1905. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1906. if (jh->b_transaction)
  1907. assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
  1908. /* If the buffer is now unused, just drop it. */
  1909. if (jh->b_next_transaction == NULL) {
  1910. __journal_unfile_buffer(jh);
  1911. return;
  1912. }
  1913. /*
  1914. * It has been modified by a later transaction: add it to the new
  1915. * transaction's metadata list.
  1916. */
  1917. was_dirty = test_clear_buffer_jbddirty(bh);
  1918. __journal_temp_unlink_buffer(jh);
  1919. jh->b_transaction = jh->b_next_transaction;
  1920. jh->b_next_transaction = NULL;
  1921. __journal_file_buffer(jh, jh->b_transaction,
  1922. jh->b_modified ? BJ_Metadata : BJ_Reserved);
  1923. J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
  1924. if (was_dirty)
  1925. set_buffer_jbddirty(bh);
  1926. }
  1927. /*
  1928. * For the unlocked version of this call, also make sure that any
  1929. * hanging journal_head is cleaned up if necessary.
  1930. *
  1931. * __journal_refile_buffer is usually called as part of a single locked
  1932. * operation on a buffer_head, in which the caller is probably going to
  1933. * be hooking the journal_head onto other lists. In that case it is up
  1934. * to the caller to remove the journal_head if necessary. For the
  1935. * unlocked journal_refile_buffer call, the caller isn't going to be
  1936. * doing anything else to the buffer so we need to do the cleanup
  1937. * ourselves to avoid a jh leak.
  1938. *
  1939. * *** The journal_head may be freed by this call! ***
  1940. */
  1941. void journal_refile_buffer(journal_t *journal, struct journal_head *jh)
  1942. {
  1943. struct buffer_head *bh = jh2bh(jh);
  1944. jbd_lock_bh_state(bh);
  1945. spin_lock(&journal->j_list_lock);
  1946. __journal_refile_buffer(jh);
  1947. jbd_unlock_bh_state(bh);
  1948. journal_remove_journal_head(bh);
  1949. spin_unlock(&journal->j_list_lock);
  1950. __brelse(bh);
  1951. }