super.c 39 KB

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