super.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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/config.h>
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/parser.h>
  26. #include <linux/random.h>
  27. #include <linux/buffer_head.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 <asm/uaccess.h>
  33. #include "ext2.h"
  34. #include "xattr.h"
  35. #include "acl.h"
  36. #include "xip.h"
  37. static void ext2_sync_super(struct super_block *sb,
  38. struct ext2_super_block *es);
  39. static int ext2_remount (struct super_block * sb, int * flags, char * data);
  40. static int ext2_statfs (struct super_block * sb, struct kstatfs * buf);
  41. void ext2_error (struct super_block * sb, const char * function,
  42. const char * fmt, ...)
  43. {
  44. va_list args;
  45. struct ext2_sb_info *sbi = EXT2_SB(sb);
  46. struct ext2_super_block *es = sbi->s_es;
  47. if (!(sb->s_flags & MS_RDONLY)) {
  48. sbi->s_mount_state |= EXT2_ERROR_FS;
  49. es->s_state =
  50. cpu_to_le16(le16_to_cpu(es->s_state) | 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, __FUNCTION__,
  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 kmem_cache_t * 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, SLAB_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->vfs_inode.i_version = 1;
  133. return &ei->vfs_inode;
  134. }
  135. static void ext2_destroy_inode(struct inode *inode)
  136. {
  137. kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
  138. }
  139. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  140. {
  141. struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
  142. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  143. SLAB_CTOR_CONSTRUCTOR) {
  144. rwlock_init(&ei->i_meta_lock);
  145. #ifdef CONFIG_EXT2_FS_XATTR
  146. init_rwsem(&ei->xattr_sem);
  147. #endif
  148. inode_init_once(&ei->vfs_inode);
  149. }
  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. init_once, NULL);
  157. if (ext2_inode_cachep == NULL)
  158. return -ENOMEM;
  159. return 0;
  160. }
  161. static void destroy_inodecache(void)
  162. {
  163. if (kmem_cache_destroy(ext2_inode_cachep))
  164. printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
  165. }
  166. static void ext2_clear_inode(struct inode *inode)
  167. {
  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. }
  180. static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs)
  181. {
  182. struct ext2_sb_info *sbi = EXT2_SB(vfs->mnt_sb);
  183. if (sbi->s_mount_opt & EXT2_MOUNT_GRPID)
  184. seq_puts(seq, ",grpid");
  185. else
  186. seq_puts(seq, ",nogrpid");
  187. #if defined(CONFIG_QUOTA)
  188. if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
  189. seq_puts(seq, ",usrquota");
  190. if (sbi->s_mount_opt & EXT2_MOUNT_GRPQUOTA)
  191. seq_puts(seq, ",grpquota");
  192. #endif
  193. return 0;
  194. }
  195. #ifdef CONFIG_QUOTA
  196. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
  197. static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
  198. #endif
  199. static struct super_operations ext2_sops = {
  200. .alloc_inode = ext2_alloc_inode,
  201. .destroy_inode = ext2_destroy_inode,
  202. .read_inode = ext2_read_inode,
  203. .write_inode = ext2_write_inode,
  204. .put_inode = ext2_put_inode,
  205. .delete_inode = ext2_delete_inode,
  206. .put_super = ext2_put_super,
  207. .write_super = ext2_write_super,
  208. .statfs = ext2_statfs,
  209. .remount_fs = ext2_remount,
  210. .clear_inode = ext2_clear_inode,
  211. .show_options = ext2_show_options,
  212. #ifdef CONFIG_QUOTA
  213. .quota_read = ext2_quota_read,
  214. .quota_write = ext2_quota_write,
  215. #endif
  216. };
  217. /* Yes, most of these are left as NULL!!
  218. * A NULL value implies the default, which works with ext2-like file
  219. * systems, but can be improved upon.
  220. * Currently only get_parent is required.
  221. */
  222. struct dentry *ext2_get_parent(struct dentry *child);
  223. static struct export_operations ext2_export_ops = {
  224. .get_parent = ext2_get_parent,
  225. };
  226. static unsigned long get_sb_block(void **data)
  227. {
  228. unsigned long sb_block;
  229. char *options = (char *) *data;
  230. if (!options || strncmp(options, "sb=", 3) != 0)
  231. return 1; /* Default location */
  232. options += 3;
  233. sb_block = simple_strtoul(options, &options, 0);
  234. if (*options && *options != ',') {
  235. printk("EXT2-fs: Invalid sb specification: %s\n",
  236. (char *) *data);
  237. return 1;
  238. }
  239. if (*options == ',')
  240. options++;
  241. *data = (void *) options;
  242. return sb_block;
  243. }
  244. enum {
  245. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  246. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
  247. Opt_err_ro, Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug,
  248. Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
  249. Opt_acl, Opt_noacl, Opt_xip, Opt_ignore, Opt_err, Opt_quota,
  250. Opt_usrquota, Opt_grpquota
  251. };
  252. static match_table_t tokens = {
  253. {Opt_bsd_df, "bsddf"},
  254. {Opt_minix_df, "minixdf"},
  255. {Opt_grpid, "grpid"},
  256. {Opt_grpid, "bsdgroups"},
  257. {Opt_nogrpid, "nogrpid"},
  258. {Opt_nogrpid, "sysvgroups"},
  259. {Opt_resgid, "resgid=%u"},
  260. {Opt_resuid, "resuid=%u"},
  261. {Opt_sb, "sb=%u"},
  262. {Opt_err_cont, "errors=continue"},
  263. {Opt_err_panic, "errors=panic"},
  264. {Opt_err_ro, "errors=remount-ro"},
  265. {Opt_nouid32, "nouid32"},
  266. {Opt_nocheck, "check=none"},
  267. {Opt_nocheck, "nocheck"},
  268. {Opt_check, "check"},
  269. {Opt_debug, "debug"},
  270. {Opt_oldalloc, "oldalloc"},
  271. {Opt_orlov, "orlov"},
  272. {Opt_nobh, "nobh"},
  273. {Opt_user_xattr, "user_xattr"},
  274. {Opt_nouser_xattr, "nouser_xattr"},
  275. {Opt_acl, "acl"},
  276. {Opt_noacl, "noacl"},
  277. {Opt_xip, "xip"},
  278. {Opt_grpquota, "grpquota"},
  279. {Opt_ignore, "noquota"},
  280. {Opt_quota, "quota"},
  281. {Opt_usrquota, "usrquota"},
  282. {Opt_err, NULL}
  283. };
  284. static int parse_options (char * options,
  285. struct ext2_sb_info *sbi)
  286. {
  287. char * p;
  288. substring_t args[MAX_OPT_ARGS];
  289. unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
  290. int option;
  291. if (!options)
  292. return 1;
  293. while ((p = strsep (&options, ",")) != NULL) {
  294. int token;
  295. if (!*p)
  296. continue;
  297. token = match_token(p, tokens, args);
  298. switch (token) {
  299. case Opt_bsd_df:
  300. clear_opt (sbi->s_mount_opt, MINIX_DF);
  301. break;
  302. case Opt_minix_df:
  303. set_opt (sbi->s_mount_opt, MINIX_DF);
  304. break;
  305. case Opt_grpid:
  306. set_opt (sbi->s_mount_opt, GRPID);
  307. break;
  308. case Opt_nogrpid:
  309. clear_opt (sbi->s_mount_opt, GRPID);
  310. break;
  311. case Opt_resuid:
  312. if (match_int(&args[0], &option))
  313. return 0;
  314. sbi->s_resuid = option;
  315. break;
  316. case Opt_resgid:
  317. if (match_int(&args[0], &option))
  318. return 0;
  319. sbi->s_resgid = option;
  320. break;
  321. case Opt_sb:
  322. /* handled by get_sb_block() instead of here */
  323. /* *sb_block = match_int(&args[0]); */
  324. break;
  325. case Opt_err_panic:
  326. kind = EXT2_MOUNT_ERRORS_PANIC;
  327. break;
  328. case Opt_err_ro:
  329. kind = EXT2_MOUNT_ERRORS_RO;
  330. break;
  331. case Opt_err_cont:
  332. kind = EXT2_MOUNT_ERRORS_CONT;
  333. break;
  334. case Opt_nouid32:
  335. set_opt (sbi->s_mount_opt, NO_UID32);
  336. break;
  337. case Opt_check:
  338. #ifdef CONFIG_EXT2_CHECK
  339. set_opt (sbi->s_mount_opt, CHECK);
  340. #else
  341. printk("EXT2 Check option not supported\n");
  342. #endif
  343. break;
  344. case Opt_nocheck:
  345. clear_opt (sbi->s_mount_opt, CHECK);
  346. break;
  347. case Opt_debug:
  348. set_opt (sbi->s_mount_opt, DEBUG);
  349. break;
  350. case Opt_oldalloc:
  351. set_opt (sbi->s_mount_opt, OLDALLOC);
  352. break;
  353. case Opt_orlov:
  354. clear_opt (sbi->s_mount_opt, OLDALLOC);
  355. break;
  356. case Opt_nobh:
  357. set_opt (sbi->s_mount_opt, NOBH);
  358. break;
  359. #ifdef CONFIG_EXT2_FS_XATTR
  360. case Opt_user_xattr:
  361. set_opt (sbi->s_mount_opt, XATTR_USER);
  362. break;
  363. case Opt_nouser_xattr:
  364. clear_opt (sbi->s_mount_opt, XATTR_USER);
  365. break;
  366. #else
  367. case Opt_user_xattr:
  368. case Opt_nouser_xattr:
  369. printk("EXT2 (no)user_xattr options not supported\n");
  370. break;
  371. #endif
  372. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  373. case Opt_acl:
  374. set_opt(sbi->s_mount_opt, POSIX_ACL);
  375. break;
  376. case Opt_noacl:
  377. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  378. break;
  379. #else
  380. case Opt_acl:
  381. case Opt_noacl:
  382. printk("EXT2 (no)acl options not supported\n");
  383. break;
  384. #endif
  385. case Opt_xip:
  386. #ifdef CONFIG_EXT2_FS_XIP
  387. set_opt (sbi->s_mount_opt, XIP);
  388. #else
  389. printk("EXT2 xip option not supported\n");
  390. #endif
  391. break;
  392. #if defined(CONFIG_QUOTA)
  393. case Opt_quota:
  394. case Opt_usrquota:
  395. set_opt(sbi->s_mount_opt, USRQUOTA);
  396. break;
  397. case Opt_grpquota:
  398. set_opt(sbi->s_mount_opt, GRPQUOTA);
  399. break;
  400. #else
  401. case Opt_quota:
  402. case Opt_usrquota:
  403. case Opt_grpquota:
  404. printk(KERN_ERR
  405. "EXT2-fs: quota operations not supported.\n");
  406. break;
  407. #endif
  408. case Opt_ignore:
  409. break;
  410. default:
  411. return 0;
  412. }
  413. }
  414. sbi->s_mount_opt |= kind;
  415. return 1;
  416. }
  417. static int ext2_setup_super (struct super_block * sb,
  418. struct ext2_super_block * es,
  419. int read_only)
  420. {
  421. int res = 0;
  422. struct ext2_sb_info *sbi = EXT2_SB(sb);
  423. if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
  424. printk ("EXT2-fs warning: revision level too high, "
  425. "forcing read-only mode\n");
  426. res = MS_RDONLY;
  427. }
  428. if (read_only)
  429. return res;
  430. if (!(sbi->s_mount_state & EXT2_VALID_FS))
  431. printk ("EXT2-fs warning: mounting unchecked fs, "
  432. "running e2fsck is recommended\n");
  433. else if ((sbi->s_mount_state & EXT2_ERROR_FS))
  434. printk ("EXT2-fs warning: mounting fs with errors, "
  435. "running e2fsck is recommended\n");
  436. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  437. le16_to_cpu(es->s_mnt_count) >=
  438. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  439. printk ("EXT2-fs warning: maximal mount count reached, "
  440. "running e2fsck is recommended\n");
  441. else if (le32_to_cpu(es->s_checkinterval) &&
  442. (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  443. printk ("EXT2-fs warning: checktime reached, "
  444. "running e2fsck is recommended\n");
  445. if (!le16_to_cpu(es->s_max_mnt_count))
  446. es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
  447. es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
  448. ext2_write_super(sb);
  449. if (test_opt (sb, DEBUG))
  450. printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
  451. "bpg=%lu, ipg=%lu, mo=%04lx]\n",
  452. EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
  453. sbi->s_frag_size,
  454. sbi->s_groups_count,
  455. EXT2_BLOCKS_PER_GROUP(sb),
  456. EXT2_INODES_PER_GROUP(sb),
  457. sbi->s_mount_opt);
  458. #ifdef CONFIG_EXT2_CHECK
  459. if (test_opt (sb, CHECK)) {
  460. ext2_check_blocks_bitmap (sb);
  461. ext2_check_inodes_bitmap (sb);
  462. }
  463. #endif
  464. return res;
  465. }
  466. static int ext2_check_descriptors (struct super_block * sb)
  467. {
  468. int i;
  469. int desc_block = 0;
  470. struct ext2_sb_info *sbi = EXT2_SB(sb);
  471. unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
  472. struct ext2_group_desc * gdp = NULL;
  473. ext2_debug ("Checking group descriptors");
  474. for (i = 0; i < sbi->s_groups_count; i++)
  475. {
  476. if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
  477. gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
  478. if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
  479. le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
  480. {
  481. ext2_error (sb, "ext2_check_descriptors",
  482. "Block bitmap for group %d"
  483. " not in group (block %lu)!",
  484. i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
  485. return 0;
  486. }
  487. if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
  488. le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
  489. {
  490. ext2_error (sb, "ext2_check_descriptors",
  491. "Inode bitmap for group %d"
  492. " not in group (block %lu)!",
  493. i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
  494. return 0;
  495. }
  496. if (le32_to_cpu(gdp->bg_inode_table) < block ||
  497. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
  498. block + EXT2_BLOCKS_PER_GROUP(sb))
  499. {
  500. ext2_error (sb, "ext2_check_descriptors",
  501. "Inode table for group %d"
  502. " not in group (block %lu)!",
  503. i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
  504. return 0;
  505. }
  506. block += EXT2_BLOCKS_PER_GROUP(sb);
  507. gdp++;
  508. }
  509. return 1;
  510. }
  511. #define log2(n) ffz(~(n))
  512. /*
  513. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  514. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  515. * We need to be 1 filesystem block less than the 2^32 sector limit.
  516. */
  517. static loff_t ext2_max_size(int bits)
  518. {
  519. loff_t res = EXT2_NDIR_BLOCKS;
  520. /* This constant is calculated to be the largest file size for a
  521. * dense, 4k-blocksize file such that the total number of
  522. * sectors in the file, including data and all indirect blocks,
  523. * does not exceed 2^32. */
  524. const loff_t upper_limit = 0x1ff7fffd000LL;
  525. res += 1LL << (bits-2);
  526. res += 1LL << (2*(bits-2));
  527. res += 1LL << (3*(bits-2));
  528. res <<= bits;
  529. if (res > upper_limit)
  530. res = upper_limit;
  531. return res;
  532. }
  533. static unsigned long descriptor_loc(struct super_block *sb,
  534. unsigned long logic_sb_block,
  535. int nr)
  536. {
  537. struct ext2_sb_info *sbi = EXT2_SB(sb);
  538. unsigned long bg, first_data_block, first_meta_bg;
  539. int has_super = 0;
  540. first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  541. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  542. if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
  543. nr < first_meta_bg)
  544. return (logic_sb_block + nr + 1);
  545. bg = sbi->s_desc_per_block * nr;
  546. if (ext2_bg_has_super(sb, bg))
  547. has_super = 1;
  548. return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
  549. }
  550. static int ext2_fill_super(struct super_block *sb, void *data, int silent)
  551. {
  552. struct buffer_head * bh;
  553. struct ext2_sb_info * sbi;
  554. struct ext2_super_block * es;
  555. struct inode *root;
  556. unsigned long block;
  557. unsigned long sb_block = get_sb_block(&data);
  558. unsigned long logic_sb_block;
  559. unsigned long offset = 0;
  560. unsigned long def_mount_opts;
  561. int blocksize = BLOCK_SIZE;
  562. int db_count;
  563. int i, j;
  564. __le32 features;
  565. sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
  566. if (!sbi)
  567. return -ENOMEM;
  568. sb->s_fs_info = sbi;
  569. memset(sbi, 0, sizeof(*sbi));
  570. /*
  571. * See what the current blocksize for the device is, and
  572. * use that as the blocksize. Otherwise (or if the blocksize
  573. * is smaller than the default) use the default.
  574. * This is important for devices that have a hardware
  575. * sectorsize that is larger than the default.
  576. */
  577. blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
  578. if (!blocksize) {
  579. printk ("EXT2-fs: unable to set blocksize\n");
  580. goto failed_sbi;
  581. }
  582. /*
  583. * If the superblock doesn't start on a hardware sector boundary,
  584. * calculate the offset.
  585. */
  586. if (blocksize != BLOCK_SIZE) {
  587. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  588. offset = (sb_block*BLOCK_SIZE) % blocksize;
  589. } else {
  590. logic_sb_block = sb_block;
  591. }
  592. if (!(bh = sb_bread(sb, logic_sb_block))) {
  593. printk ("EXT2-fs: unable to read superblock\n");
  594. goto failed_sbi;
  595. }
  596. /*
  597. * Note: s_es must be initialized as soon as possible because
  598. * some ext2 macro-instructions depend on its value
  599. */
  600. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  601. sbi->s_es = es;
  602. sb->s_magic = le16_to_cpu(es->s_magic);
  603. if (sb->s_magic != EXT2_SUPER_MAGIC)
  604. goto cantfind_ext2;
  605. /* Set defaults before we parse the mount options */
  606. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  607. if (def_mount_opts & EXT2_DEFM_DEBUG)
  608. set_opt(sbi->s_mount_opt, DEBUG);
  609. if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
  610. set_opt(sbi->s_mount_opt, GRPID);
  611. if (def_mount_opts & EXT2_DEFM_UID16)
  612. set_opt(sbi->s_mount_opt, NO_UID32);
  613. if (def_mount_opts & EXT2_DEFM_XATTR_USER)
  614. set_opt(sbi->s_mount_opt, XATTR_USER);
  615. if (def_mount_opts & EXT2_DEFM_ACL)
  616. set_opt(sbi->s_mount_opt, POSIX_ACL);
  617. if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
  618. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  619. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
  620. set_opt(sbi->s_mount_opt, ERRORS_RO);
  621. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  622. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  623. if (!parse_options ((char *) data, sbi))
  624. goto failed_mount;
  625. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  626. ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
  627. MS_POSIXACL : 0);
  628. ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
  629. EXT2_MOUNT_XIP if not */
  630. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
  631. (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
  632. EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  633. EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  634. printk("EXT2-fs warning: feature flags set on rev 0 fs, "
  635. "running e2fsck is recommended\n");
  636. /*
  637. * Check feature flags regardless of the revision level, since we
  638. * previously didn't change the revision level when setting the flags,
  639. * so there is a chance incompat flags are set on a rev 0 filesystem.
  640. */
  641. features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
  642. if (features) {
  643. printk("EXT2-fs: %s: couldn't mount because of "
  644. "unsupported optional features (%x).\n",
  645. sb->s_id, le32_to_cpu(features));
  646. goto failed_mount;
  647. }
  648. if (!(sb->s_flags & MS_RDONLY) &&
  649. (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
  650. printk("EXT2-fs: %s: couldn't mount RDWR because of "
  651. "unsupported optional features (%x).\n",
  652. sb->s_id, le32_to_cpu(features));
  653. goto failed_mount;
  654. }
  655. blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
  656. if ((ext2_use_xip(sb)) && ((blocksize != PAGE_SIZE) ||
  657. (sb->s_blocksize != blocksize))) {
  658. if (!silent)
  659. printk("XIP: Unsupported blocksize\n");
  660. goto failed_mount;
  661. }
  662. /* If the blocksize doesn't match, re-read the thing.. */
  663. if (sb->s_blocksize != blocksize) {
  664. brelse(bh);
  665. if (!sb_set_blocksize(sb, blocksize)) {
  666. printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
  667. goto failed_sbi;
  668. }
  669. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  670. offset = (sb_block*BLOCK_SIZE) % blocksize;
  671. bh = sb_bread(sb, logic_sb_block);
  672. if(!bh) {
  673. printk("EXT2-fs: Couldn't read superblock on "
  674. "2nd try.\n");
  675. goto failed_sbi;
  676. }
  677. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  678. sbi->s_es = es;
  679. if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
  680. printk ("EXT2-fs: Magic mismatch, very weird !\n");
  681. goto failed_mount;
  682. }
  683. }
  684. sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
  685. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
  686. sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
  687. sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
  688. } else {
  689. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  690. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  691. if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
  692. (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
  693. (sbi->s_inode_size > blocksize)) {
  694. printk ("EXT2-fs: unsupported inode size: %d\n",
  695. sbi->s_inode_size);
  696. goto failed_mount;
  697. }
  698. }
  699. sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
  700. le32_to_cpu(es->s_log_frag_size);
  701. if (sbi->s_frag_size == 0)
  702. goto cantfind_ext2;
  703. sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
  704. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  705. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  706. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  707. if (EXT2_INODE_SIZE(sb) == 0)
  708. goto cantfind_ext2;
  709. sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
  710. if (sbi->s_inodes_per_block == 0)
  711. goto cantfind_ext2;
  712. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  713. sbi->s_inodes_per_block;
  714. sbi->s_desc_per_block = sb->s_blocksize /
  715. sizeof (struct ext2_group_desc);
  716. sbi->s_sbh = bh;
  717. sbi->s_mount_state = le16_to_cpu(es->s_state);
  718. sbi->s_addr_per_block_bits =
  719. log2 (EXT2_ADDR_PER_BLOCK(sb));
  720. sbi->s_desc_per_block_bits =
  721. log2 (EXT2_DESC_PER_BLOCK(sb));
  722. if (sb->s_magic != EXT2_SUPER_MAGIC)
  723. goto cantfind_ext2;
  724. if (sb->s_blocksize != bh->b_size) {
  725. if (!silent)
  726. printk ("VFS: Unsupported blocksize on dev "
  727. "%s.\n", sb->s_id);
  728. goto failed_mount;
  729. }
  730. if (sb->s_blocksize != sbi->s_frag_size) {
  731. printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
  732. sbi->s_frag_size, sb->s_blocksize);
  733. goto failed_mount;
  734. }
  735. if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
  736. printk ("EXT2-fs: #blocks per group too big: %lu\n",
  737. sbi->s_blocks_per_group);
  738. goto failed_mount;
  739. }
  740. if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
  741. printk ("EXT2-fs: #fragments per group too big: %lu\n",
  742. sbi->s_frags_per_group);
  743. goto failed_mount;
  744. }
  745. if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
  746. printk ("EXT2-fs: #inodes per group too big: %lu\n",
  747. sbi->s_inodes_per_group);
  748. goto failed_mount;
  749. }
  750. if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
  751. goto cantfind_ext2;
  752. sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
  753. le32_to_cpu(es->s_first_data_block) +
  754. EXT2_BLOCKS_PER_GROUP(sb) - 1) /
  755. EXT2_BLOCKS_PER_GROUP(sb);
  756. db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
  757. EXT2_DESC_PER_BLOCK(sb);
  758. sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
  759. if (sbi->s_group_desc == NULL) {
  760. printk ("EXT2-fs: not enough memory\n");
  761. goto failed_mount;
  762. }
  763. percpu_counter_init(&sbi->s_freeblocks_counter);
  764. percpu_counter_init(&sbi->s_freeinodes_counter);
  765. percpu_counter_init(&sbi->s_dirs_counter);
  766. bgl_lock_init(&sbi->s_blockgroup_lock);
  767. sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
  768. GFP_KERNEL);
  769. if (!sbi->s_debts) {
  770. printk ("EXT2-fs: not enough memory\n");
  771. goto failed_mount_group_desc;
  772. }
  773. memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
  774. for (i = 0; i < db_count; i++) {
  775. block = descriptor_loc(sb, logic_sb_block, i);
  776. sbi->s_group_desc[i] = sb_bread(sb, block);
  777. if (!sbi->s_group_desc[i]) {
  778. for (j = 0; j < i; j++)
  779. brelse (sbi->s_group_desc[j]);
  780. printk ("EXT2-fs: unable to read group descriptors\n");
  781. goto failed_mount_group_desc;
  782. }
  783. }
  784. if (!ext2_check_descriptors (sb)) {
  785. printk ("EXT2-fs: group descriptors corrupted!\n");
  786. db_count = i;
  787. goto failed_mount2;
  788. }
  789. sbi->s_gdb_count = db_count;
  790. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  791. spin_lock_init(&sbi->s_next_gen_lock);
  792. /*
  793. * set up enough so that it can read an inode
  794. */
  795. sb->s_op = &ext2_sops;
  796. sb->s_export_op = &ext2_export_ops;
  797. sb->s_xattr = ext2_xattr_handlers;
  798. root = iget(sb, EXT2_ROOT_INO);
  799. sb->s_root = d_alloc_root(root);
  800. if (!sb->s_root) {
  801. iput(root);
  802. printk(KERN_ERR "EXT2-fs: get root inode failed\n");
  803. goto failed_mount2;
  804. }
  805. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  806. dput(sb->s_root);
  807. sb->s_root = NULL;
  808. printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
  809. goto failed_mount2;
  810. }
  811. if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
  812. ext2_warning(sb, __FUNCTION__,
  813. "mounting ext3 filesystem as ext2\n");
  814. ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
  815. percpu_counter_mod(&sbi->s_freeblocks_counter,
  816. ext2_count_free_blocks(sb));
  817. percpu_counter_mod(&sbi->s_freeinodes_counter,
  818. ext2_count_free_inodes(sb));
  819. percpu_counter_mod(&sbi->s_dirs_counter,
  820. ext2_count_dirs(sb));
  821. return 0;
  822. cantfind_ext2:
  823. if (!silent)
  824. printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
  825. sb->s_id);
  826. goto failed_mount;
  827. failed_mount2:
  828. for (i = 0; i < db_count; i++)
  829. brelse(sbi->s_group_desc[i]);
  830. failed_mount_group_desc:
  831. kfree(sbi->s_group_desc);
  832. kfree(sbi->s_debts);
  833. failed_mount:
  834. brelse(bh);
  835. failed_sbi:
  836. sb->s_fs_info = NULL;
  837. kfree(sbi);
  838. return -EINVAL;
  839. }
  840. static void ext2_commit_super (struct super_block * sb,
  841. struct ext2_super_block * es)
  842. {
  843. es->s_wtime = cpu_to_le32(get_seconds());
  844. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  845. sb->s_dirt = 0;
  846. }
  847. static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
  848. {
  849. es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
  850. es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
  851. es->s_wtime = cpu_to_le32(get_seconds());
  852. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  853. sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
  854. sb->s_dirt = 0;
  855. }
  856. /*
  857. * In the second extended file system, it is not necessary to
  858. * write the super block since we use a mapping of the
  859. * disk super block in a buffer.
  860. *
  861. * However, this function is still used to set the fs valid
  862. * flags to 0. We need to set this flag to 0 since the fs
  863. * may have been checked while mounted and e2fsck may have
  864. * set s_state to EXT2_VALID_FS after some corrections.
  865. */
  866. void ext2_write_super (struct super_block * sb)
  867. {
  868. struct ext2_super_block * es;
  869. lock_kernel();
  870. if (!(sb->s_flags & MS_RDONLY)) {
  871. es = EXT2_SB(sb)->s_es;
  872. if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
  873. ext2_debug ("setting valid to 0\n");
  874. es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
  875. ~EXT2_VALID_FS);
  876. es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
  877. es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
  878. es->s_mtime = cpu_to_le32(get_seconds());
  879. ext2_sync_super(sb, es);
  880. } else
  881. ext2_commit_super (sb, es);
  882. }
  883. sb->s_dirt = 0;
  884. unlock_kernel();
  885. }
  886. static int ext2_remount (struct super_block * sb, int * flags, char * data)
  887. {
  888. struct ext2_sb_info * sbi = EXT2_SB(sb);
  889. struct ext2_super_block * es;
  890. unsigned long old_mount_opt = sbi->s_mount_opt;
  891. struct ext2_mount_options old_opts;
  892. unsigned long old_sb_flags;
  893. int err;
  894. /* Store the old options */
  895. old_sb_flags = sb->s_flags;
  896. old_opts.s_mount_opt = sbi->s_mount_opt;
  897. old_opts.s_resuid = sbi->s_resuid;
  898. old_opts.s_resgid = sbi->s_resgid;
  899. /*
  900. * Allow the "check" option to be passed as a remount option.
  901. */
  902. if (!parse_options (data, sbi)) {
  903. err = -EINVAL;
  904. goto restore_opts;
  905. }
  906. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  907. ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  908. es = sbi->s_es;
  909. if (((sbi->s_mount_opt & EXT2_MOUNT_XIP) !=
  910. (old_mount_opt & EXT2_MOUNT_XIP)) &&
  911. invalidate_inodes(sb))
  912. ext2_warning(sb, __FUNCTION__, "busy inodes while remounting "\
  913. "xip remain in cache (no functional problem)");
  914. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  915. return 0;
  916. if (*flags & MS_RDONLY) {
  917. if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
  918. !(sbi->s_mount_state & EXT2_VALID_FS))
  919. return 0;
  920. /*
  921. * OK, we are remounting a valid rw partition rdonly, so set
  922. * the rdonly flag and then mark the partition as valid again.
  923. */
  924. es->s_state = cpu_to_le16(sbi->s_mount_state);
  925. es->s_mtime = cpu_to_le32(get_seconds());
  926. } else {
  927. __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
  928. ~EXT2_FEATURE_RO_COMPAT_SUPP);
  929. if (ret) {
  930. printk("EXT2-fs: %s: couldn't remount RDWR because of "
  931. "unsupported optional features (%x).\n",
  932. sb->s_id, le32_to_cpu(ret));
  933. err = -EROFS;
  934. goto restore_opts;
  935. }
  936. /*
  937. * Mounting a RDONLY partition read-write, so reread and
  938. * store the current valid flag. (It may have been changed
  939. * by e2fsck since we originally mounted the partition.)
  940. */
  941. sbi->s_mount_state = le16_to_cpu(es->s_state);
  942. if (!ext2_setup_super (sb, es, 0))
  943. sb->s_flags &= ~MS_RDONLY;
  944. }
  945. ext2_sync_super(sb, es);
  946. return 0;
  947. restore_opts:
  948. sbi->s_mount_opt = old_opts.s_mount_opt;
  949. sbi->s_resuid = old_opts.s_resuid;
  950. sbi->s_resgid = old_opts.s_resgid;
  951. sb->s_flags = old_sb_flags;
  952. return err;
  953. }
  954. static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
  955. {
  956. struct ext2_sb_info *sbi = EXT2_SB(sb);
  957. unsigned long overhead;
  958. int i;
  959. if (test_opt (sb, MINIX_DF))
  960. overhead = 0;
  961. else {
  962. /*
  963. * Compute the overhead (FS structures)
  964. */
  965. /*
  966. * All of the blocks before first_data_block are
  967. * overhead
  968. */
  969. overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
  970. /*
  971. * Add the overhead attributed to the superblock and
  972. * block group descriptors. If the sparse superblocks
  973. * feature is turned on, then not all groups have this.
  974. */
  975. for (i = 0; i < sbi->s_groups_count; i++)
  976. overhead += ext2_bg_has_super(sb, i) +
  977. ext2_bg_num_gdb(sb, i);
  978. /*
  979. * Every block group has an inode bitmap, a block
  980. * bitmap, and an inode table.
  981. */
  982. overhead += (sbi->s_groups_count *
  983. (2 + sbi->s_itb_per_group));
  984. }
  985. buf->f_type = EXT2_SUPER_MAGIC;
  986. buf->f_bsize = sb->s_blocksize;
  987. buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
  988. buf->f_bfree = ext2_count_free_blocks(sb);
  989. buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
  990. if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
  991. buf->f_bavail = 0;
  992. buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
  993. buf->f_ffree = ext2_count_free_inodes (sb);
  994. buf->f_namelen = EXT2_NAME_LEN;
  995. return 0;
  996. }
  997. static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
  998. int flags, const char *dev_name, void *data)
  999. {
  1000. return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
  1001. }
  1002. #ifdef CONFIG_QUOTA
  1003. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  1004. * acquiring the locks... As quota files are never truncated and quota code
  1005. * itself serializes the operations (and noone else should touch the files)
  1006. * we don't have to be afraid of races */
  1007. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
  1008. size_t len, loff_t off)
  1009. {
  1010. struct inode *inode = sb_dqopt(sb)->files[type];
  1011. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1012. int err = 0;
  1013. int offset = off & (sb->s_blocksize - 1);
  1014. int tocopy;
  1015. size_t toread;
  1016. struct buffer_head tmp_bh;
  1017. struct buffer_head *bh;
  1018. loff_t i_size = i_size_read(inode);
  1019. if (off > i_size)
  1020. return 0;
  1021. if (off+len > i_size)
  1022. len = i_size-off;
  1023. toread = len;
  1024. while (toread > 0) {
  1025. tocopy = sb->s_blocksize - offset < toread ?
  1026. sb->s_blocksize - offset : toread;
  1027. tmp_bh.b_state = 0;
  1028. err = ext2_get_block(inode, blk, &tmp_bh, 0);
  1029. if (err)
  1030. return err;
  1031. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  1032. memset(data, 0, tocopy);
  1033. else {
  1034. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1035. if (!bh)
  1036. return -EIO;
  1037. memcpy(data, bh->b_data+offset, tocopy);
  1038. brelse(bh);
  1039. }
  1040. offset = 0;
  1041. toread -= tocopy;
  1042. data += tocopy;
  1043. blk++;
  1044. }
  1045. return len;
  1046. }
  1047. /* Write to quotafile */
  1048. static ssize_t ext2_quota_write(struct super_block *sb, int type,
  1049. const char *data, size_t len, loff_t off)
  1050. {
  1051. struct inode *inode = sb_dqopt(sb)->files[type];
  1052. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1053. int err = 0;
  1054. int offset = off & (sb->s_blocksize - 1);
  1055. int tocopy;
  1056. size_t towrite = len;
  1057. struct buffer_head tmp_bh;
  1058. struct buffer_head *bh;
  1059. down(&inode->i_sem);
  1060. while (towrite > 0) {
  1061. tocopy = sb->s_blocksize - offset < towrite ?
  1062. sb->s_blocksize - offset : towrite;
  1063. tmp_bh.b_state = 0;
  1064. err = ext2_get_block(inode, blk, &tmp_bh, 1);
  1065. if (err)
  1066. goto out;
  1067. if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
  1068. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1069. else
  1070. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  1071. if (!bh) {
  1072. err = -EIO;
  1073. goto out;
  1074. }
  1075. lock_buffer(bh);
  1076. memcpy(bh->b_data+offset, data, tocopy);
  1077. flush_dcache_page(bh->b_page);
  1078. set_buffer_uptodate(bh);
  1079. mark_buffer_dirty(bh);
  1080. unlock_buffer(bh);
  1081. brelse(bh);
  1082. offset = 0;
  1083. towrite -= tocopy;
  1084. data += tocopy;
  1085. blk++;
  1086. }
  1087. out:
  1088. if (len == towrite)
  1089. return err;
  1090. if (inode->i_size < off+len-towrite)
  1091. i_size_write(inode, off+len-towrite);
  1092. inode->i_version++;
  1093. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1094. mark_inode_dirty(inode);
  1095. up(&inode->i_sem);
  1096. return len - towrite;
  1097. }
  1098. #endif
  1099. static struct file_system_type ext2_fs_type = {
  1100. .owner = THIS_MODULE,
  1101. .name = "ext2",
  1102. .get_sb = ext2_get_sb,
  1103. .kill_sb = kill_block_super,
  1104. .fs_flags = FS_REQUIRES_DEV,
  1105. };
  1106. static int __init init_ext2_fs(void)
  1107. {
  1108. int err = init_ext2_xattr();
  1109. if (err)
  1110. return err;
  1111. err = init_inodecache();
  1112. if (err)
  1113. goto out1;
  1114. err = register_filesystem(&ext2_fs_type);
  1115. if (err)
  1116. goto out;
  1117. return 0;
  1118. out:
  1119. destroy_inodecache();
  1120. out1:
  1121. exit_ext2_xattr();
  1122. return err;
  1123. }
  1124. static void __exit exit_ext2_fs(void)
  1125. {
  1126. unregister_filesystem(&ext2_fs_type);
  1127. destroy_inodecache();
  1128. exit_ext2_xattr();
  1129. }
  1130. module_init(init_ext2_fs)
  1131. module_exit(exit_ext2_fs)