super.c 34 KB

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