journal.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  1. /*
  2. * linux/fs/journal.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 journal-writing code; part of the ext2fs
  13. * journaling system.
  14. *
  15. * This file manages journals: areas of disk reserved for logging
  16. * transactional updates. This includes the kernel journaling thread
  17. * which is responsible for scheduling updates to the log.
  18. *
  19. * We do not actually manage the physical storage of the journal in this
  20. * file: that is left to a per-journal policy function, which allows us
  21. * to store the journal within a filesystem-specified area for ext2
  22. * journaling (ext2 can use a reserved inode for storing the log).
  23. */
  24. #include <linux/module.h>
  25. #include <linux/time.h>
  26. #include <linux/fs.h>
  27. #include <linux/jbd.h>
  28. #include <linux/errno.h>
  29. #include <linux/slab.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/init.h>
  32. #include <linux/mm.h>
  33. #include <linux/suspend.h>
  34. #include <linux/pagemap.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/page.h>
  37. #include <linux/proc_fs.h>
  38. EXPORT_SYMBOL(journal_start);
  39. EXPORT_SYMBOL(journal_restart);
  40. EXPORT_SYMBOL(journal_extend);
  41. EXPORT_SYMBOL(journal_stop);
  42. EXPORT_SYMBOL(journal_lock_updates);
  43. EXPORT_SYMBOL(journal_unlock_updates);
  44. EXPORT_SYMBOL(journal_get_write_access);
  45. EXPORT_SYMBOL(journal_get_create_access);
  46. EXPORT_SYMBOL(journal_get_undo_access);
  47. EXPORT_SYMBOL(journal_dirty_data);
  48. EXPORT_SYMBOL(journal_dirty_metadata);
  49. EXPORT_SYMBOL(journal_release_buffer);
  50. EXPORT_SYMBOL(journal_forget);
  51. #if 0
  52. EXPORT_SYMBOL(journal_sync_buffer);
  53. #endif
  54. EXPORT_SYMBOL(journal_flush);
  55. EXPORT_SYMBOL(journal_revoke);
  56. EXPORT_SYMBOL(journal_init_dev);
  57. EXPORT_SYMBOL(journal_init_inode);
  58. EXPORT_SYMBOL(journal_update_format);
  59. EXPORT_SYMBOL(journal_check_used_features);
  60. EXPORT_SYMBOL(journal_check_available_features);
  61. EXPORT_SYMBOL(journal_set_features);
  62. EXPORT_SYMBOL(journal_create);
  63. EXPORT_SYMBOL(journal_load);
  64. EXPORT_SYMBOL(journal_destroy);
  65. EXPORT_SYMBOL(journal_recover);
  66. EXPORT_SYMBOL(journal_update_superblock);
  67. EXPORT_SYMBOL(journal_abort);
  68. EXPORT_SYMBOL(journal_errno);
  69. EXPORT_SYMBOL(journal_ack_err);
  70. EXPORT_SYMBOL(journal_clear_err);
  71. EXPORT_SYMBOL(log_wait_commit);
  72. EXPORT_SYMBOL(journal_start_commit);
  73. EXPORT_SYMBOL(journal_force_commit_nested);
  74. EXPORT_SYMBOL(journal_wipe);
  75. EXPORT_SYMBOL(journal_blocks_per_page);
  76. EXPORT_SYMBOL(journal_invalidatepage);
  77. EXPORT_SYMBOL(journal_try_to_free_buffers);
  78. EXPORT_SYMBOL(journal_force_commit);
  79. static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
  80. /*
  81. * Helper function used to manage commit timeouts
  82. */
  83. static void commit_timeout(unsigned long __data)
  84. {
  85. struct task_struct * p = (struct task_struct *) __data;
  86. wake_up_process(p);
  87. }
  88. /* Static check for data structure consistency. There's no code
  89. * invoked --- we'll just get a linker failure if things aren't right.
  90. */
  91. void __journal_internal_check(void)
  92. {
  93. extern void journal_bad_superblock_size(void);
  94. if (sizeof(struct journal_superblock_s) != 1024)
  95. journal_bad_superblock_size();
  96. }
  97. /*
  98. * kjournald: The main thread function used to manage a logging device
  99. * journal.
  100. *
  101. * This kernel thread is responsible for two things:
  102. *
  103. * 1) COMMIT: Every so often we need to commit the current state of the
  104. * filesystem to disk. The journal thread is responsible for writing
  105. * all of the metadata buffers to disk.
  106. *
  107. * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
  108. * of the data in that part of the log has been rewritten elsewhere on
  109. * the disk. Flushing these old buffers to reclaim space in the log is
  110. * known as checkpointing, and this thread is responsible for that job.
  111. */
  112. journal_t *current_journal; // AKPM: debug
  113. int kjournald(void *arg)
  114. {
  115. journal_t *journal = (journal_t *) arg;
  116. transaction_t *transaction;
  117. struct timer_list timer;
  118. current_journal = journal;
  119. daemonize("kjournald");
  120. /* Set up an interval timer which can be used to trigger a
  121. commit wakeup after the commit interval expires */
  122. init_timer(&timer);
  123. timer.data = (unsigned long) current;
  124. timer.function = commit_timeout;
  125. journal->j_commit_timer = &timer;
  126. /* Record that the journal thread is running */
  127. journal->j_task = current;
  128. wake_up(&journal->j_wait_done_commit);
  129. printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
  130. journal->j_commit_interval / HZ);
  131. /*
  132. * And now, wait forever for commit wakeup events.
  133. */
  134. spin_lock(&journal->j_state_lock);
  135. loop:
  136. if (journal->j_flags & JFS_UNMOUNT)
  137. goto end_loop;
  138. jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
  139. journal->j_commit_sequence, journal->j_commit_request);
  140. if (journal->j_commit_sequence != journal->j_commit_request) {
  141. jbd_debug(1, "OK, requests differ\n");
  142. spin_unlock(&journal->j_state_lock);
  143. del_timer_sync(journal->j_commit_timer);
  144. journal_commit_transaction(journal);
  145. spin_lock(&journal->j_state_lock);
  146. goto loop;
  147. }
  148. wake_up(&journal->j_wait_done_commit);
  149. if (freezing(current)) {
  150. /*
  151. * The simpler the better. Flushing journal isn't a
  152. * good idea, because that depends on threads that may
  153. * be already stopped.
  154. */
  155. jbd_debug(1, "Now suspending kjournald\n");
  156. spin_unlock(&journal->j_state_lock);
  157. refrigerator();
  158. spin_lock(&journal->j_state_lock);
  159. } else {
  160. /*
  161. * We assume on resume that commits are already there,
  162. * so we don't sleep
  163. */
  164. DEFINE_WAIT(wait);
  165. int should_sleep = 1;
  166. prepare_to_wait(&journal->j_wait_commit, &wait,
  167. TASK_INTERRUPTIBLE);
  168. if (journal->j_commit_sequence != journal->j_commit_request)
  169. should_sleep = 0;
  170. transaction = journal->j_running_transaction;
  171. if (transaction && time_after_eq(jiffies,
  172. transaction->t_expires))
  173. should_sleep = 0;
  174. if (should_sleep) {
  175. spin_unlock(&journal->j_state_lock);
  176. schedule();
  177. spin_lock(&journal->j_state_lock);
  178. }
  179. finish_wait(&journal->j_wait_commit, &wait);
  180. }
  181. jbd_debug(1, "kjournald wakes\n");
  182. /*
  183. * Were we woken up by a commit wakeup event?
  184. */
  185. transaction = journal->j_running_transaction;
  186. if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
  187. journal->j_commit_request = transaction->t_tid;
  188. jbd_debug(1, "woke because of timeout\n");
  189. }
  190. goto loop;
  191. end_loop:
  192. spin_unlock(&journal->j_state_lock);
  193. del_timer_sync(journal->j_commit_timer);
  194. journal->j_task = NULL;
  195. wake_up(&journal->j_wait_done_commit);
  196. jbd_debug(1, "Journal thread exiting.\n");
  197. return 0;
  198. }
  199. static void journal_start_thread(journal_t *journal)
  200. {
  201. kernel_thread(kjournald, journal, CLONE_VM|CLONE_FS|CLONE_FILES);
  202. wait_event(journal->j_wait_done_commit, journal->j_task != 0);
  203. }
  204. static void journal_kill_thread(journal_t *journal)
  205. {
  206. spin_lock(&journal->j_state_lock);
  207. journal->j_flags |= JFS_UNMOUNT;
  208. while (journal->j_task) {
  209. wake_up(&journal->j_wait_commit);
  210. spin_unlock(&journal->j_state_lock);
  211. wait_event(journal->j_wait_done_commit, journal->j_task == 0);
  212. spin_lock(&journal->j_state_lock);
  213. }
  214. spin_unlock(&journal->j_state_lock);
  215. }
  216. /*
  217. * journal_write_metadata_buffer: write a metadata buffer to the journal.
  218. *
  219. * Writes a metadata buffer to a given disk block. The actual IO is not
  220. * performed but a new buffer_head is constructed which labels the data
  221. * to be written with the correct destination disk block.
  222. *
  223. * Any magic-number escaping which needs to be done will cause a
  224. * copy-out here. If the buffer happens to start with the
  225. * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
  226. * magic number is only written to the log for descripter blocks. In
  227. * this case, we copy the data and replace the first word with 0, and we
  228. * return a result code which indicates that this buffer needs to be
  229. * marked as an escaped buffer in the corresponding log descriptor
  230. * block. The missing word can then be restored when the block is read
  231. * during recovery.
  232. *
  233. * If the source buffer has already been modified by a new transaction
  234. * since we took the last commit snapshot, we use the frozen copy of
  235. * that data for IO. If we end up using the existing buffer_head's data
  236. * for the write, then we *have* to lock the buffer to prevent anyone
  237. * else from using and possibly modifying it while the IO is in
  238. * progress.
  239. *
  240. * The function returns a pointer to the buffer_heads to be used for IO.
  241. *
  242. * We assume that the journal has already been locked in this function.
  243. *
  244. * Return value:
  245. * <0: Error
  246. * >=0: Finished OK
  247. *
  248. * On success:
  249. * Bit 0 set == escape performed on the data
  250. * Bit 1 set == buffer copy-out performed (kfree the data after IO)
  251. */
  252. int journal_write_metadata_buffer(transaction_t *transaction,
  253. struct journal_head *jh_in,
  254. struct journal_head **jh_out,
  255. int blocknr)
  256. {
  257. int need_copy_out = 0;
  258. int done_copy_out = 0;
  259. int do_escape = 0;
  260. char *mapped_data;
  261. struct buffer_head *new_bh;
  262. struct journal_head *new_jh;
  263. struct page *new_page;
  264. unsigned int new_offset;
  265. struct buffer_head *bh_in = jh2bh(jh_in);
  266. /*
  267. * The buffer really shouldn't be locked: only the current committing
  268. * transaction is allowed to write it, so nobody else is allowed
  269. * to do any IO.
  270. *
  271. * akpm: except if we're journalling data, and write() output is
  272. * also part of a shared mapping, and another thread has
  273. * decided to launch a writepage() against this buffer.
  274. */
  275. J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
  276. new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
  277. /*
  278. * If a new transaction has already done a buffer copy-out, then
  279. * we use that version of the data for the commit.
  280. */
  281. jbd_lock_bh_state(bh_in);
  282. repeat:
  283. if (jh_in->b_frozen_data) {
  284. done_copy_out = 1;
  285. new_page = virt_to_page(jh_in->b_frozen_data);
  286. new_offset = offset_in_page(jh_in->b_frozen_data);
  287. } else {
  288. new_page = jh2bh(jh_in)->b_page;
  289. new_offset = offset_in_page(jh2bh(jh_in)->b_data);
  290. }
  291. mapped_data = kmap_atomic(new_page, KM_USER0);
  292. /*
  293. * Check for escaping
  294. */
  295. if (*((__be32 *)(mapped_data + new_offset)) ==
  296. cpu_to_be32(JFS_MAGIC_NUMBER)) {
  297. need_copy_out = 1;
  298. do_escape = 1;
  299. }
  300. kunmap_atomic(mapped_data, KM_USER0);
  301. /*
  302. * Do we need to do a data copy?
  303. */
  304. if (need_copy_out && !done_copy_out) {
  305. char *tmp;
  306. jbd_unlock_bh_state(bh_in);
  307. tmp = jbd_rep_kmalloc(bh_in->b_size, GFP_NOFS);
  308. jbd_lock_bh_state(bh_in);
  309. if (jh_in->b_frozen_data) {
  310. kfree(tmp);
  311. goto repeat;
  312. }
  313. jh_in->b_frozen_data = tmp;
  314. mapped_data = kmap_atomic(new_page, KM_USER0);
  315. memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
  316. kunmap_atomic(mapped_data, KM_USER0);
  317. new_page = virt_to_page(tmp);
  318. new_offset = offset_in_page(tmp);
  319. done_copy_out = 1;
  320. }
  321. /*
  322. * Did we need to do an escaping? Now we've done all the
  323. * copying, we can finally do so.
  324. */
  325. if (do_escape) {
  326. mapped_data = kmap_atomic(new_page, KM_USER0);
  327. *((unsigned int *)(mapped_data + new_offset)) = 0;
  328. kunmap_atomic(mapped_data, KM_USER0);
  329. }
  330. /* keep subsequent assertions sane */
  331. new_bh->b_state = 0;
  332. init_buffer(new_bh, NULL, NULL);
  333. atomic_set(&new_bh->b_count, 1);
  334. jbd_unlock_bh_state(bh_in);
  335. new_jh = journal_add_journal_head(new_bh); /* This sleeps */
  336. set_bh_page(new_bh, new_page, new_offset);
  337. new_jh->b_transaction = NULL;
  338. new_bh->b_size = jh2bh(jh_in)->b_size;
  339. new_bh->b_bdev = transaction->t_journal->j_dev;
  340. new_bh->b_blocknr = blocknr;
  341. set_buffer_mapped(new_bh);
  342. set_buffer_dirty(new_bh);
  343. *jh_out = new_jh;
  344. /*
  345. * The to-be-written buffer needs to get moved to the io queue,
  346. * and the original buffer whose contents we are shadowing or
  347. * copying is moved to the transaction's shadow queue.
  348. */
  349. JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
  350. journal_file_buffer(jh_in, transaction, BJ_Shadow);
  351. JBUFFER_TRACE(new_jh, "file as BJ_IO");
  352. journal_file_buffer(new_jh, transaction, BJ_IO);
  353. return do_escape | (done_copy_out << 1);
  354. }
  355. /*
  356. * Allocation code for the journal file. Manage the space left in the
  357. * journal, so that we can begin checkpointing when appropriate.
  358. */
  359. /*
  360. * __log_space_left: Return the number of free blocks left in the journal.
  361. *
  362. * Called with the journal already locked.
  363. *
  364. * Called under j_state_lock
  365. */
  366. int __log_space_left(journal_t *journal)
  367. {
  368. int left = journal->j_free;
  369. assert_spin_locked(&journal->j_state_lock);
  370. /*
  371. * Be pessimistic here about the number of those free blocks which
  372. * might be required for log descriptor control blocks.
  373. */
  374. #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
  375. left -= MIN_LOG_RESERVED_BLOCKS;
  376. if (left <= 0)
  377. return 0;
  378. left -= (left >> 3);
  379. return left;
  380. }
  381. /*
  382. * Called under j_state_lock. Returns true if a transaction was started.
  383. */
  384. int __log_start_commit(journal_t *journal, tid_t target)
  385. {
  386. /*
  387. * Are we already doing a recent enough commit?
  388. */
  389. if (!tid_geq(journal->j_commit_request, target)) {
  390. /*
  391. * We want a new commit: OK, mark the request and wakup the
  392. * commit thread. We do _not_ do the commit ourselves.
  393. */
  394. journal->j_commit_request = target;
  395. jbd_debug(1, "JBD: requesting commit %d/%d\n",
  396. journal->j_commit_request,
  397. journal->j_commit_sequence);
  398. wake_up(&journal->j_wait_commit);
  399. return 1;
  400. }
  401. return 0;
  402. }
  403. int log_start_commit(journal_t *journal, tid_t tid)
  404. {
  405. int ret;
  406. spin_lock(&journal->j_state_lock);
  407. ret = __log_start_commit(journal, tid);
  408. spin_unlock(&journal->j_state_lock);
  409. return ret;
  410. }
  411. /*
  412. * Force and wait upon a commit if the calling process is not within
  413. * transaction. This is used for forcing out undo-protected data which contains
  414. * bitmaps, when the fs is running out of space.
  415. *
  416. * We can only force the running transaction if we don't have an active handle;
  417. * otherwise, we will deadlock.
  418. *
  419. * Returns true if a transaction was started.
  420. */
  421. int journal_force_commit_nested(journal_t *journal)
  422. {
  423. transaction_t *transaction = NULL;
  424. tid_t tid;
  425. spin_lock(&journal->j_state_lock);
  426. if (journal->j_running_transaction && !current->journal_info) {
  427. transaction = journal->j_running_transaction;
  428. __log_start_commit(journal, transaction->t_tid);
  429. } else if (journal->j_committing_transaction)
  430. transaction = journal->j_committing_transaction;
  431. if (!transaction) {
  432. spin_unlock(&journal->j_state_lock);
  433. return 0; /* Nothing to retry */
  434. }
  435. tid = transaction->t_tid;
  436. spin_unlock(&journal->j_state_lock);
  437. log_wait_commit(journal, tid);
  438. return 1;
  439. }
  440. /*
  441. * Start a commit of the current running transaction (if any). Returns true
  442. * if a transaction was started, and fills its tid in at *ptid
  443. */
  444. int journal_start_commit(journal_t *journal, tid_t *ptid)
  445. {
  446. int ret = 0;
  447. spin_lock(&journal->j_state_lock);
  448. if (journal->j_running_transaction) {
  449. tid_t tid = journal->j_running_transaction->t_tid;
  450. ret = __log_start_commit(journal, tid);
  451. if (ret && ptid)
  452. *ptid = tid;
  453. } else if (journal->j_committing_transaction && ptid) {
  454. /*
  455. * If ext3_write_super() recently started a commit, then we
  456. * have to wait for completion of that transaction
  457. */
  458. *ptid = journal->j_committing_transaction->t_tid;
  459. ret = 1;
  460. }
  461. spin_unlock(&journal->j_state_lock);
  462. return ret;
  463. }
  464. /*
  465. * Wait for a specified commit to complete.
  466. * The caller may not hold the journal lock.
  467. */
  468. int log_wait_commit(journal_t *journal, tid_t tid)
  469. {
  470. int err = 0;
  471. #ifdef CONFIG_JBD_DEBUG
  472. spin_lock(&journal->j_state_lock);
  473. if (!tid_geq(journal->j_commit_request, tid)) {
  474. printk(KERN_EMERG
  475. "%s: error: j_commit_request=%d, tid=%d\n",
  476. __FUNCTION__, journal->j_commit_request, tid);
  477. }
  478. spin_unlock(&journal->j_state_lock);
  479. #endif
  480. spin_lock(&journal->j_state_lock);
  481. while (tid_gt(tid, journal->j_commit_sequence)) {
  482. jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
  483. tid, journal->j_commit_sequence);
  484. wake_up(&journal->j_wait_commit);
  485. spin_unlock(&journal->j_state_lock);
  486. wait_event(journal->j_wait_done_commit,
  487. !tid_gt(tid, journal->j_commit_sequence));
  488. spin_lock(&journal->j_state_lock);
  489. }
  490. spin_unlock(&journal->j_state_lock);
  491. if (unlikely(is_journal_aborted(journal))) {
  492. printk(KERN_EMERG "journal commit I/O error\n");
  493. err = -EIO;
  494. }
  495. return err;
  496. }
  497. /*
  498. * Log buffer allocation routines:
  499. */
  500. int journal_next_log_block(journal_t *journal, unsigned long *retp)
  501. {
  502. unsigned long blocknr;
  503. spin_lock(&journal->j_state_lock);
  504. J_ASSERT(journal->j_free > 1);
  505. blocknr = journal->j_head;
  506. journal->j_head++;
  507. journal->j_free--;
  508. if (journal->j_head == journal->j_last)
  509. journal->j_head = journal->j_first;
  510. spin_unlock(&journal->j_state_lock);
  511. return journal_bmap(journal, blocknr, retp);
  512. }
  513. /*
  514. * Conversion of logical to physical block numbers for the journal
  515. *
  516. * On external journals the journal blocks are identity-mapped, so
  517. * this is a no-op. If needed, we can use j_blk_offset - everything is
  518. * ready.
  519. */
  520. int journal_bmap(journal_t *journal, unsigned long blocknr,
  521. unsigned long *retp)
  522. {
  523. int err = 0;
  524. unsigned long ret;
  525. if (journal->j_inode) {
  526. ret = bmap(journal->j_inode, blocknr);
  527. if (ret)
  528. *retp = ret;
  529. else {
  530. char b[BDEVNAME_SIZE];
  531. printk(KERN_ALERT "%s: journal block not found "
  532. "at offset %lu on %s\n",
  533. __FUNCTION__,
  534. blocknr,
  535. bdevname(journal->j_dev, b));
  536. err = -EIO;
  537. __journal_abort_soft(journal, err);
  538. }
  539. } else {
  540. *retp = blocknr; /* +journal->j_blk_offset */
  541. }
  542. return err;
  543. }
  544. /*
  545. * We play buffer_head aliasing tricks to write data/metadata blocks to
  546. * the journal without copying their contents, but for journal
  547. * descriptor blocks we do need to generate bona fide buffers.
  548. *
  549. * After the caller of journal_get_descriptor_buffer() has finished modifying
  550. * the buffer's contents they really should run flush_dcache_page(bh->b_page).
  551. * But we don't bother doing that, so there will be coherency problems with
  552. * mmaps of blockdevs which hold live JBD-controlled filesystems.
  553. */
  554. struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
  555. {
  556. struct buffer_head *bh;
  557. unsigned long blocknr;
  558. int err;
  559. err = journal_next_log_block(journal, &blocknr);
  560. if (err)
  561. return NULL;
  562. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  563. lock_buffer(bh);
  564. memset(bh->b_data, 0, journal->j_blocksize);
  565. set_buffer_uptodate(bh);
  566. unlock_buffer(bh);
  567. BUFFER_TRACE(bh, "return this buffer");
  568. return journal_add_journal_head(bh);
  569. }
  570. /*
  571. * Management for journal control blocks: functions to create and
  572. * destroy journal_t structures, and to initialise and read existing
  573. * journal blocks from disk. */
  574. /* First: create and setup a journal_t object in memory. We initialise
  575. * very few fields yet: that has to wait until we have created the
  576. * journal structures from from scratch, or loaded them from disk. */
  577. static journal_t * journal_init_common (void)
  578. {
  579. journal_t *journal;
  580. int err;
  581. journal = jbd_kmalloc(sizeof(*journal), GFP_KERNEL);
  582. if (!journal)
  583. goto fail;
  584. memset(journal, 0, sizeof(*journal));
  585. init_waitqueue_head(&journal->j_wait_transaction_locked);
  586. init_waitqueue_head(&journal->j_wait_logspace);
  587. init_waitqueue_head(&journal->j_wait_done_commit);
  588. init_waitqueue_head(&journal->j_wait_checkpoint);
  589. init_waitqueue_head(&journal->j_wait_commit);
  590. init_waitqueue_head(&journal->j_wait_updates);
  591. init_MUTEX(&journal->j_barrier);
  592. init_MUTEX(&journal->j_checkpoint_sem);
  593. spin_lock_init(&journal->j_revoke_lock);
  594. spin_lock_init(&journal->j_list_lock);
  595. spin_lock_init(&journal->j_state_lock);
  596. journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
  597. /* The journal is marked for error until we succeed with recovery! */
  598. journal->j_flags = JFS_ABORT;
  599. /* Set up a default-sized revoke table for the new mount. */
  600. err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
  601. if (err) {
  602. kfree(journal);
  603. goto fail;
  604. }
  605. return journal;
  606. fail:
  607. return NULL;
  608. }
  609. /* journal_init_dev and journal_init_inode:
  610. *
  611. * Create a journal structure assigned some fixed set of disk blocks to
  612. * the journal. We don't actually touch those disk blocks yet, but we
  613. * need to set up all of the mapping information to tell the journaling
  614. * system where the journal blocks are.
  615. *
  616. */
  617. /**
  618. * journal_t * journal_init_dev() - creates an initialises a journal structure
  619. * @bdev: Block device on which to create the journal
  620. * @fs_dev: Device which hold journalled filesystem for this journal.
  621. * @start: Block nr Start of journal.
  622. * @len: Lenght of the journal in blocks.
  623. * @blocksize: blocksize of journalling device
  624. * @returns: a newly created journal_t *
  625. *
  626. * journal_init_dev creates a journal which maps a fixed contiguous
  627. * range of blocks on an arbitrary block device.
  628. *
  629. */
  630. journal_t * journal_init_dev(struct block_device *bdev,
  631. struct block_device *fs_dev,
  632. int start, int len, int blocksize)
  633. {
  634. journal_t *journal = journal_init_common();
  635. struct buffer_head *bh;
  636. int n;
  637. if (!journal)
  638. return NULL;
  639. journal->j_dev = bdev;
  640. journal->j_fs_dev = fs_dev;
  641. journal->j_blk_offset = start;
  642. journal->j_maxlen = len;
  643. journal->j_blocksize = blocksize;
  644. bh = __getblk(journal->j_dev, start, journal->j_blocksize);
  645. J_ASSERT(bh != NULL);
  646. journal->j_sb_buffer = bh;
  647. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  648. /* journal descriptor can store up to n blocks -bzzz */
  649. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  650. journal->j_wbufsize = n;
  651. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  652. if (!journal->j_wbuf) {
  653. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  654. __FUNCTION__);
  655. kfree(journal);
  656. journal = NULL;
  657. }
  658. return journal;
  659. }
  660. /**
  661. * journal_t * journal_init_inode () - creates a journal which maps to a inode.
  662. * @inode: An inode to create the journal in
  663. *
  664. * journal_init_inode creates a journal which maps an on-disk inode as
  665. * the journal. The inode must exist already, must support bmap() and
  666. * must have all data blocks preallocated.
  667. */
  668. journal_t * journal_init_inode (struct inode *inode)
  669. {
  670. struct buffer_head *bh;
  671. journal_t *journal = journal_init_common();
  672. int err;
  673. int n;
  674. unsigned long blocknr;
  675. if (!journal)
  676. return NULL;
  677. journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
  678. journal->j_inode = inode;
  679. jbd_debug(1,
  680. "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
  681. journal, inode->i_sb->s_id, inode->i_ino,
  682. (long long) inode->i_size,
  683. inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
  684. journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
  685. journal->j_blocksize = inode->i_sb->s_blocksize;
  686. /* journal descriptor can store up to n blocks -bzzz */
  687. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  688. journal->j_wbufsize = n;
  689. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  690. if (!journal->j_wbuf) {
  691. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  692. __FUNCTION__);
  693. kfree(journal);
  694. return NULL;
  695. }
  696. err = journal_bmap(journal, 0, &blocknr);
  697. /* If that failed, give up */
  698. if (err) {
  699. printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
  700. __FUNCTION__);
  701. kfree(journal);
  702. return NULL;
  703. }
  704. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  705. J_ASSERT(bh != NULL);
  706. journal->j_sb_buffer = bh;
  707. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  708. return journal;
  709. }
  710. /*
  711. * If the journal init or create aborts, we need to mark the journal
  712. * superblock as being NULL to prevent the journal destroy from writing
  713. * back a bogus superblock.
  714. */
  715. static void journal_fail_superblock (journal_t *journal)
  716. {
  717. struct buffer_head *bh = journal->j_sb_buffer;
  718. brelse(bh);
  719. journal->j_sb_buffer = NULL;
  720. }
  721. /*
  722. * Given a journal_t structure, initialise the various fields for
  723. * startup of a new journaling session. We use this both when creating
  724. * a journal, and after recovering an old journal to reset it for
  725. * subsequent use.
  726. */
  727. static int journal_reset(journal_t *journal)
  728. {
  729. journal_superblock_t *sb = journal->j_superblock;
  730. unsigned int first, last;
  731. first = be32_to_cpu(sb->s_first);
  732. last = be32_to_cpu(sb->s_maxlen);
  733. journal->j_first = first;
  734. journal->j_last = last;
  735. journal->j_head = first;
  736. journal->j_tail = first;
  737. journal->j_free = last - first;
  738. journal->j_tail_sequence = journal->j_transaction_sequence;
  739. journal->j_commit_sequence = journal->j_transaction_sequence - 1;
  740. journal->j_commit_request = journal->j_commit_sequence;
  741. journal->j_max_transaction_buffers = journal->j_maxlen / 4;
  742. /* Add the dynamic fields and write it to disk. */
  743. journal_update_superblock(journal, 1);
  744. journal_start_thread(journal);
  745. return 0;
  746. }
  747. /**
  748. * int journal_create() - Initialise the new journal file
  749. * @journal: Journal to create. This structure must have been initialised
  750. *
  751. * Given a journal_t structure which tells us which disk blocks we can
  752. * use, create a new journal superblock and initialise all of the
  753. * journal fields from scratch.
  754. **/
  755. int journal_create(journal_t *journal)
  756. {
  757. unsigned long blocknr;
  758. struct buffer_head *bh;
  759. journal_superblock_t *sb;
  760. int i, err;
  761. if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
  762. printk (KERN_ERR "Journal length (%d blocks) too short.\n",
  763. journal->j_maxlen);
  764. journal_fail_superblock(journal);
  765. return -EINVAL;
  766. }
  767. if (journal->j_inode == NULL) {
  768. /*
  769. * We don't know what block to start at!
  770. */
  771. printk(KERN_EMERG
  772. "%s: creation of journal on external device!\n",
  773. __FUNCTION__);
  774. BUG();
  775. }
  776. /* Zero out the entire journal on disk. We cannot afford to
  777. have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
  778. jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
  779. for (i = 0; i < journal->j_maxlen; i++) {
  780. err = journal_bmap(journal, i, &blocknr);
  781. if (err)
  782. return err;
  783. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  784. lock_buffer(bh);
  785. memset (bh->b_data, 0, journal->j_blocksize);
  786. BUFFER_TRACE(bh, "marking dirty");
  787. mark_buffer_dirty(bh);
  788. BUFFER_TRACE(bh, "marking uptodate");
  789. set_buffer_uptodate(bh);
  790. unlock_buffer(bh);
  791. __brelse(bh);
  792. }
  793. sync_blockdev(journal->j_dev);
  794. jbd_debug(1, "JBD: journal cleared.\n");
  795. /* OK, fill in the initial static fields in the new superblock */
  796. sb = journal->j_superblock;
  797. sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
  798. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  799. sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
  800. sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
  801. sb->s_first = cpu_to_be32(1);
  802. journal->j_transaction_sequence = 1;
  803. journal->j_flags &= ~JFS_ABORT;
  804. journal->j_format_version = 2;
  805. return journal_reset(journal);
  806. }
  807. /**
  808. * void journal_update_superblock() - Update journal sb on disk.
  809. * @journal: The journal to update.
  810. * @wait: Set to '0' if you don't want to wait for IO completion.
  811. *
  812. * Update a journal's dynamic superblock fields and write it to disk,
  813. * optionally waiting for the IO to complete.
  814. */
  815. void journal_update_superblock(journal_t *journal, int wait)
  816. {
  817. journal_superblock_t *sb = journal->j_superblock;
  818. struct buffer_head *bh = journal->j_sb_buffer;
  819. /*
  820. * As a special case, if the on-disk copy is already marked as needing
  821. * no recovery (s_start == 0) and there are no outstanding transactions
  822. * in the filesystem, then we can safely defer the superblock update
  823. * until the next commit by setting JFS_FLUSHED. This avoids
  824. * attempting a write to a potential-readonly device.
  825. */
  826. if (sb->s_start == 0 && journal->j_tail_sequence ==
  827. journal->j_transaction_sequence) {
  828. jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
  829. "(start %ld, seq %d, errno %d)\n",
  830. journal->j_tail, journal->j_tail_sequence,
  831. journal->j_errno);
  832. goto out;
  833. }
  834. spin_lock(&journal->j_state_lock);
  835. jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
  836. journal->j_tail, journal->j_tail_sequence, journal->j_errno);
  837. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  838. sb->s_start = cpu_to_be32(journal->j_tail);
  839. sb->s_errno = cpu_to_be32(journal->j_errno);
  840. spin_unlock(&journal->j_state_lock);
  841. BUFFER_TRACE(bh, "marking dirty");
  842. mark_buffer_dirty(bh);
  843. if (wait)
  844. sync_dirty_buffer(bh);
  845. else
  846. ll_rw_block(WRITE, 1, &bh);
  847. out:
  848. /* If we have just flushed the log (by marking s_start==0), then
  849. * any future commit will have to be careful to update the
  850. * superblock again to re-record the true start of the log. */
  851. spin_lock(&journal->j_state_lock);
  852. if (sb->s_start)
  853. journal->j_flags &= ~JFS_FLUSHED;
  854. else
  855. journal->j_flags |= JFS_FLUSHED;
  856. spin_unlock(&journal->j_state_lock);
  857. }
  858. /*
  859. * Read the superblock for a given journal, performing initial
  860. * validation of the format.
  861. */
  862. static int journal_get_superblock(journal_t *journal)
  863. {
  864. struct buffer_head *bh;
  865. journal_superblock_t *sb;
  866. int err = -EIO;
  867. bh = journal->j_sb_buffer;
  868. J_ASSERT(bh != NULL);
  869. if (!buffer_uptodate(bh)) {
  870. ll_rw_block(READ, 1, &bh);
  871. wait_on_buffer(bh);
  872. if (!buffer_uptodate(bh)) {
  873. printk (KERN_ERR
  874. "JBD: IO error reading journal superblock\n");
  875. goto out;
  876. }
  877. }
  878. sb = journal->j_superblock;
  879. err = -EINVAL;
  880. if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
  881. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  882. printk(KERN_WARNING "JBD: no valid journal superblock found\n");
  883. goto out;
  884. }
  885. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  886. case JFS_SUPERBLOCK_V1:
  887. journal->j_format_version = 1;
  888. break;
  889. case JFS_SUPERBLOCK_V2:
  890. journal->j_format_version = 2;
  891. break;
  892. default:
  893. printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
  894. goto out;
  895. }
  896. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  897. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  898. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  899. printk (KERN_WARNING "JBD: journal file too short\n");
  900. goto out;
  901. }
  902. return 0;
  903. out:
  904. journal_fail_superblock(journal);
  905. return err;
  906. }
  907. /*
  908. * Load the on-disk journal superblock and read the key fields into the
  909. * journal_t.
  910. */
  911. static int load_superblock(journal_t *journal)
  912. {
  913. int err;
  914. journal_superblock_t *sb;
  915. err = journal_get_superblock(journal);
  916. if (err)
  917. return err;
  918. sb = journal->j_superblock;
  919. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  920. journal->j_tail = be32_to_cpu(sb->s_start);
  921. journal->j_first = be32_to_cpu(sb->s_first);
  922. journal->j_last = be32_to_cpu(sb->s_maxlen);
  923. journal->j_errno = be32_to_cpu(sb->s_errno);
  924. return 0;
  925. }
  926. /**
  927. * int journal_load() - Read journal from disk.
  928. * @journal: Journal to act on.
  929. *
  930. * Given a journal_t structure which tells us which disk blocks contain
  931. * a journal, read the journal from disk to initialise the in-memory
  932. * structures.
  933. */
  934. int journal_load(journal_t *journal)
  935. {
  936. int err;
  937. err = load_superblock(journal);
  938. if (err)
  939. return err;
  940. /* If this is a V2 superblock, then we have to check the
  941. * features flags on it. */
  942. if (journal->j_format_version >= 2) {
  943. journal_superblock_t *sb = journal->j_superblock;
  944. if ((sb->s_feature_ro_compat &
  945. ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
  946. (sb->s_feature_incompat &
  947. ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
  948. printk (KERN_WARNING
  949. "JBD: Unrecognised features on journal\n");
  950. return -EINVAL;
  951. }
  952. }
  953. /* Let the recovery code check whether it needs to recover any
  954. * data from the journal. */
  955. if (journal_recover(journal))
  956. goto recovery_error;
  957. /* OK, we've finished with the dynamic journal bits:
  958. * reinitialise the dynamic contents of the superblock in memory
  959. * and reset them on disk. */
  960. if (journal_reset(journal))
  961. goto recovery_error;
  962. journal->j_flags &= ~JFS_ABORT;
  963. journal->j_flags |= JFS_LOADED;
  964. return 0;
  965. recovery_error:
  966. printk (KERN_WARNING "JBD: recovery failed\n");
  967. return -EIO;
  968. }
  969. /**
  970. * void journal_destroy() - Release a journal_t structure.
  971. * @journal: Journal to act on.
  972. *
  973. * Release a journal_t structure once it is no longer in use by the
  974. * journaled object.
  975. */
  976. void journal_destroy(journal_t *journal)
  977. {
  978. /* Wait for the commit thread to wake up and die. */
  979. journal_kill_thread(journal);
  980. /* Force a final log commit */
  981. if (journal->j_running_transaction)
  982. journal_commit_transaction(journal);
  983. /* Force any old transactions to disk */
  984. /* Totally anal locking here... */
  985. spin_lock(&journal->j_list_lock);
  986. while (journal->j_checkpoint_transactions != NULL) {
  987. spin_unlock(&journal->j_list_lock);
  988. log_do_checkpoint(journal);
  989. spin_lock(&journal->j_list_lock);
  990. }
  991. J_ASSERT(journal->j_running_transaction == NULL);
  992. J_ASSERT(journal->j_committing_transaction == NULL);
  993. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  994. spin_unlock(&journal->j_list_lock);
  995. /* We can now mark the journal as empty. */
  996. journal->j_tail = 0;
  997. journal->j_tail_sequence = ++journal->j_transaction_sequence;
  998. if (journal->j_sb_buffer) {
  999. journal_update_superblock(journal, 1);
  1000. brelse(journal->j_sb_buffer);
  1001. }
  1002. if (journal->j_inode)
  1003. iput(journal->j_inode);
  1004. if (journal->j_revoke)
  1005. journal_destroy_revoke(journal);
  1006. kfree(journal->j_wbuf);
  1007. kfree(journal);
  1008. }
  1009. /**
  1010. *int journal_check_used_features () - Check if features specified are used.
  1011. * @journal: Journal to check.
  1012. * @compat: bitmask of compatible features
  1013. * @ro: bitmask of features that force read-only mount
  1014. * @incompat: bitmask of incompatible features
  1015. *
  1016. * Check whether the journal uses all of a given set of
  1017. * features. Return true (non-zero) if it does.
  1018. **/
  1019. int journal_check_used_features (journal_t *journal, unsigned long compat,
  1020. unsigned long ro, unsigned long incompat)
  1021. {
  1022. journal_superblock_t *sb;
  1023. if (!compat && !ro && !incompat)
  1024. return 1;
  1025. if (journal->j_format_version == 1)
  1026. return 0;
  1027. sb = journal->j_superblock;
  1028. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1029. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1030. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1031. return 1;
  1032. return 0;
  1033. }
  1034. /**
  1035. * int journal_check_available_features() - Check feature set in journalling layer
  1036. * @journal: Journal to check.
  1037. * @compat: bitmask of compatible features
  1038. * @ro: bitmask of features that force read-only mount
  1039. * @incompat: bitmask of incompatible features
  1040. *
  1041. * Check whether the journaling code supports the use of
  1042. * all of a given set of features on this journal. Return true
  1043. * (non-zero) if it can. */
  1044. int journal_check_available_features (journal_t *journal, unsigned long compat,
  1045. unsigned long ro, unsigned long incompat)
  1046. {
  1047. journal_superblock_t *sb;
  1048. if (!compat && !ro && !incompat)
  1049. return 1;
  1050. sb = journal->j_superblock;
  1051. /* We can support any known requested features iff the
  1052. * superblock is in version 2. Otherwise we fail to support any
  1053. * extended sb features. */
  1054. if (journal->j_format_version != 2)
  1055. return 0;
  1056. if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
  1057. (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1058. (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
  1059. return 1;
  1060. return 0;
  1061. }
  1062. /**
  1063. * int journal_set_features () - Mark a given journal feature in the superblock
  1064. * @journal: Journal to act on.
  1065. * @compat: bitmask of compatible features
  1066. * @ro: bitmask of features that force read-only mount
  1067. * @incompat: bitmask of incompatible features
  1068. *
  1069. * Mark a given journal feature as present on the
  1070. * superblock. Returns true if the requested features could be set.
  1071. *
  1072. */
  1073. int journal_set_features (journal_t *journal, unsigned long compat,
  1074. unsigned long ro, unsigned long incompat)
  1075. {
  1076. journal_superblock_t *sb;
  1077. if (journal_check_used_features(journal, compat, ro, incompat))
  1078. return 1;
  1079. if (!journal_check_available_features(journal, compat, ro, incompat))
  1080. return 0;
  1081. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1082. compat, ro, incompat);
  1083. sb = journal->j_superblock;
  1084. sb->s_feature_compat |= cpu_to_be32(compat);
  1085. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1086. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1087. return 1;
  1088. }
  1089. /**
  1090. * int journal_update_format () - Update on-disk journal structure.
  1091. * @journal: Journal to act on.
  1092. *
  1093. * Given an initialised but unloaded journal struct, poke about in the
  1094. * on-disk structure to update it to the most recent supported version.
  1095. */
  1096. int journal_update_format (journal_t *journal)
  1097. {
  1098. journal_superblock_t *sb;
  1099. int err;
  1100. err = journal_get_superblock(journal);
  1101. if (err)
  1102. return err;
  1103. sb = journal->j_superblock;
  1104. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1105. case JFS_SUPERBLOCK_V2:
  1106. return 0;
  1107. case JFS_SUPERBLOCK_V1:
  1108. return journal_convert_superblock_v1(journal, sb);
  1109. default:
  1110. break;
  1111. }
  1112. return -EINVAL;
  1113. }
  1114. static int journal_convert_superblock_v1(journal_t *journal,
  1115. journal_superblock_t *sb)
  1116. {
  1117. int offset, blocksize;
  1118. struct buffer_head *bh;
  1119. printk(KERN_WARNING
  1120. "JBD: Converting superblock from version 1 to 2.\n");
  1121. /* Pre-initialise new fields to zero */
  1122. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1123. blocksize = be32_to_cpu(sb->s_blocksize);
  1124. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1125. sb->s_nr_users = cpu_to_be32(1);
  1126. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  1127. journal->j_format_version = 2;
  1128. bh = journal->j_sb_buffer;
  1129. BUFFER_TRACE(bh, "marking dirty");
  1130. mark_buffer_dirty(bh);
  1131. sync_dirty_buffer(bh);
  1132. return 0;
  1133. }
  1134. /**
  1135. * int journal_flush () - Flush journal
  1136. * @journal: Journal to act on.
  1137. *
  1138. * Flush all data for a given journal to disk and empty the journal.
  1139. * Filesystems can use this when remounting readonly to ensure that
  1140. * recovery does not need to happen on remount.
  1141. */
  1142. int journal_flush(journal_t *journal)
  1143. {
  1144. int err = 0;
  1145. transaction_t *transaction = NULL;
  1146. unsigned long old_tail;
  1147. spin_lock(&journal->j_state_lock);
  1148. /* Force everything buffered to the log... */
  1149. if (journal->j_running_transaction) {
  1150. transaction = journal->j_running_transaction;
  1151. __log_start_commit(journal, transaction->t_tid);
  1152. } else if (journal->j_committing_transaction)
  1153. transaction = journal->j_committing_transaction;
  1154. /* Wait for the log commit to complete... */
  1155. if (transaction) {
  1156. tid_t tid = transaction->t_tid;
  1157. spin_unlock(&journal->j_state_lock);
  1158. log_wait_commit(journal, tid);
  1159. } else {
  1160. spin_unlock(&journal->j_state_lock);
  1161. }
  1162. /* ...and flush everything in the log out to disk. */
  1163. spin_lock(&journal->j_list_lock);
  1164. while (!err && journal->j_checkpoint_transactions != NULL) {
  1165. spin_unlock(&journal->j_list_lock);
  1166. err = log_do_checkpoint(journal);
  1167. spin_lock(&journal->j_list_lock);
  1168. }
  1169. spin_unlock(&journal->j_list_lock);
  1170. cleanup_journal_tail(journal);
  1171. /* Finally, mark the journal as really needing no recovery.
  1172. * This sets s_start==0 in the underlying superblock, which is
  1173. * the magic code for a fully-recovered superblock. Any future
  1174. * commits of data to the journal will restore the current
  1175. * s_start value. */
  1176. spin_lock(&journal->j_state_lock);
  1177. old_tail = journal->j_tail;
  1178. journal->j_tail = 0;
  1179. spin_unlock(&journal->j_state_lock);
  1180. journal_update_superblock(journal, 1);
  1181. spin_lock(&journal->j_state_lock);
  1182. journal->j_tail = old_tail;
  1183. J_ASSERT(!journal->j_running_transaction);
  1184. J_ASSERT(!journal->j_committing_transaction);
  1185. J_ASSERT(!journal->j_checkpoint_transactions);
  1186. J_ASSERT(journal->j_head == journal->j_tail);
  1187. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1188. spin_unlock(&journal->j_state_lock);
  1189. return err;
  1190. }
  1191. /**
  1192. * int journal_wipe() - Wipe journal contents
  1193. * @journal: Journal to act on.
  1194. * @write: flag (see below)
  1195. *
  1196. * Wipe out all of the contents of a journal, safely. This will produce
  1197. * a warning if the journal contains any valid recovery information.
  1198. * Must be called between journal_init_*() and journal_load().
  1199. *
  1200. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1201. * we merely suppress recovery.
  1202. */
  1203. int journal_wipe(journal_t *journal, int write)
  1204. {
  1205. journal_superblock_t *sb;
  1206. int err = 0;
  1207. J_ASSERT (!(journal->j_flags & JFS_LOADED));
  1208. err = load_superblock(journal);
  1209. if (err)
  1210. return err;
  1211. sb = journal->j_superblock;
  1212. if (!journal->j_tail)
  1213. goto no_recovery;
  1214. printk (KERN_WARNING "JBD: %s recovery information on journal\n",
  1215. write ? "Clearing" : "Ignoring");
  1216. err = journal_skip_recovery(journal);
  1217. if (write)
  1218. journal_update_superblock(journal, 1);
  1219. no_recovery:
  1220. return err;
  1221. }
  1222. /*
  1223. * journal_dev_name: format a character string to describe on what
  1224. * device this journal is present.
  1225. */
  1226. const char *journal_dev_name(journal_t *journal, char *buffer)
  1227. {
  1228. struct block_device *bdev;
  1229. if (journal->j_inode)
  1230. bdev = journal->j_inode->i_sb->s_bdev;
  1231. else
  1232. bdev = journal->j_dev;
  1233. return bdevname(bdev, buffer);
  1234. }
  1235. /*
  1236. * Journal abort has very specific semantics, which we describe
  1237. * for journal abort.
  1238. *
  1239. * Two internal function, which provide abort to te jbd layer
  1240. * itself are here.
  1241. */
  1242. /*
  1243. * Quick version for internal journal use (doesn't lock the journal).
  1244. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1245. * and don't attempt to make any other journal updates.
  1246. */
  1247. void __journal_abort_hard(journal_t *journal)
  1248. {
  1249. transaction_t *transaction;
  1250. char b[BDEVNAME_SIZE];
  1251. if (journal->j_flags & JFS_ABORT)
  1252. return;
  1253. printk(KERN_ERR "Aborting journal on device %s.\n",
  1254. journal_dev_name(journal, b));
  1255. spin_lock(&journal->j_state_lock);
  1256. journal->j_flags |= JFS_ABORT;
  1257. transaction = journal->j_running_transaction;
  1258. if (transaction)
  1259. __log_start_commit(journal, transaction->t_tid);
  1260. spin_unlock(&journal->j_state_lock);
  1261. }
  1262. /* Soft abort: record the abort error status in the journal superblock,
  1263. * but don't do any other IO. */
  1264. void __journal_abort_soft (journal_t *journal, int errno)
  1265. {
  1266. if (journal->j_flags & JFS_ABORT)
  1267. return;
  1268. if (!journal->j_errno)
  1269. journal->j_errno = errno;
  1270. __journal_abort_hard(journal);
  1271. if (errno)
  1272. journal_update_superblock(journal, 1);
  1273. }
  1274. /**
  1275. * void journal_abort () - Shutdown the journal immediately.
  1276. * @journal: the journal to shutdown.
  1277. * @errno: an error number to record in the journal indicating
  1278. * the reason for the shutdown.
  1279. *
  1280. * Perform a complete, immediate shutdown of the ENTIRE
  1281. * journal (not of a single transaction). This operation cannot be
  1282. * undone without closing and reopening the journal.
  1283. *
  1284. * The journal_abort function is intended to support higher level error
  1285. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1286. * mode.
  1287. *
  1288. * Journal abort has very specific semantics. Any existing dirty,
  1289. * unjournaled buffers in the main filesystem will still be written to
  1290. * disk by bdflush, but the journaling mechanism will be suspended
  1291. * immediately and no further transaction commits will be honoured.
  1292. *
  1293. * Any dirty, journaled buffers will be written back to disk without
  1294. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1295. * filesystem, but we _do_ attempt to leave as much data as possible
  1296. * behind for fsck to use for cleanup.
  1297. *
  1298. * Any attempt to get a new transaction handle on a journal which is in
  1299. * ABORT state will just result in an -EROFS error return. A
  1300. * journal_stop on an existing handle will return -EIO if we have
  1301. * entered abort state during the update.
  1302. *
  1303. * Recursive transactions are not disturbed by journal abort until the
  1304. * final journal_stop, which will receive the -EIO error.
  1305. *
  1306. * Finally, the journal_abort call allows the caller to supply an errno
  1307. * which will be recorded (if possible) in the journal superblock. This
  1308. * allows a client to record failure conditions in the middle of a
  1309. * transaction without having to complete the transaction to record the
  1310. * failure to disk. ext3_error, for example, now uses this
  1311. * functionality.
  1312. *
  1313. * Errors which originate from within the journaling layer will NOT
  1314. * supply an errno; a null errno implies that absolutely no further
  1315. * writes are done to the journal (unless there are any already in
  1316. * progress).
  1317. *
  1318. */
  1319. void journal_abort(journal_t *journal, int errno)
  1320. {
  1321. __journal_abort_soft(journal, errno);
  1322. }
  1323. /**
  1324. * int journal_errno () - returns the journal's error state.
  1325. * @journal: journal to examine.
  1326. *
  1327. * This is the errno numbet set with journal_abort(), the last
  1328. * time the journal was mounted - if the journal was stopped
  1329. * without calling abort this will be 0.
  1330. *
  1331. * If the journal has been aborted on this mount time -EROFS will
  1332. * be returned.
  1333. */
  1334. int journal_errno(journal_t *journal)
  1335. {
  1336. int err;
  1337. spin_lock(&journal->j_state_lock);
  1338. if (journal->j_flags & JFS_ABORT)
  1339. err = -EROFS;
  1340. else
  1341. err = journal->j_errno;
  1342. spin_unlock(&journal->j_state_lock);
  1343. return err;
  1344. }
  1345. /**
  1346. * int journal_clear_err () - clears the journal's error state
  1347. * @journal: journal to act on.
  1348. *
  1349. * An error must be cleared or Acked to take a FS out of readonly
  1350. * mode.
  1351. */
  1352. int journal_clear_err(journal_t *journal)
  1353. {
  1354. int err = 0;
  1355. spin_lock(&journal->j_state_lock);
  1356. if (journal->j_flags & JFS_ABORT)
  1357. err = -EROFS;
  1358. else
  1359. journal->j_errno = 0;
  1360. spin_unlock(&journal->j_state_lock);
  1361. return err;
  1362. }
  1363. /**
  1364. * void journal_ack_err() - Ack journal err.
  1365. * @journal: journal to act on.
  1366. *
  1367. * An error must be cleared or Acked to take a FS out of readonly
  1368. * mode.
  1369. */
  1370. void journal_ack_err(journal_t *journal)
  1371. {
  1372. spin_lock(&journal->j_state_lock);
  1373. if (journal->j_errno)
  1374. journal->j_flags |= JFS_ACK_ERR;
  1375. spin_unlock(&journal->j_state_lock);
  1376. }
  1377. int journal_blocks_per_page(struct inode *inode)
  1378. {
  1379. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1380. }
  1381. /*
  1382. * Simple support for retrying memory allocations. Introduced to help to
  1383. * debug different VM deadlock avoidance strategies.
  1384. */
  1385. void * __jbd_kmalloc (const char *where, size_t size, int flags, int retry)
  1386. {
  1387. return kmalloc(size, flags | (retry ? __GFP_NOFAIL : 0));
  1388. }
  1389. /*
  1390. * Journal_head storage management
  1391. */
  1392. static kmem_cache_t *journal_head_cache;
  1393. #ifdef CONFIG_JBD_DEBUG
  1394. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1395. #endif
  1396. static int journal_init_journal_head_cache(void)
  1397. {
  1398. int retval;
  1399. J_ASSERT(journal_head_cache == 0);
  1400. journal_head_cache = kmem_cache_create("journal_head",
  1401. sizeof(struct journal_head),
  1402. 0, /* offset */
  1403. 0, /* flags */
  1404. NULL, /* ctor */
  1405. NULL); /* dtor */
  1406. retval = 0;
  1407. if (journal_head_cache == 0) {
  1408. retval = -ENOMEM;
  1409. printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
  1410. }
  1411. return retval;
  1412. }
  1413. static void journal_destroy_journal_head_cache(void)
  1414. {
  1415. J_ASSERT(journal_head_cache != NULL);
  1416. kmem_cache_destroy(journal_head_cache);
  1417. journal_head_cache = NULL;
  1418. }
  1419. /*
  1420. * journal_head splicing and dicing
  1421. */
  1422. static struct journal_head *journal_alloc_journal_head(void)
  1423. {
  1424. struct journal_head *ret;
  1425. static unsigned long last_warning;
  1426. #ifdef CONFIG_JBD_DEBUG
  1427. atomic_inc(&nr_journal_heads);
  1428. #endif
  1429. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1430. if (ret == 0) {
  1431. jbd_debug(1, "out of memory for journal_head\n");
  1432. if (time_after(jiffies, last_warning + 5*HZ)) {
  1433. printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
  1434. __FUNCTION__);
  1435. last_warning = jiffies;
  1436. }
  1437. while (ret == 0) {
  1438. yield();
  1439. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1440. }
  1441. }
  1442. return ret;
  1443. }
  1444. static void journal_free_journal_head(struct journal_head *jh)
  1445. {
  1446. #ifdef CONFIG_JBD_DEBUG
  1447. atomic_dec(&nr_journal_heads);
  1448. memset(jh, 0x5b, sizeof(*jh));
  1449. #endif
  1450. kmem_cache_free(journal_head_cache, jh);
  1451. }
  1452. /*
  1453. * A journal_head is attached to a buffer_head whenever JBD has an
  1454. * interest in the buffer.
  1455. *
  1456. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1457. * is set. This bit is tested in core kernel code where we need to take
  1458. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1459. * there.
  1460. *
  1461. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1462. *
  1463. * When a buffer has its BH_JBD bit set it is immune from being released by
  1464. * core kernel code, mainly via ->b_count.
  1465. *
  1466. * A journal_head may be detached from its buffer_head when the journal_head's
  1467. * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
  1468. * Various places in JBD call journal_remove_journal_head() to indicate that the
  1469. * journal_head can be dropped if needed.
  1470. *
  1471. * Various places in the kernel want to attach a journal_head to a buffer_head
  1472. * _before_ attaching the journal_head to a transaction. To protect the
  1473. * journal_head in this situation, journal_add_journal_head elevates the
  1474. * journal_head's b_jcount refcount by one. The caller must call
  1475. * journal_put_journal_head() to undo this.
  1476. *
  1477. * So the typical usage would be:
  1478. *
  1479. * (Attach a journal_head if needed. Increments b_jcount)
  1480. * struct journal_head *jh = journal_add_journal_head(bh);
  1481. * ...
  1482. * jh->b_transaction = xxx;
  1483. * journal_put_journal_head(jh);
  1484. *
  1485. * Now, the journal_head's b_jcount is zero, but it is safe from being released
  1486. * because it has a non-zero b_transaction.
  1487. */
  1488. /*
  1489. * Give a buffer_head a journal_head.
  1490. *
  1491. * Doesn't need the journal lock.
  1492. * May sleep.
  1493. */
  1494. struct journal_head *journal_add_journal_head(struct buffer_head *bh)
  1495. {
  1496. struct journal_head *jh;
  1497. struct journal_head *new_jh = NULL;
  1498. repeat:
  1499. if (!buffer_jbd(bh)) {
  1500. new_jh = journal_alloc_journal_head();
  1501. memset(new_jh, 0, sizeof(*new_jh));
  1502. }
  1503. jbd_lock_bh_journal_head(bh);
  1504. if (buffer_jbd(bh)) {
  1505. jh = bh2jh(bh);
  1506. } else {
  1507. J_ASSERT_BH(bh,
  1508. (atomic_read(&bh->b_count) > 0) ||
  1509. (bh->b_page && bh->b_page->mapping));
  1510. if (!new_jh) {
  1511. jbd_unlock_bh_journal_head(bh);
  1512. goto repeat;
  1513. }
  1514. jh = new_jh;
  1515. new_jh = NULL; /* We consumed it */
  1516. set_buffer_jbd(bh);
  1517. bh->b_private = jh;
  1518. jh->b_bh = bh;
  1519. get_bh(bh);
  1520. BUFFER_TRACE(bh, "added journal_head");
  1521. }
  1522. jh->b_jcount++;
  1523. jbd_unlock_bh_journal_head(bh);
  1524. if (new_jh)
  1525. journal_free_journal_head(new_jh);
  1526. return bh->b_private;
  1527. }
  1528. /*
  1529. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1530. * having a journal_head, return NULL
  1531. */
  1532. struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
  1533. {
  1534. struct journal_head *jh = NULL;
  1535. jbd_lock_bh_journal_head(bh);
  1536. if (buffer_jbd(bh)) {
  1537. jh = bh2jh(bh);
  1538. jh->b_jcount++;
  1539. }
  1540. jbd_unlock_bh_journal_head(bh);
  1541. return jh;
  1542. }
  1543. static void __journal_remove_journal_head(struct buffer_head *bh)
  1544. {
  1545. struct journal_head *jh = bh2jh(bh);
  1546. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1547. get_bh(bh);
  1548. if (jh->b_jcount == 0) {
  1549. if (jh->b_transaction == NULL &&
  1550. jh->b_next_transaction == NULL &&
  1551. jh->b_cp_transaction == NULL) {
  1552. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1553. J_ASSERT_BH(bh, buffer_jbd(bh));
  1554. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1555. BUFFER_TRACE(bh, "remove journal_head");
  1556. if (jh->b_frozen_data) {
  1557. printk(KERN_WARNING "%s: freeing "
  1558. "b_frozen_data\n",
  1559. __FUNCTION__);
  1560. kfree(jh->b_frozen_data);
  1561. }
  1562. if (jh->b_committed_data) {
  1563. printk(KERN_WARNING "%s: freeing "
  1564. "b_committed_data\n",
  1565. __FUNCTION__);
  1566. kfree(jh->b_committed_data);
  1567. }
  1568. bh->b_private = NULL;
  1569. jh->b_bh = NULL; /* debug, really */
  1570. clear_buffer_jbd(bh);
  1571. __brelse(bh);
  1572. journal_free_journal_head(jh);
  1573. } else {
  1574. BUFFER_TRACE(bh, "journal_head was locked");
  1575. }
  1576. }
  1577. }
  1578. /*
  1579. * journal_remove_journal_head(): if the buffer isn't attached to a transaction
  1580. * and has a zero b_jcount then remove and release its journal_head. If we did
  1581. * see that the buffer is not used by any transaction we also "logically"
  1582. * decrement ->b_count.
  1583. *
  1584. * We in fact take an additional increment on ->b_count as a convenience,
  1585. * because the caller usually wants to do additional things with the bh
  1586. * after calling here.
  1587. * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
  1588. * time. Once the caller has run __brelse(), the buffer is eligible for
  1589. * reaping by try_to_free_buffers().
  1590. */
  1591. void journal_remove_journal_head(struct buffer_head *bh)
  1592. {
  1593. jbd_lock_bh_journal_head(bh);
  1594. __journal_remove_journal_head(bh);
  1595. jbd_unlock_bh_journal_head(bh);
  1596. }
  1597. /*
  1598. * Drop a reference on the passed journal_head. If it fell to zero then try to
  1599. * release the journal_head from the buffer_head.
  1600. */
  1601. void journal_put_journal_head(struct journal_head *jh)
  1602. {
  1603. struct buffer_head *bh = jh2bh(jh);
  1604. jbd_lock_bh_journal_head(bh);
  1605. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1606. --jh->b_jcount;
  1607. if (!jh->b_jcount && !jh->b_transaction) {
  1608. __journal_remove_journal_head(bh);
  1609. __brelse(bh);
  1610. }
  1611. jbd_unlock_bh_journal_head(bh);
  1612. }
  1613. /*
  1614. * /proc tunables
  1615. */
  1616. #if defined(CONFIG_JBD_DEBUG)
  1617. int journal_enable_debug;
  1618. EXPORT_SYMBOL(journal_enable_debug);
  1619. #endif
  1620. #if defined(CONFIG_JBD_DEBUG) && defined(CONFIG_PROC_FS)
  1621. static struct proc_dir_entry *proc_jbd_debug;
  1622. int read_jbd_debug(char *page, char **start, off_t off,
  1623. int count, int *eof, void *data)
  1624. {
  1625. int ret;
  1626. ret = sprintf(page + off, "%d\n", journal_enable_debug);
  1627. *eof = 1;
  1628. return ret;
  1629. }
  1630. int write_jbd_debug(struct file *file, const char __user *buffer,
  1631. unsigned long count, void *data)
  1632. {
  1633. char buf[32];
  1634. if (count > ARRAY_SIZE(buf) - 1)
  1635. count = ARRAY_SIZE(buf) - 1;
  1636. if (copy_from_user(buf, buffer, count))
  1637. return -EFAULT;
  1638. buf[ARRAY_SIZE(buf) - 1] = '\0';
  1639. journal_enable_debug = simple_strtoul(buf, NULL, 10);
  1640. return count;
  1641. }
  1642. #define JBD_PROC_NAME "sys/fs/jbd-debug"
  1643. static void __init create_jbd_proc_entry(void)
  1644. {
  1645. proc_jbd_debug = create_proc_entry(JBD_PROC_NAME, 0644, NULL);
  1646. if (proc_jbd_debug) {
  1647. /* Why is this so hard? */
  1648. proc_jbd_debug->read_proc = read_jbd_debug;
  1649. proc_jbd_debug->write_proc = write_jbd_debug;
  1650. }
  1651. }
  1652. static void __exit remove_jbd_proc_entry(void)
  1653. {
  1654. if (proc_jbd_debug)
  1655. remove_proc_entry(JBD_PROC_NAME, NULL);
  1656. }
  1657. #else
  1658. #define create_jbd_proc_entry() do {} while (0)
  1659. #define remove_jbd_proc_entry() do {} while (0)
  1660. #endif
  1661. kmem_cache_t *jbd_handle_cache;
  1662. static int __init journal_init_handle_cache(void)
  1663. {
  1664. jbd_handle_cache = kmem_cache_create("journal_handle",
  1665. sizeof(handle_t),
  1666. 0, /* offset */
  1667. 0, /* flags */
  1668. NULL, /* ctor */
  1669. NULL); /* dtor */
  1670. if (jbd_handle_cache == NULL) {
  1671. printk(KERN_EMERG "JBD: failed to create handle cache\n");
  1672. return -ENOMEM;
  1673. }
  1674. return 0;
  1675. }
  1676. static void journal_destroy_handle_cache(void)
  1677. {
  1678. if (jbd_handle_cache)
  1679. kmem_cache_destroy(jbd_handle_cache);
  1680. }
  1681. /*
  1682. * Module startup and shutdown
  1683. */
  1684. static int __init journal_init_caches(void)
  1685. {
  1686. int ret;
  1687. ret = journal_init_revoke_caches();
  1688. if (ret == 0)
  1689. ret = journal_init_journal_head_cache();
  1690. if (ret == 0)
  1691. ret = journal_init_handle_cache();
  1692. return ret;
  1693. }
  1694. static void journal_destroy_caches(void)
  1695. {
  1696. journal_destroy_revoke_caches();
  1697. journal_destroy_journal_head_cache();
  1698. journal_destroy_handle_cache();
  1699. }
  1700. static int __init journal_init(void)
  1701. {
  1702. int ret;
  1703. ret = journal_init_caches();
  1704. if (ret != 0)
  1705. journal_destroy_caches();
  1706. create_jbd_proc_entry();
  1707. return ret;
  1708. }
  1709. static void __exit journal_exit(void)
  1710. {
  1711. #ifdef CONFIG_JBD_DEBUG
  1712. int n = atomic_read(&nr_journal_heads);
  1713. if (n)
  1714. printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
  1715. #endif
  1716. remove_jbd_proc_entry();
  1717. journal_destroy_caches();
  1718. }
  1719. MODULE_LICENSE("GPL");
  1720. module_init(journal_init);
  1721. module_exit(journal_exit);