super.c 42 KB

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