super.c 69 KB

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