transaction.c 61 KB

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