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