transaction.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. /*
  2. * linux/fs/jbd2/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/jbd2.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/timer.h>
  25. #include <linux/mm.h>
  26. #include <linux/highmem.h>
  27. #include <linux/hrtimer.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/bug.h>
  30. #include <linux/module.h>
  31. #include <trace/events/jbd2.h>
  32. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
  33. static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
  34. static struct kmem_cache *transaction_cache;
  35. int __init jbd2_journal_init_transaction_cache(void)
  36. {
  37. J_ASSERT(!transaction_cache);
  38. transaction_cache = kmem_cache_create("jbd2_transaction_s",
  39. sizeof(transaction_t),
  40. 0,
  41. SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
  42. NULL);
  43. if (transaction_cache)
  44. return 0;
  45. return -ENOMEM;
  46. }
  47. void jbd2_journal_destroy_transaction_cache(void)
  48. {
  49. if (transaction_cache) {
  50. kmem_cache_destroy(transaction_cache);
  51. transaction_cache = NULL;
  52. }
  53. }
  54. void jbd2_journal_free_transaction(transaction_t *transaction)
  55. {
  56. if (unlikely(ZERO_OR_NULL_PTR(transaction)))
  57. return;
  58. kmem_cache_free(transaction_cache, transaction);
  59. }
  60. /*
  61. * jbd2_get_transaction: obtain a new transaction_t object.
  62. *
  63. * Simply allocate and initialise a new transaction. Create it in
  64. * RUNNING state and add it to the current journal (which should not
  65. * have an existing running transaction: we only make a new transaction
  66. * once we have started to commit the old one).
  67. *
  68. * Preconditions:
  69. * The journal MUST be locked. We don't perform atomic mallocs on the
  70. * new transaction and we can't block without protecting against other
  71. * processes trying to touch the journal while it is in transition.
  72. *
  73. */
  74. static transaction_t *
  75. jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
  76. {
  77. transaction->t_journal = journal;
  78. transaction->t_state = T_RUNNING;
  79. transaction->t_start_time = ktime_get();
  80. transaction->t_tid = journal->j_transaction_sequence++;
  81. transaction->t_expires = jiffies + journal->j_commit_interval;
  82. spin_lock_init(&transaction->t_handle_lock);
  83. atomic_set(&transaction->t_updates, 0);
  84. atomic_set(&transaction->t_outstanding_credits,
  85. atomic_read(&journal->j_reserved_credits));
  86. atomic_set(&transaction->t_handle_count, 0);
  87. INIT_LIST_HEAD(&transaction->t_inode_list);
  88. INIT_LIST_HEAD(&transaction->t_private_list);
  89. /* Set up the commit timer for the new transaction. */
  90. journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
  91. add_timer(&journal->j_commit_timer);
  92. J_ASSERT(journal->j_running_transaction == NULL);
  93. journal->j_running_transaction = transaction;
  94. transaction->t_max_wait = 0;
  95. transaction->t_start = jiffies;
  96. transaction->t_requested = 0;
  97. return transaction;
  98. }
  99. /*
  100. * Handle management.
  101. *
  102. * A handle_t is an object which represents a single atomic update to a
  103. * filesystem, and which tracks all of the modifications which form part
  104. * of that one update.
  105. */
  106. /*
  107. * Update transaction's maximum wait time, if debugging is enabled.
  108. *
  109. * In order for t_max_wait to be reliable, it must be protected by a
  110. * lock. But doing so will mean that start_this_handle() can not be
  111. * run in parallel on SMP systems, which limits our scalability. So
  112. * unless debugging is enabled, we no longer update t_max_wait, which
  113. * means that maximum wait time reported by the jbd2_run_stats
  114. * tracepoint will always be zero.
  115. */
  116. static inline void update_t_max_wait(transaction_t *transaction,
  117. unsigned long ts)
  118. {
  119. #ifdef CONFIG_JBD2_DEBUG
  120. if (jbd2_journal_enable_debug &&
  121. time_after(transaction->t_start, ts)) {
  122. ts = jbd2_time_diff(ts, transaction->t_start);
  123. spin_lock(&transaction->t_handle_lock);
  124. if (ts > transaction->t_max_wait)
  125. transaction->t_max_wait = ts;
  126. spin_unlock(&transaction->t_handle_lock);
  127. }
  128. #endif
  129. }
  130. /*
  131. * Wait until running transaction passes T_LOCKED state. Also starts the commit
  132. * if needed. The function expects running transaction to exist and releases
  133. * j_state_lock.
  134. */
  135. static void wait_transaction_locked(journal_t *journal)
  136. __releases(journal->j_state_lock)
  137. {
  138. DEFINE_WAIT(wait);
  139. int need_to_start;
  140. tid_t tid = journal->j_running_transaction->t_tid;
  141. prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
  142. TASK_UNINTERRUPTIBLE);
  143. need_to_start = !tid_geq(journal->j_commit_request, tid);
  144. read_unlock(&journal->j_state_lock);
  145. if (need_to_start)
  146. jbd2_log_start_commit(journal, tid);
  147. schedule();
  148. finish_wait(&journal->j_wait_transaction_locked, &wait);
  149. }
  150. static void sub_reserved_credits(journal_t *journal, int blocks)
  151. {
  152. atomic_sub(blocks, &journal->j_reserved_credits);
  153. wake_up(&journal->j_wait_reserved);
  154. }
  155. /*
  156. * Wait until we can add credits for handle to the running transaction. Called
  157. * with j_state_lock held for reading. Returns 0 if handle joined the running
  158. * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
  159. * caller must retry.
  160. */
  161. static int add_transaction_credits(journal_t *journal, int blocks,
  162. int rsv_blocks)
  163. {
  164. transaction_t *t = journal->j_running_transaction;
  165. int needed;
  166. int total = blocks + rsv_blocks;
  167. /*
  168. * If the current transaction is locked down for commit, wait
  169. * for the lock to be released.
  170. */
  171. if (t->t_state == T_LOCKED) {
  172. wait_transaction_locked(journal);
  173. return 1;
  174. }
  175. /*
  176. * If there is not enough space left in the log to write all
  177. * potential buffers requested by this operation, we need to
  178. * stall pending a log checkpoint to free some more log space.
  179. */
  180. needed = atomic_add_return(total, &t->t_outstanding_credits);
  181. if (needed > journal->j_max_transaction_buffers) {
  182. /*
  183. * If the current transaction is already too large,
  184. * then start to commit it: we can then go back and
  185. * attach this handle to a new transaction.
  186. */
  187. atomic_sub(total, &t->t_outstanding_credits);
  188. wait_transaction_locked(journal);
  189. return 1;
  190. }
  191. /*
  192. * The commit code assumes that it can get enough log space
  193. * without forcing a checkpoint. This is *critical* for
  194. * correctness: a checkpoint of a buffer which is also
  195. * associated with a committing transaction creates a deadlock,
  196. * so commit simply cannot force through checkpoints.
  197. *
  198. * We must therefore ensure the necessary space in the journal
  199. * *before* starting to dirty potentially checkpointed buffers
  200. * in the new transaction.
  201. */
  202. if (jbd2_log_space_left(journal) < jbd2_space_needed(journal)) {
  203. atomic_sub(total, &t->t_outstanding_credits);
  204. read_unlock(&journal->j_state_lock);
  205. write_lock(&journal->j_state_lock);
  206. if (jbd2_log_space_left(journal) < jbd2_space_needed(journal))
  207. __jbd2_log_wait_for_space(journal);
  208. write_unlock(&journal->j_state_lock);
  209. return 1;
  210. }
  211. /* No reservation? We are done... */
  212. if (!rsv_blocks)
  213. return 0;
  214. needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
  215. /* We allow at most half of a transaction to be reserved */
  216. if (needed > journal->j_max_transaction_buffers / 2) {
  217. sub_reserved_credits(journal, rsv_blocks);
  218. atomic_sub(total, &t->t_outstanding_credits);
  219. read_unlock(&journal->j_state_lock);
  220. wait_event(journal->j_wait_reserved,
  221. atomic_read(&journal->j_reserved_credits) + rsv_blocks
  222. <= journal->j_max_transaction_buffers / 2);
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. /*
  228. * start_this_handle: Given a handle, deal with any locking or stalling
  229. * needed to make sure that there is enough journal space for the handle
  230. * to begin. Attach the handle to a transaction and set up the
  231. * transaction's buffer credits.
  232. */
  233. static int start_this_handle(journal_t *journal, handle_t *handle,
  234. gfp_t gfp_mask)
  235. {
  236. transaction_t *transaction, *new_transaction = NULL;
  237. int blocks = handle->h_buffer_credits;
  238. int rsv_blocks = 0;
  239. unsigned long ts = jiffies;
  240. /*
  241. * 1/2 of transaction can be reserved so we can practically handle
  242. * only 1/2 of maximum transaction size per operation
  243. */
  244. if (WARN_ON(blocks > journal->j_max_transaction_buffers / 2)) {
  245. printk(KERN_ERR "JBD2: %s wants too many credits (%d > %d)\n",
  246. current->comm, blocks,
  247. journal->j_max_transaction_buffers / 2);
  248. return -ENOSPC;
  249. }
  250. if (handle->h_rsv_handle)
  251. rsv_blocks = handle->h_rsv_handle->h_buffer_credits;
  252. alloc_transaction:
  253. if (!journal->j_running_transaction) {
  254. new_transaction = kmem_cache_zalloc(transaction_cache,
  255. gfp_mask);
  256. if (!new_transaction) {
  257. /*
  258. * If __GFP_FS is not present, then we may be
  259. * being called from inside the fs writeback
  260. * layer, so we MUST NOT fail. Since
  261. * __GFP_NOFAIL is going away, we will arrange
  262. * to retry the allocation ourselves.
  263. */
  264. if ((gfp_mask & __GFP_FS) == 0) {
  265. congestion_wait(BLK_RW_ASYNC, HZ/50);
  266. goto alloc_transaction;
  267. }
  268. return -ENOMEM;
  269. }
  270. }
  271. jbd_debug(3, "New handle %p going live.\n", handle);
  272. /*
  273. * We need to hold j_state_lock until t_updates has been incremented,
  274. * for proper journal barrier handling
  275. */
  276. repeat:
  277. read_lock(&journal->j_state_lock);
  278. BUG_ON(journal->j_flags & JBD2_UNMOUNT);
  279. if (is_journal_aborted(journal) ||
  280. (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
  281. read_unlock(&journal->j_state_lock);
  282. jbd2_journal_free_transaction(new_transaction);
  283. return -EROFS;
  284. }
  285. /*
  286. * Wait on the journal's transaction barrier if necessary. Specifically
  287. * we allow reserved handles to proceed because otherwise commit could
  288. * deadlock on page writeback not being able to complete.
  289. */
  290. if (!handle->h_reserved && journal->j_barrier_count) {
  291. read_unlock(&journal->j_state_lock);
  292. wait_event(journal->j_wait_transaction_locked,
  293. journal->j_barrier_count == 0);
  294. goto repeat;
  295. }
  296. if (!journal->j_running_transaction) {
  297. read_unlock(&journal->j_state_lock);
  298. if (!new_transaction)
  299. goto alloc_transaction;
  300. write_lock(&journal->j_state_lock);
  301. if (!journal->j_running_transaction &&
  302. (handle->h_reserved || !journal->j_barrier_count)) {
  303. jbd2_get_transaction(journal, new_transaction);
  304. new_transaction = NULL;
  305. }
  306. write_unlock(&journal->j_state_lock);
  307. goto repeat;
  308. }
  309. transaction = journal->j_running_transaction;
  310. if (!handle->h_reserved) {
  311. /* We may have dropped j_state_lock - restart in that case */
  312. if (add_transaction_credits(journal, blocks, rsv_blocks))
  313. goto repeat;
  314. } else {
  315. /*
  316. * We have handle reserved so we are allowed to join T_LOCKED
  317. * transaction and we don't have to check for transaction size
  318. * and journal space.
  319. */
  320. sub_reserved_credits(journal, blocks);
  321. handle->h_reserved = 0;
  322. }
  323. /* OK, account for the buffers that this operation expects to
  324. * use and add the handle to the running transaction.
  325. */
  326. update_t_max_wait(transaction, ts);
  327. handle->h_transaction = transaction;
  328. handle->h_requested_credits = blocks;
  329. handle->h_start_jiffies = jiffies;
  330. atomic_inc(&transaction->t_updates);
  331. atomic_inc(&transaction->t_handle_count);
  332. jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
  333. handle, blocks,
  334. atomic_read(&transaction->t_outstanding_credits),
  335. jbd2_log_space_left(journal));
  336. read_unlock(&journal->j_state_lock);
  337. lock_map_acquire(&handle->h_lockdep_map);
  338. jbd2_journal_free_transaction(new_transaction);
  339. return 0;
  340. }
  341. static struct lock_class_key jbd2_handle_key;
  342. /* Allocate a new handle. This should probably be in a slab... */
  343. static handle_t *new_handle(int nblocks)
  344. {
  345. handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
  346. if (!handle)
  347. return NULL;
  348. handle->h_buffer_credits = nblocks;
  349. handle->h_ref = 1;
  350. lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
  351. &jbd2_handle_key, 0);
  352. return handle;
  353. }
  354. /**
  355. * handle_t *jbd2_journal_start() - Obtain a new handle.
  356. * @journal: Journal to start transaction on.
  357. * @nblocks: number of block buffer we might modify
  358. *
  359. * We make sure that the transaction can guarantee at least nblocks of
  360. * modified buffers in the log. We block until the log can guarantee
  361. * that much space. Additionally, if rsv_blocks > 0, we also create another
  362. * handle with rsv_blocks reserved blocks in the journal. This handle is
  363. * is stored in h_rsv_handle. It is not attached to any particular transaction
  364. * and thus doesn't block transaction commit. If the caller uses this reserved
  365. * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
  366. * on the parent handle will dispose the reserved one. Reserved handle has to
  367. * be converted to a normal handle using jbd2_journal_start_reserved() before
  368. * it can be used.
  369. *
  370. * Return a pointer to a newly allocated handle, or an ERR_PTR() value
  371. * on failure.
  372. */
  373. handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
  374. gfp_t gfp_mask, unsigned int type,
  375. unsigned int line_no)
  376. {
  377. handle_t *handle = journal_current_handle();
  378. int err;
  379. if (!journal)
  380. return ERR_PTR(-EROFS);
  381. if (handle) {
  382. J_ASSERT(handle->h_transaction->t_journal == journal);
  383. handle->h_ref++;
  384. return handle;
  385. }
  386. handle = new_handle(nblocks);
  387. if (!handle)
  388. return ERR_PTR(-ENOMEM);
  389. if (rsv_blocks) {
  390. handle_t *rsv_handle;
  391. rsv_handle = new_handle(rsv_blocks);
  392. if (!rsv_handle) {
  393. jbd2_free_handle(handle);
  394. return ERR_PTR(-ENOMEM);
  395. }
  396. rsv_handle->h_reserved = 1;
  397. rsv_handle->h_journal = journal;
  398. handle->h_rsv_handle = rsv_handle;
  399. }
  400. current->journal_info = handle;
  401. err = start_this_handle(journal, handle, gfp_mask);
  402. if (err < 0) {
  403. if (handle->h_rsv_handle)
  404. jbd2_free_handle(handle->h_rsv_handle);
  405. jbd2_free_handle(handle);
  406. current->journal_info = NULL;
  407. return ERR_PTR(err);
  408. }
  409. handle->h_type = type;
  410. handle->h_line_no = line_no;
  411. trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
  412. handle->h_transaction->t_tid, type,
  413. line_no, nblocks);
  414. return handle;
  415. }
  416. EXPORT_SYMBOL(jbd2__journal_start);
  417. handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
  418. {
  419. return jbd2__journal_start(journal, nblocks, 0, GFP_NOFS, 0, 0);
  420. }
  421. EXPORT_SYMBOL(jbd2_journal_start);
  422. void jbd2_journal_free_reserved(handle_t *handle)
  423. {
  424. journal_t *journal = handle->h_journal;
  425. WARN_ON(!handle->h_reserved);
  426. sub_reserved_credits(journal, handle->h_buffer_credits);
  427. jbd2_free_handle(handle);
  428. }
  429. EXPORT_SYMBOL(jbd2_journal_free_reserved);
  430. /**
  431. * int jbd2_journal_start_reserved(handle_t *handle) - start reserved handle
  432. * @handle: handle to start
  433. *
  434. * Start handle that has been previously reserved with jbd2_journal_reserve().
  435. * This attaches @handle to the running transaction (or creates one if there's
  436. * not transaction running). Unlike jbd2_journal_start() this function cannot
  437. * block on journal commit, checkpointing, or similar stuff. It can block on
  438. * memory allocation or frozen journal though.
  439. *
  440. * Return 0 on success, non-zero on error - handle is freed in that case.
  441. */
  442. int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
  443. unsigned int line_no)
  444. {
  445. journal_t *journal = handle->h_journal;
  446. int ret = -EIO;
  447. if (WARN_ON(!handle->h_reserved)) {
  448. /* Someone passed in normal handle? Just stop it. */
  449. jbd2_journal_stop(handle);
  450. return ret;
  451. }
  452. /*
  453. * Usefulness of mixing of reserved and unreserved handles is
  454. * questionable. So far nobody seems to need it so just error out.
  455. */
  456. if (WARN_ON(current->journal_info)) {
  457. jbd2_journal_free_reserved(handle);
  458. return ret;
  459. }
  460. handle->h_journal = NULL;
  461. current->journal_info = handle;
  462. /*
  463. * GFP_NOFS is here because callers are likely from writeback or
  464. * similarly constrained call sites
  465. */
  466. ret = start_this_handle(journal, handle, GFP_NOFS);
  467. if (ret < 0) {
  468. current->journal_info = NULL;
  469. jbd2_journal_free_reserved(handle);
  470. }
  471. handle->h_type = type;
  472. handle->h_line_no = line_no;
  473. return ret;
  474. }
  475. EXPORT_SYMBOL(jbd2_journal_start_reserved);
  476. /**
  477. * int jbd2_journal_extend() - extend buffer credits.
  478. * @handle: handle to 'extend'
  479. * @nblocks: nr blocks to try to extend by.
  480. *
  481. * Some transactions, such as large extends and truncates, can be done
  482. * atomically all at once or in several stages. The operation requests
  483. * a credit for a number of buffer modications in advance, but can
  484. * extend its credit if it needs more.
  485. *
  486. * jbd2_journal_extend tries to give the running handle more buffer credits.
  487. * It does not guarantee that allocation - this is a best-effort only.
  488. * The calling process MUST be able to deal cleanly with a failure to
  489. * extend here.
  490. *
  491. * Return 0 on success, non-zero on failure.
  492. *
  493. * return code < 0 implies an error
  494. * return code > 0 implies normal transaction-full status.
  495. */
  496. int jbd2_journal_extend(handle_t *handle, int nblocks)
  497. {
  498. transaction_t *transaction = handle->h_transaction;
  499. journal_t *journal = transaction->t_journal;
  500. int result;
  501. int wanted;
  502. result = -EIO;
  503. if (is_handle_aborted(handle))
  504. goto out;
  505. result = 1;
  506. read_lock(&journal->j_state_lock);
  507. /* Don't extend a locked-down transaction! */
  508. if (handle->h_transaction->t_state != T_RUNNING) {
  509. jbd_debug(3, "denied handle %p %d blocks: "
  510. "transaction not running\n", handle, nblocks);
  511. goto error_out;
  512. }
  513. spin_lock(&transaction->t_handle_lock);
  514. wanted = atomic_add_return(nblocks,
  515. &transaction->t_outstanding_credits);
  516. if (wanted > journal->j_max_transaction_buffers) {
  517. jbd_debug(3, "denied handle %p %d blocks: "
  518. "transaction too large\n", handle, nblocks);
  519. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  520. goto unlock;
  521. }
  522. if (wanted + (wanted >> JBD2_CONTROL_BLOCKS_SHIFT) >
  523. jbd2_log_space_left(journal)) {
  524. jbd_debug(3, "denied handle %p %d blocks: "
  525. "insufficient log space\n", handle, nblocks);
  526. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  527. goto unlock;
  528. }
  529. trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
  530. handle->h_transaction->t_tid,
  531. handle->h_type, handle->h_line_no,
  532. handle->h_buffer_credits,
  533. nblocks);
  534. handle->h_buffer_credits += nblocks;
  535. handle->h_requested_credits += nblocks;
  536. result = 0;
  537. jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
  538. unlock:
  539. spin_unlock(&transaction->t_handle_lock);
  540. error_out:
  541. read_unlock(&journal->j_state_lock);
  542. out:
  543. return result;
  544. }
  545. /**
  546. * int jbd2_journal_restart() - restart a handle .
  547. * @handle: handle to restart
  548. * @nblocks: nr credits requested
  549. *
  550. * Restart a handle for a multi-transaction filesystem
  551. * operation.
  552. *
  553. * If the jbd2_journal_extend() call above fails to grant new buffer credits
  554. * to a running handle, a call to jbd2_journal_restart will commit the
  555. * handle's transaction so far and reattach the handle to a new
  556. * transaction capabable of guaranteeing the requested number of
  557. * credits. We preserve reserved handle if there's any attached to the
  558. * passed in handle.
  559. */
  560. int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
  561. {
  562. transaction_t *transaction = handle->h_transaction;
  563. journal_t *journal = transaction->t_journal;
  564. tid_t tid;
  565. int need_to_start, ret;
  566. /* If we've had an abort of any type, don't even think about
  567. * actually doing the restart! */
  568. if (is_handle_aborted(handle))
  569. return 0;
  570. /*
  571. * First unlink the handle from its current transaction, and start the
  572. * commit on that.
  573. */
  574. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  575. J_ASSERT(journal_current_handle() == handle);
  576. read_lock(&journal->j_state_lock);
  577. spin_lock(&transaction->t_handle_lock);
  578. atomic_sub(handle->h_buffer_credits,
  579. &transaction->t_outstanding_credits);
  580. if (handle->h_rsv_handle) {
  581. sub_reserved_credits(journal,
  582. handle->h_rsv_handle->h_buffer_credits);
  583. }
  584. if (atomic_dec_and_test(&transaction->t_updates))
  585. wake_up(&journal->j_wait_updates);
  586. tid = transaction->t_tid;
  587. spin_unlock(&transaction->t_handle_lock);
  588. jbd_debug(2, "restarting handle %p\n", handle);
  589. need_to_start = !tid_geq(journal->j_commit_request, tid);
  590. read_unlock(&journal->j_state_lock);
  591. if (need_to_start)
  592. jbd2_log_start_commit(journal, tid);
  593. lock_map_release(&handle->h_lockdep_map);
  594. handle->h_buffer_credits = nblocks;
  595. ret = start_this_handle(journal, handle, gfp_mask);
  596. return ret;
  597. }
  598. EXPORT_SYMBOL(jbd2__journal_restart);
  599. int jbd2_journal_restart(handle_t *handle, int nblocks)
  600. {
  601. return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
  602. }
  603. EXPORT_SYMBOL(jbd2_journal_restart);
  604. /**
  605. * void jbd2_journal_lock_updates () - establish a transaction barrier.
  606. * @journal: Journal to establish a barrier on.
  607. *
  608. * This locks out any further updates from being started, and blocks
  609. * until all existing updates have completed, returning only once the
  610. * journal is in a quiescent state with no updates running.
  611. *
  612. * The journal lock should not be held on entry.
  613. */
  614. void jbd2_journal_lock_updates(journal_t *journal)
  615. {
  616. DEFINE_WAIT(wait);
  617. write_lock(&journal->j_state_lock);
  618. ++journal->j_barrier_count;
  619. /* Wait until there are no reserved handles */
  620. if (atomic_read(&journal->j_reserved_credits)) {
  621. write_unlock(&journal->j_state_lock);
  622. wait_event(journal->j_wait_reserved,
  623. atomic_read(&journal->j_reserved_credits) == 0);
  624. write_lock(&journal->j_state_lock);
  625. }
  626. /* Wait until there are no running updates */
  627. while (1) {
  628. transaction_t *transaction = journal->j_running_transaction;
  629. if (!transaction)
  630. break;
  631. spin_lock(&transaction->t_handle_lock);
  632. prepare_to_wait(&journal->j_wait_updates, &wait,
  633. TASK_UNINTERRUPTIBLE);
  634. if (!atomic_read(&transaction->t_updates)) {
  635. spin_unlock(&transaction->t_handle_lock);
  636. finish_wait(&journal->j_wait_updates, &wait);
  637. break;
  638. }
  639. spin_unlock(&transaction->t_handle_lock);
  640. write_unlock(&journal->j_state_lock);
  641. schedule();
  642. finish_wait(&journal->j_wait_updates, &wait);
  643. write_lock(&journal->j_state_lock);
  644. }
  645. write_unlock(&journal->j_state_lock);
  646. /*
  647. * We have now established a barrier against other normal updates, but
  648. * we also need to barrier against other jbd2_journal_lock_updates() calls
  649. * to make sure that we serialise special journal-locked operations
  650. * too.
  651. */
  652. mutex_lock(&journal->j_barrier);
  653. }
  654. /**
  655. * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
  656. * @journal: Journal to release the barrier on.
  657. *
  658. * Release a transaction barrier obtained with jbd2_journal_lock_updates().
  659. *
  660. * Should be called without the journal lock held.
  661. */
  662. void jbd2_journal_unlock_updates (journal_t *journal)
  663. {
  664. J_ASSERT(journal->j_barrier_count != 0);
  665. mutex_unlock(&journal->j_barrier);
  666. write_lock(&journal->j_state_lock);
  667. --journal->j_barrier_count;
  668. write_unlock(&journal->j_state_lock);
  669. wake_up(&journal->j_wait_transaction_locked);
  670. }
  671. static void warn_dirty_buffer(struct buffer_head *bh)
  672. {
  673. char b[BDEVNAME_SIZE];
  674. printk(KERN_WARNING
  675. "JBD2: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
  676. "There's a risk of filesystem corruption in case of system "
  677. "crash.\n",
  678. bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
  679. }
  680. static int sleep_on_shadow_bh(void *word)
  681. {
  682. io_schedule();
  683. return 0;
  684. }
  685. /*
  686. * If the buffer is already part of the current transaction, then there
  687. * is nothing we need to do. If it is already part of a prior
  688. * transaction which we are still committing to disk, then we need to
  689. * make sure that we do not overwrite the old copy: we do copy-out to
  690. * preserve the copy going to disk. We also account the buffer against
  691. * the handle's metadata buffer credits (unless the buffer is already
  692. * part of the transaction, that is).
  693. *
  694. */
  695. static int
  696. do_get_write_access(handle_t *handle, struct journal_head *jh,
  697. int force_copy)
  698. {
  699. struct buffer_head *bh;
  700. transaction_t *transaction;
  701. journal_t *journal;
  702. int error;
  703. char *frozen_buffer = NULL;
  704. int need_copy = 0;
  705. unsigned long start_lock, time_lock;
  706. if (is_handle_aborted(handle))
  707. return -EROFS;
  708. transaction = handle->h_transaction;
  709. journal = transaction->t_journal;
  710. jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
  711. JBUFFER_TRACE(jh, "entry");
  712. repeat:
  713. bh = jh2bh(jh);
  714. /* @@@ Need to check for errors here at some point. */
  715. start_lock = jiffies;
  716. lock_buffer(bh);
  717. jbd_lock_bh_state(bh);
  718. /* If it takes too long to lock the buffer, trace it */
  719. time_lock = jbd2_time_diff(start_lock, jiffies);
  720. if (time_lock > HZ/10)
  721. trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
  722. jiffies_to_msecs(time_lock));
  723. /* We now hold the buffer lock so it is safe to query the buffer
  724. * state. Is the buffer dirty?
  725. *
  726. * If so, there are two possibilities. The buffer may be
  727. * non-journaled, and undergoing a quite legitimate writeback.
  728. * Otherwise, it is journaled, and we don't expect dirty buffers
  729. * in that state (the buffers should be marked JBD_Dirty
  730. * instead.) So either the IO is being done under our own
  731. * control and this is a bug, or it's a third party IO such as
  732. * dump(8) (which may leave the buffer scheduled for read ---
  733. * ie. locked but not dirty) or tune2fs (which may actually have
  734. * the buffer dirtied, ugh.) */
  735. if (buffer_dirty(bh)) {
  736. /*
  737. * First question: is this buffer already part of the current
  738. * transaction or the existing committing transaction?
  739. */
  740. if (jh->b_transaction) {
  741. J_ASSERT_JH(jh,
  742. jh->b_transaction == transaction ||
  743. jh->b_transaction ==
  744. journal->j_committing_transaction);
  745. if (jh->b_next_transaction)
  746. J_ASSERT_JH(jh, jh->b_next_transaction ==
  747. transaction);
  748. warn_dirty_buffer(bh);
  749. }
  750. /*
  751. * In any case we need to clean the dirty flag and we must
  752. * do it under the buffer lock to be sure we don't race
  753. * with running write-out.
  754. */
  755. JBUFFER_TRACE(jh, "Journalling dirty buffer");
  756. clear_buffer_dirty(bh);
  757. set_buffer_jbddirty(bh);
  758. }
  759. unlock_buffer(bh);
  760. error = -EROFS;
  761. if (is_handle_aborted(handle)) {
  762. jbd_unlock_bh_state(bh);
  763. goto out;
  764. }
  765. error = 0;
  766. /*
  767. * The buffer is already part of this transaction if b_transaction or
  768. * b_next_transaction points to it
  769. */
  770. if (jh->b_transaction == transaction ||
  771. jh->b_next_transaction == transaction)
  772. goto done;
  773. /*
  774. * this is the first time this transaction is touching this buffer,
  775. * reset the modified flag
  776. */
  777. jh->b_modified = 0;
  778. /*
  779. * If there is already a copy-out version of this buffer, then we don't
  780. * need to make another one
  781. */
  782. if (jh->b_frozen_data) {
  783. JBUFFER_TRACE(jh, "has frozen data");
  784. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  785. jh->b_next_transaction = transaction;
  786. goto done;
  787. }
  788. /* Is there data here we need to preserve? */
  789. if (jh->b_transaction && jh->b_transaction != transaction) {
  790. JBUFFER_TRACE(jh, "owned by older transaction");
  791. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  792. J_ASSERT_JH(jh, jh->b_transaction ==
  793. journal->j_committing_transaction);
  794. /* There is one case we have to be very careful about.
  795. * If the committing transaction is currently writing
  796. * this buffer out to disk and has NOT made a copy-out,
  797. * then we cannot modify the buffer contents at all
  798. * right now. The essence of copy-out is that it is the
  799. * extra copy, not the primary copy, which gets
  800. * journaled. If the primary copy is already going to
  801. * disk then we cannot do copy-out here. */
  802. if (buffer_shadow(bh)) {
  803. JBUFFER_TRACE(jh, "on shadow: sleep");
  804. jbd_unlock_bh_state(bh);
  805. wait_on_bit(&bh->b_state, BH_Shadow,
  806. sleep_on_shadow_bh, TASK_UNINTERRUPTIBLE);
  807. goto repeat;
  808. }
  809. /*
  810. * Only do the copy if the currently-owning transaction still
  811. * needs it. If buffer isn't on BJ_Metadata list, the
  812. * committing transaction is past that stage (here we use the
  813. * fact that BH_Shadow is set under bh_state lock together with
  814. * refiling to BJ_Shadow list and at this point we know the
  815. * buffer doesn't have BH_Shadow set).
  816. *
  817. * Subtle point, though: if this is a get_undo_access,
  818. * then we will be relying on the frozen_data to contain
  819. * the new value of the committed_data record after the
  820. * transaction, so we HAVE to force the frozen_data copy
  821. * in that case.
  822. */
  823. if (jh->b_jlist == BJ_Metadata || force_copy) {
  824. JBUFFER_TRACE(jh, "generate frozen data");
  825. if (!frozen_buffer) {
  826. JBUFFER_TRACE(jh, "allocate memory for buffer");
  827. jbd_unlock_bh_state(bh);
  828. frozen_buffer =
  829. jbd2_alloc(jh2bh(jh)->b_size,
  830. GFP_NOFS);
  831. if (!frozen_buffer) {
  832. printk(KERN_EMERG
  833. "%s: OOM for frozen_buffer\n",
  834. __func__);
  835. JBUFFER_TRACE(jh, "oom!");
  836. error = -ENOMEM;
  837. jbd_lock_bh_state(bh);
  838. goto done;
  839. }
  840. goto repeat;
  841. }
  842. jh->b_frozen_data = frozen_buffer;
  843. frozen_buffer = NULL;
  844. need_copy = 1;
  845. }
  846. jh->b_next_transaction = transaction;
  847. }
  848. /*
  849. * Finally, if the buffer is not journaled right now, we need to make
  850. * sure it doesn't get written to disk before the caller actually
  851. * commits the new data
  852. */
  853. if (!jh->b_transaction) {
  854. JBUFFER_TRACE(jh, "no transaction");
  855. J_ASSERT_JH(jh, !jh->b_next_transaction);
  856. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  857. spin_lock(&journal->j_list_lock);
  858. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  859. spin_unlock(&journal->j_list_lock);
  860. }
  861. done:
  862. if (need_copy) {
  863. struct page *page;
  864. int offset;
  865. char *source;
  866. J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
  867. "Possible IO failure.\n");
  868. page = jh2bh(jh)->b_page;
  869. offset = offset_in_page(jh2bh(jh)->b_data);
  870. source = kmap_atomic(page);
  871. /* Fire data frozen trigger just before we copy the data */
  872. jbd2_buffer_frozen_trigger(jh, source + offset,
  873. jh->b_triggers);
  874. memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
  875. kunmap_atomic(source);
  876. /*
  877. * Now that the frozen data is saved off, we need to store
  878. * any matching triggers.
  879. */
  880. jh->b_frozen_triggers = jh->b_triggers;
  881. }
  882. jbd_unlock_bh_state(bh);
  883. /*
  884. * If we are about to journal a buffer, then any revoke pending on it is
  885. * no longer valid
  886. */
  887. jbd2_journal_cancel_revoke(handle, jh);
  888. out:
  889. if (unlikely(frozen_buffer)) /* It's usually NULL */
  890. jbd2_free(frozen_buffer, bh->b_size);
  891. JBUFFER_TRACE(jh, "exit");
  892. return error;
  893. }
  894. /**
  895. * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
  896. * @handle: transaction to add buffer modifications to
  897. * @bh: bh to be used for metadata writes
  898. *
  899. * Returns an error code or 0 on success.
  900. *
  901. * In full data journalling mode the buffer may be of type BJ_AsyncData,
  902. * because we're write()ing a buffer which is also part of a shared mapping.
  903. */
  904. int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
  905. {
  906. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  907. int rc;
  908. /* We do not want to get caught playing with fields which the
  909. * log thread also manipulates. Make sure that the buffer
  910. * completes any outstanding IO before proceeding. */
  911. rc = do_get_write_access(handle, jh, 0);
  912. jbd2_journal_put_journal_head(jh);
  913. return rc;
  914. }
  915. /*
  916. * When the user wants to journal a newly created buffer_head
  917. * (ie. getblk() returned a new buffer and we are going to populate it
  918. * manually rather than reading off disk), then we need to keep the
  919. * buffer_head locked until it has been completely filled with new
  920. * data. In this case, we should be able to make the assertion that
  921. * the bh is not already part of an existing transaction.
  922. *
  923. * The buffer should already be locked by the caller by this point.
  924. * There is no lock ranking violation: it was a newly created,
  925. * unlocked buffer beforehand. */
  926. /**
  927. * int jbd2_journal_get_create_access () - notify intent to use newly created bh
  928. * @handle: transaction to new buffer to
  929. * @bh: new buffer.
  930. *
  931. * Call this if you create a new bh.
  932. */
  933. int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
  934. {
  935. transaction_t *transaction = handle->h_transaction;
  936. journal_t *journal = transaction->t_journal;
  937. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  938. int err;
  939. jbd_debug(5, "journal_head %p\n", jh);
  940. err = -EROFS;
  941. if (is_handle_aborted(handle))
  942. goto out;
  943. err = 0;
  944. JBUFFER_TRACE(jh, "entry");
  945. /*
  946. * The buffer may already belong to this transaction due to pre-zeroing
  947. * in the filesystem's new_block code. It may also be on the previous,
  948. * committing transaction's lists, but it HAS to be in Forget state in
  949. * that case: the transaction must have deleted the buffer for it to be
  950. * reused here.
  951. */
  952. jbd_lock_bh_state(bh);
  953. spin_lock(&journal->j_list_lock);
  954. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  955. jh->b_transaction == NULL ||
  956. (jh->b_transaction == journal->j_committing_transaction &&
  957. jh->b_jlist == BJ_Forget)));
  958. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  959. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  960. if (jh->b_transaction == NULL) {
  961. /*
  962. * Previous jbd2_journal_forget() could have left the buffer
  963. * with jbddirty bit set because it was being committed. When
  964. * the commit finished, we've filed the buffer for
  965. * checkpointing and marked it dirty. Now we are reallocating
  966. * the buffer so the transaction freeing it must have
  967. * committed and so it's safe to clear the dirty bit.
  968. */
  969. clear_buffer_dirty(jh2bh(jh));
  970. /* first access by this transaction */
  971. jh->b_modified = 0;
  972. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  973. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  974. } else if (jh->b_transaction == journal->j_committing_transaction) {
  975. /* first access by this transaction */
  976. jh->b_modified = 0;
  977. JBUFFER_TRACE(jh, "set next transaction");
  978. jh->b_next_transaction = transaction;
  979. }
  980. spin_unlock(&journal->j_list_lock);
  981. jbd_unlock_bh_state(bh);
  982. /*
  983. * akpm: I added this. ext3_alloc_branch can pick up new indirect
  984. * blocks which contain freed but then revoked metadata. We need
  985. * to cancel the revoke in case we end up freeing it yet again
  986. * and the reallocating as data - this would cause a second revoke,
  987. * which hits an assertion error.
  988. */
  989. JBUFFER_TRACE(jh, "cancelling revoke");
  990. jbd2_journal_cancel_revoke(handle, jh);
  991. out:
  992. jbd2_journal_put_journal_head(jh);
  993. return err;
  994. }
  995. /**
  996. * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
  997. * non-rewindable consequences
  998. * @handle: transaction
  999. * @bh: buffer to undo
  1000. *
  1001. * Sometimes there is a need to distinguish between metadata which has
  1002. * been committed to disk and that which has not. The ext3fs code uses
  1003. * this for freeing and allocating space, we have to make sure that we
  1004. * do not reuse freed space until the deallocation has been committed,
  1005. * since if we overwrote that space we would make the delete
  1006. * un-rewindable in case of a crash.
  1007. *
  1008. * To deal with that, jbd2_journal_get_undo_access requests write access to a
  1009. * buffer for parts of non-rewindable operations such as delete
  1010. * operations on the bitmaps. The journaling code must keep a copy of
  1011. * the buffer's contents prior to the undo_access call until such time
  1012. * as we know that the buffer has definitely been committed to disk.
  1013. *
  1014. * We never need to know which transaction the committed data is part
  1015. * of, buffers touched here are guaranteed to be dirtied later and so
  1016. * will be committed to a new transaction in due course, at which point
  1017. * we can discard the old committed data pointer.
  1018. *
  1019. * Returns error number or 0 on success.
  1020. */
  1021. int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
  1022. {
  1023. int err;
  1024. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  1025. char *committed_data = NULL;
  1026. JBUFFER_TRACE(jh, "entry");
  1027. /*
  1028. * Do this first --- it can drop the journal lock, so we want to
  1029. * make sure that obtaining the committed_data is done
  1030. * atomically wrt. completion of any outstanding commits.
  1031. */
  1032. err = do_get_write_access(handle, jh, 1);
  1033. if (err)
  1034. goto out;
  1035. repeat:
  1036. if (!jh->b_committed_data) {
  1037. committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
  1038. if (!committed_data) {
  1039. printk(KERN_EMERG "%s: No memory for committed data\n",
  1040. __func__);
  1041. err = -ENOMEM;
  1042. goto out;
  1043. }
  1044. }
  1045. jbd_lock_bh_state(bh);
  1046. if (!jh->b_committed_data) {
  1047. /* Copy out the current buffer contents into the
  1048. * preserved, committed copy. */
  1049. JBUFFER_TRACE(jh, "generate b_committed data");
  1050. if (!committed_data) {
  1051. jbd_unlock_bh_state(bh);
  1052. goto repeat;
  1053. }
  1054. jh->b_committed_data = committed_data;
  1055. committed_data = NULL;
  1056. memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
  1057. }
  1058. jbd_unlock_bh_state(bh);
  1059. out:
  1060. jbd2_journal_put_journal_head(jh);
  1061. if (unlikely(committed_data))
  1062. jbd2_free(committed_data, bh->b_size);
  1063. return err;
  1064. }
  1065. /**
  1066. * void jbd2_journal_set_triggers() - Add triggers for commit writeout
  1067. * @bh: buffer to trigger on
  1068. * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
  1069. *
  1070. * Set any triggers on this journal_head. This is always safe, because
  1071. * triggers for a committing buffer will be saved off, and triggers for
  1072. * a running transaction will match the buffer in that transaction.
  1073. *
  1074. * Call with NULL to clear the triggers.
  1075. */
  1076. void jbd2_journal_set_triggers(struct buffer_head *bh,
  1077. struct jbd2_buffer_trigger_type *type)
  1078. {
  1079. struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
  1080. if (WARN_ON(!jh))
  1081. return;
  1082. jh->b_triggers = type;
  1083. jbd2_journal_put_journal_head(jh);
  1084. }
  1085. void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
  1086. struct jbd2_buffer_trigger_type *triggers)
  1087. {
  1088. struct buffer_head *bh = jh2bh(jh);
  1089. if (!triggers || !triggers->t_frozen)
  1090. return;
  1091. triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
  1092. }
  1093. void jbd2_buffer_abort_trigger(struct journal_head *jh,
  1094. struct jbd2_buffer_trigger_type *triggers)
  1095. {
  1096. if (!triggers || !triggers->t_abort)
  1097. return;
  1098. triggers->t_abort(triggers, jh2bh(jh));
  1099. }
  1100. /**
  1101. * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
  1102. * @handle: transaction to add buffer to.
  1103. * @bh: buffer to mark
  1104. *
  1105. * mark dirty metadata which needs to be journaled as part of the current
  1106. * transaction.
  1107. *
  1108. * The buffer must have previously had jbd2_journal_get_write_access()
  1109. * called so that it has a valid journal_head attached to the buffer
  1110. * head.
  1111. *
  1112. * The buffer is placed on the transaction's metadata list and is marked
  1113. * as belonging to the transaction.
  1114. *
  1115. * Returns error number or 0 on success.
  1116. *
  1117. * Special care needs to be taken if the buffer already belongs to the
  1118. * current committing transaction (in which case we should have frozen
  1119. * data present for that commit). In that case, we don't relink the
  1120. * buffer: that only gets done when the old transaction finally
  1121. * completes its commit.
  1122. */
  1123. int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
  1124. {
  1125. transaction_t *transaction = handle->h_transaction;
  1126. journal_t *journal = transaction->t_journal;
  1127. struct journal_head *jh;
  1128. int ret = 0;
  1129. if (is_handle_aborted(handle))
  1130. goto out;
  1131. jh = jbd2_journal_grab_journal_head(bh);
  1132. if (!jh) {
  1133. ret = -EUCLEAN;
  1134. goto out;
  1135. }
  1136. jbd_debug(5, "journal_head %p\n", jh);
  1137. JBUFFER_TRACE(jh, "entry");
  1138. jbd_lock_bh_state(bh);
  1139. if (jh->b_modified == 0) {
  1140. /*
  1141. * This buffer's got modified and becoming part
  1142. * of the transaction. This needs to be done
  1143. * once a transaction -bzzz
  1144. */
  1145. jh->b_modified = 1;
  1146. J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
  1147. handle->h_buffer_credits--;
  1148. }
  1149. /*
  1150. * fastpath, to avoid expensive locking. If this buffer is already
  1151. * on the running transaction's metadata list there is nothing to do.
  1152. * Nobody can take it off again because there is a handle open.
  1153. * I _think_ we're OK here with SMP barriers - a mistaken decision will
  1154. * result in this test being false, so we go in and take the locks.
  1155. */
  1156. if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
  1157. JBUFFER_TRACE(jh, "fastpath");
  1158. if (unlikely(jh->b_transaction !=
  1159. journal->j_running_transaction)) {
  1160. printk(KERN_EMERG "JBD: %s: "
  1161. "jh->b_transaction (%llu, %p, %u) != "
  1162. "journal->j_running_transaction (%p, %u)",
  1163. journal->j_devname,
  1164. (unsigned long long) bh->b_blocknr,
  1165. jh->b_transaction,
  1166. jh->b_transaction ? jh->b_transaction->t_tid : 0,
  1167. journal->j_running_transaction,
  1168. journal->j_running_transaction ?
  1169. journal->j_running_transaction->t_tid : 0);
  1170. ret = -EINVAL;
  1171. }
  1172. goto out_unlock_bh;
  1173. }
  1174. set_buffer_jbddirty(bh);
  1175. /*
  1176. * Metadata already on the current transaction list doesn't
  1177. * need to be filed. Metadata on another transaction's list must
  1178. * be committing, and will be refiled once the commit completes:
  1179. * leave it alone for now.
  1180. */
  1181. if (jh->b_transaction != transaction) {
  1182. JBUFFER_TRACE(jh, "already on other transaction");
  1183. if (unlikely(jh->b_transaction !=
  1184. journal->j_committing_transaction)) {
  1185. printk(KERN_EMERG "JBD: %s: "
  1186. "jh->b_transaction (%llu, %p, %u) != "
  1187. "journal->j_committing_transaction (%p, %u)",
  1188. journal->j_devname,
  1189. (unsigned long long) bh->b_blocknr,
  1190. jh->b_transaction,
  1191. jh->b_transaction ? jh->b_transaction->t_tid : 0,
  1192. journal->j_committing_transaction,
  1193. journal->j_committing_transaction ?
  1194. journal->j_committing_transaction->t_tid : 0);
  1195. ret = -EINVAL;
  1196. }
  1197. if (unlikely(jh->b_next_transaction != transaction)) {
  1198. printk(KERN_EMERG "JBD: %s: "
  1199. "jh->b_next_transaction (%llu, %p, %u) != "
  1200. "transaction (%p, %u)",
  1201. journal->j_devname,
  1202. (unsigned long long) bh->b_blocknr,
  1203. jh->b_next_transaction,
  1204. jh->b_next_transaction ?
  1205. jh->b_next_transaction->t_tid : 0,
  1206. transaction, transaction->t_tid);
  1207. ret = -EINVAL;
  1208. }
  1209. /* And this case is illegal: we can't reuse another
  1210. * transaction's data buffer, ever. */
  1211. goto out_unlock_bh;
  1212. }
  1213. /* That test should have eliminated the following case: */
  1214. J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
  1215. JBUFFER_TRACE(jh, "file as BJ_Metadata");
  1216. spin_lock(&journal->j_list_lock);
  1217. __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
  1218. spin_unlock(&journal->j_list_lock);
  1219. out_unlock_bh:
  1220. jbd_unlock_bh_state(bh);
  1221. jbd2_journal_put_journal_head(jh);
  1222. out:
  1223. JBUFFER_TRACE(jh, "exit");
  1224. WARN_ON(ret); /* All errors are bugs, so dump the stack */
  1225. return ret;
  1226. }
  1227. /**
  1228. * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
  1229. * @handle: transaction handle
  1230. * @bh: bh to 'forget'
  1231. *
  1232. * We can only do the bforget if there are no commits pending against the
  1233. * buffer. If the buffer is dirty in the current running transaction we
  1234. * can safely unlink it.
  1235. *
  1236. * bh may not be a journalled buffer at all - it may be a non-JBD
  1237. * buffer which came off the hashtable. Check for this.
  1238. *
  1239. * Decrements bh->b_count by one.
  1240. *
  1241. * Allow this call even if the handle has aborted --- it may be part of
  1242. * the caller's cleanup after an abort.
  1243. */
  1244. int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
  1245. {
  1246. transaction_t *transaction = handle->h_transaction;
  1247. journal_t *journal = transaction->t_journal;
  1248. struct journal_head *jh;
  1249. int drop_reserve = 0;
  1250. int err = 0;
  1251. int was_modified = 0;
  1252. BUFFER_TRACE(bh, "entry");
  1253. jbd_lock_bh_state(bh);
  1254. spin_lock(&journal->j_list_lock);
  1255. if (!buffer_jbd(bh))
  1256. goto not_jbd;
  1257. jh = bh2jh(bh);
  1258. /* Critical error: attempting to delete a bitmap buffer, maybe?
  1259. * Don't do any jbd operations, and return an error. */
  1260. if (!J_EXPECT_JH(jh, !jh->b_committed_data,
  1261. "inconsistent data on disk")) {
  1262. err = -EIO;
  1263. goto not_jbd;
  1264. }
  1265. /* keep track of whether or not this transaction modified us */
  1266. was_modified = jh->b_modified;
  1267. /*
  1268. * The buffer's going from the transaction, we must drop
  1269. * all references -bzzz
  1270. */
  1271. jh->b_modified = 0;
  1272. if (jh->b_transaction == handle->h_transaction) {
  1273. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1274. /* If we are forgetting a buffer which is already part
  1275. * of this transaction, then we can just drop it from
  1276. * the transaction immediately. */
  1277. clear_buffer_dirty(bh);
  1278. clear_buffer_jbddirty(bh);
  1279. JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
  1280. /*
  1281. * we only want to drop a reference if this transaction
  1282. * modified the buffer
  1283. */
  1284. if (was_modified)
  1285. drop_reserve = 1;
  1286. /*
  1287. * We are no longer going to journal this buffer.
  1288. * However, the commit of this transaction is still
  1289. * important to the buffer: the delete that we are now
  1290. * processing might obsolete an old log entry, so by
  1291. * committing, we can satisfy the buffer's checkpoint.
  1292. *
  1293. * So, if we have a checkpoint on the buffer, we should
  1294. * now refile the buffer on our BJ_Forget list so that
  1295. * we know to remove the checkpoint after we commit.
  1296. */
  1297. if (jh->b_cp_transaction) {
  1298. __jbd2_journal_temp_unlink_buffer(jh);
  1299. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1300. } else {
  1301. __jbd2_journal_unfile_buffer(jh);
  1302. if (!buffer_jbd(bh)) {
  1303. spin_unlock(&journal->j_list_lock);
  1304. jbd_unlock_bh_state(bh);
  1305. __bforget(bh);
  1306. goto drop;
  1307. }
  1308. }
  1309. } else if (jh->b_transaction) {
  1310. J_ASSERT_JH(jh, (jh->b_transaction ==
  1311. journal->j_committing_transaction));
  1312. /* However, if the buffer is still owned by a prior
  1313. * (committing) transaction, we can't drop it yet... */
  1314. JBUFFER_TRACE(jh, "belongs to older transaction");
  1315. /* ... but we CAN drop it from the new transaction if we
  1316. * have also modified it since the original commit. */
  1317. if (jh->b_next_transaction) {
  1318. J_ASSERT(jh->b_next_transaction == transaction);
  1319. jh->b_next_transaction = NULL;
  1320. /*
  1321. * only drop a reference if this transaction modified
  1322. * the buffer
  1323. */
  1324. if (was_modified)
  1325. drop_reserve = 1;
  1326. }
  1327. }
  1328. not_jbd:
  1329. spin_unlock(&journal->j_list_lock);
  1330. jbd_unlock_bh_state(bh);
  1331. __brelse(bh);
  1332. drop:
  1333. if (drop_reserve) {
  1334. /* no need to reserve log space for this block -bzzz */
  1335. handle->h_buffer_credits++;
  1336. }
  1337. return err;
  1338. }
  1339. /**
  1340. * int jbd2_journal_stop() - complete a transaction
  1341. * @handle: tranaction to complete.
  1342. *
  1343. * All done for a particular handle.
  1344. *
  1345. * There is not much action needed here. We just return any remaining
  1346. * buffer credits to the transaction and remove the handle. The only
  1347. * complication is that we need to start a commit operation if the
  1348. * filesystem is marked for synchronous update.
  1349. *
  1350. * jbd2_journal_stop itself will not usually return an error, but it may
  1351. * do so in unusual circumstances. In particular, expect it to
  1352. * return -EIO if a jbd2_journal_abort has been executed since the
  1353. * transaction began.
  1354. */
  1355. int jbd2_journal_stop(handle_t *handle)
  1356. {
  1357. transaction_t *transaction = handle->h_transaction;
  1358. journal_t *journal = transaction->t_journal;
  1359. int err, wait_for_commit = 0;
  1360. tid_t tid;
  1361. pid_t pid;
  1362. J_ASSERT(journal_current_handle() == handle);
  1363. if (is_handle_aborted(handle))
  1364. err = -EIO;
  1365. else {
  1366. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  1367. err = 0;
  1368. }
  1369. if (--handle->h_ref > 0) {
  1370. jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1371. handle->h_ref);
  1372. return err;
  1373. }
  1374. jbd_debug(4, "Handle %p going down\n", handle);
  1375. trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
  1376. handle->h_transaction->t_tid,
  1377. handle->h_type, handle->h_line_no,
  1378. jiffies - handle->h_start_jiffies,
  1379. handle->h_sync, handle->h_requested_credits,
  1380. (handle->h_requested_credits -
  1381. handle->h_buffer_credits));
  1382. /*
  1383. * Implement synchronous transaction batching. If the handle
  1384. * was synchronous, don't force a commit immediately. Let's
  1385. * yield and let another thread piggyback onto this
  1386. * transaction. Keep doing that while new threads continue to
  1387. * arrive. It doesn't cost much - we're about to run a commit
  1388. * and sleep on IO anyway. Speeds up many-threaded, many-dir
  1389. * operations by 30x or more...
  1390. *
  1391. * We try and optimize the sleep time against what the
  1392. * underlying disk can do, instead of having a static sleep
  1393. * time. This is useful for the case where our storage is so
  1394. * fast that it is more optimal to go ahead and force a flush
  1395. * and wait for the transaction to be committed than it is to
  1396. * wait for an arbitrary amount of time for new writers to
  1397. * join the transaction. We achieve this by measuring how
  1398. * long it takes to commit a transaction, and compare it with
  1399. * how long this transaction has been running, and if run time
  1400. * < commit time then we sleep for the delta and commit. This
  1401. * greatly helps super fast disks that would see slowdowns as
  1402. * more threads started doing fsyncs.
  1403. *
  1404. * But don't do this if this process was the most recent one
  1405. * to perform a synchronous write. We do this to detect the
  1406. * case where a single process is doing a stream of sync
  1407. * writes. No point in waiting for joiners in that case.
  1408. */
  1409. pid = current->pid;
  1410. if (handle->h_sync && journal->j_last_sync_writer != pid) {
  1411. u64 commit_time, trans_time;
  1412. journal->j_last_sync_writer = pid;
  1413. read_lock(&journal->j_state_lock);
  1414. commit_time = journal->j_average_commit_time;
  1415. read_unlock(&journal->j_state_lock);
  1416. trans_time = ktime_to_ns(ktime_sub(ktime_get(),
  1417. transaction->t_start_time));
  1418. commit_time = max_t(u64, commit_time,
  1419. 1000*journal->j_min_batch_time);
  1420. commit_time = min_t(u64, commit_time,
  1421. 1000*journal->j_max_batch_time);
  1422. if (trans_time < commit_time) {
  1423. ktime_t expires = ktime_add_ns(ktime_get(),
  1424. commit_time);
  1425. set_current_state(TASK_UNINTERRUPTIBLE);
  1426. schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
  1427. }
  1428. }
  1429. if (handle->h_sync)
  1430. transaction->t_synchronous_commit = 1;
  1431. current->journal_info = NULL;
  1432. atomic_sub(handle->h_buffer_credits,
  1433. &transaction->t_outstanding_credits);
  1434. /*
  1435. * If the handle is marked SYNC, we need to set another commit
  1436. * going! We also want to force a commit if the current
  1437. * transaction is occupying too much of the log, or if the
  1438. * transaction is too old now.
  1439. */
  1440. if (handle->h_sync ||
  1441. (atomic_read(&transaction->t_outstanding_credits) >
  1442. journal->j_max_transaction_buffers) ||
  1443. time_after_eq(jiffies, transaction->t_expires)) {
  1444. /* Do this even for aborted journals: an abort still
  1445. * completes the commit thread, it just doesn't write
  1446. * anything to disk. */
  1447. jbd_debug(2, "transaction too old, requesting commit for "
  1448. "handle %p\n", handle);
  1449. /* This is non-blocking */
  1450. jbd2_log_start_commit(journal, transaction->t_tid);
  1451. /*
  1452. * Special case: JBD2_SYNC synchronous updates require us
  1453. * to wait for the commit to complete.
  1454. */
  1455. if (handle->h_sync && !(current->flags & PF_MEMALLOC))
  1456. wait_for_commit = 1;
  1457. }
  1458. /*
  1459. * Once we drop t_updates, if it goes to zero the transaction
  1460. * could start committing on us and eventually disappear. So
  1461. * once we do this, we must not dereference transaction
  1462. * pointer again.
  1463. */
  1464. tid = transaction->t_tid;
  1465. if (atomic_dec_and_test(&transaction->t_updates)) {
  1466. wake_up(&journal->j_wait_updates);
  1467. if (journal->j_barrier_count)
  1468. wake_up(&journal->j_wait_transaction_locked);
  1469. }
  1470. if (wait_for_commit)
  1471. err = jbd2_log_wait_commit(journal, tid);
  1472. lock_map_release(&handle->h_lockdep_map);
  1473. if (handle->h_rsv_handle)
  1474. jbd2_journal_free_reserved(handle->h_rsv_handle);
  1475. jbd2_free_handle(handle);
  1476. return err;
  1477. }
  1478. /*
  1479. *
  1480. * List management code snippets: various functions for manipulating the
  1481. * transaction buffer lists.
  1482. *
  1483. */
  1484. /*
  1485. * Append a buffer to a transaction list, given the transaction's list head
  1486. * pointer.
  1487. *
  1488. * j_list_lock is held.
  1489. *
  1490. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1491. */
  1492. static inline void
  1493. __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
  1494. {
  1495. if (!*list) {
  1496. jh->b_tnext = jh->b_tprev = jh;
  1497. *list = jh;
  1498. } else {
  1499. /* Insert at the tail of the list to preserve order */
  1500. struct journal_head *first = *list, *last = first->b_tprev;
  1501. jh->b_tprev = last;
  1502. jh->b_tnext = first;
  1503. last->b_tnext = first->b_tprev = jh;
  1504. }
  1505. }
  1506. /*
  1507. * Remove a buffer from a transaction list, given the transaction's list
  1508. * head pointer.
  1509. *
  1510. * Called with j_list_lock held, and the journal may not be locked.
  1511. *
  1512. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1513. */
  1514. static inline void
  1515. __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
  1516. {
  1517. if (*list == jh) {
  1518. *list = jh->b_tnext;
  1519. if (*list == jh)
  1520. *list = NULL;
  1521. }
  1522. jh->b_tprev->b_tnext = jh->b_tnext;
  1523. jh->b_tnext->b_tprev = jh->b_tprev;
  1524. }
  1525. /*
  1526. * Remove a buffer from the appropriate transaction list.
  1527. *
  1528. * Note that this function can *change* the value of
  1529. * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
  1530. * t_reserved_list. If the caller is holding onto a copy of one of these
  1531. * pointers, it could go bad. Generally the caller needs to re-read the
  1532. * pointer from the transaction_t.
  1533. *
  1534. * Called under j_list_lock.
  1535. */
  1536. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
  1537. {
  1538. struct journal_head **list = NULL;
  1539. transaction_t *transaction;
  1540. struct buffer_head *bh = jh2bh(jh);
  1541. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1542. transaction = jh->b_transaction;
  1543. if (transaction)
  1544. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1545. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1546. if (jh->b_jlist != BJ_None)
  1547. J_ASSERT_JH(jh, transaction != NULL);
  1548. switch (jh->b_jlist) {
  1549. case BJ_None:
  1550. return;
  1551. case BJ_Metadata:
  1552. transaction->t_nr_buffers--;
  1553. J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
  1554. list = &transaction->t_buffers;
  1555. break;
  1556. case BJ_Forget:
  1557. list = &transaction->t_forget;
  1558. break;
  1559. case BJ_Shadow:
  1560. list = &transaction->t_shadow_list;
  1561. break;
  1562. case BJ_Reserved:
  1563. list = &transaction->t_reserved_list;
  1564. break;
  1565. }
  1566. __blist_del_buffer(list, jh);
  1567. jh->b_jlist = BJ_None;
  1568. if (test_clear_buffer_jbddirty(bh))
  1569. mark_buffer_dirty(bh); /* Expose it to the VM */
  1570. }
  1571. /*
  1572. * Remove buffer from all transactions.
  1573. *
  1574. * Called with bh_state lock and j_list_lock
  1575. *
  1576. * jh and bh may be already freed when this function returns.
  1577. */
  1578. static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
  1579. {
  1580. __jbd2_journal_temp_unlink_buffer(jh);
  1581. jh->b_transaction = NULL;
  1582. jbd2_journal_put_journal_head(jh);
  1583. }
  1584. void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
  1585. {
  1586. struct buffer_head *bh = jh2bh(jh);
  1587. /* Get reference so that buffer cannot be freed before we unlock it */
  1588. get_bh(bh);
  1589. jbd_lock_bh_state(bh);
  1590. spin_lock(&journal->j_list_lock);
  1591. __jbd2_journal_unfile_buffer(jh);
  1592. spin_unlock(&journal->j_list_lock);
  1593. jbd_unlock_bh_state(bh);
  1594. __brelse(bh);
  1595. }
  1596. /*
  1597. * Called from jbd2_journal_try_to_free_buffers().
  1598. *
  1599. * Called under jbd_lock_bh_state(bh)
  1600. */
  1601. static void
  1602. __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
  1603. {
  1604. struct journal_head *jh;
  1605. jh = bh2jh(bh);
  1606. if (buffer_locked(bh) || buffer_dirty(bh))
  1607. goto out;
  1608. if (jh->b_next_transaction != NULL)
  1609. goto out;
  1610. spin_lock(&journal->j_list_lock);
  1611. if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
  1612. /* written-back checkpointed metadata buffer */
  1613. JBUFFER_TRACE(jh, "remove from checkpoint list");
  1614. __jbd2_journal_remove_checkpoint(jh);
  1615. }
  1616. spin_unlock(&journal->j_list_lock);
  1617. out:
  1618. return;
  1619. }
  1620. /**
  1621. * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
  1622. * @journal: journal for operation
  1623. * @page: to try and free
  1624. * @gfp_mask: we use the mask to detect how hard should we try to release
  1625. * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
  1626. * release the buffers.
  1627. *
  1628. *
  1629. * For all the buffers on this page,
  1630. * if they are fully written out ordered data, move them onto BUF_CLEAN
  1631. * so try_to_free_buffers() can reap them.
  1632. *
  1633. * This function returns non-zero if we wish try_to_free_buffers()
  1634. * to be called. We do this if the page is releasable by try_to_free_buffers().
  1635. * We also do it if the page has locked or dirty buffers and the caller wants
  1636. * us to perform sync or async writeout.
  1637. *
  1638. * This complicates JBD locking somewhat. We aren't protected by the
  1639. * BKL here. We wish to remove the buffer from its committing or
  1640. * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
  1641. *
  1642. * This may *change* the value of transaction_t->t_datalist, so anyone
  1643. * who looks at t_datalist needs to lock against this function.
  1644. *
  1645. * Even worse, someone may be doing a jbd2_journal_dirty_data on this
  1646. * buffer. So we need to lock against that. jbd2_journal_dirty_data()
  1647. * will come out of the lock with the buffer dirty, which makes it
  1648. * ineligible for release here.
  1649. *
  1650. * Who else is affected by this? hmm... Really the only contender
  1651. * is do_get_write_access() - it could be looking at the buffer while
  1652. * journal_try_to_free_buffer() is changing its state. But that
  1653. * cannot happen because we never reallocate freed data as metadata
  1654. * while the data is part of a transaction. Yes?
  1655. *
  1656. * Return 0 on failure, 1 on success
  1657. */
  1658. int jbd2_journal_try_to_free_buffers(journal_t *journal,
  1659. struct page *page, gfp_t gfp_mask)
  1660. {
  1661. struct buffer_head *head;
  1662. struct buffer_head *bh;
  1663. int ret = 0;
  1664. J_ASSERT(PageLocked(page));
  1665. head = page_buffers(page);
  1666. bh = head;
  1667. do {
  1668. struct journal_head *jh;
  1669. /*
  1670. * We take our own ref against the journal_head here to avoid
  1671. * having to add tons of locking around each instance of
  1672. * jbd2_journal_put_journal_head().
  1673. */
  1674. jh = jbd2_journal_grab_journal_head(bh);
  1675. if (!jh)
  1676. continue;
  1677. jbd_lock_bh_state(bh);
  1678. __journal_try_to_free_buffer(journal, bh);
  1679. jbd2_journal_put_journal_head(jh);
  1680. jbd_unlock_bh_state(bh);
  1681. if (buffer_jbd(bh))
  1682. goto busy;
  1683. } while ((bh = bh->b_this_page) != head);
  1684. ret = try_to_free_buffers(page);
  1685. busy:
  1686. return ret;
  1687. }
  1688. /*
  1689. * This buffer is no longer needed. If it is on an older transaction's
  1690. * checkpoint list we need to record it on this transaction's forget list
  1691. * to pin this buffer (and hence its checkpointing transaction) down until
  1692. * this transaction commits. If the buffer isn't on a checkpoint list, we
  1693. * release it.
  1694. * Returns non-zero if JBD no longer has an interest in the buffer.
  1695. *
  1696. * Called under j_list_lock.
  1697. *
  1698. * Called under jbd_lock_bh_state(bh).
  1699. */
  1700. static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
  1701. {
  1702. int may_free = 1;
  1703. struct buffer_head *bh = jh2bh(jh);
  1704. if (jh->b_cp_transaction) {
  1705. JBUFFER_TRACE(jh, "on running+cp transaction");
  1706. __jbd2_journal_temp_unlink_buffer(jh);
  1707. /*
  1708. * We don't want to write the buffer anymore, clear the
  1709. * bit so that we don't confuse checks in
  1710. * __journal_file_buffer
  1711. */
  1712. clear_buffer_dirty(bh);
  1713. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1714. may_free = 0;
  1715. } else {
  1716. JBUFFER_TRACE(jh, "on running transaction");
  1717. __jbd2_journal_unfile_buffer(jh);
  1718. }
  1719. return may_free;
  1720. }
  1721. /*
  1722. * jbd2_journal_invalidatepage
  1723. *
  1724. * This code is tricky. It has a number of cases to deal with.
  1725. *
  1726. * There are two invariants which this code relies on:
  1727. *
  1728. * i_size must be updated on disk before we start calling invalidatepage on the
  1729. * data.
  1730. *
  1731. * This is done in ext3 by defining an ext3_setattr method which
  1732. * updates i_size before truncate gets going. By maintaining this
  1733. * invariant, we can be sure that it is safe to throw away any buffers
  1734. * attached to the current transaction: once the transaction commits,
  1735. * we know that the data will not be needed.
  1736. *
  1737. * Note however that we can *not* throw away data belonging to the
  1738. * previous, committing transaction!
  1739. *
  1740. * Any disk blocks which *are* part of the previous, committing
  1741. * transaction (and which therefore cannot be discarded immediately) are
  1742. * not going to be reused in the new running transaction
  1743. *
  1744. * The bitmap committed_data images guarantee this: any block which is
  1745. * allocated in one transaction and removed in the next will be marked
  1746. * as in-use in the committed_data bitmap, so cannot be reused until
  1747. * the next transaction to delete the block commits. This means that
  1748. * leaving committing buffers dirty is quite safe: the disk blocks
  1749. * cannot be reallocated to a different file and so buffer aliasing is
  1750. * not possible.
  1751. *
  1752. *
  1753. * The above applies mainly to ordered data mode. In writeback mode we
  1754. * don't make guarantees about the order in which data hits disk --- in
  1755. * particular we don't guarantee that new dirty data is flushed before
  1756. * transaction commit --- so it is always safe just to discard data
  1757. * immediately in that mode. --sct
  1758. */
  1759. /*
  1760. * The journal_unmap_buffer helper function returns zero if the buffer
  1761. * concerned remains pinned as an anonymous buffer belonging to an older
  1762. * transaction.
  1763. *
  1764. * We're outside-transaction here. Either or both of j_running_transaction
  1765. * and j_committing_transaction may be NULL.
  1766. */
  1767. static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
  1768. int partial_page)
  1769. {
  1770. transaction_t *transaction;
  1771. struct journal_head *jh;
  1772. int may_free = 1;
  1773. BUFFER_TRACE(bh, "entry");
  1774. /*
  1775. * It is safe to proceed here without the j_list_lock because the
  1776. * buffers cannot be stolen by try_to_free_buffers as long as we are
  1777. * holding the page lock. --sct
  1778. */
  1779. if (!buffer_jbd(bh))
  1780. goto zap_buffer_unlocked;
  1781. /* OK, we have data buffer in journaled mode */
  1782. write_lock(&journal->j_state_lock);
  1783. jbd_lock_bh_state(bh);
  1784. spin_lock(&journal->j_list_lock);
  1785. jh = jbd2_journal_grab_journal_head(bh);
  1786. if (!jh)
  1787. goto zap_buffer_no_jh;
  1788. /*
  1789. * We cannot remove the buffer from checkpoint lists until the
  1790. * transaction adding inode to orphan list (let's call it T)
  1791. * is committed. Otherwise if the transaction changing the
  1792. * buffer would be cleaned from the journal before T is
  1793. * committed, a crash will cause that the correct contents of
  1794. * the buffer will be lost. On the other hand we have to
  1795. * clear the buffer dirty bit at latest at the moment when the
  1796. * transaction marking the buffer as freed in the filesystem
  1797. * structures is committed because from that moment on the
  1798. * block can be reallocated and used by a different page.
  1799. * Since the block hasn't been freed yet but the inode has
  1800. * already been added to orphan list, it is safe for us to add
  1801. * the buffer to BJ_Forget list of the newest transaction.
  1802. *
  1803. * Also we have to clear buffer_mapped flag of a truncated buffer
  1804. * because the buffer_head may be attached to the page straddling
  1805. * i_size (can happen only when blocksize < pagesize) and thus the
  1806. * buffer_head can be reused when the file is extended again. So we end
  1807. * up keeping around invalidated buffers attached to transactions'
  1808. * BJ_Forget list just to stop checkpointing code from cleaning up
  1809. * the transaction this buffer was modified in.
  1810. */
  1811. transaction = jh->b_transaction;
  1812. if (transaction == NULL) {
  1813. /* First case: not on any transaction. If it
  1814. * has no checkpoint link, then we can zap it:
  1815. * it's a writeback-mode buffer so we don't care
  1816. * if it hits disk safely. */
  1817. if (!jh->b_cp_transaction) {
  1818. JBUFFER_TRACE(jh, "not on any transaction: zap");
  1819. goto zap_buffer;
  1820. }
  1821. if (!buffer_dirty(bh)) {
  1822. /* bdflush has written it. We can drop it now */
  1823. goto zap_buffer;
  1824. }
  1825. /* OK, it must be in the journal but still not
  1826. * written fully to disk: it's metadata or
  1827. * journaled data... */
  1828. if (journal->j_running_transaction) {
  1829. /* ... and once the current transaction has
  1830. * committed, the buffer won't be needed any
  1831. * longer. */
  1832. JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
  1833. may_free = __dispose_buffer(jh,
  1834. journal->j_running_transaction);
  1835. goto zap_buffer;
  1836. } else {
  1837. /* There is no currently-running transaction. So the
  1838. * orphan record which we wrote for this file must have
  1839. * passed into commit. We must attach this buffer to
  1840. * the committing transaction, if it exists. */
  1841. if (journal->j_committing_transaction) {
  1842. JBUFFER_TRACE(jh, "give to committing trans");
  1843. may_free = __dispose_buffer(jh,
  1844. journal->j_committing_transaction);
  1845. goto zap_buffer;
  1846. } else {
  1847. /* The orphan record's transaction has
  1848. * committed. We can cleanse this buffer */
  1849. clear_buffer_jbddirty(bh);
  1850. goto zap_buffer;
  1851. }
  1852. }
  1853. } else if (transaction == journal->j_committing_transaction) {
  1854. JBUFFER_TRACE(jh, "on committing transaction");
  1855. /*
  1856. * The buffer is committing, we simply cannot touch
  1857. * it. If the page is straddling i_size we have to wait
  1858. * for commit and try again.
  1859. */
  1860. if (partial_page) {
  1861. jbd2_journal_put_journal_head(jh);
  1862. spin_unlock(&journal->j_list_lock);
  1863. jbd_unlock_bh_state(bh);
  1864. write_unlock(&journal->j_state_lock);
  1865. return -EBUSY;
  1866. }
  1867. /*
  1868. * OK, buffer won't be reachable after truncate. We just set
  1869. * j_next_transaction to the running transaction (if there is
  1870. * one) and mark buffer as freed so that commit code knows it
  1871. * should clear dirty bits when it is done with the buffer.
  1872. */
  1873. set_buffer_freed(bh);
  1874. if (journal->j_running_transaction && buffer_jbddirty(bh))
  1875. jh->b_next_transaction = journal->j_running_transaction;
  1876. jbd2_journal_put_journal_head(jh);
  1877. spin_unlock(&journal->j_list_lock);
  1878. jbd_unlock_bh_state(bh);
  1879. write_unlock(&journal->j_state_lock);
  1880. return 0;
  1881. } else {
  1882. /* Good, the buffer belongs to the running transaction.
  1883. * We are writing our own transaction's data, not any
  1884. * previous one's, so it is safe to throw it away
  1885. * (remember that we expect the filesystem to have set
  1886. * i_size already for this truncate so recovery will not
  1887. * expose the disk blocks we are discarding here.) */
  1888. J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
  1889. JBUFFER_TRACE(jh, "on running transaction");
  1890. may_free = __dispose_buffer(jh, transaction);
  1891. }
  1892. zap_buffer:
  1893. /*
  1894. * This is tricky. Although the buffer is truncated, it may be reused
  1895. * if blocksize < pagesize and it is attached to the page straddling
  1896. * EOF. Since the buffer might have been added to BJ_Forget list of the
  1897. * running transaction, journal_get_write_access() won't clear
  1898. * b_modified and credit accounting gets confused. So clear b_modified
  1899. * here.
  1900. */
  1901. jh->b_modified = 0;
  1902. jbd2_journal_put_journal_head(jh);
  1903. zap_buffer_no_jh:
  1904. spin_unlock(&journal->j_list_lock);
  1905. jbd_unlock_bh_state(bh);
  1906. write_unlock(&journal->j_state_lock);
  1907. zap_buffer_unlocked:
  1908. clear_buffer_dirty(bh);
  1909. J_ASSERT_BH(bh, !buffer_jbddirty(bh));
  1910. clear_buffer_mapped(bh);
  1911. clear_buffer_req(bh);
  1912. clear_buffer_new(bh);
  1913. clear_buffer_delay(bh);
  1914. clear_buffer_unwritten(bh);
  1915. bh->b_bdev = NULL;
  1916. return may_free;
  1917. }
  1918. /**
  1919. * void jbd2_journal_invalidatepage()
  1920. * @journal: journal to use for flush...
  1921. * @page: page to flush
  1922. * @offset: start of the range to invalidate
  1923. * @length: length of the range to invalidate
  1924. *
  1925. * Reap page buffers containing data after in the specified range in page.
  1926. * Can return -EBUSY if buffers are part of the committing transaction and
  1927. * the page is straddling i_size. Caller then has to wait for current commit
  1928. * and try again.
  1929. */
  1930. int jbd2_journal_invalidatepage(journal_t *journal,
  1931. struct page *page,
  1932. unsigned int offset,
  1933. unsigned int length)
  1934. {
  1935. struct buffer_head *head, *bh, *next;
  1936. unsigned int stop = offset + length;
  1937. unsigned int curr_off = 0;
  1938. int partial_page = (offset || length < PAGE_CACHE_SIZE);
  1939. int may_free = 1;
  1940. int ret = 0;
  1941. if (!PageLocked(page))
  1942. BUG();
  1943. if (!page_has_buffers(page))
  1944. return 0;
  1945. BUG_ON(stop > PAGE_CACHE_SIZE || stop < length);
  1946. /* We will potentially be playing with lists other than just the
  1947. * data lists (especially for journaled data mode), so be
  1948. * cautious in our locking. */
  1949. head = bh = page_buffers(page);
  1950. do {
  1951. unsigned int next_off = curr_off + bh->b_size;
  1952. next = bh->b_this_page;
  1953. if (next_off > stop)
  1954. return 0;
  1955. if (offset <= curr_off) {
  1956. /* This block is wholly outside the truncation point */
  1957. lock_buffer(bh);
  1958. ret = journal_unmap_buffer(journal, bh, partial_page);
  1959. unlock_buffer(bh);
  1960. if (ret < 0)
  1961. return ret;
  1962. may_free &= ret;
  1963. }
  1964. curr_off = next_off;
  1965. bh = next;
  1966. } while (bh != head);
  1967. if (!partial_page) {
  1968. if (may_free && try_to_free_buffers(page))
  1969. J_ASSERT(!page_has_buffers(page));
  1970. }
  1971. return 0;
  1972. }
  1973. /*
  1974. * File a buffer on the given transaction list.
  1975. */
  1976. void __jbd2_journal_file_buffer(struct journal_head *jh,
  1977. transaction_t *transaction, int jlist)
  1978. {
  1979. struct journal_head **list = NULL;
  1980. int was_dirty = 0;
  1981. struct buffer_head *bh = jh2bh(jh);
  1982. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1983. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1984. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1985. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  1986. jh->b_transaction == NULL);
  1987. if (jh->b_transaction && jh->b_jlist == jlist)
  1988. return;
  1989. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  1990. jlist == BJ_Shadow || jlist == BJ_Forget) {
  1991. /*
  1992. * For metadata buffers, we track dirty bit in buffer_jbddirty
  1993. * instead of buffer_dirty. We should not see a dirty bit set
  1994. * here because we clear it in do_get_write_access but e.g.
  1995. * tune2fs can modify the sb and set the dirty bit at any time
  1996. * so we try to gracefully handle that.
  1997. */
  1998. if (buffer_dirty(bh))
  1999. warn_dirty_buffer(bh);
  2000. if (test_clear_buffer_dirty(bh) ||
  2001. test_clear_buffer_jbddirty(bh))
  2002. was_dirty = 1;
  2003. }
  2004. if (jh->b_transaction)
  2005. __jbd2_journal_temp_unlink_buffer(jh);
  2006. else
  2007. jbd2_journal_grab_journal_head(bh);
  2008. jh->b_transaction = transaction;
  2009. switch (jlist) {
  2010. case BJ_None:
  2011. J_ASSERT_JH(jh, !jh->b_committed_data);
  2012. J_ASSERT_JH(jh, !jh->b_frozen_data);
  2013. return;
  2014. case BJ_Metadata:
  2015. transaction->t_nr_buffers++;
  2016. list = &transaction->t_buffers;
  2017. break;
  2018. case BJ_Forget:
  2019. list = &transaction->t_forget;
  2020. break;
  2021. case BJ_Shadow:
  2022. list = &transaction->t_shadow_list;
  2023. break;
  2024. case BJ_Reserved:
  2025. list = &transaction->t_reserved_list;
  2026. break;
  2027. }
  2028. __blist_add_buffer(list, jh);
  2029. jh->b_jlist = jlist;
  2030. if (was_dirty)
  2031. set_buffer_jbddirty(bh);
  2032. }
  2033. void jbd2_journal_file_buffer(struct journal_head *jh,
  2034. transaction_t *transaction, int jlist)
  2035. {
  2036. jbd_lock_bh_state(jh2bh(jh));
  2037. spin_lock(&transaction->t_journal->j_list_lock);
  2038. __jbd2_journal_file_buffer(jh, transaction, jlist);
  2039. spin_unlock(&transaction->t_journal->j_list_lock);
  2040. jbd_unlock_bh_state(jh2bh(jh));
  2041. }
  2042. /*
  2043. * Remove a buffer from its current buffer list in preparation for
  2044. * dropping it from its current transaction entirely. If the buffer has
  2045. * already started to be used by a subsequent transaction, refile the
  2046. * buffer on that transaction's metadata list.
  2047. *
  2048. * Called under j_list_lock
  2049. * Called under jbd_lock_bh_state(jh2bh(jh))
  2050. *
  2051. * jh and bh may be already free when this function returns
  2052. */
  2053. void __jbd2_journal_refile_buffer(struct journal_head *jh)
  2054. {
  2055. int was_dirty, jlist;
  2056. struct buffer_head *bh = jh2bh(jh);
  2057. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  2058. if (jh->b_transaction)
  2059. assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
  2060. /* If the buffer is now unused, just drop it. */
  2061. if (jh->b_next_transaction == NULL) {
  2062. __jbd2_journal_unfile_buffer(jh);
  2063. return;
  2064. }
  2065. /*
  2066. * It has been modified by a later transaction: add it to the new
  2067. * transaction's metadata list.
  2068. */
  2069. was_dirty = test_clear_buffer_jbddirty(bh);
  2070. __jbd2_journal_temp_unlink_buffer(jh);
  2071. /*
  2072. * We set b_transaction here because b_next_transaction will inherit
  2073. * our jh reference and thus __jbd2_journal_file_buffer() must not
  2074. * take a new one.
  2075. */
  2076. jh->b_transaction = jh->b_next_transaction;
  2077. jh->b_next_transaction = NULL;
  2078. if (buffer_freed(bh))
  2079. jlist = BJ_Forget;
  2080. else if (jh->b_modified)
  2081. jlist = BJ_Metadata;
  2082. else
  2083. jlist = BJ_Reserved;
  2084. __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
  2085. J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
  2086. if (was_dirty)
  2087. set_buffer_jbddirty(bh);
  2088. }
  2089. /*
  2090. * __jbd2_journal_refile_buffer() with necessary locking added. We take our
  2091. * bh reference so that we can safely unlock bh.
  2092. *
  2093. * The jh and bh may be freed by this call.
  2094. */
  2095. void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
  2096. {
  2097. struct buffer_head *bh = jh2bh(jh);
  2098. /* Get reference so that buffer cannot be freed before we unlock it */
  2099. get_bh(bh);
  2100. jbd_lock_bh_state(bh);
  2101. spin_lock(&journal->j_list_lock);
  2102. __jbd2_journal_refile_buffer(jh);
  2103. jbd_unlock_bh_state(bh);
  2104. spin_unlock(&journal->j_list_lock);
  2105. __brelse(bh);
  2106. }
  2107. /*
  2108. * File inode in the inode list of the handle's transaction
  2109. */
  2110. int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
  2111. {
  2112. transaction_t *transaction = handle->h_transaction;
  2113. journal_t *journal = transaction->t_journal;
  2114. if (is_handle_aborted(handle))
  2115. return -EIO;
  2116. jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
  2117. transaction->t_tid);
  2118. /*
  2119. * First check whether inode isn't already on the transaction's
  2120. * lists without taking the lock. Note that this check is safe
  2121. * without the lock as we cannot race with somebody removing inode
  2122. * from the transaction. The reason is that we remove inode from the
  2123. * transaction only in journal_release_jbd_inode() and when we commit
  2124. * the transaction. We are guarded from the first case by holding
  2125. * a reference to the inode. We are safe against the second case
  2126. * because if jinode->i_transaction == transaction, commit code
  2127. * cannot touch the transaction because we hold reference to it,
  2128. * and if jinode->i_next_transaction == transaction, commit code
  2129. * will only file the inode where we want it.
  2130. */
  2131. if (jinode->i_transaction == transaction ||
  2132. jinode->i_next_transaction == transaction)
  2133. return 0;
  2134. spin_lock(&journal->j_list_lock);
  2135. if (jinode->i_transaction == transaction ||
  2136. jinode->i_next_transaction == transaction)
  2137. goto done;
  2138. /*
  2139. * We only ever set this variable to 1 so the test is safe. Since
  2140. * t_need_data_flush is likely to be set, we do the test to save some
  2141. * cacheline bouncing
  2142. */
  2143. if (!transaction->t_need_data_flush)
  2144. transaction->t_need_data_flush = 1;
  2145. /* On some different transaction's list - should be
  2146. * the committing one */
  2147. if (jinode->i_transaction) {
  2148. J_ASSERT(jinode->i_next_transaction == NULL);
  2149. J_ASSERT(jinode->i_transaction ==
  2150. journal->j_committing_transaction);
  2151. jinode->i_next_transaction = transaction;
  2152. goto done;
  2153. }
  2154. /* Not on any transaction list... */
  2155. J_ASSERT(!jinode->i_next_transaction);
  2156. jinode->i_transaction = transaction;
  2157. list_add(&jinode->i_list, &transaction->t_inode_list);
  2158. done:
  2159. spin_unlock(&journal->j_list_lock);
  2160. return 0;
  2161. }
  2162. /*
  2163. * File truncate and transaction commit interact with each other in a
  2164. * non-trivial way. If a transaction writing data block A is
  2165. * committing, we cannot discard the data by truncate until we have
  2166. * written them. Otherwise if we crashed after the transaction with
  2167. * write has committed but before the transaction with truncate has
  2168. * committed, we could see stale data in block A. This function is a
  2169. * helper to solve this problem. It starts writeout of the truncated
  2170. * part in case it is in the committing transaction.
  2171. *
  2172. * Filesystem code must call this function when inode is journaled in
  2173. * ordered mode before truncation happens and after the inode has been
  2174. * placed on orphan list with the new inode size. The second condition
  2175. * avoids the race that someone writes new data and we start
  2176. * committing the transaction after this function has been called but
  2177. * before a transaction for truncate is started (and furthermore it
  2178. * allows us to optimize the case where the addition to orphan list
  2179. * happens in the same transaction as write --- we don't have to write
  2180. * any data in such case).
  2181. */
  2182. int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  2183. struct jbd2_inode *jinode,
  2184. loff_t new_size)
  2185. {
  2186. transaction_t *inode_trans, *commit_trans;
  2187. int ret = 0;
  2188. /* This is a quick check to avoid locking if not necessary */
  2189. if (!jinode->i_transaction)
  2190. goto out;
  2191. /* Locks are here just to force reading of recent values, it is
  2192. * enough that the transaction was not committing before we started
  2193. * a transaction adding the inode to orphan list */
  2194. read_lock(&journal->j_state_lock);
  2195. commit_trans = journal->j_committing_transaction;
  2196. read_unlock(&journal->j_state_lock);
  2197. spin_lock(&journal->j_list_lock);
  2198. inode_trans = jinode->i_transaction;
  2199. spin_unlock(&journal->j_list_lock);
  2200. if (inode_trans == commit_trans) {
  2201. ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  2202. new_size, LLONG_MAX);
  2203. if (ret)
  2204. jbd2_journal_abort(journal, ret);
  2205. }
  2206. out:
  2207. return ret;
  2208. }