]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/quota/quota_v1.c
Revert "(namespace) Revert "UBUNTU: SAUCE: quota: Convert ids relative to s_user_ns""
[mirror_ubuntu-artful-kernel.git] / fs / quota / quota_v1.c
CommitLineData
1da177e4
LT
1#include <linux/errno.h>
2#include <linux/fs.h>
3#include <linux/quota.h>
74abb989 4#include <linux/quotaops.h>
1da177e4 5#include <linux/dqblk_v1.h>
1da177e4
LT
6#include <linux/kernel.h>
7#include <linux/init.h>
8#include <linux/module.h>
9
10#include <asm/byteorder.h>
11
cf770c13
JK
12#include "quotaio_v1.h"
13
1da177e4
LT
14MODULE_AUTHOR("Jan Kara");
15MODULE_DESCRIPTION("Old quota format support");
16MODULE_LICENSE("GPL");
17
12095460
JK
18#define QUOTABLOCK_BITS 10
19#define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
20
21static inline qsize_t v1_stoqb(qsize_t space)
22{
23 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
24}
25
26static inline qsize_t v1_qbtos(qsize_t blocks)
27{
28 return blocks << QUOTABLOCK_BITS;
29}
30
1da177e4
LT
31static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
32{
33 m->dqb_ihardlimit = d->dqb_ihardlimit;
34 m->dqb_isoftlimit = d->dqb_isoftlimit;
35 m->dqb_curinodes = d->dqb_curinodes;
12095460
JK
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);
1da177e4
LT
39 m->dqb_itime = d->dqb_itime;
40 m->dqb_btime = d->dqb_btime;
41}
42
43static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
44{
45 d->dqb_ihardlimit = m->dqb_ihardlimit;
46 d->dqb_isoftlimit = m->dqb_isoftlimit;
47 d->dqb_curinodes = m->dqb_curinodes;
12095460
JK
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);
1da177e4
LT
51 d->dqb_itime = m->dqb_itime;
52 d->dqb_btime = m->dqb_btime;
53}
54
55static int v1_read_dqblk(struct dquot *dquot)
56{
4c376dca 57 int type = dquot->dq_id.type;
1da177e4 58 struct v1_disk_dqblk dqblk;
8010267b 59 qid_t qid;
1da177e4
LT
60
61 if (!sb_dqopt(dquot->dq_sb)->files[type])
62 return -EINVAL;
63
8010267b
LH
64 qid = from_kqid(dquot->dq_sb->s_user_ns, dquot->dq_id);
65 if (qid == (qid_t)-1)
66 return -EOVERFLOW;
67
1da177e4
LT
68 /* Set structure to 0s in case read fails/is after end of file */
69 memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
268157ba 70 dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk,
4c376dca 71 sizeof(struct v1_disk_dqblk),
8010267b 72 v1_dqoff(qid));
1da177e4
LT
73
74 v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
268157ba
JK
75 if (dquot->dq_dqb.dqb_bhardlimit == 0 &&
76 dquot->dq_dqb.dqb_bsoftlimit == 0 &&
77 dquot->dq_dqb.dqb_ihardlimit == 0 &&
78 dquot->dq_dqb.dqb_isoftlimit == 0)
1da177e4 79 set_bit(DQ_FAKE_B, &dquot->dq_flags);
dde95888 80 dqstats_inc(DQST_READS);
1da177e4
LT
81
82 return 0;
83}
84
85static int v1_commit_dqblk(struct dquot *dquot)
86{
4c376dca 87 short type = dquot->dq_id.type;
1da177e4
LT
88 ssize_t ret;
89 struct v1_disk_dqblk dqblk;
8010267b
LH
90 qid_t qid = from_kqid(dquot->dq_sb->s_user_ns, dquot->dq_id);
91
92 if (qid == (qid_t)-1)
93 return -EOVERFLOW;
1da177e4
LT
94
95 v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
4c376dca
EB
96 if (((type == USRQUOTA) && uid_eq(dquot->dq_id.uid, GLOBAL_ROOT_UID)) ||
97 ((type == GRPQUOTA) && gid_eq(dquot->dq_id.gid, GLOBAL_ROOT_GID))) {
268157ba
JK
98 dqblk.dqb_btime =
99 sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
100 dqblk.dqb_itime =
101 sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
1da177e4
LT
102 }
103 ret = 0;
104 if (sb_dqopt(dquot->dq_sb)->files[type])
268157ba
JK
105 ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type,
106 (char *)&dqblk, sizeof(struct v1_disk_dqblk),
8010267b 107 v1_dqoff(qid));
1da177e4 108 if (ret != sizeof(struct v1_disk_dqblk)) {
fb5ffb0e 109 quota_error(dquot->dq_sb, "dquota write failed");
1da177e4
LT
110 if (ret >= 0)
111 ret = -EIO;
112 goto out;
113 }
114 ret = 0;
115
116out:
dde95888 117 dqstats_inc(DQST_WRITES);
1da177e4
LT
118
119 return ret;
120}
121
122/* Magics of new quota format */
123#define V2_INITQMAGICS {\
124 0xd9c01f11, /* USRQUOTA */\
125 0xd9c01927 /* GRPQUOTA */\
126}
127
128/* Header of new quota format */
129struct v2_disk_dqheader {
130 __le32 dqh_magic; /* Magic number identifying file */
131 __le32 dqh_version; /* File version */
132};
133
134static int v1_check_quota_file(struct super_block *sb, int type)
135{
136 struct inode *inode = sb_dqopt(sb)->files[type];
137 ulong blocks;
138 size_t off;
139 struct v2_disk_dqheader dqhead;
140 ssize_t size;
141 loff_t isize;
142 static const uint quota_magics[] = V2_INITQMAGICS;
143
144 isize = i_size_read(inode);
145 if (!isize)
146 return 0;
147 blocks = isize >> BLOCK_SIZE_BITS;
148 off = isize & (BLOCK_SIZE - 1);
268157ba
JK
149 if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) %
150 sizeof(struct v1_disk_dqblk))
1da177e4 151 return 0;
268157ba
JK
152 /* Doublecheck whether we didn't get file with new format - with old
153 * quotactl() this could happen */
154 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
155 sizeof(struct v2_disk_dqheader), 0);
1da177e4
LT
156 if (size != sizeof(struct v2_disk_dqheader))
157 return 1; /* Probably not new format */
158 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
159 return 1; /* Definitely not new format */
268157ba
JK
160 printk(KERN_INFO
161 "VFS: %s: Refusing to turn on old quota format on given file."
162 " It probably contains newer quota format.\n", sb->s_id);
1da177e4
LT
163 return 0; /* Seems like a new format file -> refuse it */
164}
165
166static int v1_read_file_info(struct super_block *sb, int type)
167{
168 struct quota_info *dqopt = sb_dqopt(sb);
169 struct v1_disk_dqblk dqblk;
170 int ret;
171
268157ba
JK
172 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
173 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
174 if (ret != sizeof(struct v1_disk_dqblk)) {
1da177e4
LT
175 if (ret >= 0)
176 ret = -EIO;
177 goto out;
178 }
179 ret = 0;
338bf9af 180 /* limits are stored as unsigned 32-bit data */
b10a0819
JK
181 dqopt->info[type].dqi_max_spc_limit = 0xffffffffULL << QUOTABLOCK_BITS;
182 dqopt->info[type].dqi_max_ino_limit = 0xffffffff;
268157ba
JK
183 dqopt->info[type].dqi_igrace =
184 dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
185 dqopt->info[type].dqi_bgrace =
186 dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
1da177e4
LT
187out:
188 return ret;
189}
190
191static int v1_write_file_info(struct super_block *sb, int type)
192{
193 struct quota_info *dqopt = sb_dqopt(sb);
194 struct v1_disk_dqblk dqblk;
195 int ret;
196
197 dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
268157ba
JK
198 ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
199 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
200 if (ret != sizeof(struct v1_disk_dqblk)) {
1da177e4
LT
201 if (ret >= 0)
202 ret = -EIO;
203 goto out;
204 }
205 dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
206 dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
207 ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
208 sizeof(struct v1_disk_dqblk), v1_dqoff(0));
209 if (ret == sizeof(struct v1_disk_dqblk))
210 ret = 0;
211 else if (ret > 0)
212 ret = -EIO;
213out:
214 return ret;
215}
216
1472da5f 217static const struct quota_format_ops v1_format_ops = {
1da177e4
LT
218 .check_quota_file = v1_check_quota_file,
219 .read_file_info = v1_read_file_info,
220 .write_file_info = v1_write_file_info,
221 .free_file_info = NULL,
222 .read_dqblk = v1_read_dqblk,
223 .commit_dqblk = v1_commit_dqblk,
224};
225
226static struct quota_format_type v1_quota_format = {
227 .qf_fmt_id = QFMT_VFS_OLD,
228 .qf_ops = &v1_format_ops,
229 .qf_owner = THIS_MODULE
230};
231
232static int __init init_v1_quota_format(void)
233{
234 return register_quota_format(&v1_quota_format);
235}
236
237static void __exit exit_v1_quota_format(void)
238{
239 unregister_quota_format(&v1_quota_format);
240}
241
242module_init(init_v1_quota_format);
243module_exit(exit_v1_quota_format);
244