super.c 42 KB

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