super.c 42 KB

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