transaction.c 75 KB

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