transaction.c 65 KB

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