super.c 43 KB

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