super.c 39 KB

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