super.c 34 KB

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