super.c 35 KB

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