Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
Jan Kara | 74abb98 | 2008-07-25 01:46:51 -0700 | [diff] [blame] | 13 | #include <linux/quotaops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #include <asm/byteorder.h> |
| 16 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 17 | #include "quota_tree.h" |
Jan Kara | cf770c1 | 2008-09-21 23:17:53 +0200 | [diff] [blame] | 18 | #include "quotaio_v2.h" |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | MODULE_AUTHOR("Jan Kara"); |
| 21 | MODULE_DESCRIPTION("Quota format v2 support"); |
| 22 | MODULE_LICENSE("GPL"); |
| 23 | |
| 24 | #define __QUOTA_V2_PARANOIA |
| 25 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 26 | static void v2_mem2diskdqb(void *dp, struct dquot *dquot); |
| 27 | static void v2_disk2memdqb(struct dquot *dquot, void *dp); |
| 28 | static int v2_is_id(void *dp, struct dquot *dquot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 30 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Jan Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 36 | #define QUOTABLOCK_BITS 10 |
| 37 | #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS) |
| 38 | |
| 39 | static inline qsize_t v2_stoqb(qsize_t space) |
| 40 | { |
| 41 | return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS; |
| 42 | } |
| 43 | |
| 44 | static inline qsize_t v2_qbtos(qsize_t blocks) |
| 45 | { |
| 46 | return blocks << QUOTABLOCK_BITS; |
| 47 | } |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* Check whether given file is really vfsv0 quotafile */ |
| 50 | static 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 Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 57 | size = sb->s_op->quota_read(sb, type, (char *)&dqhead, |
| 58 | sizeof(struct v2_disk_dqheader), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (size != sizeof(struct v2_disk_dqheader)) { |
Andrew Morton | 8c17e1e | 2006-02-03 03:04:03 -0800 | [diff] [blame] | 60 | printk("quota_v2: failed read expected=%zd got=%zd\n", |
Valdis.Kletnieks@vt.edu | 3135806 | 2006-01-14 13:21:10 -0800 | [diff] [blame] | 61 | sizeof(struct v2_disk_dqheader), size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | 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 */ |
| 71 | static int v2_read_file_info(struct super_block *sb, int type) |
| 72 | { |
| 73 | struct v2_disk_dqinfo dinfo; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 74 | struct mem_dqinfo *info = sb_dqinfo(sb, type); |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 75 | struct qtree_mem_dqinfo *qinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | 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 Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 85 | 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 Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 92 | /* limits are stored as unsigned 32-bit data */ |
| 93 | info->dqi_maxblimit = 0xffffffff; |
| 94 | info->dqi_maxilimit = 0xffffffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | 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 Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 98 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* Write information header to quota file */ |
| 112 | static int v2_write_file_info(struct super_block *sb, int type) |
| 113 | { |
| 114 | struct v2_disk_dqinfo dinfo; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 115 | struct mem_dqinfo *info = sb_dqinfo(sb, type); |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 116 | struct qtree_mem_dqinfo *qinfo = info->dqi_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | 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 Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 125 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | 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 Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 138 | static void v2_disk2memdqb(struct dquot *dquot, void *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 140 | struct v2_disk_dqblk *d = dp, empty; |
| 141 | struct mem_dqblk *m = &dquot->dq_dqb; |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | 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 Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 147 | m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit)); |
| 148 | m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | m->dqb_curspace = le64_to_cpu(d->dqb_curspace); |
| 150 | m->dqb_btime = le64_to_cpu(d->dqb_btime); |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 151 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 158 | static void v2_mem2diskdqb(void *dp, struct dquot *dquot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 160 | struct v2_disk_dqblk *d = dp; |
| 161 | struct mem_dqblk *m = &dquot->dq_dqb; |
| 162 | struct qtree_mem_dqinfo *info = |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 163 | sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | 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 Kara | cf770c1 | 2008-09-21 23:17:53 +0200 | [diff] [blame] | 169 | d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit)); |
| 170 | d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | d->dqb_curspace = cpu_to_le64(m->dqb_curspace); |
| 172 | d->dqb_btime = cpu_to_le64(m->dqb_btime); |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 173 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 178 | static int v2_is_id(void *dp, struct dquot *dquot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 180 | struct v2_disk_dqblk *d = dp; |
| 181 | struct qtree_mem_dqinfo *info = |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 182 | sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 184 | if (qtree_entry_unused(info, dp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return 0; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 186 | return le32_to_cpu(d->dqb_id) == dquot->dq_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static int v2_read_dquot(struct dquot *dquot) |
| 190 | { |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 191 | return qtree_read_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 194 | static int v2_write_dquot(struct dquot *dquot) |
| 195 | { |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 196 | return qtree_write_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot); |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | static int v2_release_dquot(struct dquot *dquot) |
| 200 | { |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 201 | return qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot); |
| 202 | } |
| 203 | |
| 204 | static int v2_free_file_info(struct super_block *sb, int type) |
| 205 | { |
| 206 | kfree(sb_dqinfo(sb, type)->dqi_priv); |
| 207 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static struct quota_format_ops v2_format_ops = { |
| 211 | .check_quota_file = v2_check_quota_file, |
| 212 | .read_file_info = v2_read_file_info, |
| 213 | .write_file_info = v2_write_file_info, |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 214 | .free_file_info = v2_free_file_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | .read_dqblk = v2_read_dquot, |
| 216 | .commit_dqblk = v2_write_dquot, |
| 217 | .release_dqblk = v2_release_dquot, |
| 218 | }; |
| 219 | |
| 220 | static 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 | |
| 226 | static int __init init_v2_quota_format(void) |
| 227 | { |
| 228 | return register_quota_format(&v2_quota_format); |
| 229 | } |
| 230 | |
| 231 | static void __exit exit_v2_quota_format(void) |
| 232 | { |
| 233 | unregister_quota_format(&v2_quota_format); |
| 234 | } |
| 235 | |
| 236 | module_init(init_v2_quota_format); |
| 237 | module_exit(exit_v2_quota_format); |