super.c 44 KB

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