super.c 43 KB

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