super.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  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 const 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. status = init_ocfs2_uptodate_cache();
  698. if (status < 0) {
  699. mlog_errno(status);
  700. goto leave;
  701. }
  702. status = ocfs2_initialize_mem_caches();
  703. if (status < 0) {
  704. mlog_errno(status);
  705. goto leave;
  706. }
  707. ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
  708. if (!ocfs2_wq) {
  709. status = -ENOMEM;
  710. goto leave;
  711. }
  712. ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
  713. if (!ocfs2_debugfs_root) {
  714. status = -EFAULT;
  715. mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
  716. }
  717. leave:
  718. if (status < 0) {
  719. ocfs2_free_mem_caches();
  720. exit_ocfs2_uptodate_cache();
  721. }
  722. mlog_exit(status);
  723. if (status >= 0) {
  724. return register_filesystem(&ocfs2_fs_type);
  725. } else
  726. return -1;
  727. }
  728. static void __exit ocfs2_exit(void)
  729. {
  730. mlog_entry_void();
  731. if (ocfs2_wq) {
  732. flush_workqueue(ocfs2_wq);
  733. destroy_workqueue(ocfs2_wq);
  734. }
  735. debugfs_remove(ocfs2_debugfs_root);
  736. ocfs2_free_mem_caches();
  737. unregister_filesystem(&ocfs2_fs_type);
  738. exit_ocfs2_uptodate_cache();
  739. mlog_exit_void();
  740. }
  741. static void ocfs2_put_super(struct super_block *sb)
  742. {
  743. mlog_entry("(0x%p)\n", sb);
  744. ocfs2_sync_blockdev(sb);
  745. ocfs2_dismount_volume(sb, 0);
  746. mlog_exit_void();
  747. }
  748. static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  749. {
  750. struct ocfs2_super *osb;
  751. u32 numbits, freebits;
  752. int status;
  753. struct ocfs2_dinode *bm_lock;
  754. struct buffer_head *bh = NULL;
  755. struct inode *inode = NULL;
  756. mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
  757. osb = OCFS2_SB(dentry->d_sb);
  758. inode = ocfs2_get_system_file_inode(osb,
  759. GLOBAL_BITMAP_SYSTEM_INODE,
  760. OCFS2_INVALID_SLOT);
  761. if (!inode) {
  762. mlog(ML_ERROR, "failed to get bitmap inode\n");
  763. status = -EIO;
  764. goto bail;
  765. }
  766. status = ocfs2_meta_lock(inode, &bh, 0);
  767. if (status < 0) {
  768. mlog_errno(status);
  769. goto bail;
  770. }
  771. bm_lock = (struct ocfs2_dinode *) bh->b_data;
  772. numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
  773. freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
  774. buf->f_type = OCFS2_SUPER_MAGIC;
  775. buf->f_bsize = dentry->d_sb->s_blocksize;
  776. buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
  777. buf->f_blocks = ((sector_t) numbits) *
  778. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  779. buf->f_bfree = ((sector_t) freebits) *
  780. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  781. buf->f_bavail = buf->f_bfree;
  782. buf->f_files = numbits;
  783. buf->f_ffree = freebits;
  784. brelse(bh);
  785. ocfs2_meta_unlock(inode, 0);
  786. status = 0;
  787. bail:
  788. if (inode)
  789. iput(inode);
  790. mlog_exit(status);
  791. return status;
  792. }
  793. static void ocfs2_inode_init_once(void *data,
  794. struct kmem_cache *cachep,
  795. unsigned long flags)
  796. {
  797. struct ocfs2_inode_info *oi = data;
  798. if (flags & SLAB_CTOR_CONSTRUCTOR) {
  799. oi->ip_flags = 0;
  800. oi->ip_open_count = 0;
  801. spin_lock_init(&oi->ip_lock);
  802. ocfs2_extent_map_init(&oi->vfs_inode);
  803. INIT_LIST_HEAD(&oi->ip_io_markers);
  804. oi->ip_created_trans = 0;
  805. oi->ip_last_trans = 0;
  806. oi->ip_dir_start_lookup = 0;
  807. init_rwsem(&oi->ip_alloc_sem);
  808. mutex_init(&oi->ip_io_mutex);
  809. oi->ip_blkno = 0ULL;
  810. oi->ip_clusters = 0;
  811. ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
  812. ocfs2_lock_res_init_once(&oi->ip_meta_lockres);
  813. ocfs2_lock_res_init_once(&oi->ip_data_lockres);
  814. ocfs2_lock_res_init_once(&oi->ip_open_lockres);
  815. ocfs2_metadata_cache_init(&oi->vfs_inode);
  816. inode_init_once(&oi->vfs_inode);
  817. }
  818. }
  819. static int ocfs2_initialize_mem_caches(void)
  820. {
  821. ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
  822. sizeof(struct ocfs2_inode_info),
  823. 0,
  824. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  825. SLAB_MEM_SPREAD),
  826. ocfs2_inode_init_once, NULL);
  827. if (!ocfs2_inode_cachep)
  828. return -ENOMEM;
  829. return 0;
  830. }
  831. static void ocfs2_free_mem_caches(void)
  832. {
  833. if (ocfs2_inode_cachep)
  834. kmem_cache_destroy(ocfs2_inode_cachep);
  835. ocfs2_inode_cachep = NULL;
  836. }
  837. static int ocfs2_get_sector(struct super_block *sb,
  838. struct buffer_head **bh,
  839. int block,
  840. int sect_size)
  841. {
  842. if (!sb_set_blocksize(sb, sect_size)) {
  843. mlog(ML_ERROR, "unable to set blocksize\n");
  844. return -EIO;
  845. }
  846. *bh = sb_getblk(sb, block);
  847. if (!*bh) {
  848. mlog_errno(-EIO);
  849. return -EIO;
  850. }
  851. lock_buffer(*bh);
  852. if (!buffer_dirty(*bh))
  853. clear_buffer_uptodate(*bh);
  854. unlock_buffer(*bh);
  855. ll_rw_block(READ, 1, bh);
  856. wait_on_buffer(*bh);
  857. return 0;
  858. }
  859. /* ocfs2 1.0 only allows one cluster and node identity per kernel image. */
  860. static int ocfs2_fill_local_node_info(struct ocfs2_super *osb)
  861. {
  862. int status;
  863. /* XXX hold a ref on the node while mounte? easy enough, if
  864. * desirable. */
  865. if (ocfs2_mount_local(osb))
  866. osb->node_num = 0;
  867. else
  868. osb->node_num = o2nm_this_node();
  869. if (osb->node_num == O2NM_MAX_NODES) {
  870. mlog(ML_ERROR, "could not find this host's node number\n");
  871. status = -ENOENT;
  872. goto bail;
  873. }
  874. mlog(0, "I am node %d\n", osb->node_num);
  875. status = 0;
  876. bail:
  877. return status;
  878. }
  879. static int ocfs2_mount_volume(struct super_block *sb)
  880. {
  881. int status = 0;
  882. int unlock_super = 0;
  883. struct ocfs2_super *osb = OCFS2_SB(sb);
  884. mlog_entry_void();
  885. if (ocfs2_is_hard_readonly(osb))
  886. goto leave;
  887. status = ocfs2_fill_local_node_info(osb);
  888. if (status < 0) {
  889. mlog_errno(status);
  890. goto leave;
  891. }
  892. status = ocfs2_register_hb_callbacks(osb);
  893. if (status < 0) {
  894. mlog_errno(status);
  895. goto leave;
  896. }
  897. status = ocfs2_dlm_init(osb);
  898. if (status < 0) {
  899. mlog_errno(status);
  900. goto leave;
  901. }
  902. /* requires vote_thread to be running. */
  903. status = ocfs2_register_net_handlers(osb);
  904. if (status < 0) {
  905. mlog_errno(status);
  906. goto leave;
  907. }
  908. status = ocfs2_super_lock(osb, 1);
  909. if (status < 0) {
  910. mlog_errno(status);
  911. goto leave;
  912. }
  913. unlock_super = 1;
  914. /* This will load up the node map and add ourselves to it. */
  915. status = ocfs2_find_slot(osb);
  916. if (status < 0) {
  917. mlog_errno(status);
  918. goto leave;
  919. }
  920. ocfs2_populate_mounted_map(osb);
  921. /* load all node-local system inodes */
  922. status = ocfs2_init_local_system_inodes(osb);
  923. if (status < 0) {
  924. mlog_errno(status);
  925. goto leave;
  926. }
  927. status = ocfs2_check_volume(osb);
  928. if (status < 0) {
  929. mlog_errno(status);
  930. goto leave;
  931. }
  932. status = ocfs2_truncate_log_init(osb);
  933. if (status < 0) {
  934. mlog_errno(status);
  935. goto leave;
  936. }
  937. if (ocfs2_mount_local(osb))
  938. goto leave;
  939. /* This should be sent *after* we recovered our journal as it
  940. * will cause other nodes to unmark us as needing
  941. * recovery. However, we need to send it *before* dropping the
  942. * super block lock as otherwise their recovery threads might
  943. * try to clean us up while we're live! */
  944. status = ocfs2_request_mount_vote(osb);
  945. if (status < 0)
  946. mlog_errno(status);
  947. leave:
  948. if (unlock_super)
  949. ocfs2_super_unlock(osb, 1);
  950. mlog_exit(status);
  951. return status;
  952. }
  953. /* we can't grab the goofy sem lock from inside wait_event, so we use
  954. * memory barriers to make sure that we'll see the null task before
  955. * being woken up */
  956. static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
  957. {
  958. mb();
  959. return osb->recovery_thread_task != NULL;
  960. }
  961. static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
  962. {
  963. int tmp;
  964. struct ocfs2_super *osb = NULL;
  965. char nodestr[8];
  966. mlog_entry("(0x%p)\n", sb);
  967. BUG_ON(!sb);
  968. osb = OCFS2_SB(sb);
  969. BUG_ON(!osb);
  970. ocfs2_shutdown_local_alloc(osb);
  971. ocfs2_truncate_log_shutdown(osb);
  972. /* disable any new recovery threads and wait for any currently
  973. * running ones to exit. Do this before setting the vol_state. */
  974. mutex_lock(&osb->recovery_lock);
  975. osb->disable_recovery = 1;
  976. mutex_unlock(&osb->recovery_lock);
  977. wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
  978. /* At this point, we know that no more recovery threads can be
  979. * launched, so wait for any recovery completion work to
  980. * complete. */
  981. flush_workqueue(ocfs2_wq);
  982. ocfs2_journal_shutdown(osb);
  983. ocfs2_sync_blockdev(sb);
  984. /* No dlm means we've failed during mount, so skip all the
  985. * steps which depended on that to complete. */
  986. if (osb->dlm) {
  987. tmp = ocfs2_super_lock(osb, 1);
  988. if (tmp < 0) {
  989. mlog_errno(tmp);
  990. return;
  991. }
  992. tmp = ocfs2_request_umount_vote(osb);
  993. if (tmp < 0)
  994. mlog_errno(tmp);
  995. if (osb->slot_num != OCFS2_INVALID_SLOT)
  996. ocfs2_put_slot(osb);
  997. ocfs2_super_unlock(osb, 1);
  998. }
  999. ocfs2_release_system_inodes(osb);
  1000. if (osb->dlm) {
  1001. ocfs2_unregister_net_handlers(osb);
  1002. ocfs2_dlm_shutdown(osb);
  1003. }
  1004. ocfs2_clear_hb_callbacks(osb);
  1005. debugfs_remove(osb->osb_debug_root);
  1006. if (!mnt_err)
  1007. ocfs2_stop_heartbeat(osb);
  1008. atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
  1009. if (ocfs2_mount_local(osb))
  1010. snprintf(nodestr, sizeof(nodestr), "local");
  1011. else
  1012. snprintf(nodestr, sizeof(nodestr), "%d", osb->node_num);
  1013. printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
  1014. osb->dev_str, nodestr);
  1015. ocfs2_delete_osb(osb);
  1016. kfree(osb);
  1017. sb->s_dev = 0;
  1018. sb->s_fs_info = NULL;
  1019. }
  1020. static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
  1021. unsigned uuid_bytes)
  1022. {
  1023. int i, ret;
  1024. char *ptr;
  1025. BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
  1026. osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
  1027. if (osb->uuid_str == NULL)
  1028. return -ENOMEM;
  1029. for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
  1030. /* print with null */
  1031. ret = snprintf(ptr, 3, "%02X", uuid[i]);
  1032. if (ret != 2) /* drop super cleans up */
  1033. return -EINVAL;
  1034. /* then only advance past the last char */
  1035. ptr += 2;
  1036. }
  1037. return 0;
  1038. }
  1039. static int ocfs2_initialize_super(struct super_block *sb,
  1040. struct buffer_head *bh,
  1041. int sector_size)
  1042. {
  1043. int status = 0;
  1044. int i;
  1045. struct ocfs2_dinode *di = NULL;
  1046. struct inode *inode = NULL;
  1047. struct buffer_head *bitmap_bh = NULL;
  1048. struct ocfs2_journal *journal;
  1049. __le32 uuid_net_key;
  1050. struct ocfs2_super *osb;
  1051. mlog_entry_void();
  1052. osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
  1053. if (!osb) {
  1054. status = -ENOMEM;
  1055. mlog_errno(status);
  1056. goto bail;
  1057. }
  1058. sb->s_fs_info = osb;
  1059. sb->s_op = &ocfs2_sops;
  1060. sb->s_export_op = &ocfs2_export_ops;
  1061. sb->s_flags |= MS_NOATIME;
  1062. /* this is needed to support O_LARGEFILE */
  1063. sb->s_maxbytes = ocfs2_max_file_offset(sb->s_blocksize_bits);
  1064. osb->sb = sb;
  1065. /* Save off for ocfs2_rw_direct */
  1066. osb->s_sectsize_bits = blksize_bits(sector_size);
  1067. BUG_ON(!osb->s_sectsize_bits);
  1068. osb->net_response_ids = 0;
  1069. spin_lock_init(&osb->net_response_lock);
  1070. INIT_LIST_HEAD(&osb->net_response_list);
  1071. INIT_LIST_HEAD(&osb->osb_net_handlers);
  1072. init_waitqueue_head(&osb->recovery_event);
  1073. spin_lock_init(&osb->vote_task_lock);
  1074. init_waitqueue_head(&osb->vote_event);
  1075. osb->vote_work_sequence = 0;
  1076. osb->vote_wake_sequence = 0;
  1077. INIT_LIST_HEAD(&osb->blocked_lock_list);
  1078. osb->blocked_lock_count = 0;
  1079. INIT_LIST_HEAD(&osb->vote_list);
  1080. spin_lock_init(&osb->osb_lock);
  1081. atomic_set(&osb->alloc_stats.moves, 0);
  1082. atomic_set(&osb->alloc_stats.local_data, 0);
  1083. atomic_set(&osb->alloc_stats.bitmap_data, 0);
  1084. atomic_set(&osb->alloc_stats.bg_allocs, 0);
  1085. atomic_set(&osb->alloc_stats.bg_extends, 0);
  1086. ocfs2_init_node_maps(osb);
  1087. snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
  1088. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1089. mutex_init(&osb->recovery_lock);
  1090. osb->disable_recovery = 0;
  1091. osb->recovery_thread_task = NULL;
  1092. init_waitqueue_head(&osb->checkpoint_event);
  1093. atomic_set(&osb->needs_checkpoint, 0);
  1094. osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
  1095. osb->node_num = O2NM_INVALID_NODE_NUM;
  1096. osb->slot_num = OCFS2_INVALID_SLOT;
  1097. osb->local_alloc_state = OCFS2_LA_UNUSED;
  1098. osb->local_alloc_bh = NULL;
  1099. ocfs2_setup_hb_callbacks(osb);
  1100. init_waitqueue_head(&osb->osb_mount_event);
  1101. osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
  1102. if (!osb->vol_label) {
  1103. mlog(ML_ERROR, "unable to alloc vol label\n");
  1104. status = -ENOMEM;
  1105. goto bail;
  1106. }
  1107. di = (struct ocfs2_dinode *)bh->b_data;
  1108. osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
  1109. if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
  1110. mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
  1111. osb->max_slots);
  1112. status = -EINVAL;
  1113. goto bail;
  1114. }
  1115. mlog(0, "max_slots for this device: %u\n", osb->max_slots);
  1116. init_waitqueue_head(&osb->osb_wipe_event);
  1117. osb->osb_orphan_wipes = kcalloc(osb->max_slots,
  1118. sizeof(*osb->osb_orphan_wipes),
  1119. GFP_KERNEL);
  1120. if (!osb->osb_orphan_wipes) {
  1121. status = -ENOMEM;
  1122. mlog_errno(status);
  1123. goto bail;
  1124. }
  1125. osb->s_feature_compat =
  1126. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
  1127. osb->s_feature_ro_compat =
  1128. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
  1129. osb->s_feature_incompat =
  1130. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
  1131. if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
  1132. mlog(ML_ERROR, "couldn't mount because of unsupported "
  1133. "optional features (%x).\n", i);
  1134. status = -EINVAL;
  1135. goto bail;
  1136. }
  1137. if (!(osb->sb->s_flags & MS_RDONLY) &&
  1138. (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
  1139. mlog(ML_ERROR, "couldn't mount RDWR because of "
  1140. "unsupported optional features (%x).\n", i);
  1141. status = -EINVAL;
  1142. goto bail;
  1143. }
  1144. get_random_bytes(&osb->s_next_generation, sizeof(u32));
  1145. /* FIXME
  1146. * This should be done in ocfs2_journal_init(), but unknown
  1147. * ordering issues will cause the filesystem to crash.
  1148. * If anyone wants to figure out what part of the code
  1149. * refers to osb->journal before ocfs2_journal_init() is run,
  1150. * be my guest.
  1151. */
  1152. /* initialize our journal structure */
  1153. journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
  1154. if (!journal) {
  1155. mlog(ML_ERROR, "unable to alloc journal\n");
  1156. status = -ENOMEM;
  1157. goto bail;
  1158. }
  1159. osb->journal = journal;
  1160. journal->j_osb = osb;
  1161. atomic_set(&journal->j_num_trans, 0);
  1162. init_rwsem(&journal->j_trans_barrier);
  1163. init_waitqueue_head(&journal->j_checkpointed);
  1164. spin_lock_init(&journal->j_lock);
  1165. journal->j_trans_id = (unsigned long) 1;
  1166. INIT_LIST_HEAD(&journal->j_la_cleanups);
  1167. INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
  1168. journal->j_state = OCFS2_JOURNAL_FREE;
  1169. /* get some pseudo constants for clustersize bits */
  1170. osb->s_clustersize_bits =
  1171. le32_to_cpu(di->id2.i_super.s_clustersize_bits);
  1172. osb->s_clustersize = 1 << osb->s_clustersize_bits;
  1173. mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
  1174. if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
  1175. osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
  1176. mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
  1177. osb->s_clustersize);
  1178. status = -EINVAL;
  1179. goto bail;
  1180. }
  1181. if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
  1182. > (u32)~0UL) {
  1183. mlog(ML_ERROR, "Volume might try to write to blocks beyond "
  1184. "what jbd can address in 32 bits.\n");
  1185. status = -EINVAL;
  1186. goto bail;
  1187. }
  1188. if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
  1189. sizeof(di->id2.i_super.s_uuid))) {
  1190. mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
  1191. status = -ENOMEM;
  1192. goto bail;
  1193. }
  1194. memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
  1195. osb->net_key = le32_to_cpu(uuid_net_key);
  1196. strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
  1197. osb->vol_label[63] = '\0';
  1198. osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
  1199. osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
  1200. osb->first_cluster_group_blkno =
  1201. le64_to_cpu(di->id2.i_super.s_first_cluster_group);
  1202. osb->fs_generation = le32_to_cpu(di->i_fs_generation);
  1203. mlog(0, "vol_label: %s\n", osb->vol_label);
  1204. mlog(0, "uuid: %s\n", osb->uuid_str);
  1205. mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
  1206. (unsigned long long)osb->root_blkno,
  1207. (unsigned long long)osb->system_dir_blkno);
  1208. osb->osb_dlm_debug = ocfs2_new_dlm_debug();
  1209. if (!osb->osb_dlm_debug) {
  1210. status = -ENOMEM;
  1211. mlog_errno(status);
  1212. goto bail;
  1213. }
  1214. atomic_set(&osb->vol_state, VOLUME_INIT);
  1215. /* load root, system_dir, and all global system inodes */
  1216. status = ocfs2_init_global_system_inodes(osb);
  1217. if (status < 0) {
  1218. mlog_errno(status);
  1219. goto bail;
  1220. }
  1221. /*
  1222. * global bitmap
  1223. */
  1224. inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
  1225. OCFS2_INVALID_SLOT);
  1226. if (!inode) {
  1227. status = -EINVAL;
  1228. mlog_errno(status);
  1229. goto bail;
  1230. }
  1231. osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
  1232. /* We don't have a cluster lock on the bitmap here because
  1233. * we're only interested in static information and the extra
  1234. * complexity at mount time isn't worht it. Don't pass the
  1235. * inode in to the read function though as we don't want it to
  1236. * be put in the cache. */
  1237. status = ocfs2_read_block(osb, osb->bitmap_blkno, &bitmap_bh, 0,
  1238. NULL);
  1239. iput(inode);
  1240. if (status < 0) {
  1241. mlog_errno(status);
  1242. goto bail;
  1243. }
  1244. di = (struct ocfs2_dinode *) bitmap_bh->b_data;
  1245. osb->bitmap_cpg = le16_to_cpu(di->id2.i_chain.cl_cpg);
  1246. brelse(bitmap_bh);
  1247. mlog(0, "cluster bitmap inode: %llu, clusters per group: %u\n",
  1248. (unsigned long long)osb->bitmap_blkno, osb->bitmap_cpg);
  1249. status = ocfs2_init_slot_info(osb);
  1250. if (status < 0) {
  1251. mlog_errno(status);
  1252. goto bail;
  1253. }
  1254. bail:
  1255. mlog_exit(status);
  1256. return status;
  1257. }
  1258. /*
  1259. * will return: -EAGAIN if it is ok to keep searching for superblocks
  1260. * -EINVAL if there is a bad superblock
  1261. * 0 on success
  1262. */
  1263. static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  1264. struct buffer_head *bh,
  1265. u32 blksz)
  1266. {
  1267. int status = -EAGAIN;
  1268. mlog_entry_void();
  1269. if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
  1270. strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
  1271. status = -EINVAL;
  1272. if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
  1273. mlog(ML_ERROR, "found superblock with incorrect block "
  1274. "size: found %u, should be %u\n",
  1275. 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
  1276. blksz);
  1277. } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
  1278. OCFS2_MAJOR_REV_LEVEL ||
  1279. le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
  1280. OCFS2_MINOR_REV_LEVEL) {
  1281. mlog(ML_ERROR, "found superblock with bad version: "
  1282. "found %u.%u, should be %u.%u\n",
  1283. le16_to_cpu(di->id2.i_super.s_major_rev_level),
  1284. le16_to_cpu(di->id2.i_super.s_minor_rev_level),
  1285. OCFS2_MAJOR_REV_LEVEL,
  1286. OCFS2_MINOR_REV_LEVEL);
  1287. } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
  1288. mlog(ML_ERROR, "bad block number on superblock: "
  1289. "found %llu, should be %llu\n",
  1290. (unsigned long long)le64_to_cpu(di->i_blkno),
  1291. (unsigned long long)bh->b_blocknr);
  1292. } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
  1293. le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
  1294. mlog(ML_ERROR, "bad cluster size found: %u\n",
  1295. 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
  1296. } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
  1297. mlog(ML_ERROR, "bad root_blkno: 0\n");
  1298. } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
  1299. mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
  1300. } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
  1301. mlog(ML_ERROR,
  1302. "Superblock slots found greater than file system "
  1303. "maximum: found %u, max %u\n",
  1304. le16_to_cpu(di->id2.i_super.s_max_slots),
  1305. OCFS2_MAX_SLOTS);
  1306. } else {
  1307. /* found it! */
  1308. status = 0;
  1309. }
  1310. }
  1311. mlog_exit(status);
  1312. return status;
  1313. }
  1314. static int ocfs2_check_volume(struct ocfs2_super *osb)
  1315. {
  1316. int status = 0;
  1317. int dirty;
  1318. int local;
  1319. struct ocfs2_dinode *local_alloc = NULL; /* only used if we
  1320. * recover
  1321. * ourselves. */
  1322. mlog_entry_void();
  1323. /* Init our journal object. */
  1324. status = ocfs2_journal_init(osb->journal, &dirty);
  1325. if (status < 0) {
  1326. mlog(ML_ERROR, "Could not initialize journal!\n");
  1327. goto finally;
  1328. }
  1329. /* If the journal was unmounted cleanly then we don't want to
  1330. * recover anything. Otherwise, journal_load will do that
  1331. * dirty work for us :) */
  1332. if (!dirty) {
  1333. status = ocfs2_journal_wipe(osb->journal, 0);
  1334. if (status < 0) {
  1335. mlog_errno(status);
  1336. goto finally;
  1337. }
  1338. } else {
  1339. mlog(ML_NOTICE, "File system was not unmounted cleanly, "
  1340. "recovering volume.\n");
  1341. }
  1342. local = ocfs2_mount_local(osb);
  1343. /* will play back anything left in the journal. */
  1344. ocfs2_journal_load(osb->journal, local);
  1345. if (dirty) {
  1346. /* recover my local alloc if we didn't unmount cleanly. */
  1347. status = ocfs2_begin_local_alloc_recovery(osb,
  1348. osb->slot_num,
  1349. &local_alloc);
  1350. if (status < 0) {
  1351. mlog_errno(status);
  1352. goto finally;
  1353. }
  1354. /* we complete the recovery process after we've marked
  1355. * ourselves as mounted. */
  1356. }
  1357. mlog(0, "Journal loaded.\n");
  1358. status = ocfs2_load_local_alloc(osb);
  1359. if (status < 0) {
  1360. mlog_errno(status);
  1361. goto finally;
  1362. }
  1363. if (dirty) {
  1364. /* Recovery will be completed after we've mounted the
  1365. * rest of the volume. */
  1366. osb->dirty = 1;
  1367. osb->local_alloc_copy = local_alloc;
  1368. local_alloc = NULL;
  1369. }
  1370. /* go through each journal, trylock it and if you get the
  1371. * lock, and it's marked as dirty, set the bit in the recover
  1372. * map and launch a recovery thread for it. */
  1373. status = ocfs2_mark_dead_nodes(osb);
  1374. if (status < 0)
  1375. mlog_errno(status);
  1376. finally:
  1377. if (local_alloc)
  1378. kfree(local_alloc);
  1379. mlog_exit(status);
  1380. return status;
  1381. }
  1382. /*
  1383. * The routine gets called from dismount or close whenever a dismount on
  1384. * volume is requested and the osb open count becomes 1.
  1385. * It will remove the osb from the global list and also free up all the
  1386. * initialized resources and fileobject.
  1387. */
  1388. static void ocfs2_delete_osb(struct ocfs2_super *osb)
  1389. {
  1390. mlog_entry_void();
  1391. /* This function assumes that the caller has the main osb resource */
  1392. if (osb->slot_info)
  1393. ocfs2_free_slot_info(osb->slot_info);
  1394. kfree(osb->osb_orphan_wipes);
  1395. /* FIXME
  1396. * This belongs in journal shutdown, but because we have to
  1397. * allocate osb->journal at the start of ocfs2_initalize_osb(),
  1398. * we free it here.
  1399. */
  1400. kfree(osb->journal);
  1401. if (osb->local_alloc_copy)
  1402. kfree(osb->local_alloc_copy);
  1403. kfree(osb->uuid_str);
  1404. ocfs2_put_dlm_debug(osb->osb_dlm_debug);
  1405. memset(osb, 0, sizeof(struct ocfs2_super));
  1406. mlog_exit_void();
  1407. }
  1408. /* Put OCFS2 into a readonly state, or (if the user specifies it),
  1409. * panic(). We do not support continue-on-error operation. */
  1410. static void ocfs2_handle_error(struct super_block *sb)
  1411. {
  1412. struct ocfs2_super *osb = OCFS2_SB(sb);
  1413. if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
  1414. panic("OCFS2: (device %s): panic forced after error\n",
  1415. sb->s_id);
  1416. ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
  1417. if (sb->s_flags & MS_RDONLY &&
  1418. (ocfs2_is_soft_readonly(osb) ||
  1419. ocfs2_is_hard_readonly(osb)))
  1420. return;
  1421. printk(KERN_CRIT "File system is now read-only due to the potential "
  1422. "of on-disk corruption. Please run fsck.ocfs2 once the file "
  1423. "system is unmounted.\n");
  1424. sb->s_flags |= MS_RDONLY;
  1425. ocfs2_set_ro_flag(osb, 0);
  1426. }
  1427. static char error_buf[1024];
  1428. void __ocfs2_error(struct super_block *sb,
  1429. const char *function,
  1430. const char *fmt, ...)
  1431. {
  1432. va_list args;
  1433. va_start(args, fmt);
  1434. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1435. va_end(args);
  1436. /* Not using mlog here because we want to show the actual
  1437. * function the error came from. */
  1438. printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
  1439. sb->s_id, function, error_buf);
  1440. ocfs2_handle_error(sb);
  1441. }
  1442. /* Handle critical errors. This is intentionally more drastic than
  1443. * ocfs2_handle_error, so we only use for things like journal errors,
  1444. * etc. */
  1445. void __ocfs2_abort(struct super_block* sb,
  1446. const char *function,
  1447. const char *fmt, ...)
  1448. {
  1449. va_list args;
  1450. va_start(args, fmt);
  1451. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1452. va_end(args);
  1453. printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
  1454. sb->s_id, function, error_buf);
  1455. /* We don't have the cluster support yet to go straight to
  1456. * hard readonly in here. Until then, we want to keep
  1457. * ocfs2_abort() so that we can at least mark critical
  1458. * errors.
  1459. *
  1460. * TODO: This should abort the journal and alert other nodes
  1461. * that our slot needs recovery. */
  1462. /* Force a panic(). This stinks, but it's better than letting
  1463. * things continue without having a proper hard readonly
  1464. * here. */
  1465. OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
  1466. ocfs2_handle_error(sb);
  1467. }
  1468. module_init(ocfs2_init);
  1469. module_exit(ocfs2_exit);