1 #include <linux/errno.h>
3 #include <linux/quota.h>
4 #include <linux/quotaops.h>
5 #include <linux/dqblk_v1.h>
6 #include <linux/quotaio_v1.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
11 #include <asm/byteorder.h>
13 MODULE_AUTHOR("Jan Kara");
14 MODULE_DESCRIPTION("Old quota format support");
15 MODULE_LICENSE("GPL");
17 #define QUOTABLOCK_BITS 10
18 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
20 static inline qsize_t v1_stoqb(qsize_t space)
22 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
25 static inline qsize_t v1_qbtos(qsize_t blocks)
27 return blocks << QUOTABLOCK_BITS;
30 static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
32 m->dqb_ihardlimit = d->dqb_ihardlimit;
33 m->dqb_isoftlimit = d->dqb_isoftlimit;
34 m->dqb_curinodes = d->dqb_curinodes;
35 m->dqb_bhardlimit = v1_qbtos(d->dqb_bhardlimit);
36 m->dqb_bsoftlimit = v1_qbtos(d->dqb_bsoftlimit);
37 m->dqb_curspace = v1_qbtos(d->dqb_curblocks);
38 m->dqb_itime = d->dqb_itime;
39 m->dqb_btime = d->dqb_btime;
42 static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
44 d->dqb_ihardlimit = m->dqb_ihardlimit;
45 d->dqb_isoftlimit = m->dqb_isoftlimit;
46 d->dqb_curinodes = m->dqb_curinodes;
47 d->dqb_bhardlimit = v1_stoqb(m->dqb_bhardlimit);
48 d->dqb_bsoftlimit = v1_stoqb(m->dqb_bsoftlimit);
49 d->dqb_curblocks = v1_stoqb(m->dqb_curspace);
50 d->dqb_itime = m->dqb_itime;
51 d->dqb_btime = m->dqb_btime;
54 static int v1_read_dqblk(struct dquot *dquot)
56 int type = dquot->dq_type;
57 struct v1_disk_dqblk dqblk;
59 if (!sb_dqopt(dquot->dq_sb)->files[type])
62 /* Set structure to 0s in case read fails/is after end of file */
63 memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
64 dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk, sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id));
66 v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
67 if (dquot->dq_dqb.dqb_bhardlimit == 0 && dquot->dq_dqb.dqb_bsoftlimit == 0 &&
68 dquot->dq_dqb.dqb_ihardlimit == 0 && dquot->dq_dqb.dqb_isoftlimit == 0)
69 set_bit(DQ_FAKE_B, &dquot->dq_flags);
75 static int v1_commit_dqblk(struct dquot *dquot)
77 short type = dquot->dq_type;
79 struct v1_disk_dqblk dqblk;
81 v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
82 if (dquot->dq_id == 0) {
83 dqblk.dqb_btime = sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
84 dqblk.dqb_itime = sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
87 if (sb_dqopt(dquot->dq_sb)->files[type])
88 ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type, (char *)&dqblk,
89 sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id));
90 if (ret != sizeof(struct v1_disk_dqblk)) {
91 printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
105 /* Magics of new quota format */
106 #define V2_INITQMAGICS {\
107 0xd9c01f11, /* USRQUOTA */\
108 0xd9c01927 /* GRPQUOTA */\
111 /* Header of new quota format */
112 struct v2_disk_dqheader {
113 __le32 dqh_magic; /* Magic number identifying file */
114 __le32 dqh_version; /* File version */
117 static int v1_check_quota_file(struct super_block *sb, int type)
119 struct inode *inode = sb_dqopt(sb)->files[type];
122 struct v2_disk_dqheader dqhead;
125 static const uint quota_magics[] = V2_INITQMAGICS;
127 isize = i_size_read(inode);
130 blocks = isize >> BLOCK_SIZE_BITS;
131 off = isize & (BLOCK_SIZE - 1);
132 if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) % sizeof(struct v1_disk_dqblk))
134 /* Doublecheck whether we didn't get file with new format - with old quotactl() this could happen */
135 size = sb->s_op->quota_read(sb, type, (char *)&dqhead, sizeof(struct v2_disk_dqheader), 0);
136 if (size != sizeof(struct v2_disk_dqheader))
137 return 1; /* Probably not new format */
138 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
139 return 1; /* Definitely not new format */
140 printk(KERN_INFO "VFS: %s: Refusing to turn on old quota format on given file. It probably contains newer quota format.\n", sb->s_id);
141 return 0; /* Seems like a new format file -> refuse it */
144 static int v1_read_file_info(struct super_block *sb, int type)
146 struct quota_info *dqopt = sb_dqopt(sb);
147 struct v1_disk_dqblk dqblk;
150 if ((ret = sb->s_op->quota_read(sb, type, (char *)&dqblk, sizeof(struct v1_disk_dqblk), v1_dqoff(0))) != sizeof(struct v1_disk_dqblk)) {
156 /* limits are stored as unsigned 32-bit data */
157 dqopt->info[type].dqi_maxblimit = 0xffffffff;
158 dqopt->info[type].dqi_maxilimit = 0xffffffff;
159 dqopt->info[type].dqi_igrace = dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
160 dqopt->info[type].dqi_bgrace = dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
165 static int v1_write_file_info(struct super_block *sb, int type)
167 struct quota_info *dqopt = sb_dqopt(sb);
168 struct v1_disk_dqblk dqblk;
171 dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
172 if ((ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
173 sizeof(struct v1_disk_dqblk), v1_dqoff(0))) != sizeof(struct v1_disk_dqblk)) {
178 dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
179 dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
180 ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
181 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
182 if (ret == sizeof(struct v1_disk_dqblk))
190 static struct quota_format_ops v1_format_ops = {
191 .check_quota_file = v1_check_quota_file,
192 .read_file_info = v1_read_file_info,
193 .write_file_info = v1_write_file_info,
194 .free_file_info = NULL,
195 .read_dqblk = v1_read_dqblk,
196 .commit_dqblk = v1_commit_dqblk,
199 static struct quota_format_type v1_quota_format = {
200 .qf_fmt_id = QFMT_VFS_OLD,
201 .qf_ops = &v1_format_ops,
202 .qf_owner = THIS_MODULE
205 static int __init init_v1_quota_format(void)
207 return register_quota_format(&v1_quota_format);
210 static void __exit exit_v1_quota_format(void)
212 unregister_quota_format(&v1_quota_format);
215 module_init(init_v1_quota_format);
216 module_exit(exit_v1_quota_format);