super.c 41 KB

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