super.c 69 KB

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