super.c 43 KB

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