super.c 32 KB

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