super.c 69 KB

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