super.c 43 KB

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