super.c 43 KB

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