super.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/ext2/super.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/fs/minix/inode.c
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * Big-endian to little-endian byte-swapping/bitmaps by
  17. * David S. Miller (davem@caip.rutgers.edu), 1995
  18. */
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include <linux/fs.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/fs_context.h>
  26. #include <linux/fs_parser.h>
  27. #include <linux/random.h>
  28. #include <linux/buffer_head.h>
  29. #include <linux/exportfs.h>
  30. #include <linux/vfs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/mount.h>
  33. #include <linux/log2.h>
  34. #include <linux/quotaops.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/dax.h>
  37. #include <linux/iversion.h>
  38. #include "ext2.h"
  39. #include "xattr.h"
  40. #include "acl.h"
  41. static void ext2_write_super(struct super_block *sb);
  42. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
  43. static int ext2_sync_fs(struct super_block *sb, int wait);
  44. static int ext2_freeze(struct super_block *sb);
  45. static int ext2_unfreeze(struct super_block *sb);
  46. void ext2_error(struct super_block *sb, const char *function,
  47. const char *fmt, ...)
  48. {
  49. struct va_format vaf;
  50. va_list args;
  51. struct ext2_sb_info *sbi = EXT2_SB(sb);
  52. struct ext2_super_block *es = sbi->s_es;
  53. if (!sb_rdonly(sb)) {
  54. spin_lock(&sbi->s_lock);
  55. sbi->s_mount_state |= EXT2_ERROR_FS;
  56. es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
  57. spin_unlock(&sbi->s_lock);
  58. ext2_sync_super(sb, es, 1);
  59. }
  60. va_start(args, fmt);
  61. vaf.fmt = fmt;
  62. vaf.va = &args;
  63. printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
  64. sb->s_id, function, &vaf);
  65. va_end(args);
  66. if (test_opt(sb, ERRORS_PANIC))
  67. panic("EXT2-fs: panic from previous error\n");
  68. if (!sb_rdonly(sb) && test_opt(sb, ERRORS_RO)) {
  69. ext2_msg(sb, KERN_CRIT,
  70. "error: remounting filesystem read-only");
  71. sb->s_flags |= SB_RDONLY;
  72. }
  73. }
  74. static void ext2_msg_fc(struct fs_context *fc, const char *prefix,
  75. const char *fmt, ...)
  76. {
  77. struct va_format vaf;
  78. va_list args;
  79. const char *s_id;
  80. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  81. s_id = fc->root->d_sb->s_id;
  82. } else {
  83. /* get last path component of source */
  84. s_id = strrchr(fc->source, '/');
  85. if (s_id)
  86. s_id++;
  87. else
  88. s_id = fc->source;
  89. }
  90. va_start(args, fmt);
  91. vaf.fmt = fmt;
  92. vaf.va = &args;
  93. printk("%sEXT2-fs (%s): %pV\n", prefix, s_id, &vaf);
  94. va_end(args);
  95. }
  96. void ext2_msg(struct super_block *sb, const char *prefix,
  97. const char *fmt, ...)
  98. {
  99. struct va_format vaf;
  100. va_list args;
  101. va_start(args, fmt);
  102. vaf.fmt = fmt;
  103. vaf.va = &args;
  104. printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
  105. va_end(args);
  106. }
  107. /*
  108. * This must be called with sbi->s_lock held.
  109. */
  110. void ext2_update_dynamic_rev(struct super_block *sb)
  111. {
  112. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  113. if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
  114. return;
  115. ext2_msg(sb, KERN_WARNING,
  116. "warning: updating to rev %d because of "
  117. "new feature flag, running e2fsck is recommended",
  118. EXT2_DYNAMIC_REV);
  119. es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
  120. es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
  121. es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
  122. /* leave es->s_feature_*compat flags alone */
  123. /* es->s_uuid will be set by e2fsck if empty */
  124. /*
  125. * The rest of the superblock fields should be zero, and if not it
  126. * means they are likely already in use, so leave them alone. We
  127. * can leave it up to e2fsck to clean up any inconsistencies there.
  128. */
  129. }
  130. #ifdef CONFIG_QUOTA
  131. static int ext2_quota_off(struct super_block *sb, int type);
  132. static void ext2_quota_off_umount(struct super_block *sb)
  133. {
  134. int type;
  135. for (type = 0; type < MAXQUOTAS; type++)
  136. ext2_quota_off(sb, type);
  137. }
  138. #else
  139. static inline void ext2_quota_off_umount(struct super_block *sb)
  140. {
  141. }
  142. #endif
  143. static void ext2_put_super (struct super_block * sb)
  144. {
  145. int db_count;
  146. int i;
  147. struct ext2_sb_info *sbi = EXT2_SB(sb);
  148. ext2_quota_off_umount(sb);
  149. ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
  150. sbi->s_ea_block_cache = NULL;
  151. if (!sb_rdonly(sb)) {
  152. struct ext2_super_block *es = sbi->s_es;
  153. spin_lock(&sbi->s_lock);
  154. es->s_state = cpu_to_le16(sbi->s_mount_state);
  155. spin_unlock(&sbi->s_lock);
  156. ext2_sync_super(sb, es, 1);
  157. }
  158. db_count = sbi->s_gdb_count;
  159. for (i = 0; i < db_count; i++)
  160. brelse(sbi->s_group_desc[i]);
  161. kvfree(sbi->s_group_desc);
  162. kfree(sbi->s_debts);
  163. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  164. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  165. percpu_counter_destroy(&sbi->s_dirs_counter);
  166. brelse (sbi->s_sbh);
  167. sb->s_fs_info = NULL;
  168. kfree(sbi->s_blockgroup_lock);
  169. fs_put_dax(sbi->s_daxdev, NULL);
  170. kfree(sbi);
  171. }
  172. static struct kmem_cache * ext2_inode_cachep;
  173. static struct inode *ext2_alloc_inode(struct super_block *sb)
  174. {
  175. struct ext2_inode_info *ei;
  176. ei = alloc_inode_sb(sb, ext2_inode_cachep, GFP_KERNEL);
  177. if (!ei)
  178. return NULL;
  179. ei->i_block_alloc_info = NULL;
  180. inode_set_iversion(&ei->vfs_inode, 1);
  181. #ifdef CONFIG_QUOTA
  182. memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
  183. #endif
  184. return &ei->vfs_inode;
  185. }
  186. static void ext2_free_in_core_inode(struct inode *inode)
  187. {
  188. kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
  189. }
  190. static void init_once(void *foo)
  191. {
  192. struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
  193. rwlock_init(&ei->i_meta_lock);
  194. #ifdef CONFIG_EXT2_FS_XATTR
  195. init_rwsem(&ei->xattr_sem);
  196. #endif
  197. mutex_init(&ei->truncate_mutex);
  198. inode_init_once(&ei->vfs_inode);
  199. }
  200. static int __init init_inodecache(void)
  201. {
  202. ext2_inode_cachep = kmem_cache_create_usercopy("ext2_inode_cache",
  203. sizeof(struct ext2_inode_info), 0,
  204. SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
  205. offsetof(struct ext2_inode_info, i_data),
  206. sizeof_field(struct ext2_inode_info, i_data),
  207. init_once);
  208. if (ext2_inode_cachep == NULL)
  209. return -ENOMEM;
  210. return 0;
  211. }
  212. static void destroy_inodecache(void)
  213. {
  214. /*
  215. * Make sure all delayed rcu free inodes are flushed before we
  216. * destroy cache.
  217. */
  218. rcu_barrier();
  219. kmem_cache_destroy(ext2_inode_cachep);
  220. }
  221. static int ext2_show_options(struct seq_file *seq, struct dentry *root)
  222. {
  223. struct super_block *sb = root->d_sb;
  224. struct ext2_sb_info *sbi = EXT2_SB(sb);
  225. struct ext2_super_block *es = sbi->s_es;
  226. unsigned long def_mount_opts;
  227. spin_lock(&sbi->s_lock);
  228. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  229. if (sbi->s_sb_block != 1)
  230. seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
  231. if (test_opt(sb, MINIX_DF))
  232. seq_puts(seq, ",minixdf");
  233. if (test_opt(sb, GRPID))
  234. seq_puts(seq, ",grpid");
  235. if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
  236. seq_puts(seq, ",nogrpid");
  237. if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT2_DEF_RESUID)) ||
  238. le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
  239. seq_printf(seq, ",resuid=%u",
  240. from_kuid_munged(&init_user_ns, sbi->s_resuid));
  241. }
  242. if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT2_DEF_RESGID)) ||
  243. le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
  244. seq_printf(seq, ",resgid=%u",
  245. from_kgid_munged(&init_user_ns, sbi->s_resgid));
  246. }
  247. if (test_opt(sb, ERRORS_RO)) {
  248. int def_errors = le16_to_cpu(es->s_errors);
  249. if (def_errors == EXT2_ERRORS_PANIC ||
  250. def_errors == EXT2_ERRORS_CONTINUE) {
  251. seq_puts(seq, ",errors=remount-ro");
  252. }
  253. }
  254. if (test_opt(sb, ERRORS_CONT))
  255. seq_puts(seq, ",errors=continue");
  256. if (test_opt(sb, ERRORS_PANIC))
  257. seq_puts(seq, ",errors=panic");
  258. if (test_opt(sb, NO_UID32))
  259. seq_puts(seq, ",nouid32");
  260. if (test_opt(sb, DEBUG))
  261. seq_puts(seq, ",debug");
  262. if (test_opt(sb, OLDALLOC))
  263. seq_puts(seq, ",oldalloc");
  264. #ifdef CONFIG_EXT2_FS_XATTR
  265. if (test_opt(sb, XATTR_USER))
  266. seq_puts(seq, ",user_xattr");
  267. if (!test_opt(sb, XATTR_USER) &&
  268. (def_mount_opts & EXT2_DEFM_XATTR_USER)) {
  269. seq_puts(seq, ",nouser_xattr");
  270. }
  271. #endif
  272. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  273. if (test_opt(sb, POSIX_ACL))
  274. seq_puts(seq, ",acl");
  275. if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
  276. seq_puts(seq, ",noacl");
  277. #endif
  278. if (test_opt(sb, USRQUOTA))
  279. seq_puts(seq, ",usrquota");
  280. if (test_opt(sb, GRPQUOTA))
  281. seq_puts(seq, ",grpquota");
  282. if (test_opt(sb, XIP))
  283. seq_puts(seq, ",xip");
  284. if (test_opt(sb, DAX))
  285. seq_puts(seq, ",dax");
  286. if (!test_opt(sb, RESERVATION))
  287. seq_puts(seq, ",noreservation");
  288. spin_unlock(&sbi->s_lock);
  289. return 0;
  290. }
  291. #ifdef CONFIG_QUOTA
  292. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
  293. static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
  294. static int ext2_quota_on(struct super_block *sb, int type, int format_id,
  295. const struct path *path);
  296. static struct dquot __rcu **ext2_get_dquots(struct inode *inode)
  297. {
  298. return EXT2_I(inode)->i_dquot;
  299. }
  300. static const struct quotactl_ops ext2_quotactl_ops = {
  301. .quota_on = ext2_quota_on,
  302. .quota_off = ext2_quota_off,
  303. .quota_sync = dquot_quota_sync,
  304. .get_state = dquot_get_state,
  305. .set_info = dquot_set_dqinfo,
  306. .get_dqblk = dquot_get_dqblk,
  307. .set_dqblk = dquot_set_dqblk,
  308. .get_nextdqblk = dquot_get_next_dqblk,
  309. };
  310. #endif
  311. static const struct super_operations ext2_sops = {
  312. .alloc_inode = ext2_alloc_inode,
  313. .free_inode = ext2_free_in_core_inode,
  314. .write_inode = ext2_write_inode,
  315. .evict_inode = ext2_evict_inode,
  316. .put_super = ext2_put_super,
  317. .sync_fs = ext2_sync_fs,
  318. .freeze_fs = ext2_freeze,
  319. .unfreeze_fs = ext2_unfreeze,
  320. .statfs = ext2_statfs,
  321. .show_options = ext2_show_options,
  322. #ifdef CONFIG_QUOTA
  323. .quota_read = ext2_quota_read,
  324. .quota_write = ext2_quota_write,
  325. .get_dquots = ext2_get_dquots,
  326. #endif
  327. };
  328. static struct inode *ext2_nfs_get_inode(struct super_block *sb,
  329. u64 ino, u32 generation)
  330. {
  331. struct inode *inode;
  332. if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
  333. return ERR_PTR(-ESTALE);
  334. if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  335. return ERR_PTR(-ESTALE);
  336. /*
  337. * ext2_iget isn't quite right if the inode is currently unallocated!
  338. * However ext2_iget currently does appropriate checks to handle stale
  339. * inodes so everything is OK.
  340. */
  341. inode = ext2_iget(sb, ino);
  342. if (IS_ERR(inode))
  343. return ERR_CAST(inode);
  344. if (generation && inode->i_generation != generation) {
  345. /* we didn't find the right inode.. */
  346. iput(inode);
  347. return ERR_PTR(-ESTALE);
  348. }
  349. return inode;
  350. }
  351. static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  352. int fh_len, int fh_type)
  353. {
  354. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  355. ext2_nfs_get_inode);
  356. }
  357. static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
  358. int fh_len, int fh_type)
  359. {
  360. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  361. ext2_nfs_get_inode);
  362. }
  363. static const struct export_operations ext2_export_ops = {
  364. .encode_fh = generic_encode_ino32_fh,
  365. .fh_to_dentry = ext2_fh_to_dentry,
  366. .fh_to_parent = ext2_fh_to_parent,
  367. .get_parent = ext2_get_parent,
  368. };
  369. enum {
  370. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid, Opt_resgid, Opt_resuid,
  371. Opt_sb, Opt_errors, Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
  372. Opt_nobh, Opt_user_xattr, Opt_acl, Opt_xip, Opt_dax, Opt_ignore,
  373. Opt_quota, Opt_usrquota, Opt_grpquota, Opt_reservation,
  374. };
  375. static const struct constant_table ext2_param_errors[] = {
  376. {"continue", EXT2_MOUNT_ERRORS_CONT},
  377. {"panic", EXT2_MOUNT_ERRORS_PANIC},
  378. {"remount-ro", EXT2_MOUNT_ERRORS_RO},
  379. {}
  380. };
  381. static const struct fs_parameter_spec ext2_param_spec[] = {
  382. fsparam_flag ("bsddf", Opt_bsd_df),
  383. fsparam_flag ("minixdf", Opt_minix_df),
  384. fsparam_flag ("grpid", Opt_grpid),
  385. fsparam_flag ("bsdgroups", Opt_grpid),
  386. fsparam_flag ("nogrpid", Opt_nogrpid),
  387. fsparam_flag ("sysvgroups", Opt_nogrpid),
  388. fsparam_gid ("resgid", Opt_resgid),
  389. fsparam_uid ("resuid", Opt_resuid),
  390. fsparam_u32 ("sb", Opt_sb),
  391. fsparam_enum ("errors", Opt_errors, ext2_param_errors),
  392. fsparam_flag ("nouid32", Opt_nouid32),
  393. fsparam_flag ("debug", Opt_debug),
  394. fsparam_flag ("oldalloc", Opt_oldalloc),
  395. fsparam_flag ("orlov", Opt_orlov),
  396. fsparam_flag ("nobh", Opt_nobh),
  397. fsparam_flag_no ("user_xattr", Opt_user_xattr),
  398. fsparam_flag_no ("acl", Opt_acl),
  399. fsparam_flag ("xip", Opt_xip),
  400. fsparam_flag ("dax", Opt_dax),
  401. fsparam_flag ("grpquota", Opt_grpquota),
  402. fsparam_flag ("noquota", Opt_ignore),
  403. fsparam_flag ("quota", Opt_quota),
  404. fsparam_flag ("usrquota", Opt_usrquota),
  405. fsparam_flag_no ("reservation", Opt_reservation),
  406. {}
  407. };
  408. #define EXT2_SPEC_s_resuid (1 << 0)
  409. #define EXT2_SPEC_s_resgid (1 << 1)
  410. struct ext2_fs_context {
  411. unsigned long vals_s_flags; /* Bits to set in s_flags */
  412. unsigned long mask_s_flags; /* Bits changed in s_flags */
  413. unsigned int vals_s_mount_opt;
  414. unsigned int mask_s_mount_opt;
  415. kuid_t s_resuid;
  416. kgid_t s_resgid;
  417. unsigned long s_sb_block;
  418. unsigned int spec;
  419. };
  420. static inline void ctx_set_mount_opt(struct ext2_fs_context *ctx,
  421. unsigned long flag)
  422. {
  423. ctx->mask_s_mount_opt |= flag;
  424. ctx->vals_s_mount_opt |= flag;
  425. }
  426. static inline void ctx_clear_mount_opt(struct ext2_fs_context *ctx,
  427. unsigned long flag)
  428. {
  429. ctx->mask_s_mount_opt |= flag;
  430. ctx->vals_s_mount_opt &= ~flag;
  431. }
  432. static inline unsigned long
  433. ctx_test_mount_opt(struct ext2_fs_context *ctx, unsigned long flag)
  434. {
  435. return (ctx->vals_s_mount_opt & flag);
  436. }
  437. static inline bool
  438. ctx_parsed_mount_opt(struct ext2_fs_context *ctx, unsigned long flag)
  439. {
  440. return (ctx->mask_s_mount_opt & flag);
  441. }
  442. static void ext2_free_fc(struct fs_context *fc)
  443. {
  444. kfree(fc->fs_private);
  445. }
  446. static int ext2_parse_param(struct fs_context *fc, struct fs_parameter *param)
  447. {
  448. struct ext2_fs_context *ctx = fc->fs_private;
  449. int opt;
  450. struct fs_parse_result result;
  451. opt = fs_parse(fc, ext2_param_spec, param, &result);
  452. if (opt < 0)
  453. return opt;
  454. switch (opt) {
  455. case Opt_bsd_df:
  456. ctx_clear_mount_opt(ctx, EXT2_MOUNT_MINIX_DF);
  457. break;
  458. case Opt_minix_df:
  459. ctx_set_mount_opt(ctx, EXT2_MOUNT_MINIX_DF);
  460. break;
  461. case Opt_grpid:
  462. ctx_set_mount_opt(ctx, EXT2_MOUNT_GRPID);
  463. break;
  464. case Opt_nogrpid:
  465. ctx_clear_mount_opt(ctx, EXT2_MOUNT_GRPID);
  466. break;
  467. case Opt_resuid:
  468. ctx->s_resuid = result.uid;
  469. ctx->spec |= EXT2_SPEC_s_resuid;
  470. break;
  471. case Opt_resgid:
  472. ctx->s_resgid = result.gid;
  473. ctx->spec |= EXT2_SPEC_s_resgid;
  474. break;
  475. case Opt_sb:
  476. /* Note that this is silently ignored on remount */
  477. ctx->s_sb_block = result.uint_32;
  478. break;
  479. case Opt_errors:
  480. ctx_clear_mount_opt(ctx, EXT2_MOUNT_ERRORS_MASK);
  481. ctx_set_mount_opt(ctx, result.uint_32);
  482. break;
  483. case Opt_nouid32:
  484. ctx_set_mount_opt(ctx, EXT2_MOUNT_NO_UID32);
  485. break;
  486. case Opt_debug:
  487. ctx_set_mount_opt(ctx, EXT2_MOUNT_DEBUG);
  488. break;
  489. case Opt_oldalloc:
  490. ctx_set_mount_opt(ctx, EXT2_MOUNT_OLDALLOC);
  491. break;
  492. case Opt_orlov:
  493. ctx_clear_mount_opt(ctx, EXT2_MOUNT_OLDALLOC);
  494. break;
  495. case Opt_nobh:
  496. ext2_msg_fc(fc, KERN_INFO, "nobh option not supported\n");
  497. break;
  498. #ifdef CONFIG_EXT2_FS_XATTR
  499. case Opt_user_xattr:
  500. if (!result.negated)
  501. ctx_set_mount_opt(ctx, EXT2_MOUNT_XATTR_USER);
  502. else
  503. ctx_clear_mount_opt(ctx, EXT2_MOUNT_XATTR_USER);
  504. break;
  505. #else
  506. case Opt_user_xattr:
  507. ext2_msg_fc(fc, KERN_INFO, "(no)user_xattr options not supported");
  508. break;
  509. #endif
  510. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  511. case Opt_acl:
  512. if (!result.negated)
  513. ctx_set_mount_opt(ctx, EXT2_MOUNT_POSIX_ACL);
  514. else
  515. ctx_clear_mount_opt(ctx, EXT2_MOUNT_POSIX_ACL);
  516. break;
  517. #else
  518. case Opt_acl:
  519. ext2_msg_fc(fc, KERN_INFO, "(no)acl options not supported");
  520. break;
  521. #endif
  522. case Opt_xip:
  523. ext2_msg_fc(fc, KERN_INFO, "use dax instead of xip");
  524. ctx_set_mount_opt(ctx, EXT2_MOUNT_XIP);
  525. fallthrough;
  526. case Opt_dax:
  527. #ifdef CONFIG_FS_DAX
  528. ext2_msg_fc(fc, KERN_WARNING,
  529. "DAX enabled. Warning: DAX support in ext2 driver is deprecated"
  530. " and will be removed at the end of 2025. Please use ext4 driver instead.");
  531. ctx_set_mount_opt(ctx, EXT2_MOUNT_DAX);
  532. #else
  533. ext2_msg_fc(fc, KERN_INFO, "dax option not supported");
  534. #endif
  535. break;
  536. #if defined(CONFIG_QUOTA)
  537. case Opt_quota:
  538. case Opt_usrquota:
  539. ctx_set_mount_opt(ctx, EXT2_MOUNT_USRQUOTA);
  540. break;
  541. case Opt_grpquota:
  542. ctx_set_mount_opt(ctx, EXT2_MOUNT_GRPQUOTA);
  543. break;
  544. #else
  545. case Opt_quota:
  546. case Opt_usrquota:
  547. case Opt_grpquota:
  548. ext2_msg_fc(fc, KERN_INFO, "quota operations not supported");
  549. break;
  550. #endif
  551. case Opt_reservation:
  552. if (!result.negated) {
  553. ctx_set_mount_opt(ctx, EXT2_MOUNT_RESERVATION);
  554. ext2_msg_fc(fc, KERN_INFO, "reservations ON");
  555. } else {
  556. ctx_clear_mount_opt(ctx, EXT2_MOUNT_RESERVATION);
  557. ext2_msg_fc(fc, KERN_INFO, "reservations OFF");
  558. }
  559. break;
  560. case Opt_ignore:
  561. break;
  562. default:
  563. return -EINVAL;
  564. }
  565. return 0;
  566. }
  567. static int ext2_setup_super (struct super_block * sb,
  568. struct ext2_super_block * es,
  569. int read_only)
  570. {
  571. int res = 0;
  572. struct ext2_sb_info *sbi = EXT2_SB(sb);
  573. if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
  574. ext2_msg(sb, KERN_ERR,
  575. "error: revision level too high, "
  576. "forcing read-only mode");
  577. res = SB_RDONLY;
  578. }
  579. if (read_only)
  580. return res;
  581. if (!(sbi->s_mount_state & EXT2_VALID_FS))
  582. ext2_msg(sb, KERN_WARNING,
  583. "warning: mounting unchecked fs, "
  584. "running e2fsck is recommended");
  585. else if ((sbi->s_mount_state & EXT2_ERROR_FS))
  586. ext2_msg(sb, KERN_WARNING,
  587. "warning: mounting fs with errors, "
  588. "running e2fsck is recommended");
  589. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  590. le16_to_cpu(es->s_mnt_count) >=
  591. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  592. ext2_msg(sb, KERN_WARNING,
  593. "warning: maximal mount count reached, "
  594. "running e2fsck is recommended");
  595. else if (le32_to_cpu(es->s_checkinterval) &&
  596. (le32_to_cpu(es->s_lastcheck) +
  597. le32_to_cpu(es->s_checkinterval) <=
  598. ktime_get_real_seconds()))
  599. ext2_msg(sb, KERN_WARNING,
  600. "warning: checktime reached, "
  601. "running e2fsck is recommended");
  602. if (!le16_to_cpu(es->s_max_mnt_count))
  603. es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
  604. le16_add_cpu(&es->s_mnt_count, 1);
  605. if (test_opt (sb, DEBUG))
  606. ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, gc=%lu, "
  607. "bpg=%lu, ipg=%lu, mo=%04lx]",
  608. EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
  609. sbi->s_groups_count,
  610. EXT2_BLOCKS_PER_GROUP(sb),
  611. EXT2_INODES_PER_GROUP(sb),
  612. sbi->s_mount_opt);
  613. return res;
  614. }
  615. static int ext2_check_descriptors(struct super_block *sb)
  616. {
  617. int i;
  618. struct ext2_sb_info *sbi = EXT2_SB(sb);
  619. ext2_debug ("Checking group descriptors");
  620. for (i = 0; i < sbi->s_groups_count; i++) {
  621. struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
  622. ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
  623. ext2_fsblk_t last_block = ext2_group_last_block_no(sb, i);
  624. if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
  625. le32_to_cpu(gdp->bg_block_bitmap) > last_block)
  626. {
  627. ext2_error (sb, "ext2_check_descriptors",
  628. "Block bitmap for group %d"
  629. " not in group (block %lu)!",
  630. i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
  631. return 0;
  632. }
  633. if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
  634. le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
  635. {
  636. ext2_error (sb, "ext2_check_descriptors",
  637. "Inode bitmap for group %d"
  638. " not in group (block %lu)!",
  639. i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
  640. return 0;
  641. }
  642. if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
  643. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
  644. last_block)
  645. {
  646. ext2_error (sb, "ext2_check_descriptors",
  647. "Inode table for group %d"
  648. " not in group (block %lu)!",
  649. i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
  650. return 0;
  651. }
  652. }
  653. return 1;
  654. }
  655. /*
  656. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  657. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  658. * We need to be 1 filesystem block less than the 2^32 sector limit.
  659. */
  660. static loff_t ext2_max_size(int bits)
  661. {
  662. loff_t res = EXT2_NDIR_BLOCKS;
  663. int meta_blocks;
  664. unsigned int upper_limit;
  665. unsigned int ppb = 1 << (bits-2);
  666. /* This is calculated to be the largest file size for a
  667. * dense, file such that the total number of
  668. * sectors in the file, including data and all indirect blocks,
  669. * does not exceed 2^32 -1
  670. * __u32 i_blocks representing the total number of
  671. * 512 bytes blocks of the file
  672. */
  673. upper_limit = (1LL << 32) - 1;
  674. /* total blocks in file system block size */
  675. upper_limit >>= (bits - 9);
  676. /* Compute how many blocks we can address by block tree */
  677. res += 1LL << (bits-2);
  678. res += 1LL << (2*(bits-2));
  679. res += 1LL << (3*(bits-2));
  680. /* Compute how many metadata blocks are needed */
  681. meta_blocks = 1;
  682. meta_blocks += 1 + ppb;
  683. meta_blocks += 1 + ppb + ppb * ppb;
  684. /* Does block tree limit file size? */
  685. if (res + meta_blocks <= upper_limit)
  686. goto check_lfs;
  687. res = upper_limit;
  688. /* How many metadata blocks are needed for addressing upper_limit? */
  689. upper_limit -= EXT2_NDIR_BLOCKS;
  690. /* indirect blocks */
  691. meta_blocks = 1;
  692. upper_limit -= ppb;
  693. /* double indirect blocks */
  694. if (upper_limit < ppb * ppb) {
  695. meta_blocks += 1 + DIV_ROUND_UP(upper_limit, ppb);
  696. res -= meta_blocks;
  697. goto check_lfs;
  698. }
  699. meta_blocks += 1 + ppb;
  700. upper_limit -= ppb * ppb;
  701. /* tripple indirect blocks for the rest */
  702. meta_blocks += 1 + DIV_ROUND_UP(upper_limit, ppb) +
  703. DIV_ROUND_UP(upper_limit, ppb*ppb);
  704. res -= meta_blocks;
  705. check_lfs:
  706. res <<= bits;
  707. if (res > MAX_LFS_FILESIZE)
  708. res = MAX_LFS_FILESIZE;
  709. return res;
  710. }
  711. static unsigned long descriptor_loc(struct super_block *sb,
  712. unsigned long logic_sb_block,
  713. int nr)
  714. {
  715. struct ext2_sb_info *sbi = EXT2_SB(sb);
  716. unsigned long bg, first_meta_bg;
  717. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  718. if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
  719. nr < first_meta_bg)
  720. return (logic_sb_block + nr + 1);
  721. bg = sbi->s_desc_per_block * nr;
  722. return ext2_group_first_block_no(sb, bg) + ext2_bg_has_super(sb, bg);
  723. }
  724. /*
  725. * Set all mount options either from defaults on disk, or from parsed
  726. * options. Parsed/specified options override on-disk defaults.
  727. */
  728. static void ext2_set_options(struct fs_context *fc, struct ext2_sb_info *sbi)
  729. {
  730. struct ext2_fs_context *ctx = fc->fs_private;
  731. struct ext2_super_block *es = sbi->s_es;
  732. unsigned long def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  733. /* Copy parsed mount options to sbi */
  734. sbi->s_mount_opt = ctx->vals_s_mount_opt;
  735. /* Use in-superblock defaults only if not specified during parsing */
  736. if (!ctx_parsed_mount_opt(ctx, EXT2_MOUNT_DEBUG) &&
  737. def_mount_opts & EXT2_DEFM_DEBUG)
  738. set_opt(sbi->s_mount_opt, DEBUG);
  739. if (!ctx_parsed_mount_opt(ctx, EXT2_MOUNT_GRPID) &&
  740. def_mount_opts & EXT2_DEFM_BSDGROUPS)
  741. set_opt(sbi->s_mount_opt, GRPID);
  742. if (!ctx_parsed_mount_opt(ctx, EXT2_MOUNT_NO_UID32) &&
  743. def_mount_opts & EXT2_DEFM_UID16)
  744. set_opt(sbi->s_mount_opt, NO_UID32);
  745. #ifdef CONFIG_EXT2_FS_XATTR
  746. if (!ctx_parsed_mount_opt(ctx, EXT2_MOUNT_XATTR_USER) &&
  747. def_mount_opts & EXT2_DEFM_XATTR_USER)
  748. set_opt(sbi->s_mount_opt, XATTR_USER);
  749. #endif
  750. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  751. if (!ctx_parsed_mount_opt(ctx, EXT2_MOUNT_POSIX_ACL) &&
  752. def_mount_opts & EXT2_DEFM_ACL)
  753. set_opt(sbi->s_mount_opt, POSIX_ACL);
  754. #endif
  755. if (!ctx_parsed_mount_opt(ctx, EXT2_MOUNT_ERRORS_MASK)) {
  756. if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
  757. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  758. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
  759. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  760. else
  761. set_opt(sbi->s_mount_opt, ERRORS_RO);
  762. }
  763. if (ctx->spec & EXT2_SPEC_s_resuid)
  764. sbi->s_resuid = ctx->s_resuid;
  765. else
  766. sbi->s_resuid = make_kuid(&init_user_ns,
  767. le16_to_cpu(es->s_def_resuid));
  768. if (ctx->spec & EXT2_SPEC_s_resgid)
  769. sbi->s_resgid = ctx->s_resgid;
  770. else
  771. sbi->s_resgid = make_kgid(&init_user_ns,
  772. le16_to_cpu(es->s_def_resgid));
  773. }
  774. static int ext2_fill_super(struct super_block *sb, struct fs_context *fc)
  775. {
  776. struct ext2_fs_context *ctx = fc->fs_private;
  777. int silent = fc->sb_flags & SB_SILENT;
  778. struct buffer_head * bh;
  779. struct ext2_sb_info * sbi;
  780. struct ext2_super_block * es;
  781. struct inode *root;
  782. unsigned long block;
  783. unsigned long sb_block = ctx->s_sb_block;
  784. unsigned long logic_sb_block;
  785. unsigned long offset = 0;
  786. long ret = -ENOMEM;
  787. int blocksize = BLOCK_SIZE;
  788. int db_count;
  789. int i, j;
  790. __le32 features;
  791. int err;
  792. sbi = kzalloc_obj(*sbi);
  793. if (!sbi)
  794. return -ENOMEM;
  795. sbi->s_blockgroup_lock =
  796. kzalloc_obj(struct blockgroup_lock);
  797. if (!sbi->s_blockgroup_lock) {
  798. kfree(sbi);
  799. return -ENOMEM;
  800. }
  801. sb->s_fs_info = sbi;
  802. sbi->s_sb_block = sb_block;
  803. sbi->s_daxdev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->s_dax_part_off,
  804. NULL, NULL);
  805. spin_lock_init(&sbi->s_lock);
  806. ret = -EINVAL;
  807. /*
  808. * See what the current blocksize for the device is, and
  809. * use that as the blocksize. Otherwise (or if the blocksize
  810. * is smaller than the default) use the default.
  811. * This is important for devices that have a hardware
  812. * sectorsize that is larger than the default.
  813. */
  814. blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
  815. if (!blocksize) {
  816. ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
  817. goto failed_sbi;
  818. }
  819. /*
  820. * If the superblock doesn't start on a hardware sector boundary,
  821. * calculate the offset.
  822. */
  823. if (blocksize != BLOCK_SIZE) {
  824. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  825. offset = (sb_block*BLOCK_SIZE) % blocksize;
  826. } else {
  827. logic_sb_block = sb_block;
  828. }
  829. if (!(bh = sb_bread(sb, logic_sb_block))) {
  830. ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
  831. goto failed_sbi;
  832. }
  833. /*
  834. * Note: s_es must be initialized as soon as possible because
  835. * some ext2 macro-instructions depend on its value
  836. */
  837. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  838. sbi->s_es = es;
  839. sb->s_magic = le16_to_cpu(es->s_magic);
  840. if (sb->s_magic != EXT2_SUPER_MAGIC)
  841. goto cantfind_ext2;
  842. ext2_set_options(fc, sbi);
  843. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  844. (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
  845. sb->s_iflags |= SB_I_CGROUPWB;
  846. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
  847. (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
  848. EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  849. EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  850. ext2_msg(sb, KERN_WARNING,
  851. "warning: feature flags set on rev 0 fs, "
  852. "running e2fsck is recommended");
  853. /*
  854. * Check feature flags regardless of the revision level, since we
  855. * previously didn't change the revision level when setting the flags,
  856. * so there is a chance incompat flags are set on a rev 0 filesystem.
  857. */
  858. features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
  859. if (features) {
  860. ext2_msg(sb, KERN_ERR, "error: couldn't mount because of "
  861. "unsupported optional features (%x)",
  862. le32_to_cpu(features));
  863. goto failed_mount;
  864. }
  865. if (!sb_rdonly(sb) && (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
  866. ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
  867. "unsupported optional features (%x)",
  868. le32_to_cpu(features));
  869. goto failed_mount;
  870. }
  871. if (le32_to_cpu(es->s_log_block_size) >
  872. (EXT2_MAX_BLOCK_LOG_SIZE - BLOCK_SIZE_BITS)) {
  873. ext2_msg(sb, KERN_ERR,
  874. "Invalid log block size: %u",
  875. le32_to_cpu(es->s_log_block_size));
  876. goto failed_mount;
  877. }
  878. blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
  879. if (test_opt(sb, DAX)) {
  880. if (!sbi->s_daxdev) {
  881. ext2_msg(sb, KERN_ERR,
  882. "DAX unsupported by block device. Turning off DAX.");
  883. clear_opt(sbi->s_mount_opt, DAX);
  884. } else if (blocksize != PAGE_SIZE) {
  885. ext2_msg(sb, KERN_ERR, "unsupported blocksize for DAX\n");
  886. clear_opt(sbi->s_mount_opt, DAX);
  887. }
  888. }
  889. /* If the blocksize doesn't match, re-read the thing.. */
  890. if (sb->s_blocksize != blocksize) {
  891. brelse(bh);
  892. if (!sb_set_blocksize(sb, blocksize)) {
  893. ext2_msg(sb, KERN_ERR,
  894. "error: bad blocksize %d", blocksize);
  895. goto failed_sbi;
  896. }
  897. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  898. offset = (sb_block*BLOCK_SIZE) % blocksize;
  899. bh = sb_bread(sb, logic_sb_block);
  900. if(!bh) {
  901. ext2_msg(sb, KERN_ERR, "error: couldn't read"
  902. "superblock on 2nd try");
  903. goto failed_sbi;
  904. }
  905. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  906. sbi->s_es = es;
  907. if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
  908. ext2_msg(sb, KERN_ERR, "error: magic mismatch");
  909. goto failed_mount;
  910. }
  911. }
  912. sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
  913. sb->s_max_links = EXT2_LINK_MAX;
  914. sb->s_time_min = S32_MIN;
  915. sb->s_time_max = S32_MAX;
  916. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
  917. sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
  918. sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
  919. } else {
  920. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  921. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  922. if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
  923. !is_power_of_2(sbi->s_inode_size) ||
  924. (sbi->s_inode_size > blocksize)) {
  925. ext2_msg(sb, KERN_ERR,
  926. "error: unsupported inode size: %d",
  927. sbi->s_inode_size);
  928. goto failed_mount;
  929. }
  930. }
  931. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  932. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  933. sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
  934. if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
  935. goto cantfind_ext2;
  936. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  937. sbi->s_inodes_per_block;
  938. sbi->s_desc_per_block = sb->s_blocksize /
  939. sizeof (struct ext2_group_desc);
  940. sbi->s_sbh = bh;
  941. sbi->s_mount_state = le16_to_cpu(es->s_state);
  942. sbi->s_addr_per_block_bits =
  943. ilog2 (EXT2_ADDR_PER_BLOCK(sb));
  944. sbi->s_desc_per_block_bits =
  945. ilog2 (EXT2_DESC_PER_BLOCK(sb));
  946. if (sb->s_magic != EXT2_SUPER_MAGIC)
  947. goto cantfind_ext2;
  948. if (sb->s_blocksize != bh->b_size) {
  949. if (!silent)
  950. ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
  951. goto failed_mount;
  952. }
  953. if (es->s_log_frag_size != es->s_log_block_size) {
  954. ext2_msg(sb, KERN_ERR,
  955. "error: fragsize log %u != blocksize log %u",
  956. le32_to_cpu(es->s_log_frag_size), sb->s_blocksize_bits);
  957. goto failed_mount;
  958. }
  959. if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
  960. ext2_msg(sb, KERN_ERR,
  961. "error: #blocks per group too big: %lu",
  962. sbi->s_blocks_per_group);
  963. goto failed_mount;
  964. }
  965. /* At least inode table, bitmaps, and sb have to fit in one group */
  966. if (sbi->s_blocks_per_group <= sbi->s_itb_per_group + 3) {
  967. ext2_msg(sb, KERN_ERR,
  968. "error: #blocks per group smaller than metadata size: %lu <= %lu",
  969. sbi->s_blocks_per_group, sbi->s_inodes_per_group + 3);
  970. goto failed_mount;
  971. }
  972. if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
  973. sbi->s_inodes_per_group > sb->s_blocksize * 8) {
  974. ext2_msg(sb, KERN_ERR,
  975. "error: invalid #inodes per group: %lu",
  976. sbi->s_inodes_per_group);
  977. goto failed_mount;
  978. }
  979. if (sb_bdev_nr_blocks(sb) < le32_to_cpu(es->s_blocks_count)) {
  980. ext2_msg(sb, KERN_ERR,
  981. "bad geometry: block count %u exceeds size of device (%u blocks)",
  982. le32_to_cpu(es->s_blocks_count),
  983. (unsigned)sb_bdev_nr_blocks(sb));
  984. goto failed_mount;
  985. }
  986. sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
  987. le32_to_cpu(es->s_first_data_block) - 1)
  988. / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
  989. if ((u64)sbi->s_groups_count * sbi->s_inodes_per_group !=
  990. le32_to_cpu(es->s_inodes_count)) {
  991. ext2_msg(sb, KERN_ERR, "error: invalid #inodes: %u vs computed %llu",
  992. le32_to_cpu(es->s_inodes_count),
  993. (u64)sbi->s_groups_count * sbi->s_inodes_per_group);
  994. goto failed_mount;
  995. }
  996. db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
  997. EXT2_DESC_PER_BLOCK(sb);
  998. sbi->s_group_desc = kvmalloc_objs(struct buffer_head *, db_count);
  999. if (sbi->s_group_desc == NULL) {
  1000. ret = -ENOMEM;
  1001. ext2_msg(sb, KERN_ERR, "error: not enough memory");
  1002. goto failed_mount;
  1003. }
  1004. bgl_lock_init(sbi->s_blockgroup_lock);
  1005. sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
  1006. if (!sbi->s_debts) {
  1007. ret = -ENOMEM;
  1008. ext2_msg(sb, KERN_ERR, "error: not enough memory");
  1009. goto failed_mount_group_desc;
  1010. }
  1011. for (i = 0; i < db_count; i++) {
  1012. block = descriptor_loc(sb, logic_sb_block, i);
  1013. sbi->s_group_desc[i] = sb_bread(sb, block);
  1014. if (!sbi->s_group_desc[i]) {
  1015. for (j = 0; j < i; j++)
  1016. brelse (sbi->s_group_desc[j]);
  1017. ext2_msg(sb, KERN_ERR,
  1018. "error: unable to read group descriptors");
  1019. goto failed_mount_group_desc;
  1020. }
  1021. }
  1022. if (!ext2_check_descriptors (sb)) {
  1023. ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
  1024. goto failed_mount2;
  1025. }
  1026. sbi->s_gdb_count = db_count;
  1027. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1028. spin_lock_init(&sbi->s_next_gen_lock);
  1029. /* per filesystem reservation list head & lock */
  1030. spin_lock_init(&sbi->s_rsv_window_lock);
  1031. sbi->s_rsv_window_root = RB_ROOT;
  1032. /*
  1033. * Add a single, static dummy reservation to the start of the
  1034. * reservation window list --- it gives us a placeholder for
  1035. * append-at-start-of-list which makes the allocation logic
  1036. * _much_ simpler.
  1037. */
  1038. sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  1039. sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  1040. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  1041. sbi->s_rsv_window_head.rsv_goal_size = 0;
  1042. ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
  1043. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  1044. ext2_count_free_blocks(sb), GFP_KERNEL);
  1045. if (!err) {
  1046. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  1047. ext2_count_free_inodes(sb), GFP_KERNEL);
  1048. }
  1049. if (!err) {
  1050. err = percpu_counter_init(&sbi->s_dirs_counter,
  1051. ext2_count_dirs(sb), GFP_KERNEL);
  1052. }
  1053. if (err) {
  1054. ret = err;
  1055. ext2_msg(sb, KERN_ERR, "error: insufficient memory");
  1056. goto failed_mount3;
  1057. }
  1058. #ifdef CONFIG_EXT2_FS_XATTR
  1059. sbi->s_ea_block_cache = ext2_xattr_create_cache();
  1060. if (!sbi->s_ea_block_cache) {
  1061. ret = -ENOMEM;
  1062. ext2_msg(sb, KERN_ERR, "Failed to create ea_block_cache");
  1063. goto failed_mount3;
  1064. }
  1065. #endif
  1066. /*
  1067. * set up enough so that it can read an inode
  1068. */
  1069. sb->s_op = &ext2_sops;
  1070. sb->s_export_op = &ext2_export_ops;
  1071. sb->s_xattr = ext2_xattr_handlers;
  1072. #ifdef CONFIG_QUOTA
  1073. sb->dq_op = &dquot_operations;
  1074. sb->s_qcop = &ext2_quotactl_ops;
  1075. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
  1076. #endif
  1077. root = ext2_iget(sb, EXT2_ROOT_INO);
  1078. if (IS_ERR(root)) {
  1079. ret = PTR_ERR(root);
  1080. goto failed_mount3;
  1081. }
  1082. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1083. iput(root);
  1084. ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
  1085. goto failed_mount3;
  1086. }
  1087. sb->s_root = d_make_root(root);
  1088. if (!sb->s_root) {
  1089. ext2_msg(sb, KERN_ERR, "error: get root inode failed");
  1090. ret = -ENOMEM;
  1091. goto failed_mount3;
  1092. }
  1093. if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
  1094. ext2_msg(sb, KERN_WARNING,
  1095. "warning: mounting ext3 filesystem as ext2");
  1096. if (ext2_setup_super (sb, es, sb_rdonly(sb)))
  1097. sb->s_flags |= SB_RDONLY;
  1098. ext2_write_super(sb);
  1099. return 0;
  1100. cantfind_ext2:
  1101. if (!silent)
  1102. ext2_msg(sb, KERN_ERR,
  1103. "error: can't find an ext2 filesystem on dev %s.",
  1104. sb->s_id);
  1105. goto failed_mount;
  1106. failed_mount3:
  1107. ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
  1108. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  1109. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  1110. percpu_counter_destroy(&sbi->s_dirs_counter);
  1111. failed_mount2:
  1112. for (i = 0; i < db_count; i++)
  1113. brelse(sbi->s_group_desc[i]);
  1114. failed_mount_group_desc:
  1115. kvfree(sbi->s_group_desc);
  1116. kfree(sbi->s_debts);
  1117. failed_mount:
  1118. brelse(bh);
  1119. failed_sbi:
  1120. fs_put_dax(sbi->s_daxdev, NULL);
  1121. sb->s_fs_info = NULL;
  1122. kfree(sbi->s_blockgroup_lock);
  1123. kfree(sbi);
  1124. return ret;
  1125. }
  1126. static void ext2_clear_super_error(struct super_block *sb)
  1127. {
  1128. struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
  1129. if (buffer_write_io_error(sbh)) {
  1130. /*
  1131. * Oh, dear. A previous attempt to write the
  1132. * superblock failed. This could happen because the
  1133. * USB device was yanked out. Or it could happen to
  1134. * be a transient write error and maybe the block will
  1135. * be remapped. Nothing we can do but to retry the
  1136. * write and hope for the best.
  1137. */
  1138. ext2_msg(sb, KERN_ERR,
  1139. "previous I/O error to superblock detected");
  1140. clear_buffer_write_io_error(sbh);
  1141. set_buffer_uptodate(sbh);
  1142. }
  1143. }
  1144. void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
  1145. int wait)
  1146. {
  1147. ext2_clear_super_error(sb);
  1148. spin_lock(&EXT2_SB(sb)->s_lock);
  1149. es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
  1150. es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
  1151. es->s_wtime = cpu_to_le32(ktime_get_real_seconds());
  1152. /* unlock before we do IO */
  1153. spin_unlock(&EXT2_SB(sb)->s_lock);
  1154. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  1155. if (wait)
  1156. sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
  1157. }
  1158. /*
  1159. * In the second extended file system, it is not necessary to
  1160. * write the super block since we use a mapping of the
  1161. * disk super block in a buffer.
  1162. *
  1163. * However, this function is still used to set the fs valid
  1164. * flags to 0. We need to set this flag to 0 since the fs
  1165. * may have been checked while mounted and e2fsck may have
  1166. * set s_state to EXT2_VALID_FS after some corrections.
  1167. */
  1168. static int ext2_sync_fs(struct super_block *sb, int wait)
  1169. {
  1170. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1171. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  1172. /*
  1173. * Write quota structures to quota file, sync_blockdev() will write
  1174. * them to disk later
  1175. */
  1176. dquot_writeback_dquots(sb, -1);
  1177. spin_lock(&sbi->s_lock);
  1178. if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
  1179. ext2_debug("setting valid to 0\n");
  1180. es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
  1181. }
  1182. spin_unlock(&sbi->s_lock);
  1183. ext2_sync_super(sb, es, wait);
  1184. return 0;
  1185. }
  1186. static int ext2_freeze(struct super_block *sb)
  1187. {
  1188. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1189. /*
  1190. * Open but unlinked files present? Keep EXT2_VALID_FS flag cleared
  1191. * because we have unattached inodes and thus filesystem is not fully
  1192. * consistent.
  1193. */
  1194. if (atomic_long_read(&sb->s_remove_count)) {
  1195. ext2_sync_fs(sb, 1);
  1196. return 0;
  1197. }
  1198. /* Set EXT2_FS_VALID flag */
  1199. spin_lock(&sbi->s_lock);
  1200. sbi->s_es->s_state = cpu_to_le16(sbi->s_mount_state);
  1201. spin_unlock(&sbi->s_lock);
  1202. ext2_sync_super(sb, sbi->s_es, 1);
  1203. return 0;
  1204. }
  1205. static int ext2_unfreeze(struct super_block *sb)
  1206. {
  1207. /* Just write sb to clear EXT2_VALID_FS flag */
  1208. ext2_write_super(sb);
  1209. return 0;
  1210. }
  1211. static void ext2_write_super(struct super_block *sb)
  1212. {
  1213. if (!sb_rdonly(sb))
  1214. ext2_sync_fs(sb, 1);
  1215. }
  1216. static int ext2_reconfigure(struct fs_context *fc)
  1217. {
  1218. struct ext2_fs_context *ctx = fc->fs_private;
  1219. struct super_block *sb = fc->root->d_sb;
  1220. struct ext2_sb_info * sbi = EXT2_SB(sb);
  1221. struct ext2_super_block * es;
  1222. struct ext2_mount_options new_opts;
  1223. int flags = fc->sb_flags;
  1224. int err;
  1225. sync_filesystem(sb);
  1226. new_opts.s_mount_opt = ctx->vals_s_mount_opt;
  1227. new_opts.s_resuid = ctx->s_resuid;
  1228. new_opts.s_resgid = ctx->s_resgid;
  1229. spin_lock(&sbi->s_lock);
  1230. es = sbi->s_es;
  1231. if ((sbi->s_mount_opt ^ new_opts.s_mount_opt) & EXT2_MOUNT_DAX) {
  1232. ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
  1233. "dax flag with busy inodes while remounting");
  1234. new_opts.s_mount_opt ^= EXT2_MOUNT_DAX;
  1235. }
  1236. if ((bool)(flags & SB_RDONLY) == sb_rdonly(sb))
  1237. goto out_set;
  1238. if (flags & SB_RDONLY) {
  1239. if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
  1240. !(sbi->s_mount_state & EXT2_VALID_FS))
  1241. goto out_set;
  1242. /*
  1243. * OK, we are remounting a valid rw partition rdonly, so set
  1244. * the rdonly flag and then mark the partition as valid again.
  1245. */
  1246. es->s_state = cpu_to_le16(sbi->s_mount_state);
  1247. es->s_mtime = cpu_to_le32(ktime_get_real_seconds());
  1248. spin_unlock(&sbi->s_lock);
  1249. err = dquot_suspend(sb, -1);
  1250. if (err < 0)
  1251. return err;
  1252. ext2_sync_super(sb, es, 1);
  1253. } else {
  1254. __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1255. ~EXT2_FEATURE_RO_COMPAT_SUPP);
  1256. if (ret) {
  1257. spin_unlock(&sbi->s_lock);
  1258. ext2_msg(sb, KERN_WARNING,
  1259. "warning: couldn't remount RDWR because of "
  1260. "unsupported optional features (%x).",
  1261. le32_to_cpu(ret));
  1262. return -EROFS;
  1263. }
  1264. /*
  1265. * Mounting a RDONLY partition read-write, so reread and
  1266. * store the current valid flag. (It may have been changed
  1267. * by e2fsck since we originally mounted the partition.)
  1268. */
  1269. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1270. if (!ext2_setup_super (sb, es, 0))
  1271. sb->s_flags &= ~SB_RDONLY;
  1272. spin_unlock(&sbi->s_lock);
  1273. ext2_write_super(sb);
  1274. dquot_resume(sb, -1);
  1275. }
  1276. spin_lock(&sbi->s_lock);
  1277. out_set:
  1278. sbi->s_mount_opt = new_opts.s_mount_opt;
  1279. sbi->s_resuid = new_opts.s_resuid;
  1280. sbi->s_resgid = new_opts.s_resgid;
  1281. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  1282. (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
  1283. spin_unlock(&sbi->s_lock);
  1284. return 0;
  1285. }
  1286. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
  1287. {
  1288. struct super_block *sb = dentry->d_sb;
  1289. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1290. struct ext2_super_block *es = sbi->s_es;
  1291. spin_lock(&sbi->s_lock);
  1292. if (test_opt (sb, MINIX_DF))
  1293. sbi->s_overhead_last = 0;
  1294. else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
  1295. unsigned long i, overhead = 0;
  1296. smp_rmb();
  1297. /*
  1298. * Compute the overhead (FS structures). This is constant
  1299. * for a given filesystem unless the number of block groups
  1300. * changes so we cache the previous value until it does.
  1301. */
  1302. /*
  1303. * All of the blocks before first_data_block are
  1304. * overhead
  1305. */
  1306. overhead = le32_to_cpu(es->s_first_data_block);
  1307. /*
  1308. * Add the overhead attributed to the superblock and
  1309. * block group descriptors. If the sparse superblocks
  1310. * feature is turned on, then not all groups have this.
  1311. */
  1312. for (i = 0; i < sbi->s_groups_count; i++)
  1313. overhead += ext2_bg_has_super(sb, i) +
  1314. ext2_bg_num_gdb(sb, i);
  1315. /*
  1316. * Every block group has an inode bitmap, a block
  1317. * bitmap, and an inode table.
  1318. */
  1319. overhead += (sbi->s_groups_count *
  1320. (2 + sbi->s_itb_per_group));
  1321. sbi->s_overhead_last = overhead;
  1322. smp_wmb();
  1323. sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
  1324. }
  1325. buf->f_type = EXT2_SUPER_MAGIC;
  1326. buf->f_bsize = sb->s_blocksize;
  1327. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
  1328. buf->f_bfree = ext2_count_free_blocks(sb);
  1329. es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
  1330. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  1331. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  1332. buf->f_bavail = 0;
  1333. buf->f_files = le32_to_cpu(es->s_inodes_count);
  1334. buf->f_ffree = ext2_count_free_inodes(sb);
  1335. es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
  1336. buf->f_namelen = EXT2_NAME_LEN;
  1337. buf->f_fsid = uuid_to_fsid(es->s_uuid);
  1338. spin_unlock(&sbi->s_lock);
  1339. return 0;
  1340. }
  1341. static int ext2_get_tree(struct fs_context *fc)
  1342. {
  1343. return get_tree_bdev(fc, ext2_fill_super);
  1344. }
  1345. #ifdef CONFIG_QUOTA
  1346. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  1347. * acquiring the locks... As quota files are never truncated and quota code
  1348. * itself serializes the operations (and no one else should touch the files)
  1349. * we don't have to be afraid of races */
  1350. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
  1351. size_t len, loff_t off)
  1352. {
  1353. struct inode *inode = sb_dqopt(sb)->files[type];
  1354. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1355. int err = 0;
  1356. int offset = off & (sb->s_blocksize - 1);
  1357. int tocopy;
  1358. size_t toread;
  1359. struct buffer_head tmp_bh;
  1360. struct buffer_head *bh;
  1361. loff_t i_size = i_size_read(inode);
  1362. if (off > i_size)
  1363. return 0;
  1364. if (off+len > i_size)
  1365. len = i_size-off;
  1366. toread = len;
  1367. while (toread > 0) {
  1368. tocopy = min_t(size_t, sb->s_blocksize - offset, toread);
  1369. tmp_bh.b_state = 0;
  1370. tmp_bh.b_size = sb->s_blocksize;
  1371. err = ext2_get_block(inode, blk, &tmp_bh, 0);
  1372. if (err < 0)
  1373. return err;
  1374. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  1375. memset(data, 0, tocopy);
  1376. else {
  1377. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1378. if (!bh)
  1379. return -EIO;
  1380. memcpy(data, bh->b_data+offset, tocopy);
  1381. brelse(bh);
  1382. }
  1383. offset = 0;
  1384. toread -= tocopy;
  1385. data += tocopy;
  1386. blk++;
  1387. }
  1388. return len;
  1389. }
  1390. /* Write to quotafile */
  1391. static ssize_t ext2_quota_write(struct super_block *sb, int type,
  1392. const char *data, size_t len, loff_t off)
  1393. {
  1394. struct inode *inode = sb_dqopt(sb)->files[type];
  1395. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1396. int err = 0;
  1397. int offset = off & (sb->s_blocksize - 1);
  1398. int tocopy;
  1399. size_t towrite = len;
  1400. struct buffer_head tmp_bh;
  1401. struct buffer_head *bh;
  1402. while (towrite > 0) {
  1403. tocopy = min_t(size_t, sb->s_blocksize - offset, towrite);
  1404. tmp_bh.b_state = 0;
  1405. tmp_bh.b_size = sb->s_blocksize;
  1406. err = ext2_get_block(inode, blk, &tmp_bh, 1);
  1407. if (err < 0)
  1408. goto out;
  1409. if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
  1410. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1411. else
  1412. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  1413. if (unlikely(!bh)) {
  1414. err = -EIO;
  1415. goto out;
  1416. }
  1417. lock_buffer(bh);
  1418. memcpy(bh->b_data+offset, data, tocopy);
  1419. flush_dcache_folio(bh->b_folio);
  1420. set_buffer_uptodate(bh);
  1421. mark_buffer_dirty(bh);
  1422. unlock_buffer(bh);
  1423. brelse(bh);
  1424. offset = 0;
  1425. towrite -= tocopy;
  1426. data += tocopy;
  1427. blk++;
  1428. }
  1429. out:
  1430. if (len == towrite)
  1431. return err;
  1432. if (inode->i_size < off+len-towrite)
  1433. i_size_write(inode, off+len-towrite);
  1434. inode_inc_iversion(inode);
  1435. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  1436. mark_inode_dirty(inode);
  1437. return len - towrite;
  1438. }
  1439. static int ext2_quota_on(struct super_block *sb, int type, int format_id,
  1440. const struct path *path)
  1441. {
  1442. int err;
  1443. struct inode *inode;
  1444. err = dquot_quota_on(sb, type, format_id, path);
  1445. if (err)
  1446. return err;
  1447. inode = d_inode(path->dentry);
  1448. inode_lock(inode);
  1449. EXT2_I(inode)->i_flags |= EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL;
  1450. inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
  1451. S_NOATIME | S_IMMUTABLE);
  1452. inode_unlock(inode);
  1453. mark_inode_dirty(inode);
  1454. return 0;
  1455. }
  1456. static int ext2_quota_off(struct super_block *sb, int type)
  1457. {
  1458. struct inode *inode = sb_dqopt(sb)->files[type];
  1459. int err;
  1460. if (!inode || !igrab(inode))
  1461. goto out;
  1462. err = dquot_quota_off(sb, type);
  1463. if (err)
  1464. goto out_put;
  1465. inode_lock(inode);
  1466. EXT2_I(inode)->i_flags &= ~(EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL);
  1467. inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
  1468. inode_unlock(inode);
  1469. mark_inode_dirty(inode);
  1470. out_put:
  1471. iput(inode);
  1472. return err;
  1473. out:
  1474. return dquot_quota_off(sb, type);
  1475. }
  1476. #endif
  1477. static const struct fs_context_operations ext2_context_ops = {
  1478. .parse_param = ext2_parse_param,
  1479. .get_tree = ext2_get_tree,
  1480. .reconfigure = ext2_reconfigure,
  1481. .free = ext2_free_fc,
  1482. };
  1483. static int ext2_init_fs_context(struct fs_context *fc)
  1484. {
  1485. struct ext2_fs_context *ctx;
  1486. ctx = kzalloc_obj(*ctx);
  1487. if (!ctx)
  1488. return -ENOMEM;
  1489. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  1490. struct super_block *sb = fc->root->d_sb;
  1491. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1492. spin_lock(&sbi->s_lock);
  1493. ctx->vals_s_mount_opt = sbi->s_mount_opt;
  1494. ctx->vals_s_flags = sb->s_flags;
  1495. ctx->s_resuid = sbi->s_resuid;
  1496. ctx->s_resgid = sbi->s_resgid;
  1497. spin_unlock(&sbi->s_lock);
  1498. } else {
  1499. ctx->s_sb_block = 1;
  1500. ctx_set_mount_opt(ctx, EXT2_MOUNT_RESERVATION);
  1501. }
  1502. fc->fs_private = ctx;
  1503. fc->ops = &ext2_context_ops;
  1504. return 0;
  1505. }
  1506. static struct file_system_type ext2_fs_type = {
  1507. .owner = THIS_MODULE,
  1508. .name = "ext2",
  1509. .kill_sb = kill_block_super,
  1510. .fs_flags = FS_REQUIRES_DEV,
  1511. .init_fs_context = ext2_init_fs_context,
  1512. .parameters = ext2_param_spec,
  1513. };
  1514. MODULE_ALIAS_FS("ext2");
  1515. static int __init init_ext2_fs(void)
  1516. {
  1517. int err;
  1518. err = init_inodecache();
  1519. if (err)
  1520. return err;
  1521. err = register_filesystem(&ext2_fs_type);
  1522. if (err)
  1523. goto out;
  1524. return 0;
  1525. out:
  1526. destroy_inodecache();
  1527. return err;
  1528. }
  1529. static void __exit exit_ext2_fs(void)
  1530. {
  1531. unregister_filesystem(&ext2_fs_type);
  1532. destroy_inodecache();
  1533. }
  1534. MODULE_AUTHOR("Remy Card and others");
  1535. MODULE_DESCRIPTION("Second Extended Filesystem");
  1536. MODULE_LICENSE("GPL");
  1537. module_init(init_ext2_fs)
  1538. module_exit(exit_ext2_fs)