super.c 44 KB

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