blob: 01f25eae684d858d43254fb33e049af30f134fd7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * vfsv0 quota IO operations on file
3 */
4
5#include <linux/errno.h>
6#include <linux/fs.h>
7#include <linux/mount.h>
8#include <linux/dqblk_v2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/slab.h>
Jan Kara74abb982008-07-25 01:46:51 -070013#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/byteorder.h>
16
Jan Kara1ccd14b2008-09-22 05:54:49 +020017#include "quota_tree.h"
Jan Karacf770c12008-09-21 23:17:53 +020018#include "quotaio_v2.h"
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020MODULE_AUTHOR("Jan Kara");
21MODULE_DESCRIPTION("Quota format v2 support");
22MODULE_LICENSE("GPL");
23
24#define __QUOTA_V2_PARANOIA
25
Jan Kara1ccd14b2008-09-22 05:54:49 +020026static void v2_mem2diskdqb(void *dp, struct dquot *dquot);
27static void v2_disk2memdqb(struct dquot *dquot, void *dp);
28static int v2_is_id(void *dp, struct dquot *dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Jan Kara1ccd14b2008-09-22 05:54:49 +020030static struct qtree_fmt_operations v2_qtree_ops = {
31 .mem2disk_dqblk = v2_mem2diskdqb,
32 .disk2mem_dqblk = v2_disk2memdqb,
33 .is_id = v2_is_id,
34};
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Jan Kara12095462008-08-20 14:45:12 +020036#define QUOTABLOCK_BITS 10
37#define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
38
39static inline qsize_t v2_stoqb(qsize_t space)
40{
41 return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
42}
43
44static inline qsize_t v2_qbtos(qsize_t blocks)
45{
46 return blocks << QUOTABLOCK_BITS;
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Check whether given file is really vfsv0 quotafile */
50static int v2_check_quota_file(struct super_block *sb, int type)
51{
52 struct v2_disk_dqheader dqhead;
53 ssize_t size;
54 static const uint quota_magics[] = V2_INITQMAGICS;
55 static const uint quota_versions[] = V2_INITQVERSIONS;
56
Jan Kara268157b2009-01-27 15:47:22 +010057 size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
58 sizeof(struct v2_disk_dqheader), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (size != sizeof(struct v2_disk_dqheader)) {
Andrew Morton8c17e1e2006-02-03 03:04:03 -080060 printk("quota_v2: failed read expected=%zd got=%zd\n",
Valdis.Kletnieks@vt.edu31358062006-01-14 13:21:10 -080061 sizeof(struct v2_disk_dqheader), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
63 }
64 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
65 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
66 return 0;
67 return 1;
68}
69
70/* Read information header from quota file */
71static int v2_read_file_info(struct super_block *sb, int type)
72{
73 struct v2_disk_dqinfo dinfo;
Jan Kara1ccd14b2008-09-22 05:54:49 +020074 struct mem_dqinfo *info = sb_dqinfo(sb, type);
Jan Karae3d4d562008-10-02 18:44:14 +020075 struct qtree_mem_dqinfo *qinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 ssize_t size;
77
78 size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
79 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
80 if (size != sizeof(struct v2_disk_dqinfo)) {
81 printk(KERN_WARNING "Can't read info structure on device %s.\n",
82 sb->s_id);
83 return -1;
84 }
Jan Karae3d4d562008-10-02 18:44:14 +020085 info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
86 if (!info->dqi_priv) {
87 printk(KERN_WARNING
88 "Not enough memory for quota information structure.\n");
89 return -1;
90 }
91 qinfo = info->dqi_priv;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -070092 /* limits are stored as unsigned 32-bit data */
93 info->dqi_maxblimit = 0xffffffff;
94 info->dqi_maxilimit = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
96 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
97 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
Jan Karae3d4d562008-10-02 18:44:14 +020098 qinfo->dqi_sb = sb;
99 qinfo->dqi_type = type;
100 qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
101 qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
102 qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
103 qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
104 qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
105 qinfo->dqi_qtree_depth = qtree_depth(qinfo);
106 qinfo->dqi_entry_size = sizeof(struct v2_disk_dqblk);
107 qinfo->dqi_ops = &v2_qtree_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
111/* Write information header to quota file */
112static int v2_write_file_info(struct super_block *sb, int type)
113{
114 struct v2_disk_dqinfo dinfo;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200115 struct mem_dqinfo *info = sb_dqinfo(sb, type);
Jan Karae3d4d562008-10-02 18:44:14 +0200116 struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ssize_t size;
118
119 spin_lock(&dq_data_lock);
120 info->dqi_flags &= ~DQF_INFO_DIRTY;
121 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
122 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
123 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
124 spin_unlock(&dq_data_lock);
Jan Karae3d4d562008-10-02 18:44:14 +0200125 dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
126 dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
127 dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
129 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
130 if (size != sizeof(struct v2_disk_dqinfo)) {
131 printk(KERN_WARNING "Can't write info structure on device %s.\n",
132 sb->s_id);
133 return -1;
134 }
135 return 0;
136}
137
Jan Kara1ccd14b2008-09-22 05:54:49 +0200138static void v2_disk2memdqb(struct dquot *dquot, void *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Jan Kara1ccd14b2008-09-22 05:54:49 +0200140 struct v2_disk_dqblk *d = dp, empty;
141 struct mem_dqblk *m = &dquot->dq_dqb;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
144 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
145 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
146 m->dqb_itime = le64_to_cpu(d->dqb_itime);
Jan Kara12095462008-08-20 14:45:12 +0200147 m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
148 m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
150 m->dqb_btime = le64_to_cpu(d->dqb_btime);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200151 /* We need to escape back all-zero structure */
152 memset(&empty, 0, sizeof(struct v2_disk_dqblk));
153 empty.dqb_itime = cpu_to_le64(1);
154 if (!memcmp(&empty, dp, sizeof(struct v2_disk_dqblk)))
155 m->dqb_itime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Jan Kara1ccd14b2008-09-22 05:54:49 +0200158static void v2_mem2diskdqb(void *dp, struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Jan Kara1ccd14b2008-09-22 05:54:49 +0200160 struct v2_disk_dqblk *d = dp;
161 struct mem_dqblk *m = &dquot->dq_dqb;
162 struct qtree_mem_dqinfo *info =
Jan Karae3d4d562008-10-02 18:44:14 +0200163 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
166 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
167 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
168 d->dqb_itime = cpu_to_le64(m->dqb_itime);
Jan Karacf770c12008-09-21 23:17:53 +0200169 d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
170 d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
172 d->dqb_btime = cpu_to_le64(m->dqb_btime);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200173 d->dqb_id = cpu_to_le32(dquot->dq_id);
174 if (qtree_entry_unused(info, dp))
175 d->dqb_itime = cpu_to_le64(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Jan Kara1ccd14b2008-09-22 05:54:49 +0200178static int v2_is_id(void *dp, struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Jan Kara1ccd14b2008-09-22 05:54:49 +0200180 struct v2_disk_dqblk *d = dp;
181 struct qtree_mem_dqinfo *info =
Jan Karae3d4d562008-10-02 18:44:14 +0200182 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Jan Kara1ccd14b2008-09-22 05:54:49 +0200184 if (qtree_entry_unused(info, dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200186 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189static int v2_read_dquot(struct dquot *dquot)
190{
Jan Karae3d4d562008-10-02 18:44:14 +0200191 return qtree_read_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Jan Kara1ccd14b2008-09-22 05:54:49 +0200194static int v2_write_dquot(struct dquot *dquot)
195{
Jan Karae3d4d562008-10-02 18:44:14 +0200196 return qtree_write_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int v2_release_dquot(struct dquot *dquot)
200{
Jan Karae3d4d562008-10-02 18:44:14 +0200201 return qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
202}
203
204static int v2_free_file_info(struct super_block *sb, int type)
205{
206 kfree(sb_dqinfo(sb, type)->dqi_priv);
207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Alexey Dobriyan1472da52009-10-16 15:26:03 +0400210static const struct quota_format_ops v2_format_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .check_quota_file = v2_check_quota_file,
212 .read_file_info = v2_read_file_info,
213 .write_file_info = v2_write_file_info,
Jan Karae3d4d562008-10-02 18:44:14 +0200214 .free_file_info = v2_free_file_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .read_dqblk = v2_read_dquot,
216 .commit_dqblk = v2_write_dquot,
217 .release_dqblk = v2_release_dquot,
218};
219
220static struct quota_format_type v2_quota_format = {
221 .qf_fmt_id = QFMT_VFS_V0,
222 .qf_ops = &v2_format_ops,
223 .qf_owner = THIS_MODULE
224};
225
226static int __init init_v2_quota_format(void)
227{
228 return register_quota_format(&v2_quota_format);
229}
230
231static void __exit exit_v2_quota_format(void)
232{
233 unregister_quota_format(&v2_quota_format);
234}
235
236module_init(init_v2_quota_format);
237module_exit(exit_v2_quota_format);