super.c 41 KB

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