super.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667
  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. trace_ocfs2_parse_options(is_remount, options ? options : "(none)");
  1135. mopt->commit_interval = 0;
  1136. mopt->mount_opt = OCFS2_MOUNT_NOINTR;
  1137. mopt->atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
  1138. mopt->slot = OCFS2_INVALID_SLOT;
  1139. mopt->localalloc_opt = -1;
  1140. mopt->cluster_stack[0] = '\0';
  1141. mopt->resv_level = OCFS2_DEFAULT_RESV_LEVEL;
  1142. mopt->dir_resv_level = -1;
  1143. if (!options) {
  1144. status = 1;
  1145. goto bail;
  1146. }
  1147. while ((p = strsep(&options, ",")) != NULL) {
  1148. int token, option;
  1149. substring_t args[MAX_OPT_ARGS];
  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. option = 0;
  1216. if (match_int(&args[0], &option)) {
  1217. status = 0;
  1218. goto bail;
  1219. }
  1220. if (option)
  1221. mopt->slot = (s16)option;
  1222. break;
  1223. case Opt_commit:
  1224. option = 0;
  1225. if (match_int(&args[0], &option)) {
  1226. status = 0;
  1227. goto bail;
  1228. }
  1229. if (option < 0)
  1230. return 0;
  1231. if (option == 0)
  1232. option = JBD2_DEFAULT_MAX_COMMIT_AGE;
  1233. mopt->commit_interval = HZ * option;
  1234. break;
  1235. case Opt_localalloc:
  1236. option = 0;
  1237. if (match_int(&args[0], &option)) {
  1238. status = 0;
  1239. goto bail;
  1240. }
  1241. if (option >= 0)
  1242. mopt->localalloc_opt = option;
  1243. break;
  1244. case Opt_localflocks:
  1245. /*
  1246. * Changing this during remount could race
  1247. * flock() requests, or "unbalance" existing
  1248. * ones (e.g., a lock is taken in one mode but
  1249. * dropped in the other). If users care enough
  1250. * to flip locking modes during remount, we
  1251. * could add a "local" flag to individual
  1252. * flock structures for proper tracking of
  1253. * state.
  1254. */
  1255. if (!is_remount)
  1256. mopt->mount_opt |= OCFS2_MOUNT_LOCALFLOCKS;
  1257. break;
  1258. case Opt_stack:
  1259. /* Check both that the option we were passed
  1260. * is of the right length and that it is a proper
  1261. * string of the right length.
  1262. */
  1263. if (((args[0].to - args[0].from) !=
  1264. OCFS2_STACK_LABEL_LEN) ||
  1265. (strnlen(args[0].from,
  1266. OCFS2_STACK_LABEL_LEN) !=
  1267. OCFS2_STACK_LABEL_LEN)) {
  1268. mlog(ML_ERROR,
  1269. "Invalid cluster_stack option\n");
  1270. status = 0;
  1271. goto bail;
  1272. }
  1273. memcpy(mopt->cluster_stack, args[0].from,
  1274. OCFS2_STACK_LABEL_LEN);
  1275. mopt->cluster_stack[OCFS2_STACK_LABEL_LEN] = '\0';
  1276. /*
  1277. * Open code the memcmp here as we don't have
  1278. * an osb to pass to
  1279. * ocfs2_userspace_stack().
  1280. */
  1281. if (memcmp(mopt->cluster_stack,
  1282. OCFS2_CLASSIC_CLUSTER_STACK,
  1283. OCFS2_STACK_LABEL_LEN))
  1284. user_stack = 1;
  1285. break;
  1286. case Opt_inode64:
  1287. mopt->mount_opt |= OCFS2_MOUNT_INODE64;
  1288. break;
  1289. case Opt_usrquota:
  1290. mopt->mount_opt |= OCFS2_MOUNT_USRQUOTA;
  1291. break;
  1292. case Opt_grpquota:
  1293. mopt->mount_opt |= OCFS2_MOUNT_GRPQUOTA;
  1294. break;
  1295. case Opt_coherency_buffered:
  1296. mopt->mount_opt |= OCFS2_MOUNT_COHERENCY_BUFFERED;
  1297. break;
  1298. case Opt_coherency_full:
  1299. mopt->mount_opt &= ~OCFS2_MOUNT_COHERENCY_BUFFERED;
  1300. break;
  1301. case Opt_acl:
  1302. mopt->mount_opt |= OCFS2_MOUNT_POSIX_ACL;
  1303. mopt->mount_opt &= ~OCFS2_MOUNT_NO_POSIX_ACL;
  1304. break;
  1305. case Opt_noacl:
  1306. mopt->mount_opt |= OCFS2_MOUNT_NO_POSIX_ACL;
  1307. mopt->mount_opt &= ~OCFS2_MOUNT_POSIX_ACL;
  1308. break;
  1309. case Opt_resv_level:
  1310. if (is_remount)
  1311. break;
  1312. if (match_int(&args[0], &option)) {
  1313. status = 0;
  1314. goto bail;
  1315. }
  1316. if (option >= OCFS2_MIN_RESV_LEVEL &&
  1317. option < OCFS2_MAX_RESV_LEVEL)
  1318. mopt->resv_level = option;
  1319. break;
  1320. case Opt_dir_resv_level:
  1321. if (is_remount)
  1322. break;
  1323. if (match_int(&args[0], &option)) {
  1324. status = 0;
  1325. goto bail;
  1326. }
  1327. if (option >= OCFS2_MIN_RESV_LEVEL &&
  1328. option < OCFS2_MAX_RESV_LEVEL)
  1329. mopt->dir_resv_level = option;
  1330. break;
  1331. case Opt_journal_async_commit:
  1332. mopt->mount_opt |= OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT;
  1333. break;
  1334. default:
  1335. mlog(ML_ERROR,
  1336. "Unrecognized mount option \"%s\" "
  1337. "or missing value\n", p);
  1338. status = 0;
  1339. goto bail;
  1340. }
  1341. }
  1342. if (user_stack == 0) {
  1343. /* Ensure only one heartbeat mode */
  1344. tmp = mopt->mount_opt & (OCFS2_MOUNT_HB_LOCAL |
  1345. OCFS2_MOUNT_HB_GLOBAL |
  1346. OCFS2_MOUNT_HB_NONE);
  1347. if (hweight32(tmp) != 1) {
  1348. mlog(ML_ERROR, "Invalid heartbeat mount options\n");
  1349. status = 0;
  1350. goto bail;
  1351. }
  1352. }
  1353. status = 1;
  1354. bail:
  1355. return status;
  1356. }
  1357. static int ocfs2_show_options(struct seq_file *s, struct dentry *root)
  1358. {
  1359. struct ocfs2_super *osb = OCFS2_SB(root->d_sb);
  1360. unsigned long opts = osb->s_mount_opt;
  1361. unsigned int local_alloc_megs;
  1362. if (opts & (OCFS2_MOUNT_HB_LOCAL | OCFS2_MOUNT_HB_GLOBAL)) {
  1363. seq_printf(s, ",_netdev");
  1364. if (opts & OCFS2_MOUNT_HB_LOCAL)
  1365. seq_printf(s, ",%s", OCFS2_HB_LOCAL);
  1366. else
  1367. seq_printf(s, ",%s", OCFS2_HB_GLOBAL);
  1368. } else
  1369. seq_printf(s, ",%s", OCFS2_HB_NONE);
  1370. if (opts & OCFS2_MOUNT_NOINTR)
  1371. seq_printf(s, ",nointr");
  1372. if (opts & OCFS2_MOUNT_DATA_WRITEBACK)
  1373. seq_printf(s, ",data=writeback");
  1374. else
  1375. seq_printf(s, ",data=ordered");
  1376. if (opts & OCFS2_MOUNT_BARRIER)
  1377. seq_printf(s, ",barrier=1");
  1378. if (opts & OCFS2_MOUNT_ERRORS_PANIC)
  1379. seq_printf(s, ",errors=panic");
  1380. else if (opts & OCFS2_MOUNT_ERRORS_CONT)
  1381. seq_printf(s, ",errors=continue");
  1382. else
  1383. seq_printf(s, ",errors=remount-ro");
  1384. if (osb->preferred_slot != OCFS2_INVALID_SLOT)
  1385. seq_printf(s, ",preferred_slot=%d", osb->preferred_slot);
  1386. seq_printf(s, ",atime_quantum=%u", osb->s_atime_quantum);
  1387. if (osb->osb_commit_interval)
  1388. seq_printf(s, ",commit=%u",
  1389. (unsigned) (osb->osb_commit_interval / HZ));
  1390. local_alloc_megs = osb->local_alloc_bits >> (20 - osb->s_clustersize_bits);
  1391. if (local_alloc_megs != ocfs2_la_default_mb(osb))
  1392. seq_printf(s, ",localalloc=%d", local_alloc_megs);
  1393. if (opts & OCFS2_MOUNT_LOCALFLOCKS)
  1394. seq_printf(s, ",localflocks,");
  1395. if (osb->osb_cluster_stack[0])
  1396. seq_show_option_n(s, "cluster_stack", osb->osb_cluster_stack,
  1397. OCFS2_STACK_LABEL_LEN);
  1398. if (opts & OCFS2_MOUNT_USRQUOTA)
  1399. seq_printf(s, ",usrquota");
  1400. if (opts & OCFS2_MOUNT_GRPQUOTA)
  1401. seq_printf(s, ",grpquota");
  1402. if (opts & OCFS2_MOUNT_COHERENCY_BUFFERED)
  1403. seq_printf(s, ",coherency=buffered");
  1404. else
  1405. seq_printf(s, ",coherency=full");
  1406. if (opts & OCFS2_MOUNT_NOUSERXATTR)
  1407. seq_printf(s, ",nouser_xattr");
  1408. else
  1409. seq_printf(s, ",user_xattr");
  1410. if (opts & OCFS2_MOUNT_INODE64)
  1411. seq_printf(s, ",inode64");
  1412. if (opts & OCFS2_MOUNT_POSIX_ACL)
  1413. seq_printf(s, ",acl");
  1414. else
  1415. seq_printf(s, ",noacl");
  1416. if (osb->osb_resv_level != OCFS2_DEFAULT_RESV_LEVEL)
  1417. seq_printf(s, ",resv_level=%d", osb->osb_resv_level);
  1418. if (osb->osb_dir_resv_level != osb->osb_resv_level)
  1419. seq_printf(s, ",dir_resv_level=%d", osb->osb_resv_level);
  1420. if (opts & OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT)
  1421. seq_printf(s, ",journal_async_commit");
  1422. return 0;
  1423. }
  1424. static int __init ocfs2_init(void)
  1425. {
  1426. int status;
  1427. status = init_ocfs2_uptodate_cache();
  1428. if (status < 0)
  1429. goto out1;
  1430. status = ocfs2_initialize_mem_caches();
  1431. if (status < 0)
  1432. goto out2;
  1433. ocfs2_wq = create_singlethread_workqueue("ocfs2_wq");
  1434. if (!ocfs2_wq) {
  1435. status = -ENOMEM;
  1436. goto out3;
  1437. }
  1438. ocfs2_debugfs_root = debugfs_create_dir("ocfs2", NULL);
  1439. if (!ocfs2_debugfs_root) {
  1440. status = -ENOMEM;
  1441. mlog(ML_ERROR, "Unable to create ocfs2 debugfs root.\n");
  1442. goto out4;
  1443. }
  1444. ocfs2_set_locking_protocol();
  1445. status = register_quota_format(&ocfs2_quota_format);
  1446. if (status < 0)
  1447. goto out4;
  1448. status = register_filesystem(&ocfs2_fs_type);
  1449. if (!status)
  1450. return 0;
  1451. unregister_quota_format(&ocfs2_quota_format);
  1452. out4:
  1453. destroy_workqueue(ocfs2_wq);
  1454. debugfs_remove(ocfs2_debugfs_root);
  1455. out3:
  1456. ocfs2_free_mem_caches();
  1457. out2:
  1458. exit_ocfs2_uptodate_cache();
  1459. out1:
  1460. mlog_errno(status);
  1461. return status;
  1462. }
  1463. static void __exit ocfs2_exit(void)
  1464. {
  1465. if (ocfs2_wq) {
  1466. flush_workqueue(ocfs2_wq);
  1467. destroy_workqueue(ocfs2_wq);
  1468. }
  1469. unregister_quota_format(&ocfs2_quota_format);
  1470. debugfs_remove(ocfs2_debugfs_root);
  1471. ocfs2_free_mem_caches();
  1472. unregister_filesystem(&ocfs2_fs_type);
  1473. exit_ocfs2_uptodate_cache();
  1474. }
  1475. static void ocfs2_put_super(struct super_block *sb)
  1476. {
  1477. trace_ocfs2_put_super(sb);
  1478. ocfs2_sync_blockdev(sb);
  1479. ocfs2_dismount_volume(sb, 0);
  1480. }
  1481. static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  1482. {
  1483. struct ocfs2_super *osb;
  1484. u32 numbits, freebits;
  1485. int status;
  1486. struct ocfs2_dinode *bm_lock;
  1487. struct buffer_head *bh = NULL;
  1488. struct inode *inode = NULL;
  1489. trace_ocfs2_statfs(dentry->d_sb, buf);
  1490. osb = OCFS2_SB(dentry->d_sb);
  1491. inode = ocfs2_get_system_file_inode(osb,
  1492. GLOBAL_BITMAP_SYSTEM_INODE,
  1493. OCFS2_INVALID_SLOT);
  1494. if (!inode) {
  1495. mlog(ML_ERROR, "failed to get bitmap inode\n");
  1496. status = -EIO;
  1497. goto bail;
  1498. }
  1499. status = ocfs2_inode_lock(inode, &bh, 0);
  1500. if (status < 0) {
  1501. mlog_errno(status);
  1502. goto bail;
  1503. }
  1504. bm_lock = (struct ocfs2_dinode *) bh->b_data;
  1505. numbits = le32_to_cpu(bm_lock->id1.bitmap1.i_total);
  1506. freebits = numbits - le32_to_cpu(bm_lock->id1.bitmap1.i_used);
  1507. buf->f_type = OCFS2_SUPER_MAGIC;
  1508. buf->f_bsize = dentry->d_sb->s_blocksize;
  1509. buf->f_namelen = OCFS2_MAX_FILENAME_LEN;
  1510. buf->f_blocks = ((sector_t) numbits) *
  1511. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  1512. buf->f_bfree = ((sector_t) freebits) *
  1513. (osb->s_clustersize >> osb->sb->s_blocksize_bits);
  1514. buf->f_bavail = buf->f_bfree;
  1515. buf->f_files = numbits;
  1516. buf->f_ffree = freebits;
  1517. buf->f_fsid.val[0] = crc32_le(0, osb->uuid_str, OCFS2_VOL_UUID_LEN)
  1518. & 0xFFFFFFFFUL;
  1519. buf->f_fsid.val[1] = crc32_le(0, osb->uuid_str + OCFS2_VOL_UUID_LEN,
  1520. OCFS2_VOL_UUID_LEN) & 0xFFFFFFFFUL;
  1521. brelse(bh);
  1522. ocfs2_inode_unlock(inode, 0);
  1523. status = 0;
  1524. bail:
  1525. if (inode)
  1526. iput(inode);
  1527. if (status)
  1528. mlog_errno(status);
  1529. return status;
  1530. }
  1531. static void ocfs2_inode_init_once(void *data)
  1532. {
  1533. struct ocfs2_inode_info *oi = data;
  1534. oi->ip_flags = 0;
  1535. oi->ip_open_count = 0;
  1536. spin_lock_init(&oi->ip_lock);
  1537. ocfs2_extent_map_init(&oi->vfs_inode);
  1538. INIT_LIST_HEAD(&oi->ip_io_markers);
  1539. oi->ip_dir_start_lookup = 0;
  1540. mutex_init(&oi->ip_unaligned_aio);
  1541. init_rwsem(&oi->ip_alloc_sem);
  1542. init_rwsem(&oi->ip_xattr_sem);
  1543. mutex_init(&oi->ip_io_mutex);
  1544. oi->ip_blkno = 0ULL;
  1545. oi->ip_clusters = 0;
  1546. ocfs2_resv_init_once(&oi->ip_la_data_resv);
  1547. ocfs2_lock_res_init_once(&oi->ip_rw_lockres);
  1548. ocfs2_lock_res_init_once(&oi->ip_inode_lockres);
  1549. ocfs2_lock_res_init_once(&oi->ip_open_lockres);
  1550. ocfs2_metadata_cache_init(INODE_CACHE(&oi->vfs_inode),
  1551. &ocfs2_inode_caching_ops);
  1552. inode_init_once(&oi->vfs_inode);
  1553. }
  1554. static int ocfs2_initialize_mem_caches(void)
  1555. {
  1556. ocfs2_inode_cachep = kmem_cache_create("ocfs2_inode_cache",
  1557. sizeof(struct ocfs2_inode_info),
  1558. 0,
  1559. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1560. SLAB_MEM_SPREAD),
  1561. ocfs2_inode_init_once);
  1562. ocfs2_dquot_cachep = kmem_cache_create("ocfs2_dquot_cache",
  1563. sizeof(struct ocfs2_dquot),
  1564. 0,
  1565. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1566. SLAB_MEM_SPREAD),
  1567. NULL);
  1568. ocfs2_qf_chunk_cachep = kmem_cache_create("ocfs2_qf_chunk_cache",
  1569. sizeof(struct ocfs2_quota_chunk),
  1570. 0,
  1571. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  1572. NULL);
  1573. if (!ocfs2_inode_cachep || !ocfs2_dquot_cachep ||
  1574. !ocfs2_qf_chunk_cachep) {
  1575. if (ocfs2_inode_cachep)
  1576. kmem_cache_destroy(ocfs2_inode_cachep);
  1577. if (ocfs2_dquot_cachep)
  1578. kmem_cache_destroy(ocfs2_dquot_cachep);
  1579. if (ocfs2_qf_chunk_cachep)
  1580. kmem_cache_destroy(ocfs2_qf_chunk_cachep);
  1581. return -ENOMEM;
  1582. }
  1583. return 0;
  1584. }
  1585. static void ocfs2_free_mem_caches(void)
  1586. {
  1587. /*
  1588. * Make sure all delayed rcu free inodes are flushed before we
  1589. * destroy cache.
  1590. */
  1591. rcu_barrier();
  1592. if (ocfs2_inode_cachep)
  1593. kmem_cache_destroy(ocfs2_inode_cachep);
  1594. ocfs2_inode_cachep = NULL;
  1595. if (ocfs2_dquot_cachep)
  1596. kmem_cache_destroy(ocfs2_dquot_cachep);
  1597. ocfs2_dquot_cachep = NULL;
  1598. if (ocfs2_qf_chunk_cachep)
  1599. kmem_cache_destroy(ocfs2_qf_chunk_cachep);
  1600. ocfs2_qf_chunk_cachep = NULL;
  1601. }
  1602. static int ocfs2_get_sector(struct super_block *sb,
  1603. struct buffer_head **bh,
  1604. int block,
  1605. int sect_size)
  1606. {
  1607. if (!sb_set_blocksize(sb, sect_size)) {
  1608. mlog(ML_ERROR, "unable to set blocksize\n");
  1609. return -EIO;
  1610. }
  1611. *bh = sb_getblk(sb, block);
  1612. if (!*bh) {
  1613. mlog_errno(-ENOMEM);
  1614. return -ENOMEM;
  1615. }
  1616. lock_buffer(*bh);
  1617. if (!buffer_dirty(*bh))
  1618. clear_buffer_uptodate(*bh);
  1619. unlock_buffer(*bh);
  1620. ll_rw_block(READ, 1, bh);
  1621. wait_on_buffer(*bh);
  1622. if (!buffer_uptodate(*bh)) {
  1623. mlog_errno(-EIO);
  1624. brelse(*bh);
  1625. *bh = NULL;
  1626. return -EIO;
  1627. }
  1628. return 0;
  1629. }
  1630. static int ocfs2_mount_volume(struct super_block *sb)
  1631. {
  1632. int status = 0;
  1633. int unlock_super = 0;
  1634. struct ocfs2_super *osb = OCFS2_SB(sb);
  1635. if (ocfs2_is_hard_readonly(osb))
  1636. goto leave;
  1637. status = ocfs2_dlm_init(osb);
  1638. if (status < 0) {
  1639. mlog_errno(status);
  1640. goto leave;
  1641. }
  1642. status = ocfs2_super_lock(osb, 1);
  1643. if (status < 0) {
  1644. mlog_errno(status);
  1645. goto leave;
  1646. }
  1647. unlock_super = 1;
  1648. /* This will load up the node map and add ourselves to it. */
  1649. status = ocfs2_find_slot(osb);
  1650. if (status < 0) {
  1651. mlog_errno(status);
  1652. goto leave;
  1653. }
  1654. /* load all node-local system inodes */
  1655. status = ocfs2_init_local_system_inodes(osb);
  1656. if (status < 0) {
  1657. mlog_errno(status);
  1658. goto leave;
  1659. }
  1660. status = ocfs2_check_volume(osb);
  1661. if (status < 0) {
  1662. mlog_errno(status);
  1663. goto leave;
  1664. }
  1665. status = ocfs2_truncate_log_init(osb);
  1666. if (status < 0)
  1667. mlog_errno(status);
  1668. leave:
  1669. if (unlock_super)
  1670. ocfs2_super_unlock(osb, 1);
  1671. return status;
  1672. }
  1673. static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err)
  1674. {
  1675. int tmp, hangup_needed = 0;
  1676. struct ocfs2_super *osb = NULL;
  1677. char nodestr[12];
  1678. trace_ocfs2_dismount_volume(sb);
  1679. BUG_ON(!sb);
  1680. osb = OCFS2_SB(sb);
  1681. BUG_ON(!osb);
  1682. debugfs_remove(osb->osb_ctxt);
  1683. /* Orphan scan should be stopped as early as possible */
  1684. ocfs2_orphan_scan_stop(osb);
  1685. ocfs2_disable_quotas(osb);
  1686. /* All dquots should be freed by now */
  1687. WARN_ON(!llist_empty(&osb->dquot_drop_list));
  1688. /* Wait for worker to be done with the work structure in osb */
  1689. cancel_work_sync(&osb->dquot_drop_work);
  1690. ocfs2_shutdown_local_alloc(osb);
  1691. ocfs2_truncate_log_shutdown(osb);
  1692. /* This will disable recovery and flush any recovery work. */
  1693. ocfs2_recovery_exit(osb);
  1694. ocfs2_journal_shutdown(osb);
  1695. ocfs2_sync_blockdev(sb);
  1696. ocfs2_purge_refcount_trees(osb);
  1697. /* No cluster connection means we've failed during mount, so skip
  1698. * all the steps which depended on that to complete. */
  1699. if (osb->cconn) {
  1700. tmp = ocfs2_super_lock(osb, 1);
  1701. if (tmp < 0) {
  1702. mlog_errno(tmp);
  1703. return;
  1704. }
  1705. }
  1706. if (osb->slot_num != OCFS2_INVALID_SLOT)
  1707. ocfs2_put_slot(osb);
  1708. if (osb->cconn)
  1709. ocfs2_super_unlock(osb, 1);
  1710. ocfs2_release_system_inodes(osb);
  1711. /*
  1712. * If we're dismounting due to mount error, mount.ocfs2 will clean
  1713. * up heartbeat. If we're a local mount, there is no heartbeat.
  1714. * If we failed before we got a uuid_str yet, we can't stop
  1715. * heartbeat. Otherwise, do it.
  1716. */
  1717. if (!mnt_err && !ocfs2_mount_local(osb) && osb->uuid_str &&
  1718. !ocfs2_is_hard_readonly(osb))
  1719. hangup_needed = 1;
  1720. if (osb->cconn)
  1721. ocfs2_dlm_shutdown(osb, hangup_needed);
  1722. ocfs2_blockcheck_stats_debugfs_remove(&osb->osb_ecc_stats);
  1723. debugfs_remove(osb->osb_debug_root);
  1724. if (hangup_needed)
  1725. ocfs2_cluster_hangup(osb->uuid_str, strlen(osb->uuid_str));
  1726. atomic_set(&osb->vol_state, VOLUME_DISMOUNTED);
  1727. if (ocfs2_mount_local(osb))
  1728. snprintf(nodestr, sizeof(nodestr), "local");
  1729. else
  1730. snprintf(nodestr, sizeof(nodestr), "%u", osb->node_num);
  1731. printk(KERN_INFO "ocfs2: Unmounting device (%s) on (node %s)\n",
  1732. osb->dev_str, nodestr);
  1733. ocfs2_delete_osb(osb);
  1734. kfree(osb);
  1735. sb->s_dev = 0;
  1736. sb->s_fs_info = NULL;
  1737. }
  1738. static int ocfs2_setup_osb_uuid(struct ocfs2_super *osb, const unsigned char *uuid,
  1739. unsigned uuid_bytes)
  1740. {
  1741. int i, ret;
  1742. char *ptr;
  1743. BUG_ON(uuid_bytes != OCFS2_VOL_UUID_LEN);
  1744. osb->uuid_str = kzalloc(OCFS2_VOL_UUID_LEN * 2 + 1, GFP_KERNEL);
  1745. if (osb->uuid_str == NULL)
  1746. return -ENOMEM;
  1747. for (i = 0, ptr = osb->uuid_str; i < OCFS2_VOL_UUID_LEN; i++) {
  1748. /* print with null */
  1749. ret = snprintf(ptr, 3, "%02X", uuid[i]);
  1750. if (ret != 2) /* drop super cleans up */
  1751. return -EINVAL;
  1752. /* then only advance past the last char */
  1753. ptr += 2;
  1754. }
  1755. return 0;
  1756. }
  1757. /* Make sure entire volume is addressable by our journal. Requires
  1758. osb_clusters_at_boot to be valid and for the journal to have been
  1759. initialized by ocfs2_journal_init(). */
  1760. static int ocfs2_journal_addressable(struct ocfs2_super *osb)
  1761. {
  1762. int status = 0;
  1763. u64 max_block =
  1764. ocfs2_clusters_to_blocks(osb->sb,
  1765. osb->osb_clusters_at_boot) - 1;
  1766. /* 32-bit block number is always OK. */
  1767. if (max_block <= (u32)~0ULL)
  1768. goto out;
  1769. /* Volume is "huge", so see if our journal is new enough to
  1770. support it. */
  1771. if (!(OCFS2_HAS_COMPAT_FEATURE(osb->sb,
  1772. OCFS2_FEATURE_COMPAT_JBD2_SB) &&
  1773. jbd2_journal_check_used_features(osb->journal->j_journal, 0, 0,
  1774. JBD2_FEATURE_INCOMPAT_64BIT))) {
  1775. mlog(ML_ERROR, "The journal cannot address the entire volume. "
  1776. "Enable the 'block64' journal option with tunefs.ocfs2");
  1777. status = -EFBIG;
  1778. goto out;
  1779. }
  1780. out:
  1781. return status;
  1782. }
  1783. static int ocfs2_initialize_super(struct super_block *sb,
  1784. struct buffer_head *bh,
  1785. int sector_size,
  1786. struct ocfs2_blockcheck_stats *stats)
  1787. {
  1788. int status;
  1789. int i, cbits, bbits;
  1790. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1791. struct inode *inode = NULL;
  1792. struct ocfs2_journal *journal;
  1793. struct ocfs2_super *osb;
  1794. u64 total_blocks;
  1795. osb = kzalloc(sizeof(struct ocfs2_super), GFP_KERNEL);
  1796. if (!osb) {
  1797. status = -ENOMEM;
  1798. mlog_errno(status);
  1799. goto bail;
  1800. }
  1801. sb->s_fs_info = osb;
  1802. sb->s_op = &ocfs2_sops;
  1803. sb->s_d_op = &ocfs2_dentry_ops;
  1804. sb->s_export_op = &ocfs2_export_ops;
  1805. sb->s_qcop = &dquot_quotactl_sysfile_ops;
  1806. sb->dq_op = &ocfs2_quota_operations;
  1807. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
  1808. sb->s_xattr = ocfs2_xattr_handlers;
  1809. sb->s_time_gran = 1;
  1810. sb->s_flags |= MS_NOATIME;
  1811. /* this is needed to support O_LARGEFILE */
  1812. cbits = le32_to_cpu(di->id2.i_super.s_clustersize_bits);
  1813. bbits = le32_to_cpu(di->id2.i_super.s_blocksize_bits);
  1814. sb->s_maxbytes = ocfs2_max_file_offset(bbits, cbits);
  1815. memcpy(sb->s_uuid, di->id2.i_super.s_uuid,
  1816. sizeof(di->id2.i_super.s_uuid));
  1817. osb->osb_dx_mask = (1 << (cbits - bbits)) - 1;
  1818. for (i = 0; i < 3; i++)
  1819. osb->osb_dx_seed[i] = le32_to_cpu(di->id2.i_super.s_dx_seed[i]);
  1820. osb->osb_dx_seed[3] = le32_to_cpu(di->id2.i_super.s_uuid_hash);
  1821. osb->sb = sb;
  1822. /* Save off for ocfs2_rw_direct */
  1823. osb->s_sectsize_bits = blksize_bits(sector_size);
  1824. BUG_ON(!osb->s_sectsize_bits);
  1825. spin_lock_init(&osb->dc_task_lock);
  1826. init_waitqueue_head(&osb->dc_event);
  1827. osb->dc_work_sequence = 0;
  1828. osb->dc_wake_sequence = 0;
  1829. INIT_LIST_HEAD(&osb->blocked_lock_list);
  1830. osb->blocked_lock_count = 0;
  1831. spin_lock_init(&osb->osb_lock);
  1832. spin_lock_init(&osb->osb_xattr_lock);
  1833. ocfs2_init_steal_slots(osb);
  1834. mutex_init(&osb->system_file_mutex);
  1835. atomic_set(&osb->alloc_stats.moves, 0);
  1836. atomic_set(&osb->alloc_stats.local_data, 0);
  1837. atomic_set(&osb->alloc_stats.bitmap_data, 0);
  1838. atomic_set(&osb->alloc_stats.bg_allocs, 0);
  1839. atomic_set(&osb->alloc_stats.bg_extends, 0);
  1840. /* Copy the blockcheck stats from the superblock probe */
  1841. osb->osb_ecc_stats = *stats;
  1842. ocfs2_init_node_maps(osb);
  1843. snprintf(osb->dev_str, sizeof(osb->dev_str), "%u,%u",
  1844. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1845. osb->max_slots = le16_to_cpu(di->id2.i_super.s_max_slots);
  1846. if (osb->max_slots > OCFS2_MAX_SLOTS || osb->max_slots == 0) {
  1847. mlog(ML_ERROR, "Invalid number of node slots (%u)\n",
  1848. osb->max_slots);
  1849. status = -EINVAL;
  1850. goto bail;
  1851. }
  1852. ocfs2_orphan_scan_init(osb);
  1853. status = ocfs2_recovery_init(osb);
  1854. if (status) {
  1855. mlog(ML_ERROR, "Unable to initialize recovery state\n");
  1856. mlog_errno(status);
  1857. goto bail;
  1858. }
  1859. init_waitqueue_head(&osb->checkpoint_event);
  1860. osb->s_atime_quantum = OCFS2_DEFAULT_ATIME_QUANTUM;
  1861. osb->slot_num = OCFS2_INVALID_SLOT;
  1862. osb->s_xattr_inline_size = le16_to_cpu(
  1863. di->id2.i_super.s_xattr_inline_size);
  1864. osb->local_alloc_state = OCFS2_LA_UNUSED;
  1865. osb->local_alloc_bh = NULL;
  1866. INIT_DELAYED_WORK(&osb->la_enable_wq, ocfs2_la_enable_worker);
  1867. init_waitqueue_head(&osb->osb_mount_event);
  1868. status = ocfs2_resmap_init(osb, &osb->osb_la_resmap);
  1869. if (status) {
  1870. mlog_errno(status);
  1871. goto bail;
  1872. }
  1873. osb->vol_label = kmalloc(OCFS2_MAX_VOL_LABEL_LEN, GFP_KERNEL);
  1874. if (!osb->vol_label) {
  1875. mlog(ML_ERROR, "unable to alloc vol label\n");
  1876. status = -ENOMEM;
  1877. goto bail;
  1878. }
  1879. osb->slot_recovery_generations =
  1880. kcalloc(osb->max_slots, sizeof(*osb->slot_recovery_generations),
  1881. GFP_KERNEL);
  1882. if (!osb->slot_recovery_generations) {
  1883. status = -ENOMEM;
  1884. mlog_errno(status);
  1885. goto bail;
  1886. }
  1887. init_waitqueue_head(&osb->osb_wipe_event);
  1888. osb->osb_orphan_wipes = kcalloc(osb->max_slots,
  1889. sizeof(*osb->osb_orphan_wipes),
  1890. GFP_KERNEL);
  1891. if (!osb->osb_orphan_wipes) {
  1892. status = -ENOMEM;
  1893. mlog_errno(status);
  1894. goto bail;
  1895. }
  1896. osb->osb_rf_lock_tree = RB_ROOT;
  1897. osb->s_feature_compat =
  1898. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_compat);
  1899. osb->s_feature_ro_compat =
  1900. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_ro_compat);
  1901. osb->s_feature_incompat =
  1902. le32_to_cpu(OCFS2_RAW_SB(di)->s_feature_incompat);
  1903. if ((i = OCFS2_HAS_INCOMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_INCOMPAT_SUPP))) {
  1904. mlog(ML_ERROR, "couldn't mount because of unsupported "
  1905. "optional features (%x).\n", i);
  1906. status = -EINVAL;
  1907. goto bail;
  1908. }
  1909. if (!(osb->sb->s_flags & MS_RDONLY) &&
  1910. (i = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP))) {
  1911. mlog(ML_ERROR, "couldn't mount RDWR because of "
  1912. "unsupported optional features (%x).\n", i);
  1913. status = -EINVAL;
  1914. goto bail;
  1915. }
  1916. if (ocfs2_clusterinfo_valid(osb)) {
  1917. osb->osb_stackflags =
  1918. OCFS2_RAW_SB(di)->s_cluster_info.ci_stackflags;
  1919. strlcpy(osb->osb_cluster_stack,
  1920. OCFS2_RAW_SB(di)->s_cluster_info.ci_stack,
  1921. OCFS2_STACK_LABEL_LEN + 1);
  1922. if (strlen(osb->osb_cluster_stack) != OCFS2_STACK_LABEL_LEN) {
  1923. mlog(ML_ERROR,
  1924. "couldn't mount because of an invalid "
  1925. "cluster stack label (%s) \n",
  1926. osb->osb_cluster_stack);
  1927. status = -EINVAL;
  1928. goto bail;
  1929. }
  1930. strlcpy(osb->osb_cluster_name,
  1931. OCFS2_RAW_SB(di)->s_cluster_info.ci_cluster,
  1932. OCFS2_CLUSTER_NAME_LEN + 1);
  1933. } else {
  1934. /* The empty string is identical with classic tools that
  1935. * don't know about s_cluster_info. */
  1936. osb->osb_cluster_stack[0] = '\0';
  1937. }
  1938. get_random_bytes(&osb->s_next_generation, sizeof(u32));
  1939. /* FIXME
  1940. * This should be done in ocfs2_journal_init(), but unknown
  1941. * ordering issues will cause the filesystem to crash.
  1942. * If anyone wants to figure out what part of the code
  1943. * refers to osb->journal before ocfs2_journal_init() is run,
  1944. * be my guest.
  1945. */
  1946. /* initialize our journal structure */
  1947. journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
  1948. if (!journal) {
  1949. mlog(ML_ERROR, "unable to alloc journal\n");
  1950. status = -ENOMEM;
  1951. goto bail;
  1952. }
  1953. osb->journal = journal;
  1954. journal->j_osb = osb;
  1955. atomic_set(&journal->j_num_trans, 0);
  1956. init_rwsem(&journal->j_trans_barrier);
  1957. init_waitqueue_head(&journal->j_checkpointed);
  1958. spin_lock_init(&journal->j_lock);
  1959. journal->j_trans_id = (unsigned long) 1;
  1960. INIT_LIST_HEAD(&journal->j_la_cleanups);
  1961. INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
  1962. journal->j_state = OCFS2_JOURNAL_FREE;
  1963. INIT_WORK(&osb->dquot_drop_work, ocfs2_drop_dquot_refs);
  1964. init_llist_head(&osb->dquot_drop_list);
  1965. /* get some pseudo constants for clustersize bits */
  1966. osb->s_clustersize_bits =
  1967. le32_to_cpu(di->id2.i_super.s_clustersize_bits);
  1968. osb->s_clustersize = 1 << osb->s_clustersize_bits;
  1969. if (osb->s_clustersize < OCFS2_MIN_CLUSTERSIZE ||
  1970. osb->s_clustersize > OCFS2_MAX_CLUSTERSIZE) {
  1971. mlog(ML_ERROR, "Volume has invalid cluster size (%d)\n",
  1972. osb->s_clustersize);
  1973. status = -EINVAL;
  1974. goto bail;
  1975. }
  1976. total_blocks = ocfs2_clusters_to_blocks(osb->sb,
  1977. le32_to_cpu(di->i_clusters));
  1978. status = generic_check_addressable(osb->sb->s_blocksize_bits,
  1979. total_blocks);
  1980. if (status) {
  1981. mlog(ML_ERROR, "Volume too large "
  1982. "to mount safely on this system");
  1983. status = -EFBIG;
  1984. goto bail;
  1985. }
  1986. if (ocfs2_setup_osb_uuid(osb, di->id2.i_super.s_uuid,
  1987. sizeof(di->id2.i_super.s_uuid))) {
  1988. mlog(ML_ERROR, "Out of memory trying to setup our uuid.\n");
  1989. status = -ENOMEM;
  1990. goto bail;
  1991. }
  1992. strlcpy(osb->vol_label, di->id2.i_super.s_label,
  1993. OCFS2_MAX_VOL_LABEL_LEN);
  1994. osb->root_blkno = le64_to_cpu(di->id2.i_super.s_root_blkno);
  1995. osb->system_dir_blkno = le64_to_cpu(di->id2.i_super.s_system_dir_blkno);
  1996. osb->first_cluster_group_blkno =
  1997. le64_to_cpu(di->id2.i_super.s_first_cluster_group);
  1998. osb->fs_generation = le32_to_cpu(di->i_fs_generation);
  1999. osb->uuid_hash = le32_to_cpu(di->id2.i_super.s_uuid_hash);
  2000. trace_ocfs2_initialize_super(osb->vol_label, osb->uuid_str,
  2001. (unsigned long long)osb->root_blkno,
  2002. (unsigned long long)osb->system_dir_blkno,
  2003. osb->s_clustersize_bits);
  2004. osb->osb_dlm_debug = ocfs2_new_dlm_debug();
  2005. if (!osb->osb_dlm_debug) {
  2006. status = -ENOMEM;
  2007. mlog_errno(status);
  2008. goto bail;
  2009. }
  2010. atomic_set(&osb->vol_state, VOLUME_INIT);
  2011. /* load root, system_dir, and all global system inodes */
  2012. status = ocfs2_init_global_system_inodes(osb);
  2013. if (status < 0) {
  2014. mlog_errno(status);
  2015. goto bail;
  2016. }
  2017. /*
  2018. * global bitmap
  2019. */
  2020. inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE,
  2021. OCFS2_INVALID_SLOT);
  2022. if (!inode) {
  2023. status = -EINVAL;
  2024. mlog_errno(status);
  2025. goto bail;
  2026. }
  2027. osb->bitmap_blkno = OCFS2_I(inode)->ip_blkno;
  2028. osb->osb_clusters_at_boot = OCFS2_I(inode)->ip_clusters;
  2029. iput(inode);
  2030. osb->bitmap_cpg = ocfs2_group_bitmap_size(sb, 0,
  2031. osb->s_feature_incompat) * 8;
  2032. status = ocfs2_init_slot_info(osb);
  2033. if (status < 0) {
  2034. mlog_errno(status);
  2035. goto bail;
  2036. }
  2037. cleancache_init_shared_fs(sb);
  2038. bail:
  2039. return status;
  2040. }
  2041. /*
  2042. * will return: -EAGAIN if it is ok to keep searching for superblocks
  2043. * -EINVAL if there is a bad superblock
  2044. * 0 on success
  2045. */
  2046. static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  2047. struct buffer_head *bh,
  2048. u32 blksz,
  2049. struct ocfs2_blockcheck_stats *stats)
  2050. {
  2051. int status = -EAGAIN;
  2052. if (memcmp(di->i_signature, OCFS2_SUPER_BLOCK_SIGNATURE,
  2053. strlen(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) {
  2054. /* We have to do a raw check of the feature here */
  2055. if (le32_to_cpu(di->id2.i_super.s_feature_incompat) &
  2056. OCFS2_FEATURE_INCOMPAT_META_ECC) {
  2057. status = ocfs2_block_check_validate(bh->b_data,
  2058. bh->b_size,
  2059. &di->i_check,
  2060. stats);
  2061. if (status)
  2062. goto out;
  2063. }
  2064. status = -EINVAL;
  2065. if ((1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits)) != blksz) {
  2066. mlog(ML_ERROR, "found superblock with incorrect block "
  2067. "size: found %u, should be %u\n",
  2068. 1 << le32_to_cpu(di->id2.i_super.s_blocksize_bits),
  2069. blksz);
  2070. } else if (le16_to_cpu(di->id2.i_super.s_major_rev_level) !=
  2071. OCFS2_MAJOR_REV_LEVEL ||
  2072. le16_to_cpu(di->id2.i_super.s_minor_rev_level) !=
  2073. OCFS2_MINOR_REV_LEVEL) {
  2074. mlog(ML_ERROR, "found superblock with bad version: "
  2075. "found %u.%u, should be %u.%u\n",
  2076. le16_to_cpu(di->id2.i_super.s_major_rev_level),
  2077. le16_to_cpu(di->id2.i_super.s_minor_rev_level),
  2078. OCFS2_MAJOR_REV_LEVEL,
  2079. OCFS2_MINOR_REV_LEVEL);
  2080. } else if (bh->b_blocknr != le64_to_cpu(di->i_blkno)) {
  2081. mlog(ML_ERROR, "bad block number on superblock: "
  2082. "found %llu, should be %llu\n",
  2083. (unsigned long long)le64_to_cpu(di->i_blkno),
  2084. (unsigned long long)bh->b_blocknr);
  2085. } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
  2086. le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
  2087. mlog(ML_ERROR, "bad cluster size found: %u\n",
  2088. 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
  2089. } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
  2090. mlog(ML_ERROR, "bad root_blkno: 0\n");
  2091. } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
  2092. mlog(ML_ERROR, "bad system_dir_blkno: 0\n");
  2093. } else if (le16_to_cpu(di->id2.i_super.s_max_slots) > OCFS2_MAX_SLOTS) {
  2094. mlog(ML_ERROR,
  2095. "Superblock slots found greater than file system "
  2096. "maximum: found %u, max %u\n",
  2097. le16_to_cpu(di->id2.i_super.s_max_slots),
  2098. OCFS2_MAX_SLOTS);
  2099. } else {
  2100. /* found it! */
  2101. status = 0;
  2102. }
  2103. }
  2104. out:
  2105. if (status && status != -EAGAIN)
  2106. mlog_errno(status);
  2107. return status;
  2108. }
  2109. static int ocfs2_check_volume(struct ocfs2_super *osb)
  2110. {
  2111. int status;
  2112. int dirty;
  2113. int local;
  2114. struct ocfs2_dinode *local_alloc = NULL; /* only used if we
  2115. * recover
  2116. * ourselves. */
  2117. /* Init our journal object. */
  2118. status = ocfs2_journal_init(osb->journal, &dirty);
  2119. if (status < 0) {
  2120. mlog(ML_ERROR, "Could not initialize journal!\n");
  2121. goto finally;
  2122. }
  2123. /* Now that journal has been initialized, check to make sure
  2124. entire volume is addressable. */
  2125. status = ocfs2_journal_addressable(osb);
  2126. if (status)
  2127. goto finally;
  2128. /* If the journal was unmounted cleanly then we don't want to
  2129. * recover anything. Otherwise, journal_load will do that
  2130. * dirty work for us :) */
  2131. if (!dirty) {
  2132. status = ocfs2_journal_wipe(osb->journal, 0);
  2133. if (status < 0) {
  2134. mlog_errno(status);
  2135. goto finally;
  2136. }
  2137. } else {
  2138. printk(KERN_NOTICE "ocfs2: File system on device (%s) was not "
  2139. "unmounted cleanly, recovering it.\n", osb->dev_str);
  2140. }
  2141. local = ocfs2_mount_local(osb);
  2142. /* will play back anything left in the journal. */
  2143. status = ocfs2_journal_load(osb->journal, local, dirty);
  2144. if (status < 0) {
  2145. mlog(ML_ERROR, "ocfs2 journal load failed! %d\n", status);
  2146. goto finally;
  2147. }
  2148. if (osb->s_mount_opt & OCFS2_MOUNT_JOURNAL_ASYNC_COMMIT)
  2149. jbd2_journal_set_features(osb->journal->j_journal,
  2150. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2151. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2152. else
  2153. jbd2_journal_clear_features(osb->journal->j_journal,
  2154. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2155. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2156. if (dirty) {
  2157. /* recover my local alloc if we didn't unmount cleanly. */
  2158. status = ocfs2_begin_local_alloc_recovery(osb,
  2159. osb->slot_num,
  2160. &local_alloc);
  2161. if (status < 0) {
  2162. mlog_errno(status);
  2163. goto finally;
  2164. }
  2165. /* we complete the recovery process after we've marked
  2166. * ourselves as mounted. */
  2167. }
  2168. status = ocfs2_load_local_alloc(osb);
  2169. if (status < 0) {
  2170. mlog_errno(status);
  2171. goto finally;
  2172. }
  2173. if (dirty) {
  2174. /* Recovery will be completed after we've mounted the
  2175. * rest of the volume. */
  2176. osb->dirty = 1;
  2177. osb->local_alloc_copy = local_alloc;
  2178. local_alloc = NULL;
  2179. }
  2180. /* go through each journal, trylock it and if you get the
  2181. * lock, and it's marked as dirty, set the bit in the recover
  2182. * map and launch a recovery thread for it. */
  2183. status = ocfs2_mark_dead_nodes(osb);
  2184. if (status < 0) {
  2185. mlog_errno(status);
  2186. goto finally;
  2187. }
  2188. status = ocfs2_compute_replay_slots(osb);
  2189. if (status < 0)
  2190. mlog_errno(status);
  2191. finally:
  2192. kfree(local_alloc);
  2193. if (status)
  2194. mlog_errno(status);
  2195. return status;
  2196. }
  2197. /*
  2198. * The routine gets called from dismount or close whenever a dismount on
  2199. * volume is requested and the osb open count becomes 1.
  2200. * It will remove the osb from the global list and also free up all the
  2201. * initialized resources and fileobject.
  2202. */
  2203. static void ocfs2_delete_osb(struct ocfs2_super *osb)
  2204. {
  2205. /* This function assumes that the caller has the main osb resource */
  2206. ocfs2_free_slot_info(osb);
  2207. kfree(osb->osb_orphan_wipes);
  2208. kfree(osb->slot_recovery_generations);
  2209. /* FIXME
  2210. * This belongs in journal shutdown, but because we have to
  2211. * allocate osb->journal at the start of ocfs2_initialize_osb(),
  2212. * we free it here.
  2213. */
  2214. kfree(osb->journal);
  2215. kfree(osb->local_alloc_copy);
  2216. kfree(osb->uuid_str);
  2217. kfree(osb->vol_label);
  2218. ocfs2_put_dlm_debug(osb->osb_dlm_debug);
  2219. memset(osb, 0, sizeof(struct ocfs2_super));
  2220. }
  2221. /* Depending on the mount option passed, perform one of the following:
  2222. * Put OCFS2 into a readonly state (default)
  2223. * Return EIO so that only the process errs
  2224. * Fix the error as if fsck.ocfs2 -y
  2225. * panic
  2226. */
  2227. static int ocfs2_handle_error(struct super_block *sb)
  2228. {
  2229. struct ocfs2_super *osb = OCFS2_SB(sb);
  2230. int rv = 0;
  2231. ocfs2_set_osb_flag(osb, OCFS2_OSB_ERROR_FS);
  2232. pr_crit("On-disk corruption discovered. "
  2233. "Please run fsck.ocfs2 once the filesystem is unmounted.\n");
  2234. if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_PANIC) {
  2235. panic("OCFS2: (device %s): panic forced after error\n",
  2236. sb->s_id);
  2237. } else if (osb->s_mount_opt & OCFS2_MOUNT_ERRORS_CONT) {
  2238. pr_crit("OCFS2: Returning error to the calling process.\n");
  2239. rv = -EIO;
  2240. } else { /* default option */
  2241. rv = -EROFS;
  2242. if (sb->s_flags & MS_RDONLY &&
  2243. (ocfs2_is_soft_readonly(osb) ||
  2244. ocfs2_is_hard_readonly(osb)))
  2245. return rv;
  2246. pr_crit("OCFS2: File system is now read-only.\n");
  2247. sb->s_flags |= MS_RDONLY;
  2248. ocfs2_set_ro_flag(osb, 0);
  2249. }
  2250. return rv;
  2251. }
  2252. int __ocfs2_error(struct super_block *sb, const char *function,
  2253. const char *fmt, ...)
  2254. {
  2255. struct va_format vaf;
  2256. va_list args;
  2257. va_start(args, fmt);
  2258. vaf.fmt = fmt;
  2259. vaf.va = &args;
  2260. /* Not using mlog here because we want to show the actual
  2261. * function the error came from. */
  2262. printk(KERN_CRIT "OCFS2: ERROR (device %s): %s: %pV",
  2263. sb->s_id, function, &vaf);
  2264. va_end(args);
  2265. return ocfs2_handle_error(sb);
  2266. }
  2267. /* Handle critical errors. This is intentionally more drastic than
  2268. * ocfs2_handle_error, so we only use for things like journal errors,
  2269. * etc. */
  2270. void __ocfs2_abort(struct super_block *sb, const char *function,
  2271. const char *fmt, ...)
  2272. {
  2273. struct va_format vaf;
  2274. va_list args;
  2275. va_start(args, fmt);
  2276. vaf.fmt = fmt;
  2277. vaf.va = &args;
  2278. printk(KERN_CRIT "OCFS2: abort (device %s): %s: %pV",
  2279. sb->s_id, function, &vaf);
  2280. va_end(args);
  2281. /* We don't have the cluster support yet to go straight to
  2282. * hard readonly in here. Until then, we want to keep
  2283. * ocfs2_abort() so that we can at least mark critical
  2284. * errors.
  2285. *
  2286. * TODO: This should abort the journal and alert other nodes
  2287. * that our slot needs recovery. */
  2288. /* Force a panic(). This stinks, but it's better than letting
  2289. * things continue without having a proper hard readonly
  2290. * here. */
  2291. if (!ocfs2_mount_local(OCFS2_SB(sb)))
  2292. OCFS2_SB(sb)->s_mount_opt |= OCFS2_MOUNT_ERRORS_PANIC;
  2293. ocfs2_handle_error(sb);
  2294. }
  2295. /*
  2296. * Void signal blockers, because in-kernel sigprocmask() only fails
  2297. * when SIG_* is wrong.
  2298. */
  2299. void ocfs2_block_signals(sigset_t *oldset)
  2300. {
  2301. int rc;
  2302. sigset_t blocked;
  2303. sigfillset(&blocked);
  2304. rc = sigprocmask(SIG_BLOCK, &blocked, oldset);
  2305. BUG_ON(rc);
  2306. }
  2307. void ocfs2_unblock_signals(sigset_t *oldset)
  2308. {
  2309. int rc = sigprocmask(SIG_SETMASK, oldset, NULL);
  2310. BUG_ON(rc);
  2311. }
  2312. module_init(ocfs2_init);
  2313. module_exit(ocfs2_exit);