blob: b618b563635c6322e63be9fa00c70d0d0ad7f5b6 [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
57 size = sb->s_op->quota_read(sb, type, (char *)&dqhead, sizeof(struct v2_disk_dqheader), 0);
58 if (size != sizeof(struct v2_disk_dqheader)) {
Andrew Morton8c17e1e2006-02-03 03:04:03 -080059 printk("quota_v2: failed read expected=%zd got=%zd\n",
Valdis.Kletnieks@vt.edu31358062006-01-14 13:21:10 -080060 sizeof(struct v2_disk_dqheader), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return 0;
62 }
63 if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
64 le32_to_cpu(dqhead.dqh_version) != quota_versions[type])
65 return 0;
66 return 1;
67}
68
69/* Read information header from quota file */
70static int v2_read_file_info(struct super_block *sb, int type)
71{
72 struct v2_disk_dqinfo dinfo;
Jan Kara1ccd14b2008-09-22 05:54:49 +020073 struct mem_dqinfo *info = sb_dqinfo(sb, type);
Jan Karae3d4d562008-10-02 18:44:14 +020074 struct qtree_mem_dqinfo *qinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 ssize_t size;
76
77 size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
78 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
79 if (size != sizeof(struct v2_disk_dqinfo)) {
80 printk(KERN_WARNING "Can't read info structure on device %s.\n",
81 sb->s_id);
82 return -1;
83 }
Jan Karae3d4d562008-10-02 18:44:14 +020084 info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
85 if (!info->dqi_priv) {
86 printk(KERN_WARNING
87 "Not enough memory for quota information structure.\n");
88 return -1;
89 }
90 qinfo = info->dqi_priv;
Andrew Perepechko338bf9a2008-04-28 02:14:31 -070091 /* limits are stored as unsigned 32-bit data */
92 info->dqi_maxblimit = 0xffffffff;
93 info->dqi_maxilimit = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
95 info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
96 info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
Jan Karae3d4d562008-10-02 18:44:14 +020097 qinfo->dqi_sb = sb;
98 qinfo->dqi_type = type;
99 qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
100 qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
101 qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
102 qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
103 qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
104 qinfo->dqi_qtree_depth = qtree_depth(qinfo);
105 qinfo->dqi_entry_size = sizeof(struct v2_disk_dqblk);
106 qinfo->dqi_ops = &v2_qtree_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108}
109
110/* Write information header to quota file */
111static int v2_write_file_info(struct super_block *sb, int type)
112{
113 struct v2_disk_dqinfo dinfo;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200114 struct mem_dqinfo *info = sb_dqinfo(sb, type);
Jan Karae3d4d562008-10-02 18:44:14 +0200115 struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ssize_t size;
117
118 spin_lock(&dq_data_lock);
119 info->dqi_flags &= ~DQF_INFO_DIRTY;
120 dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
121 dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
122 dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
123 spin_unlock(&dq_data_lock);
Jan Karae3d4d562008-10-02 18:44:14 +0200124 dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
125 dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
126 dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
128 sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
129 if (size != sizeof(struct v2_disk_dqinfo)) {
130 printk(KERN_WARNING "Can't write info structure on device %s.\n",
131 sb->s_id);
132 return -1;
133 }
134 return 0;
135}
136
Jan Kara1ccd14b2008-09-22 05:54:49 +0200137static void v2_disk2memdqb(struct dquot *dquot, void *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Jan Kara1ccd14b2008-09-22 05:54:49 +0200139 struct v2_disk_dqblk *d = dp, empty;
140 struct mem_dqblk *m = &dquot->dq_dqb;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
143 m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
144 m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
145 m->dqb_itime = le64_to_cpu(d->dqb_itime);
Jan Kara12095462008-08-20 14:45:12 +0200146 m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
147 m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
149 m->dqb_btime = le64_to_cpu(d->dqb_btime);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200150 /* We need to escape back all-zero structure */
151 memset(&empty, 0, sizeof(struct v2_disk_dqblk));
152 empty.dqb_itime = cpu_to_le64(1);
153 if (!memcmp(&empty, dp, sizeof(struct v2_disk_dqblk)))
154 m->dqb_itime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Jan Kara1ccd14b2008-09-22 05:54:49 +0200157static void v2_mem2diskdqb(void *dp, struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Jan Kara1ccd14b2008-09-22 05:54:49 +0200159 struct v2_disk_dqblk *d = dp;
160 struct mem_dqblk *m = &dquot->dq_dqb;
161 struct qtree_mem_dqinfo *info =
Jan Karae3d4d562008-10-02 18:44:14 +0200162 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
165 d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
166 d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
167 d->dqb_itime = cpu_to_le64(m->dqb_itime);
Jan Karacf770c12008-09-21 23:17:53 +0200168 d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
169 d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
171 d->dqb_btime = cpu_to_le64(m->dqb_btime);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200172 d->dqb_id = cpu_to_le32(dquot->dq_id);
173 if (qtree_entry_unused(info, dp))
174 d->dqb_itime = cpu_to_le64(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Jan Kara1ccd14b2008-09-22 05:54:49 +0200177static int v2_is_id(void *dp, struct dquot *dquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Jan Kara1ccd14b2008-09-22 05:54:49 +0200179 struct v2_disk_dqblk *d = dp;
180 struct qtree_mem_dqinfo *info =
Jan Karae3d4d562008-10-02 18:44:14 +0200181 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jan Kara1ccd14b2008-09-22 05:54:49 +0200183 if (qtree_entry_unused(info, dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 0;
Jan Kara1ccd14b2008-09-22 05:54:49 +0200185 return le32_to_cpu(d->dqb_id) == dquot->dq_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static int v2_read_dquot(struct dquot *dquot)
189{
Jan Karae3d4d562008-10-02 18:44:14 +0200190 return qtree_read_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Jan Kara1ccd14b2008-09-22 05:54:49 +0200193static int v2_write_dquot(struct dquot *dquot)
194{
Jan Karae3d4d562008-10-02 18:44:14 +0200195 return qtree_write_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
Jan Kara1ccd14b2008-09-22 05:54:49 +0200196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static int v2_release_dquot(struct dquot *dquot)
199{
Jan Karae3d4d562008-10-02 18:44:14 +0200200 return qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
201}
202
203static int v2_free_file_info(struct super_block *sb, int type)
204{
205 kfree(sb_dqinfo(sb, type)->dqi_priv);
206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209static struct quota_format_ops v2_format_ops = {
210 .check_quota_file = v2_check_quota_file,
211 .read_file_info = v2_read_file_info,
212 .write_file_info = v2_write_file_info,
Jan Karae3d4d562008-10-02 18:44:14 +0200213 .free_file_info = v2_free_file_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .read_dqblk = v2_read_dquot,
215 .commit_dqblk = v2_write_dquot,
216 .release_dqblk = v2_release_dquot,
217};
218
219static struct quota_format_type v2_quota_format = {
220 .qf_fmt_id = QFMT_VFS_V0,
221 .qf_ops = &v2_format_ops,
222 .qf_owner = THIS_MODULE
223};
224
225static int __init init_v2_quota_format(void)
226{
227 return register_quota_format(&v2_quota_format);
228}
229
230static void __exit exit_v2_quota_format(void)
231{
232 unregister_quota_format(&v2_quota_format);
233}
234
235module_init(init_v2_quota_format);
236module_exit(exit_v2_quota_format);