super.c 69 KB

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