super.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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. #include <linux/quotaops.h>
  44. #define MLOG_MASK_PREFIX ML_SUPER
  45. #include <cluster/masklog.h>
  46. #include "ocfs2.h"
  47. /* this should be the only file to include a version 1 header */
  48. #include "ocfs1_fs_compat.h"
  49. #include "alloc.h"
  50. #include "blockcheck.h"
  51. #include "dlmglue.h"
  52. #include "export.h"
  53. #include "extent_map.h"
  54. #include "heartbeat.h"
  55. #include "inode.h"
  56. #include "journal.h"
  57. #include "localalloc.h"
  58. #include "namei.h"
  59. #include "slot_map.h"
  60. #include "super.h"
  61. #include "sysfile.h"
  62. #include "uptodate.h"
  63. #include "ver.h"
  64. #include "xattr.h"
  65. #include "quota.h"
  66. #include "buffer_head_io.h"
  67. static struct kmem_cache *ocfs2_inode_cachep = NULL;
  68. struct kmem_cache *ocfs2_dquot_cachep;
  69. struct kmem_cache *ocfs2_qf_chunk_cachep;
  70. /* OCFS2 needs to schedule several differnt types of work which
  71. * require cluster locking, disk I/O, recovery waits, etc. Since these
  72. * types of work tend to be heavy we avoid using the kernel events
  73. * workqueue and schedule on our own. */
  74. struct workqueue_struct *ocfs2_wq = NULL;
  75. static struct dentry *ocfs2_debugfs_root = NULL;
  76. MODULE_AUTHOR("Oracle");
  77. MODULE_LICENSE("GPL");
  78. struct mount_options
  79. {
  80. unsigned long commit_interval;
  81. unsigned long mount_opt;
  82. unsigned int atime_quantum;
  83. signed short slot;
  84. unsigned int localalloc_opt;
  85. char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
  86. };
  87. static int ocfs2_parse_options(struct super_block *sb, char *options,
  88. struct mount_options *mopt,
  89. int is_remount);
  90. static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);
  91. static void ocfs2_put_super(struct super_block *sb);
  92. static int ocfs2_mount_volume(struct super_block *sb);
  93. static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
  94. static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
  95. static int ocfs2_initialize_mem_caches(void);
  96. static void ocfs2_free_mem_caches(void);
  97. static void ocfs2_delete_osb(struct ocfs2_super *osb);
  98. static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
  99. static int ocfs2_sync_fs(struct super_block *sb, int wait);
  100. static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
  101. static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
  102. static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
  103. static int ocfs2_check_volume(struct ocfs2_super *osb);
  104. static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  105. struct buffer_head *bh,
  106. u32 sectsize);
  107. static int ocfs2_initialize_super(struct super_block *sb,
  108. struct buffer_head *bh,
  109. int sector_size);
  110. static int ocfs2_get_sector(struct super_block *sb,
  111. struct buffer_head **bh,
  112. int block,
  113. int sect_size);
  114. static void ocfs2_write_super(struct super_block *sb);
  115. static struct inode *ocfs2_alloc_inode(struct super_block *sb);
  116. static void ocfs2_destroy_inode(struct inode *inode);
  117. static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
  118. static int ocfs2_enable_quotas(struct ocfs2_super *osb);
  119. static void ocfs2_disable_quotas(struct ocfs2_super *osb);
  120. static const struct super_operations ocfs2_sops = {
  121. .statfs = ocfs2_statfs,
  122. .alloc_inode = ocfs2_alloc_inode,
  123. .destroy_inode = ocfs2_destroy_inode,
  124. .drop_inode = ocfs2_drop_inode,
  125. .clear_inode = ocfs2_clear_inode,
  126. .delete_inode = ocfs2_delete_inode,
  127. .sync_fs = ocfs2_sync_fs,
  128. .write_super = ocfs2_write_super,
  129. .put_super = ocfs2_put_super,
  130. .remount_fs = ocfs2_remount,
  131. .show_options = ocfs2_show_options,
  132. .quota_read = ocfs2_quota_read,
  133. .quota_write = ocfs2_quota_write,
  134. };
  135. enum {
  136. Opt_barrier,
  137. Opt_err_panic,
  138. Opt_err_ro,
  139. Opt_intr,
  140. Opt_nointr,
  141. Opt_hb_none,
  142. Opt_hb_local,
  143. Opt_data_ordered,
  144. Opt_data_writeback,
  145. Opt_atime_quantum,
  146. Opt_slot,
  147. Opt_commit,
  148. Opt_localalloc,
  149. Opt_localflocks,
  150. Opt_stack,
  151. Opt_user_xattr,
  152. Opt_nouser_xattr,
  153. Opt_inode64,
  154. Opt_acl,
  155. Opt_noacl,
  156. Opt_usrquota,
  157. Opt_grpquota,
  158. Opt_err,
  159. };
  160. static const match_table_t tokens = {
  161. {Opt_barrier, "barrier=%u"},
  162. {Opt_err_panic, "errors=panic"},
  163. {Opt_err_ro, "errors=remount-ro"},
  164. {Opt_intr, "intr"},
  165. {Opt_nointr, "nointr"},
  166. {Opt_hb_none, OCFS2_HB_NONE},
  167. {Opt_hb_local, OCFS2_HB_LOCAL},
  168. {Opt_data_ordered, "data=ordered"},
  169. {Opt_data_writeback, "data=writeback"},
  170. {Opt_atime_quantum, "atime_quantum=%u"},
  171. {Opt_slot, "preferred_slot=%u"},
  172. {Opt_commit, "commit=%u"},
  173. {Opt_localalloc, "localalloc=%d"},
  174. {Opt_localflocks, "localflocks"},
  175. {Opt_stack, "cluster_stack=%s"},
  176. {Opt_user_xattr, "user_xattr"},
  177. {Opt_nouser_xattr, "nouser_xattr"},
  178. {Opt_inode64, "inode64"},
  179. {Opt_acl, "acl"},
  180. {Opt_noacl, "noacl"},
  181. {Opt_usrquota, "usrquota"},
  182. {Opt_grpquota, "grpquota"},
  183. {Opt_err, NULL}
  184. };
  185. /*
  186. * write_super and sync_fs ripped right out of ext3.
  187. */
  188. static void ocfs2_write_super(struct super_block *sb)
  189. {
  190. if (mutex_trylock(&sb->s_lock) != 0)
  191. BUG();
  192. sb->s_dirt = 0;
  193. }
  194. static int ocfs2_sync_fs(struct super_block *sb, int wait)
  195. {
  196. int status;
  197. tid_t target;
  198. struct ocfs2_super *osb = OCFS2_SB(sb);
  199. sb->s_dirt = 0;
  200. if (ocfs2_is_hard_readonly(osb))
  201. return -EROFS;
  202. if (wait) {
  203. status = ocfs2_flush_truncate_log(osb);
  204. if (status < 0)
  205. mlog_errno(status);
  206. } else {
  207. ocfs2_schedule_truncate_log_flush(osb, 0);
  208. }
  209. if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
  210. &target)) {
  211. if (wait)
  212. jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
  213. target);
  214. }
  215. return 0;
  216. }
  217. static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
  218. {
  219. if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
  220. && (ino == USER_QUOTA_SYSTEM_INODE
  221. || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
  222. return 0;
  223. if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
  224. && (ino == GROUP_QUOTA_SYSTEM_INODE
  225. || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
  226. return 0;
  227. return 1;
  228. }
  229. static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
  230. {
  231. struct inode *new = NULL;
  232. int status = 0;
  233. int i;
  234. mlog_entry_void();
  235. new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
  236. if (IS_ERR(new)) {
  237. status = PTR_ERR(new);
  238. mlog_errno(status);
  239. goto bail;
  240. }
  241. osb->root_inode = new;
  242. new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
  243. if (IS_ERR(new)) {
  244. status = PTR_ERR(new);
  245. mlog_errno(status);
  246. goto bail;
  247. }
  248. osb->sys_root_inode = new;
  249. for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
  250. i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
  251. if (!ocfs2_need_system_inode(osb, i))
  252. continue;
  253. new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
  254. if (!new) {
  255. ocfs2_release_system_inodes(osb);
  256. status = -EINVAL;
  257. mlog_errno(status);
  258. /* FIXME: Should ERROR_RO_FS */
  259. mlog(ML_ERROR, "Unable to load system inode %d, "
  260. "possibly corrupt fs?", i);
  261. goto bail;
  262. }
  263. // the array now has one ref, so drop this one
  264. iput(new);
  265. }
  266. bail:
  267. mlog_exit(status);
  268. return status;
  269. }
  270. static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
  271. {
  272. struct inode *new = NULL;
  273. int status = 0;
  274. int i;
  275. mlog_entry_void();
  276. for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
  277. i < NUM_SYSTEM_INODES;
  278. i++) {
  279. if (!ocfs2_need_system_inode(osb, i))
  280. continue;
  281. new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
  282. if (!new) {
  283. ocfs2_release_system_inodes(osb);
  284. status = -EINVAL;
  285. mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
  286. status, i, osb->slot_num);
  287. goto bail;
  288. }
  289. /* the array now has one ref, so drop this one */
  290. iput(new);
  291. }
  292. bail:
  293. mlog_exit(status);
  294. return status;
  295. }
  296. static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
  297. {
  298. int i;
  299. struct inode *inode;
  300. mlog_entry_void();
  301. for (i = 0; i < NUM_SYSTEM_INODES; i++) {
  302. inode = osb->system_inodes[i];
  303. if (inode) {
  304. iput(inode);
  305. osb->system_inodes[i] = NULL;
  306. }
  307. }
  308. inode = osb->sys_root_inode;
  309. if (inode) {
  310. iput(inode);
  311. osb->sys_root_inode = NULL;
  312. }
  313. inode = osb->root_inode;
  314. if (inode) {
  315. iput(inode);
  316. osb->root_inode = NULL;
  317. }
  318. mlog_exit(0);
  319. }
  320. /* We're allocating fs objects, use GFP_NOFS */
  321. static struct inode *ocfs2_alloc_inode(struct super_block *sb)
  322. {
  323. struct ocfs2_inode_info *oi;
  324. oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
  325. if (!oi)
  326. return NULL;
  327. jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
  328. return &oi->vfs_inode;
  329. }
  330. static void ocfs2_destroy_inode(struct inode *inode)
  331. {
  332. kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
  333. }
  334. static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
  335. unsigned int cbits)
  336. {
  337. unsigned int bytes = 1 << cbits;
  338. unsigned int trim = bytes;
  339. unsigned int bitshift = 32;
  340. /*
  341. * i_size and all block offsets in ocfs2 are always 64 bits
  342. * wide. i_clusters is 32 bits, in cluster-sized units. So on
  343. * 64 bit platforms, cluster size will be the limiting factor.
  344. */
  345. #if BITS_PER_LONG == 32
  346. # if defined(CONFIG_LBD)
  347. BUILD_BUG_ON(sizeof(sector_t) != 8);
  348. /*
  349. * We might be limited by page cache size.
  350. */
  351. if (bytes > PAGE_CACHE_SIZE) {
  352. bytes = PAGE_CACHE_SIZE;
  353. trim = 1;
  354. /*
  355. * Shift by 31 here so that we don't get larger than
  356. * MAX_LFS_FILESIZE
  357. */
  358. bitshift = 31;
  359. }
  360. # else
  361. /*
  362. * We are limited by the size of sector_t. Use block size, as
  363. * that's what we expose to the VFS.
  364. */
  365. bytes = 1 << bbits;
  366. trim = 1;
  367. bitshift = 31;
  368. # endif
  369. #endif
  370. /*
  371. * Trim by a whole cluster when we can actually approach the
  372. * on-disk limits. Otherwise we can overflow i_clusters when
  373. * an extent start is at the max offset.
  374. */
  375. return (((unsigned long long)bytes) << bitshift) - trim;
  376. }
  377. static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
  378. {
  379. int incompat_features;
  380. int ret = 0;
  381. struct mount_options parsed_options;
  382. struct ocfs2_super *osb = OCFS2_SB(sb);
  383. if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) {
  384. ret = -EINVAL;
  385. goto out;
  386. }
  387. if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) !=
  388. (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
  389. ret = -EINVAL;
  390. mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
  391. goto out;
  392. }
  393. if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
  394. (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
  395. ret = -EINVAL;
  396. mlog(ML_ERROR, "Cannot change data mode on remount\n");
  397. goto out;
  398. }
  399. /* Probably don't want this on remount; it might
  400. * mess with other nodes */
  401. if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
  402. (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
  403. ret = -EINVAL;
  404. mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
  405. goto out;
  406. }
  407. /* We're going to/from readonly mode. */
  408. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
  409. /* Disable quota accounting before remounting RO */
  410. if (*flags & MS_RDONLY) {
  411. ret = ocfs2_susp_quotas(osb, 0);
  412. if (ret < 0)
  413. goto out;
  414. }
  415. /* Lock here so the check of HARD_RO and the potential
  416. * setting of SOFT_RO is atomic. */
  417. spin_lock(&osb->osb_lock);
  418. if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
  419. mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
  420. ret = -EROFS;
  421. goto unlock_osb;
  422. }
  423. if (*flags & MS_RDONLY) {
  424. mlog(0, "Going to ro mode.\n");
  425. sb->s_flags |= MS_RDONLY;
  426. osb->osb_flags |= OCFS2_OSB_SOFT_RO;
  427. } else {
  428. mlog(0, "Making ro filesystem writeable.\n");
  429. if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
  430. mlog(ML_ERROR, "Cannot remount RDWR "
  431. "filesystem due to previous errors.\n");
  432. ret = -EROFS;
  433. goto unlock_osb;
  434. }
  435. incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
  436. if (incompat_features) {
  437. mlog(ML_ERROR, "Cannot remount RDWR because "
  438. "of unsupported optional features "
  439. "(%x).\n", incompat_features);
  440. ret = -EINVAL;
  441. goto unlock_osb;
  442. }
  443. sb->s_flags &= ~MS_RDONLY;
  444. osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
  445. }
  446. unlock_osb:
  447. spin_unlock(&osb->osb_lock);
  448. /* Enable quota accounting after remounting RW */
  449. if (!ret && !(*flags & MS_RDONLY)) {
  450. if (sb_any_quota_suspended(sb))
  451. ret = ocfs2_susp_quotas(osb, 1);
  452. else
  453. ret = ocfs2_enable_quotas(osb);
  454. if (ret < 0) {
  455. /* Return back changes... */
  456. spin_lock(&osb->osb_lock);
  457. sb->s_flags |= MS_RDONLY;
  458. osb->osb_flags |= OCFS2_OSB_SOFT_RO;
  459. spin_unlock(&osb->osb_lock);
  460. goto out;
  461. }
  462. }
  463. }
  464. if (!ret) {
  465. /* Only save off the new mount options in case of a successful
  466. * remount. */
  467. if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
  468. parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
  469. osb->s_mount_opt = parsed_options.mount_opt;
  470. osb->s_atime_quantum = parsed_options.atime_quantum;
  471. osb->preferred_slot = parsed_options.slot;
  472. if (parsed_options.commit_interval)
  473. osb->osb_commit_interval = parsed_options.commit_interval;
  474. if (!ocfs2_is_hard_readonly(osb))
  475. ocfs2_set_journal_params(osb);
  476. }
  477. out:
  478. return ret;
  479. }
  480. static int ocfs2_sb_probe(struct super_block *sb,
  481. struct buffer_head **bh,
  482. int *sector_size)
  483. {
  484. int status, tmpstat;
  485. struct ocfs1_vol_disk_hdr *hdr;
  486. struct ocfs2_dinode *di;
  487. int blksize;
  488. *bh = NULL;
  489. /* may be > 512 */
  490. *sector_size = bdev_hardsect_size(sb->s_bdev);
  491. if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
  492. mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
  493. *sector_size, OCFS2_MAX_BLOCKSIZE);
  494. status = -EINVAL;
  495. goto bail;
  496. }
  497. /* Can this really happen? */
  498. if (*sector_size < OCFS2_MIN_BLOCKSIZE)
  499. *sector_size = OCFS2_MIN_BLOCKSIZE;
  500. /* check block zero for old format */
  501. status = ocfs2_get_sector(sb, bh, 0, *sector_size);
  502. if (status < 0) {
  503. mlog_errno(status);
  504. goto bail;
  505. }
  506. hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
  507. if (hdr->major_version == OCFS1_MAJOR_VERSION) {
  508. mlog(ML_ERROR, "incompatible version: %u.%u\n",
  509. hdr->major_version, hdr->minor_version);
  510. status = -EINVAL;
  511. }
  512. if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
  513. strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
  514. mlog(ML_ERROR, "incompatible volume signature: %8s\n",
  515. hdr->signature);
  516. status = -EINVAL;
  517. }
  518. brelse(*bh);
  519. *bh = NULL;
  520. if (status < 0) {
  521. mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
  522. "upgraded before mounting with ocfs v2\n");
  523. goto bail;
  524. }
  525. /*
  526. * Now check at magic offset for 512, 1024, 2048, 4096
  527. * blocksizes. 4096 is the maximum blocksize because it is
  528. * the minimum clustersize.
  529. */
  530. status = -EINVAL;
  531. for (blksize = *sector_size;
  532. blksize <= OCFS2_MAX_BLOCKSIZE;
  533. blksize <<= 1) {
  534. tmpstat = ocfs2_get_sector(sb, bh,
  535. OCFS2_SUPER_BLOCK_BLKNO,
  536. blksize);
  537. if (tmpstat < 0) {
  538. status = tmpstat;
  539. mlog_errno(status);
  540. goto bail;
  541. }
  542. di = (struct ocfs2_dinode *) (*bh)->b_data;
  543. status = ocfs2_verify_volume(di, *bh, blksize);
  544. if (status >= 0)
  545. goto bail;
  546. brelse(*bh);
  547. *bh = NULL;
  548. if (status != -EAGAIN)
  549. break;
  550. }
  551. bail:
  552. return status;
  553. }
  554. static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
  555. {
  556. if (ocfs2_mount_local(osb)) {
  557. if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
  558. mlog(ML_ERROR, "Cannot heartbeat on a locally "
  559. "mounted device.\n");
  560. return -EINVAL;
  561. }
  562. }
  563. if (ocfs2_userspace_stack(osb)) {
  564. if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
  565. mlog(ML_ERROR, "Userspace stack expected, but "
  566. "o2cb heartbeat arguments passed to mount\n");
  567. return -EINVAL;
  568. }
  569. }
  570. if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {
  571. if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
  572. !ocfs2_userspace_stack(osb)) {
  573. mlog(ML_ERROR, "Heartbeat has to be started to mount "
  574. "a read-write clustered device.\n");
  575. return -EINVAL;
  576. }
  577. }
  578. return 0;
  579. }
  580. /*
  581. * If we're using a userspace stack, mount should have passed
  582. * a name that matches the disk. If not, mount should not
  583. * have passed a stack.
  584. */
  585. static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
  586. struct mount_options *mopt)
  587. {
  588. if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
  589. mlog(ML_ERROR,
  590. "cluster stack passed to mount, but this filesystem "
  591. "does not support it\n");
  592. return -EINVAL;
  593. }
  594. if (ocfs2_userspace_stack(osb) &&
  595. strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
  596. OCFS2_STACK_LABEL_LEN)) {
  597. mlog(ML_ERROR,
  598. "cluster stack passed to mount (\"%s\") does not "
  599. "match the filesystem (\"%s\")\n",
  600. mopt->cluster_stack,
  601. osb->osb_cluster_stack);
  602. return -EINVAL;
  603. }
  604. return 0;
  605. }
  606. static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
  607. {
  608. int type;
  609. struct super_block *sb = osb->sb;
  610. unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  611. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
  612. int status = 0;
  613. for (type = 0; type < MAXQUOTAS; type++) {
  614. if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
  615. continue;
  616. if (unsuspend)
  617. status = vfs_quota_enable(
  618. sb_dqopt(sb)->files[type],
  619. type, QFMT_OCFS2,
  620. DQUOT_SUSPENDED);
  621. else
  622. status = vfs_quota_disable(sb, type,
  623. DQUOT_SUSPENDED);
  624. if (status < 0)
  625. break;
  626. }
  627. if (status < 0)
  628. mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
  629. "remount (error = %d).\n", status);
  630. return status;
  631. }
  632. static int ocfs2_enable_quotas(struct ocfs2_super *osb)
  633. {
  634. struct inode *inode[MAXQUOTAS] = { NULL, NULL };
  635. struct super_block *sb = osb->sb;
  636. unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  637. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
  638. unsigned int ino[MAXQUOTAS] = { LOCAL_USER_QUOTA_SYSTEM_INODE,
  639. LOCAL_GROUP_QUOTA_SYSTEM_INODE };
  640. int status;
  641. int type;
  642. sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
  643. for (type = 0; type < MAXQUOTAS; type++) {
  644. if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
  645. continue;
  646. inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
  647. osb->slot_num);
  648. if (!inode[type]) {
  649. status = -ENOENT;
  650. goto out_quota_off;
  651. }
  652. status = vfs_quota_enable(inode[type], type, QFMT_OCFS2,
  653. DQUOT_USAGE_ENABLED);
  654. if (status < 0)
  655. goto out_quota_off;
  656. }
  657. for (type = 0; type < MAXQUOTAS; type++)
  658. iput(inode[type]);
  659. return 0;
  660. out_quota_off:
  661. ocfs2_disable_quotas(osb);
  662. for (type = 0; type < MAXQUOTAS; type++)
  663. iput(inode[type]);
  664. mlog_errno(status);
  665. return status;
  666. }
  667. static void ocfs2_disable_quotas(struct ocfs2_super *osb)
  668. {
  669. int type;
  670. struct inode *inode;
  671. struct super_block *sb = osb->sb;
  672. /* We mostly ignore errors in this function because there's not much
  673. * we can do when we see them */
  674. for (type = 0; type < MAXQUOTAS; type++) {
  675. if (!sb_has_quota_loaded(sb, type))
  676. continue;
  677. inode = igrab(sb->s_dquot.files[type]);
  678. /* Turn off quotas. This will remove all dquot structures from
  679. * memory and so they will be automatically synced to global
  680. * quota files */
  681. vfs_quota_disable(sb, type, DQUOT_USAGE_ENABLED |
  682. DQUOT_LIMITS_ENABLED);
  683. if (!inode)
  684. continue;
  685. iput(inode);
  686. }
  687. }
  688. /* Handle quota on quotactl */
  689. static int ocfs2_quota_on(struct super_block *sb, int type, int format_id,
  690. char *path, int remount)
  691. {
  692. unsigned int feature[MAXQUOTAS] = { OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  693. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
  694. if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
  695. return -EINVAL;
  696. if (remount)
  697. return 0; /* Just ignore it has been handled in
  698. * ocfs2_remount() */
  699. return vfs_quota_enable(sb_dqopt(sb)->files[type], type,
  700. format_id, DQUOT_LIMITS_ENABLED);
  701. }
  702. /* Handle quota off quotactl */
  703. static int ocfs2_quota_off(struct super_block *sb, int type, int remount)
  704. {
  705. if (remount)
  706. return 0; /* Ignore now and handle later in
  707. * ocfs2_remount() */
  708. return vfs_quota_disable(sb, type, DQUOT_LIMITS_ENABLED);
  709. }
  710. static struct quotactl_ops ocfs2_quotactl_ops = {
  711. .quota_on = ocfs2_quota_on,
  712. .quota_off = ocfs2_quota_off,
  713. .quota_sync = vfs_quota_sync,
  714. .get_info = vfs_get_dqinfo,
  715. .set_info = vfs_set_dqinfo,
  716. .get_dqblk = vfs_get_dqblk,
  717. .set_dqblk = vfs_set_dqblk,
  718. };
  719. static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
  720. {
  721. struct dentry *root;
  722. int status, sector_size;
  723. struct mount_options parsed_options;
  724. struct inode *inode = NULL;
  725. struct ocfs2_super *osb = NULL;
  726. struct buffer_head *bh = NULL;
  727. char nodestr[8];
  728. mlog_entry("%p, %p, %i", sb, data, silent);
  729. if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
  730. status = -EINVAL;
  731. goto read_super_error;
  732. }
  733. /* probe for superblock */
  734. status = ocfs2_sb_probe(sb, &bh, &sector_size);
  735. if (status < 0) {
  736. mlog(ML_ERROR, "superblock probe failed!\n");
  737. goto read_super_error;
  738. }
  739. status = ocfs2_initialize_super(sb, bh, sector_size);
  740. osb = OCFS2_SB(sb);
  741. if (status < 0) {
  742. mlog_errno(status);
  743. goto read_super_error;
  744. }
  745. brelse(bh);
  746. bh = NULL;
  747. if (!(osb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
  748. parsed_options.mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
  749. osb->s_mount_opt = parsed_options.mount_opt;
  750. osb->s_atime_quantum = parsed_options.atime_quantum;
  751. osb->preferred_slot = parsed_options.slot;
  752. osb->osb_commit_interval = parsed_options.commit_interval;
  753. osb->local_alloc_default_bits = ocfs2_megabytes_to_clusters(sb, parsed_options.localalloc_opt);
  754. osb->local_alloc_bits = osb->local_alloc_default_bits;
  755. if (osb->s_mount_opt & OCFS2_MOUNT_USRQUOTA &&
  756. !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  757. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  758. status = -EINVAL;
  759. mlog(ML_ERROR, "User quotas were requested, but this "
  760. "filesystem does not have the feature enabled.\n");
  761. goto read_super_error;
  762. }
  763. if (osb->s_mount_opt & OCFS2_MOUNT_GRPQUOTA &&
  764. !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  765. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  766. status = -EINVAL;
  767. mlog(ML_ERROR, "Group quotas were requested, but this "
  768. "filesystem does not have the feature enabled.\n");
  769. goto read_super_error;
  770. }
  771. status = ocfs2_verify_userspace_stack(osb, &parsed_options);
  772. if (status)
  773. goto read_super_error;
  774. sb->s_magic = OCFS2_SUPER_MAGIC;
  775. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  776. ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  777. /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
  778. * heartbeat=none */
  779. if (bdev_read_only(sb->s_bdev)) {
  780. if (!(sb->s_flags & MS_RDONLY)) {
  781. status = -EACCES;
  782. mlog(ML_ERROR, "Readonly device detected but readonly "
  783. "mount was not specified.\n");
  784. goto read_super_error;
  785. }
  786. /* You should not be able to start a local heartbeat
  787. * on a readonly device. */
  788. if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
  789. status = -EROFS;
  790. mlog(ML_ERROR, "Local heartbeat specified on readonly "
  791. "device.\n");
  792. goto read_super_error;
  793. }
  794. status = ocfs2_check_journals_nolocks(osb);
  795. if (status < 0) {
  796. if (status == -EROFS)
  797. mlog(ML_ERROR, "Recovery required on readonly "
  798. "file system, but write access is "
  799. "unavailable.\n");
  800. else
  801. mlog_errno(status);
  802. goto read_super_error;
  803. }
  804. ocfs2_set_ro_flag(osb, 1);
  805. printk(KERN_NOTICE "Readonly device detected. No cluster "
  806. "services will be utilized for this mount. Recovery "
  807. "will be skipped.\n");
  808. }
  809. if (!ocfs2_is_hard_readonly(osb)) {
  810. if (sb->s_flags & MS_RDONLY)
  811. ocfs2_set_ro_flag(osb, 0);
  812. }
  813. status = ocfs2_verify_heartbeat(osb);
  814. if (status < 0) {
  815. mlog_errno(status);
  816. goto read_super_error;
  817. }
  818. osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
  819. ocfs2_debugfs_root);
  820. if (!osb->osb_debug_root) {
  821. status = -EINVAL;
  822. mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
  823. goto read_super_error;
  824. }
  825. status = ocfs2_mount_volume(sb);
  826. if (osb->root_inode)
  827. inode = igrab(osb->root_inode);
  828. if (status < 0)
  829. goto read_super_error;
  830. if (!inode) {
  831. status = -EIO;
  832. mlog_errno(status);
  833. goto read_super_error;
  834. }
  835. root = d_alloc_root(inode);
  836. if (!root) {
  837. status = -ENOMEM;
  838. mlog_errno(status);
  839. goto read_super_error;
  840. }
  841. sb->s_root = root;
  842. ocfs2_complete_mount_recovery(osb);
  843. if (ocfs2_mount_local(osb))
  844. snprintf(nodestr, sizeof(nodestr), "local");
  845. else
  846. snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
  847. printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
  848. "with %s data mode.\n",
  849. osb->dev_str, nodestr, osb->slot_num,
  850. osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
  851. "ordered");
  852. atomic_set(&osb->vol_state, VOLUME_MOUNTED);
  853. wake_up(&osb->osb_mount_event);
  854. /* Now we can initialize quotas because we can afford to wait
  855. * for cluster locks recovery now. That also means that truncation
  856. * log recovery can happen but that waits for proper quota setup */
  857. if (!(sb->s_flags & MS_RDONLY)) {
  858. status = ocfs2_enable_quotas(osb);
  859. if (status < 0) {
  860. /* We have to err-out specially here because
  861. * s_root is already set */
  862. mlog_errno(status);
  863. atomic_set(&osb->vol_state, VOLUME_DISABLED);
  864. wake_up(&osb->osb_mount_event);
  865. mlog_exit(status);
  866. return status;
  867. }
  868. }
  869. ocfs2_complete_quota_recovery(osb);
  870. /* Now we wake up again for processes waiting for quotas */
  871. atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
  872. wake_up(&osb->osb_mount_event);
  873. mlog_exit(status);
  874. return status;
  875. read_super_error:
  876. brelse(bh);
  877. if (inode)
  878. iput(inode);
  879. if (osb) {
  880. atomic_set(&osb->vol_state, VOLUME_DISABLED);
  881. wake_up(&osb->osb_mount_event);
  882. ocfs2_dismount_volume(sb, 1);
  883. }
  884. mlog_exit(status);
  885. return status;
  886. }
  887. static int ocfs2_get_sb(struct file_system_type *fs_type,
  888. int flags,
  889. const char *dev_name,
  890. void *data,
  891. struct vfsmount *mnt)
  892. {
  893. return get_sb_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super,
  894. mnt);
  895. }
  896. static struct file_system_type ocfs2_fs_type = {
  897. .owner = THIS_MODULE,
  898. .name = "ocfs2",
  899. .get_sb = ocfs2_get_sb, /* is this called when we mount
  900. * the fs? */
  901. .kill_sb = kill_block_super, /* set to the generic one
  902. * right now, but do we
  903. * need to change that? */
  904. .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
  905. .next = NULL
  906. };
  907. static int ocfs2_parse_options(struct super_block *sb,
  908. char *options,
  909. struct mount_options *mopt,
  910. int is_remount)
  911. {
  912. int status;
  913. char *p;
  914. mlog_entry("remount: %d, options: \"%s\"\n", is_remount,
  915. options ? options : "(none)");
  916. mopt->commit_interval = 0;
  917. mopt->mount_opt = 0;
  918. mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
  919. mopt->slot = OCFS2_INVALID_SLOT;
  920. mopt->localalloc_opt = OCFS2_DEFAULT_LOCAL_ALLOC_SIZE;
  921. mopt->cluster_stack[0] = '\0';
  922. if (!options) {
  923. status = 1;
  924. goto bail;
  925. }
  926. while ((p = strsep(&options, ",")) != NULL) {
  927. int token, option;
  928. substring_t args[MAX_OPT_ARGS];
  929. if (!*p)
  930. continue;
  931. token = match_token(p, tokens, args);
  932. switch (token) {
  933. case Opt_hb_local:
  934. mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
  935. break;
  936. case Opt_hb_none:
  937. mopt->mount_opt &= ~OCFS2_MOUNT_HB_LOCAL;
  938. break;
  939. case Opt_barrier:
  940. if (match_int(&args[0], &option)) {
  941. status = 0;
  942. goto bail;
  943. }
  944. if (option)
  945. mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
  946. else
  947. mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
  948. break;
  949. case Opt_intr:
  950. mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
  951. break;
  952. case Opt_nointr:
  953. mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
  954. break;
  955. case Opt_err_panic:
  956. mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
  957. break;
  958. case Opt_err_ro:
  959. mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
  960. break;
  961. case Opt_data_ordered:
  962. mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
  963. break;
  964. case Opt_data_writeback:
  965. mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
  966. break;
  967. case Opt_user_xattr:
  968. mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
  969. break;
  970. case Opt_nouser_xattr:
  971. mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
  972. break;
  973. case Opt_atime_quantum:
  974. if (match_int(&args[0], &option)) {
  975. status = 0;
  976. goto bail;
  977. }
  978. if (option >= 0)
  979. mopt->atime_quantum = option;
  980. break;
  981. case Opt_slot:
  982. option = 0;
  983. if (match_int(&args[0], &option)) {
  984. status = 0;
  985. goto bail;
  986. }
  987. if (option)
  988. mopt->slot = (s16)option;
  989. break;
  990. case Opt_commit:
  991. option = 0;
  992. if (match_int(&args[0], &option)) {
  993. status = 0;
  994. goto bail;
  995. }
  996. if (option < 0)
  997. return 0;
  998. if (option == 0)
  999. option = JBD2_DEFAULT_MAX_COMMIT_AGE;
  1000. mopt->commit_interval = HZ * option;
  1001. break;
  1002. case Opt_localalloc:
  1003. option = 0;
  1004. if (match_int(&args[0], &option)) {
  1005. status = 0;
  1006. goto bail;
  1007. }
  1008. if (option >= 0 && (option <= ocfs2_local_alloc_size(sb) * 8))
  1009. mopt->localalloc_opt = option;
  1010. break;
  1011. case Opt_localflocks:
  1012. /*
  1013. * Changing this during remount could race
  1014. * flock() requests, or "unbalance" existing
  1015. * ones (e.g., a lock is taken in one mode but
  1016. * dropped in the other). If users care enough
  1017. * to flip locking modes during remount, we
  1018. * could add a "local" flag to individual
  1019. * flock structures for proper tracking of
  1020. * state.
  1021. */
  1022. if (!is_remount)
  1023. mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
  1024. break;
  1025. case Opt_stack:
  1026. /* Check both that the option we were passed
  1027. * is of the right length and that it is a proper
  1028. * string of the right length.
  1029. */
  1030. if (((args[0].to - args[0].from) !=
  1031. OCFS2_STACK_LABEL_LEN) ||
  1032. (strnlen(args[0].from,
  1033. OCFS2_STACK_LABEL_LEN) !=
  1034. OCFS2_STACK_LABEL_LEN)) {
  1035. mlog(ML_ERROR,
  1036. "Invalid cluster_stack option\n");
  1037. status = 0;
  1038. goto bail;
  1039. }
  1040. memcpy(mopt->cluster_stack, args[0].from,
  1041. OCFS2_STACK_LABEL_LEN);
  1042. mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
  1043. break;
  1044. case Opt_inode64:
  1045. mopt->mount_opt |= OCFS2_MOUNT_INODE64;
  1046. break;
  1047. case Opt_usrquota:
  1048. /* We check only on remount, otherwise features
  1049. * aren't yet initialized. */
  1050. if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1051. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  1052. mlog(ML_ERROR, "User quota requested but "
  1053. "filesystem feature is not set\n");
  1054. status = 0;
  1055. goto bail;
  1056. }
  1057. mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
  1058. break;
  1059. case Opt_grpquota:
  1060. if (is_remount && !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1061. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  1062. mlog(ML_ERROR, "Group quota requested but "
  1063. "filesystem feature is not set\n");
  1064. status = 0;
  1065. goto bail;
  1066. }
  1067. mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
  1068. break;
  1069. #ifdef CONFIG_OCFS2_FS_POSIX_ACL
  1070. case Opt_acl:
  1071. mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
  1072. break;
  1073. case Opt_noacl:
  1074. mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
  1075. break;
  1076. #else
  1077. case Opt_acl:
  1078. case Opt_noacl:
  1079. printk(KERN_INFO "ocfs2 (no)acl options not supported\n");
  1080. break;
  1081. #endif
  1082. default:
  1083. mlog(ML_ERROR,
  1084. "Unrecognized mount option \"%s\" "
  1085. "or missing value\n", p);
  1086. status = 0;
  1087. goto bail;
  1088. }
  1089. }
  1090. status = 1;
  1091. bail:
  1092. mlog_exit(status);
  1093. return status;
  1094. }
  1095. static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
  1096. {
  1097. struct ocfs2_super *osb = OCFS2_SB(mnt->mnt_sb);
  1098. unsigned long opts = osb->s_mount_opt;
  1099. unsigned int local_alloc_megs;
  1100. if (opts & OCFS2_MOUNT_HB_LOCAL)
  1101. seq_printf(s, ",_netdev,heartbeat=local");
  1102. else
  1103. seq_printf(s, ",heartbeat=none");
  1104. if (opts & OCFS2_MOUNT_NOINTR)
  1105. seq_printf(s, ",nointr");
  1106. if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
  1107. seq_printf(s, ",data=writeback");
  1108. else
  1109. seq_printf(s, ",data=ordered");
  1110. if (opts & OCFS2_MOUNT_BARRIER)
  1111. seq_printf(s, ",barrier=1");
  1112. if (opts & OCFS2_MOUNT_ERRORS_PANIC)
  1113. seq_printf(s, ",errors=panic");
  1114. else
  1115. seq_printf(s, ",errors=remount-ro");
  1116. if (osb->preferred_slot != OCFS2_INVALID_SLOT)
  1117. seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
  1118. if (osb->s_atime_quantum != OCFS2_DEFAULT_ATIME_QUANTUM)
  1119. seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
  1120. if (osb->osb_commit_interval)
  1121. seq_printf(s, ",commit=%u",
  1122. (unsigned) (osb->osb_commit_interval / HZ));
  1123. local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
  1124. if (local_alloc_megs != OCFS2_DEFAULT_LOCAL_ALLOC_SIZE)
  1125. seq_printf(s, ",localalloc=%d", local_alloc_megs);
  1126. if (opts & OCFS2_MOUNT_LOCALFLOCKS)
  1127. seq_printf(s, ",localflocks,");
  1128. if (osb->osb_cluster_stack[0])
  1129. seq_printf(s, ",cluster_stack=%.*s", OCFS2_STACK_LABEL_LEN,
  1130. osb->osb_cluster_stack);
  1131. if (opts & OCFS2_MOUNT_USRQUOTA)
  1132. seq_printf(s, ",usrquota");
  1133. if (opts & OCFS2_MOUNT_GRPQUOTA)
  1134. seq_printf(s, ",grpquota");
  1135. if (opts & OCFS2_MOUNT_NOUSERXATTR)
  1136. seq_printf(s, ",nouser_xattr");
  1137. else
  1138. seq_printf(s, ",user_xattr");
  1139. if (opts & OCFS2_MOUNT_INODE64)
  1140. seq_printf(s, ",inode64");
  1141. #ifdef CONFIG_OCFS2_FS_POSIX_ACL
  1142. if (opts & OCFS2_MOUNT_POSIX_ACL)
  1143. seq_printf(s, ",acl");
  1144. else
  1145. seq_printf(s, ",noacl");
  1146. #endif
  1147. return 0;
  1148. }
  1149. static int __init ocfs2_init(void)
  1150. {
  1151. int status;
  1152. mlog_entry_void();
  1153. ocfs2_print_version();
  1154. status = init_ocfs2_uptodate_cache();
  1155. if (status < 0) {
  1156. mlog_errno(status);
  1157. goto leave;
  1158. }
  1159. status = ocfs2_initialize_mem_caches();
  1160. if (status < 0) {
  1161. mlog_errno(status);
  1162. goto leave;
  1163. }
  1164. ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
  1165. if (!ocfs2_wq) {
  1166. status = -ENOMEM;
  1167. goto leave;
  1168. }
  1169. ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
  1170. if (!ocfs2_debugfs_root) {
  1171. status = -EFAULT;
  1172. mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
  1173. }
  1174. status = ocfs2_quota_setup();
  1175. if (status)
  1176. goto leave;
  1177. ocfs2_set_locking_protocol();
  1178. status = register_quota_format(&ocfs2_quota_format);
  1179. leave:
  1180. if (status < 0) {
  1181. ocfs2_quota_shutdown();
  1182. ocfs2_free_mem_caches();
  1183. exit_ocfs2_uptodate_cache();
  1184. }
  1185. mlog_exit(status);
  1186. if (status >= 0) {
  1187. return register_filesystem(&ocfs2_fs_type);
  1188. } else
  1189. return -1;
  1190. }
  1191. static void __exit ocfs2_exit(void)
  1192. {
  1193. mlog_entry_void();
  1194. ocfs2_quota_shutdown();
  1195. if (ocfs2_wq) {
  1196. flush_workqueue(ocfs2_wq);
  1197. destroy_workqueue(ocfs2_wq);
  1198. }
  1199. unregister_quota_format(&ocfs2_quota_format);
  1200. debugfs_remove(ocfs2_debugfs_root);
  1201. ocfs2_free_mem_caches();
  1202. unregister_filesystem(&ocfs2_fs_type);
  1203. exit_ocfs2_uptodate_cache();
  1204. mlog_exit_void();
  1205. }
  1206. static void ocfs2_put_super(struct super_block *sb)
  1207. {
  1208. mlog_entry("(0x%p)\n", sb);
  1209. ocfs2_sync_blockdev(sb);
  1210. ocfs2_dismount_volume(sb, 0);
  1211. mlog_exit_void();
  1212. }
  1213. static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  1214. {
  1215. struct ocfs2_super *osb;
  1216. u32 numbits, freebits;
  1217. int status;
  1218. struct ocfs2_dinode *bm_lock;
  1219. struct buffer_head *bh = NULL;
  1220. struct inode *inode = NULL;
  1221. mlog_entry("(%p, %p)\n", dentry->d_sb, buf);
  1222. osb = OCFS2_SB(dentry->d_sb);
  1223. inode = ocfs2_get_system_file_inode(osb,
  1224. GLOBAL_BITMAP_SYSTEM_INODE,
  1225. OCFS2_INVALID_SLOT);
  1226. if (!inode) {
  1227. mlog(ML_ERROR, "failed to get bitmap inode\n");
  1228. status = -EIO;
  1229. goto bail;
  1230. }
  1231. status = ocfs2_inode_lock(inode, &bh, 0);
  1232. if (status < 0) {
  1233. mlog_errno(status);
  1234. goto bail;
  1235. }
  1236. bm_lock = (struct ocfs2_dinode *) bh->b_data;
  1237. numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
  1238. freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
  1239. buf->f_type = OCFS2_SUPER_MAGIC;
  1240. buf->f_bsize = dentry->d_sb->s_blocksize;
  1241. buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
  1242. buf->f_blocks = ((sector_t) numbits) *
  1243. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  1244. buf->f_bfree = ((sector_t) freebits) *
  1245. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  1246. buf->f_bavail = buf->f_bfree;
  1247. buf->f_files = numbits;
  1248. buf->f_ffree = freebits;
  1249. brelse(bh);
  1250. ocfs2_inode_unlock(inode, 0);
  1251. status = 0;
  1252. bail:
  1253. if (inode)
  1254. iput(inode);
  1255. mlog_exit(status);
  1256. return status;
  1257. }
  1258. static void ocfs2_inode_init_once(void *data)
  1259. {
  1260. struct ocfs2_inode_info *oi = data;
  1261. oi->ip_flags = 0;
  1262. oi->ip_open_count = 0;
  1263. spin_lock_init(&oi->ip_lock);
  1264. ocfs2_extent_map_init(&oi->vfs_inode);
  1265. INIT_LIST_HEAD(&oi->ip_io_markers);
  1266. oi->ip_created_trans = 0;
  1267. oi->ip_last_trans = 0;
  1268. oi->ip_dir_start_lookup = 0;
  1269. init_rwsem(&oi->ip_alloc_sem);
  1270. init_rwsem(&oi->ip_xattr_sem);
  1271. mutex_init(&oi->ip_io_mutex);
  1272. oi->ip_blkno = 0ULL;
  1273. oi->ip_clusters = 0;
  1274. ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
  1275. ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
  1276. ocfs2_lock_res_init_once(&oi->ip_open_lockres);
  1277. ocfs2_metadata_cache_init(&oi->vfs_inode);
  1278. inode_init_once(&oi->vfs_inode);
  1279. }
  1280. static int ocfs2_initialize_mem_caches(void)
  1281. {
  1282. ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
  1283. sizeof(struct ocfs2_inode_info),
  1284. 0,
  1285. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1286. SLAB_MEM_SPREAD),
  1287. ocfs2_inode_init_once);
  1288. ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
  1289. sizeof(struct ocfs2_dquot),
  1290. 0,
  1291. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1292. SLAB_MEM_SPREAD),
  1293. NULL);
  1294. ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
  1295. sizeof(struct ocfs2_quota_chunk),
  1296. 0,
  1297. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  1298. NULL);
  1299. if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
  1300. !ocfs2_qf_chunk_cachep) {
  1301. if (ocfs2_inode_cachep)
  1302. kmem_cache_destroy(ocfs2_inode_cachep);
  1303. if (ocfs2_dquot_cachep)
  1304. kmem_cache_destroy(ocfs2_dquot_cachep);
  1305. if (ocfs2_qf_chunk_cachep)
  1306. kmem_cache_destroy(ocfs2_qf_chunk_cachep);
  1307. return -ENOMEM;
  1308. }
  1309. return 0;
  1310. }
  1311. static void ocfs2_free_mem_caches(void)
  1312. {
  1313. if (ocfs2_inode_cachep)
  1314. kmem_cache_destroy(ocfs2_inode_cachep);
  1315. ocfs2_inode_cachep = NULL;
  1316. if (ocfs2_dquot_cachep)
  1317. kmem_cache_destroy(ocfs2_dquot_cachep);
  1318. ocfs2_dquot_cachep = NULL;
  1319. if (ocfs2_qf_chunk_cachep)
  1320. kmem_cache_destroy(ocfs2_qf_chunk_cachep);
  1321. ocfs2_qf_chunk_cachep = NULL;
  1322. }
  1323. static int ocfs2_get_sector(struct super_block *sb,
  1324. struct buffer_head **bh,
  1325. int block,
  1326. int sect_size)
  1327. {
  1328. if (!sb_set_blocksize(sb, sect_size)) {
  1329. mlog(ML_ERROR, "unable to set blocksize\n");
  1330. return -EIO;
  1331. }
  1332. *bh = sb_getblk(sb, block);
  1333. if (!*bh) {
  1334. mlog_errno(-EIO);
  1335. return -EIO;
  1336. }
  1337. lock_buffer(*bh);
  1338. if (!buffer_dirty(*bh))
  1339. clear_buffer_uptodate(*bh);
  1340. unlock_buffer(*bh);
  1341. ll_rw_block(READ, 1, bh);
  1342. wait_on_buffer(*bh);
  1343. if (!buffer_uptodate(*bh)) {
  1344. mlog_errno(-EIO);
  1345. brelse(*bh);
  1346. *bh = NULL;
  1347. return -EIO;
  1348. }
  1349. return 0;
  1350. }
  1351. static int ocfs2_mount_volume(struct super_block *sb)
  1352. {
  1353. int status = 0;
  1354. int unlock_super = 0;
  1355. struct ocfs2_super *osb = OCFS2_SB(sb);
  1356. mlog_entry_void();
  1357. if (ocfs2_is_hard_readonly(osb))
  1358. goto leave;
  1359. status = ocfs2_dlm_init(osb);
  1360. if (status < 0) {
  1361. mlog_errno(status);
  1362. goto leave;
  1363. }
  1364. status = ocfs2_super_lock(osb, 1);
  1365. if (status < 0) {
  1366. mlog_errno(status);
  1367. goto leave;
  1368. }
  1369. unlock_super = 1;
  1370. /* This will load up the node map and add ourselves to it. */
  1371. status = ocfs2_find_slot(osb);
  1372. if (status < 0) {
  1373. mlog_errno(status);
  1374. goto leave;
  1375. }
  1376. /* load all node-local system inodes */
  1377. status = ocfs2_init_local_system_inodes(osb);
  1378. if (status < 0) {
  1379. mlog_errno(status);
  1380. goto leave;
  1381. }
  1382. status = ocfs2_check_volume(osb);
  1383. if (status < 0) {
  1384. mlog_errno(status);
  1385. goto leave;
  1386. }
  1387. status = ocfs2_truncate_log_init(osb);
  1388. if (status < 0) {
  1389. mlog_errno(status);
  1390. goto leave;
  1391. }
  1392. if (ocfs2_mount_local(osb))
  1393. goto leave;
  1394. leave:
  1395. if (unlock_super)
  1396. ocfs2_super_unlock(osb, 1);
  1397. mlog_exit(status);
  1398. return status;
  1399. }
  1400. static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
  1401. {
  1402. int tmp, hangup_needed = 0;
  1403. struct ocfs2_super *osb = NULL;
  1404. char nodestr[8];
  1405. mlog_entry("(0x%p)\n", sb);
  1406. BUG_ON(!sb);
  1407. osb = OCFS2_SB(sb);
  1408. BUG_ON(!osb);
  1409. ocfs2_disable_quotas(osb);
  1410. ocfs2_shutdown_local_alloc(osb);
  1411. ocfs2_truncate_log_shutdown(osb);
  1412. /* This will disable recovery and flush any recovery work. */
  1413. ocfs2_recovery_exit(osb);
  1414. ocfs2_journal_shutdown(osb);
  1415. ocfs2_sync_blockdev(sb);
  1416. /* No cluster connection means we've failed during mount, so skip
  1417. * all the steps which depended on that to complete. */
  1418. if (osb->cconn) {
  1419. tmp = ocfs2_super_lock(osb, 1);
  1420. if (tmp < 0) {
  1421. mlog_errno(tmp);
  1422. return;
  1423. }
  1424. }
  1425. if (osb->slot_num != OCFS2_INVALID_SLOT)
  1426. ocfs2_put_slot(osb);
  1427. if (osb->cconn)
  1428. ocfs2_super_unlock(osb, 1);
  1429. ocfs2_release_system_inodes(osb);
  1430. /*
  1431. * If we're dismounting due to mount error, mount.ocfs2 will clean
  1432. * up heartbeat. If we're a local mount, there is no heartbeat.
  1433. * If we failed before we got a uuid_str yet, we can't stop
  1434. * heartbeat. Otherwise, do it.
  1435. */
  1436. if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str)
  1437. hangup_needed = 1;
  1438. if (osb->cconn)
  1439. ocfs2_dlm_shutdown(osb, hangup_needed);
  1440. debugfs_remove(osb->osb_debug_root);
  1441. if (hangup_needed)
  1442. ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
  1443. atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
  1444. if (ocfs2_mount_local(osb))
  1445. snprintf(nodestr, sizeof(nodestr), "local");
  1446. else
  1447. snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
  1448. printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
  1449. osb->dev_str, nodestr);
  1450. ocfs2_delete_osb(osb);
  1451. kfree(osb);
  1452. sb->s_dev = 0;
  1453. sb->s_fs_info = NULL;
  1454. }
  1455. static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
  1456. unsigned uuid_bytes)
  1457. {
  1458. int i, ret;
  1459. char *ptr;
  1460. BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
  1461. osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
  1462. if (osb->uuid_str == NULL)
  1463. return -ENOMEM;
  1464. for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
  1465. /* print with null */
  1466. ret = snprintf(ptr, 3, "%02X", uuid[i]);
  1467. if (ret != 2) /* drop super cleans up */
  1468. return -EINVAL;
  1469. /* then only advance past the last char */
  1470. ptr += 2;
  1471. }
  1472. return 0;
  1473. }
  1474. static int ocfs2_initialize_super(struct super_block *sb,
  1475. struct buffer_head *bh,
  1476. int sector_size)
  1477. {
  1478. int status;
  1479. int i, cbits, bbits;
  1480. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1481. struct inode *inode = NULL;
  1482. struct ocfs2_journal *journal;
  1483. __le32 uuid_net_key;
  1484. struct ocfs2_super *osb;
  1485. mlog_entry_void();
  1486. osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
  1487. if (!osb) {
  1488. status = -ENOMEM;
  1489. mlog_errno(status);
  1490. goto bail;
  1491. }
  1492. sb->s_fs_info = osb;
  1493. sb->s_op = &ocfs2_sops;
  1494. sb->s_export_op = &ocfs2_export_ops;
  1495. sb->s_qcop = &ocfs2_quotactl_ops;
  1496. sb->dq_op = &ocfs2_quota_operations;
  1497. sb->s_xattr = ocfs2_xattr_handlers;
  1498. sb->s_time_gran = 1;
  1499. sb->s_flags |= MS_NOATIME;
  1500. /* this is needed to support O_LARGEFILE */
  1501. cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
  1502. bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
  1503. sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
  1504. osb->sb = sb;
  1505. /* Save off for ocfs2_rw_direct */
  1506. osb->s_sectsize_bits = blksize_bits(sector_size);
  1507. BUG_ON(!osb->s_sectsize_bits);
  1508. spin_lock_init(&osb->dc_task_lock);
  1509. init_waitqueue_head(&osb->dc_event);
  1510. osb->dc_work_sequence = 0;
  1511. osb->dc_wake_sequence = 0;
  1512. INIT_LIST_HEAD(&osb->blocked_lock_list);
  1513. osb->blocked_lock_count = 0;
  1514. spin_lock_init(&osb->osb_lock);
  1515. spin_lock_init(&osb->osb_xattr_lock);
  1516. ocfs2_init_inode_steal_slot(osb);
  1517. atomic_set(&osb->alloc_stats.moves, 0);
  1518. atomic_set(&osb->alloc_stats.local_data, 0);
  1519. atomic_set(&osb->alloc_stats.bitmap_data, 0);
  1520. atomic_set(&osb->alloc_stats.bg_allocs, 0);
  1521. atomic_set(&osb->alloc_stats.bg_extends, 0);
  1522. ocfs2_init_node_maps(osb);
  1523. snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
  1524. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1525. status = ocfs2_recovery_init(osb);
  1526. if (status) {
  1527. mlog(ML_ERROR, "Unable to initialize recovery state\n");
  1528. mlog_errno(status);
  1529. goto bail;
  1530. }
  1531. init_waitqueue_head(&osb->checkpoint_event);
  1532. atomic_set(&osb->needs_checkpoint, 0);
  1533. osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
  1534. osb->slot_num = OCFS2_INVALID_SLOT;
  1535. osb->s_xattr_inline_size = le16_to_cpu(
  1536. di->id2.i_super.s_xattr_inline_size);
  1537. osb->local_alloc_state = OCFS2_LA_UNUSED;
  1538. osb->local_alloc_bh = NULL;
  1539. INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
  1540. init_waitqueue_head(&osb->osb_mount_event);
  1541. osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
  1542. if (!osb->vol_label) {
  1543. mlog(ML_ERROR, "unable to alloc vol label\n");
  1544. status = -ENOMEM;
  1545. goto bail;
  1546. }
  1547. osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
  1548. if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
  1549. mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
  1550. osb->max_slots);
  1551. status = -EINVAL;
  1552. goto bail;
  1553. }
  1554. mlog(0, "max_slots for this device: %u\n", osb->max_slots);
  1555. osb->slot_recovery_generations =
  1556. kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
  1557. GFP_KERNEL);
  1558. if (!osb->slot_recovery_generations) {
  1559. status = -ENOMEM;
  1560. mlog_errno(status);
  1561. goto bail;
  1562. }
  1563. init_waitqueue_head(&osb->osb_wipe_event);
  1564. osb->osb_orphan_wipes = kcalloc(osb->max_slots,
  1565. sizeof(*osb->osb_orphan_wipes),
  1566. GFP_KERNEL);
  1567. if (!osb->osb_orphan_wipes) {
  1568. status = -ENOMEM;
  1569. mlog_errno(status);
  1570. goto bail;
  1571. }
  1572. osb->s_feature_compat =
  1573. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
  1574. osb->s_feature_ro_compat =
  1575. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
  1576. osb->s_feature_incompat =
  1577. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
  1578. if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
  1579. mlog(ML_ERROR, "couldn't mount because of unsupported "
  1580. "optional features (%x).\n", i);
  1581. status = -EINVAL;
  1582. goto bail;
  1583. }
  1584. if (!(osb->sb->s_flags & MS_RDONLY) &&
  1585. (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
  1586. mlog(ML_ERROR, "couldn't mount RDWR because of "
  1587. "unsupported optional features (%x).\n", i);
  1588. status = -EINVAL;
  1589. goto bail;
  1590. }
  1591. if (ocfs2_userspace_stack(osb)) {
  1592. memcpy(osb->osb_cluster_stack,
  1593. OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
  1594. OCFS2_STACK_LABEL_LEN);
  1595. osb->osb_cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
  1596. if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
  1597. mlog(ML_ERROR,
  1598. "couldn't mount because of an invalid "
  1599. "cluster stack label (%s) \n",
  1600. osb->osb_cluster_stack);
  1601. status = -EINVAL;
  1602. goto bail;
  1603. }
  1604. } else {
  1605. /* The empty string is identical with classic tools that
  1606. * don't know about s_cluster_info. */
  1607. osb->osb_cluster_stack[0] = '\0';
  1608. }
  1609. get_random_bytes(&osb->s_next_generation, sizeof(u32));
  1610. /* FIXME
  1611. * This should be done in ocfs2_journal_init(), but unknown
  1612. * ordering issues will cause the filesystem to crash.
  1613. * If anyone wants to figure out what part of the code
  1614. * refers to osb->journal before ocfs2_journal_init() is run,
  1615. * be my guest.
  1616. */
  1617. /* initialize our journal structure */
  1618. journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
  1619. if (!journal) {
  1620. mlog(ML_ERROR, "unable to alloc journal\n");
  1621. status = -ENOMEM;
  1622. goto bail;
  1623. }
  1624. osb->journal = journal;
  1625. journal->j_osb = osb;
  1626. atomic_set(&journal->j_num_trans, 0);
  1627. init_rwsem(&journal->j_trans_barrier);
  1628. init_waitqueue_head(&journal->j_checkpointed);
  1629. spin_lock_init(&journal->j_lock);
  1630. journal->j_trans_id = (unsigned long) 1;
  1631. INIT_LIST_HEAD(&journal->j_la_cleanups);
  1632. INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
  1633. journal->j_state = OCFS2_JOURNAL_FREE;
  1634. INIT_WORK(&osb->dentry_lock_work, ocfs2_drop_dl_inodes);
  1635. osb->dentry_lock_list = NULL;
  1636. /* get some pseudo constants for clustersize bits */
  1637. osb->s_clustersize_bits =
  1638. le32_to_cpu(di->id2.i_super.s_clustersize_bits);
  1639. osb->s_clustersize = 1 << osb->s_clustersize_bits;
  1640. mlog(0, "clusterbits=%d\n", osb->s_clustersize_bits);
  1641. if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
  1642. osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
  1643. mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
  1644. osb->s_clustersize);
  1645. status = -EINVAL;
  1646. goto bail;
  1647. }
  1648. if (ocfs2_clusters_to_blocks(osb->sb, le32_to_cpu(di->i_clusters) - 1)
  1649. > (u32)~0UL) {
  1650. mlog(ML_ERROR, "Volume might try to write to blocks beyond "
  1651. "what jbd can address in 32 bits.\n");
  1652. status = -EINVAL;
  1653. goto bail;
  1654. }
  1655. if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
  1656. sizeof(di->id2.i_super.s_uuid))) {
  1657. mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
  1658. status = -ENOMEM;
  1659. goto bail;
  1660. }
  1661. memcpy(&uuid_net_key, di->id2.i_super.s_uuid, sizeof(uuid_net_key));
  1662. strncpy(osb->vol_label, di->id2.i_super.s_label, 63);
  1663. osb->vol_label[63] = '\0';
  1664. osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
  1665. osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
  1666. osb->first_cluster_group_blkno =
  1667. le64_to_cpu(di->id2.i_super.s_first_cluster_group);
  1668. osb->fs_generation = le32_to_cpu(di->i_fs_generation);
  1669. osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
  1670. mlog(0, "vol_label: %s\n", osb->vol_label);
  1671. mlog(0, "uuid: %s\n", osb->uuid_str);
  1672. mlog(0, "root_blkno=%llu, system_dir_blkno=%llu\n",
  1673. (unsigned long long)osb->root_blkno,
  1674. (unsigned long long)osb->system_dir_blkno);
  1675. osb->osb_dlm_debug = ocfs2_new_dlm_debug();
  1676. if (!osb->osb_dlm_debug) {
  1677. status = -ENOMEM;
  1678. mlog_errno(status);
  1679. goto bail;
  1680. }
  1681. atomic_set(&osb->vol_state, VOLUME_INIT);
  1682. /* load root, system_dir, and all global system inodes */
  1683. status = ocfs2_init_global_system_inodes(osb);
  1684. if (status < 0) {
  1685. mlog_errno(status);
  1686. goto bail;
  1687. }
  1688. /*
  1689. * global bitmap
  1690. */
  1691. inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
  1692. OCFS2_INVALID_SLOT);
  1693. if (!inode) {
  1694. status = -EINVAL;
  1695. mlog_errno(status);
  1696. goto bail;
  1697. }
  1698. osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
  1699. iput(inode);
  1700. osb->bitmap_cpg = ocfs2_group_bitmap_size(sb) * 8;
  1701. status = ocfs2_init_slot_info(osb);
  1702. if (status < 0) {
  1703. mlog_errno(status);
  1704. goto bail;
  1705. }
  1706. bail:
  1707. mlog_exit(status);
  1708. return status;
  1709. }
  1710. /*
  1711. * will return: -EAGAIN if it is ok to keep searching for superblocks
  1712. * -EINVAL if there is a bad superblock
  1713. * 0 on success
  1714. */
  1715. static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  1716. struct buffer_head *bh,
  1717. u32 blksz)
  1718. {
  1719. int status = -EAGAIN;
  1720. mlog_entry_void();
  1721. if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
  1722. strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
  1723. /* We have to do a raw check of the feature here */
  1724. if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
  1725. OCFS2_FEATURE_INCOMPAT_META_ECC) {
  1726. status = ocfs2_block_check_validate(bh->b_data,
  1727. bh->b_size,
  1728. &di->i_check);
  1729. if (status)
  1730. goto out;
  1731. }
  1732. status = -EINVAL;
  1733. if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
  1734. mlog(ML_ERROR, "found superblock with incorrect block "
  1735. "size: found %u, should be %u\n",
  1736. 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
  1737. blksz);
  1738. } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
  1739. OCFS2_MAJOR_REV_LEVEL ||
  1740. le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
  1741. OCFS2_MINOR_REV_LEVEL) {
  1742. mlog(ML_ERROR, "found superblock with bad version: "
  1743. "found %u.%u, should be %u.%u\n",
  1744. le16_to_cpu(di->id2.i_super.s_major_rev_level),
  1745. le16_to_cpu(di->id2.i_super.s_minor_rev_level),
  1746. OCFS2_MAJOR_REV_LEVEL,
  1747. OCFS2_MINOR_REV_LEVEL);
  1748. } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
  1749. mlog(ML_ERROR, "bad block number on superblock: "
  1750. "found %llu, should be %llu\n",
  1751. (unsigned long long)le64_to_cpu(di->i_blkno),
  1752. (unsigned long long)bh->b_blocknr);
  1753. } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
  1754. le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
  1755. mlog(ML_ERROR, "bad cluster size found: %u\n",
  1756. 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
  1757. } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
  1758. mlog(ML_ERROR, "bad root_blkno: 0\n");
  1759. } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
  1760. mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
  1761. } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
  1762. mlog(ML_ERROR,
  1763. "Superblock slots found greater than file system "
  1764. "maximum: found %u, max %u\n",
  1765. le16_to_cpu(di->id2.i_super.s_max_slots),
  1766. OCFS2_MAX_SLOTS);
  1767. } else {
  1768. /* found it! */
  1769. status = 0;
  1770. }
  1771. }
  1772. out:
  1773. mlog_exit(status);
  1774. return status;
  1775. }
  1776. static int ocfs2_check_volume(struct ocfs2_super *osb)
  1777. {
  1778. int status;
  1779. int dirty;
  1780. int local;
  1781. struct ocfs2_dinode *local_alloc = NULL; /* only used if we
  1782. * recover
  1783. * ourselves. */
  1784. mlog_entry_void();
  1785. /* Init our journal object. */
  1786. status = ocfs2_journal_init(osb->journal, &dirty);
  1787. if (status < 0) {
  1788. mlog(ML_ERROR, "Could not initialize journal!\n");
  1789. goto finally;
  1790. }
  1791. /* If the journal was unmounted cleanly then we don't want to
  1792. * recover anything. Otherwise, journal_load will do that
  1793. * dirty work for us :) */
  1794. if (!dirty) {
  1795. status = ocfs2_journal_wipe(osb->journal, 0);
  1796. if (status < 0) {
  1797. mlog_errno(status);
  1798. goto finally;
  1799. }
  1800. } else {
  1801. mlog(ML_NOTICE, "File system was not unmounted cleanly, "
  1802. "recovering volume.\n");
  1803. }
  1804. local = ocfs2_mount_local(osb);
  1805. /* will play back anything left in the journal. */
  1806. status = ocfs2_journal_load(osb->journal, local, dirty);
  1807. if (status < 0) {
  1808. mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
  1809. goto finally;
  1810. }
  1811. if (dirty) {
  1812. /* recover my local alloc if we didn't unmount cleanly. */
  1813. status = ocfs2_begin_local_alloc_recovery(osb,
  1814. osb->slot_num,
  1815. &local_alloc);
  1816. if (status < 0) {
  1817. mlog_errno(status);
  1818. goto finally;
  1819. }
  1820. /* we complete the recovery process after we've marked
  1821. * ourselves as mounted. */
  1822. }
  1823. mlog(0, "Journal loaded.\n");
  1824. status = ocfs2_load_local_alloc(osb);
  1825. if (status < 0) {
  1826. mlog_errno(status);
  1827. goto finally;
  1828. }
  1829. if (dirty) {
  1830. /* Recovery will be completed after we've mounted the
  1831. * rest of the volume. */
  1832. osb->dirty = 1;
  1833. osb->local_alloc_copy = local_alloc;
  1834. local_alloc = NULL;
  1835. }
  1836. /* go through each journal, trylock it and if you get the
  1837. * lock, and it's marked as dirty, set the bit in the recover
  1838. * map and launch a recovery thread for it. */
  1839. status = ocfs2_mark_dead_nodes(osb);
  1840. if (status < 0)
  1841. mlog_errno(status);
  1842. finally:
  1843. if (local_alloc)
  1844. kfree(local_alloc);
  1845. mlog_exit(status);
  1846. return status;
  1847. }
  1848. /*
  1849. * The routine gets called from dismount or close whenever a dismount on
  1850. * volume is requested and the osb open count becomes 1.
  1851. * It will remove the osb from the global list and also free up all the
  1852. * initialized resources and fileobject.
  1853. */
  1854. static void ocfs2_delete_osb(struct ocfs2_super *osb)
  1855. {
  1856. mlog_entry_void();
  1857. /* This function assumes that the caller has the main osb resource */
  1858. ocfs2_free_slot_info(osb);
  1859. kfree(osb->osb_orphan_wipes);
  1860. kfree(osb->slot_recovery_generations);
  1861. /* FIXME
  1862. * This belongs in journal shutdown, but because we have to
  1863. * allocate osb->journal at the start of ocfs2_initalize_osb(),
  1864. * we free it here.
  1865. */
  1866. kfree(osb->journal);
  1867. if (osb->local_alloc_copy)
  1868. kfree(osb->local_alloc_copy);
  1869. kfree(osb->uuid_str);
  1870. ocfs2_put_dlm_debug(osb->osb_dlm_debug);
  1871. memset(osb, 0, sizeof(struct ocfs2_super));
  1872. mlog_exit_void();
  1873. }
  1874. /* Put OCFS2 into a readonly state, or (if the user specifies it),
  1875. * panic(). We do not support continue-on-error operation. */
  1876. static void ocfs2_handle_error(struct super_block *sb)
  1877. {
  1878. struct ocfs2_super *osb = OCFS2_SB(sb);
  1879. if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC)
  1880. panic("OCFS2: (device %s): panic forced after error\n",
  1881. sb->s_id);
  1882. ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
  1883. if (sb->s_flags & MS_RDONLY &&
  1884. (ocfs2_is_soft_readonly(osb) ||
  1885. ocfs2_is_hard_readonly(osb)))
  1886. return;
  1887. printk(KERN_CRIT "File system is now read-only due to the potential "
  1888. "of on-disk corruption. Please run fsck.ocfs2 once the file "
  1889. "system is unmounted.\n");
  1890. sb->s_flags |= MS_RDONLY;
  1891. ocfs2_set_ro_flag(osb, 0);
  1892. }
  1893. static char error_buf[1024];
  1894. void __ocfs2_error(struct super_block *sb,
  1895. const char *function,
  1896. const char *fmt, ...)
  1897. {
  1898. va_list args;
  1899. va_start(args, fmt);
  1900. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1901. va_end(args);
  1902. /* Not using mlog here because we want to show the actual
  1903. * function the error came from. */
  1904. printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %s\n",
  1905. sb->s_id, function, error_buf);
  1906. ocfs2_handle_error(sb);
  1907. }
  1908. /* Handle critical errors. This is intentionally more drastic than
  1909. * ocfs2_handle_error, so we only use for things like journal errors,
  1910. * etc. */
  1911. void __ocfs2_abort(struct super_block* sb,
  1912. const char *function,
  1913. const char *fmt, ...)
  1914. {
  1915. va_list args;
  1916. va_start(args, fmt);
  1917. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1918. va_end(args);
  1919. printk(KERN_CRIT "OCFS2: abort (device %s): %s: %s\n",
  1920. sb->s_id, function, error_buf);
  1921. /* We don't have the cluster support yet to go straight to
  1922. * hard readonly in here. Until then, we want to keep
  1923. * ocfs2_abort() so that we can at least mark critical
  1924. * errors.
  1925. *
  1926. * TODO: This should abort the journal and alert other nodes
  1927. * that our slot needs recovery. */
  1928. /* Force a panic(). This stinks, but it's better than letting
  1929. * things continue without having a proper hard readonly
  1930. * here. */
  1931. OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
  1932. ocfs2_handle_error(sb);
  1933. }
  1934. module_init(ocfs2_init);
  1935. module_exit(ocfs2_exit);