super.c 32 KB

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