super.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  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/init.h>
  31. #include <linux/random.h>
  32. #include <linux/statfs.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/socket.h>
  36. #include <linux/inet.h>
  37. #include <linux/parser.h>
  38. #include <linux/crc32.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/mount.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/quotaops.h>
  43. #include <linux/cleancache.h>
  44. #define CREATE_TRACE_POINTS
  45. #include "ocfs2_trace.h"
  46. #include <cluster/masklog.h>
  47. #include "ocfs2.h"
  48. /* this should be the only file to include a version 1 header */
  49. #include "ocfs1_fs_compat.h"
  50. #include "alloc.h"
  51. #include "aops.h"
  52. #include "blockcheck.h"
  53. #include "dlmglue.h"
  54. #include "export.h"
  55. #include "extent_map.h"
  56. #include "heartbeat.h"
  57. #include "inode.h"
  58. #include "journal.h"
  59. #include "localalloc.h"
  60. #include "namei.h"
  61. #include "slot_map.h"
  62. #include "super.h"
  63. #include "sysfile.h"
  64. #include "uptodate.h"
  65. #include "xattr.h"
  66. #include "quota.h"
  67. #include "refcounttree.h"
  68. #include "suballoc.h"
  69. #include "buffer_head_io.h"
  70. static struct kmem_cache *ocfs2_inode_cachep;
  71. struct kmem_cache *ocfs2_dquot_cachep;
  72. struct kmem_cache *ocfs2_qf_chunk_cachep;
  73. /* OCFS2 needs to schedule several different types of work which
  74. * require cluster locking, disk I/O, recovery waits, etc. Since these
  75. * types of work tend to be heavy we avoid using the kernel events
  76. * workqueue and schedule on our own. */
  77. struct workqueue_struct *ocfs2_wq = NULL;
  78. static struct dentry *ocfs2_debugfs_root;
  79. MODULE_AUTHOR("Oracle");
  80. MODULE_LICENSE("GPL");
  81. MODULE_DESCRIPTION("OCFS2 cluster file system");
  82. struct mount_options
  83. {
  84. unsigned long commit_interval;
  85. unsigned long mount_opt;
  86. unsigned int atime_quantum;
  87. signed short slot;
  88. int localalloc_opt;
  89. unsigned int resv_level;
  90. int dir_resv_level;
  91. char cluster_stack[OCFS2_STACK_LABEL_LEN + 1];
  92. };
  93. static int ocfs2_parse_options(struct super_block *sb, char *options,
  94. struct mount_options *mopt,
  95. int is_remount);
  96. static int ocfs2_check_set_options(struct super_block *sb,
  97. struct mount_options *options);
  98. static int ocfs2_show_options(struct seq_file *s, struct dentry *root);
  99. static void ocfs2_put_super(struct super_block *sb);
  100. static int ocfs2_mount_volume(struct super_block *sb);
  101. static int ocfs2_remount(struct super_block *sb, int *flags, char *data);
  102. static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);
  103. static int ocfs2_initialize_mem_caches(void);
  104. static void ocfs2_free_mem_caches(void);
  105. static void ocfs2_delete_osb(struct ocfs2_super *osb);
  106. static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);
  107. static int ocfs2_sync_fs(struct super_block *sb, int wait);
  108. static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);
  109. static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);
  110. static void ocfs2_release_system_inodes(struct ocfs2_super *osb);
  111. static int ocfs2_check_volume(struct ocfs2_super *osb);
  112. static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  113. struct buffer_head *bh,
  114. u32 sectsize,
  115. struct ocfs2_blockcheck_stats *stats);
  116. static int ocfs2_initialize_super(struct super_block *sb,
  117. struct buffer_head *bh,
  118. int sector_size,
  119. struct ocfs2_blockcheck_stats *stats);
  120. static int ocfs2_get_sector(struct super_block *sb,
  121. struct buffer_head **bh,
  122. int block,
  123. int sect_size);
  124. static struct inode *ocfs2_alloc_inode(struct super_block *sb);
  125. static void ocfs2_destroy_inode(struct inode *inode);
  126. static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend);
  127. static int ocfs2_enable_quotas(struct ocfs2_super *osb);
  128. static void ocfs2_disable_quotas(struct ocfs2_super *osb);
  129. static struct dquot **ocfs2_get_dquots(struct inode *inode)
  130. {
  131. return OCFS2_I(inode)->i_dquot;
  132. }
  133. static const struct super_operations ocfs2_sops = {
  134. .statfs = ocfs2_statfs,
  135. .alloc_inode = ocfs2_alloc_inode,
  136. .destroy_inode = ocfs2_destroy_inode,
  137. .drop_inode = ocfs2_drop_inode,
  138. .evict_inode = ocfs2_evict_inode,
  139. .sync_fs = ocfs2_sync_fs,
  140. .put_super = ocfs2_put_super,
  141. .remount_fs = ocfs2_remount,
  142. .show_options = ocfs2_show_options,
  143. .quota_read = ocfs2_quota_read,
  144. .quota_write = ocfs2_quota_write,
  145. .get_dquots = ocfs2_get_dquots,
  146. };
  147. enum {
  148. Opt_barrier,
  149. Opt_err_panic,
  150. Opt_err_ro,
  151. Opt_intr,
  152. Opt_nointr,
  153. Opt_hb_none,
  154. Opt_hb_local,
  155. Opt_hb_global,
  156. Opt_data_ordered,
  157. Opt_data_writeback,
  158. Opt_atime_quantum,
  159. Opt_slot,
  160. Opt_commit,
  161. Opt_localalloc,
  162. Opt_localflocks,
  163. Opt_stack,
  164. Opt_user_xattr,
  165. Opt_nouser_xattr,
  166. Opt_inode64,
  167. Opt_acl,
  168. Opt_noacl,
  169. Opt_usrquota,
  170. Opt_grpquota,
  171. Opt_coherency_buffered,
  172. Opt_coherency_full,
  173. Opt_resv_level,
  174. Opt_dir_resv_level,
  175. Opt_journal_async_commit,
  176. Opt_err_cont,
  177. Opt_err,
  178. };
  179. static const match_table_t tokens = {
  180. {Opt_barrier, "barrier=%u"},
  181. {Opt_err_panic, "errors=panic"},
  182. {Opt_err_ro, "errors=remount-ro"},
  183. {Opt_intr, "intr"},
  184. {Opt_nointr, "nointr"},
  185. {Opt_hb_none, OCFS2_HB_NONE},
  186. {Opt_hb_local, OCFS2_HB_LOCAL},
  187. {Opt_hb_global, OCFS2_HB_GLOBAL},
  188. {Opt_data_ordered, "data=ordered"},
  189. {Opt_data_writeback, "data=writeback"},
  190. {Opt_atime_quantum, "atime_quantum=%u"},
  191. {Opt_slot, "preferred_slot=%u"},
  192. {Opt_commit, "commit=%u"},
  193. {Opt_localalloc, "localalloc=%d"},
  194. {Opt_localflocks, "localflocks"},
  195. {Opt_stack, "cluster_stack=%s"},
  196. {Opt_user_xattr, "user_xattr"},
  197. {Opt_nouser_xattr, "nouser_xattr"},
  198. {Opt_inode64, "inode64"},
  199. {Opt_acl, "acl"},
  200. {Opt_noacl, "noacl"},
  201. {Opt_usrquota, "usrquota"},
  202. {Opt_grpquota, "grpquota"},
  203. {Opt_coherency_buffered, "coherency=buffered"},
  204. {Opt_coherency_full, "coherency=full"},
  205. {Opt_resv_level, "resv_level=%u"},
  206. {Opt_dir_resv_level, "dir_resv_level=%u"},
  207. {Opt_journal_async_commit, "journal_async_commit"},
  208. {Opt_err_cont, "errors=continue"},
  209. {Opt_err, NULL}
  210. };
  211. #ifdef CONFIG_DEBUG_FS
  212. static int ocfs2_osb_dump(struct ocfs2_super *osb, char *buf, int len)
  213. {
  214. struct ocfs2_cluster_connection *cconn = osb->cconn;
  215. struct ocfs2_recovery_map *rm = osb->recovery_map;
  216. struct ocfs2_orphan_scan *os = &osb->osb_orphan_scan;
  217. int i, out = 0;
  218. out += snprintf(buf + out, len - out,
  219. "%10s => Id: %-s Uuid: %-s Gen: 0x%X Label: %-s\n",
  220. "Device", osb->dev_str, osb->uuid_str,
  221. osb->fs_generation, osb->vol_label);
  222. out += snprintf(buf + out, len - out,
  223. "%10s => State: %d Flags: 0x%lX\n", "Volume",
  224. atomic_read(&osb->vol_state), osb->osb_flags);
  225. out += snprintf(buf + out, len - out,
  226. "%10s => Block: %lu Cluster: %d\n", "Sizes",
  227. osb->sb->s_blocksize, osb->s_clustersize);
  228. out += snprintf(buf + out, len - out,
  229. "%10s => Compat: 0x%X Incompat: 0x%X "
  230. "ROcompat: 0x%X\n",
  231. "Features", osb->s_feature_compat,
  232. osb->s_feature_incompat, osb->s_feature_ro_compat);
  233. out += snprintf(buf + out, len - out,
  234. "%10s => Opts: 0x%lX AtimeQuanta: %u\n", "Mount",
  235. osb->s_mount_opt, osb->s_atime_quantum);
  236. if (cconn) {
  237. out += snprintf(buf + out, len - out,
  238. "%10s => Stack: %s Name: %*s "
  239. "Version: %d.%d\n", "Cluster",
  240. (*osb->osb_cluster_stack == '\0' ?
  241. "o2cb" : osb->osb_cluster_stack),
  242. cconn->cc_namelen, cconn->cc_name,
  243. cconn->cc_version.pv_major,
  244. cconn->cc_version.pv_minor);
  245. }
  246. spin_lock(&osb->dc_task_lock);
  247. out += snprintf(buf + out, len - out,
  248. "%10s => Pid: %d Count: %lu WakeSeq: %lu "
  249. "WorkSeq: %lu\n", "DownCnvt",
  250. (osb->dc_task ? task_pid_nr(osb->dc_task) : -1),
  251. osb->blocked_lock_count, osb->dc_wake_sequence,
  252. osb->dc_work_sequence);
  253. spin_unlock(&osb->dc_task_lock);
  254. spin_lock(&osb->osb_lock);
  255. out += snprintf(buf + out, len - out, "%10s => Pid: %d Nodes:",
  256. "Recovery",
  257. (osb->recovery_thread_task ?
  258. task_pid_nr(osb->recovery_thread_task) : -1));
  259. if (rm->rm_used == 0)
  260. out += snprintf(buf + out, len - out, " None\n");
  261. else {
  262. for (i = 0; i < rm->rm_used; i++)
  263. out += snprintf(buf + out, len - out, " %d",
  264. rm->rm_entries[i]);
  265. out += snprintf(buf + out, len - out, "\n");
  266. }
  267. spin_unlock(&osb->osb_lock);
  268. out += snprintf(buf + out, len - out,
  269. "%10s => Pid: %d Interval: %lu\n", "Commit",
  270. (osb->commit_task ? task_pid_nr(osb->commit_task) : -1),
  271. osb->osb_commit_interval);
  272. out += snprintf(buf + out, len - out,
  273. "%10s => State: %d TxnId: %lu NumTxns: %d\n",
  274. "Journal", osb->journal->j_state,
  275. osb->journal->j_trans_id,
  276. atomic_read(&osb->journal->j_num_trans));
  277. out += snprintf(buf + out, len - out,
  278. "%10s => GlobalAllocs: %d LocalAllocs: %d "
  279. "SubAllocs: %d LAWinMoves: %d SAExtends: %d\n",
  280. "Stats",
  281. atomic_read(&osb->alloc_stats.bitmap_data),
  282. atomic_read(&osb->alloc_stats.local_data),
  283. atomic_read(&osb->alloc_stats.bg_allocs),
  284. atomic_read(&osb->alloc_stats.moves),
  285. atomic_read(&osb->alloc_stats.bg_extends));
  286. out += snprintf(buf + out, len - out,
  287. "%10s => State: %u Descriptor: %llu Size: %u bits "
  288. "Default: %u bits\n",
  289. "LocalAlloc", osb->local_alloc_state,
  290. (unsigned long long)osb->la_last_gd,
  291. osb->local_alloc_bits, osb->local_alloc_default_bits);
  292. spin_lock(&osb->osb_lock);
  293. out += snprintf(buf + out, len - out,
  294. "%10s => InodeSlot: %d StolenInodes: %d, "
  295. "MetaSlot: %d StolenMeta: %d\n", "Steal",
  296. osb->s_inode_steal_slot,
  297. atomic_read(&osb->s_num_inodes_stolen),
  298. osb->s_meta_steal_slot,
  299. atomic_read(&osb->s_num_meta_stolen));
  300. spin_unlock(&osb->osb_lock);
  301. out += snprintf(buf + out, len - out, "OrphanScan => ");
  302. out += snprintf(buf + out, len - out, "Local: %u Global: %u ",
  303. os->os_count, os->os_seqno);
  304. out += snprintf(buf + out, len - out, " Last Scan: ");
  305. if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
  306. out += snprintf(buf + out, len - out, "Disabled\n");
  307. else
  308. out += snprintf(buf + out, len - out, "%lu seconds ago\n",
  309. (get_seconds() - os->os_scantime.tv_sec));
  310. out += snprintf(buf + out, len - out, "%10s => %3s %10s\n",
  311. "Slots", "Num", "RecoGen");
  312. for (i = 0; i < osb->max_slots; ++i) {
  313. out += snprintf(buf + out, len - out,
  314. "%10s %c %3d %10d\n",
  315. " ",
  316. (i == osb->slot_num ? '*' : ' '),
  317. i, osb->slot_recovery_generations[i]);
  318. }
  319. return out;
  320. }
  321. static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
  322. {
  323. struct ocfs2_super *osb = inode->i_private;
  324. char *buf = NULL;
  325. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  326. if (!buf)
  327. goto bail;
  328. i_size_write(inode, ocfs2_osb_dump(osb, buf, PAGE_SIZE));
  329. file->private_data = buf;
  330. return 0;
  331. bail:
  332. return -ENOMEM;
  333. }
  334. static int ocfs2_debug_release(struct inode *inode, struct file *file)
  335. {
  336. kfree(file->private_data);
  337. return 0;
  338. }
  339. static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
  340. size_t nbytes, loff_t *ppos)
  341. {
  342. return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
  343. i_size_read(file->f_mapping->host));
  344. }
  345. #else
  346. static int ocfs2_osb_debug_open(struct inode *inode, struct file *file)
  347. {
  348. return 0;
  349. }
  350. static int ocfs2_debug_release(struct inode *inode, struct file *file)
  351. {
  352. return 0;
  353. }
  354. static ssize_t ocfs2_debug_read(struct file *file, char __user *buf,
  355. size_t nbytes, loff_t *ppos)
  356. {
  357. return 0;
  358. }
  359. #endif /* CONFIG_DEBUG_FS */
  360. static const struct file_operations ocfs2_osb_debug_fops = {
  361. .open = ocfs2_osb_debug_open,
  362. .release = ocfs2_debug_release,
  363. .read = ocfs2_debug_read,
  364. .llseek = generic_file_llseek,
  365. };
  366. static int ocfs2_sync_fs(struct super_block *sb, int wait)
  367. {
  368. int status;
  369. tid_t target;
  370. struct ocfs2_super *osb = OCFS2_SB(sb);
  371. if (ocfs2_is_hard_readonly(osb))
  372. return -EROFS;
  373. if (wait) {
  374. status = ocfs2_flush_truncate_log(osb);
  375. if (status < 0)
  376. mlog_errno(status);
  377. } else {
  378. ocfs2_schedule_truncate_log_flush(osb, 0);
  379. }
  380. if (jbd2_journal_start_commit(OCFS2_SB(sb)->journal->j_journal,
  381. &target)) {
  382. if (wait)
  383. jbd2_log_wait_commit(OCFS2_SB(sb)->journal->j_journal,
  384. target);
  385. }
  386. return 0;
  387. }
  388. static int ocfs2_need_system_inode(struct ocfs2_super *osb, int ino)
  389. {
  390. if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
  391. && (ino == USER_QUOTA_SYSTEM_INODE
  392. || ino == LOCAL_USER_QUOTA_SYSTEM_INODE))
  393. return 0;
  394. if (!OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)
  395. && (ino == GROUP_QUOTA_SYSTEM_INODE
  396. || ino == LOCAL_GROUP_QUOTA_SYSTEM_INODE))
  397. return 0;
  398. return 1;
  399. }
  400. static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb)
  401. {
  402. struct inode *new = NULL;
  403. int status = 0;
  404. int i;
  405. new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
  406. if (IS_ERR(new)) {
  407. status = PTR_ERR(new);
  408. mlog_errno(status);
  409. goto bail;
  410. }
  411. osb->root_inode = new;
  412. new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0);
  413. if (IS_ERR(new)) {
  414. status = PTR_ERR(new);
  415. mlog_errno(status);
  416. goto bail;
  417. }
  418. osb->sys_root_inode = new;
  419. for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE;
  420. i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) {
  421. if (!ocfs2_need_system_inode(osb, i))
  422. continue;
  423. new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
  424. if (!new) {
  425. ocfs2_release_system_inodes(osb);
  426. status = -EINVAL;
  427. mlog_errno(status);
  428. /* FIXME: Should ERROR_RO_FS */
  429. mlog(ML_ERROR, "Unable to load system inode %d, "
  430. "possibly corrupt fs?", i);
  431. goto bail;
  432. }
  433. // the array now has one ref, so drop this one
  434. iput(new);
  435. }
  436. bail:
  437. if (status)
  438. mlog_errno(status);
  439. return status;
  440. }
  441. static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb)
  442. {
  443. struct inode *new = NULL;
  444. int status = 0;
  445. int i;
  446. for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1;
  447. i < NUM_SYSTEM_INODES;
  448. i++) {
  449. if (!ocfs2_need_system_inode(osb, i))
  450. continue;
  451. new = ocfs2_get_system_file_inode(osb, i, osb->slot_num);
  452. if (!new) {
  453. ocfs2_release_system_inodes(osb);
  454. status = -EINVAL;
  455. mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n",
  456. status, i, osb->slot_num);
  457. goto bail;
  458. }
  459. /* the array now has one ref, so drop this one */
  460. iput(new);
  461. }
  462. bail:
  463. if (status)
  464. mlog_errno(status);
  465. return status;
  466. }
  467. static void ocfs2_release_system_inodes(struct ocfs2_super *osb)
  468. {
  469. int i;
  470. struct inode *inode;
  471. for (i = 0; i < NUM_GLOBAL_SYSTEM_INODES; i++) {
  472. inode = osb->global_system_inodes[i];
  473. if (inode) {
  474. iput(inode);
  475. osb->global_system_inodes[i] = NULL;
  476. }
  477. }
  478. inode = osb->sys_root_inode;
  479. if (inode) {
  480. iput(inode);
  481. osb->sys_root_inode = NULL;
  482. }
  483. inode = osb->root_inode;
  484. if (inode) {
  485. iput(inode);
  486. osb->root_inode = NULL;
  487. }
  488. if (!osb->local_system_inodes)
  489. return;
  490. for (i = 0; i < NUM_LOCAL_SYSTEM_INODES * osb->max_slots; i++) {
  491. if (osb->local_system_inodes[i]) {
  492. iput(osb->local_system_inodes[i]);
  493. osb->local_system_inodes[i] = NULL;
  494. }
  495. }
  496. kfree(osb->local_system_inodes);
  497. osb->local_system_inodes = NULL;
  498. }
  499. /* We're allocating fs objects, use GFP_NOFS */
  500. static struct inode *ocfs2_alloc_inode(struct super_block *sb)
  501. {
  502. struct ocfs2_inode_info *oi;
  503. oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS);
  504. if (!oi)
  505. return NULL;
  506. oi->i_sync_tid = 0;
  507. oi->i_datasync_tid = 0;
  508. memset(&oi->i_dquot, 0, sizeof(oi->i_dquot));
  509. jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
  510. return &oi->vfs_inode;
  511. }
  512. static void ocfs2_i_callback(struct rcu_head *head)
  513. {
  514. struct inode *inode = container_of(head, struct inode, i_rcu);
  515. kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));
  516. }
  517. static void ocfs2_destroy_inode(struct inode *inode)
  518. {
  519. call_rcu(&inode->i_rcu, ocfs2_i_callback);
  520. }
  521. static unsigned long long ocfs2_max_file_offset(unsigned int bbits,
  522. unsigned int cbits)
  523. {
  524. unsigned int bytes = 1 << cbits;
  525. unsigned int trim = bytes;
  526. unsigned int bitshift = 32;
  527. /*
  528. * i_size and all block offsets in ocfs2 are always 64 bits
  529. * wide. i_clusters is 32 bits, in cluster-sized units. So on
  530. * 64 bit platforms, cluster size will be the limiting factor.
  531. */
  532. #if BITS_PER_LONG == 32
  533. # if defined(CONFIG_LBDAF)
  534. BUILD_BUG_ON(sizeof(sector_t) != 8);
  535. /*
  536. * We might be limited by page cache size.
  537. */
  538. if (bytes > PAGE_CACHE_SIZE) {
  539. bytes = PAGE_CACHE_SIZE;
  540. trim = 1;
  541. /*
  542. * Shift by 31 here so that we don't get larger than
  543. * MAX_LFS_FILESIZE
  544. */
  545. bitshift = 31;
  546. }
  547. # else
  548. /*
  549. * We are limited by the size of sector_t. Use block size, as
  550. * that's what we expose to the VFS.
  551. */
  552. bytes = 1 << bbits;
  553. trim = 1;
  554. bitshift = 31;
  555. # endif
  556. #endif
  557. /*
  558. * Trim by a whole cluster when we can actually approach the
  559. * on-disk limits. Otherwise we can overflow i_clusters when
  560. * an extent start is at the max offset.
  561. */
  562. return (((unsigned long long)bytes) << bitshift) - trim;
  563. }
  564. static int ocfs2_remount(struct super_block *sb, int *flags, char *data)
  565. {
  566. int incompat_features;
  567. int ret = 0;
  568. struct mount_options parsed_options;
  569. struct ocfs2_super *osb = OCFS2_SB(sb);
  570. u32 tmp;
  571. sync_filesystem(sb);
  572. if (!ocfs2_parse_options(sb, data, &parsed_options, 1) ||
  573. !ocfs2_check_set_options(sb, &parsed_options)) {
  574. ret = -EINVAL;
  575. goto out;
  576. }
  577. tmp = OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL |
  578. OCFS2_MOUNT_HB_NONE;
  579. if ((osb->s_mount_opt & tmp) != (parsed_options.mount_opt & tmp)) {
  580. ret = -EINVAL;
  581. mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n");
  582. goto out;
  583. }
  584. if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) !=
  585. (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) {
  586. ret = -EINVAL;
  587. mlog(ML_ERROR, "Cannot change data mode on remount\n");
  588. goto out;
  589. }
  590. /* Probably don't want this on remount; it might
  591. * mess with other nodes */
  592. if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64) &&
  593. (parsed_options.mount_opt & OCFS2_MOUNT_INODE64)) {
  594. ret = -EINVAL;
  595. mlog(ML_ERROR, "Cannot enable inode64 on remount\n");
  596. goto out;
  597. }
  598. /* We're going to/from readonly mode. */
  599. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
  600. /* Disable quota accounting before remounting RO */
  601. if (*flags & MS_RDONLY) {
  602. ret = ocfs2_susp_quotas(osb, 0);
  603. if (ret < 0)
  604. goto out;
  605. }
  606. /* Lock here so the check of HARD_RO and the potential
  607. * setting of SOFT_RO is atomic. */
  608. spin_lock(&osb->osb_lock);
  609. if (osb->osb_flags & OCFS2_OSB_HARD_RO) {
  610. mlog(ML_ERROR, "Remount on readonly device is forbidden.\n");
  611. ret = -EROFS;
  612. goto unlock_osb;
  613. }
  614. if (*flags & MS_RDONLY) {
  615. sb->s_flags |= MS_RDONLY;
  616. osb->osb_flags |= OCFS2_OSB_SOFT_RO;
  617. } else {
  618. if (osb->osb_flags & OCFS2_OSB_ERROR_FS) {
  619. mlog(ML_ERROR, "Cannot remount RDWR "
  620. "filesystem due to previous errors.\n");
  621. ret = -EROFS;
  622. goto unlock_osb;
  623. }
  624. incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP);
  625. if (incompat_features) {
  626. mlog(ML_ERROR, "Cannot remount RDWR because "
  627. "of unsupported optional features "
  628. "(%x).\n", incompat_features);
  629. ret = -EINVAL;
  630. goto unlock_osb;
  631. }
  632. sb->s_flags &= ~MS_RDONLY;
  633. osb->osb_flags &= ~OCFS2_OSB_SOFT_RO;
  634. }
  635. trace_ocfs2_remount(sb->s_flags, osb->osb_flags, *flags);
  636. unlock_osb:
  637. spin_unlock(&osb->osb_lock);
  638. /* Enable quota accounting after remounting RW */
  639. if (!ret && !(*flags & MS_RDONLY)) {
  640. if (sb_any_quota_suspended(sb))
  641. ret = ocfs2_susp_quotas(osb, 1);
  642. else
  643. ret = ocfs2_enable_quotas(osb);
  644. if (ret < 0) {
  645. /* Return back changes... */
  646. spin_lock(&osb->osb_lock);
  647. sb->s_flags |= MS_RDONLY;
  648. osb->osb_flags |= OCFS2_OSB_SOFT_RO;
  649. spin_unlock(&osb->osb_lock);
  650. goto out;
  651. }
  652. }
  653. }
  654. if (!ret) {
  655. /* Only save off the new mount options in case of a successful
  656. * remount. */
  657. osb->s_mount_opt = parsed_options.mount_opt;
  658. osb->s_atime_quantum = parsed_options.atime_quantum;
  659. osb->preferred_slot = parsed_options.slot;
  660. if (parsed_options.commit_interval)
  661. osb->osb_commit_interval = parsed_options.commit_interval;
  662. if (!ocfs2_is_hard_readonly(osb))
  663. ocfs2_set_journal_params(osb);
  664. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  665. ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ?
  666. MS_POSIXACL : 0);
  667. }
  668. out:
  669. return ret;
  670. }
  671. static int ocfs2_sb_probe(struct super_block *sb,
  672. struct buffer_head **bh,
  673. int *sector_size,
  674. struct ocfs2_blockcheck_stats *stats)
  675. {
  676. int status, tmpstat;
  677. struct ocfs1_vol_disk_hdr *hdr;
  678. struct ocfs2_dinode *di;
  679. int blksize;
  680. *bh = NULL;
  681. /* may be > 512 */
  682. *sector_size = bdev_logical_block_size(sb->s_bdev);
  683. if (*sector_size > OCFS2_MAX_BLOCKSIZE) {
  684. mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",
  685. *sector_size, OCFS2_MAX_BLOCKSIZE);
  686. status = -EINVAL;
  687. goto bail;
  688. }
  689. /* Can this really happen? */
  690. if (*sector_size < OCFS2_MIN_BLOCKSIZE)
  691. *sector_size = OCFS2_MIN_BLOCKSIZE;
  692. /* check block zero for old format */
  693. status = ocfs2_get_sector(sb, bh, 0, *sector_size);
  694. if (status < 0) {
  695. mlog_errno(status);
  696. goto bail;
  697. }
  698. hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;
  699. if (hdr->major_version == OCFS1_MAJOR_VERSION) {
  700. mlog(ML_ERROR, "incompatible version: %u.%u\n",
  701. hdr->major_version, hdr->minor_version);
  702. status = -EINVAL;
  703. }
  704. if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,
  705. strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {
  706. mlog(ML_ERROR, "incompatible volume signature: %8s\n",
  707. hdr->signature);
  708. status = -EINVAL;
  709. }
  710. brelse(*bh);
  711. *bh = NULL;
  712. if (status < 0) {
  713. mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "
  714. "upgraded before mounting with ocfs v2\n");
  715. goto bail;
  716. }
  717. /*
  718. * Now check at magic offset for 512, 1024, 2048, 4096
  719. * blocksizes. 4096 is the maximum blocksize because it is
  720. * the minimum clustersize.
  721. */
  722. status = -EINVAL;
  723. for (blksize = *sector_size;
  724. blksize <= OCFS2_MAX_BLOCKSIZE;
  725. blksize <<= 1) {
  726. tmpstat = ocfs2_get_sector(sb, bh,
  727. OCFS2_SUPER_BLOCK_BLKNO,
  728. blksize);
  729. if (tmpstat < 0) {
  730. status = tmpstat;
  731. mlog_errno(status);
  732. break;
  733. }
  734. di = (struct ocfs2_dinode *) (*bh)->b_data;
  735. memset(stats, 0, sizeof(struct ocfs2_blockcheck_stats));
  736. spin_lock_init(&stats->b_lock);
  737. tmpstat = ocfs2_verify_volume(di, *bh, blksize, stats);
  738. if (tmpstat < 0) {
  739. brelse(*bh);
  740. *bh = NULL;
  741. }
  742. if (tmpstat != -EAGAIN) {
  743. status = tmpstat;
  744. break;
  745. }
  746. }
  747. bail:
  748. return status;
  749. }
  750. static int ocfs2_verify_heartbeat(struct ocfs2_super *osb)
  751. {
  752. u32 hb_enabled = OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL;
  753. if (osb->s_mount_opt & hb_enabled) {
  754. if (ocfs2_mount_local(osb)) {
  755. mlog(ML_ERROR, "Cannot heartbeat on a locally "
  756. "mounted device.\n");
  757. return -EINVAL;
  758. }
  759. if (ocfs2_userspace_stack(osb)) {
  760. mlog(ML_ERROR, "Userspace stack expected, but "
  761. "o2cb heartbeat arguments passed to mount\n");
  762. return -EINVAL;
  763. }
  764. if (((osb->s_mount_opt & OCFS2_MOUNT_HB_GLOBAL) &&
  765. !ocfs2_cluster_o2cb_global_heartbeat(osb)) ||
  766. ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) &&
  767. ocfs2_cluster_o2cb_global_heartbeat(osb))) {
  768. mlog(ML_ERROR, "Mismatching o2cb heartbeat modes\n");
  769. return -EINVAL;
  770. }
  771. }
  772. if (!(osb->s_mount_opt & hb_enabled)) {
  773. if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb) &&
  774. !ocfs2_userspace_stack(osb)) {
  775. mlog(ML_ERROR, "Heartbeat has to be started to mount "
  776. "a read-write clustered device.\n");
  777. return -EINVAL;
  778. }
  779. }
  780. return 0;
  781. }
  782. /*
  783. * If we're using a userspace stack, mount should have passed
  784. * a name that matches the disk. If not, mount should not
  785. * have passed a stack.
  786. */
  787. static int ocfs2_verify_userspace_stack(struct ocfs2_super *osb,
  788. struct mount_options *mopt)
  789. {
  790. if (!ocfs2_userspace_stack(osb) && mopt->cluster_stack[0]) {
  791. mlog(ML_ERROR,
  792. "cluster stack passed to mount, but this filesystem "
  793. "does not support it\n");
  794. return -EINVAL;
  795. }
  796. if (ocfs2_userspace_stack(osb) &&
  797. strncmp(osb->osb_cluster_stack, mopt->cluster_stack,
  798. OCFS2_STACK_LABEL_LEN)) {
  799. mlog(ML_ERROR,
  800. "cluster stack passed to mount (\"%s\") does not "
  801. "match the filesystem (\"%s\")\n",
  802. mopt->cluster_stack,
  803. osb->osb_cluster_stack);
  804. return -EINVAL;
  805. }
  806. return 0;
  807. }
  808. static int ocfs2_susp_quotas(struct ocfs2_super *osb, int unsuspend)
  809. {
  810. int type;
  811. struct super_block *sb = osb->sb;
  812. unsigned int feature[OCFS2_MAXQUOTAS] = {
  813. OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  814. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
  815. int status = 0;
  816. for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
  817. if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
  818. continue;
  819. if (unsuspend)
  820. status = dquot_resume(sb, type);
  821. else {
  822. struct ocfs2_mem_dqinfo *oinfo;
  823. /* Cancel periodic syncing before suspending */
  824. oinfo = sb_dqinfo(sb, type)->dqi_priv;
  825. cancel_delayed_work_sync(&oinfo->dqi_sync_work);
  826. status = dquot_suspend(sb, type);
  827. }
  828. if (status < 0)
  829. break;
  830. }
  831. if (status < 0)
  832. mlog(ML_ERROR, "Failed to suspend/unsuspend quotas on "
  833. "remount (error = %d).\n", status);
  834. return status;
  835. }
  836. static int ocfs2_enable_quotas(struct ocfs2_super *osb)
  837. {
  838. struct inode *inode[OCFS2_MAXQUOTAS] = { NULL, NULL };
  839. struct super_block *sb = osb->sb;
  840. unsigned int feature[OCFS2_MAXQUOTAS] = {
  841. OCFS2_FEATURE_RO_COMPAT_USRQUOTA,
  842. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA};
  843. unsigned int ino[OCFS2_MAXQUOTAS] = {
  844. LOCAL_USER_QUOTA_SYSTEM_INODE,
  845. LOCAL_GROUP_QUOTA_SYSTEM_INODE };
  846. int status;
  847. int type;
  848. sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NEGATIVE_USAGE;
  849. for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
  850. if (!OCFS2_HAS_RO_COMPAT_FEATURE(sb, feature[type]))
  851. continue;
  852. inode[type] = ocfs2_get_system_file_inode(osb, ino[type],
  853. osb->slot_num);
  854. if (!inode[type]) {
  855. status = -ENOENT;
  856. goto out_quota_off;
  857. }
  858. status = dquot_enable(inode[type], type, QFMT_OCFS2,
  859. DQUOT_USAGE_ENABLED);
  860. if (status < 0)
  861. goto out_quota_off;
  862. }
  863. for (type = 0; type < OCFS2_MAXQUOTAS; type++)
  864. iput(inode[type]);
  865. return 0;
  866. out_quota_off:
  867. ocfs2_disable_quotas(osb);
  868. for (type = 0; type < OCFS2_MAXQUOTAS; type++)
  869. iput(inode[type]);
  870. mlog_errno(status);
  871. return status;
  872. }
  873. static void ocfs2_disable_quotas(struct ocfs2_super *osb)
  874. {
  875. int type;
  876. struct inode *inode;
  877. struct super_block *sb = osb->sb;
  878. struct ocfs2_mem_dqinfo *oinfo;
  879. /* We mostly ignore errors in this function because there's not much
  880. * we can do when we see them */
  881. for (type = 0; type < OCFS2_MAXQUOTAS; type++) {
  882. if (!sb_has_quota_loaded(sb, type))
  883. continue;
  884. /* Cancel periodic syncing before we grab dqonoff_mutex */
  885. oinfo = sb_dqinfo(sb, type)->dqi_priv;
  886. cancel_delayed_work_sync(&oinfo->dqi_sync_work);
  887. inode = igrab(sb->s_dquot.files[type]);
  888. /* Turn off quotas. This will remove all dquot structures from
  889. * memory and so they will be automatically synced to global
  890. * quota files */
  891. dquot_disable(sb, type, DQUOT_USAGE_ENABLED |
  892. DQUOT_LIMITS_ENABLED);
  893. if (!inode)
  894. continue;
  895. iput(inode);
  896. }
  897. }
  898. static int ocfs2_fill_super(struct super_block *sb, void *data, int silent)
  899. {
  900. struct dentry *root;
  901. int status, sector_size;
  902. struct mount_options parsed_options;
  903. struct inode *inode = NULL;
  904. struct ocfs2_super *osb = NULL;
  905. struct buffer_head *bh = NULL;
  906. char nodestr[12];
  907. struct ocfs2_blockcheck_stats stats;
  908. trace_ocfs2_fill_super(sb, data, silent);
  909. if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {
  910. status = -EINVAL;
  911. goto read_super_error;
  912. }
  913. /* probe for superblock */
  914. status = ocfs2_sb_probe(sb, &bh, &sector_size, &stats);
  915. if (status < 0) {
  916. mlog(ML_ERROR, "superblock probe failed!\n");
  917. goto read_super_error;
  918. }
  919. status = ocfs2_initialize_super(sb, bh, sector_size, &stats);
  920. osb = OCFS2_SB(sb);
  921. if (status < 0) {
  922. mlog_errno(status);
  923. goto read_super_error;
  924. }
  925. brelse(bh);
  926. bh = NULL;
  927. if (!ocfs2_check_set_options(sb, &parsed_options)) {
  928. status = -EINVAL;
  929. goto read_super_error;
  930. }
  931. osb->s_mount_opt = parsed_options.mount_opt;
  932. osb->s_atime_quantum = parsed_options.atime_quantum;
  933. osb->preferred_slot = parsed_options.slot;
  934. osb->osb_commit_interval = parsed_options.commit_interval;
  935. ocfs2_la_set_sizes(osb, parsed_options.localalloc_opt);
  936. osb->osb_resv_level = parsed_options.resv_level;
  937. osb->osb_dir_resv_level = parsed_options.resv_level;
  938. if (parsed_options.dir_resv_level == -1)
  939. osb->osb_dir_resv_level = parsed_options.resv_level;
  940. else
  941. osb->osb_dir_resv_level = parsed_options.dir_resv_level;
  942. status = ocfs2_verify_userspace_stack(osb, &parsed_options);
  943. if (status)
  944. goto read_super_error;
  945. sb->s_magic = OCFS2_SUPER_MAGIC;
  946. sb->s_flags = (sb->s_flags & ~(MS_POSIXACL | MS_NOSEC)) |
  947. ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  948. /* Hard readonly mode only if: bdev_read_only, MS_RDONLY,
  949. * heartbeat=none */
  950. if (bdev_read_only(sb->s_bdev)) {
  951. if (!(sb->s_flags & MS_RDONLY)) {
  952. status = -EACCES;
  953. mlog(ML_ERROR, "Readonly device detected but readonly "
  954. "mount was not specified.\n");
  955. goto read_super_error;
  956. }
  957. /* You should not be able to start a local heartbeat
  958. * on a readonly device. */
  959. if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {
  960. status = -EROFS;
  961. mlog(ML_ERROR, "Local heartbeat specified on readonly "
  962. "device.\n");
  963. goto read_super_error;
  964. }
  965. status = ocfs2_check_journals_nolocks(osb);
  966. if (status < 0) {
  967. if (status == -EROFS)
  968. mlog(ML_ERROR, "Recovery required on readonly "
  969. "file system, but write access is "
  970. "unavailable.\n");
  971. else
  972. mlog_errno(status);
  973. goto read_super_error;
  974. }
  975. ocfs2_set_ro_flag(osb, 1);
  976. printk(KERN_NOTICE "ocfs2: Readonly device (%s) detected. "
  977. "Cluster services will not be used for this mount. "
  978. "Recovery will be skipped.\n", osb->dev_str);
  979. }
  980. if (!ocfs2_is_hard_readonly(osb)) {
  981. if (sb->s_flags & MS_RDONLY)
  982. ocfs2_set_ro_flag(osb, 0);
  983. }
  984. status = ocfs2_verify_heartbeat(osb);
  985. if (status < 0) {
  986. mlog_errno(status);
  987. goto read_super_error;
  988. }
  989. osb->osb_debug_root = debugfs_create_dir(osb->uuid_str,
  990. ocfs2_debugfs_root);
  991. if (!osb->osb_debug_root) {
  992. status = -EINVAL;
  993. mlog(ML_ERROR, "Unable to create per-mount debugfs root.\n");
  994. goto read_super_error;
  995. }
  996. osb->osb_ctxt = debugfs_create_file("fs_state", S_IFREG|S_IRUSR,
  997. osb->osb_debug_root,
  998. osb,
  999. &ocfs2_osb_debug_fops);
  1000. if (!osb->osb_ctxt) {
  1001. status = -EINVAL;
  1002. mlog_errno(status);
  1003. goto read_super_error;
  1004. }
  1005. if (ocfs2_meta_ecc(osb)) {
  1006. status = ocfs2_blockcheck_stats_debugfs_install(
  1007. &osb->osb_ecc_stats,
  1008. osb->osb_debug_root);
  1009. if (status) {
  1010. mlog(ML_ERROR,
  1011. "Unable to create blockcheck statistics "
  1012. "files\n");
  1013. goto read_super_error;
  1014. }
  1015. }
  1016. status = ocfs2_mount_volume(sb);
  1017. if (status < 0)
  1018. goto read_super_error;
  1019. if (osb->root_inode)
  1020. inode = igrab(osb->root_inode);
  1021. if (!inode) {
  1022. status = -EIO;
  1023. mlog_errno(status);
  1024. goto read_super_error;
  1025. }
  1026. root = d_make_root(inode);
  1027. if (!root) {
  1028. status = -ENOMEM;
  1029. mlog_errno(status);
  1030. goto read_super_error;
  1031. }
  1032. sb->s_root = root;
  1033. ocfs2_complete_mount_recovery(osb);
  1034. if (ocfs2_mount_local(osb))
  1035. snprintf(nodestr, sizeof(nodestr), "local");
  1036. else
  1037. snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
  1038. printk(KERN_INFO "ocfs2: Mounting device (%s) on (node %s, slot %d) "
  1039. "with %s data mode.\n",
  1040. osb->dev_str, nodestr, osb->slot_num,
  1041. osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK ? "writeback" :
  1042. "ordered");
  1043. atomic_set(&osb->vol_state, VOLUME_MOUNTED);
  1044. wake_up(&osb->osb_mount_event);
  1045. /* Now we can initialize quotas because we can afford to wait
  1046. * for cluster locks recovery now. That also means that truncation
  1047. * log recovery can happen but that waits for proper quota setup */
  1048. if (!(sb->s_flags & MS_RDONLY)) {
  1049. status = ocfs2_enable_quotas(osb);
  1050. if (status < 0) {
  1051. /* We have to err-out specially here because
  1052. * s_root is already set */
  1053. mlog_errno(status);
  1054. atomic_set(&osb->vol_state, VOLUME_DISABLED);
  1055. wake_up(&osb->osb_mount_event);
  1056. return status;
  1057. }
  1058. }
  1059. ocfs2_complete_quota_recovery(osb);
  1060. /* Now we wake up again for processes waiting for quotas */
  1061. atomic_set(&osb->vol_state, VOLUME_MOUNTED_QUOTAS);
  1062. wake_up(&osb->osb_mount_event);
  1063. /* Start this when the mount is almost sure of being successful */
  1064. ocfs2_orphan_scan_start(osb);
  1065. return status;
  1066. read_super_error:
  1067. brelse(bh);
  1068. if (osb) {
  1069. atomic_set(&osb->vol_state, VOLUME_DISABLED);
  1070. wake_up(&osb->osb_mount_event);
  1071. ocfs2_dismount_volume(sb, 1);
  1072. }
  1073. if (status)
  1074. mlog_errno(status);
  1075. return status;
  1076. }
  1077. static struct dentry *ocfs2_mount(struct file_system_type *fs_type,
  1078. int flags,
  1079. const char *dev_name,
  1080. void *data)
  1081. {
  1082. return mount_bdev(fs_type, flags, dev_name, data, ocfs2_fill_super);
  1083. }
  1084. static struct file_system_type ocfs2_fs_type = {
  1085. .owner = THIS_MODULE,
  1086. .name = "ocfs2",
  1087. .mount = ocfs2_mount,
  1088. .kill_sb = kill_block_super,
  1089. .fs_flags = FS_REQUIRES_DEV|FS_RENAME_DOES_D_MOVE,
  1090. .next = NULL
  1091. };
  1092. MODULE_ALIAS_FS("ocfs2");
  1093. static int ocfs2_check_set_options(struct super_block *sb,
  1094. struct mount_options *options)
  1095. {
  1096. if (options->mount_opt & OCFS2_MOUNT_USRQUOTA &&
  1097. !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1098. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  1099. mlog(ML_ERROR, "User quotas were requested, but this "
  1100. "filesystem does not have the feature enabled.\n");
  1101. return 0;
  1102. }
  1103. if (options->mount_opt & OCFS2_MOUNT_GRPQUOTA &&
  1104. !OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1105. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  1106. mlog(ML_ERROR, "Group quotas were requested, but this "
  1107. "filesystem does not have the feature enabled.\n");
  1108. return 0;
  1109. }
  1110. if (options->mount_opt & OCFS2_MOUNT_POSIX_ACL &&
  1111. !OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR)) {
  1112. mlog(ML_ERROR, "ACL support requested but extended attributes "
  1113. "feature is not enabled\n");
  1114. return 0;
  1115. }
  1116. /* No ACL setting specified? Use XATTR feature... */
  1117. if (!(options->mount_opt & (OCFS2_MOUNT_POSIX_ACL |
  1118. OCFS2_MOUNT_NO_POSIX_ACL))) {
  1119. if (OCFS2_HAS_INCOMPAT_FEATURE(sb, OCFS2_FEATURE_INCOMPAT_XATTR))
  1120. options->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
  1121. else
  1122. options->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
  1123. }
  1124. return 1;
  1125. }
  1126. static int ocfs2_parse_options(struct super_block *sb,
  1127. char *options,
  1128. struct mount_options *mopt,
  1129. int is_remount)
  1130. {
  1131. int status, user_stack = 0;
  1132. char *p;
  1133. u32 tmp;
  1134. int token, option;
  1135. substring_t args[MAX_OPT_ARGS];
  1136. trace_ocfs2_parse_options(is_remount, options ? options : "(none)");
  1137. mopt->commit_interval = 0;
  1138. mopt->mount_opt = OCFS2_MOUNT_NOINTR;
  1139. mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
  1140. mopt->slot = OCFS2_INVALID_SLOT;
  1141. mopt->localalloc_opt = -1;
  1142. mopt->cluster_stack[0] = '\0';
  1143. mopt->resv_level = OCFS2_DEFAULT_RESV_LEVEL;
  1144. mopt->dir_resv_level = -1;
  1145. if (!options) {
  1146. status = 1;
  1147. goto bail;
  1148. }
  1149. while ((p = strsep(&options, ",")) != NULL) {
  1150. if (!*p)
  1151. continue;
  1152. token = match_token(p, tokens, args);
  1153. switch (token) {
  1154. case Opt_hb_local:
  1155. mopt->mount_opt |= OCFS2_MOUNT_HB_LOCAL;
  1156. break;
  1157. case Opt_hb_none:
  1158. mopt->mount_opt |= OCFS2_MOUNT_HB_NONE;
  1159. break;
  1160. case Opt_hb_global:
  1161. mopt->mount_opt |= OCFS2_MOUNT_HB_GLOBAL;
  1162. break;
  1163. case Opt_barrier:
  1164. if (match_int(&args[0], &option)) {
  1165. status = 0;
  1166. goto bail;
  1167. }
  1168. if (option)
  1169. mopt->mount_opt |= OCFS2_MOUNT_BARRIER;
  1170. else
  1171. mopt->mount_opt &= ~OCFS2_MOUNT_BARRIER;
  1172. break;
  1173. case Opt_intr:
  1174. mopt->mount_opt &= ~OCFS2_MOUNT_NOINTR;
  1175. break;
  1176. case Opt_nointr:
  1177. mopt->mount_opt |= OCFS2_MOUNT_NOINTR;
  1178. break;
  1179. case Opt_err_panic:
  1180. mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_CONT;
  1181. mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_ROFS;
  1182. mopt->mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
  1183. break;
  1184. case Opt_err_ro:
  1185. mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_CONT;
  1186. mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
  1187. mopt->mount_opt |= OCFS2_MOUNT_ERRORS_ROFS;
  1188. break;
  1189. case Opt_err_cont:
  1190. mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_ROFS;
  1191. mopt->mount_opt &= ~OCFS2_MOUNT_ERRORS_PANIC;
  1192. mopt->mount_opt |= OCFS2_MOUNT_ERRORS_CONT;
  1193. break;
  1194. case Opt_data_ordered:
  1195. mopt->mount_opt &= ~OCFS2_MOUNT_DATA_WRITEBACK;
  1196. break;
  1197. case Opt_data_writeback:
  1198. mopt->mount_opt |= OCFS2_MOUNT_DATA_WRITEBACK;
  1199. break;
  1200. case Opt_user_xattr:
  1201. mopt->mount_opt &= ~OCFS2_MOUNT_NOUSERXATTR;
  1202. break;
  1203. case Opt_nouser_xattr:
  1204. mopt->mount_opt |= OCFS2_MOUNT_NOUSERXATTR;
  1205. break;
  1206. case Opt_atime_quantum:
  1207. if (match_int(&args[0], &option)) {
  1208. status = 0;
  1209. goto bail;
  1210. }
  1211. if (option >= 0)
  1212. mopt->atime_quantum = option;
  1213. break;
  1214. case Opt_slot:
  1215. if (match_int(&args[0], &option)) {
  1216. status = 0;
  1217. goto bail;
  1218. }
  1219. if (option)
  1220. mopt->slot = (s16)option;
  1221. break;
  1222. case Opt_commit:
  1223. if (match_int(&args[0], &option)) {
  1224. status = 0;
  1225. goto bail;
  1226. }
  1227. if (option < 0)
  1228. return 0;
  1229. if (option == 0)
  1230. option = JBD2_DEFAULT_MAX_COMMIT_AGE;
  1231. mopt->commit_interval = HZ * option;
  1232. break;
  1233. case Opt_localalloc:
  1234. if (match_int(&args[0], &option)) {
  1235. status = 0;
  1236. goto bail;
  1237. }
  1238. if (option >= 0)
  1239. mopt->localalloc_opt = option;
  1240. break;
  1241. case Opt_localflocks:
  1242. /*
  1243. * Changing this during remount could race
  1244. * flock() requests, or "unbalance" existing
  1245. * ones (e.g., a lock is taken in one mode but
  1246. * dropped in the other). If users care enough
  1247. * to flip locking modes during remount, we
  1248. * could add a "local" flag to individual
  1249. * flock structures for proper tracking of
  1250. * state.
  1251. */
  1252. if (!is_remount)
  1253. mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
  1254. break;
  1255. case Opt_stack:
  1256. /* Check both that the option we were passed
  1257. * is of the right length and that it is a proper
  1258. * string of the right length.
  1259. */
  1260. if (((args[0].to - args[0].from) !=
  1261. OCFS2_STACK_LABEL_LEN) ||
  1262. (strnlen(args[0].from,
  1263. OCFS2_STACK_LABEL_LEN) !=
  1264. OCFS2_STACK_LABEL_LEN)) {
  1265. mlog(ML_ERROR,
  1266. "Invalid cluster_stack option\n");
  1267. status = 0;
  1268. goto bail;
  1269. }
  1270. memcpy(mopt->cluster_stack, args[0].from,
  1271. OCFS2_STACK_LABEL_LEN);
  1272. mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
  1273. /*
  1274. * Open code the memcmp here as we don't have
  1275. * an osb to pass to
  1276. * ocfs2_userspace_stack().
  1277. */
  1278. if (memcmp(mopt->cluster_stack,
  1279. OCFS2_CLASSIC_CLUSTER_STACK,
  1280. OCFS2_STACK_LABEL_LEN))
  1281. user_stack = 1;
  1282. break;
  1283. case Opt_inode64:
  1284. mopt->mount_opt |= OCFS2_MOUNT_INODE64;
  1285. break;
  1286. case Opt_usrquota:
  1287. mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
  1288. break;
  1289. case Opt_grpquota:
  1290. mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
  1291. break;
  1292. case Opt_coherency_buffered:
  1293. mopt->mount_opt |= OCFS2_MOUNT_COHERENCY_BUFFERED;
  1294. break;
  1295. case Opt_coherency_full:
  1296. mopt->mount_opt &= ~OCFS2_MOUNT_COHERENCY_BUFFERED;
  1297. break;
  1298. case Opt_acl:
  1299. mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
  1300. mopt->mount_opt &= ~OCFS2_MOUNT_NO_POSIX_ACL;
  1301. break;
  1302. case Opt_noacl:
  1303. mopt->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
  1304. mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
  1305. break;
  1306. case Opt_resv_level:
  1307. if (is_remount)
  1308. break;
  1309. if (match_int(&args[0], &option)) {
  1310. status = 0;
  1311. goto bail;
  1312. }
  1313. if (option >= OCFS2_MIN_RESV_LEVEL &&
  1314. option < OCFS2_MAX_RESV_LEVEL)
  1315. mopt->resv_level = option;
  1316. break;
  1317. case Opt_dir_resv_level:
  1318. if (is_remount)
  1319. break;
  1320. if (match_int(&args[0], &option)) {
  1321. status = 0;
  1322. goto bail;
  1323. }
  1324. if (option >= OCFS2_MIN_RESV_LEVEL &&
  1325. option < OCFS2_MAX_RESV_LEVEL)
  1326. mopt->dir_resv_level = option;
  1327. break;
  1328. case Opt_journal_async_commit:
  1329. mopt->mount_opt |= OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT;
  1330. break;
  1331. default:
  1332. mlog(ML_ERROR,
  1333. "Unrecognized mount option \"%s\" "
  1334. "or missing value\n", p);
  1335. status = 0;
  1336. goto bail;
  1337. }
  1338. }
  1339. if (user_stack == 0) {
  1340. /* Ensure only one heartbeat mode */
  1341. tmp = mopt->mount_opt & (OCFS2_MOUNT_HB_LOCAL |
  1342. OCFS2_MOUNT_HB_GLOBAL |
  1343. OCFS2_MOUNT_HB_NONE);
  1344. if (hweight32(tmp) != 1) {
  1345. mlog(ML_ERROR, "Invalid heartbeat mount options\n");
  1346. status = 0;
  1347. goto bail;
  1348. }
  1349. }
  1350. status = 1;
  1351. bail:
  1352. return status;
  1353. }
  1354. static int ocfs2_show_options(struct seq_file *s, struct dentry *root)
  1355. {
  1356. struct ocfs2_super *osb = OCFS2_SB(root->d_sb);
  1357. unsigned long opts = osb->s_mount_opt;
  1358. unsigned int local_alloc_megs;
  1359. if (opts & (OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL)) {
  1360. seq_printf(s, ",_netdev");
  1361. if (opts & OCFS2_MOUNT_HB_LOCAL)
  1362. seq_printf(s, ",%s", OCFS2_HB_LOCAL);
  1363. else
  1364. seq_printf(s, ",%s", OCFS2_HB_GLOBAL);
  1365. } else
  1366. seq_printf(s, ",%s", OCFS2_HB_NONE);
  1367. if (opts & OCFS2_MOUNT_NOINTR)
  1368. seq_printf(s, ",nointr");
  1369. if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
  1370. seq_printf(s, ",data=writeback");
  1371. else
  1372. seq_printf(s, ",data=ordered");
  1373. if (opts & OCFS2_MOUNT_BARRIER)
  1374. seq_printf(s, ",barrier=1");
  1375. if (opts & OCFS2_MOUNT_ERRORS_PANIC)
  1376. seq_printf(s, ",errors=panic");
  1377. else if (opts & OCFS2_MOUNT_ERRORS_CONT)
  1378. seq_printf(s, ",errors=continue");
  1379. else
  1380. seq_printf(s, ",errors=remount-ro");
  1381. if (osb->preferred_slot != OCFS2_INVALID_SLOT)
  1382. seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
  1383. seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
  1384. if (osb->osb_commit_interval)
  1385. seq_printf(s, ",commit=%u",
  1386. (unsigned) (osb->osb_commit_interval / HZ));
  1387. local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
  1388. if (local_alloc_megs != ocfs2_la_default_mb(osb))
  1389. seq_printf(s, ",localalloc=%d", local_alloc_megs);
  1390. if (opts & OCFS2_MOUNT_LOCALFLOCKS)
  1391. seq_printf(s, ",localflocks,");
  1392. if (osb->osb_cluster_stack[0])
  1393. seq_show_option_n(s, "cluster_stack", osb->osb_cluster_stack,
  1394. OCFS2_STACK_LABEL_LEN);
  1395. if (opts & OCFS2_MOUNT_USRQUOTA)
  1396. seq_printf(s, ",usrquota");
  1397. if (opts & OCFS2_MOUNT_GRPQUOTA)
  1398. seq_printf(s, ",grpquota");
  1399. if (opts & OCFS2_MOUNT_COHERENCY_BUFFERED)
  1400. seq_printf(s, ",coherency=buffered");
  1401. else
  1402. seq_printf(s, ",coherency=full");
  1403. if (opts & OCFS2_MOUNT_NOUSERXATTR)
  1404. seq_printf(s, ",nouser_xattr");
  1405. else
  1406. seq_printf(s, ",user_xattr");
  1407. if (opts & OCFS2_MOUNT_INODE64)
  1408. seq_printf(s, ",inode64");
  1409. if (opts & OCFS2_MOUNT_POSIX_ACL)
  1410. seq_printf(s, ",acl");
  1411. else
  1412. seq_printf(s, ",noacl");
  1413. if (osb->osb_resv_level != OCFS2_DEFAULT_RESV_LEVEL)
  1414. seq_printf(s, ",resv_level=%d", osb->osb_resv_level);
  1415. if (osb->osb_dir_resv_level != osb->osb_resv_level)
  1416. seq_printf(s, ",dir_resv_level=%d", osb->osb_resv_level);
  1417. if (opts & OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT)
  1418. seq_printf(s, ",journal_async_commit");
  1419. return 0;
  1420. }
  1421. static int __init ocfs2_init(void)
  1422. {
  1423. int status;
  1424. status = init_ocfs2_uptodate_cache();
  1425. if (status < 0)
  1426. goto out1;
  1427. status = ocfs2_initialize_mem_caches();
  1428. if (status < 0)
  1429. goto out2;
  1430. ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
  1431. if (!ocfs2_wq) {
  1432. status = -ENOMEM;
  1433. goto out3;
  1434. }
  1435. ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
  1436. if (!ocfs2_debugfs_root) {
  1437. status = -ENOMEM;
  1438. mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
  1439. goto out4;
  1440. }
  1441. ocfs2_set_locking_protocol();
  1442. status = register_quota_format(&ocfs2_quota_format);
  1443. if (status < 0)
  1444. goto out4;
  1445. status = register_filesystem(&ocfs2_fs_type);
  1446. if (!status)
  1447. return 0;
  1448. unregister_quota_format(&ocfs2_quota_format);
  1449. out4:
  1450. destroy_workqueue(ocfs2_wq);
  1451. debugfs_remove(ocfs2_debugfs_root);
  1452. out3:
  1453. ocfs2_free_mem_caches();
  1454. out2:
  1455. exit_ocfs2_uptodate_cache();
  1456. out1:
  1457. mlog_errno(status);
  1458. return status;
  1459. }
  1460. static void __exit ocfs2_exit(void)
  1461. {
  1462. if (ocfs2_wq) {
  1463. flush_workqueue(ocfs2_wq);
  1464. destroy_workqueue(ocfs2_wq);
  1465. }
  1466. unregister_quota_format(&ocfs2_quota_format);
  1467. debugfs_remove(ocfs2_debugfs_root);
  1468. ocfs2_free_mem_caches();
  1469. unregister_filesystem(&ocfs2_fs_type);
  1470. exit_ocfs2_uptodate_cache();
  1471. }
  1472. static void ocfs2_put_super(struct super_block *sb)
  1473. {
  1474. trace_ocfs2_put_super(sb);
  1475. ocfs2_sync_blockdev(sb);
  1476. ocfs2_dismount_volume(sb, 0);
  1477. }
  1478. static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  1479. {
  1480. struct ocfs2_super *osb;
  1481. u32 numbits, freebits;
  1482. int status;
  1483. struct ocfs2_dinode *bm_lock;
  1484. struct buffer_head *bh = NULL;
  1485. struct inode *inode = NULL;
  1486. trace_ocfs2_statfs(dentry->d_sb, buf);
  1487. osb = OCFS2_SB(dentry->d_sb);
  1488. inode = ocfs2_get_system_file_inode(osb,
  1489. GLOBAL_BITMAP_SYSTEM_INODE,
  1490. OCFS2_INVALID_SLOT);
  1491. if (!inode) {
  1492. mlog(ML_ERROR, "failed to get bitmap inode\n");
  1493. status = -EIO;
  1494. goto bail;
  1495. }
  1496. status = ocfs2_inode_lock(inode, &bh, 0);
  1497. if (status < 0) {
  1498. mlog_errno(status);
  1499. goto bail;
  1500. }
  1501. bm_lock = (struct ocfs2_dinode *) bh->b_data;
  1502. numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
  1503. freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
  1504. buf->f_type = OCFS2_SUPER_MAGIC;
  1505. buf->f_bsize = dentry->d_sb->s_blocksize;
  1506. buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
  1507. buf->f_blocks = ((sector_t) numbits) *
  1508. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  1509. buf->f_bfree = ((sector_t) freebits) *
  1510. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  1511. buf->f_bavail = buf->f_bfree;
  1512. buf->f_files = numbits;
  1513. buf->f_ffree = freebits;
  1514. buf->f_fsid.val[0] = crc32_le(0, osb->uuid_str, OCFS2_VOL_UUID_LEN)
  1515. & 0xFFFFFFFFUL;
  1516. buf->f_fsid.val[1] = crc32_le(0, osb->uuid_str + OCFS2_VOL_UUID_LEN,
  1517. OCFS2_VOL_UUID_LEN) & 0xFFFFFFFFUL;
  1518. brelse(bh);
  1519. ocfs2_inode_unlock(inode, 0);
  1520. status = 0;
  1521. bail:
  1522. iput(inode);
  1523. if (status)
  1524. mlog_errno(status);
  1525. return status;
  1526. }
  1527. static void ocfs2_inode_init_once(void *data)
  1528. {
  1529. struct ocfs2_inode_info *oi = data;
  1530. oi->ip_flags = 0;
  1531. oi->ip_open_count = 0;
  1532. spin_lock_init(&oi->ip_lock);
  1533. ocfs2_extent_map_init(&oi->vfs_inode);
  1534. INIT_LIST_HEAD(&oi->ip_io_markers);
  1535. oi->ip_dir_start_lookup = 0;
  1536. mutex_init(&oi->ip_unaligned_aio);
  1537. init_rwsem(&oi->ip_alloc_sem);
  1538. init_rwsem(&oi->ip_xattr_sem);
  1539. mutex_init(&oi->ip_io_mutex);
  1540. oi->ip_blkno = 0ULL;
  1541. oi->ip_clusters = 0;
  1542. ocfs2_resv_init_once(&oi->ip_la_data_resv);
  1543. ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
  1544. ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
  1545. ocfs2_lock_res_init_once(&oi->ip_open_lockres);
  1546. ocfs2_metadata_cache_init(INODE_CACHE(&oi->vfs_inode),
  1547. &ocfs2_inode_caching_ops);
  1548. inode_init_once(&oi->vfs_inode);
  1549. }
  1550. static int ocfs2_initialize_mem_caches(void)
  1551. {
  1552. ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
  1553. sizeof(struct ocfs2_inode_info),
  1554. 0,
  1555. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1556. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  1557. ocfs2_inode_init_once);
  1558. ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
  1559. sizeof(struct ocfs2_dquot),
  1560. 0,
  1561. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1562. SLAB_MEM_SPREAD),
  1563. NULL);
  1564. ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
  1565. sizeof(struct ocfs2_quota_chunk),
  1566. 0,
  1567. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  1568. NULL);
  1569. if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
  1570. !ocfs2_qf_chunk_cachep) {
  1571. if (ocfs2_inode_cachep)
  1572. kmem_cache_destroy(ocfs2_inode_cachep);
  1573. if (ocfs2_dquot_cachep)
  1574. kmem_cache_destroy(ocfs2_dquot_cachep);
  1575. if (ocfs2_qf_chunk_cachep)
  1576. kmem_cache_destroy(ocfs2_qf_chunk_cachep);
  1577. return -ENOMEM;
  1578. }
  1579. return 0;
  1580. }
  1581. static void ocfs2_free_mem_caches(void)
  1582. {
  1583. /*
  1584. * Make sure all delayed rcu free inodes are flushed before we
  1585. * destroy cache.
  1586. */
  1587. rcu_barrier();
  1588. if (ocfs2_inode_cachep)
  1589. kmem_cache_destroy(ocfs2_inode_cachep);
  1590. ocfs2_inode_cachep = NULL;
  1591. if (ocfs2_dquot_cachep)
  1592. kmem_cache_destroy(ocfs2_dquot_cachep);
  1593. ocfs2_dquot_cachep = NULL;
  1594. if (ocfs2_qf_chunk_cachep)
  1595. kmem_cache_destroy(ocfs2_qf_chunk_cachep);
  1596. ocfs2_qf_chunk_cachep = NULL;
  1597. }
  1598. static int ocfs2_get_sector(struct super_block *sb,
  1599. struct buffer_head **bh,
  1600. int block,
  1601. int sect_size)
  1602. {
  1603. if (!sb_set_blocksize(sb, sect_size)) {
  1604. mlog(ML_ERROR, "unable to set blocksize\n");
  1605. return -EIO;
  1606. }
  1607. *bh = sb_getblk(sb, block);
  1608. if (!*bh) {
  1609. mlog_errno(-ENOMEM);
  1610. return -ENOMEM;
  1611. }
  1612. lock_buffer(*bh);
  1613. if (!buffer_dirty(*bh))
  1614. clear_buffer_uptodate(*bh);
  1615. unlock_buffer(*bh);
  1616. ll_rw_block(READ, 1, bh);
  1617. wait_on_buffer(*bh);
  1618. if (!buffer_uptodate(*bh)) {
  1619. mlog_errno(-EIO);
  1620. brelse(*bh);
  1621. *bh = NULL;
  1622. return -EIO;
  1623. }
  1624. return 0;
  1625. }
  1626. static int ocfs2_mount_volume(struct super_block *sb)
  1627. {
  1628. int status = 0;
  1629. int unlock_super = 0;
  1630. struct ocfs2_super *osb = OCFS2_SB(sb);
  1631. if (ocfs2_is_hard_readonly(osb))
  1632. goto leave;
  1633. status = ocfs2_dlm_init(osb);
  1634. if (status < 0) {
  1635. mlog_errno(status);
  1636. goto leave;
  1637. }
  1638. status = ocfs2_super_lock(osb, 1);
  1639. if (status < 0) {
  1640. mlog_errno(status);
  1641. goto leave;
  1642. }
  1643. unlock_super = 1;
  1644. /* This will load up the node map and add ourselves to it. */
  1645. status = ocfs2_find_slot(osb);
  1646. if (status < 0) {
  1647. mlog_errno(status);
  1648. goto leave;
  1649. }
  1650. /* load all node-local system inodes */
  1651. status = ocfs2_init_local_system_inodes(osb);
  1652. if (status < 0) {
  1653. mlog_errno(status);
  1654. goto leave;
  1655. }
  1656. status = ocfs2_check_volume(osb);
  1657. if (status < 0) {
  1658. mlog_errno(status);
  1659. goto leave;
  1660. }
  1661. status = ocfs2_truncate_log_init(osb);
  1662. if (status < 0)
  1663. mlog_errno(status);
  1664. leave:
  1665. if (unlock_super)
  1666. ocfs2_super_unlock(osb, 1);
  1667. return status;
  1668. }
  1669. static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
  1670. {
  1671. int tmp, hangup_needed = 0;
  1672. struct ocfs2_super *osb = NULL;
  1673. char nodestr[12];
  1674. trace_ocfs2_dismount_volume(sb);
  1675. BUG_ON(!sb);
  1676. osb = OCFS2_SB(sb);
  1677. BUG_ON(!osb);
  1678. debugfs_remove(osb->osb_ctxt);
  1679. /* Orphan scan should be stopped as early as possible */
  1680. ocfs2_orphan_scan_stop(osb);
  1681. ocfs2_disable_quotas(osb);
  1682. /* All dquots should be freed by now */
  1683. WARN_ON(!llist_empty(&osb->dquot_drop_list));
  1684. /* Wait for worker to be done with the work structure in osb */
  1685. cancel_work_sync(&osb->dquot_drop_work);
  1686. ocfs2_shutdown_local_alloc(osb);
  1687. ocfs2_truncate_log_shutdown(osb);
  1688. /* This will disable recovery and flush any recovery work. */
  1689. ocfs2_recovery_exit(osb);
  1690. ocfs2_journal_shutdown(osb);
  1691. ocfs2_sync_blockdev(sb);
  1692. ocfs2_purge_refcount_trees(osb);
  1693. /* No cluster connection means we've failed during mount, so skip
  1694. * all the steps which depended on that to complete. */
  1695. if (osb->cconn) {
  1696. tmp = ocfs2_super_lock(osb, 1);
  1697. if (tmp < 0) {
  1698. mlog_errno(tmp);
  1699. return;
  1700. }
  1701. }
  1702. if (osb->slot_num != OCFS2_INVALID_SLOT)
  1703. ocfs2_put_slot(osb);
  1704. if (osb->cconn)
  1705. ocfs2_super_unlock(osb, 1);
  1706. ocfs2_release_system_inodes(osb);
  1707. /*
  1708. * If we're dismounting due to mount error, mount.ocfs2 will clean
  1709. * up heartbeat. If we're a local mount, there is no heartbeat.
  1710. * If we failed before we got a uuid_str yet, we can't stop
  1711. * heartbeat. Otherwise, do it.
  1712. */
  1713. if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str &&
  1714. !ocfs2_is_hard_readonly(osb))
  1715. hangup_needed = 1;
  1716. if (osb->cconn)
  1717. ocfs2_dlm_shutdown(osb, hangup_needed);
  1718. ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
  1719. debugfs_remove(osb->osb_debug_root);
  1720. if (hangup_needed)
  1721. ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
  1722. atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
  1723. if (ocfs2_mount_local(osb))
  1724. snprintf(nodestr, sizeof(nodestr), "local");
  1725. else
  1726. snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
  1727. printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
  1728. osb->dev_str, nodestr);
  1729. ocfs2_delete_osb(osb);
  1730. kfree(osb);
  1731. sb->s_dev = 0;
  1732. sb->s_fs_info = NULL;
  1733. }
  1734. static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
  1735. unsigned uuid_bytes)
  1736. {
  1737. int i, ret;
  1738. char *ptr;
  1739. BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
  1740. osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
  1741. if (osb->uuid_str == NULL)
  1742. return -ENOMEM;
  1743. for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
  1744. /* print with null */
  1745. ret = snprintf(ptr, 3, "%02X", uuid[i]);
  1746. if (ret != 2) /* drop super cleans up */
  1747. return -EINVAL;
  1748. /* then only advance past the last char */
  1749. ptr += 2;
  1750. }
  1751. return 0;
  1752. }
  1753. /* Make sure entire volume is addressable by our journal. Requires
  1754. osb_clusters_at_boot to be valid and for the journal to have been
  1755. initialized by ocfs2_journal_init(). */
  1756. static int ocfs2_journal_addressable(struct ocfs2_super *osb)
  1757. {
  1758. int status = 0;
  1759. u64 max_block =
  1760. ocfs2_clusters_to_blocks(osb->sb,
  1761. osb->osb_clusters_at_boot) - 1;
  1762. /* 32-bit block number is always OK. */
  1763. if (max_block <= (u32)~0ULL)
  1764. goto out;
  1765. /* Volume is "huge", so see if our journal is new enough to
  1766. support it. */
  1767. if (!(OCFS2_HAS_COMPAT_FEATURE(osb->sb,
  1768. OCFS2_FEATURE_COMPAT_JBD2_SB) &&
  1769. jbd2_journal_check_used_features(osb->journal->j_journal, 0, 0,
  1770. JBD2_FEATURE_INCOMPAT_64BIT))) {
  1771. mlog(ML_ERROR, "The journal cannot address the entire volume. "
  1772. "Enable the 'block64' journal option with tunefs.ocfs2");
  1773. status = -EFBIG;
  1774. goto out;
  1775. }
  1776. out:
  1777. return status;
  1778. }
  1779. static int ocfs2_initialize_super(struct super_block *sb,
  1780. struct buffer_head *bh,
  1781. int sector_size,
  1782. struct ocfs2_blockcheck_stats *stats)
  1783. {
  1784. int status;
  1785. int i, cbits, bbits;
  1786. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1787. struct inode *inode = NULL;
  1788. struct ocfs2_journal *journal;
  1789. struct ocfs2_super *osb;
  1790. u64 total_blocks;
  1791. osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
  1792. if (!osb) {
  1793. status = -ENOMEM;
  1794. mlog_errno(status);
  1795. goto bail;
  1796. }
  1797. sb->s_fs_info = osb;
  1798. sb->s_op = &ocfs2_sops;
  1799. sb->s_d_op = &ocfs2_dentry_ops;
  1800. sb->s_export_op = &ocfs2_export_ops;
  1801. sb->s_qcop = &dquot_quotactl_sysfile_ops;
  1802. sb->dq_op = &ocfs2_quota_operations;
  1803. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
  1804. sb->s_xattr = ocfs2_xattr_handlers;
  1805. sb->s_time_gran = 1;
  1806. sb->s_flags |= MS_NOATIME;
  1807. /* this is needed to support O_LARGEFILE */
  1808. cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
  1809. bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
  1810. sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
  1811. memcpy(sb->s_uuid, di->id2.i_super.s_uuid,
  1812. sizeof(di->id2.i_super.s_uuid));
  1813. osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
  1814. for (i = 0; i < 3; i++)
  1815. osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
  1816. osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
  1817. osb->sb = sb;
  1818. /* Save off for ocfs2_rw_direct */
  1819. osb->s_sectsize_bits = blksize_bits(sector_size);
  1820. BUG_ON(!osb->s_sectsize_bits);
  1821. spin_lock_init(&osb->dc_task_lock);
  1822. init_waitqueue_head(&osb->dc_event);
  1823. osb->dc_work_sequence = 0;
  1824. osb->dc_wake_sequence = 0;
  1825. INIT_LIST_HEAD(&osb->blocked_lock_list);
  1826. osb->blocked_lock_count = 0;
  1827. spin_lock_init(&osb->osb_lock);
  1828. spin_lock_init(&osb->osb_xattr_lock);
  1829. ocfs2_init_steal_slots(osb);
  1830. mutex_init(&osb->system_file_mutex);
  1831. atomic_set(&osb->alloc_stats.moves, 0);
  1832. atomic_set(&osb->alloc_stats.local_data, 0);
  1833. atomic_set(&osb->alloc_stats.bitmap_data, 0);
  1834. atomic_set(&osb->alloc_stats.bg_allocs, 0);
  1835. atomic_set(&osb->alloc_stats.bg_extends, 0);
  1836. /* Copy the blockcheck stats from the superblock probe */
  1837. osb->osb_ecc_stats = *stats;
  1838. ocfs2_init_node_maps(osb);
  1839. snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
  1840. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1841. osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
  1842. if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
  1843. mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
  1844. osb->max_slots);
  1845. status = -EINVAL;
  1846. goto bail;
  1847. }
  1848. ocfs2_orphan_scan_init(osb);
  1849. status = ocfs2_recovery_init(osb);
  1850. if (status) {
  1851. mlog(ML_ERROR, "Unable to initialize recovery state\n");
  1852. mlog_errno(status);
  1853. goto bail;
  1854. }
  1855. init_waitqueue_head(&osb->checkpoint_event);
  1856. osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
  1857. osb->slot_num = OCFS2_INVALID_SLOT;
  1858. osb->s_xattr_inline_size = le16_to_cpu(
  1859. di->id2.i_super.s_xattr_inline_size);
  1860. osb->local_alloc_state = OCFS2_LA_UNUSED;
  1861. osb->local_alloc_bh = NULL;
  1862. INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
  1863. init_waitqueue_head(&osb->osb_mount_event);
  1864. status = ocfs2_resmap_init(osb, &osb->osb_la_resmap);
  1865. if (status) {
  1866. mlog_errno(status);
  1867. goto bail;
  1868. }
  1869. osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
  1870. if (!osb->vol_label) {
  1871. mlog(ML_ERROR, "unable to alloc vol label\n");
  1872. status = -ENOMEM;
  1873. goto bail;
  1874. }
  1875. osb->slot_recovery_generations =
  1876. kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
  1877. GFP_KERNEL);
  1878. if (!osb->slot_recovery_generations) {
  1879. status = -ENOMEM;
  1880. mlog_errno(status);
  1881. goto bail;
  1882. }
  1883. init_waitqueue_head(&osb->osb_wipe_event);
  1884. osb->osb_orphan_wipes = kcalloc(osb->max_slots,
  1885. sizeof(*osb->osb_orphan_wipes),
  1886. GFP_KERNEL);
  1887. if (!osb->osb_orphan_wipes) {
  1888. status = -ENOMEM;
  1889. mlog_errno(status);
  1890. goto bail;
  1891. }
  1892. osb->osb_rf_lock_tree = RB_ROOT;
  1893. osb->s_feature_compat =
  1894. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
  1895. osb->s_feature_ro_compat =
  1896. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
  1897. osb->s_feature_incompat =
  1898. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
  1899. if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
  1900. mlog(ML_ERROR, "couldn't mount because of unsupported "
  1901. "optional features (%x).\n", i);
  1902. status = -EINVAL;
  1903. goto bail;
  1904. }
  1905. if (!(osb->sb->s_flags & MS_RDONLY) &&
  1906. (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
  1907. mlog(ML_ERROR, "couldn't mount RDWR because of "
  1908. "unsupported optional features (%x).\n", i);
  1909. status = -EINVAL;
  1910. goto bail;
  1911. }
  1912. if (ocfs2_clusterinfo_valid(osb)) {
  1913. osb->osb_stackflags =
  1914. OCFS2_RAW_SB(di)->s_cluster_info.ci_stackflags;
  1915. strlcpy(osb->osb_cluster_stack,
  1916. OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
  1917. OCFS2_STACK_LABEL_LEN + 1);
  1918. if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
  1919. mlog(ML_ERROR,
  1920. "couldn't mount because of an invalid "
  1921. "cluster stack label (%s) \n",
  1922. osb->osb_cluster_stack);
  1923. status = -EINVAL;
  1924. goto bail;
  1925. }
  1926. strlcpy(osb->osb_cluster_name,
  1927. OCFS2_RAW_SB(di)->s_cluster_info.ci_cluster,
  1928. OCFS2_CLUSTER_NAME_LEN + 1);
  1929. } else {
  1930. /* The empty string is identical with classic tools that
  1931. * don't know about s_cluster_info. */
  1932. osb->osb_cluster_stack[0] = '\0';
  1933. }
  1934. get_random_bytes(&osb->s_next_generation, sizeof(u32));
  1935. /* FIXME
  1936. * This should be done in ocfs2_journal_init(), but unknown
  1937. * ordering issues will cause the filesystem to crash.
  1938. * If anyone wants to figure out what part of the code
  1939. * refers to osb->journal before ocfs2_journal_init() is run,
  1940. * be my guest.
  1941. */
  1942. /* initialize our journal structure */
  1943. journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
  1944. if (!journal) {
  1945. mlog(ML_ERROR, "unable to alloc journal\n");
  1946. status = -ENOMEM;
  1947. goto bail;
  1948. }
  1949. osb->journal = journal;
  1950. journal->j_osb = osb;
  1951. atomic_set(&journal->j_num_trans, 0);
  1952. init_rwsem(&journal->j_trans_barrier);
  1953. init_waitqueue_head(&journal->j_checkpointed);
  1954. spin_lock_init(&journal->j_lock);
  1955. journal->j_trans_id = (unsigned long) 1;
  1956. INIT_LIST_HEAD(&journal->j_la_cleanups);
  1957. INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
  1958. journal->j_state = OCFS2_JOURNAL_FREE;
  1959. INIT_WORK(&osb->dquot_drop_work, ocfs2_drop_dquot_refs);
  1960. init_llist_head(&osb->dquot_drop_list);
  1961. /* get some pseudo constants for clustersize bits */
  1962. osb->s_clustersize_bits =
  1963. le32_to_cpu(di->id2.i_super.s_clustersize_bits);
  1964. osb->s_clustersize = 1 << osb->s_clustersize_bits;
  1965. if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
  1966. osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
  1967. mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
  1968. osb->s_clustersize);
  1969. status = -EINVAL;
  1970. goto bail;
  1971. }
  1972. total_blocks = ocfs2_clusters_to_blocks(osb->sb,
  1973. le32_to_cpu(di->i_clusters));
  1974. status = generic_check_addressable(osb->sb->s_blocksize_bits,
  1975. total_blocks);
  1976. if (status) {
  1977. mlog(ML_ERROR, "Volume too large "
  1978. "to mount safely on this system");
  1979. status = -EFBIG;
  1980. goto bail;
  1981. }
  1982. if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
  1983. sizeof(di->id2.i_super.s_uuid))) {
  1984. mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
  1985. status = -ENOMEM;
  1986. goto bail;
  1987. }
  1988. strlcpy(osb->vol_label, di->id2.i_super.s_label,
  1989. OCFS2_MAX_VOL_LABEL_LEN);
  1990. osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
  1991. osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
  1992. osb->first_cluster_group_blkno =
  1993. le64_to_cpu(di->id2.i_super.s_first_cluster_group);
  1994. osb->fs_generation = le32_to_cpu(di->i_fs_generation);
  1995. osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
  1996. trace_ocfs2_initialize_super(osb->vol_label, osb->uuid_str,
  1997. (unsigned long long)osb->root_blkno,
  1998. (unsigned long long)osb->system_dir_blkno,
  1999. osb->s_clustersize_bits);
  2000. osb->osb_dlm_debug = ocfs2_new_dlm_debug();
  2001. if (!osb->osb_dlm_debug) {
  2002. status = -ENOMEM;
  2003. mlog_errno(status);
  2004. goto bail;
  2005. }
  2006. atomic_set(&osb->vol_state, VOLUME_INIT);
  2007. /* load root, system_dir, and all global system inodes */
  2008. status = ocfs2_init_global_system_inodes(osb);
  2009. if (status < 0) {
  2010. mlog_errno(status);
  2011. goto bail;
  2012. }
  2013. /*
  2014. * global bitmap
  2015. */
  2016. inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
  2017. OCFS2_INVALID_SLOT);
  2018. if (!inode) {
  2019. status = -EINVAL;
  2020. mlog_errno(status);
  2021. goto bail;
  2022. }
  2023. osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
  2024. osb->osb_clusters_at_boot = OCFS2_I(inode)->ip_clusters;
  2025. iput(inode);
  2026. osb->bitmap_cpg = ocfs2_group_bitmap_size(sb, 0,
  2027. osb->s_feature_incompat) * 8;
  2028. status = ocfs2_init_slot_info(osb);
  2029. if (status < 0) {
  2030. mlog_errno(status);
  2031. goto bail;
  2032. }
  2033. cleancache_init_shared_fs(sb);
  2034. bail:
  2035. return status;
  2036. }
  2037. /*
  2038. * will return: -EAGAIN if it is ok to keep searching for superblocks
  2039. * -EINVAL if there is a bad superblock
  2040. * 0 on success
  2041. */
  2042. static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  2043. struct buffer_head *bh,
  2044. u32 blksz,
  2045. struct ocfs2_blockcheck_stats *stats)
  2046. {
  2047. int status = -EAGAIN;
  2048. if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
  2049. strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
  2050. /* We have to do a raw check of the feature here */
  2051. if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
  2052. OCFS2_FEATURE_INCOMPAT_META_ECC) {
  2053. status = ocfs2_block_check_validate(bh->b_data,
  2054. bh->b_size,
  2055. &di->i_check,
  2056. stats);
  2057. if (status)
  2058. goto out;
  2059. }
  2060. status = -EINVAL;
  2061. if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
  2062. mlog(ML_ERROR, "found superblock with incorrect block "
  2063. "size: found %u, should be %u\n",
  2064. 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
  2065. blksz);
  2066. } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
  2067. OCFS2_MAJOR_REV_LEVEL ||
  2068. le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
  2069. OCFS2_MINOR_REV_LEVEL) {
  2070. mlog(ML_ERROR, "found superblock with bad version: "
  2071. "found %u.%u, should be %u.%u\n",
  2072. le16_to_cpu(di->id2.i_super.s_major_rev_level),
  2073. le16_to_cpu(di->id2.i_super.s_minor_rev_level),
  2074. OCFS2_MAJOR_REV_LEVEL,
  2075. OCFS2_MINOR_REV_LEVEL);
  2076. } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
  2077. mlog(ML_ERROR, "bad block number on superblock: "
  2078. "found %llu, should be %llu\n",
  2079. (unsigned long long)le64_to_cpu(di->i_blkno),
  2080. (unsigned long long)bh->b_blocknr);
  2081. } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
  2082. le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
  2083. mlog(ML_ERROR, "bad cluster size found: %u\n",
  2084. 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
  2085. } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
  2086. mlog(ML_ERROR, "bad root_blkno: 0\n");
  2087. } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
  2088. mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
  2089. } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
  2090. mlog(ML_ERROR,
  2091. "Superblock slots found greater than file system "
  2092. "maximum: found %u, max %u\n",
  2093. le16_to_cpu(di->id2.i_super.s_max_slots),
  2094. OCFS2_MAX_SLOTS);
  2095. } else {
  2096. /* found it! */
  2097. status = 0;
  2098. }
  2099. }
  2100. out:
  2101. if (status && status != -EAGAIN)
  2102. mlog_errno(status);
  2103. return status;
  2104. }
  2105. static int ocfs2_check_volume(struct ocfs2_super *osb)
  2106. {
  2107. int status;
  2108. int dirty;
  2109. int local;
  2110. struct ocfs2_dinode *local_alloc = NULL; /* only used if we
  2111. * recover
  2112. * ourselves. */
  2113. /* Init our journal object. */
  2114. status = ocfs2_journal_init(osb->journal, &dirty);
  2115. if (status < 0) {
  2116. mlog(ML_ERROR, "Could not initialize journal!\n");
  2117. goto finally;
  2118. }
  2119. /* Now that journal has been initialized, check to make sure
  2120. entire volume is addressable. */
  2121. status = ocfs2_journal_addressable(osb);
  2122. if (status)
  2123. goto finally;
  2124. /* If the journal was unmounted cleanly then we don't want to
  2125. * recover anything. Otherwise, journal_load will do that
  2126. * dirty work for us :) */
  2127. if (!dirty) {
  2128. status = ocfs2_journal_wipe(osb->journal, 0);
  2129. if (status < 0) {
  2130. mlog_errno(status);
  2131. goto finally;
  2132. }
  2133. } else {
  2134. printk(KERN_NOTICE "ocfs2: File system on device (%s) was not "
  2135. "unmounted cleanly, recovering it.\n", osb->dev_str);
  2136. }
  2137. local = ocfs2_mount_local(osb);
  2138. /* will play back anything left in the journal. */
  2139. status = ocfs2_journal_load(osb->journal, local, dirty);
  2140. if (status < 0) {
  2141. mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
  2142. goto finally;
  2143. }
  2144. if (osb->s_mount_opt & OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT)
  2145. jbd2_journal_set_features(osb->journal->j_journal,
  2146. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2147. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2148. else
  2149. jbd2_journal_clear_features(osb->journal->j_journal,
  2150. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2151. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2152. if (dirty) {
  2153. /* recover my local alloc if we didn't unmount cleanly. */
  2154. status = ocfs2_begin_local_alloc_recovery(osb,
  2155. osb->slot_num,
  2156. &local_alloc);
  2157. if (status < 0) {
  2158. mlog_errno(status);
  2159. goto finally;
  2160. }
  2161. /* we complete the recovery process after we've marked
  2162. * ourselves as mounted. */
  2163. }
  2164. status = ocfs2_load_local_alloc(osb);
  2165. if (status < 0) {
  2166. mlog_errno(status);
  2167. goto finally;
  2168. }
  2169. if (dirty) {
  2170. /* Recovery will be completed after we've mounted the
  2171. * rest of the volume. */
  2172. osb->dirty = 1;
  2173. osb->local_alloc_copy = local_alloc;
  2174. local_alloc = NULL;
  2175. }
  2176. /* go through each journal, trylock it and if you get the
  2177. * lock, and it's marked as dirty, set the bit in the recover
  2178. * map and launch a recovery thread for it. */
  2179. status = ocfs2_mark_dead_nodes(osb);
  2180. if (status < 0) {
  2181. mlog_errno(status);
  2182. goto finally;
  2183. }
  2184. status = ocfs2_compute_replay_slots(osb);
  2185. if (status < 0)
  2186. mlog_errno(status);
  2187. finally:
  2188. kfree(local_alloc);
  2189. if (status)
  2190. mlog_errno(status);
  2191. return status;
  2192. }
  2193. /*
  2194. * The routine gets called from dismount or close whenever a dismount on
  2195. * volume is requested and the osb open count becomes 1.
  2196. * It will remove the osb from the global list and also free up all the
  2197. * initialized resources and fileobject.
  2198. */
  2199. static void ocfs2_delete_osb(struct ocfs2_super *osb)
  2200. {
  2201. /* This function assumes that the caller has the main osb resource */
  2202. ocfs2_free_slot_info(osb);
  2203. kfree(osb->osb_orphan_wipes);
  2204. kfree(osb->slot_recovery_generations);
  2205. /* FIXME
  2206. * This belongs in journal shutdown, but because we have to
  2207. * allocate osb->journal at the start of ocfs2_initialize_osb(),
  2208. * we free it here.
  2209. */
  2210. kfree(osb->journal);
  2211. kfree(osb->local_alloc_copy);
  2212. kfree(osb->uuid_str);
  2213. kfree(osb->vol_label);
  2214. ocfs2_put_dlm_debug(osb->osb_dlm_debug);
  2215. memset(osb, 0, sizeof(struct ocfs2_super));
  2216. }
  2217. /* Depending on the mount option passed, perform one of the following:
  2218. * Put OCFS2 into a readonly state (default)
  2219. * Return EIO so that only the process errs
  2220. * Fix the error as if fsck.ocfs2 -y
  2221. * panic
  2222. */
  2223. static int ocfs2_handle_error(struct super_block *sb)
  2224. {
  2225. struct ocfs2_super *osb = OCFS2_SB(sb);
  2226. int rv = 0;
  2227. ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
  2228. pr_crit("On-disk corruption discovered. "
  2229. "Please run fsck.ocfs2 once the filesystem is unmounted.\n");
  2230. if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC) {
  2231. panic("OCFS2: (device %s): panic forced after error\n",
  2232. sb->s_id);
  2233. } else if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_CONT) {
  2234. pr_crit("OCFS2: Returning error to the calling process.\n");
  2235. rv = -EIO;
  2236. } else { /* default option */
  2237. rv = -EROFS;
  2238. if (sb->s_flags & MS_RDONLY &&
  2239. (ocfs2_is_soft_readonly(osb) ||
  2240. ocfs2_is_hard_readonly(osb)))
  2241. return rv;
  2242. pr_crit("OCFS2: File system is now read-only.\n");
  2243. sb->s_flags |= MS_RDONLY;
  2244. ocfs2_set_ro_flag(osb, 0);
  2245. }
  2246. return rv;
  2247. }
  2248. int __ocfs2_error(struct super_block *sb, const char *function,
  2249. const char *fmt, ...)
  2250. {
  2251. struct va_format vaf;
  2252. va_list args;
  2253. va_start(args, fmt);
  2254. vaf.fmt = fmt;
  2255. vaf.va = &args;
  2256. /* Not using mlog here because we want to show the actual
  2257. * function the error came from. */
  2258. printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %pV",
  2259. sb->s_id, function, &vaf);
  2260. va_end(args);
  2261. return ocfs2_handle_error(sb);
  2262. }
  2263. /* Handle critical errors. This is intentionally more drastic than
  2264. * ocfs2_handle_error, so we only use for things like journal errors,
  2265. * etc. */
  2266. void __ocfs2_abort(struct super_block *sb, const char *function,
  2267. const char *fmt, ...)
  2268. {
  2269. struct va_format vaf;
  2270. va_list args;
  2271. va_start(args, fmt);
  2272. vaf.fmt = fmt;
  2273. vaf.va = &args;
  2274. printk(KERN_CRIT "OCFS2: abort (device %s): %s: %pV",
  2275. sb->s_id, function, &vaf);
  2276. va_end(args);
  2277. /* We don't have the cluster support yet to go straight to
  2278. * hard readonly in here. Until then, we want to keep
  2279. * ocfs2_abort() so that we can at least mark critical
  2280. * errors.
  2281. *
  2282. * TODO: This should abort the journal and alert other nodes
  2283. * that our slot needs recovery. */
  2284. /* Force a panic(). This stinks, but it's better than letting
  2285. * things continue without having a proper hard readonly
  2286. * here. */
  2287. if (!ocfs2_mount_local(OCFS2_SB(sb)))
  2288. OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
  2289. ocfs2_handle_error(sb);
  2290. }
  2291. /*
  2292. * Void signal blockers, because in-kernel sigprocmask() only fails
  2293. * when SIG_* is wrong.
  2294. */
  2295. void ocfs2_block_signals(sigset_t *oldset)
  2296. {
  2297. int rc;
  2298. sigset_t blocked;
  2299. sigfillset(&blocked);
  2300. rc = sigprocmask(SIG_BLOCK, &blocked, oldset);
  2301. BUG_ON(rc);
  2302. }
  2303. void ocfs2_unblock_signals(sigset_t *oldset)
  2304. {
  2305. int rc = sigprocmask(SIG_SETMASK, oldset, NULL);
  2306. BUG_ON(rc);
  2307. }
  2308. module_init(ocfs2_init);
  2309. module_exit(ocfs2_exit);