super.c 69 KB

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