super.c 35 KB

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