super.c 43 KB

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