super.c 33 KB

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