super.c 43 KB

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