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 | |
| 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 Morton | 8c17e1e | 2006-02-03 03:04:03 -0800 | [diff] [blame] | 59 | printk("quota_v2: failed read expected=%zd got=%zd\n", |
Valdis.Kletnieks@vt.edu | 3135806 | 2006-01-14 13:21:10 -0800 | [diff] [blame] | 60 | sizeof(struct v2_disk_dqheader), size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | 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 */ |
| 70 | static int v2_read_file_info(struct super_block *sb, int type) |
| 71 | { |
| 72 | struct v2_disk_dqinfo dinfo; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 73 | struct mem_dqinfo *info = sb_dqinfo(sb, type); |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 74 | struct qtree_mem_dqinfo *qinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 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 Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 84 | 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 Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 91 | /* limits are stored as unsigned 32-bit data */ |
| 92 | info->dqi_maxblimit = 0xffffffff; |
| 93 | info->dqi_maxilimit = 0xffffffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | 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 Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 97 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* Write information header to quota file */ |
| 111 | static int v2_write_file_info(struct super_block *sb, int type) |
| 112 | { |
| 113 | struct v2_disk_dqinfo dinfo; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 114 | struct mem_dqinfo *info = sb_dqinfo(sb, type); |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 115 | struct qtree_mem_dqinfo *qinfo = info->dqi_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | 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 Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 124 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | 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 Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 137 | static void v2_disk2memdqb(struct dquot *dquot, void *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 139 | struct v2_disk_dqblk *d = dp, empty; |
| 140 | struct mem_dqblk *m = &dquot->dq_dqb; |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | 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 Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 146 | m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit)); |
| 147 | m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | m->dqb_curspace = le64_to_cpu(d->dqb_curspace); |
| 149 | m->dqb_btime = le64_to_cpu(d->dqb_btime); |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 150 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 157 | static void v2_mem2diskdqb(void *dp, struct dquot *dquot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 159 | struct v2_disk_dqblk *d = dp; |
| 160 | struct mem_dqblk *m = &dquot->dq_dqb; |
| 161 | struct qtree_mem_dqinfo *info = |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 162 | sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | 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 Kara | cf770c1 | 2008-09-21 23:17:53 +0200 | [diff] [blame] | 168 | d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit)); |
| 169 | d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | d->dqb_curspace = cpu_to_le64(m->dqb_curspace); |
| 171 | d->dqb_btime = cpu_to_le64(m->dqb_btime); |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 172 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 177 | static int v2_is_id(void *dp, struct dquot *dquot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 179 | struct v2_disk_dqblk *d = dp; |
| 180 | struct qtree_mem_dqinfo *info = |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 181 | sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 183 | if (qtree_entry_unused(info, dp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return 0; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 185 | return le32_to_cpu(d->dqb_id) == dquot->dq_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static int v2_read_dquot(struct dquot *dquot) |
| 189 | { |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 190 | 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] | 191 | } |
| 192 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 193 | static int v2_write_dquot(struct dquot *dquot) |
| 194 | { |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 195 | 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] | 196 | } |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | static int v2_release_dquot(struct dquot *dquot) |
| 199 | { |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 200 | return qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot); |
| 201 | } |
| 202 | |
| 203 | static int v2_free_file_info(struct super_block *sb, int type) |
| 204 | { |
| 205 | kfree(sb_dqinfo(sb, type)->dqi_priv); |
| 206 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static 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 Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 213 | .free_file_info = v2_free_file_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | .read_dqblk = v2_read_dquot, |
| 215 | .commit_dqblk = v2_write_dquot, |
| 216 | .release_dqblk = v2_release_dquot, |
| 217 | }; |
| 218 | |
| 219 | static 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 | |
| 225 | static int __init init_v2_quota_format(void) |
| 226 | { |
| 227 | return register_quota_format(&v2_quota_format); |
| 228 | } |
| 229 | |
| 230 | static void __exit exit_v2_quota_format(void) |
| 231 | { |
| 232 | unregister_quota_format(&v2_quota_format); |
| 233 | } |
| 234 | |
| 235 | module_init(init_v2_quota_format); |
| 236 | module_exit(exit_v2_quota_format); |