super.c 39 KB

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