super.c 67 KB

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