super.c 34 KB

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