super.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. /*
  2. * linux/fs/ext2/super.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. */
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/fs.h>
  21. #include <linux/slab.h>
  22. #include <linux/init.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/parser.h>
  25. #include <linux/random.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/exportfs.h>
  28. #include <linux/vfs.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/mount.h>
  31. #include <linux/log2.h>
  32. #include <linux/quotaops.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/dax.h>
  35. #include "ext2.h"
  36. #include "xattr.h"
  37. #include "acl.h"
  38. static void ext2_sync_super(struct super_block *sb,
  39. struct ext2_super_block *es, int wait);
  40. static int ext2_remount (struct super_block * sb, int * flags, char * data);
  41. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
  42. static int ext2_sync_fs(struct super_block *sb, int wait);
  43. static int ext2_freeze(struct super_block *sb);
  44. static int ext2_unfreeze(struct super_block *sb);
  45. void ext2_error(struct super_block *sb, const char *function,
  46. const char *fmt, ...)
  47. {
  48. struct va_format vaf;
  49. va_list args;
  50. struct ext2_sb_info *sbi = EXT2_SB(sb);
  51. struct ext2_super_block *es = sbi->s_es;
  52. if (!(sb->s_flags & MS_RDONLY)) {
  53. spin_lock(&sbi->s_lock);
  54. sbi->s_mount_state |= EXT2_ERROR_FS;
  55. es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
  56. spin_unlock(&sbi->s_lock);
  57. ext2_sync_super(sb, es, 1);
  58. }
  59. va_start(args, fmt);
  60. vaf.fmt = fmt;
  61. vaf.va = &args;
  62. printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
  63. sb->s_id, function, &vaf);
  64. va_end(args);
  65. if (test_opt(sb, ERRORS_PANIC))
  66. panic("EXT2-fs: panic from previous error\n");
  67. if (test_opt(sb, ERRORS_RO)) {
  68. ext2_msg(sb, KERN_CRIT,
  69. "error: remounting filesystem read-only");
  70. sb->s_flags |= MS_RDONLY;
  71. }
  72. }
  73. void ext2_msg(struct super_block *sb, const char *prefix,
  74. const char *fmt, ...)
  75. {
  76. struct va_format vaf;
  77. va_list args;
  78. va_start(args, fmt);
  79. vaf.fmt = fmt;
  80. vaf.va = &args;
  81. printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
  82. va_end(args);
  83. }
  84. /*
  85. * This must be called with sbi->s_lock held.
  86. */
  87. void ext2_update_dynamic_rev(struct super_block *sb)
  88. {
  89. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  90. if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
  91. return;
  92. ext2_msg(sb, KERN_WARNING,
  93. "warning: updating to rev %d because of "
  94. "new feature flag, running e2fsck is recommended",
  95. EXT2_DYNAMIC_REV);
  96. es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
  97. es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
  98. es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
  99. /* leave es->s_feature_*compat flags alone */
  100. /* es->s_uuid will be set by e2fsck if empty */
  101. /*
  102. * The rest of the superblock fields should be zero, and if not it
  103. * means they are likely already in use, so leave them alone. We
  104. * can leave it up to e2fsck to clean up any inconsistencies there.
  105. */
  106. }
  107. static void ext2_put_super (struct super_block * sb)
  108. {
  109. int db_count;
  110. int i;
  111. struct ext2_sb_info *sbi = EXT2_SB(sb);
  112. dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  113. if (sbi->s_mb_cache) {
  114. ext2_xattr_destroy_cache(sbi->s_mb_cache);
  115. sbi->s_mb_cache = NULL;
  116. }
  117. if (!(sb->s_flags & MS_RDONLY)) {
  118. struct ext2_super_block *es = sbi->s_es;
  119. spin_lock(&sbi->s_lock);
  120. es->s_state = cpu_to_le16(sbi->s_mount_state);
  121. spin_unlock(&sbi->s_lock);
  122. ext2_sync_super(sb, es, 1);
  123. }
  124. db_count = sbi->s_gdb_count;
  125. for (i = 0; i < db_count; i++)
  126. if (sbi->s_group_desc[i])
  127. brelse (sbi->s_group_desc[i]);
  128. kfree(sbi->s_group_desc);
  129. kfree(sbi->s_debts);
  130. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  131. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  132. percpu_counter_destroy(&sbi->s_dirs_counter);
  133. brelse (sbi->s_sbh);
  134. sb->s_fs_info = NULL;
  135. kfree(sbi->s_blockgroup_lock);
  136. kfree(sbi);
  137. }
  138. static struct kmem_cache * ext2_inode_cachep;
  139. static struct inode *ext2_alloc_inode(struct super_block *sb)
  140. {
  141. struct ext2_inode_info *ei;
  142. ei = kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
  143. if (!ei)
  144. return NULL;
  145. ei->i_block_alloc_info = NULL;
  146. ei->vfs_inode.i_version = 1;
  147. #ifdef CONFIG_QUOTA
  148. memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
  149. #endif
  150. return &ei->vfs_inode;
  151. }
  152. static void ext2_i_callback(struct rcu_head *head)
  153. {
  154. struct inode *inode = container_of(head, struct inode, i_rcu);
  155. kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
  156. }
  157. static void ext2_destroy_inode(struct inode *inode)
  158. {
  159. call_rcu(&inode->i_rcu, ext2_i_callback);
  160. }
  161. static void init_once(void *foo)
  162. {
  163. struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
  164. rwlock_init(&ei->i_meta_lock);
  165. #ifdef CONFIG_EXT2_FS_XATTR
  166. init_rwsem(&ei->xattr_sem);
  167. #endif
  168. mutex_init(&ei->truncate_mutex);
  169. #ifdef CONFIG_FS_DAX
  170. init_rwsem(&ei->dax_sem);
  171. #endif
  172. inode_init_once(&ei->vfs_inode);
  173. }
  174. static int __init init_inodecache(void)
  175. {
  176. ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
  177. sizeof(struct ext2_inode_info),
  178. 0, (SLAB_RECLAIM_ACCOUNT|
  179. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  180. init_once);
  181. if (ext2_inode_cachep == NULL)
  182. return -ENOMEM;
  183. return 0;
  184. }
  185. static void destroy_inodecache(void)
  186. {
  187. /*
  188. * Make sure all delayed rcu free inodes are flushed before we
  189. * destroy cache.
  190. */
  191. rcu_barrier();
  192. kmem_cache_destroy(ext2_inode_cachep);
  193. }
  194. static int ext2_show_options(struct seq_file *seq, struct dentry *root)
  195. {
  196. struct super_block *sb = root->d_sb;
  197. struct ext2_sb_info *sbi = EXT2_SB(sb);
  198. struct ext2_super_block *es = sbi->s_es;
  199. unsigned long def_mount_opts;
  200. spin_lock(&sbi->s_lock);
  201. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  202. if (sbi->s_sb_block != 1)
  203. seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
  204. if (test_opt(sb, MINIX_DF))
  205. seq_puts(seq, ",minixdf");
  206. if (test_opt(sb, GRPID))
  207. seq_puts(seq, ",grpid");
  208. if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
  209. seq_puts(seq, ",nogrpid");
  210. if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT2_DEF_RESUID)) ||
  211. le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
  212. seq_printf(seq, ",resuid=%u",
  213. from_kuid_munged(&init_user_ns, sbi->s_resuid));
  214. }
  215. if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT2_DEF_RESGID)) ||
  216. le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
  217. seq_printf(seq, ",resgid=%u",
  218. from_kgid_munged(&init_user_ns, sbi->s_resgid));
  219. }
  220. if (test_opt(sb, ERRORS_RO)) {
  221. int def_errors = le16_to_cpu(es->s_errors);
  222. if (def_errors == EXT2_ERRORS_PANIC ||
  223. def_errors == EXT2_ERRORS_CONTINUE) {
  224. seq_puts(seq, ",errors=remount-ro");
  225. }
  226. }
  227. if (test_opt(sb, ERRORS_CONT))
  228. seq_puts(seq, ",errors=continue");
  229. if (test_opt(sb, ERRORS_PANIC))
  230. seq_puts(seq, ",errors=panic");
  231. if (test_opt(sb, NO_UID32))
  232. seq_puts(seq, ",nouid32");
  233. if (test_opt(sb, DEBUG))
  234. seq_puts(seq, ",debug");
  235. if (test_opt(sb, OLDALLOC))
  236. seq_puts(seq, ",oldalloc");
  237. #ifdef CONFIG_EXT2_FS_XATTR
  238. if (test_opt(sb, XATTR_USER))
  239. seq_puts(seq, ",user_xattr");
  240. if (!test_opt(sb, XATTR_USER) &&
  241. (def_mount_opts & EXT2_DEFM_XATTR_USER)) {
  242. seq_puts(seq, ",nouser_xattr");
  243. }
  244. #endif
  245. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  246. if (test_opt(sb, POSIX_ACL))
  247. seq_puts(seq, ",acl");
  248. if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
  249. seq_puts(seq, ",noacl");
  250. #endif
  251. if (test_opt(sb, NOBH))
  252. seq_puts(seq, ",nobh");
  253. #if defined(CONFIG_QUOTA)
  254. if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
  255. seq_puts(seq, ",usrquota");
  256. if (sbi->s_mount_opt & EXT2_MOUNT_GRPQUOTA)
  257. seq_puts(seq, ",grpquota");
  258. #endif
  259. #ifdef CONFIG_FS_DAX
  260. if (sbi->s_mount_opt & EXT2_MOUNT_XIP)
  261. seq_puts(seq, ",xip");
  262. if (sbi->s_mount_opt & EXT2_MOUNT_DAX)
  263. seq_puts(seq, ",dax");
  264. #endif
  265. if (!test_opt(sb, RESERVATION))
  266. seq_puts(seq, ",noreservation");
  267. spin_unlock(&sbi->s_lock);
  268. return 0;
  269. }
  270. #ifdef CONFIG_QUOTA
  271. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
  272. static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
  273. static struct dquot **ext2_get_dquots(struct inode *inode)
  274. {
  275. return EXT2_I(inode)->i_dquot;
  276. }
  277. #endif
  278. static const struct super_operations ext2_sops = {
  279. .alloc_inode = ext2_alloc_inode,
  280. .destroy_inode = ext2_destroy_inode,
  281. .write_inode = ext2_write_inode,
  282. .evict_inode = ext2_evict_inode,
  283. .put_super = ext2_put_super,
  284. .sync_fs = ext2_sync_fs,
  285. .freeze_fs = ext2_freeze,
  286. .unfreeze_fs = ext2_unfreeze,
  287. .statfs = ext2_statfs,
  288. .remount_fs = ext2_remount,
  289. .show_options = ext2_show_options,
  290. #ifdef CONFIG_QUOTA
  291. .quota_read = ext2_quota_read,
  292. .quota_write = ext2_quota_write,
  293. .get_dquots = ext2_get_dquots,
  294. #endif
  295. };
  296. static struct inode *ext2_nfs_get_inode(struct super_block *sb,
  297. u64 ino, u32 generation)
  298. {
  299. struct inode *inode;
  300. if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
  301. return ERR_PTR(-ESTALE);
  302. if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  303. return ERR_PTR(-ESTALE);
  304. /*
  305. * ext2_iget isn't quite right if the inode is currently unallocated!
  306. * However ext2_iget currently does appropriate checks to handle stale
  307. * inodes so everything is OK.
  308. */
  309. inode = ext2_iget(sb, ino);
  310. if (IS_ERR(inode))
  311. return ERR_CAST(inode);
  312. if (generation && inode->i_generation != generation) {
  313. /* we didn't find the right inode.. */
  314. iput(inode);
  315. return ERR_PTR(-ESTALE);
  316. }
  317. return inode;
  318. }
  319. static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  320. int fh_len, int fh_type)
  321. {
  322. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  323. ext2_nfs_get_inode);
  324. }
  325. static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
  326. int fh_len, int fh_type)
  327. {
  328. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  329. ext2_nfs_get_inode);
  330. }
  331. static const struct export_operations ext2_export_ops = {
  332. .fh_to_dentry = ext2_fh_to_dentry,
  333. .fh_to_parent = ext2_fh_to_parent,
  334. .get_parent = ext2_get_parent,
  335. };
  336. static unsigned long get_sb_block(void **data)
  337. {
  338. unsigned long sb_block;
  339. char *options = (char *) *data;
  340. if (!options || strncmp(options, "sb=", 3) != 0)
  341. return 1; /* Default location */
  342. options += 3;
  343. sb_block = simple_strtoul(options, &options, 0);
  344. if (*options && *options != ',') {
  345. printk("EXT2-fs: Invalid sb specification: %s\n",
  346. (char *) *data);
  347. return 1;
  348. }
  349. if (*options == ',')
  350. options++;
  351. *data = (void *) options;
  352. return sb_block;
  353. }
  354. enum {
  355. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  356. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
  357. Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
  358. Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
  359. Opt_acl, Opt_noacl, Opt_xip, Opt_dax, Opt_ignore, Opt_err, Opt_quota,
  360. Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
  361. };
  362. static const match_table_t tokens = {
  363. {Opt_bsd_df, "bsddf"},
  364. {Opt_minix_df, "minixdf"},
  365. {Opt_grpid, "grpid"},
  366. {Opt_grpid, "bsdgroups"},
  367. {Opt_nogrpid, "nogrpid"},
  368. {Opt_nogrpid, "sysvgroups"},
  369. {Opt_resgid, "resgid=%u"},
  370. {Opt_resuid, "resuid=%u"},
  371. {Opt_sb, "sb=%u"},
  372. {Opt_err_cont, "errors=continue"},
  373. {Opt_err_panic, "errors=panic"},
  374. {Opt_err_ro, "errors=remount-ro"},
  375. {Opt_nouid32, "nouid32"},
  376. {Opt_nocheck, "check=none"},
  377. {Opt_nocheck, "nocheck"},
  378. {Opt_debug, "debug"},
  379. {Opt_oldalloc, "oldalloc"},
  380. {Opt_orlov, "orlov"},
  381. {Opt_nobh, "nobh"},
  382. {Opt_user_xattr, "user_xattr"},
  383. {Opt_nouser_xattr, "nouser_xattr"},
  384. {Opt_acl, "acl"},
  385. {Opt_noacl, "noacl"},
  386. {Opt_xip, "xip"},
  387. {Opt_dax, "dax"},
  388. {Opt_grpquota, "grpquota"},
  389. {Opt_ignore, "noquota"},
  390. {Opt_quota, "quota"},
  391. {Opt_usrquota, "usrquota"},
  392. {Opt_reservation, "reservation"},
  393. {Opt_noreservation, "noreservation"},
  394. {Opt_err, NULL}
  395. };
  396. static int parse_options(char *options, struct super_block *sb)
  397. {
  398. char *p;
  399. struct ext2_sb_info *sbi = EXT2_SB(sb);
  400. substring_t args[MAX_OPT_ARGS];
  401. int option;
  402. kuid_t uid;
  403. kgid_t gid;
  404. if (!options)
  405. return 1;
  406. while ((p = strsep (&options, ",")) != NULL) {
  407. int token;
  408. if (!*p)
  409. continue;
  410. token = match_token(p, tokens, args);
  411. switch (token) {
  412. case Opt_bsd_df:
  413. clear_opt (sbi->s_mount_opt, MINIX_DF);
  414. break;
  415. case Opt_minix_df:
  416. set_opt (sbi->s_mount_opt, MINIX_DF);
  417. break;
  418. case Opt_grpid:
  419. set_opt (sbi->s_mount_opt, GRPID);
  420. break;
  421. case Opt_nogrpid:
  422. clear_opt (sbi->s_mount_opt, GRPID);
  423. break;
  424. case Opt_resuid:
  425. if (match_int(&args[0], &option))
  426. return 0;
  427. uid = make_kuid(current_user_ns(), option);
  428. if (!uid_valid(uid)) {
  429. ext2_msg(sb, KERN_ERR, "Invalid uid value %d", option);
  430. return 0;
  431. }
  432. sbi->s_resuid = uid;
  433. break;
  434. case Opt_resgid:
  435. if (match_int(&args[0], &option))
  436. return 0;
  437. gid = make_kgid(current_user_ns(), option);
  438. if (!gid_valid(gid)) {
  439. ext2_msg(sb, KERN_ERR, "Invalid gid value %d", option);
  440. return 0;
  441. }
  442. sbi->s_resgid = gid;
  443. break;
  444. case Opt_sb:
  445. /* handled by get_sb_block() instead of here */
  446. /* *sb_block = match_int(&args[0]); */
  447. break;
  448. case Opt_err_panic:
  449. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  450. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  451. set_opt (sbi->s_mount_opt, ERRORS_PANIC);
  452. break;
  453. case Opt_err_ro:
  454. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  455. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  456. set_opt (sbi->s_mount_opt, ERRORS_RO);
  457. break;
  458. case Opt_err_cont:
  459. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  460. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  461. set_opt (sbi->s_mount_opt, ERRORS_CONT);
  462. break;
  463. case Opt_nouid32:
  464. set_opt (sbi->s_mount_opt, NO_UID32);
  465. break;
  466. case Opt_nocheck:
  467. clear_opt (sbi->s_mount_opt, CHECK);
  468. break;
  469. case Opt_debug:
  470. set_opt (sbi->s_mount_opt, DEBUG);
  471. break;
  472. case Opt_oldalloc:
  473. set_opt (sbi->s_mount_opt, OLDALLOC);
  474. break;
  475. case Opt_orlov:
  476. clear_opt (sbi->s_mount_opt, OLDALLOC);
  477. break;
  478. case Opt_nobh:
  479. set_opt (sbi->s_mount_opt, NOBH);
  480. break;
  481. #ifdef CONFIG_EXT2_FS_XATTR
  482. case Opt_user_xattr:
  483. set_opt (sbi->s_mount_opt, XATTR_USER);
  484. break;
  485. case Opt_nouser_xattr:
  486. clear_opt (sbi->s_mount_opt, XATTR_USER);
  487. break;
  488. #else
  489. case Opt_user_xattr:
  490. case Opt_nouser_xattr:
  491. ext2_msg(sb, KERN_INFO, "(no)user_xattr options"
  492. "not supported");
  493. break;
  494. #endif
  495. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  496. case Opt_acl:
  497. set_opt(sbi->s_mount_opt, POSIX_ACL);
  498. break;
  499. case Opt_noacl:
  500. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  501. break;
  502. #else
  503. case Opt_acl:
  504. case Opt_noacl:
  505. ext2_msg(sb, KERN_INFO,
  506. "(no)acl options not supported");
  507. break;
  508. #endif
  509. case Opt_xip:
  510. ext2_msg(sb, KERN_INFO, "use dax instead of xip");
  511. set_opt(sbi->s_mount_opt, XIP);
  512. /* Fall through */
  513. case Opt_dax:
  514. #ifdef CONFIG_FS_DAX
  515. ext2_msg(sb, KERN_WARNING,
  516. "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
  517. set_opt(sbi->s_mount_opt, DAX);
  518. #else
  519. ext2_msg(sb, KERN_INFO, "dax option not supported");
  520. #endif
  521. break;
  522. #if defined(CONFIG_QUOTA)
  523. case Opt_quota:
  524. case Opt_usrquota:
  525. set_opt(sbi->s_mount_opt, USRQUOTA);
  526. break;
  527. case Opt_grpquota:
  528. set_opt(sbi->s_mount_opt, GRPQUOTA);
  529. break;
  530. #else
  531. case Opt_quota:
  532. case Opt_usrquota:
  533. case Opt_grpquota:
  534. ext2_msg(sb, KERN_INFO,
  535. "quota operations not supported");
  536. break;
  537. #endif
  538. case Opt_reservation:
  539. set_opt(sbi->s_mount_opt, RESERVATION);
  540. ext2_msg(sb, KERN_INFO, "reservations ON");
  541. break;
  542. case Opt_noreservation:
  543. clear_opt(sbi->s_mount_opt, RESERVATION);
  544. ext2_msg(sb, KERN_INFO, "reservations OFF");
  545. break;
  546. case Opt_ignore:
  547. break;
  548. default:
  549. return 0;
  550. }
  551. }
  552. return 1;
  553. }
  554. static int ext2_setup_super (struct super_block * sb,
  555. struct ext2_super_block * es,
  556. int read_only)
  557. {
  558. int res = 0;
  559. struct ext2_sb_info *sbi = EXT2_SB(sb);
  560. if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
  561. ext2_msg(sb, KERN_ERR,
  562. "error: revision level too high, "
  563. "forcing read-only mode");
  564. res = MS_RDONLY;
  565. }
  566. if (read_only)
  567. return res;
  568. if (!(sbi->s_mount_state & EXT2_VALID_FS))
  569. ext2_msg(sb, KERN_WARNING,
  570. "warning: mounting unchecked fs, "
  571. "running e2fsck is recommended");
  572. else if ((sbi->s_mount_state & EXT2_ERROR_FS))
  573. ext2_msg(sb, KERN_WARNING,
  574. "warning: mounting fs with errors, "
  575. "running e2fsck is recommended");
  576. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  577. le16_to_cpu(es->s_mnt_count) >=
  578. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  579. ext2_msg(sb, KERN_WARNING,
  580. "warning: maximal mount count reached, "
  581. "running e2fsck is recommended");
  582. else if (le32_to_cpu(es->s_checkinterval) &&
  583. (le32_to_cpu(es->s_lastcheck) +
  584. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  585. ext2_msg(sb, KERN_WARNING,
  586. "warning: checktime reached, "
  587. "running e2fsck is recommended");
  588. if (!le16_to_cpu(es->s_max_mnt_count))
  589. es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
  590. le16_add_cpu(&es->s_mnt_count, 1);
  591. if (test_opt (sb, DEBUG))
  592. ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
  593. "bpg=%lu, ipg=%lu, mo=%04lx]",
  594. EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
  595. sbi->s_frag_size,
  596. sbi->s_groups_count,
  597. EXT2_BLOCKS_PER_GROUP(sb),
  598. EXT2_INODES_PER_GROUP(sb),
  599. sbi->s_mount_opt);
  600. return res;
  601. }
  602. static int ext2_check_descriptors(struct super_block *sb)
  603. {
  604. int i;
  605. struct ext2_sb_info *sbi = EXT2_SB(sb);
  606. ext2_debug ("Checking group descriptors");
  607. for (i = 0; i < sbi->s_groups_count; i++) {
  608. struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
  609. ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
  610. ext2_fsblk_t last_block;
  611. if (i == sbi->s_groups_count - 1)
  612. last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
  613. else
  614. last_block = first_block +
  615. (EXT2_BLOCKS_PER_GROUP(sb) - 1);
  616. if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
  617. le32_to_cpu(gdp->bg_block_bitmap) > last_block)
  618. {
  619. ext2_error (sb, "ext2_check_descriptors",
  620. "Block bitmap for group %d"
  621. " not in group (block %lu)!",
  622. i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
  623. return 0;
  624. }
  625. if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
  626. le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
  627. {
  628. ext2_error (sb, "ext2_check_descriptors",
  629. "Inode bitmap for group %d"
  630. " not in group (block %lu)!",
  631. i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
  632. return 0;
  633. }
  634. if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
  635. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
  636. last_block)
  637. {
  638. ext2_error (sb, "ext2_check_descriptors",
  639. "Inode table for group %d"
  640. " not in group (block %lu)!",
  641. i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
  642. return 0;
  643. }
  644. }
  645. return 1;
  646. }
  647. /*
  648. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  649. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  650. * We need to be 1 filesystem block less than the 2^32 sector limit.
  651. */
  652. static loff_t ext2_max_size(int bits)
  653. {
  654. loff_t res = EXT2_NDIR_BLOCKS;
  655. int meta_blocks;
  656. loff_t upper_limit;
  657. /* This is calculated to be the largest file size for a
  658. * dense, file such that the total number of
  659. * sectors in the file, including data and all indirect blocks,
  660. * does not exceed 2^32 -1
  661. * __u32 i_blocks representing the total number of
  662. * 512 bytes blocks of the file
  663. */
  664. upper_limit = (1LL << 32) - 1;
  665. /* total blocks in file system block size */
  666. upper_limit >>= (bits - 9);
  667. /* indirect blocks */
  668. meta_blocks = 1;
  669. /* double indirect blocks */
  670. meta_blocks += 1 + (1LL << (bits-2));
  671. /* tripple indirect blocks */
  672. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  673. upper_limit -= meta_blocks;
  674. upper_limit <<= bits;
  675. res += 1LL << (bits-2);
  676. res += 1LL << (2*(bits-2));
  677. res += 1LL << (3*(bits-2));
  678. res <<= bits;
  679. if (res > upper_limit)
  680. res = upper_limit;
  681. if (res > MAX_LFS_FILESIZE)
  682. res = MAX_LFS_FILESIZE;
  683. return res;
  684. }
  685. static unsigned long descriptor_loc(struct super_block *sb,
  686. unsigned long logic_sb_block,
  687. int nr)
  688. {
  689. struct ext2_sb_info *sbi = EXT2_SB(sb);
  690. unsigned long bg, first_meta_bg;
  691. int has_super = 0;
  692. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  693. if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
  694. nr < first_meta_bg)
  695. return (logic_sb_block + nr + 1);
  696. bg = sbi->s_desc_per_block * nr;
  697. if (ext2_bg_has_super(sb, bg))
  698. has_super = 1;
  699. return ext2_group_first_block_no(sb, bg) + has_super;
  700. }
  701. static int ext2_fill_super(struct super_block *sb, void *data, int silent)
  702. {
  703. struct buffer_head * bh;
  704. struct ext2_sb_info * sbi;
  705. struct ext2_super_block * es;
  706. struct inode *root;
  707. unsigned long block;
  708. unsigned long sb_block = get_sb_block(&data);
  709. unsigned long logic_sb_block;
  710. unsigned long offset = 0;
  711. unsigned long def_mount_opts;
  712. long ret = -EINVAL;
  713. int blocksize = BLOCK_SIZE;
  714. int db_count;
  715. int i, j;
  716. __le32 features;
  717. int err;
  718. err = -ENOMEM;
  719. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  720. if (!sbi)
  721. goto failed;
  722. sbi->s_blockgroup_lock =
  723. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  724. if (!sbi->s_blockgroup_lock) {
  725. kfree(sbi);
  726. goto failed;
  727. }
  728. sb->s_fs_info = sbi;
  729. sbi->s_sb_block = sb_block;
  730. spin_lock_init(&sbi->s_lock);
  731. /*
  732. * See what the current blocksize for the device is, and
  733. * use that as the blocksize. Otherwise (or if the blocksize
  734. * is smaller than the default) use the default.
  735. * This is important for devices that have a hardware
  736. * sectorsize that is larger than the default.
  737. */
  738. blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
  739. if (!blocksize) {
  740. ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
  741. goto failed_sbi;
  742. }
  743. /*
  744. * If the superblock doesn't start on a hardware sector boundary,
  745. * calculate the offset.
  746. */
  747. if (blocksize != BLOCK_SIZE) {
  748. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  749. offset = (sb_block*BLOCK_SIZE) % blocksize;
  750. } else {
  751. logic_sb_block = sb_block;
  752. }
  753. if (!(bh = sb_bread(sb, logic_sb_block))) {
  754. ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
  755. goto failed_sbi;
  756. }
  757. /*
  758. * Note: s_es must be initialized as soon as possible because
  759. * some ext2 macro-instructions depend on its value
  760. */
  761. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  762. sbi->s_es = es;
  763. sb->s_magic = le16_to_cpu(es->s_magic);
  764. if (sb->s_magic != EXT2_SUPER_MAGIC)
  765. goto cantfind_ext2;
  766. /* Set defaults before we parse the mount options */
  767. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  768. if (def_mount_opts & EXT2_DEFM_DEBUG)
  769. set_opt(sbi->s_mount_opt, DEBUG);
  770. if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
  771. set_opt(sbi->s_mount_opt, GRPID);
  772. if (def_mount_opts & EXT2_DEFM_UID16)
  773. set_opt(sbi->s_mount_opt, NO_UID32);
  774. #ifdef CONFIG_EXT2_FS_XATTR
  775. if (def_mount_opts & EXT2_DEFM_XATTR_USER)
  776. set_opt(sbi->s_mount_opt, XATTR_USER);
  777. #endif
  778. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  779. if (def_mount_opts & EXT2_DEFM_ACL)
  780. set_opt(sbi->s_mount_opt, POSIX_ACL);
  781. #endif
  782. if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
  783. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  784. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
  785. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  786. else
  787. set_opt(sbi->s_mount_opt, ERRORS_RO);
  788. sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
  789. sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
  790. set_opt(sbi->s_mount_opt, RESERVATION);
  791. if (!parse_options((char *) data, sb))
  792. goto failed_mount;
  793. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  794. ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
  795. MS_POSIXACL : 0);
  796. sb->s_iflags |= SB_I_CGROUPWB;
  797. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
  798. (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
  799. EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  800. EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  801. ext2_msg(sb, KERN_WARNING,
  802. "warning: feature flags set on rev 0 fs, "
  803. "running e2fsck is recommended");
  804. /*
  805. * Check feature flags regardless of the revision level, since we
  806. * previously didn't change the revision level when setting the flags,
  807. * so there is a chance incompat flags are set on a rev 0 filesystem.
  808. */
  809. features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
  810. if (features) {
  811. ext2_msg(sb, KERN_ERR, "error: couldn't mount because of "
  812. "unsupported optional features (%x)",
  813. le32_to_cpu(features));
  814. goto failed_mount;
  815. }
  816. if (!(sb->s_flags & MS_RDONLY) &&
  817. (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
  818. ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
  819. "unsupported optional features (%x)",
  820. le32_to_cpu(features));
  821. goto failed_mount;
  822. }
  823. blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
  824. if (sbi->s_mount_opt & EXT2_MOUNT_DAX) {
  825. err = bdev_dax_supported(sb, blocksize);
  826. if (err)
  827. goto failed_mount;
  828. }
  829. /* If the blocksize doesn't match, re-read the thing.. */
  830. if (sb->s_blocksize != blocksize) {
  831. brelse(bh);
  832. if (!sb_set_blocksize(sb, blocksize)) {
  833. ext2_msg(sb, KERN_ERR,
  834. "error: bad blocksize %d", blocksize);
  835. goto failed_sbi;
  836. }
  837. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  838. offset = (sb_block*BLOCK_SIZE) % blocksize;
  839. bh = sb_bread(sb, logic_sb_block);
  840. if(!bh) {
  841. ext2_msg(sb, KERN_ERR, "error: couldn't read"
  842. "superblock on 2nd try");
  843. goto failed_sbi;
  844. }
  845. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  846. sbi->s_es = es;
  847. if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
  848. ext2_msg(sb, KERN_ERR, "error: magic mismatch");
  849. goto failed_mount;
  850. }
  851. }
  852. sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
  853. sb->s_max_links = EXT2_LINK_MAX;
  854. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
  855. sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
  856. sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
  857. } else {
  858. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  859. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  860. if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
  861. !is_power_of_2(sbi->s_inode_size) ||
  862. (sbi->s_inode_size > blocksize)) {
  863. ext2_msg(sb, KERN_ERR,
  864. "error: unsupported inode size: %d",
  865. sbi->s_inode_size);
  866. goto failed_mount;
  867. }
  868. }
  869. sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
  870. le32_to_cpu(es->s_log_frag_size);
  871. if (sbi->s_frag_size == 0)
  872. goto cantfind_ext2;
  873. sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
  874. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  875. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  876. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  877. if (EXT2_INODE_SIZE(sb) == 0)
  878. goto cantfind_ext2;
  879. sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
  880. if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
  881. goto cantfind_ext2;
  882. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  883. sbi->s_inodes_per_block;
  884. sbi->s_desc_per_block = sb->s_blocksize /
  885. sizeof (struct ext2_group_desc);
  886. sbi->s_sbh = bh;
  887. sbi->s_mount_state = le16_to_cpu(es->s_state);
  888. sbi->s_addr_per_block_bits =
  889. ilog2 (EXT2_ADDR_PER_BLOCK(sb));
  890. sbi->s_desc_per_block_bits =
  891. ilog2 (EXT2_DESC_PER_BLOCK(sb));
  892. if (sb->s_magic != EXT2_SUPER_MAGIC)
  893. goto cantfind_ext2;
  894. if (sb->s_blocksize != bh->b_size) {
  895. if (!silent)
  896. ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
  897. goto failed_mount;
  898. }
  899. if (sb->s_blocksize != sbi->s_frag_size) {
  900. ext2_msg(sb, KERN_ERR,
  901. "error: fragsize %lu != blocksize %lu"
  902. "(not supported yet)",
  903. sbi->s_frag_size, sb->s_blocksize);
  904. goto failed_mount;
  905. }
  906. if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
  907. ext2_msg(sb, KERN_ERR,
  908. "error: #blocks per group too big: %lu",
  909. sbi->s_blocks_per_group);
  910. goto failed_mount;
  911. }
  912. if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
  913. ext2_msg(sb, KERN_ERR,
  914. "error: #fragments per group too big: %lu",
  915. sbi->s_frags_per_group);
  916. goto failed_mount;
  917. }
  918. if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
  919. ext2_msg(sb, KERN_ERR,
  920. "error: #inodes per group too big: %lu",
  921. sbi->s_inodes_per_group);
  922. goto failed_mount;
  923. }
  924. if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
  925. goto cantfind_ext2;
  926. sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
  927. le32_to_cpu(es->s_first_data_block) - 1)
  928. / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
  929. db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
  930. EXT2_DESC_PER_BLOCK(sb);
  931. sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
  932. if (sbi->s_group_desc == NULL) {
  933. ext2_msg(sb, KERN_ERR, "error: not enough memory");
  934. goto failed_mount;
  935. }
  936. bgl_lock_init(sbi->s_blockgroup_lock);
  937. sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
  938. if (!sbi->s_debts) {
  939. ext2_msg(sb, KERN_ERR, "error: not enough memory");
  940. goto failed_mount_group_desc;
  941. }
  942. for (i = 0; i < db_count; i++) {
  943. block = descriptor_loc(sb, logic_sb_block, i);
  944. sbi->s_group_desc[i] = sb_bread(sb, block);
  945. if (!sbi->s_group_desc[i]) {
  946. for (j = 0; j < i; j++)
  947. brelse (sbi->s_group_desc[j]);
  948. ext2_msg(sb, KERN_ERR,
  949. "error: unable to read group descriptors");
  950. goto failed_mount_group_desc;
  951. }
  952. }
  953. if (!ext2_check_descriptors (sb)) {
  954. ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
  955. goto failed_mount2;
  956. }
  957. sbi->s_gdb_count = db_count;
  958. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  959. spin_lock_init(&sbi->s_next_gen_lock);
  960. /* per fileystem reservation list head & lock */
  961. spin_lock_init(&sbi->s_rsv_window_lock);
  962. sbi->s_rsv_window_root = RB_ROOT;
  963. /*
  964. * Add a single, static dummy reservation to the start of the
  965. * reservation window list --- it gives us a placeholder for
  966. * append-at-start-of-list which makes the allocation logic
  967. * _much_ simpler.
  968. */
  969. sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  970. sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  971. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  972. sbi->s_rsv_window_head.rsv_goal_size = 0;
  973. ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
  974. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  975. ext2_count_free_blocks(sb), GFP_KERNEL);
  976. if (!err) {
  977. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  978. ext2_count_free_inodes(sb), GFP_KERNEL);
  979. }
  980. if (!err) {
  981. err = percpu_counter_init(&sbi->s_dirs_counter,
  982. ext2_count_dirs(sb), GFP_KERNEL);
  983. }
  984. if (err) {
  985. ext2_msg(sb, KERN_ERR, "error: insufficient memory");
  986. goto failed_mount3;
  987. }
  988. #ifdef CONFIG_EXT2_FS_XATTR
  989. sbi->s_mb_cache = ext2_xattr_create_cache();
  990. if (!sbi->s_mb_cache) {
  991. ext2_msg(sb, KERN_ERR, "Failed to create an mb_cache");
  992. goto failed_mount3;
  993. }
  994. #endif
  995. /*
  996. * set up enough so that it can read an inode
  997. */
  998. sb->s_op = &ext2_sops;
  999. sb->s_export_op = &ext2_export_ops;
  1000. sb->s_xattr = ext2_xattr_handlers;
  1001. #ifdef CONFIG_QUOTA
  1002. sb->dq_op = &dquot_operations;
  1003. sb->s_qcop = &dquot_quotactl_ops;
  1004. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
  1005. #endif
  1006. root = ext2_iget(sb, EXT2_ROOT_INO);
  1007. if (IS_ERR(root)) {
  1008. ret = PTR_ERR(root);
  1009. goto failed_mount3;
  1010. }
  1011. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1012. iput(root);
  1013. ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
  1014. goto failed_mount3;
  1015. }
  1016. sb->s_root = d_make_root(root);
  1017. if (!sb->s_root) {
  1018. ext2_msg(sb, KERN_ERR, "error: get root inode failed");
  1019. ret = -ENOMEM;
  1020. goto failed_mount3;
  1021. }
  1022. if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
  1023. ext2_msg(sb, KERN_WARNING,
  1024. "warning: mounting ext3 filesystem as ext2");
  1025. if (ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY))
  1026. sb->s_flags |= MS_RDONLY;
  1027. ext2_write_super(sb);
  1028. return 0;
  1029. cantfind_ext2:
  1030. if (!silent)
  1031. ext2_msg(sb, KERN_ERR,
  1032. "error: can't find an ext2 filesystem on dev %s.",
  1033. sb->s_id);
  1034. goto failed_mount;
  1035. failed_mount3:
  1036. if (sbi->s_mb_cache)
  1037. ext2_xattr_destroy_cache(sbi->s_mb_cache);
  1038. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  1039. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  1040. percpu_counter_destroy(&sbi->s_dirs_counter);
  1041. failed_mount2:
  1042. for (i = 0; i < db_count; i++)
  1043. brelse(sbi->s_group_desc[i]);
  1044. failed_mount_group_desc:
  1045. kfree(sbi->s_group_desc);
  1046. kfree(sbi->s_debts);
  1047. failed_mount:
  1048. brelse(bh);
  1049. failed_sbi:
  1050. sb->s_fs_info = NULL;
  1051. kfree(sbi->s_blockgroup_lock);
  1052. kfree(sbi);
  1053. failed:
  1054. return ret;
  1055. }
  1056. static void ext2_clear_super_error(struct super_block *sb)
  1057. {
  1058. struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
  1059. if (buffer_write_io_error(sbh)) {
  1060. /*
  1061. * Oh, dear. A previous attempt to write the
  1062. * superblock failed. This could happen because the
  1063. * USB device was yanked out. Or it could happen to
  1064. * be a transient write error and maybe the block will
  1065. * be remapped. Nothing we can do but to retry the
  1066. * write and hope for the best.
  1067. */
  1068. ext2_msg(sb, KERN_ERR,
  1069. "previous I/O error to superblock detected\n");
  1070. clear_buffer_write_io_error(sbh);
  1071. set_buffer_uptodate(sbh);
  1072. }
  1073. }
  1074. static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
  1075. int wait)
  1076. {
  1077. ext2_clear_super_error(sb);
  1078. spin_lock(&EXT2_SB(sb)->s_lock);
  1079. es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
  1080. es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
  1081. es->s_wtime = cpu_to_le32(get_seconds());
  1082. /* unlock before we do IO */
  1083. spin_unlock(&EXT2_SB(sb)->s_lock);
  1084. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  1085. if (wait)
  1086. sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
  1087. }
  1088. /*
  1089. * In the second extended file system, it is not necessary to
  1090. * write the super block since we use a mapping of the
  1091. * disk super block in a buffer.
  1092. *
  1093. * However, this function is still used to set the fs valid
  1094. * flags to 0. We need to set this flag to 0 since the fs
  1095. * may have been checked while mounted and e2fsck may have
  1096. * set s_state to EXT2_VALID_FS after some corrections.
  1097. */
  1098. static int ext2_sync_fs(struct super_block *sb, int wait)
  1099. {
  1100. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1101. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  1102. /*
  1103. * Write quota structures to quota file, sync_blockdev() will write
  1104. * them to disk later
  1105. */
  1106. dquot_writeback_dquots(sb, -1);
  1107. spin_lock(&sbi->s_lock);
  1108. if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
  1109. ext2_debug("setting valid to 0\n");
  1110. es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
  1111. }
  1112. spin_unlock(&sbi->s_lock);
  1113. ext2_sync_super(sb, es, wait);
  1114. return 0;
  1115. }
  1116. static int ext2_freeze(struct super_block *sb)
  1117. {
  1118. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1119. /*
  1120. * Open but unlinked files present? Keep EXT2_VALID_FS flag cleared
  1121. * because we have unattached inodes and thus filesystem is not fully
  1122. * consistent.
  1123. */
  1124. if (atomic_long_read(&sb->s_remove_count)) {
  1125. ext2_sync_fs(sb, 1);
  1126. return 0;
  1127. }
  1128. /* Set EXT2_FS_VALID flag */
  1129. spin_lock(&sbi->s_lock);
  1130. sbi->s_es->s_state = cpu_to_le16(sbi->s_mount_state);
  1131. spin_unlock(&sbi->s_lock);
  1132. ext2_sync_super(sb, sbi->s_es, 1);
  1133. return 0;
  1134. }
  1135. static int ext2_unfreeze(struct super_block *sb)
  1136. {
  1137. /* Just write sb to clear EXT2_VALID_FS flag */
  1138. ext2_write_super(sb);
  1139. return 0;
  1140. }
  1141. void ext2_write_super(struct super_block *sb)
  1142. {
  1143. if (!(sb->s_flags & MS_RDONLY))
  1144. ext2_sync_fs(sb, 1);
  1145. }
  1146. static int ext2_remount (struct super_block * sb, int * flags, char * data)
  1147. {
  1148. struct ext2_sb_info * sbi = EXT2_SB(sb);
  1149. struct ext2_super_block * es;
  1150. struct ext2_mount_options old_opts;
  1151. unsigned long old_sb_flags;
  1152. int err;
  1153. sync_filesystem(sb);
  1154. spin_lock(&sbi->s_lock);
  1155. /* Store the old options */
  1156. old_sb_flags = sb->s_flags;
  1157. old_opts.s_mount_opt = sbi->s_mount_opt;
  1158. old_opts.s_resuid = sbi->s_resuid;
  1159. old_opts.s_resgid = sbi->s_resgid;
  1160. /*
  1161. * Allow the "check" option to be passed as a remount option.
  1162. */
  1163. if (!parse_options(data, sb)) {
  1164. err = -EINVAL;
  1165. goto restore_opts;
  1166. }
  1167. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1168. ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1169. es = sbi->s_es;
  1170. if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT2_MOUNT_DAX) {
  1171. ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
  1172. "dax flag with busy inodes while remounting");
  1173. sbi->s_mount_opt ^= EXT2_MOUNT_DAX;
  1174. }
  1175. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
  1176. spin_unlock(&sbi->s_lock);
  1177. return 0;
  1178. }
  1179. if (*flags & MS_RDONLY) {
  1180. if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
  1181. !(sbi->s_mount_state & EXT2_VALID_FS)) {
  1182. spin_unlock(&sbi->s_lock);
  1183. return 0;
  1184. }
  1185. /*
  1186. * OK, we are remounting a valid rw partition rdonly, so set
  1187. * the rdonly flag and then mark the partition as valid again.
  1188. */
  1189. es->s_state = cpu_to_le16(sbi->s_mount_state);
  1190. es->s_mtime = cpu_to_le32(get_seconds());
  1191. spin_unlock(&sbi->s_lock);
  1192. err = dquot_suspend(sb, -1);
  1193. if (err < 0) {
  1194. spin_lock(&sbi->s_lock);
  1195. goto restore_opts;
  1196. }
  1197. ext2_sync_super(sb, es, 1);
  1198. } else {
  1199. __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1200. ~EXT2_FEATURE_RO_COMPAT_SUPP);
  1201. if (ret) {
  1202. ext2_msg(sb, KERN_WARNING,
  1203. "warning: couldn't remount RDWR because of "
  1204. "unsupported optional features (%x).",
  1205. le32_to_cpu(ret));
  1206. err = -EROFS;
  1207. goto restore_opts;
  1208. }
  1209. /*
  1210. * Mounting a RDONLY partition read-write, so reread and
  1211. * store the current valid flag. (It may have been changed
  1212. * by e2fsck since we originally mounted the partition.)
  1213. */
  1214. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1215. if (!ext2_setup_super (sb, es, 0))
  1216. sb->s_flags &= ~MS_RDONLY;
  1217. spin_unlock(&sbi->s_lock);
  1218. ext2_write_super(sb);
  1219. dquot_resume(sb, -1);
  1220. }
  1221. return 0;
  1222. restore_opts:
  1223. sbi->s_mount_opt = old_opts.s_mount_opt;
  1224. sbi->s_resuid = old_opts.s_resuid;
  1225. sbi->s_resgid = old_opts.s_resgid;
  1226. sb->s_flags = old_sb_flags;
  1227. spin_unlock(&sbi->s_lock);
  1228. return err;
  1229. }
  1230. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
  1231. {
  1232. struct super_block *sb = dentry->d_sb;
  1233. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1234. struct ext2_super_block *es = sbi->s_es;
  1235. u64 fsid;
  1236. spin_lock(&sbi->s_lock);
  1237. if (test_opt (sb, MINIX_DF))
  1238. sbi->s_overhead_last = 0;
  1239. else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
  1240. unsigned long i, overhead = 0;
  1241. smp_rmb();
  1242. /*
  1243. * Compute the overhead (FS structures). This is constant
  1244. * for a given filesystem unless the number of block groups
  1245. * changes so we cache the previous value until it does.
  1246. */
  1247. /*
  1248. * All of the blocks before first_data_block are
  1249. * overhead
  1250. */
  1251. overhead = le32_to_cpu(es->s_first_data_block);
  1252. /*
  1253. * Add the overhead attributed to the superblock and
  1254. * block group descriptors. If the sparse superblocks
  1255. * feature is turned on, then not all groups have this.
  1256. */
  1257. for (i = 0; i < sbi->s_groups_count; i++)
  1258. overhead += ext2_bg_has_super(sb, i) +
  1259. ext2_bg_num_gdb(sb, i);
  1260. /*
  1261. * Every block group has an inode bitmap, a block
  1262. * bitmap, and an inode table.
  1263. */
  1264. overhead += (sbi->s_groups_count *
  1265. (2 + sbi->s_itb_per_group));
  1266. sbi->s_overhead_last = overhead;
  1267. smp_wmb();
  1268. sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
  1269. }
  1270. buf->f_type = EXT2_SUPER_MAGIC;
  1271. buf->f_bsize = sb->s_blocksize;
  1272. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
  1273. buf->f_bfree = ext2_count_free_blocks(sb);
  1274. es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
  1275. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  1276. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  1277. buf->f_bavail = 0;
  1278. buf->f_files = le32_to_cpu(es->s_inodes_count);
  1279. buf->f_ffree = ext2_count_free_inodes(sb);
  1280. es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
  1281. buf->f_namelen = EXT2_NAME_LEN;
  1282. fsid = le64_to_cpup((void *)es->s_uuid) ^
  1283. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  1284. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  1285. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  1286. spin_unlock(&sbi->s_lock);
  1287. return 0;
  1288. }
  1289. static struct dentry *ext2_mount(struct file_system_type *fs_type,
  1290. int flags, const char *dev_name, void *data)
  1291. {
  1292. return mount_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
  1293. }
  1294. #ifdef CONFIG_QUOTA
  1295. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  1296. * acquiring the locks... As quota files are never truncated and quota code
  1297. * itself serializes the operations (and no one else should touch the files)
  1298. * we don't have to be afraid of races */
  1299. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
  1300. size_t len, loff_t off)
  1301. {
  1302. struct inode *inode = sb_dqopt(sb)->files[type];
  1303. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1304. int err = 0;
  1305. int offset = off & (sb->s_blocksize - 1);
  1306. int tocopy;
  1307. size_t toread;
  1308. struct buffer_head tmp_bh;
  1309. struct buffer_head *bh;
  1310. loff_t i_size = i_size_read(inode);
  1311. if (off > i_size)
  1312. return 0;
  1313. if (off+len > i_size)
  1314. len = i_size-off;
  1315. toread = len;
  1316. while (toread > 0) {
  1317. tocopy = sb->s_blocksize - offset < toread ?
  1318. sb->s_blocksize - offset : toread;
  1319. tmp_bh.b_state = 0;
  1320. tmp_bh.b_size = sb->s_blocksize;
  1321. err = ext2_get_block(inode, blk, &tmp_bh, 0);
  1322. if (err < 0)
  1323. return err;
  1324. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  1325. memset(data, 0, tocopy);
  1326. else {
  1327. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1328. if (!bh)
  1329. return -EIO;
  1330. memcpy(data, bh->b_data+offset, tocopy);
  1331. brelse(bh);
  1332. }
  1333. offset = 0;
  1334. toread -= tocopy;
  1335. data += tocopy;
  1336. blk++;
  1337. }
  1338. return len;
  1339. }
  1340. /* Write to quotafile */
  1341. static ssize_t ext2_quota_write(struct super_block *sb, int type,
  1342. const char *data, size_t len, loff_t off)
  1343. {
  1344. struct inode *inode = sb_dqopt(sb)->files[type];
  1345. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1346. int err = 0;
  1347. int offset = off & (sb->s_blocksize - 1);
  1348. int tocopy;
  1349. size_t towrite = len;
  1350. struct buffer_head tmp_bh;
  1351. struct buffer_head *bh;
  1352. while (towrite > 0) {
  1353. tocopy = sb->s_blocksize - offset < towrite ?
  1354. sb->s_blocksize - offset : towrite;
  1355. tmp_bh.b_state = 0;
  1356. tmp_bh.b_size = sb->s_blocksize;
  1357. err = ext2_get_block(inode, blk, &tmp_bh, 1);
  1358. if (err < 0)
  1359. goto out;
  1360. if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
  1361. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1362. else
  1363. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  1364. if (unlikely(!bh)) {
  1365. err = -EIO;
  1366. goto out;
  1367. }
  1368. lock_buffer(bh);
  1369. memcpy(bh->b_data+offset, data, tocopy);
  1370. flush_dcache_page(bh->b_page);
  1371. set_buffer_uptodate(bh);
  1372. mark_buffer_dirty(bh);
  1373. unlock_buffer(bh);
  1374. brelse(bh);
  1375. offset = 0;
  1376. towrite -= tocopy;
  1377. data += tocopy;
  1378. blk++;
  1379. }
  1380. out:
  1381. if (len == towrite)
  1382. return err;
  1383. if (inode->i_size < off+len-towrite)
  1384. i_size_write(inode, off+len-towrite);
  1385. inode->i_version++;
  1386. inode->i_mtime = inode->i_ctime = current_time(inode);
  1387. mark_inode_dirty(inode);
  1388. return len - towrite;
  1389. }
  1390. #endif
  1391. static struct file_system_type ext2_fs_type = {
  1392. .owner = THIS_MODULE,
  1393. .name = "ext2",
  1394. .mount = ext2_mount,
  1395. .kill_sb = kill_block_super,
  1396. .fs_flags = FS_REQUIRES_DEV,
  1397. };
  1398. MODULE_ALIAS_FS("ext2");
  1399. static int __init init_ext2_fs(void)
  1400. {
  1401. int err;
  1402. err = init_inodecache();
  1403. if (err)
  1404. return err;
  1405. err = register_filesystem(&ext2_fs_type);
  1406. if (err)
  1407. goto out;
  1408. return 0;
  1409. out:
  1410. destroy_inodecache();
  1411. return err;
  1412. }
  1413. static void __exit exit_ext2_fs(void)
  1414. {
  1415. unregister_filesystem(&ext2_fs_type);
  1416. destroy_inodecache();
  1417. }
  1418. MODULE_AUTHOR("Remy Card and others");
  1419. MODULE_DESCRIPTION("Second Extended Filesystem");
  1420. MODULE_LICENSE("GPL");
  1421. module_init(init_ext2_fs)
  1422. module_exit(exit_ext2_fs)