super.c 34 KB

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