Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/errno.h> |
| 2 | #include <linux/fs.h> |
| 3 | #include <linux/quota.h> |
Jan Kara | 74abb98 | 2008-07-25 01:46:51 -0700 | [diff] [blame] | 4 | #include <linux/quotaops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/dqblk_v1.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/kernel.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/module.h> |
| 9 | |
| 10 | #include <asm/byteorder.h> |
| 11 | |
Jan Kara | cf770c1 | 2008-09-21 23:17:53 +0200 | [diff] [blame] | 12 | #include "quotaio_v1.h" |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | MODULE_AUTHOR("Jan Kara"); |
| 15 | MODULE_DESCRIPTION("Old quota format support"); |
| 16 | MODULE_LICENSE("GPL"); |
| 17 | |
Jan Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 18 | #define QUOTABLOCK_BITS 10 |
| 19 | #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS) |
| 20 | |
| 21 | static inline qsize_t v1_stoqb(qsize_t space) |
| 22 | { |
| 23 | return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS; |
| 24 | } |
| 25 | |
| 26 | static inline qsize_t v1_qbtos(qsize_t blocks) |
| 27 | { |
| 28 | return blocks << QUOTABLOCK_BITS; |
| 29 | } |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d) |
| 32 | { |
| 33 | m->dqb_ihardlimit = d->dqb_ihardlimit; |
| 34 | m->dqb_isoftlimit = d->dqb_isoftlimit; |
| 35 | m->dqb_curinodes = d->dqb_curinodes; |
Jan Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 36 | m->dqb_bhardlimit = v1_qbtos(d->dqb_bhardlimit); |
| 37 | m->dqb_bsoftlimit = v1_qbtos(d->dqb_bsoftlimit); |
| 38 | m->dqb_curspace = v1_qbtos(d->dqb_curblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | m->dqb_itime = d->dqb_itime; |
| 40 | m->dqb_btime = d->dqb_btime; |
| 41 | } |
| 42 | |
| 43 | static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m) |
| 44 | { |
| 45 | d->dqb_ihardlimit = m->dqb_ihardlimit; |
| 46 | d->dqb_isoftlimit = m->dqb_isoftlimit; |
| 47 | d->dqb_curinodes = m->dqb_curinodes; |
Jan Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 48 | d->dqb_bhardlimit = v1_stoqb(m->dqb_bhardlimit); |
| 49 | d->dqb_bsoftlimit = v1_stoqb(m->dqb_bsoftlimit); |
| 50 | d->dqb_curblocks = v1_stoqb(m->dqb_curspace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | d->dqb_itime = m->dqb_itime; |
| 52 | d->dqb_btime = m->dqb_btime; |
| 53 | } |
| 54 | |
| 55 | static int v1_read_dqblk(struct dquot *dquot) |
| 56 | { |
| 57 | int type = dquot->dq_type; |
| 58 | struct v1_disk_dqblk dqblk; |
| 59 | |
| 60 | if (!sb_dqopt(dquot->dq_sb)->files[type]) |
| 61 | return -EINVAL; |
| 62 | |
| 63 | /* Set structure to 0s in case read fails/is after end of file */ |
| 64 | memset(&dqblk, 0, sizeof(struct v1_disk_dqblk)); |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 65 | dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk, |
| 66 | sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk); |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 69 | if (dquot->dq_dqb.dqb_bhardlimit == 0 && |
| 70 | dquot->dq_dqb.dqb_bsoftlimit == 0 && |
| 71 | dquot->dq_dqb.dqb_ihardlimit == 0 && |
| 72 | dquot->dq_dqb.dqb_isoftlimit == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | set_bit(DQ_FAKE_B, &dquot->dq_flags); |
| 74 | dqstats.reads++; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int v1_commit_dqblk(struct dquot *dquot) |
| 80 | { |
| 81 | short type = dquot->dq_type; |
| 82 | ssize_t ret; |
| 83 | struct v1_disk_dqblk dqblk; |
| 84 | |
| 85 | v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb); |
| 86 | if (dquot->dq_id == 0) { |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 87 | dqblk.dqb_btime = |
| 88 | sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace; |
| 89 | dqblk.dqb_itime = |
| 90 | sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | ret = 0; |
| 93 | if (sb_dqopt(dquot->dq_sb)->files[type]) |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 94 | ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type, |
| 95 | (char *)&dqblk, sizeof(struct v1_disk_dqblk), |
| 96 | v1_dqoff(dquot->dq_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (ret != sizeof(struct v1_disk_dqblk)) { |
| 98 | printk(KERN_WARNING "VFS: dquota write failed on dev %s\n", |
| 99 | dquot->dq_sb->s_id); |
| 100 | if (ret >= 0) |
| 101 | ret = -EIO; |
| 102 | goto out; |
| 103 | } |
| 104 | ret = 0; |
| 105 | |
| 106 | out: |
| 107 | dqstats.writes++; |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | /* Magics of new quota format */ |
| 113 | #define V2_INITQMAGICS {\ |
| 114 | 0xd9c01f11, /* USRQUOTA */\ |
| 115 | 0xd9c01927 /* GRPQUOTA */\ |
| 116 | } |
| 117 | |
| 118 | /* Header of new quota format */ |
| 119 | struct v2_disk_dqheader { |
| 120 | __le32 dqh_magic; /* Magic number identifying file */ |
| 121 | __le32 dqh_version; /* File version */ |
| 122 | }; |
| 123 | |
| 124 | static int v1_check_quota_file(struct super_block *sb, int type) |
| 125 | { |
| 126 | struct inode *inode = sb_dqopt(sb)->files[type]; |
| 127 | ulong blocks; |
| 128 | size_t off; |
| 129 | struct v2_disk_dqheader dqhead; |
| 130 | ssize_t size; |
| 131 | loff_t isize; |
| 132 | static const uint quota_magics[] = V2_INITQMAGICS; |
| 133 | |
| 134 | isize = i_size_read(inode); |
| 135 | if (!isize) |
| 136 | return 0; |
| 137 | blocks = isize >> BLOCK_SIZE_BITS; |
| 138 | off = isize & (BLOCK_SIZE - 1); |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 139 | if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) % |
| 140 | sizeof(struct v1_disk_dqblk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return 0; |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 142 | /* Doublecheck whether we didn't get file with new format - with old |
| 143 | * quotactl() this could happen */ |
| 144 | size = sb->s_op->quota_read(sb, type, (char *)&dqhead, |
| 145 | sizeof(struct v2_disk_dqheader), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (size != sizeof(struct v2_disk_dqheader)) |
| 147 | return 1; /* Probably not new format */ |
| 148 | if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type]) |
| 149 | return 1; /* Definitely not new format */ |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 150 | printk(KERN_INFO |
| 151 | "VFS: %s: Refusing to turn on old quota format on given file." |
| 152 | " It probably contains newer quota format.\n", sb->s_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return 0; /* Seems like a new format file -> refuse it */ |
| 154 | } |
| 155 | |
| 156 | static int v1_read_file_info(struct super_block *sb, int type) |
| 157 | { |
| 158 | struct quota_info *dqopt = sb_dqopt(sb); |
| 159 | struct v1_disk_dqblk dqblk; |
| 160 | int ret; |
| 161 | |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 162 | ret = sb->s_op->quota_read(sb, type, (char *)&dqblk, |
| 163 | sizeof(struct v1_disk_dqblk), v1_dqoff(0)); |
| 164 | if (ret != sizeof(struct v1_disk_dqblk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (ret >= 0) |
| 166 | ret = -EIO; |
| 167 | goto out; |
| 168 | } |
| 169 | ret = 0; |
Andrew Perepechko | 338bf9a | 2008-04-28 02:14:31 -0700 | [diff] [blame] | 170 | /* limits are stored as unsigned 32-bit data */ |
| 171 | dqopt->info[type].dqi_maxblimit = 0xffffffff; |
| 172 | dqopt->info[type].dqi_maxilimit = 0xffffffff; |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 173 | dqopt->info[type].dqi_igrace = |
| 174 | dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME; |
| 175 | dqopt->info[type].dqi_bgrace = |
| 176 | dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | out: |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static int v1_write_file_info(struct super_block *sb, int type) |
| 182 | { |
| 183 | struct quota_info *dqopt = sb_dqopt(sb); |
| 184 | struct v1_disk_dqblk dqblk; |
| 185 | int ret; |
| 186 | |
| 187 | dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY; |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame^] | 188 | ret = sb->s_op->quota_read(sb, type, (char *)&dqblk, |
| 189 | sizeof(struct v1_disk_dqblk), v1_dqoff(0)); |
| 190 | if (ret != sizeof(struct v1_disk_dqblk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (ret >= 0) |
| 192 | ret = -EIO; |
| 193 | goto out; |
| 194 | } |
| 195 | dqblk.dqb_itime = dqopt->info[type].dqi_igrace; |
| 196 | dqblk.dqb_btime = dqopt->info[type].dqi_bgrace; |
| 197 | ret = sb->s_op->quota_write(sb, type, (char *)&dqblk, |
| 198 | sizeof(struct v1_disk_dqblk), v1_dqoff(0)); |
| 199 | if (ret == sizeof(struct v1_disk_dqblk)) |
| 200 | ret = 0; |
| 201 | else if (ret > 0) |
| 202 | ret = -EIO; |
| 203 | out: |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | static struct quota_format_ops v1_format_ops = { |
| 208 | .check_quota_file = v1_check_quota_file, |
| 209 | .read_file_info = v1_read_file_info, |
| 210 | .write_file_info = v1_write_file_info, |
| 211 | .free_file_info = NULL, |
| 212 | .read_dqblk = v1_read_dqblk, |
| 213 | .commit_dqblk = v1_commit_dqblk, |
| 214 | }; |
| 215 | |
| 216 | static struct quota_format_type v1_quota_format = { |
| 217 | .qf_fmt_id = QFMT_VFS_OLD, |
| 218 | .qf_ops = &v1_format_ops, |
| 219 | .qf_owner = THIS_MODULE |
| 220 | }; |
| 221 | |
| 222 | static int __init init_v1_quota_format(void) |
| 223 | { |
| 224 | return register_quota_format(&v1_quota_format); |
| 225 | } |
| 226 | |
| 227 | static void __exit exit_v1_quota_format(void) |
| 228 | { |
| 229 | unregister_quota_format(&v1_quota_format); |
| 230 | } |
| 231 | |
| 232 | module_init(init_v1_quota_format); |
| 233 | module_exit(exit_v1_quota_format); |
| 234 | |