1 #include <linux/errno.h>
3 #include <linux/quota.h>
4 #include <linux/quotaops.h>
5 #include <linux/dqblk_v1.h>
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
10 #include <asm/byteorder.h>
12 #include "quotaio_v1.h"
14 MODULE_AUTHOR("Jan Kara");
15 MODULE_DESCRIPTION("Old quota format support");
16 MODULE_LICENSE("GPL");
18 #define QUOTABLOCK_BITS 10
19 #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
21 static inline qsize_t v1_stoqb(qsize_t space)
23 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
26 static inline qsize_t v1_qbtos(qsize_t blocks)
28 return blocks << QUOTABLOCK_BITS;
31 static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
33 m->dqb_ihardlimit = d->dqb_ihardlimit;
34 m->dqb_isoftlimit = d->dqb_isoftlimit;
35 m->dqb_curinodes = d->dqb_curinodes;
36 m->dqb_bhardlimit = v1_qbtos(d->dqb_bhardlimit);
37 m->dqb_bsoftlimit = v1_qbtos(d->dqb_bsoftlimit);
38 m->dqb_curspace = v1_qbtos(d->dqb_curblocks);
39 m->dqb_itime = d->dqb_itime;
40 m->dqb_btime = d->dqb_btime;
43 static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
45 d->dqb_ihardlimit = m->dqb_ihardlimit;
46 d->dqb_isoftlimit = m->dqb_isoftlimit;
47 d->dqb_curinodes = m->dqb_curinodes;
48 d->dqb_bhardlimit = v1_stoqb(m->dqb_bhardlimit);
49 d->dqb_bsoftlimit = v1_stoqb(m->dqb_bsoftlimit);
50 d->dqb_curblocks = v1_stoqb(m->dqb_curspace);
51 d->dqb_itime = m->dqb_itime;
52 d->dqb_btime = m->dqb_btime;
55 static int v1_read_dqblk(struct dquot *dquot)
57 int type = dquot->dq_id.type;
58 struct v1_disk_dqblk dqblk;
59 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
61 if (!dqopt->files[type])
64 down_read(&dqopt->dqio_sem);
65 /* Set structure to 0s in case read fails/is after end of file */
66 memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
67 dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk,
68 sizeof(struct v1_disk_dqblk),
69 v1_dqoff(from_kqid(&init_user_ns, dquot->dq_id)));
71 v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
72 if (dquot->dq_dqb.dqb_bhardlimit == 0 &&
73 dquot->dq_dqb.dqb_bsoftlimit == 0 &&
74 dquot->dq_dqb.dqb_ihardlimit == 0 &&
75 dquot->dq_dqb.dqb_isoftlimit == 0)
76 set_bit(DQ_FAKE_B, &dquot->dq_flags);
77 dqstats_inc(DQST_READS);
78 up_read(&dqopt->dqio_sem);
83 static int v1_commit_dqblk(struct dquot *dquot)
85 short type = dquot->dq_id.type;
87 struct v1_disk_dqblk dqblk;
89 v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
90 if (((type == USRQUOTA) && uid_eq(dquot->dq_id.uid, GLOBAL_ROOT_UID)) ||
91 ((type == GRPQUOTA) && gid_eq(dquot->dq_id.gid, GLOBAL_ROOT_GID))) {
93 sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
95 sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
98 if (sb_dqopt(dquot->dq_sb)->files[type])
99 ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type,
100 (char *)&dqblk, sizeof(struct v1_disk_dqblk),
101 v1_dqoff(from_kqid(&init_user_ns, dquot->dq_id)));
102 if (ret != sizeof(struct v1_disk_dqblk)) {
103 quota_error(dquot->dq_sb, "dquota write failed");
111 dqstats_inc(DQST_WRITES);
116 /* Magics of new quota format */
117 #define V2_INITQMAGICS {\
118 0xd9c01f11, /* USRQUOTA */\
119 0xd9c01927 /* GRPQUOTA */\
122 /* Header of new quota format */
123 struct v2_disk_dqheader {
124 __le32 dqh_magic; /* Magic number identifying file */
125 __le32 dqh_version; /* File version */
128 static int v1_check_quota_file(struct super_block *sb, int type)
130 struct inode *inode = sb_dqopt(sb)->files[type];
133 struct v2_disk_dqheader dqhead;
136 static const uint quota_magics[] = V2_INITQMAGICS;
138 isize = i_size_read(inode);
141 blocks = isize >> BLOCK_SIZE_BITS;
142 off = isize & (BLOCK_SIZE - 1);
143 if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) %
144 sizeof(struct v1_disk_dqblk))
146 /* Doublecheck whether we didn't get file with new format - with old
147 * quotactl() this could happen */
148 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
149 sizeof(struct v2_disk_dqheader), 0);
150 if (size != sizeof(struct v2_disk_dqheader))
151 return 1; /* Probably not new format */
152 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
153 return 1; /* Definitely not new format */
155 "VFS: %s: Refusing to turn on old quota format on given file."
156 " It probably contains newer quota format.\n", sb->s_id);
157 return 0; /* Seems like a new format file -> refuse it */
160 static int v1_read_file_info(struct super_block *sb, int type)
162 struct quota_info *dqopt = sb_dqopt(sb);
163 struct v1_disk_dqblk dqblk;
166 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
167 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
168 if (ret != sizeof(struct v1_disk_dqblk)) {
174 /* limits are stored as unsigned 32-bit data */
175 dqopt->info[type].dqi_max_spc_limit = 0xffffffffULL << QUOTABLOCK_BITS;
176 dqopt->info[type].dqi_max_ino_limit = 0xffffffff;
177 dqopt->info[type].dqi_igrace =
178 dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
179 dqopt->info[type].dqi_bgrace =
180 dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
185 static int v1_write_file_info(struct super_block *sb, int type)
187 struct quota_info *dqopt = sb_dqopt(sb);
188 struct v1_disk_dqblk dqblk;
191 dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
192 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
193 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
194 if (ret != sizeof(struct v1_disk_dqblk)) {
199 dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
200 dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
201 ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
202 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
203 if (ret == sizeof(struct v1_disk_dqblk))
211 static const struct quota_format_ops v1_format_ops = {
212 .check_quota_file = v1_check_quota_file,
213 .read_file_info = v1_read_file_info,
214 .write_file_info = v1_write_file_info,
215 .free_file_info = NULL,
216 .read_dqblk = v1_read_dqblk,
217 .commit_dqblk = v1_commit_dqblk,
220 static struct quota_format_type v1_quota_format = {
221 .qf_fmt_id = QFMT_VFS_OLD,
222 .qf_ops = &v1_format_ops,
223 .qf_owner = THIS_MODULE
226 static int __init init_v1_quota_format(void)
228 return register_quota_format(&v1_quota_format);
231 static void __exit exit_v1_quota_format(void)
233 unregister_quota_format(&v1_quota_format);
236 module_init(init_v1_quota_format);
237 module_exit(exit_v1_quota_format);