super.c 42 KB

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