super.c 40 KB

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