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