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> 9 #include <linux/kernel.h> 10 #include <linux/init.h> 11 #include <linux/module.h> 12 #include <linux/slab.h> 13 #include <linux/quotaops.h> 14 15 #include <asm/byteorder.h> 16 17 #include "quota_tree.h" 18 #include "quotaio_v2.h" 19 20 MODULE_AUTHOR("Jan Kara"); 21 MODULE_DESCRIPTION("Quota format v2 support"); 22 MODULE_LICENSE("GPL"); 23 24 #define __QUOTA_V2_PARANOIA 25 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); 29 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 }; 35 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 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, 58 sizeof(struct v2_disk_dqheader), 0); 59 if (size != sizeof(struct v2_disk_dqheader)) { 60 printk("quota_v2: failed read expected=%zd got=%zd\n", 61 sizeof(struct v2_disk_dqheader), size); 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; 74 struct mem_dqinfo *info = sb_dqinfo(sb, type); 75 struct qtree_mem_dqinfo *qinfo; 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 } 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; 92 /* limits are stored as unsigned 32-bit data */ 93 info->dqi_maxblimit = 0xffffffff; 94 info->dqi_maxilimit = 0xffffffff; 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); 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; 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; 115 struct mem_dqinfo *info = sb_dqinfo(sb, type); 116 struct qtree_mem_dqinfo *qinfo = info->dqi_priv; 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); 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); 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 138 static void v2_disk2memdqb(struct dquot *dquot, void *dp) 139 { 140 struct v2_disk_dqblk *d = dp, empty; 141 struct mem_dqblk *m = &dquot->dq_dqb; 142 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); 147 m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit)); 148 m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit)); 149 m->dqb_curspace = le64_to_cpu(d->dqb_curspace); 150 m->dqb_btime = le64_to_cpu(d->dqb_btime); 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; 156 } 157 158 static void v2_mem2diskdqb(void *dp, struct dquot *dquot) 159 { 160 struct v2_disk_dqblk *d = dp; 161 struct mem_dqblk *m = &dquot->dq_dqb; 162 struct qtree_mem_dqinfo *info = 163 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; 164 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); 169 d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit)); 170 d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit)); 171 d->dqb_curspace = cpu_to_le64(m->dqb_curspace); 172 d->dqb_btime = cpu_to_le64(m->dqb_btime); 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); 176 } 177 178 static int v2_is_id(void *dp, struct dquot *dquot) 179 { 180 struct v2_disk_dqblk *d = dp; 181 struct qtree_mem_dqinfo *info = 182 sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv; 183 184 if (qtree_entry_unused(info, dp)) 185 return 0; 186 return le32_to_cpu(d->dqb_id) == dquot->dq_id; 187 } 188 189 static int v2_read_dquot(struct dquot *dquot) 190 { 191 return qtree_read_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot); 192 } 193 194 static int v2_write_dquot(struct dquot *dquot) 195 { 196 return qtree_write_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot); 197 } 198 199 static int v2_release_dquot(struct dquot *dquot) 200 { 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; 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, 214 .free_file_info = v2_free_file_info, 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); 238