journal.c 54 KB

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