Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * vfsv0 quota IO operations on file |
| 4 | */ |
| 5 | |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/fs.h> |
| 8 | #include <linux/mount.h> |
| 9 | #include <linux/dqblk_v2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/slab.h> |
Jan Kara | 74abb98 | 2008-07-25 01:46:51 -0700 | [diff] [blame] | 14 | #include <linux/quotaops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <asm/byteorder.h> |
| 17 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 18 | #include "quota_tree.h" |
Jan Kara | cf770c1 | 2008-09-21 23:17:53 +0200 | [diff] [blame] | 19 | #include "quotaio_v2.h" |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | MODULE_AUTHOR("Jan Kara"); |
| 22 | MODULE_DESCRIPTION("Quota format v2 support"); |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 25 | static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot); |
| 26 | static void v2r0_disk2memdqb(struct dquot *dquot, void *dp); |
| 27 | static int v2r0_is_id(void *dp, struct dquot *dquot); |
| 28 | static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot); |
| 29 | static void v2r1_disk2memdqb(struct dquot *dquot, void *dp); |
| 30 | static int v2r1_is_id(void *dp, struct dquot *dquot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Julia Lawall | d1b98c2 | 2016-01-01 08:53:37 +0100 | [diff] [blame] | 32 | static const struct qtree_fmt_operations v2r0_qtree_ops = { |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 33 | .mem2disk_dqblk = v2r0_mem2diskdqb, |
| 34 | .disk2mem_dqblk = v2r0_disk2memdqb, |
| 35 | .is_id = v2r0_is_id, |
| 36 | }; |
| 37 | |
Julia Lawall | d1b98c2 | 2016-01-01 08:53:37 +0100 | [diff] [blame] | 38 | static const struct qtree_fmt_operations v2r1_qtree_ops = { |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 39 | .mem2disk_dqblk = v2r1_mem2diskdqb, |
| 40 | .disk2mem_dqblk = v2r1_disk2memdqb, |
| 41 | .is_id = v2r1_is_id, |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 42 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Jan Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 44 | #define QUOTABLOCK_BITS 10 |
| 45 | #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS) |
| 46 | |
| 47 | static inline qsize_t v2_stoqb(qsize_t space) |
| 48 | { |
| 49 | return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS; |
| 50 | } |
| 51 | |
| 52 | static inline qsize_t v2_qbtos(qsize_t blocks) |
| 53 | { |
| 54 | return blocks << QUOTABLOCK_BITS; |
| 55 | } |
| 56 | |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 57 | static int v2_read_header(struct super_block *sb, int type, |
| 58 | struct v2_disk_dqheader *dqhead) |
| 59 | { |
| 60 | ssize_t size; |
| 61 | |
| 62 | size = sb->s_op->quota_read(sb, type, (char *)dqhead, |
| 63 | sizeof(struct v2_disk_dqheader), 0); |
| 64 | if (size != sizeof(struct v2_disk_dqheader)) { |
Jiaying Zhang | fb5ffb0 | 2010-07-20 16:54:43 +0200 | [diff] [blame] | 65 | quota_error(sb, "Failed header read: expected=%zd got=%zd", |
| 66 | sizeof(struct v2_disk_dqheader), size); |
Jan Kara | f98bbe3 | 2017-08-17 19:20:28 +0200 | [diff] [blame] | 67 | if (size < 0) |
| 68 | return size; |
| 69 | return -EIO; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 70 | } |
Jan Kara | f98bbe3 | 2017-08-17 19:20:28 +0200 | [diff] [blame] | 71 | return 0; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* Check whether given file is really vfsv0 quotafile */ |
| 75 | static int v2_check_quota_file(struct super_block *sb, int type) |
| 76 | { |
| 77 | struct v2_disk_dqheader dqhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | static const uint quota_magics[] = V2_INITQMAGICS; |
| 79 | static const uint quota_versions[] = V2_INITQVERSIONS; |
Sascha Hauer | 27942ef | 2019-03-26 08:54:31 +0100 | [diff] [blame] | 80 | |
Jan Kara | f98bbe3 | 2017-08-17 19:20:28 +0200 | [diff] [blame] | 81 | if (v2_read_header(sb, type, &dqhead)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] || |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 84 | le32_to_cpu(dqhead.dqh_version) > quota_versions[type]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return 0; |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | /* Read information header from quota file */ |
| 90 | static int v2_read_file_info(struct super_block *sb, int type) |
| 91 | { |
| 92 | struct v2_disk_dqinfo dinfo; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 93 | struct v2_disk_dqheader dqhead; |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 94 | struct quota_info *dqopt = sb_dqopt(sb); |
| 95 | struct mem_dqinfo *info = &dqopt->info[type]; |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 96 | struct qtree_mem_dqinfo *qinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | ssize_t size; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 98 | unsigned int version; |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 99 | int ret; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 100 | |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 101 | down_read(&dqopt->dqio_sem); |
Jan Kara | f98bbe3 | 2017-08-17 19:20:28 +0200 | [diff] [blame] | 102 | ret = v2_read_header(sb, type, &dqhead); |
| 103 | if (ret < 0) |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 104 | goto out; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 105 | version = le32_to_cpu(dqhead.dqh_version); |
Jan Kara | 869835d | 2009-12-21 21:57:04 +0100 | [diff] [blame] | 106 | if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) || |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 107 | (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1)) { |
Jan Kara | cb8d01b | 2017-06-09 09:04:47 +0200 | [diff] [blame] | 108 | ret = -EINVAL; |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 109 | goto out; |
| 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | size = sb->s_op->quota_read(sb, type, (char *)&dinfo, |
| 113 | sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF); |
| 114 | if (size != sizeof(struct v2_disk_dqinfo)) { |
Jiaying Zhang | fb5ffb0 | 2010-07-20 16:54:43 +0200 | [diff] [blame] | 115 | quota_error(sb, "Can't read info structure"); |
Jan Kara | f98bbe3 | 2017-08-17 19:20:28 +0200 | [diff] [blame] | 116 | if (size < 0) |
| 117 | ret = size; |
| 118 | else |
| 119 | ret = -EIO; |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 120 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 122 | info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS); |
| 123 | if (!info->dqi_priv) { |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 124 | ret = -ENOMEM; |
| 125 | goto out; |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 126 | } |
| 127 | qinfo = info->dqi_priv; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 128 | if (version == 0) { |
| 129 | /* limits are stored as unsigned 32-bit data */ |
Jan Kara | 7e08da5 | 2015-03-04 14:42:02 +0100 | [diff] [blame] | 130 | info->dqi_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS; |
Jan Kara | b10a081 | 2014-10-09 16:54:13 +0200 | [diff] [blame] | 131 | info->dqi_max_ino_limit = 0xffffffff; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 132 | } else { |
Jan Kara | 7e08da5 | 2015-03-04 14:42:02 +0100 | [diff] [blame] | 133 | /* |
| 134 | * Used space is stored as unsigned 64-bit value in bytes but |
| 135 | * quota core supports only signed 64-bit values so use that |
| 136 | * as a limit |
| 137 | */ |
| 138 | info->dqi_max_spc_limit = 0x7fffffffffffffffLL; /* 2^63-1 */ |
| 139 | info->dqi_max_ino_limit = 0x7fffffffffffffffLL; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace); |
| 142 | info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace); |
Jan Kara | c119c5b | 2014-11-19 09:03:28 +0100 | [diff] [blame] | 143 | /* No flags currently supported */ |
| 144 | info->dqi_flags = 0; |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 145 | qinfo->dqi_sb = sb; |
| 146 | qinfo->dqi_type = type; |
| 147 | qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks); |
| 148 | qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk); |
| 149 | qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry); |
| 150 | qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS; |
| 151 | qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS; |
| 152 | qinfo->dqi_qtree_depth = qtree_depth(qinfo); |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 153 | if (version == 0) { |
| 154 | qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk); |
| 155 | qinfo->dqi_ops = &v2r0_qtree_ops; |
| 156 | } else { |
| 157 | qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk); |
| 158 | qinfo->dqi_ops = &v2r1_qtree_ops; |
| 159 | } |
Jan Kara | 42fdb85 | 2017-06-09 08:59:46 +0200 | [diff] [blame] | 160 | ret = 0; |
| 161 | out: |
| 162 | up_read(&dqopt->dqio_sem); |
| 163 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* Write information header to quota file */ |
| 167 | static int v2_write_file_info(struct super_block *sb, int type) |
| 168 | { |
| 169 | struct v2_disk_dqinfo dinfo; |
Jan Kara | 9a8ae30 | 2017-06-09 08:45:43 +0200 | [diff] [blame] | 170 | struct quota_info *dqopt = sb_dqopt(sb); |
| 171 | struct mem_dqinfo *info = &dqopt->info[type]; |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 172 | struct qtree_mem_dqinfo *qinfo = info->dqi_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | ssize_t size; |
| 174 | |
Jan Kara | 9a8ae30 | 2017-06-09 08:45:43 +0200 | [diff] [blame] | 175 | down_write(&dqopt->dqio_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | spin_lock(&dq_data_lock); |
| 177 | info->dqi_flags &= ~DQF_INFO_DIRTY; |
| 178 | dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace); |
| 179 | dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace); |
Jan Kara | c119c5b | 2014-11-19 09:03:28 +0100 | [diff] [blame] | 180 | /* No flags currently supported */ |
| 181 | dinfo.dqi_flags = cpu_to_le32(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | spin_unlock(&dq_data_lock); |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 183 | dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks); |
| 184 | dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk); |
| 185 | dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | size = sb->s_op->quota_write(sb, type, (char *)&dinfo, |
| 187 | sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF); |
Jan Kara | 9a8ae30 | 2017-06-09 08:45:43 +0200 | [diff] [blame] | 188 | up_write(&dqopt->dqio_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (size != sizeof(struct v2_disk_dqinfo)) { |
Jiaying Zhang | fb5ffb0 | 2010-07-20 16:54:43 +0200 | [diff] [blame] | 190 | quota_error(sb, "Can't write info structure"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 196 | static void v2r0_disk2memdqb(struct dquot *dquot, void *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 198 | struct v2r0_disk_dqblk *d = dp, empty; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 199 | struct mem_dqblk *m = &dquot->dq_dqb; |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit); |
| 202 | m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit); |
| 203 | m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes); |
| 204 | m->dqb_itime = le64_to_cpu(d->dqb_itime); |
Jan Kara | 1209546 | 2008-08-20 14:45:12 +0200 | [diff] [blame] | 205 | m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit)); |
| 206 | m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | m->dqb_curspace = le64_to_cpu(d->dqb_curspace); |
| 208 | m->dqb_btime = le64_to_cpu(d->dqb_btime); |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 209 | /* We need to escape back all-zero structure */ |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 210 | memset(&empty, 0, sizeof(struct v2r0_disk_dqblk)); |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 211 | empty.dqb_itime = cpu_to_le64(1); |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 212 | if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk))) |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 213 | m->dqb_itime = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 216 | static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 218 | struct v2r0_disk_dqblk *d = dp; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 219 | struct mem_dqblk *m = &dquot->dq_dqb; |
| 220 | struct qtree_mem_dqinfo *info = |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 221 | sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit); |
| 224 | d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit); |
| 225 | d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes); |
| 226 | d->dqb_itime = cpu_to_le64(m->dqb_itime); |
Jan Kara | cf770c1 | 2008-09-21 23:17:53 +0200 | [diff] [blame] | 227 | d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit)); |
| 228 | d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | d->dqb_curspace = cpu_to_le64(m->dqb_curspace); |
| 230 | d->dqb_btime = cpu_to_le64(m->dqb_btime); |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 231 | d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id)); |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 232 | if (qtree_entry_unused(info, dp)) |
| 233 | d->dqb_itime = cpu_to_le64(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 236 | static int v2r0_is_id(void *dp, struct dquot *dquot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 238 | struct v2r0_disk_dqblk *d = dp; |
| 239 | struct qtree_mem_dqinfo *info = |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 240 | sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 241 | |
| 242 | if (qtree_entry_unused(info, dp)) |
| 243 | return 0; |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 244 | return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type, |
| 245 | le32_to_cpu(d->dqb_id)), |
| 246 | dquot->dq_id); |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void v2r1_disk2memdqb(struct dquot *dquot, void *dp) |
| 250 | { |
| 251 | struct v2r1_disk_dqblk *d = dp, empty; |
| 252 | struct mem_dqblk *m = &dquot->dq_dqb; |
| 253 | |
| 254 | m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit); |
| 255 | m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit); |
| 256 | m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes); |
| 257 | m->dqb_itime = le64_to_cpu(d->dqb_itime); |
| 258 | m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit)); |
| 259 | m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit)); |
| 260 | m->dqb_curspace = le64_to_cpu(d->dqb_curspace); |
| 261 | m->dqb_btime = le64_to_cpu(d->dqb_btime); |
| 262 | /* We need to escape back all-zero structure */ |
| 263 | memset(&empty, 0, sizeof(struct v2r1_disk_dqblk)); |
| 264 | empty.dqb_itime = cpu_to_le64(1); |
| 265 | if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk))) |
| 266 | m->dqb_itime = 0; |
| 267 | } |
| 268 | |
| 269 | static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot) |
| 270 | { |
| 271 | struct v2r1_disk_dqblk *d = dp; |
| 272 | struct mem_dqblk *m = &dquot->dq_dqb; |
| 273 | struct qtree_mem_dqinfo *info = |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 274 | sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 275 | |
| 276 | d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit); |
| 277 | d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit); |
| 278 | d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes); |
| 279 | d->dqb_itime = cpu_to_le64(m->dqb_itime); |
| 280 | d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit)); |
| 281 | d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit)); |
| 282 | d->dqb_curspace = cpu_to_le64(m->dqb_curspace); |
| 283 | d->dqb_btime = cpu_to_le64(m->dqb_btime); |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 284 | d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id)); |
Eric Dumazet | 3d3dc27 | 2020-09-24 11:36:19 -0700 | [diff] [blame^] | 285 | d->dqb_pad = 0; |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 286 | if (qtree_entry_unused(info, dp)) |
| 287 | d->dqb_itime = cpu_to_le64(1); |
| 288 | } |
| 289 | |
| 290 | static int v2r1_is_id(void *dp, struct dquot *dquot) |
| 291 | { |
| 292 | struct v2r1_disk_dqblk *d = dp; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 293 | struct qtree_mem_dqinfo *info = |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 294 | sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 296 | if (qtree_entry_unused(info, dp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return 0; |
Eric W. Biederman | 4c376dc | 2012-09-16 03:56:19 -0700 | [diff] [blame] | 298 | return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type, |
| 299 | le32_to_cpu(d->dqb_id)), |
| 300 | dquot->dq_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static int v2_read_dquot(struct dquot *dquot) |
| 304 | { |
Jan Kara | e342e38 | 2017-06-08 15:30:45 +0200 | [diff] [blame] | 305 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); |
| 306 | int ret; |
| 307 | |
| 308 | down_read(&dqopt->dqio_sem); |
| 309 | ret = qtree_read_dquot( |
| 310 | sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, |
| 311 | dquot); |
| 312 | up_read(&dqopt->dqio_sem); |
| 313 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 316 | static int v2_write_dquot(struct dquot *dquot) |
| 317 | { |
Jan Kara | 8fc32c2 | 2017-06-08 15:48:16 +0200 | [diff] [blame] | 318 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); |
| 319 | int ret; |
Jan Kara | d2faa41 | 2017-06-08 16:09:46 +0200 | [diff] [blame] | 320 | bool alloc = false; |
Jan Kara | 8fc32c2 | 2017-06-08 15:48:16 +0200 | [diff] [blame] | 321 | |
Jan Kara | d2faa41 | 2017-06-08 16:09:46 +0200 | [diff] [blame] | 322 | /* |
| 323 | * If space for dquot is already allocated, we don't need any |
| 324 | * protection as we'll only overwrite the place of dquot. We are |
| 325 | * still protected by concurrent writes of the same dquot by |
| 326 | * dquot->dq_lock. |
| 327 | */ |
| 328 | if (!dquot->dq_off) { |
| 329 | alloc = true; |
| 330 | down_write(&dqopt->dqio_sem); |
Jan Kara | 4c6bb69 | 2017-09-26 10:36:05 +0200 | [diff] [blame] | 331 | } else { |
| 332 | down_read(&dqopt->dqio_sem); |
Jan Kara | d2faa41 | 2017-06-08 16:09:46 +0200 | [diff] [blame] | 333 | } |
Jan Kara | 8fc32c2 | 2017-06-08 15:48:16 +0200 | [diff] [blame] | 334 | ret = qtree_write_dquot( |
| 335 | sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, |
| 336 | dquot); |
Jan Kara | d2faa41 | 2017-06-08 16:09:46 +0200 | [diff] [blame] | 337 | if (alloc) |
| 338 | up_write(&dqopt->dqio_sem); |
Jan Kara | 4c6bb69 | 2017-09-26 10:36:05 +0200 | [diff] [blame] | 339 | else |
| 340 | up_read(&dqopt->dqio_sem); |
Jan Kara | 8fc32c2 | 2017-06-08 15:48:16 +0200 | [diff] [blame] | 341 | return ret; |
Jan Kara | 1ccd14b | 2008-09-22 05:54:49 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | static int v2_release_dquot(struct dquot *dquot) |
| 345 | { |
Jan Kara | b9a1a7f | 2017-06-08 17:20:36 +0200 | [diff] [blame] | 346 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb); |
| 347 | int ret; |
| 348 | |
| 349 | down_write(&dqopt->dqio_sem); |
| 350 | ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot); |
| 351 | up_write(&dqopt->dqio_sem); |
| 352 | |
| 353 | return ret; |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static int v2_free_file_info(struct super_block *sb, int type) |
| 357 | { |
| 358 | kfree(sb_dqinfo(sb, type)->dqi_priv); |
| 359 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Jan Kara | 0066373 | 2016-01-25 20:39:27 +0100 | [diff] [blame] | 362 | static int v2_get_next_id(struct super_block *sb, struct kqid *qid) |
| 363 | { |
Jan Kara | f14618c | 2017-06-09 08:36:16 +0200 | [diff] [blame] | 364 | struct quota_info *dqopt = sb_dqopt(sb); |
| 365 | int ret; |
| 366 | |
| 367 | down_read(&dqopt->dqio_sem); |
| 368 | ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid); |
| 369 | up_read(&dqopt->dqio_sem); |
| 370 | return ret; |
Jan Kara | 0066373 | 2016-01-25 20:39:27 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Alexey Dobriyan | 1472da5 | 2009-10-16 15:26:03 +0400 | [diff] [blame] | 373 | static const struct quota_format_ops v2_format_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | .check_quota_file = v2_check_quota_file, |
| 375 | .read_file_info = v2_read_file_info, |
| 376 | .write_file_info = v2_write_file_info, |
Jan Kara | e3d4d56 | 2008-10-02 18:44:14 +0200 | [diff] [blame] | 377 | .free_file_info = v2_free_file_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | .read_dqblk = v2_read_dquot, |
| 379 | .commit_dqblk = v2_write_dquot, |
| 380 | .release_dqblk = v2_release_dquot, |
Jan Kara | 0066373 | 2016-01-25 20:39:27 +0100 | [diff] [blame] | 381 | .get_next_id = v2_get_next_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | }; |
| 383 | |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 384 | static struct quota_format_type v2r0_quota_format = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | .qf_fmt_id = QFMT_VFS_V0, |
| 386 | .qf_ops = &v2_format_ops, |
| 387 | .qf_owner = THIS_MODULE |
| 388 | }; |
| 389 | |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 390 | static struct quota_format_type v2r1_quota_format = { |
| 391 | .qf_fmt_id = QFMT_VFS_V1, |
| 392 | .qf_ops = &v2_format_ops, |
| 393 | .qf_owner = THIS_MODULE |
| 394 | }; |
| 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | static int __init init_v2_quota_format(void) |
| 397 | { |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 398 | int ret; |
| 399 | |
| 400 | ret = register_quota_format(&v2r0_quota_format); |
| 401 | if (ret) |
| 402 | return ret; |
| 403 | return register_quota_format(&v2r1_quota_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static void __exit exit_v2_quota_format(void) |
| 407 | { |
Jan Kara | 498c601 | 2009-11-16 18:09:47 +0100 | [diff] [blame] | 408 | unregister_quota_format(&v2r0_quota_format); |
| 409 | unregister_quota_format(&v2r1_quota_format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | module_init(init_v2_quota_format); |
| 413 | module_exit(exit_v2_quota_format); |