transaction.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  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. current->journal_info = handle;
  338. lock_map_acquire(&handle->h_lockdep_map);
  339. jbd2_journal_free_transaction(new_transaction);
  340. return 0;
  341. }
  342. static struct lock_class_key jbd2_handle_key;
  343. /* Allocate a new handle. This should probably be in a slab... */
  344. static handle_t *new_handle(int nblocks)
  345. {
  346. handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
  347. if (!handle)
  348. return NULL;
  349. handle->h_buffer_credits = nblocks;
  350. handle->h_ref = 1;
  351. lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
  352. &jbd2_handle_key, 0);
  353. return handle;
  354. }
  355. /**
  356. * handle_t *jbd2_journal_start() - Obtain a new handle.
  357. * @journal: Journal to start transaction on.
  358. * @nblocks: number of block buffer we might modify
  359. *
  360. * We make sure that the transaction can guarantee at least nblocks of
  361. * modified buffers in the log. We block until the log can guarantee
  362. * that much space. Additionally, if rsv_blocks > 0, we also create another
  363. * handle with rsv_blocks reserved blocks in the journal. This handle is
  364. * is stored in h_rsv_handle. It is not attached to any particular transaction
  365. * and thus doesn't block transaction commit. If the caller uses this reserved
  366. * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
  367. * on the parent handle will dispose the reserved one. Reserved handle has to
  368. * be converted to a normal handle using jbd2_journal_start_reserved() before
  369. * it can be used.
  370. *
  371. * Return a pointer to a newly allocated handle, or an ERR_PTR() value
  372. * on failure.
  373. */
  374. handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
  375. gfp_t gfp_mask, unsigned int type,
  376. unsigned int line_no)
  377. {
  378. handle_t *handle = journal_current_handle();
  379. int err;
  380. if (!journal)
  381. return ERR_PTR(-EROFS);
  382. if (handle) {
  383. J_ASSERT(handle->h_transaction->t_journal == journal);
  384. handle->h_ref++;
  385. return handle;
  386. }
  387. handle = new_handle(nblocks);
  388. if (!handle)
  389. return ERR_PTR(-ENOMEM);
  390. if (rsv_blocks) {
  391. handle_t *rsv_handle;
  392. rsv_handle = new_handle(rsv_blocks);
  393. if (!rsv_handle) {
  394. jbd2_free_handle(handle);
  395. return ERR_PTR(-ENOMEM);
  396. }
  397. rsv_handle->h_reserved = 1;
  398. rsv_handle->h_journal = journal;
  399. handle->h_rsv_handle = rsv_handle;
  400. }
  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. return ERR_PTR(err);
  407. }
  408. handle->h_type = type;
  409. handle->h_line_no = line_no;
  410. trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
  411. handle->h_transaction->t_tid, type,
  412. line_no, nblocks);
  413. return handle;
  414. }
  415. EXPORT_SYMBOL(jbd2__journal_start);
  416. handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
  417. {
  418. return jbd2__journal_start(journal, nblocks, 0, GFP_NOFS, 0, 0);
  419. }
  420. EXPORT_SYMBOL(jbd2_journal_start);
  421. void jbd2_journal_free_reserved(handle_t *handle)
  422. {
  423. journal_t *journal = handle->h_journal;
  424. WARN_ON(!handle->h_reserved);
  425. sub_reserved_credits(journal, handle->h_buffer_credits);
  426. jbd2_free_handle(handle);
  427. }
  428. EXPORT_SYMBOL(jbd2_journal_free_reserved);
  429. /**
  430. * int jbd2_journal_start_reserved(handle_t *handle) - start reserved handle
  431. * @handle: handle to start
  432. *
  433. * Start handle that has been previously reserved with jbd2_journal_reserve().
  434. * This attaches @handle to the running transaction (or creates one if there's
  435. * not transaction running). Unlike jbd2_journal_start() this function cannot
  436. * block on journal commit, checkpointing, or similar stuff. It can block on
  437. * memory allocation or frozen journal though.
  438. *
  439. * Return 0 on success, non-zero on error - handle is freed in that case.
  440. */
  441. int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
  442. unsigned int line_no)
  443. {
  444. journal_t *journal = handle->h_journal;
  445. int ret = -EIO;
  446. if (WARN_ON(!handle->h_reserved)) {
  447. /* Someone passed in normal handle? Just stop it. */
  448. jbd2_journal_stop(handle);
  449. return ret;
  450. }
  451. /*
  452. * Usefulness of mixing of reserved and unreserved handles is
  453. * questionable. So far nobody seems to need it so just error out.
  454. */
  455. if (WARN_ON(current->journal_info)) {
  456. jbd2_journal_free_reserved(handle);
  457. return ret;
  458. }
  459. handle->h_journal = NULL;
  460. /*
  461. * GFP_NOFS is here because callers are likely from writeback or
  462. * similarly constrained call sites
  463. */
  464. ret = start_this_handle(journal, handle, GFP_NOFS);
  465. if (ret < 0)
  466. jbd2_journal_free_reserved(handle);
  467. handle->h_type = type;
  468. handle->h_line_no = line_no;
  469. return ret;
  470. }
  471. EXPORT_SYMBOL(jbd2_journal_start_reserved);
  472. /**
  473. * int jbd2_journal_extend() - extend buffer credits.
  474. * @handle: handle to 'extend'
  475. * @nblocks: nr blocks to try to extend by.
  476. *
  477. * Some transactions, such as large extends and truncates, can be done
  478. * atomically all at once or in several stages. The operation requests
  479. * a credit for a number of buffer modications in advance, but can
  480. * extend its credit if it needs more.
  481. *
  482. * jbd2_journal_extend tries to give the running handle more buffer credits.
  483. * It does not guarantee that allocation - this is a best-effort only.
  484. * The calling process MUST be able to deal cleanly with a failure to
  485. * extend here.
  486. *
  487. * Return 0 on success, non-zero on failure.
  488. *
  489. * return code < 0 implies an error
  490. * return code > 0 implies normal transaction-full status.
  491. */
  492. int jbd2_journal_extend(handle_t *handle, int nblocks)
  493. {
  494. transaction_t *transaction = handle->h_transaction;
  495. journal_t *journal;
  496. int result;
  497. int wanted;
  498. WARN_ON(!transaction);
  499. if (is_handle_aborted(handle))
  500. return -EROFS;
  501. journal = transaction->t_journal;
  502. result = 1;
  503. read_lock(&journal->j_state_lock);
  504. /* Don't extend a locked-down transaction! */
  505. if (transaction->t_state != T_RUNNING) {
  506. jbd_debug(3, "denied handle %p %d blocks: "
  507. "transaction not running\n", handle, nblocks);
  508. goto error_out;
  509. }
  510. spin_lock(&transaction->t_handle_lock);
  511. wanted = atomic_add_return(nblocks,
  512. &transaction->t_outstanding_credits);
  513. if (wanted > journal->j_max_transaction_buffers) {
  514. jbd_debug(3, "denied handle %p %d blocks: "
  515. "transaction too large\n", handle, nblocks);
  516. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  517. goto unlock;
  518. }
  519. if (wanted + (wanted >> JBD2_CONTROL_BLOCKS_SHIFT) >
  520. jbd2_log_space_left(journal)) {
  521. jbd_debug(3, "denied handle %p %d blocks: "
  522. "insufficient log space\n", handle, nblocks);
  523. atomic_sub(nblocks, &transaction->t_outstanding_credits);
  524. goto unlock;
  525. }
  526. trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
  527. transaction->t_tid,
  528. handle->h_type, handle->h_line_no,
  529. handle->h_buffer_credits,
  530. nblocks);
  531. handle->h_buffer_credits += nblocks;
  532. handle->h_requested_credits += nblocks;
  533. result = 0;
  534. jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
  535. unlock:
  536. spin_unlock(&transaction->t_handle_lock);
  537. error_out:
  538. read_unlock(&journal->j_state_lock);
  539. return result;
  540. }
  541. /**
  542. * int jbd2_journal_restart() - restart a handle .
  543. * @handle: handle to restart
  544. * @nblocks: nr credits requested
  545. *
  546. * Restart a handle for a multi-transaction filesystem
  547. * operation.
  548. *
  549. * If the jbd2_journal_extend() call above fails to grant new buffer credits
  550. * to a running handle, a call to jbd2_journal_restart will commit the
  551. * handle's transaction so far and reattach the handle to a new
  552. * transaction capabable of guaranteeing the requested number of
  553. * credits. We preserve reserved handle if there's any attached to the
  554. * passed in handle.
  555. */
  556. int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
  557. {
  558. transaction_t *transaction = handle->h_transaction;
  559. journal_t *journal;
  560. tid_t tid;
  561. int need_to_start, ret;
  562. WARN_ON(!transaction);
  563. /* If we've had an abort of any type, don't even think about
  564. * actually doing the restart! */
  565. if (is_handle_aborted(handle))
  566. return 0;
  567. journal = transaction->t_journal;
  568. /*
  569. * First unlink the handle from its current transaction, and start the
  570. * commit on that.
  571. */
  572. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  573. J_ASSERT(journal_current_handle() == handle);
  574. read_lock(&journal->j_state_lock);
  575. spin_lock(&transaction->t_handle_lock);
  576. atomic_sub(handle->h_buffer_credits,
  577. &transaction->t_outstanding_credits);
  578. if (handle->h_rsv_handle) {
  579. sub_reserved_credits(journal,
  580. handle->h_rsv_handle->h_buffer_credits);
  581. }
  582. if (atomic_dec_and_test(&transaction->t_updates))
  583. wake_up(&journal->j_wait_updates);
  584. tid = transaction->t_tid;
  585. spin_unlock(&transaction->t_handle_lock);
  586. handle->h_transaction = NULL;
  587. current->journal_info = NULL;
  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 = handle->h_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. WARN_ON(!transaction);
  707. if (is_handle_aborted(handle))
  708. return -EROFS;
  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;
  937. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  938. int err;
  939. jbd_debug(5, "journal_head %p\n", jh);
  940. WARN_ON(!transaction);
  941. err = -EROFS;
  942. if (is_handle_aborted(handle))
  943. goto out;
  944. journal = transaction->t_journal;
  945. err = 0;
  946. JBUFFER_TRACE(jh, "entry");
  947. /*
  948. * The buffer may already belong to this transaction due to pre-zeroing
  949. * in the filesystem's new_block code. It may also be on the previous,
  950. * committing transaction's lists, but it HAS to be in Forget state in
  951. * that case: the transaction must have deleted the buffer for it to be
  952. * reused here.
  953. */
  954. jbd_lock_bh_state(bh);
  955. spin_lock(&journal->j_list_lock);
  956. J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
  957. jh->b_transaction == NULL ||
  958. (jh->b_transaction == journal->j_committing_transaction &&
  959. jh->b_jlist == BJ_Forget)));
  960. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  961. J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
  962. if (jh->b_transaction == NULL) {
  963. /*
  964. * Previous jbd2_journal_forget() could have left the buffer
  965. * with jbddirty bit set because it was being committed. When
  966. * the commit finished, we've filed the buffer for
  967. * checkpointing and marked it dirty. Now we are reallocating
  968. * the buffer so the transaction freeing it must have
  969. * committed and so it's safe to clear the dirty bit.
  970. */
  971. clear_buffer_dirty(jh2bh(jh));
  972. /* first access by this transaction */
  973. jh->b_modified = 0;
  974. JBUFFER_TRACE(jh, "file as BJ_Reserved");
  975. __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
  976. } else if (jh->b_transaction == journal->j_committing_transaction) {
  977. /* first access by this transaction */
  978. jh->b_modified = 0;
  979. JBUFFER_TRACE(jh, "set next transaction");
  980. jh->b_next_transaction = transaction;
  981. }
  982. spin_unlock(&journal->j_list_lock);
  983. jbd_unlock_bh_state(bh);
  984. /*
  985. * akpm: I added this. ext3_alloc_branch can pick up new indirect
  986. * blocks which contain freed but then revoked metadata. We need
  987. * to cancel the revoke in case we end up freeing it yet again
  988. * and the reallocating as data - this would cause a second revoke,
  989. * which hits an assertion error.
  990. */
  991. JBUFFER_TRACE(jh, "cancelling revoke");
  992. jbd2_journal_cancel_revoke(handle, jh);
  993. out:
  994. jbd2_journal_put_journal_head(jh);
  995. return err;
  996. }
  997. /**
  998. * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
  999. * non-rewindable consequences
  1000. * @handle: transaction
  1001. * @bh: buffer to undo
  1002. *
  1003. * Sometimes there is a need to distinguish between metadata which has
  1004. * been committed to disk and that which has not. The ext3fs code uses
  1005. * this for freeing and allocating space, we have to make sure that we
  1006. * do not reuse freed space until the deallocation has been committed,
  1007. * since if we overwrote that space we would make the delete
  1008. * un-rewindable in case of a crash.
  1009. *
  1010. * To deal with that, jbd2_journal_get_undo_access requests write access to a
  1011. * buffer for parts of non-rewindable operations such as delete
  1012. * operations on the bitmaps. The journaling code must keep a copy of
  1013. * the buffer's contents prior to the undo_access call until such time
  1014. * as we know that the buffer has definitely been committed to disk.
  1015. *
  1016. * We never need to know which transaction the committed data is part
  1017. * of, buffers touched here are guaranteed to be dirtied later and so
  1018. * will be committed to a new transaction in due course, at which point
  1019. * we can discard the old committed data pointer.
  1020. *
  1021. * Returns error number or 0 on success.
  1022. */
  1023. int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
  1024. {
  1025. int err;
  1026. struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  1027. char *committed_data = NULL;
  1028. JBUFFER_TRACE(jh, "entry");
  1029. /*
  1030. * Do this first --- it can drop the journal lock, so we want to
  1031. * make sure that obtaining the committed_data is done
  1032. * atomically wrt. completion of any outstanding commits.
  1033. */
  1034. err = do_get_write_access(handle, jh, 1);
  1035. if (err)
  1036. goto out;
  1037. repeat:
  1038. if (!jh->b_committed_data) {
  1039. committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
  1040. if (!committed_data) {
  1041. printk(KERN_EMERG "%s: No memory for committed data\n",
  1042. __func__);
  1043. err = -ENOMEM;
  1044. goto out;
  1045. }
  1046. }
  1047. jbd_lock_bh_state(bh);
  1048. if (!jh->b_committed_data) {
  1049. /* Copy out the current buffer contents into the
  1050. * preserved, committed copy. */
  1051. JBUFFER_TRACE(jh, "generate b_committed data");
  1052. if (!committed_data) {
  1053. jbd_unlock_bh_state(bh);
  1054. goto repeat;
  1055. }
  1056. jh->b_committed_data = committed_data;
  1057. committed_data = NULL;
  1058. memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
  1059. }
  1060. jbd_unlock_bh_state(bh);
  1061. out:
  1062. jbd2_journal_put_journal_head(jh);
  1063. if (unlikely(committed_data))
  1064. jbd2_free(committed_data, bh->b_size);
  1065. return err;
  1066. }
  1067. /**
  1068. * void jbd2_journal_set_triggers() - Add triggers for commit writeout
  1069. * @bh: buffer to trigger on
  1070. * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
  1071. *
  1072. * Set any triggers on this journal_head. This is always safe, because
  1073. * triggers for a committing buffer will be saved off, and triggers for
  1074. * a running transaction will match the buffer in that transaction.
  1075. *
  1076. * Call with NULL to clear the triggers.
  1077. */
  1078. void jbd2_journal_set_triggers(struct buffer_head *bh,
  1079. struct jbd2_buffer_trigger_type *type)
  1080. {
  1081. struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
  1082. if (WARN_ON(!jh))
  1083. return;
  1084. jh->b_triggers = type;
  1085. jbd2_journal_put_journal_head(jh);
  1086. }
  1087. void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
  1088. struct jbd2_buffer_trigger_type *triggers)
  1089. {
  1090. struct buffer_head *bh = jh2bh(jh);
  1091. if (!triggers || !triggers->t_frozen)
  1092. return;
  1093. triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
  1094. }
  1095. void jbd2_buffer_abort_trigger(struct journal_head *jh,
  1096. struct jbd2_buffer_trigger_type *triggers)
  1097. {
  1098. if (!triggers || !triggers->t_abort)
  1099. return;
  1100. triggers->t_abort(triggers, jh2bh(jh));
  1101. }
  1102. /**
  1103. * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
  1104. * @handle: transaction to add buffer to.
  1105. * @bh: buffer to mark
  1106. *
  1107. * mark dirty metadata which needs to be journaled as part of the current
  1108. * transaction.
  1109. *
  1110. * The buffer must have previously had jbd2_journal_get_write_access()
  1111. * called so that it has a valid journal_head attached to the buffer
  1112. * head.
  1113. *
  1114. * The buffer is placed on the transaction's metadata list and is marked
  1115. * as belonging to the transaction.
  1116. *
  1117. * Returns error number or 0 on success.
  1118. *
  1119. * Special care needs to be taken if the buffer already belongs to the
  1120. * current committing transaction (in which case we should have frozen
  1121. * data present for that commit). In that case, we don't relink the
  1122. * buffer: that only gets done when the old transaction finally
  1123. * completes its commit.
  1124. */
  1125. int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
  1126. {
  1127. transaction_t *transaction = handle->h_transaction;
  1128. journal_t *journal;
  1129. struct journal_head *jh;
  1130. int ret = 0;
  1131. WARN_ON(!transaction);
  1132. if (is_handle_aborted(handle))
  1133. return -EROFS;
  1134. journal = transaction->t_journal;
  1135. jh = jbd2_journal_grab_journal_head(bh);
  1136. if (!jh) {
  1137. ret = -EUCLEAN;
  1138. goto out;
  1139. }
  1140. jbd_debug(5, "journal_head %p\n", jh);
  1141. JBUFFER_TRACE(jh, "entry");
  1142. jbd_lock_bh_state(bh);
  1143. if (jh->b_modified == 0) {
  1144. /*
  1145. * This buffer's got modified and becoming part
  1146. * of the transaction. This needs to be done
  1147. * once a transaction -bzzz
  1148. */
  1149. jh->b_modified = 1;
  1150. J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
  1151. handle->h_buffer_credits--;
  1152. }
  1153. /*
  1154. * fastpath, to avoid expensive locking. If this buffer is already
  1155. * on the running transaction's metadata list there is nothing to do.
  1156. * Nobody can take it off again because there is a handle open.
  1157. * I _think_ we're OK here with SMP barriers - a mistaken decision will
  1158. * result in this test being false, so we go in and take the locks.
  1159. */
  1160. if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
  1161. JBUFFER_TRACE(jh, "fastpath");
  1162. if (unlikely(jh->b_transaction !=
  1163. journal->j_running_transaction)) {
  1164. printk(KERN_EMERG "JBD: %s: "
  1165. "jh->b_transaction (%llu, %p, %u) != "
  1166. "journal->j_running_transaction (%p, %u)",
  1167. journal->j_devname,
  1168. (unsigned long long) bh->b_blocknr,
  1169. jh->b_transaction,
  1170. jh->b_transaction ? jh->b_transaction->t_tid : 0,
  1171. journal->j_running_transaction,
  1172. journal->j_running_transaction ?
  1173. journal->j_running_transaction->t_tid : 0);
  1174. ret = -EINVAL;
  1175. }
  1176. goto out_unlock_bh;
  1177. }
  1178. set_buffer_jbddirty(bh);
  1179. /*
  1180. * Metadata already on the current transaction list doesn't
  1181. * need to be filed. Metadata on another transaction's list must
  1182. * be committing, and will be refiled once the commit completes:
  1183. * leave it alone for now.
  1184. */
  1185. if (jh->b_transaction != transaction) {
  1186. JBUFFER_TRACE(jh, "already on other transaction");
  1187. if (unlikely(jh->b_transaction !=
  1188. journal->j_committing_transaction)) {
  1189. printk(KERN_EMERG "JBD: %s: "
  1190. "jh->b_transaction (%llu, %p, %u) != "
  1191. "journal->j_committing_transaction (%p, %u)",
  1192. journal->j_devname,
  1193. (unsigned long long) bh->b_blocknr,
  1194. jh->b_transaction,
  1195. jh->b_transaction ? jh->b_transaction->t_tid : 0,
  1196. journal->j_committing_transaction,
  1197. journal->j_committing_transaction ?
  1198. journal->j_committing_transaction->t_tid : 0);
  1199. ret = -EINVAL;
  1200. }
  1201. if (unlikely(jh->b_next_transaction != transaction)) {
  1202. printk(KERN_EMERG "JBD: %s: "
  1203. "jh->b_next_transaction (%llu, %p, %u) != "
  1204. "transaction (%p, %u)",
  1205. journal->j_devname,
  1206. (unsigned long long) bh->b_blocknr,
  1207. jh->b_next_transaction,
  1208. jh->b_next_transaction ?
  1209. jh->b_next_transaction->t_tid : 0,
  1210. transaction, transaction->t_tid);
  1211. ret = -EINVAL;
  1212. }
  1213. /* And this case is illegal: we can't reuse another
  1214. * transaction's data buffer, ever. */
  1215. goto out_unlock_bh;
  1216. }
  1217. /* That test should have eliminated the following case: */
  1218. J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
  1219. JBUFFER_TRACE(jh, "file as BJ_Metadata");
  1220. spin_lock(&journal->j_list_lock);
  1221. __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
  1222. spin_unlock(&journal->j_list_lock);
  1223. out_unlock_bh:
  1224. jbd_unlock_bh_state(bh);
  1225. jbd2_journal_put_journal_head(jh);
  1226. out:
  1227. JBUFFER_TRACE(jh, "exit");
  1228. WARN_ON(ret); /* All errors are bugs, so dump the stack */
  1229. return ret;
  1230. }
  1231. /**
  1232. * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
  1233. * @handle: transaction handle
  1234. * @bh: bh to 'forget'
  1235. *
  1236. * We can only do the bforget if there are no commits pending against the
  1237. * buffer. If the buffer is dirty in the current running transaction we
  1238. * can safely unlink it.
  1239. *
  1240. * bh may not be a journalled buffer at all - it may be a non-JBD
  1241. * buffer which came off the hashtable. Check for this.
  1242. *
  1243. * Decrements bh->b_count by one.
  1244. *
  1245. * Allow this call even if the handle has aborted --- it may be part of
  1246. * the caller's cleanup after an abort.
  1247. */
  1248. int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
  1249. {
  1250. transaction_t *transaction = handle->h_transaction;
  1251. journal_t *journal;
  1252. struct journal_head *jh;
  1253. int drop_reserve = 0;
  1254. int err = 0;
  1255. int was_modified = 0;
  1256. WARN_ON(!transaction);
  1257. if (is_handle_aborted(handle))
  1258. return -EROFS;
  1259. journal = transaction->t_journal;
  1260. BUFFER_TRACE(bh, "entry");
  1261. jbd_lock_bh_state(bh);
  1262. spin_lock(&journal->j_list_lock);
  1263. if (!buffer_jbd(bh))
  1264. goto not_jbd;
  1265. jh = bh2jh(bh);
  1266. /* Critical error: attempting to delete a bitmap buffer, maybe?
  1267. * Don't do any jbd operations, and return an error. */
  1268. if (!J_EXPECT_JH(jh, !jh->b_committed_data,
  1269. "inconsistent data on disk")) {
  1270. err = -EIO;
  1271. goto not_jbd;
  1272. }
  1273. /* keep track of whether or not this transaction modified us */
  1274. was_modified = jh->b_modified;
  1275. /*
  1276. * The buffer's going from the transaction, we must drop
  1277. * all references -bzzz
  1278. */
  1279. jh->b_modified = 0;
  1280. if (jh->b_transaction == transaction) {
  1281. J_ASSERT_JH(jh, !jh->b_frozen_data);
  1282. /* If we are forgetting a buffer which is already part
  1283. * of this transaction, then we can just drop it from
  1284. * the transaction immediately. */
  1285. clear_buffer_dirty(bh);
  1286. clear_buffer_jbddirty(bh);
  1287. JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
  1288. /*
  1289. * we only want to drop a reference if this transaction
  1290. * modified the buffer
  1291. */
  1292. if (was_modified)
  1293. drop_reserve = 1;
  1294. /*
  1295. * We are no longer going to journal this buffer.
  1296. * However, the commit of this transaction is still
  1297. * important to the buffer: the delete that we are now
  1298. * processing might obsolete an old log entry, so by
  1299. * committing, we can satisfy the buffer's checkpoint.
  1300. *
  1301. * So, if we have a checkpoint on the buffer, we should
  1302. * now refile the buffer on our BJ_Forget list so that
  1303. * we know to remove the checkpoint after we commit.
  1304. */
  1305. if (jh->b_cp_transaction) {
  1306. __jbd2_journal_temp_unlink_buffer(jh);
  1307. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1308. } else {
  1309. __jbd2_journal_unfile_buffer(jh);
  1310. if (!buffer_jbd(bh)) {
  1311. spin_unlock(&journal->j_list_lock);
  1312. jbd_unlock_bh_state(bh);
  1313. __bforget(bh);
  1314. goto drop;
  1315. }
  1316. }
  1317. } else if (jh->b_transaction) {
  1318. J_ASSERT_JH(jh, (jh->b_transaction ==
  1319. journal->j_committing_transaction));
  1320. /* However, if the buffer is still owned by a prior
  1321. * (committing) transaction, we can't drop it yet... */
  1322. JBUFFER_TRACE(jh, "belongs to older transaction");
  1323. /* ... but we CAN drop it from the new transaction if we
  1324. * have also modified it since the original commit. */
  1325. if (jh->b_next_transaction) {
  1326. J_ASSERT(jh->b_next_transaction == transaction);
  1327. jh->b_next_transaction = NULL;
  1328. /*
  1329. * only drop a reference if this transaction modified
  1330. * the buffer
  1331. */
  1332. if (was_modified)
  1333. drop_reserve = 1;
  1334. }
  1335. }
  1336. not_jbd:
  1337. spin_unlock(&journal->j_list_lock);
  1338. jbd_unlock_bh_state(bh);
  1339. __brelse(bh);
  1340. drop:
  1341. if (drop_reserve) {
  1342. /* no need to reserve log space for this block -bzzz */
  1343. handle->h_buffer_credits++;
  1344. }
  1345. return err;
  1346. }
  1347. /**
  1348. * int jbd2_journal_stop() - complete a transaction
  1349. * @handle: tranaction to complete.
  1350. *
  1351. * All done for a particular handle.
  1352. *
  1353. * There is not much action needed here. We just return any remaining
  1354. * buffer credits to the transaction and remove the handle. The only
  1355. * complication is that we need to start a commit operation if the
  1356. * filesystem is marked for synchronous update.
  1357. *
  1358. * jbd2_journal_stop itself will not usually return an error, but it may
  1359. * do so in unusual circumstances. In particular, expect it to
  1360. * return -EIO if a jbd2_journal_abort has been executed since the
  1361. * transaction began.
  1362. */
  1363. int jbd2_journal_stop(handle_t *handle)
  1364. {
  1365. transaction_t *transaction = handle->h_transaction;
  1366. journal_t *journal;
  1367. int err = 0, wait_for_commit = 0;
  1368. tid_t tid;
  1369. pid_t pid;
  1370. if (!transaction)
  1371. goto free_and_exit;
  1372. journal = transaction->t_journal;
  1373. J_ASSERT(journal_current_handle() == handle);
  1374. if (is_handle_aborted(handle))
  1375. err = -EIO;
  1376. else
  1377. J_ASSERT(atomic_read(&transaction->t_updates) > 0);
  1378. if (--handle->h_ref > 0) {
  1379. jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
  1380. handle->h_ref);
  1381. return err;
  1382. }
  1383. jbd_debug(4, "Handle %p going down\n", handle);
  1384. trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
  1385. transaction->t_tid,
  1386. handle->h_type, handle->h_line_no,
  1387. jiffies - handle->h_start_jiffies,
  1388. handle->h_sync, handle->h_requested_credits,
  1389. (handle->h_requested_credits -
  1390. handle->h_buffer_credits));
  1391. /*
  1392. * Implement synchronous transaction batching. If the handle
  1393. * was synchronous, don't force a commit immediately. Let's
  1394. * yield and let another thread piggyback onto this
  1395. * transaction. Keep doing that while new threads continue to
  1396. * arrive. It doesn't cost much - we're about to run a commit
  1397. * and sleep on IO anyway. Speeds up many-threaded, many-dir
  1398. * operations by 30x or more...
  1399. *
  1400. * We try and optimize the sleep time against what the
  1401. * underlying disk can do, instead of having a static sleep
  1402. * time. This is useful for the case where our storage is so
  1403. * fast that it is more optimal to go ahead and force a flush
  1404. * and wait for the transaction to be committed than it is to
  1405. * wait for an arbitrary amount of time for new writers to
  1406. * join the transaction. We achieve this by measuring how
  1407. * long it takes to commit a transaction, and compare it with
  1408. * how long this transaction has been running, and if run time
  1409. * < commit time then we sleep for the delta and commit. This
  1410. * greatly helps super fast disks that would see slowdowns as
  1411. * more threads started doing fsyncs.
  1412. *
  1413. * But don't do this if this process was the most recent one
  1414. * to perform a synchronous write. We do this to detect the
  1415. * case where a single process is doing a stream of sync
  1416. * writes. No point in waiting for joiners in that case.
  1417. */
  1418. pid = current->pid;
  1419. if (handle->h_sync && journal->j_last_sync_writer != pid) {
  1420. u64 commit_time, trans_time;
  1421. journal->j_last_sync_writer = pid;
  1422. read_lock(&journal->j_state_lock);
  1423. commit_time = journal->j_average_commit_time;
  1424. read_unlock(&journal->j_state_lock);
  1425. trans_time = ktime_to_ns(ktime_sub(ktime_get(),
  1426. transaction->t_start_time));
  1427. commit_time = max_t(u64, commit_time,
  1428. 1000*journal->j_min_batch_time);
  1429. commit_time = min_t(u64, commit_time,
  1430. 1000*journal->j_max_batch_time);
  1431. if (trans_time < commit_time) {
  1432. ktime_t expires = ktime_add_ns(ktime_get(),
  1433. commit_time);
  1434. set_current_state(TASK_UNINTERRUPTIBLE);
  1435. schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
  1436. }
  1437. }
  1438. if (handle->h_sync)
  1439. transaction->t_synchronous_commit = 1;
  1440. current->journal_info = NULL;
  1441. atomic_sub(handle->h_buffer_credits,
  1442. &transaction->t_outstanding_credits);
  1443. /*
  1444. * If the handle is marked SYNC, we need to set another commit
  1445. * going! We also want to force a commit if the current
  1446. * transaction is occupying too much of the log, or if the
  1447. * transaction is too old now.
  1448. */
  1449. if (handle->h_sync ||
  1450. (atomic_read(&transaction->t_outstanding_credits) >
  1451. journal->j_max_transaction_buffers) ||
  1452. time_after_eq(jiffies, transaction->t_expires)) {
  1453. /* Do this even for aborted journals: an abort still
  1454. * completes the commit thread, it just doesn't write
  1455. * anything to disk. */
  1456. jbd_debug(2, "transaction too old, requesting commit for "
  1457. "handle %p\n", handle);
  1458. /* This is non-blocking */
  1459. jbd2_log_start_commit(journal, transaction->t_tid);
  1460. /*
  1461. * Special case: JBD2_SYNC synchronous updates require us
  1462. * to wait for the commit to complete.
  1463. */
  1464. if (handle->h_sync && !(current->flags & PF_MEMALLOC))
  1465. wait_for_commit = 1;
  1466. }
  1467. /*
  1468. * Once we drop t_updates, if it goes to zero the transaction
  1469. * could start committing on us and eventually disappear. So
  1470. * once we do this, we must not dereference transaction
  1471. * pointer again.
  1472. */
  1473. tid = transaction->t_tid;
  1474. if (atomic_dec_and_test(&transaction->t_updates)) {
  1475. wake_up(&journal->j_wait_updates);
  1476. if (journal->j_barrier_count)
  1477. wake_up(&journal->j_wait_transaction_locked);
  1478. }
  1479. if (wait_for_commit)
  1480. err = jbd2_log_wait_commit(journal, tid);
  1481. lock_map_release(&handle->h_lockdep_map);
  1482. if (handle->h_rsv_handle)
  1483. jbd2_journal_free_reserved(handle->h_rsv_handle);
  1484. free_and_exit:
  1485. jbd2_free_handle(handle);
  1486. return err;
  1487. }
  1488. /*
  1489. *
  1490. * List management code snippets: various functions for manipulating the
  1491. * transaction buffer lists.
  1492. *
  1493. */
  1494. /*
  1495. * Append a buffer to a transaction list, given the transaction's list head
  1496. * pointer.
  1497. *
  1498. * j_list_lock is held.
  1499. *
  1500. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1501. */
  1502. static inline void
  1503. __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
  1504. {
  1505. if (!*list) {
  1506. jh->b_tnext = jh->b_tprev = jh;
  1507. *list = jh;
  1508. } else {
  1509. /* Insert at the tail of the list to preserve order */
  1510. struct journal_head *first = *list, *last = first->b_tprev;
  1511. jh->b_tprev = last;
  1512. jh->b_tnext = first;
  1513. last->b_tnext = first->b_tprev = jh;
  1514. }
  1515. }
  1516. /*
  1517. * Remove a buffer from a transaction list, given the transaction's list
  1518. * head pointer.
  1519. *
  1520. * Called with j_list_lock held, and the journal may not be locked.
  1521. *
  1522. * jbd_lock_bh_state(jh2bh(jh)) is held.
  1523. */
  1524. static inline void
  1525. __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
  1526. {
  1527. if (*list == jh) {
  1528. *list = jh->b_tnext;
  1529. if (*list == jh)
  1530. *list = NULL;
  1531. }
  1532. jh->b_tprev->b_tnext = jh->b_tnext;
  1533. jh->b_tnext->b_tprev = jh->b_tprev;
  1534. }
  1535. /*
  1536. * Remove a buffer from the appropriate transaction list.
  1537. *
  1538. * Note that this function can *change* the value of
  1539. * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
  1540. * t_reserved_list. If the caller is holding onto a copy of one of these
  1541. * pointers, it could go bad. Generally the caller needs to re-read the
  1542. * pointer from the transaction_t.
  1543. *
  1544. * Called under j_list_lock.
  1545. */
  1546. static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
  1547. {
  1548. struct journal_head **list = NULL;
  1549. transaction_t *transaction;
  1550. struct buffer_head *bh = jh2bh(jh);
  1551. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1552. transaction = jh->b_transaction;
  1553. if (transaction)
  1554. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1555. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1556. if (jh->b_jlist != BJ_None)
  1557. J_ASSERT_JH(jh, transaction != NULL);
  1558. switch (jh->b_jlist) {
  1559. case BJ_None:
  1560. return;
  1561. case BJ_Metadata:
  1562. transaction->t_nr_buffers--;
  1563. J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
  1564. list = &transaction->t_buffers;
  1565. break;
  1566. case BJ_Forget:
  1567. list = &transaction->t_forget;
  1568. break;
  1569. case BJ_Shadow:
  1570. list = &transaction->t_shadow_list;
  1571. break;
  1572. case BJ_Reserved:
  1573. list = &transaction->t_reserved_list;
  1574. break;
  1575. }
  1576. __blist_del_buffer(list, jh);
  1577. jh->b_jlist = BJ_None;
  1578. if (test_clear_buffer_jbddirty(bh))
  1579. mark_buffer_dirty(bh); /* Expose it to the VM */
  1580. }
  1581. /*
  1582. * Remove buffer from all transactions.
  1583. *
  1584. * Called with bh_state lock and j_list_lock
  1585. *
  1586. * jh and bh may be already freed when this function returns.
  1587. */
  1588. static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
  1589. {
  1590. __jbd2_journal_temp_unlink_buffer(jh);
  1591. jh->b_transaction = NULL;
  1592. jbd2_journal_put_journal_head(jh);
  1593. }
  1594. void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
  1595. {
  1596. struct buffer_head *bh = jh2bh(jh);
  1597. /* Get reference so that buffer cannot be freed before we unlock it */
  1598. get_bh(bh);
  1599. jbd_lock_bh_state(bh);
  1600. spin_lock(&journal->j_list_lock);
  1601. __jbd2_journal_unfile_buffer(jh);
  1602. spin_unlock(&journal->j_list_lock);
  1603. jbd_unlock_bh_state(bh);
  1604. __brelse(bh);
  1605. }
  1606. /*
  1607. * Called from jbd2_journal_try_to_free_buffers().
  1608. *
  1609. * Called under jbd_lock_bh_state(bh)
  1610. */
  1611. static void
  1612. __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
  1613. {
  1614. struct journal_head *jh;
  1615. jh = bh2jh(bh);
  1616. if (buffer_locked(bh) || buffer_dirty(bh))
  1617. goto out;
  1618. if (jh->b_next_transaction != NULL)
  1619. goto out;
  1620. spin_lock(&journal->j_list_lock);
  1621. if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
  1622. /* written-back checkpointed metadata buffer */
  1623. JBUFFER_TRACE(jh, "remove from checkpoint list");
  1624. __jbd2_journal_remove_checkpoint(jh);
  1625. }
  1626. spin_unlock(&journal->j_list_lock);
  1627. out:
  1628. return;
  1629. }
  1630. /**
  1631. * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
  1632. * @journal: journal for operation
  1633. * @page: to try and free
  1634. * @gfp_mask: we use the mask to detect how hard should we try to release
  1635. * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
  1636. * release the buffers.
  1637. *
  1638. *
  1639. * For all the buffers on this page,
  1640. * if they are fully written out ordered data, move them onto BUF_CLEAN
  1641. * so try_to_free_buffers() can reap them.
  1642. *
  1643. * This function returns non-zero if we wish try_to_free_buffers()
  1644. * to be called. We do this if the page is releasable by try_to_free_buffers().
  1645. * We also do it if the page has locked or dirty buffers and the caller wants
  1646. * us to perform sync or async writeout.
  1647. *
  1648. * This complicates JBD locking somewhat. We aren't protected by the
  1649. * BKL here. We wish to remove the buffer from its committing or
  1650. * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
  1651. *
  1652. * This may *change* the value of transaction_t->t_datalist, so anyone
  1653. * who looks at t_datalist needs to lock against this function.
  1654. *
  1655. * Even worse, someone may be doing a jbd2_journal_dirty_data on this
  1656. * buffer. So we need to lock against that. jbd2_journal_dirty_data()
  1657. * will come out of the lock with the buffer dirty, which makes it
  1658. * ineligible for release here.
  1659. *
  1660. * Who else is affected by this? hmm... Really the only contender
  1661. * is do_get_write_access() - it could be looking at the buffer while
  1662. * journal_try_to_free_buffer() is changing its state. But that
  1663. * cannot happen because we never reallocate freed data as metadata
  1664. * while the data is part of a transaction. Yes?
  1665. *
  1666. * Return 0 on failure, 1 on success
  1667. */
  1668. int jbd2_journal_try_to_free_buffers(journal_t *journal,
  1669. struct page *page, gfp_t gfp_mask)
  1670. {
  1671. struct buffer_head *head;
  1672. struct buffer_head *bh;
  1673. int ret = 0;
  1674. J_ASSERT(PageLocked(page));
  1675. head = page_buffers(page);
  1676. bh = head;
  1677. do {
  1678. struct journal_head *jh;
  1679. /*
  1680. * We take our own ref against the journal_head here to avoid
  1681. * having to add tons of locking around each instance of
  1682. * jbd2_journal_put_journal_head().
  1683. */
  1684. jh = jbd2_journal_grab_journal_head(bh);
  1685. if (!jh)
  1686. continue;
  1687. jbd_lock_bh_state(bh);
  1688. __journal_try_to_free_buffer(journal, bh);
  1689. jbd2_journal_put_journal_head(jh);
  1690. jbd_unlock_bh_state(bh);
  1691. if (buffer_jbd(bh))
  1692. goto busy;
  1693. } while ((bh = bh->b_this_page) != head);
  1694. ret = try_to_free_buffers(page);
  1695. busy:
  1696. return ret;
  1697. }
  1698. /*
  1699. * This buffer is no longer needed. If it is on an older transaction's
  1700. * checkpoint list we need to record it on this transaction's forget list
  1701. * to pin this buffer (and hence its checkpointing transaction) down until
  1702. * this transaction commits. If the buffer isn't on a checkpoint list, we
  1703. * release it.
  1704. * Returns non-zero if JBD no longer has an interest in the buffer.
  1705. *
  1706. * Called under j_list_lock.
  1707. *
  1708. * Called under jbd_lock_bh_state(bh).
  1709. */
  1710. static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
  1711. {
  1712. int may_free = 1;
  1713. struct buffer_head *bh = jh2bh(jh);
  1714. if (jh->b_cp_transaction) {
  1715. JBUFFER_TRACE(jh, "on running+cp transaction");
  1716. __jbd2_journal_temp_unlink_buffer(jh);
  1717. /*
  1718. * We don't want to write the buffer anymore, clear the
  1719. * bit so that we don't confuse checks in
  1720. * __journal_file_buffer
  1721. */
  1722. clear_buffer_dirty(bh);
  1723. __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
  1724. may_free = 0;
  1725. } else {
  1726. JBUFFER_TRACE(jh, "on running transaction");
  1727. __jbd2_journal_unfile_buffer(jh);
  1728. }
  1729. return may_free;
  1730. }
  1731. /*
  1732. * jbd2_journal_invalidatepage
  1733. *
  1734. * This code is tricky. It has a number of cases to deal with.
  1735. *
  1736. * There are two invariants which this code relies on:
  1737. *
  1738. * i_size must be updated on disk before we start calling invalidatepage on the
  1739. * data.
  1740. *
  1741. * This is done in ext3 by defining an ext3_setattr method which
  1742. * updates i_size before truncate gets going. By maintaining this
  1743. * invariant, we can be sure that it is safe to throw away any buffers
  1744. * attached to the current transaction: once the transaction commits,
  1745. * we know that the data will not be needed.
  1746. *
  1747. * Note however that we can *not* throw away data belonging to the
  1748. * previous, committing transaction!
  1749. *
  1750. * Any disk blocks which *are* part of the previous, committing
  1751. * transaction (and which therefore cannot be discarded immediately) are
  1752. * not going to be reused in the new running transaction
  1753. *
  1754. * The bitmap committed_data images guarantee this: any block which is
  1755. * allocated in one transaction and removed in the next will be marked
  1756. * as in-use in the committed_data bitmap, so cannot be reused until
  1757. * the next transaction to delete the block commits. This means that
  1758. * leaving committing buffers dirty is quite safe: the disk blocks
  1759. * cannot be reallocated to a different file and so buffer aliasing is
  1760. * not possible.
  1761. *
  1762. *
  1763. * The above applies mainly to ordered data mode. In writeback mode we
  1764. * don't make guarantees about the order in which data hits disk --- in
  1765. * particular we don't guarantee that new dirty data is flushed before
  1766. * transaction commit --- so it is always safe just to discard data
  1767. * immediately in that mode. --sct
  1768. */
  1769. /*
  1770. * The journal_unmap_buffer helper function returns zero if the buffer
  1771. * concerned remains pinned as an anonymous buffer belonging to an older
  1772. * transaction.
  1773. *
  1774. * We're outside-transaction here. Either or both of j_running_transaction
  1775. * and j_committing_transaction may be NULL.
  1776. */
  1777. static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
  1778. int partial_page)
  1779. {
  1780. transaction_t *transaction;
  1781. struct journal_head *jh;
  1782. int may_free = 1;
  1783. BUFFER_TRACE(bh, "entry");
  1784. /*
  1785. * It is safe to proceed here without the j_list_lock because the
  1786. * buffers cannot be stolen by try_to_free_buffers as long as we are
  1787. * holding the page lock. --sct
  1788. */
  1789. if (!buffer_jbd(bh))
  1790. goto zap_buffer_unlocked;
  1791. /* OK, we have data buffer in journaled mode */
  1792. write_lock(&journal->j_state_lock);
  1793. jbd_lock_bh_state(bh);
  1794. spin_lock(&journal->j_list_lock);
  1795. jh = jbd2_journal_grab_journal_head(bh);
  1796. if (!jh)
  1797. goto zap_buffer_no_jh;
  1798. /*
  1799. * We cannot remove the buffer from checkpoint lists until the
  1800. * transaction adding inode to orphan list (let's call it T)
  1801. * is committed. Otherwise if the transaction changing the
  1802. * buffer would be cleaned from the journal before T is
  1803. * committed, a crash will cause that the correct contents of
  1804. * the buffer will be lost. On the other hand we have to
  1805. * clear the buffer dirty bit at latest at the moment when the
  1806. * transaction marking the buffer as freed in the filesystem
  1807. * structures is committed because from that moment on the
  1808. * block can be reallocated and used by a different page.
  1809. * Since the block hasn't been freed yet but the inode has
  1810. * already been added to orphan list, it is safe for us to add
  1811. * the buffer to BJ_Forget list of the newest transaction.
  1812. *
  1813. * Also we have to clear buffer_mapped flag of a truncated buffer
  1814. * because the buffer_head may be attached to the page straddling
  1815. * i_size (can happen only when blocksize < pagesize) and thus the
  1816. * buffer_head can be reused when the file is extended again. So we end
  1817. * up keeping around invalidated buffers attached to transactions'
  1818. * BJ_Forget list just to stop checkpointing code from cleaning up
  1819. * the transaction this buffer was modified in.
  1820. */
  1821. transaction = jh->b_transaction;
  1822. if (transaction == NULL) {
  1823. /* First case: not on any transaction. If it
  1824. * has no checkpoint link, then we can zap it:
  1825. * it's a writeback-mode buffer so we don't care
  1826. * if it hits disk safely. */
  1827. if (!jh->b_cp_transaction) {
  1828. JBUFFER_TRACE(jh, "not on any transaction: zap");
  1829. goto zap_buffer;
  1830. }
  1831. if (!buffer_dirty(bh)) {
  1832. /* bdflush has written it. We can drop it now */
  1833. goto zap_buffer;
  1834. }
  1835. /* OK, it must be in the journal but still not
  1836. * written fully to disk: it's metadata or
  1837. * journaled data... */
  1838. if (journal->j_running_transaction) {
  1839. /* ... and once the current transaction has
  1840. * committed, the buffer won't be needed any
  1841. * longer. */
  1842. JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
  1843. may_free = __dispose_buffer(jh,
  1844. journal->j_running_transaction);
  1845. goto zap_buffer;
  1846. } else {
  1847. /* There is no currently-running transaction. So the
  1848. * orphan record which we wrote for this file must have
  1849. * passed into commit. We must attach this buffer to
  1850. * the committing transaction, if it exists. */
  1851. if (journal->j_committing_transaction) {
  1852. JBUFFER_TRACE(jh, "give to committing trans");
  1853. may_free = __dispose_buffer(jh,
  1854. journal->j_committing_transaction);
  1855. goto zap_buffer;
  1856. } else {
  1857. /* The orphan record's transaction has
  1858. * committed. We can cleanse this buffer */
  1859. clear_buffer_jbddirty(bh);
  1860. goto zap_buffer;
  1861. }
  1862. }
  1863. } else if (transaction == journal->j_committing_transaction) {
  1864. JBUFFER_TRACE(jh, "on committing transaction");
  1865. /*
  1866. * The buffer is committing, we simply cannot touch
  1867. * it. If the page is straddling i_size we have to wait
  1868. * for commit and try again.
  1869. */
  1870. if (partial_page) {
  1871. jbd2_journal_put_journal_head(jh);
  1872. spin_unlock(&journal->j_list_lock);
  1873. jbd_unlock_bh_state(bh);
  1874. write_unlock(&journal->j_state_lock);
  1875. return -EBUSY;
  1876. }
  1877. /*
  1878. * OK, buffer won't be reachable after truncate. We just set
  1879. * j_next_transaction to the running transaction (if there is
  1880. * one) and mark buffer as freed so that commit code knows it
  1881. * should clear dirty bits when it is done with the buffer.
  1882. */
  1883. set_buffer_freed(bh);
  1884. if (journal->j_running_transaction && buffer_jbddirty(bh))
  1885. jh->b_next_transaction = journal->j_running_transaction;
  1886. jbd2_journal_put_journal_head(jh);
  1887. spin_unlock(&journal->j_list_lock);
  1888. jbd_unlock_bh_state(bh);
  1889. write_unlock(&journal->j_state_lock);
  1890. return 0;
  1891. } else {
  1892. /* Good, the buffer belongs to the running transaction.
  1893. * We are writing our own transaction's data, not any
  1894. * previous one's, so it is safe to throw it away
  1895. * (remember that we expect the filesystem to have set
  1896. * i_size already for this truncate so recovery will not
  1897. * expose the disk blocks we are discarding here.) */
  1898. J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
  1899. JBUFFER_TRACE(jh, "on running transaction");
  1900. may_free = __dispose_buffer(jh, transaction);
  1901. }
  1902. zap_buffer:
  1903. /*
  1904. * This is tricky. Although the buffer is truncated, it may be reused
  1905. * if blocksize < pagesize and it is attached to the page straddling
  1906. * EOF. Since the buffer might have been added to BJ_Forget list of the
  1907. * running transaction, journal_get_write_access() won't clear
  1908. * b_modified and credit accounting gets confused. So clear b_modified
  1909. * here.
  1910. */
  1911. jh->b_modified = 0;
  1912. jbd2_journal_put_journal_head(jh);
  1913. zap_buffer_no_jh:
  1914. spin_unlock(&journal->j_list_lock);
  1915. jbd_unlock_bh_state(bh);
  1916. write_unlock(&journal->j_state_lock);
  1917. zap_buffer_unlocked:
  1918. clear_buffer_dirty(bh);
  1919. J_ASSERT_BH(bh, !buffer_jbddirty(bh));
  1920. clear_buffer_mapped(bh);
  1921. clear_buffer_req(bh);
  1922. clear_buffer_new(bh);
  1923. clear_buffer_delay(bh);
  1924. clear_buffer_unwritten(bh);
  1925. bh->b_bdev = NULL;
  1926. return may_free;
  1927. }
  1928. /**
  1929. * void jbd2_journal_invalidatepage()
  1930. * @journal: journal to use for flush...
  1931. * @page: page to flush
  1932. * @offset: start of the range to invalidate
  1933. * @length: length of the range to invalidate
  1934. *
  1935. * Reap page buffers containing data after in the specified range in page.
  1936. * Can return -EBUSY if buffers are part of the committing transaction and
  1937. * the page is straddling i_size. Caller then has to wait for current commit
  1938. * and try again.
  1939. */
  1940. int jbd2_journal_invalidatepage(journal_t *journal,
  1941. struct page *page,
  1942. unsigned int offset,
  1943. unsigned int length)
  1944. {
  1945. struct buffer_head *head, *bh, *next;
  1946. unsigned int stop = offset + length;
  1947. unsigned int curr_off = 0;
  1948. int partial_page = (offset || length < PAGE_CACHE_SIZE);
  1949. int may_free = 1;
  1950. int ret = 0;
  1951. if (!PageLocked(page))
  1952. BUG();
  1953. if (!page_has_buffers(page))
  1954. return 0;
  1955. BUG_ON(stop > PAGE_CACHE_SIZE || stop < length);
  1956. /* We will potentially be playing with lists other than just the
  1957. * data lists (especially for journaled data mode), so be
  1958. * cautious in our locking. */
  1959. head = bh = page_buffers(page);
  1960. do {
  1961. unsigned int next_off = curr_off + bh->b_size;
  1962. next = bh->b_this_page;
  1963. if (next_off > stop)
  1964. return 0;
  1965. if (offset <= curr_off) {
  1966. /* This block is wholly outside the truncation point */
  1967. lock_buffer(bh);
  1968. ret = journal_unmap_buffer(journal, bh, partial_page);
  1969. unlock_buffer(bh);
  1970. if (ret < 0)
  1971. return ret;
  1972. may_free &= ret;
  1973. }
  1974. curr_off = next_off;
  1975. bh = next;
  1976. } while (bh != head);
  1977. if (!partial_page) {
  1978. if (may_free && try_to_free_buffers(page))
  1979. J_ASSERT(!page_has_buffers(page));
  1980. }
  1981. return 0;
  1982. }
  1983. /*
  1984. * File a buffer on the given transaction list.
  1985. */
  1986. void __jbd2_journal_file_buffer(struct journal_head *jh,
  1987. transaction_t *transaction, int jlist)
  1988. {
  1989. struct journal_head **list = NULL;
  1990. int was_dirty = 0;
  1991. struct buffer_head *bh = jh2bh(jh);
  1992. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  1993. assert_spin_locked(&transaction->t_journal->j_list_lock);
  1994. J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
  1995. J_ASSERT_JH(jh, jh->b_transaction == transaction ||
  1996. jh->b_transaction == NULL);
  1997. if (jh->b_transaction && jh->b_jlist == jlist)
  1998. return;
  1999. if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
  2000. jlist == BJ_Shadow || jlist == BJ_Forget) {
  2001. /*
  2002. * For metadata buffers, we track dirty bit in buffer_jbddirty
  2003. * instead of buffer_dirty. We should not see a dirty bit set
  2004. * here because we clear it in do_get_write_access but e.g.
  2005. * tune2fs can modify the sb and set the dirty bit at any time
  2006. * so we try to gracefully handle that.
  2007. */
  2008. if (buffer_dirty(bh))
  2009. warn_dirty_buffer(bh);
  2010. if (test_clear_buffer_dirty(bh) ||
  2011. test_clear_buffer_jbddirty(bh))
  2012. was_dirty = 1;
  2013. }
  2014. if (jh->b_transaction)
  2015. __jbd2_journal_temp_unlink_buffer(jh);
  2016. else
  2017. jbd2_journal_grab_journal_head(bh);
  2018. jh->b_transaction = transaction;
  2019. switch (jlist) {
  2020. case BJ_None:
  2021. J_ASSERT_JH(jh, !jh->b_committed_data);
  2022. J_ASSERT_JH(jh, !jh->b_frozen_data);
  2023. return;
  2024. case BJ_Metadata:
  2025. transaction->t_nr_buffers++;
  2026. list = &transaction->t_buffers;
  2027. break;
  2028. case BJ_Forget:
  2029. list = &transaction->t_forget;
  2030. break;
  2031. case BJ_Shadow:
  2032. list = &transaction->t_shadow_list;
  2033. break;
  2034. case BJ_Reserved:
  2035. list = &transaction->t_reserved_list;
  2036. break;
  2037. }
  2038. __blist_add_buffer(list, jh);
  2039. jh->b_jlist = jlist;
  2040. if (was_dirty)
  2041. set_buffer_jbddirty(bh);
  2042. }
  2043. void jbd2_journal_file_buffer(struct journal_head *jh,
  2044. transaction_t *transaction, int jlist)
  2045. {
  2046. jbd_lock_bh_state(jh2bh(jh));
  2047. spin_lock(&transaction->t_journal->j_list_lock);
  2048. __jbd2_journal_file_buffer(jh, transaction, jlist);
  2049. spin_unlock(&transaction->t_journal->j_list_lock);
  2050. jbd_unlock_bh_state(jh2bh(jh));
  2051. }
  2052. /*
  2053. * Remove a buffer from its current buffer list in preparation for
  2054. * dropping it from its current transaction entirely. If the buffer has
  2055. * already started to be used by a subsequent transaction, refile the
  2056. * buffer on that transaction's metadata list.
  2057. *
  2058. * Called under j_list_lock
  2059. * Called under jbd_lock_bh_state(jh2bh(jh))
  2060. *
  2061. * jh and bh may be already free when this function returns
  2062. */
  2063. void __jbd2_journal_refile_buffer(struct journal_head *jh)
  2064. {
  2065. int was_dirty, jlist;
  2066. struct buffer_head *bh = jh2bh(jh);
  2067. J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
  2068. if (jh->b_transaction)
  2069. assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
  2070. /* If the buffer is now unused, just drop it. */
  2071. if (jh->b_next_transaction == NULL) {
  2072. __jbd2_journal_unfile_buffer(jh);
  2073. return;
  2074. }
  2075. /*
  2076. * It has been modified by a later transaction: add it to the new
  2077. * transaction's metadata list.
  2078. */
  2079. was_dirty = test_clear_buffer_jbddirty(bh);
  2080. __jbd2_journal_temp_unlink_buffer(jh);
  2081. /*
  2082. * We set b_transaction here because b_next_transaction will inherit
  2083. * our jh reference and thus __jbd2_journal_file_buffer() must not
  2084. * take a new one.
  2085. */
  2086. jh->b_transaction = jh->b_next_transaction;
  2087. jh->b_next_transaction = NULL;
  2088. if (buffer_freed(bh))
  2089. jlist = BJ_Forget;
  2090. else if (jh->b_modified)
  2091. jlist = BJ_Metadata;
  2092. else
  2093. jlist = BJ_Reserved;
  2094. __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
  2095. J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
  2096. if (was_dirty)
  2097. set_buffer_jbddirty(bh);
  2098. }
  2099. /*
  2100. * __jbd2_journal_refile_buffer() with necessary locking added. We take our
  2101. * bh reference so that we can safely unlock bh.
  2102. *
  2103. * The jh and bh may be freed by this call.
  2104. */
  2105. void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
  2106. {
  2107. struct buffer_head *bh = jh2bh(jh);
  2108. /* Get reference so that buffer cannot be freed before we unlock it */
  2109. get_bh(bh);
  2110. jbd_lock_bh_state(bh);
  2111. spin_lock(&journal->j_list_lock);
  2112. __jbd2_journal_refile_buffer(jh);
  2113. jbd_unlock_bh_state(bh);
  2114. spin_unlock(&journal->j_list_lock);
  2115. __brelse(bh);
  2116. }
  2117. /*
  2118. * File inode in the inode list of the handle's transaction
  2119. */
  2120. int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
  2121. {
  2122. transaction_t *transaction = handle->h_transaction;
  2123. journal_t *journal;
  2124. WARN_ON(!transaction);
  2125. if (is_handle_aborted(handle))
  2126. return -EROFS;
  2127. journal = transaction->t_journal;
  2128. jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
  2129. transaction->t_tid);
  2130. /*
  2131. * First check whether inode isn't already on the transaction's
  2132. * lists without taking the lock. Note that this check is safe
  2133. * without the lock as we cannot race with somebody removing inode
  2134. * from the transaction. The reason is that we remove inode from the
  2135. * transaction only in journal_release_jbd_inode() and when we commit
  2136. * the transaction. We are guarded from the first case by holding
  2137. * a reference to the inode. We are safe against the second case
  2138. * because if jinode->i_transaction == transaction, commit code
  2139. * cannot touch the transaction because we hold reference to it,
  2140. * and if jinode->i_next_transaction == transaction, commit code
  2141. * will only file the inode where we want it.
  2142. */
  2143. if (jinode->i_transaction == transaction ||
  2144. jinode->i_next_transaction == transaction)
  2145. return 0;
  2146. spin_lock(&journal->j_list_lock);
  2147. if (jinode->i_transaction == transaction ||
  2148. jinode->i_next_transaction == transaction)
  2149. goto done;
  2150. /*
  2151. * We only ever set this variable to 1 so the test is safe. Since
  2152. * t_need_data_flush is likely to be set, we do the test to save some
  2153. * cacheline bouncing
  2154. */
  2155. if (!transaction->t_need_data_flush)
  2156. transaction->t_need_data_flush = 1;
  2157. /* On some different transaction's list - should be
  2158. * the committing one */
  2159. if (jinode->i_transaction) {
  2160. J_ASSERT(jinode->i_next_transaction == NULL);
  2161. J_ASSERT(jinode->i_transaction ==
  2162. journal->j_committing_transaction);
  2163. jinode->i_next_transaction = transaction;
  2164. goto done;
  2165. }
  2166. /* Not on any transaction list... */
  2167. J_ASSERT(!jinode->i_next_transaction);
  2168. jinode->i_transaction = transaction;
  2169. list_add(&jinode->i_list, &transaction->t_inode_list);
  2170. done:
  2171. spin_unlock(&journal->j_list_lock);
  2172. return 0;
  2173. }
  2174. /*
  2175. * File truncate and transaction commit interact with each other in a
  2176. * non-trivial way. If a transaction writing data block A is
  2177. * committing, we cannot discard the data by truncate until we have
  2178. * written them. Otherwise if we crashed after the transaction with
  2179. * write has committed but before the transaction with truncate has
  2180. * committed, we could see stale data in block A. This function is a
  2181. * helper to solve this problem. It starts writeout of the truncated
  2182. * part in case it is in the committing transaction.
  2183. *
  2184. * Filesystem code must call this function when inode is journaled in
  2185. * ordered mode before truncation happens and after the inode has been
  2186. * placed on orphan list with the new inode size. The second condition
  2187. * avoids the race that someone writes new data and we start
  2188. * committing the transaction after this function has been called but
  2189. * before a transaction for truncate is started (and furthermore it
  2190. * allows us to optimize the case where the addition to orphan list
  2191. * happens in the same transaction as write --- we don't have to write
  2192. * any data in such case).
  2193. */
  2194. int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  2195. struct jbd2_inode *jinode,
  2196. loff_t new_size)
  2197. {
  2198. transaction_t *inode_trans, *commit_trans;
  2199. int ret = 0;
  2200. /* This is a quick check to avoid locking if not necessary */
  2201. if (!jinode->i_transaction)
  2202. goto out;
  2203. /* Locks are here just to force reading of recent values, it is
  2204. * enough that the transaction was not committing before we started
  2205. * a transaction adding the inode to orphan list */
  2206. read_lock(&journal->j_state_lock);
  2207. commit_trans = journal->j_committing_transaction;
  2208. read_unlock(&journal->j_state_lock);
  2209. spin_lock(&journal->j_list_lock);
  2210. inode_trans = jinode->i_transaction;
  2211. spin_unlock(&journal->j_list_lock);
  2212. if (inode_trans == commit_trans) {
  2213. ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  2214. new_size, LLONG_MAX);
  2215. if (ret)
  2216. jbd2_journal_abort(journal, ret);
  2217. }
  2218. out:
  2219. return ret;
  2220. }