super.c 42 KB

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