super.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * super.c
  5. *
  6. * load/unload driver, mount/dismount volumes
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/utsname.h>
  31. #include <linux/init.h>
  32. #include <linux/random.h>
  33. #include <linux/statfs.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/socket.h>
  37. #include <linux/inet.h>
  38. #include <linux/parser.h>
  39. #include <linux/crc32.h>
  40. #include <linux/debugfs.h>
  41. #include <cluster/nodemanager.h>
  42. #define MLOG_MASK_PREFIX ML_SUPER
  43. #include <cluster/masklog.h>
  44. #include "ocfs2.h"
  45. /* this should be the only file to include a version 1 header */
  46. #include "ocfs1_fs_compat.h"
  47. #include "alloc.h"
  48. #include "dlmglue.h"
  49. #include "export.h"
  50. #include "extent_map.h"
  51. #include "heartbeat.h"
  52. #include "inode.h"
  53. #include "journal.h"
  54. #include "localalloc.h"
  55. #include "namei.h"
  56. #include "slot_map.h"
  57. #include "super.h"
  58. #include "sysfile.h"
  59. #include "uptodate.h"
  60. #include "ver.h"
  61. #include "vote.h"
  62. #include "buffer_head_io.h"
  63. /*
  64. * Globals
  65. */
  66. static spinlock_t ocfs2_globals_lock = SPIN_LOCK_UNLOCKED;
  67. static u32 osb_id; /* Keeps track of next available OSB Id */
  68. static kmem_cache_t *ocfs2_inode_cachep = NULL;
  69. kmem_cache_t *ocfs2_lock_cache = NULL;
  70. /* OCFS2 needs to schedule several differnt types of work which
  71. * require cluster locking, disk I/O, recovery waits, etc. Since these
  72. * types of work tend to be heavy we avoid using the kernel events
  73. * workqueue and schedule on our own. */
  74. struct workqueue_struct *ocfs2_wq = NULL;
  75. static struct dentry *ocfs2_debugfs_root = NULL;
  76. MODULE_AUTHOR("Oracle");
  77. MODULE_LICENSE("GPL");
  78. static int ocfs2_parse_options(struct super_block *sb, char *options,
  79. unsigned long *mount_opt, int is_remount);
  80. static void ocfs2_put_super(struct super_block *sb);
  81. static int ocfs2_mount_volume(struct super_block *sb);
  82. static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
  83. static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
  84. static int ocfs2_initialize_mem_caches(void);
  85. static void ocfs2_free_mem_caches(void);
  86. static void ocfs2_delete_osb(struct ocfs2_super *osb);
  87. static int ocfs2_statfs(struct super_block *sb, struct kstatfs *buf);
  88. static int ocfs2_sync_fs(struct super_block *sb, int wait);
  89. static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
  90. static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
  91. static int ocfs2_release_system_inodes(struct ocfs2_super *osb);
  92. static int ocfs2_fill_local_node_info(struct ocfs2_super *osb);
  93. static int ocfs2_check_volume(struct ocfs2_super *osb);
  94. static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  95. struct buffer_head *bh,
  96. u32 sectsize);
  97. static int ocfs2_initialize_super(struct super_block *sb,
  98. struct buffer_head *bh,
  99. int sector_size);
  100. static int ocfs2_get_sector(struct super_block *sb,
  101. struct buffer_head **bh,
  102. int block,
  103. int sect_size);
  104. static void ocfs2_write_super(struct super_block *sb);
  105. static struct inode *ocfs2_alloc_inode(struct super_block *sb);
  106. static void ocfs2_destroy_inode(struct inode *inode);
  107. static unsigned long long ocfs2_max_file_offset(unsigned int blockshift);
  108. static struct super_operations ocfs2_sops = {
  109. .statfs = ocfs2_statfs,
  110. .alloc_inode = ocfs2_alloc_inode,
  111. .destroy_inode = ocfs2_destroy_inode,
  112. .drop_inode = ocfs2_drop_inode,
  113. .clear_inode = ocfs2_clear_inode,
  114. .delete_inode = ocfs2_delete_inode,
  115. .sync_fs = ocfs2_sync_fs,
  116. .write_super = ocfs2_write_super,
  117. .put_super = ocfs2_put_super,
  118. .remount_fs = ocfs2_remount,
  119. };
  120. enum {
  121. Opt_barrier,
  122. Opt_err_panic,
  123. Opt_err_ro,
  124. Opt_intr,
  125. Opt_nointr,
  126. Opt_hb_none,
  127. Opt_hb_local,
  128. Opt_data_ordered,
  129. Opt_data_writeback,
  130. Opt_err,
  131. };
  132. static match_table_t tokens = {
  133. {Opt_barrier, "barrier=%u"},
  134. {Opt_err_panic, "errors=panic"},
  135. {Opt_err_ro, "errors=remount-ro"},
  136. {Opt_intr, "intr"},
  137. {Opt_nointr, "nointr"},
  138. {Opt_hb_none, OCFS2_HB_NONE},
  139. {Opt_hb_local, OCFS2_HB_LOCAL},
  140. {Opt_data_ordered, "data=ordered"},
  141. {Opt_data_writeback, "data=writeback"},
  142. {Opt_err, NULL}
  143. };
  144. /*
  145. * write_super and sync_fs ripped right out of ext3.
  146. */
  147. static void ocfs2_write_super(struct super_block *sb)
  148. {
  149. if (mutex_trylock(&sb->s_lock) != 0)
  150. BUG();
  151. sb->s_dirt = 0;
  152. }
  153. static int ocfs2_sync_fs(struct super_block *sb, int wait)
  154. {
  155. int status = 0;
  156. tid_t target;
  157. struct ocfs2_super *osb = OCFS2_SB(sb);
  158. sb->s_dirt = 0;
  159. if (ocfs2_is_hard_readonly(osb))
  160. return -EROFS;
  161. if (wait) {
  162. status = ocfs2_flush_truncate_log(osb);
  163. if (status < 0)
  164. mlog_errno(status);
  165. } else {
  166. ocfs2_schedule_truncate_log_flush(osb, 0);
  167. }
  168. if (journal_start_commit(OCFS2_SB(sb)->journal->j_journal, &target)) {
  169. if (wait)
  170. log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
  171. target);
  172. }
  173. return 0;
  174. }
  175. static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
  176. {
  177. struct inode *new = NULL;
  178. int status = 0;
  179. int i;
  180. mlog_entry_void();
  181. new = ocfs2_iget(osb, osb->root_blkno);
  182. if (IS_ERR(new)) {
  183. status = PTR_ERR(new);
  184. mlog_errno(status);
  185. goto bail;
  186. }
  187. osb->root_inode = new;
  188. new = ocfs2_iget(osb, osb->system_dir_blkno);
  189. if (IS_ERR(new)) {
  190. status = PTR_ERR(new);
  191. mlog_errno(status);
  192. goto bail;
  193. }
  194. osb->sys_root_inode = new;
  195. for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
  196. i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
  197. new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
  198. if (!new) {
  199. ocfs2_release_system_inodes(osb);
  200. status = -EINVAL;
  201. mlog_errno(status);
  202. /* FIXME: Should ERROR_RO_FS */
  203. mlog(ML_ERROR, "Unable to load system inode %d, "
  204. "possibly corrupt fs?", i);
  205. goto bail;
  206. }
  207. // the array now has one ref, so drop this one
  208. iput(new);
  209. }
  210. bail:
  211. mlog_exit(status);
  212. return status;
  213. }
  214. static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
  215. {
  216. struct inode *new = NULL;
  217. int status = 0;
  218. int i;
  219. mlog_entry_void();
  220. for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
  221. i < NUM_SYSTEM_INODES;
  222. i++) {
  223. new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
  224. if (!new) {
  225. ocfs2_release_system_inodes(osb);
  226. status = -EINVAL;
  227. mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
  228. status, i, osb->slot_num);
  229. goto bail;
  230. }
  231. /* the array now has one ref, so drop this one */
  232. iput(new);
  233. }
  234. bail:
  235. mlog_exit(status);
  236. return status;
  237. }
  238. static int ocfs2_release_system_inodes(struct ocfs2_super *osb)
  239. {
  240. int status = 0, i;
  241. struct inode *inode;
  242. mlog_entry_void();
  243. for (i = 0; i < NUM_SYSTEM_INODES; i++) {
  244. inode = osb->system_inodes[i];
  245. if (inode) {
  246. iput(inode);
  247. osb->system_inodes[i] = NULL;
  248. }
  249. }
  250. inode = osb->sys_root_inode;
  251. if (inode) {
  252. iput(inode);
  253. osb->sys_root_inode = NULL;
  254. }
  255. inode = osb->root_inode;
  256. if (inode) {
  257. iput(inode);
  258. osb->root_inode = NULL;
  259. }
  260. mlog_exit(status);
  261. return status;
  262. }
  263. /* We're allocating fs objects, use GFP_NOFS */
  264. static struct inode *ocfs2_alloc_inode(struct super_block *sb)
  265. {
  266. struct ocfs2_inode_info *oi;
  267. oi = kmem_cache_alloc(ocfs2_inode_cachep, SLAB_NOFS);
  268. if (!oi)
  269. return NULL;
  270. return &oi->vfs_inode;
  271. }
  272. static void ocfs2_destroy_inode(struct inode *inode)
  273. {
  274. kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
  275. }
  276. /* From xfs_super.c:xfs_max_file_offset
  277. * Copyright (c) 2000-2004 Silicon Graphics, Inc.
  278. */
  279. static unsigned long long ocfs2_max_file_offset(unsigned int blockshift)
  280. {
  281. unsigned int pagefactor = 1;
  282. unsigned int bitshift = BITS_PER_LONG - 1;
  283. /* Figure out maximum filesize, on Linux this can depend on
  284. * the filesystem blocksize (on 32 bit platforms).
  285. * __block_prepare_write does this in an [unsigned] long...
  286. * page->index << (PAGE_CACHE_SHIFT - bbits)
  287. * So, for page sized blocks (4K on 32 bit platforms),
  288. * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
  289. * (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
  290. * but for smaller blocksizes it is less (bbits = log2 bsize).
  291. * Note1: get_block_t takes a long (implicit cast from above)
  292. * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
  293. * can optionally convert the [unsigned] long from above into
  294. * an [unsigned] long long.
  295. */
  296. #if BITS_PER_LONG == 32
  297. # if defined(CONFIG_LBD)
  298. BUG_ON(sizeof(sector_t) != 8);
  299. pagefactor = PAGE_CACHE_SIZE;
  300. bitshift = BITS_PER_LONG;
  301. # else
  302. pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
  303. # endif
  304. #endif
  305. return (((unsigned long long)pagefactor) << bitshift) - 1;
  306. }
  307. static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
  308. {
  309. int incompat_features;
  310. int ret = 0;
  311. unsigned long parsed_options;
  312. struct ocfs2_super *osb = OCFS2_SB(sb);
  313. if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
  314. ret = -EINVAL;
  315. goto out;
  316. }
  317. if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
  318. (parsed_options & OCFS2_MOUNT_HB_LOCAL)) {
  319. ret = -EINVAL;
  320. mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
  321. goto out;
  322. }
  323. if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
  324. (parsed_options & OCFS2_MOUNT_DATA_WRITEBACK)) {
  325. ret = -EINVAL;
  326. mlog(ML_ERROR, "Cannot change data mode on remount\n");
  327. goto out;
  328. }
  329. /* We're going to/from readonly mode. */
  330. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
  331. /* Lock here so the check of HARD_RO and the potential
  332. * setting of SOFT_RO is atomic. */
  333. spin_lock(&osb->osb_lock);
  334. if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
  335. mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
  336. ret = -EROFS;
  337. goto unlock_osb;
  338. }
  339. if (*flags & MS_RDONLY) {
  340. mlog(0, "Going to ro mode.\n");
  341. sb->s_flags |= MS_RDONLY;
  342. osb->osb_flags |= OCFS2_OSB_SOFT_RO;
  343. } else {
  344. mlog(0, "Making ro filesystem writeable.\n");
  345. if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
  346. mlog(ML_ERROR, "Cannot remount RDWR "
  347. "filesystem due to previous errors.\n");
  348. ret = -EROFS;
  349. goto unlock_osb;
  350. }
  351. incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
  352. if (incompat_features) {
  353. mlog(ML_ERROR, "Cannot remount RDWR because "
  354. "of unsupported optional features "
  355. "(%x).\n", incompat_features);
  356. ret = -EINVAL;
  357. goto unlock_osb;
  358. }
  359. sb->s_flags &= ~MS_RDONLY;
  360. osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
  361. }
  362. unlock_osb:
  363. spin_unlock(&osb->osb_lock);
  364. }
  365. if (!ret) {
  366. if (!ocfs2_is_hard_readonly(osb))
  367. ocfs2_set_journal_params(osb);
  368. /* Only save off the new mount options in case of a successful
  369. * remount. */
  370. osb->s_mount_opt = parsed_options;
  371. }
  372. out:
  373. return ret;
  374. }
  375. static int ocfs2_sb_probe(struct super_block *sb,
  376. struct buffer_head **bh,
  377. int *sector_size)
  378. {
  379. int status = 0, tmpstat;
  380. struct ocfs1_vol_disk_hdr *hdr;
  381. struct ocfs2_dinode *di;
  382. int blksize;
  383. *bh = NULL;
  384. /* may be > 512 */
  385. *sector_size = bdev_hardsect_size(sb->s_bdev);
  386. if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
  387. mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
  388. *sector_size, OCFS2_MAX_BLOCKSIZE);
  389. status = -EINVAL;
  390. goto bail;
  391. }
  392. /* Can this really happen? */
  393. if (*sector_size < OCFS2_MIN_BLOCKSIZE)
  394. *sector_size = OCFS2_MIN_BLOCKSIZE;
  395. /* check block zero for old format */
  396. status = ocfs2_get_sector(sb, bh, 0, *sector_size);
  397. if (status < 0) {
  398. mlog_errno(status);
  399. goto bail;
  400. }
  401. hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
  402. if (hdr->major_version == OCFS1_MAJOR_VERSION) {
  403. mlog(ML_ERROR, "incompatible version: %u.%u\n",
  404. hdr->major_version, hdr->minor_version);
  405. status = -EINVAL;
  406. }
  407. if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
  408. strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
  409. mlog(ML_ERROR, "incompatible volume signature: %8s\n",
  410. hdr->signature);
  411. status = -EINVAL;
  412. }
  413. brelse(*bh);
  414. *bh = NULL;
  415. if (status < 0) {
  416. mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
  417. "upgraded before mounting with ocfs v2\n");
  418. goto bail;
  419. }
  420. /*
  421. * Now check at magic offset for 512, 1024, 2048, 4096
  422. * blocksizes. 4096 is the maximum blocksize because it is
  423. * the minimum clustersize.
  424. */
  425. status = -EINVAL;
  426. for (blksize = *sector_size;
  427. blksize <= OCFS2_MAX_BLOCKSIZE;
  428. blksize <<= 1) {
  429. tmpstat = ocfs2_get_sector(sb, bh,
  430. OCFS2_SUPER_BLOCK_BLKNO,
  431. blksize);
  432. if (tmpstat < 0) {
  433. status = tmpstat;
  434. mlog_errno(status);
  435. goto bail;
  436. }
  437. di = (struct ocfs2_dinode *) (*bh)->b_data;
  438. status = ocfs2_verify_volume(di, *bh, blksize);
  439. if (status >= 0)
  440. goto bail;
  441. brelse(*bh);
  442. *bh = NULL;
  443. if (status != -EAGAIN)
  444. break;
  445. }
  446. bail:
  447. return status;
  448. }
  449. static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
  450. {
  451. struct dentry *root;
  452. int status, sector_size;
  453. unsigned long parsed_opt;
  454. struct inode *inode = NULL;
  455. struct ocfs2_super *osb = NULL;
  456. struct buffer_head *bh = NULL;
  457. mlog_entry("%p, %p, %i", sb, data, silent);
  458. /* for now we only have one cluster/node, make sure we see it
  459. * in the heartbeat universe */
  460. if (!o2hb_check_local_node_heartbeating()) {
  461. status = -EINVAL;
  462. goto read_super_error;
  463. }
  464. /* probe for superblock */
  465. status = ocfs2_sb_probe(sb, &bh, &sector_size);
  466. if (status < 0) {
  467. mlog(ML_ERROR, "superblock probe failed!\n");
  468. goto read_super_error;
  469. }
  470. status = ocfs2_initialize_super(sb, bh, sector_size);
  471. osb = OCFS2_SB(sb);
  472. if (status < 0) {
  473. mlog_errno(status);
  474. goto read_super_error;
  475. }
  476. brelse(bh);
  477. bh = NULL;
  478. if (!ocfs2_parse_options(sb, data, &parsed_opt, 0)) {
  479. status = -EINVAL;
  480. goto read_super_error;
  481. }
  482. osb->s_mount_opt = parsed_opt;
  483. sb->s_magic = OCFS2_SUPER_MAGIC;
  484. /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
  485. * heartbeat=none */
  486. if (bdev_read_only(sb->s_bdev)) {
  487. if (!(sb->s_flags & MS_RDONLY)) {
  488. status = -EACCES;
  489. mlog(ML_ERROR, "Readonly device detected but readonly "
  490. "mount was not specified.\n");
  491. goto read_super_error;
  492. }
  493. /* You should not be able to start a local heartbeat
  494. * on a readonly device. */
  495. if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
  496. status = -EROFS;
  497. mlog(ML_ERROR, "Local heartbeat specified on readonly "
  498. "device.\n");
  499. goto read_super_error;
  500. }
  501. status = ocfs2_check_journals_nolocks(osb);
  502. if (status < 0) {
  503. if (status == -EROFS)
  504. mlog(ML_ERROR, "Recovery required on readonly "
  505. "file system, but write access is "
  506. "unavailable.\n");
  507. else
  508. mlog_errno(status);
  509. goto read_super_error;
  510. }
  511. ocfs2_set_ro_flag(osb, 1);
  512. printk(KERN_NOTICE "Readonly device detected. No cluster "
  513. "services will be utilized for this mount. Recovery "
  514. "will be skipped.\n");
  515. }
  516. if (!ocfs2_is_hard_readonly(osb)) {
  517. /* If this isn't a hard readonly mount, then we need
  518. * to make sure that heartbeat is in a valid state,
  519. * and that we mark ourselves soft readonly is -oro
  520. * was specified. */
  521. if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
  522. mlog(ML_ERROR, "No heartbeat for device (%s)\n",
  523. sb->s_id);
  524. status = -EINVAL;
  525. goto read_super_error;
  526. }
  527. if (sb->s_flags & MS_RDONLY)
  528. ocfs2_set_ro_flag(osb, 0);
  529. }
  530. osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
  531. ocfs2_debugfs_root);
  532. if (!osb->osb_debug_root) {
  533. status = -EINVAL;
  534. mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
  535. goto read_super_error;
  536. }
  537. status = ocfs2_mount_volume(sb);
  538. if (osb->root_inode)
  539. inode = igrab(osb->root_inode);
  540. if (status < 0)
  541. goto read_super_error;
  542. if (!inode) {
  543. status = -EIO;
  544. mlog_errno(status);
  545. goto read_super_error;
  546. }
  547. root = d_alloc_root(inode);
  548. if (!root) {
  549. status = -ENOMEM;
  550. mlog_errno(status);
  551. goto read_super_error;
  552. }
  553. sb->s_root = root;
  554. ocfs2_complete_mount_recovery(osb);
  555. printk("ocfs2: Mounting device (%u,%u) on (node %d, slot %d) with %s "
  556. "data mode.\n",
  557. MAJOR(sb->s_dev), MINOR(sb->s_dev), osb->node_num,
  558. osb->slot_num,
  559. osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
  560. "ordered");
  561. atomic_set(&osb->vol_state, VOLUME_MOUNTED);
  562. wake_up(&osb->osb_mount_event);
  563. mlog_exit(status);
  564. return status;
  565. read_super_error:
  566. if (bh != NULL)
  567. brelse(bh);
  568. if (inode)
  569. iput(inode);
  570. if (osb) {
  571. atomic_set(&osb->vol_state, VOLUME_DISABLED);
  572. wake_up(&osb->osb_mount_event);
  573. ocfs2_dismount_volume(sb, 1);
  574. }
  575. mlog_exit(status);
  576. return status;
  577. }
  578. static struct super_block *ocfs2_get_sb(struct file_system_type *fs_type,
  579. int flags,
  580. const char *dev_name,
  581. void *data)
  582. {
  583. return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super);
  584. }
  585. static struct file_system_type ocfs2_fs_type = {
  586. .owner = THIS_MODULE,
  587. .name = "ocfs2",
  588. .get_sb = ocfs2_get_sb, /* is this called when we mount
  589. * the fs? */
  590. .kill_sb = kill_block_super, /* set to the generic one
  591. * right now, but do we
  592. * need to change that? */
  593. .fs_flags = FS_REQUIRES_DEV,
  594. .next = NULL
  595. };
  596. static int ocfs2_parse_options(struct super_block *sb,
  597. char *options,
  598. unsigned long *mount_opt,
  599. int is_remount)
  600. {
  601. int status;
  602. char *p;
  603. mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
  604. options ? options : "(none)");
  605. *mount_opt = 0;
  606. if (!options) {
  607. status = 1;
  608. goto bail;
  609. }
  610. while ((p = strsep(&options, ",")) != NULL) {
  611. int token, option;
  612. substring_t args[MAX_OPT_ARGS];
  613. if (!*p)
  614. continue;
  615. token = match_token(p, tokens, args);
  616. switch (token) {
  617. case Opt_hb_local:
  618. *mount_opt |= OCFS2_MOUNT_HB_LOCAL;
  619. break;
  620. case Opt_hb_none:
  621. *mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
  622. break;
  623. case Opt_barrier:
  624. if (match_int(&args[0], &option)) {
  625. status = 0;
  626. goto bail;
  627. }
  628. if (option)
  629. *mount_opt |= OCFS2_MOUNT_BARRIER;
  630. else
  631. *mount_opt &= ~OCFS2_MOUNT_BARRIER;
  632. break;
  633. case Opt_intr:
  634. *mount_opt &= ~OCFS2_MOUNT_NOINTR;
  635. break;
  636. case Opt_nointr:
  637. *mount_opt |= OCFS2_MOUNT_NOINTR;
  638. break;
  639. case Opt_err_panic:
  640. *mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
  641. break;
  642. case Opt_err_ro:
  643. *mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
  644. break;
  645. case Opt_data_ordered:
  646. *mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
  647. break;
  648. case Opt_data_writeback:
  649. *mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
  650. break;
  651. default:
  652. mlog(ML_ERROR,
  653. "Unrecognized mount option \"%s\" "
  654. "or missing value\n", p);
  655. status = 0;
  656. goto bail;
  657. }
  658. }
  659. status = 1;
  660. bail:
  661. mlog_exit(status);
  662. return status;
  663. }
  664. static int __init ocfs2_init(void)
  665. {
  666. int status;
  667. mlog_entry_void();
  668. ocfs2_print_version();
  669. if (init_ocfs2_extent_maps())
  670. return -ENOMEM;
  671. status = init_ocfs2_uptodate_cache();
  672. if (status < 0) {
  673. mlog_errno(status);
  674. goto leave;
  675. }
  676. status = ocfs2_initialize_mem_caches();
  677. if (status < 0) {
  678. mlog_errno(status);
  679. goto leave;
  680. }
  681. ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
  682. if (!ocfs2_wq) {
  683. status = -ENOMEM;
  684. goto leave;
  685. }
  686. spin_lock(&ocfs2_globals_lock);
  687. osb_id = 0;
  688. spin_unlock(&ocfs2_globals_lock);
  689. ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
  690. if (!ocfs2_debugfs_root) {
  691. status = -EFAULT;
  692. mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
  693. }
  694. leave:
  695. if (status < 0) {
  696. ocfs2_free_mem_caches();
  697. exit_ocfs2_uptodate_cache();
  698. exit_ocfs2_extent_maps();
  699. }
  700. mlog_exit(status);
  701. if (status >= 0) {
  702. return register_filesystem(&ocfs2_fs_type);
  703. } else
  704. return -1;
  705. }
  706. static void __exit ocfs2_exit(void)
  707. {
  708. mlog_entry_void();
  709. if (ocfs2_wq) {
  710. flush_workqueue(ocfs2_wq);
  711. destroy_workqueue(ocfs2_wq);
  712. }
  713. debugfs_remove(ocfs2_debugfs_root);
  714. ocfs2_free_mem_caches();
  715. unregister_filesystem(&ocfs2_fs_type);
  716. exit_ocfs2_extent_maps();
  717. exit_ocfs2_uptodate_cache();
  718. mlog_exit_void();
  719. }
  720. static void ocfs2_put_super(struct super_block *sb)
  721. {
  722. mlog_entry("(0x%p)\n", sb);
  723. ocfs2_sync_blockdev(sb);
  724. ocfs2_dismount_volume(sb, 0);
  725. mlog_exit_void();
  726. }
  727. static int ocfs2_statfs(struct super_block *sb, struct kstatfs *buf)
  728. {
  729. struct ocfs2_super *osb;
  730. u32 numbits, freebits;
  731. int status;
  732. struct ocfs2_dinode *bm_lock;
  733. struct buffer_head *bh = NULL;
  734. struct inode *inode = NULL;
  735. mlog_entry("(%p, %p)\n", sb, buf);
  736. osb = OCFS2_SB(sb);
  737. inode = ocfs2_get_system_file_inode(osb,
  738. GLOBAL_BITMAP_SYSTEM_INODE,
  739. OCFS2_INVALID_SLOT);
  740. if (!inode) {
  741. mlog(ML_ERROR, "failed to get bitmap inode\n");
  742. status = -EIO;
  743. goto bail;
  744. }
  745. status = ocfs2_meta_lock(inode, NULL, &bh, 0);
  746. if (status < 0) {
  747. mlog_errno(status);
  748. goto bail;
  749. }
  750. bm_lock = (struct ocfs2_dinode *) bh->b_data;
  751. numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
  752. freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
  753. buf->f_type = OCFS2_SUPER_MAGIC;
  754. buf->f_bsize = sb->s_blocksize;
  755. buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
  756. buf->f_blocks = ((sector_t) numbits) *
  757. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  758. buf->f_bfree = ((sector_t) freebits) *
  759. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  760. buf->f_bavail = buf->f_bfree;
  761. buf->f_files = numbits;
  762. buf->f_ffree = freebits;
  763. brelse(bh);
  764. ocfs2_meta_unlock(inode, 0);
  765. status = 0;
  766. bail:
  767. if (inode)
  768. iput(inode);
  769. mlog_exit(status);
  770. return status;
  771. }
  772. static void ocfs2_inode_init_once(void *data,
  773. kmem_cache_t *cachep,
  774. unsigned long flags)
  775. {
  776. struct ocfs2_inode_info *oi = data;
  777. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  778. SLAB_CTOR_CONSTRUCTOR) {
  779. oi->ip_flags = 0;
  780. oi->ip_open_count = 0;
  781. spin_lock_init(&oi->ip_lock);
  782. ocfs2_extent_map_init(&oi->vfs_inode);
  783. INIT_LIST_HEAD(&oi->ip_handle_list);
  784. INIT_LIST_HEAD(&oi->ip_io_markers);
  785. oi->ip_handle = NULL;
  786. oi->ip_created_trans = 0;
  787. oi->ip_last_trans = 0;
  788. oi->ip_dir_start_lookup = 0;
  789. init_rwsem(&oi->ip_alloc_sem);
  790. mutex_init(&oi->ip_io_mutex);
  791. oi->ip_blkno = 0ULL;
  792. oi->ip_clusters = 0;
  793. ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
  794. ocfs2_lock_res_init_once(&oi->ip_meta_lockres);
  795. ocfs2_lock_res_init_once(&oi->ip_data_lockres);
  796. ocfs2_metadata_cache_init(&oi->vfs_inode);
  797. inode_init_once(&oi->vfs_inode);
  798. }
  799. }
  800. static int ocfs2_initialize_mem_caches(void)
  801. {
  802. ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
  803. sizeof(struct ocfs2_inode_info),
  804. 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
  805. ocfs2_inode_init_once, NULL);
  806. if (!ocfs2_inode_cachep)
  807. return -ENOMEM;
  808. ocfs2_lock_cache = kmem_cache_create("ocfs2_lock",
  809. sizeof(struct ocfs2_journal_lock),
  810. 0,
  811. SLAB_NO_REAP|SLAB_HWCACHE_ALIGN,
  812. NULL, NULL);
  813. if (!ocfs2_lock_cache)
  814. return -ENOMEM;
  815. return 0;
  816. }
  817. static void ocfs2_free_mem_caches(void)
  818. {
  819. if (ocfs2_inode_cachep)
  820. kmem_cache_destroy(ocfs2_inode_cachep);
  821. if (ocfs2_lock_cache)
  822. kmem_cache_destroy(ocfs2_lock_cache);
  823. ocfs2_inode_cachep = NULL;
  824. ocfs2_lock_cache = NULL;
  825. }
  826. static int ocfs2_get_sector(struct super_block *sb,
  827. struct buffer_head **bh,
  828. int block,
  829. int sect_size)
  830. {
  831. if (!sb_set_blocksize(sb, sect_size)) {
  832. mlog(ML_ERROR, "unable to set blocksize\n");
  833. return -EIO;
  834. }
  835. *bh = sb_getblk(sb, block);
  836. if (!*bh) {
  837. mlog_errno(-EIO);
  838. return -EIO;
  839. }
  840. lock_buffer(*bh);
  841. if (!buffer_dirty(*bh))
  842. clear_buffer_uptodate(*bh);
  843. unlock_buffer(*bh);
  844. ll_rw_block(READ, 1, bh);
  845. wait_on_buffer(*bh);
  846. return 0;
  847. }
  848. /* ocfs2 1.0 only allows one cluster and node identity per kernel image. */
  849. static int ocfs2_fill_local_node_info(struct ocfs2_super *osb)
  850. {
  851. int status;
  852. /* XXX hold a ref on the node while mounte? easy enough, if
  853. * desirable. */
  854. osb->node_num = o2nm_this_node();
  855. if (osb->node_num == O2NM_MAX_NODES) {
  856. mlog(ML_ERROR, "could not find this host's node number\n");
  857. status = -ENOENT;
  858. goto bail;
  859. }
  860. mlog(ML_NOTICE, "I am node %d\n", osb->node_num);
  861. status = 0;
  862. bail:
  863. return status;
  864. }
  865. static int ocfs2_mount_volume(struct super_block *sb)
  866. {
  867. int status = 0;
  868. int unlock_super = 0;
  869. struct ocfs2_super *osb = OCFS2_SB(sb);
  870. mlog_entry_void();
  871. if (ocfs2_is_hard_readonly(osb))
  872. goto leave;
  873. status = ocfs2_fill_local_node_info(osb);
  874. if (status < 0) {
  875. mlog_errno(status);
  876. goto leave;
  877. }
  878. status = ocfs2_register_hb_callbacks(osb);
  879. if (status < 0) {
  880. mlog_errno(status);
  881. goto leave;
  882. }
  883. status = ocfs2_dlm_init(osb);
  884. if (status < 0) {
  885. mlog_errno(status);
  886. goto leave;
  887. }
  888. /* requires vote_thread to be running. */
  889. status = ocfs2_register_net_handlers(osb);
  890. if (status < 0) {
  891. mlog_errno(status);
  892. goto leave;
  893. }
  894. status = ocfs2_super_lock(osb, 1);
  895. if (status < 0) {
  896. mlog_errno(status);
  897. goto leave;
  898. }
  899. unlock_super = 1;
  900. /* This will load up the node map and add ourselves to it. */
  901. status = ocfs2_find_slot(osb);
  902. if (status < 0) {
  903. mlog_errno(status);
  904. goto leave;
  905. }
  906. ocfs2_populate_mounted_map(osb);
  907. /* load all node-local system inodes */
  908. status = ocfs2_init_local_system_inodes(osb);
  909. if (status < 0) {
  910. mlog_errno(status);
  911. goto leave;
  912. }
  913. status = ocfs2_check_volume(osb);
  914. if (status < 0) {
  915. mlog_errno(status);
  916. goto leave;
  917. }
  918. status = ocfs2_truncate_log_init(osb);
  919. if (status < 0) {
  920. mlog_errno(status);
  921. goto leave;
  922. }
  923. /* This should be sent *after* we recovered our journal as it
  924. * will cause other nodes to unmark us as needing
  925. * recovery. However, we need to send it *before* dropping the
  926. * super block lock as otherwise their recovery threads might
  927. * try to clean us up while we're live! */
  928. status = ocfs2_request_mount_vote(osb);
  929. if (status < 0)
  930. mlog_errno(status);
  931. leave:
  932. if (unlock_super)
  933. ocfs2_super_unlock(osb, 1);
  934. mlog_exit(status);
  935. return status;
  936. }
  937. /* we can't grab the goofy sem lock from inside wait_event, so we use
  938. * memory barriers to make sure that we'll see the null task before
  939. * being woken up */
  940. static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
  941. {
  942. mb();
  943. return osb->recovery_thread_task != NULL;
  944. }
  945. static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
  946. {
  947. int tmp;
  948. struct ocfs2_super *osb = NULL;
  949. mlog_entry("(0x%p)\n", sb);
  950. BUG_ON(!sb);
  951. osb = OCFS2_SB(sb);
  952. BUG_ON(!osb);
  953. ocfs2_shutdown_local_alloc(osb);
  954. ocfs2_truncate_log_shutdown(osb);
  955. /* disable any new recovery threads and wait for any currently
  956. * running ones to exit. Do this before setting the vol_state. */
  957. mutex_lock(&osb->recovery_lock);
  958. osb->disable_recovery = 1;
  959. mutex_unlock(&osb->recovery_lock);
  960. wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
  961. /* At this point, we know that no more recovery threads can be
  962. * launched, so wait for any recovery completion work to
  963. * complete. */
  964. flush_workqueue(ocfs2_wq);
  965. ocfs2_journal_shutdown(osb);
  966. ocfs2_sync_blockdev(sb);
  967. /* No dlm means we've failed during mount, so skip all the
  968. * steps which depended on that to complete. */
  969. if (osb->dlm) {
  970. tmp = ocfs2_super_lock(osb, 1);
  971. if (tmp < 0) {
  972. mlog_errno(tmp);
  973. return;
  974. }
  975. tmp = ocfs2_request_umount_vote(osb);
  976. if (tmp < 0)
  977. mlog_errno(tmp);
  978. if (osb->slot_num != OCFS2_INVALID_SLOT)
  979. ocfs2_put_slot(osb);
  980. ocfs2_super_unlock(osb, 1);
  981. }
  982. ocfs2_release_system_inodes(osb);
  983. if (osb->dlm) {
  984. ocfs2_unregister_net_handlers(osb);
  985. ocfs2_dlm_shutdown(osb);
  986. }
  987. ocfs2_clear_hb_callbacks(osb);
  988. debugfs_remove(osb->osb_debug_root);
  989. if (!mnt_err)
  990. ocfs2_stop_heartbeat(osb);
  991. atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
  992. printk("ocfs2: Unmounting device (%u,%u) on (node %d)\n",
  993. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev), osb->node_num);
  994. ocfs2_delete_osb(osb);
  995. kfree(osb);
  996. sb->s_dev = 0;
  997. sb->s_fs_info = NULL;
  998. }
  999. static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
  1000. unsigned uuid_bytes)
  1001. {
  1002. int i, ret;
  1003. char *ptr;
  1004. BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
  1005. osb->uuid_str = kcalloc(1, OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
  1006. if (osb->uuid_str == NULL)
  1007. return -ENOMEM;
  1008. memcpy(osb->uuid, uuid, OCFS2_VOL_UUID_LEN);
  1009. for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
  1010. /* print with null */
  1011. ret = snprintf(ptr, 3, "%02X", uuid[i]);
  1012. if (ret != 2) /* drop super cleans up */
  1013. return -EINVAL;
  1014. /* then only advance past the last char */
  1015. ptr += 2;
  1016. }
  1017. return 0;
  1018. }
  1019. static int ocfs2_initialize_super(struct super_block *sb,
  1020. struct buffer_head *bh,
  1021. int sector_size)
  1022. {
  1023. int status = 0;
  1024. int i;
  1025. struct ocfs2_dinode *di = NULL;
  1026. struct inode *inode = NULL;
  1027. struct buffer_head *bitmap_bh = NULL;
  1028. struct ocfs2_journal *journal;
  1029. __le32 uuid_net_key;
  1030. struct ocfs2_super *osb;
  1031. mlog_entry_void();
  1032. osb = kcalloc(1, sizeof(struct ocfs2_super), GFP_KERNEL);
  1033. if (!osb) {
  1034. status = -ENOMEM;
  1035. mlog_errno(status);
  1036. goto bail;
  1037. }
  1038. sb->s_fs_info = osb;
  1039. sb->s_op = &ocfs2_sops;
  1040. sb->s_export_op = &ocfs2_export_ops;
  1041. sb->s_flags |= MS_NOATIME;
  1042. /* this is needed to support O_LARGEFILE */
  1043. sb->s_maxbytes = ocfs2_max_file_offset(sb->s_blocksize_bits);
  1044. osb->sb = sb;
  1045. /* Save off for ocfs2_rw_direct */
  1046. osb->s_sectsize_bits = blksize_bits(sector_size);
  1047. BUG_ON(!osb->s_sectsize_bits);
  1048. osb->net_response_ids = 0;
  1049. spin_lock_init(&osb->net_response_lock);
  1050. INIT_LIST_HEAD(&osb->net_response_list);
  1051. INIT_LIST_HEAD(&osb->osb_net_handlers);
  1052. init_waitqueue_head(&osb->recovery_event);
  1053. spin_lock_init(&osb->vote_task_lock);
  1054. init_waitqueue_head(&osb->vote_event);
  1055. osb->vote_work_sequence = 0;
  1056. osb->vote_wake_sequence = 0;
  1057. INIT_LIST_HEAD(&osb->blocked_lock_list);
  1058. osb->blocked_lock_count = 0;
  1059. INIT_LIST_HEAD(&osb->vote_list);
  1060. spin_lock_init(&osb->osb_lock);
  1061. atomic_set(&osb->alloc_stats.moves, 0);
  1062. atomic_set(&osb->alloc_stats.local_data, 0);
  1063. atomic_set(&osb->alloc_stats.bitmap_data, 0);
  1064. atomic_set(&osb->alloc_stats.bg_allocs, 0);
  1065. atomic_set(&osb->alloc_stats.bg_extends, 0);
  1066. ocfs2_init_node_maps(osb);
  1067. snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
  1068. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1069. mutex_init(&osb->recovery_lock);
  1070. osb->disable_recovery = 0;
  1071. osb->recovery_thread_task = NULL;
  1072. init_waitqueue_head(&osb->checkpoint_event);
  1073. atomic_set(&osb->needs_checkpoint, 0);
  1074. osb->node_num = O2NM_INVALID_NODE_NUM;
  1075. osb->slot_num = OCFS2_INVALID_SLOT;
  1076. osb->local_alloc_state = OCFS2_LA_UNUSED;
  1077. osb->local_alloc_bh = NULL;
  1078. ocfs2_setup_hb_callbacks(osb);
  1079. init_waitqueue_head(&osb->osb_mount_event);
  1080. osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
  1081. if (!osb->vol_label) {
  1082. mlog(ML_ERROR, "unable to alloc vol label\n");
  1083. status = -ENOMEM;
  1084. goto bail;
  1085. }
  1086. osb->uuid = kmalloc(OCFS2_VOL_UUID_LEN, GFP_KERNEL);
  1087. if (!osb->uuid) {
  1088. mlog(ML_ERROR, "unable to alloc uuid\n");
  1089. status = -ENOMEM;
  1090. goto bail;
  1091. }
  1092. di = (struct ocfs2_dinode *)bh->b_data;
  1093. osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
  1094. if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
  1095. mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
  1096. osb->max_slots);
  1097. status = -EINVAL;
  1098. goto bail;
  1099. }
  1100. mlog(ML_NOTICE, "max_slots for this device: %u\n", osb->max_slots);
  1101. init_waitqueue_head(&osb->osb_wipe_event);
  1102. osb->osb_orphan_wipes = kcalloc(osb->max_slots,
  1103. sizeof(*osb->osb_orphan_wipes),
  1104. GFP_KERNEL);
  1105. if (!osb->osb_orphan_wipes) {
  1106. status = -ENOMEM;
  1107. mlog_errno(status);
  1108. goto bail;
  1109. }
  1110. osb->s_feature_compat =
  1111. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
  1112. osb->s_feature_ro_compat =
  1113. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
  1114. osb->s_feature_incompat =
  1115. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
  1116. if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
  1117. mlog(ML_ERROR, "couldn't mount because of unsupported "
  1118. "optional features (%x).\n", i);
  1119. status = -EINVAL;
  1120. goto bail;
  1121. }
  1122. if (!(osb->sb->s_flags & MS_RDONLY) &&
  1123. (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
  1124. mlog(ML_ERROR, "couldn't mount RDWR because of "
  1125. "unsupported optional features (%x).\n", i);
  1126. status = -EINVAL;
  1127. goto bail;
  1128. }
  1129. get_random_bytes(&osb->s_next_generation, sizeof(u32));
  1130. /* FIXME
  1131. * This should be done in ocfs2_journal_init(), but unknown
  1132. * ordering issues will cause the filesystem to crash.
  1133. * If anyone wants to figure out what part of the code
  1134. * refers to osb->journal before ocfs2_journal_init() is run,
  1135. * be my guest.
  1136. */
  1137. /* initialize our journal structure */
  1138. journal = kcalloc(1, sizeof(struct ocfs2_journal), GFP_KERNEL);
  1139. if (!journal) {
  1140. mlog(ML_ERROR, "unable to alloc journal\n");
  1141. status = -ENOMEM;
  1142. goto bail;
  1143. }
  1144. osb->journal = journal;
  1145. journal->j_osb = osb;
  1146. atomic_set(&journal->j_num_trans, 0);
  1147. init_rwsem(&journal->j_trans_barrier);
  1148. init_waitqueue_head(&journal->j_checkpointed);
  1149. spin_lock_init(&journal->j_lock);
  1150. journal->j_trans_id = (unsigned long) 1;
  1151. INIT_LIST_HEAD(&journal->j_la_cleanups);
  1152. INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery, osb);
  1153. journal->j_state = OCFS2_JOURNAL_FREE;
  1154. /* get some pseudo constants for clustersize bits */
  1155. osb->s_clustersize_bits =
  1156. le32_to_cpu(di->id2.i_super.s_clustersize_bits);
  1157. osb->s_clustersize = 1 << osb->s_clustersize_bits;
  1158. mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
  1159. if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
  1160. osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
  1161. mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
  1162. osb->s_clustersize);
  1163. status = -EINVAL;
  1164. goto bail;
  1165. }
  1166. if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
  1167. > (u32)~0UL) {
  1168. mlog(ML_ERROR, "Volume might try to write to blocks beyond "
  1169. "what jbd can address in 32 bits.\n");
  1170. status = -EINVAL;
  1171. goto bail;
  1172. }
  1173. if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
  1174. sizeof(di->id2.i_super.s_uuid))) {
  1175. mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
  1176. status = -ENOMEM;
  1177. goto bail;
  1178. }
  1179. memcpy(&uuid_net_key, &osb->uuid[i], sizeof(osb->net_key));
  1180. osb->net_key = le32_to_cpu(uuid_net_key);
  1181. strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
  1182. osb->vol_label[63] = '\0';
  1183. osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
  1184. osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
  1185. osb->first_cluster_group_blkno =
  1186. le64_to_cpu(di->id2.i_super.s_first_cluster_group);
  1187. osb->fs_generation = le32_to_cpu(di->i_fs_generation);
  1188. mlog(0, "vol_label: %s\n", osb->vol_label);
  1189. mlog(0, "uuid: %s\n", osb->uuid_str);
  1190. mlog(0, "root_blkno=%"MLFu64", system_dir_blkno=%"MLFu64"\n",
  1191. osb->root_blkno, osb->system_dir_blkno);
  1192. osb->osb_dlm_debug = ocfs2_new_dlm_debug();
  1193. if (!osb->osb_dlm_debug) {
  1194. status = -ENOMEM;
  1195. mlog_errno(status);
  1196. goto bail;
  1197. }
  1198. atomic_set(&osb->vol_state, VOLUME_INIT);
  1199. /* load root, system_dir, and all global system inodes */
  1200. status = ocfs2_init_global_system_inodes(osb);
  1201. if (status < 0) {
  1202. mlog_errno(status);
  1203. goto bail;
  1204. }
  1205. /*
  1206. * global bitmap
  1207. */
  1208. inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
  1209. OCFS2_INVALID_SLOT);
  1210. if (!inode) {
  1211. status = -EINVAL;
  1212. mlog_errno(status);
  1213. goto bail;
  1214. }
  1215. osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
  1216. status = ocfs2_read_block(osb, osb->bitmap_blkno, &bitmap_bh, 0,
  1217. inode);
  1218. iput(inode);
  1219. if (status < 0) {
  1220. mlog_errno(status);
  1221. goto bail;
  1222. }
  1223. di = (struct ocfs2_dinode *) bitmap_bh->b_data;
  1224. osb->bitmap_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg);
  1225. osb->num_clusters = le32_to_cpu(di->id1.bitmap1.i_total);
  1226. brelse(bitmap_bh);
  1227. mlog(0, "cluster bitmap inode: %"MLFu64", clusters per group: %u\n",
  1228. osb->bitmap_blkno, osb->bitmap_cpg);
  1229. status = ocfs2_init_slot_info(osb);
  1230. if (status < 0) {
  1231. mlog_errno(status);
  1232. goto bail;
  1233. }
  1234. /* Link this osb onto the global linked list of all osb structures. */
  1235. /* The Global Link List is mainted for the whole driver . */
  1236. spin_lock(&ocfs2_globals_lock);
  1237. osb->osb_id = osb_id;
  1238. if (osb_id < OCFS2_MAX_OSB_ID)
  1239. osb_id++;
  1240. else {
  1241. mlog(ML_ERROR, "Too many volumes mounted\n");
  1242. status = -ENOMEM;
  1243. }
  1244. spin_unlock(&ocfs2_globals_lock);
  1245. bail:
  1246. mlog_exit(status);
  1247. return status;
  1248. }
  1249. /*
  1250. * will return: -EAGAIN if it is ok to keep searching for superblocks
  1251. * -EINVAL if there is a bad superblock
  1252. * 0 on success
  1253. */
  1254. static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  1255. struct buffer_head *bh,
  1256. u32 blksz)
  1257. {
  1258. int status = -EAGAIN;
  1259. mlog_entry_void();
  1260. if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
  1261. strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
  1262. status = -EINVAL;
  1263. if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
  1264. mlog(ML_ERROR, "found superblock with incorrect block "
  1265. "size: found %u, should be %u\n",
  1266. 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
  1267. blksz);
  1268. } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
  1269. OCFS2_MAJOR_REV_LEVEL ||
  1270. le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
  1271. OCFS2_MINOR_REV_LEVEL) {
  1272. mlog(ML_ERROR, "found superblock with bad version: "
  1273. "found %u.%u, should be %u.%u\n",
  1274. le16_to_cpu(di->id2.i_super.s_major_rev_level),
  1275. le16_to_cpu(di->id2.i_super.s_minor_rev_level),
  1276. OCFS2_MAJOR_REV_LEVEL,
  1277. OCFS2_MINOR_REV_LEVEL);
  1278. } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
  1279. mlog(ML_ERROR, "bad block number on superblock: "
  1280. "found %"MLFu64", should be %llu\n",
  1281. di->i_blkno, (unsigned long long)bh->b_blocknr);
  1282. } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
  1283. le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
  1284. mlog(ML_ERROR, "bad cluster size found: %u\n",
  1285. 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
  1286. } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
  1287. mlog(ML_ERROR, "bad root_blkno: 0\n");
  1288. } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
  1289. mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
  1290. } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
  1291. mlog(ML_ERROR,
  1292. "Superblock slots found greater than file system "
  1293. "maximum: found %u, max %u\n",
  1294. le16_to_cpu(di->id2.i_super.s_max_slots),
  1295. OCFS2_MAX_SLOTS);
  1296. } else {
  1297. /* found it! */
  1298. status = 0;
  1299. }
  1300. }
  1301. mlog_exit(status);
  1302. return status;
  1303. }
  1304. static int ocfs2_check_volume(struct ocfs2_super *osb)
  1305. {
  1306. int status = 0;
  1307. int dirty;
  1308. struct ocfs2_dinode *local_alloc = NULL; /* only used if we
  1309. * recover
  1310. * ourselves. */
  1311. mlog_entry_void();
  1312. /* Init our journal object. */
  1313. status = ocfs2_journal_init(osb->journal, &dirty);
  1314. if (status < 0) {
  1315. mlog(ML_ERROR, "Could not initialize journal!\n");
  1316. goto finally;
  1317. }
  1318. /* If the journal was unmounted cleanly then we don't want to
  1319. * recover anything. Otherwise, journal_load will do that
  1320. * dirty work for us :) */
  1321. if (!dirty) {
  1322. status = ocfs2_journal_wipe(osb->journal, 0);
  1323. if (status < 0) {
  1324. mlog_errno(status);
  1325. goto finally;
  1326. }
  1327. } else {
  1328. mlog(ML_NOTICE, "File system was not unmounted cleanly, "
  1329. "recovering volume.\n");
  1330. }
  1331. /* will play back anything left in the journal. */
  1332. ocfs2_journal_load(osb->journal);
  1333. if (dirty) {
  1334. /* recover my local alloc if we didn't unmount cleanly. */
  1335. status = ocfs2_begin_local_alloc_recovery(osb,
  1336. osb->slot_num,
  1337. &local_alloc);
  1338. if (status < 0) {
  1339. mlog_errno(status);
  1340. goto finally;
  1341. }
  1342. /* we complete the recovery process after we've marked
  1343. * ourselves as mounted. */
  1344. }
  1345. mlog(0, "Journal loaded.\n");
  1346. status = ocfs2_load_local_alloc(osb);
  1347. if (status < 0) {
  1348. mlog_errno(status);
  1349. goto finally;
  1350. }
  1351. if (dirty) {
  1352. /* Recovery will be completed after we've mounted the
  1353. * rest of the volume. */
  1354. osb->dirty = 1;
  1355. osb->local_alloc_copy = local_alloc;
  1356. local_alloc = NULL;
  1357. }
  1358. /* go through each journal, trylock it and if you get the
  1359. * lock, and it's marked as dirty, set the bit in the recover
  1360. * map and launch a recovery thread for it. */
  1361. status = ocfs2_mark_dead_nodes(osb);
  1362. if (status < 0)
  1363. mlog_errno(status);
  1364. finally:
  1365. if (local_alloc)
  1366. kfree(local_alloc);
  1367. mlog_exit(status);
  1368. return status;
  1369. }
  1370. /*
  1371. * The routine gets called from dismount or close whenever a dismount on
  1372. * volume is requested and the osb open count becomes 1.
  1373. * It will remove the osb from the global list and also free up all the
  1374. * initialized resources and fileobject.
  1375. */
  1376. static void ocfs2_delete_osb(struct ocfs2_super *osb)
  1377. {
  1378. mlog_entry_void();
  1379. /* This function assumes that the caller has the main osb resource */
  1380. if (osb->slot_info)
  1381. ocfs2_free_slot_info(osb->slot_info);
  1382. kfree(osb->osb_orphan_wipes);
  1383. /* FIXME
  1384. * This belongs in journal shutdown, but because we have to
  1385. * allocate osb->journal at the start of ocfs2_initalize_osb(),
  1386. * we free it here.
  1387. */
  1388. kfree(osb->journal);
  1389. if (osb->local_alloc_copy)
  1390. kfree(osb->local_alloc_copy);
  1391. kfree(osb->uuid_str);
  1392. ocfs2_put_dlm_debug(osb->osb_dlm_debug);
  1393. memset(osb, 0, sizeof(struct ocfs2_super));
  1394. mlog_exit_void();
  1395. }
  1396. /* Put OCFS2 into a readonly state, or (if the user specifies it),
  1397. * panic(). We do not support continue-on-error operation. */
  1398. static void ocfs2_handle_error(struct super_block *sb)
  1399. {
  1400. struct ocfs2_super *osb = OCFS2_SB(sb);
  1401. if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
  1402. panic("OCFS2: (device %s): panic forced after error\n",
  1403. sb->s_id);
  1404. ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
  1405. if (sb->s_flags & MS_RDONLY &&
  1406. (ocfs2_is_soft_readonly(osb) ||
  1407. ocfs2_is_hard_readonly(osb)))
  1408. return;
  1409. printk(KERN_CRIT "File system is now read-only due to the potential "
  1410. "of on-disk corruption. Please run fsck.ocfs2 once the file "
  1411. "system is unmounted.\n");
  1412. sb->s_flags |= MS_RDONLY;
  1413. ocfs2_set_ro_flag(osb, 0);
  1414. }
  1415. static char error_buf[1024];
  1416. void __ocfs2_error(struct super_block *sb,
  1417. const char *function,
  1418. const char *fmt, ...)
  1419. {
  1420. va_list args;
  1421. va_start(args, fmt);
  1422. vsprintf(error_buf, fmt, args);
  1423. va_end(args);
  1424. /* Not using mlog here because we want to show the actual
  1425. * function the error came from. */
  1426. printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
  1427. sb->s_id, function, error_buf);
  1428. ocfs2_handle_error(sb);
  1429. }
  1430. /* Handle critical errors. This is intentionally more drastic than
  1431. * ocfs2_handle_error, so we only use for things like journal errors,
  1432. * etc. */
  1433. void __ocfs2_abort(struct super_block* sb,
  1434. const char *function,
  1435. const char *fmt, ...)
  1436. {
  1437. va_list args;
  1438. va_start(args, fmt);
  1439. vsprintf(error_buf, fmt, args);
  1440. va_end(args);
  1441. printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
  1442. sb->s_id, function, error_buf);
  1443. /* We don't have the cluster support yet to go straight to
  1444. * hard readonly in here. Until then, we want to keep
  1445. * ocfs2_abort() so that we can at least mark critical
  1446. * errors.
  1447. *
  1448. * TODO: This should abort the journal and alert other nodes
  1449. * that our slot needs recovery. */
  1450. /* Force a panic(). This stinks, but it's better than letting
  1451. * things continue without having a proper hard readonly
  1452. * here. */
  1453. OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
  1454. ocfs2_handle_error(sb);
  1455. }
  1456. module_init(ocfs2_init);
  1457. module_exit(ocfs2_exit);