super.c 68 KB

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