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