super.c 41 KB

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