Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Quota code necessary even when VFS quota support is not compiled |
| 3 | * into the kernel. The interesting stuff is over in dquot.c, here |
| 4 | * we have symbols for initial quotactl(2) handling, the sysctl(2) |
| 5 | * variables, etc - things needed even when quota support disabled. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/namei.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <asm/current.h> |
| 12 | #include <asm/uaccess.h> |
Vasily Tarasov | b716395 | 2007-07-15 23:41:12 -0700 | [diff] [blame] | 13 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/security.h> |
| 16 | #include <linux/syscalls.h> |
| 17 | #include <linux/buffer_head.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | #include <linux/capability.h> |
Adrian Bunk | be586ba | 2005-11-07 00:59:35 -0800 | [diff] [blame] | 19 | #include <linux/quotaops.h> |
Vasily Tarasov | b716395 | 2007-07-15 23:41:12 -0700 | [diff] [blame] | 20 | #include <linux/types.h> |
Steven Whitehouse | 86e931a | 2009-09-28 12:35:17 +0100 | [diff] [blame] | 21 | #include <net/netlink.h> |
| 22 | #include <net/genetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /* Check validity of generic quotactl commands */ |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 25 | static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, |
| 26 | qid_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | if (type >= MAXQUOTAS) |
| 29 | return -EINVAL; |
| 30 | if (!sb && cmd != Q_SYNC) |
| 31 | return -ENODEV; |
| 32 | /* Is operation supported? */ |
| 33 | if (sb && !sb->s_qcop) |
| 34 | return -ENOSYS; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* Check privileges */ |
| 37 | if (cmd == Q_GETQUOTA) { |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 38 | if (((type == USRQUOTA && current_euid() != id) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | (type == GRPQUOTA && !in_egroup_p(id))) && |
| 40 | !capable(CAP_SYS_ADMIN)) |
| 41 | return -EPERM; |
| 42 | } |
| 43 | else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO) |
| 44 | if (!capable(CAP_SYS_ADMIN)) |
| 45 | return -EPERM; |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /* Check validity of XFS Quota Manager commands */ |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 51 | static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, |
| 52 | qid_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
| 54 | if (type >= XQM_MAXQUOTAS) |
| 55 | return -EINVAL; |
| 56 | if (!sb) |
| 57 | return -ENODEV; |
| 58 | if (!sb->s_qcop) |
| 59 | return -ENOSYS; |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* Check privileges */ |
| 62 | if (cmd == Q_XGETQUOTA) { |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 63 | if (((type == XQM_USRQUOTA && current_euid() != id) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | (type == XQM_GRPQUOTA && !in_egroup_p(id))) && |
| 65 | !capable(CAP_SYS_ADMIN)) |
| 66 | return -EPERM; |
Nathan Scott | de69e5f | 2005-11-03 13:53:34 +1100 | [diff] [blame] | 67 | } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (!capable(CAP_SYS_ADMIN)) |
| 69 | return -EPERM; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 75 | static int check_quotactl_valid(struct super_block *sb, int type, int cmd, |
| 76 | qid_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | int error; |
| 79 | |
| 80 | if (XQM_COMMAND(cmd)) |
| 81 | error = xqm_quotactl_valid(sb, type, cmd, id); |
| 82 | else |
| 83 | error = generic_quotactl_valid(sb, type, cmd, id); |
| 84 | if (!error) |
| 85 | error = security_quotactl(cmd, type, id, sb); |
| 86 | return error; |
| 87 | } |
| 88 | |
Christoph Hellwig | 850b201 | 2009-04-27 16:43:54 +0200 | [diff] [blame] | 89 | #ifdef CONFIG_QUOTA |
| 90 | void sync_quota_sb(struct super_block *sb, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
| 92 | int cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Christoph Hellwig | 850b201 | 2009-04-27 16:43:54 +0200 | [diff] [blame] | 94 | if (!sb->s_qcop->quota_sync) |
| 95 | return; |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | sb->s_qcop->quota_sync(sb, type); |
Jan Kara | ca785ec | 2008-09-30 17:53:37 +0200 | [diff] [blame] | 98 | |
| 99 | if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE) |
| 100 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* This is not very clever (and fast) but currently I don't know about |
| 102 | * any other simple way of getting quota data to disk and we must get |
| 103 | * them there for userspace to be visible... */ |
| 104 | if (sb->s_op->sync_fs) |
| 105 | sb->s_op->sync_fs(sb, 1); |
| 106 | sync_blockdev(sb->s_bdev); |
| 107 | |
Jan Kara | 7925409 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 108 | /* |
| 109 | * Now when everything is written we can discard the pagecache so |
| 110 | * that userspace sees the changes. |
| 111 | */ |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 112 | mutex_lock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if (type != -1 && cnt != type) |
| 115 | continue; |
Jan Kara | f55abc0 | 2008-08-20 17:50:32 +0200 | [diff] [blame] | 116 | if (!sb_has_quota_active(sb, cnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | continue; |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 118 | mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex, |
| 119 | I_MUTEX_QUOTA); |
Jan Kara | 7925409 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 120 | truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0); |
| 121 | mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
Ingo Molnar | d3be915 | 2006-03-23 03:00:29 -0800 | [diff] [blame] | 123 | mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
Christoph Hellwig | 850b201 | 2009-04-27 16:43:54 +0200 | [diff] [blame] | 125 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Christoph Hellwig | 850b201 | 2009-04-27 16:43:54 +0200 | [diff] [blame] | 127 | static void sync_dquots(int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Christoph Hellwig | 850b201 | 2009-04-27 16:43:54 +0200 | [diff] [blame] | 129 | struct super_block *sb; |
Jan Kara | 02a55ca | 2008-07-25 01:46:50 -0700 | [diff] [blame] | 130 | int cnt; |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 131 | |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 132 | spin_lock(&sb_lock); |
| 133 | restart: |
| 134 | list_for_each_entry(sb, &super_blocks, s_list) { |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 135 | /* This test just improves performance so it needn't be |
| 136 | * reliable... */ |
Jan Kara | 02a55ca | 2008-07-25 01:46:50 -0700 | [diff] [blame] | 137 | for (cnt = 0; cnt < MAXQUOTAS; cnt++) { |
| 138 | if (type != -1 && type != cnt) |
| 139 | continue; |
Jan Kara | f55abc0 | 2008-08-20 17:50:32 +0200 | [diff] [blame] | 140 | if (!sb_has_quota_active(sb, cnt)) |
Jan Kara | 02a55ca | 2008-07-25 01:46:50 -0700 | [diff] [blame] | 141 | continue; |
| 142 | if (!info_dirty(&sb_dqopt(sb)->info[cnt]) && |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 143 | list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list)) |
Jan Kara | 02a55ca | 2008-07-25 01:46:50 -0700 | [diff] [blame] | 144 | continue; |
| 145 | break; |
| 146 | } |
| 147 | if (cnt == MAXQUOTAS) |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 148 | continue; |
| 149 | sb->s_count++; |
| 150 | spin_unlock(&sb_lock); |
| 151 | down_read(&sb->s_umount); |
Christoph Hellwig | 850b201 | 2009-04-27 16:43:54 +0200 | [diff] [blame] | 152 | if (sb->s_root) |
| 153 | sync_quota_sb(sb, type); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 154 | up_read(&sb->s_umount); |
| 155 | spin_lock(&sb_lock); |
| 156 | if (__put_super_and_need_restart(sb)) |
| 157 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 159 | spin_unlock(&sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 162 | static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id, |
| 163 | void __user *addr) |
| 164 | { |
| 165 | char *pathname; |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 166 | int ret = -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 167 | |
| 168 | pathname = getname(addr); |
| 169 | if (IS_ERR(pathname)) |
| 170 | return PTR_ERR(pathname); |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 171 | if (sb->s_qcop->quota_on) |
| 172 | ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 173 | putname(pathname); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | static int quota_getfmt(struct super_block *sb, int type, void __user *addr) |
| 178 | { |
| 179 | __u32 fmt; |
| 180 | |
| 181 | down_read(&sb_dqopt(sb)->dqptr_sem); |
| 182 | if (!sb_has_quota_active(sb, type)) { |
| 183 | up_read(&sb_dqopt(sb)->dqptr_sem); |
| 184 | return -ESRCH; |
| 185 | } |
| 186 | fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id; |
| 187 | up_read(&sb_dqopt(sb)->dqptr_sem); |
| 188 | if (copy_to_user(addr, &fmt, sizeof(fmt))) |
| 189 | return -EFAULT; |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int quota_getinfo(struct super_block *sb, int type, void __user *addr) |
| 194 | { |
| 195 | struct if_dqinfo info; |
| 196 | int ret; |
| 197 | |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 198 | if (!sb_has_quota_active(sb, type)) |
| 199 | return -ESRCH; |
| 200 | if (!sb->s_qcop->get_info) |
| 201 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 202 | ret = sb->s_qcop->get_info(sb, type, &info); |
| 203 | if (!ret && copy_to_user(addr, &info, sizeof(info))) |
| 204 | return -EFAULT; |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static int quota_setinfo(struct super_block *sb, int type, void __user *addr) |
| 209 | { |
| 210 | struct if_dqinfo info; |
| 211 | |
| 212 | if (copy_from_user(&info, addr, sizeof(info))) |
| 213 | return -EFAULT; |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 214 | if (!sb_has_quota_active(sb, type)) |
| 215 | return -ESRCH; |
| 216 | if (!sb->s_qcop->set_info) |
| 217 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 218 | return sb->s_qcop->set_info(sb, type, &info); |
| 219 | } |
| 220 | |
| 221 | static int quota_getquota(struct super_block *sb, int type, qid_t id, |
| 222 | void __user *addr) |
| 223 | { |
| 224 | struct if_dqblk idq; |
| 225 | int ret; |
| 226 | |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 227 | if (!sb_has_quota_active(sb, type)) |
| 228 | return -ESRCH; |
| 229 | if (!sb->s_qcop->get_dqblk) |
| 230 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 231 | ret = sb->s_qcop->get_dqblk(sb, type, id, &idq); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | if (copy_to_user(addr, &idq, sizeof(idq))) |
| 235 | return -EFAULT; |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int quota_setquota(struct super_block *sb, int type, qid_t id, |
| 240 | void __user *addr) |
| 241 | { |
| 242 | struct if_dqblk idq; |
| 243 | |
| 244 | if (copy_from_user(&idq, addr, sizeof(idq))) |
| 245 | return -EFAULT; |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 246 | if (!sb_has_quota_active(sb, type)) |
| 247 | return -ESRCH; |
| 248 | if (!sb->s_qcop->set_dqblk) |
| 249 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 250 | return sb->s_qcop->set_dqblk(sb, type, id, &idq); |
| 251 | } |
| 252 | |
| 253 | static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr) |
| 254 | { |
| 255 | __u32 flags; |
| 256 | |
| 257 | if (copy_from_user(&flags, addr, sizeof(flags))) |
| 258 | return -EFAULT; |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 259 | if (!sb->s_qcop->set_xstate) |
| 260 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 261 | return sb->s_qcop->set_xstate(sb, flags, cmd); |
| 262 | } |
| 263 | |
| 264 | static int quota_getxstate(struct super_block *sb, void __user *addr) |
| 265 | { |
| 266 | struct fs_quota_stat fqs; |
| 267 | int ret; |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 268 | |
| 269 | if (!sb->s_qcop->get_xstate) |
| 270 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 271 | ret = sb->s_qcop->get_xstate(sb, &fqs); |
| 272 | if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) |
| 273 | return -EFAULT; |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | static int quota_setxquota(struct super_block *sb, int type, qid_t id, |
| 278 | void __user *addr) |
| 279 | { |
| 280 | struct fs_disk_quota fdq; |
| 281 | |
| 282 | if (copy_from_user(&fdq, addr, sizeof(fdq))) |
| 283 | return -EFAULT; |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 284 | if (!sb->s_qcop->set_xquota) |
| 285 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 286 | return sb->s_qcop->set_xquota(sb, type, id, &fdq); |
| 287 | } |
| 288 | |
| 289 | static int quota_getxquota(struct super_block *sb, int type, qid_t id, |
| 290 | void __user *addr) |
| 291 | { |
| 292 | struct fs_disk_quota fdq; |
| 293 | int ret; |
| 294 | |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 295 | if (!sb->s_qcop->get_xquota) |
| 296 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 297 | ret = sb->s_qcop->get_xquota(sb, type, id, &fdq); |
| 298 | if (!ret && copy_to_user(addr, &fdq, sizeof(fdq))) |
| 299 | return -EFAULT; |
| 300 | return ret; |
| 301 | } |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | /* Copy parameters and call proper function */ |
Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 304 | static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, |
| 305 | void __user *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | switch (cmd) { |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 308 | case Q_QUOTAON: |
| 309 | return quota_quotaon(sb, type, cmd, id, addr); |
| 310 | case Q_QUOTAOFF: |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 311 | if (!sb->s_qcop->quota_off) |
| 312 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 313 | return sb->s_qcop->quota_off(sb, type, 0); |
| 314 | case Q_GETFMT: |
| 315 | return quota_getfmt(sb, type, addr); |
| 316 | case Q_GETINFO: |
| 317 | return quota_getinfo(sb, type, addr); |
| 318 | case Q_SETINFO: |
| 319 | return quota_setinfo(sb, type, addr); |
| 320 | case Q_GETQUOTA: |
| 321 | return quota_getquota(sb, type, id, addr); |
| 322 | case Q_SETQUOTA: |
| 323 | return quota_setquota(sb, type, id, addr); |
| 324 | case Q_SYNC: |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 325 | if (sb) { |
| 326 | if (!sb->s_qcop->quota_sync) |
| 327 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 328 | sync_quota_sb(sb, type); |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 329 | } else |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 330 | sync_dquots(type); |
| 331 | return 0; |
| 332 | case Q_XQUOTAON: |
| 333 | case Q_XQUOTAOFF: |
| 334 | case Q_XQUOTARM: |
| 335 | return quota_setxstate(sb, cmd, addr); |
| 336 | case Q_XGETQSTAT: |
| 337 | return quota_getxstate(sb, addr); |
| 338 | case Q_XSETQLIM: |
| 339 | return quota_setxquota(sb, type, id, addr); |
| 340 | case Q_XGETQUOTA: |
| 341 | return quota_getxquota(sb, type, id, addr); |
| 342 | case Q_XQUOTASYNC: |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 343 | if (!sb->s_qcop->quota_sync) |
| 344 | return -ENOSYS; |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 345 | return sb->s_qcop->quota_sync(sb, type); |
Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 346 | default: |
Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame^] | 347 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 352 | * look up a superblock on which quota ops will be performed |
| 353 | * - use the name of a block device to find the superblock thereon |
| 354 | */ |
Jan Kara | 7a2435d | 2009-01-27 01:47:11 +0100 | [diff] [blame] | 355 | static struct super_block *quotactl_block(const char __user *special) |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 356 | { |
| 357 | #ifdef CONFIG_BLOCK |
| 358 | struct block_device *bdev; |
| 359 | struct super_block *sb; |
| 360 | char *tmp = getname(special); |
| 361 | |
| 362 | if (IS_ERR(tmp)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 363 | return ERR_CAST(tmp); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 364 | bdev = lookup_bdev(tmp); |
| 365 | putname(tmp); |
| 366 | if (IS_ERR(bdev)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 367 | return ERR_CAST(bdev); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 368 | sb = get_super(bdev); |
| 369 | bdput(bdev); |
| 370 | if (!sb) |
| 371 | return ERR_PTR(-ENODEV); |
| 372 | |
| 373 | return sb; |
| 374 | #else |
| 375 | return ERR_PTR(-ENODEV); |
| 376 | #endif |
| 377 | } |
| 378 | |
| 379 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * This is the system call interface. This communicates with |
| 381 | * the user-level programs. Currently this only supports diskquota |
| 382 | * calls. Maybe we need to add the process quotas etc. in the future, |
| 383 | * but we probably should use rlimits for that. |
| 384 | */ |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 385 | SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special, |
| 386 | qid_t, id, void __user *, addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
| 388 | uint cmds, type; |
| 389 | struct super_block *sb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | int ret; |
| 391 | |
| 392 | cmds = cmd >> SUBCMDSHIFT; |
| 393 | type = cmd & SUBCMDMASK; |
| 394 | |
| 395 | if (cmds != Q_SYNC || special) { |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 396 | sb = quotactl_block(special); |
| 397 | if (IS_ERR(sb)) |
| 398 | return PTR_ERR(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | ret = check_quotactl_valid(sb, type, cmds, id); |
| 402 | if (ret >= 0) |
| 403 | ret = do_quotactl(sb, type, cmds, id, addr); |
| 404 | if (sb) |
| 405 | drop_super(sb); |
| 406 | |
| 407 | return ret; |
| 408 | } |
Vasily Tarasov | b716395 | 2007-07-15 23:41:12 -0700 | [diff] [blame] | 409 | |
Tony Luck | 7a6c813 | 2007-07-27 15:35:43 -0700 | [diff] [blame] | 410 | #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT) |
Vasily Tarasov | b716395 | 2007-07-15 23:41:12 -0700 | [diff] [blame] | 411 | /* |
| 412 | * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64) |
| 413 | * and is necessary due to alignment problems. |
| 414 | */ |
| 415 | struct compat_if_dqblk { |
| 416 | compat_u64 dqb_bhardlimit; |
| 417 | compat_u64 dqb_bsoftlimit; |
| 418 | compat_u64 dqb_curspace; |
| 419 | compat_u64 dqb_ihardlimit; |
| 420 | compat_u64 dqb_isoftlimit; |
| 421 | compat_u64 dqb_curinodes; |
| 422 | compat_u64 dqb_btime; |
| 423 | compat_u64 dqb_itime; |
| 424 | compat_uint_t dqb_valid; |
| 425 | }; |
| 426 | |
| 427 | /* XFS structures */ |
| 428 | struct compat_fs_qfilestat { |
| 429 | compat_u64 dqb_bhardlimit; |
| 430 | compat_u64 qfs_nblks; |
| 431 | compat_uint_t qfs_nextents; |
| 432 | }; |
| 433 | |
| 434 | struct compat_fs_quota_stat { |
| 435 | __s8 qs_version; |
| 436 | __u16 qs_flags; |
| 437 | __s8 qs_pad; |
| 438 | struct compat_fs_qfilestat qs_uquota; |
| 439 | struct compat_fs_qfilestat qs_gquota; |
| 440 | compat_uint_t qs_incoredqs; |
| 441 | compat_int_t qs_btimelimit; |
| 442 | compat_int_t qs_itimelimit; |
| 443 | compat_int_t qs_rtbtimelimit; |
| 444 | __u16 qs_bwarnlimit; |
| 445 | __u16 qs_iwarnlimit; |
| 446 | }; |
| 447 | |
| 448 | asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special, |
| 449 | qid_t id, void __user *addr) |
| 450 | { |
| 451 | unsigned int cmds; |
| 452 | struct if_dqblk __user *dqblk; |
| 453 | struct compat_if_dqblk __user *compat_dqblk; |
| 454 | struct fs_quota_stat __user *fsqstat; |
| 455 | struct compat_fs_quota_stat __user *compat_fsqstat; |
| 456 | compat_uint_t data; |
| 457 | u16 xdata; |
| 458 | long ret; |
| 459 | |
| 460 | cmds = cmd >> SUBCMDSHIFT; |
| 461 | |
| 462 | switch (cmds) { |
| 463 | case Q_GETQUOTA: |
| 464 | dqblk = compat_alloc_user_space(sizeof(struct if_dqblk)); |
| 465 | compat_dqblk = addr; |
| 466 | ret = sys_quotactl(cmd, special, id, dqblk); |
| 467 | if (ret) |
| 468 | break; |
| 469 | if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) || |
| 470 | get_user(data, &dqblk->dqb_valid) || |
| 471 | put_user(data, &compat_dqblk->dqb_valid)) |
| 472 | ret = -EFAULT; |
| 473 | break; |
| 474 | case Q_SETQUOTA: |
| 475 | dqblk = compat_alloc_user_space(sizeof(struct if_dqblk)); |
| 476 | compat_dqblk = addr; |
| 477 | ret = -EFAULT; |
| 478 | if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) || |
| 479 | get_user(data, &compat_dqblk->dqb_valid) || |
| 480 | put_user(data, &dqblk->dqb_valid)) |
| 481 | break; |
| 482 | ret = sys_quotactl(cmd, special, id, dqblk); |
| 483 | break; |
| 484 | case Q_XGETQSTAT: |
| 485 | fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat)); |
| 486 | compat_fsqstat = addr; |
| 487 | ret = sys_quotactl(cmd, special, id, fsqstat); |
| 488 | if (ret) |
| 489 | break; |
| 490 | ret = -EFAULT; |
| 491 | /* Copying qs_version, qs_flags, qs_pad */ |
| 492 | if (copy_in_user(compat_fsqstat, fsqstat, |
| 493 | offsetof(struct compat_fs_quota_stat, qs_uquota))) |
| 494 | break; |
| 495 | /* Copying qs_uquota */ |
| 496 | if (copy_in_user(&compat_fsqstat->qs_uquota, |
| 497 | &fsqstat->qs_uquota, |
| 498 | sizeof(compat_fsqstat->qs_uquota)) || |
| 499 | get_user(data, &fsqstat->qs_uquota.qfs_nextents) || |
| 500 | put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents)) |
| 501 | break; |
| 502 | /* Copying qs_gquota */ |
| 503 | if (copy_in_user(&compat_fsqstat->qs_gquota, |
| 504 | &fsqstat->qs_gquota, |
| 505 | sizeof(compat_fsqstat->qs_gquota)) || |
| 506 | get_user(data, &fsqstat->qs_gquota.qfs_nextents) || |
| 507 | put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents)) |
| 508 | break; |
| 509 | /* Copying the rest */ |
| 510 | if (copy_in_user(&compat_fsqstat->qs_incoredqs, |
| 511 | &fsqstat->qs_incoredqs, |
| 512 | sizeof(struct compat_fs_quota_stat) - |
| 513 | offsetof(struct compat_fs_quota_stat, qs_incoredqs)) || |
| 514 | get_user(xdata, &fsqstat->qs_iwarnlimit) || |
| 515 | put_user(xdata, &compat_fsqstat->qs_iwarnlimit)) |
| 516 | break; |
| 517 | ret = 0; |
| 518 | break; |
| 519 | default: |
| 520 | ret = sys_quotactl(cmd, special, id, addr); |
| 521 | } |
| 522 | return ret; |
| 523 | } |
| 524 | #endif |
Steven Whitehouse | 86e931a | 2009-09-28 12:35:17 +0100 | [diff] [blame] | 525 | |
| 526 | |
| 527 | #ifdef CONFIG_QUOTA_NETLINK_INTERFACE |
| 528 | |
| 529 | /* Netlink family structure for quota */ |
| 530 | static struct genl_family quota_genl_family = { |
| 531 | .id = GENL_ID_GENERATE, |
| 532 | .hdrsize = 0, |
| 533 | .name = "VFS_DQUOT", |
| 534 | .version = 1, |
| 535 | .maxattr = QUOTA_NL_A_MAX, |
| 536 | }; |
| 537 | |
| 538 | /** |
| 539 | * quota_send_warning - Send warning to userspace about exceeded quota |
| 540 | * @type: The quota type: USRQQUOTA, GRPQUOTA,... |
| 541 | * @id: The user or group id of the quota that was exceeded |
| 542 | * @dev: The device on which the fs is mounted (sb->s_dev) |
| 543 | * @warntype: The type of the warning: QUOTA_NL_... |
| 544 | * |
| 545 | * This can be used by filesystems (including those which don't use |
| 546 | * dquot) to send a message to userspace relating to quota limits. |
| 547 | * |
| 548 | */ |
| 549 | |
| 550 | void quota_send_warning(short type, unsigned int id, dev_t dev, |
| 551 | const char warntype) |
| 552 | { |
| 553 | static atomic_t seq; |
| 554 | struct sk_buff *skb; |
| 555 | void *msg_head; |
| 556 | int ret; |
| 557 | int msg_size = 4 * nla_total_size(sizeof(u32)) + |
| 558 | 2 * nla_total_size(sizeof(u64)); |
| 559 | |
| 560 | /* We have to allocate using GFP_NOFS as we are called from a |
| 561 | * filesystem performing write and thus further recursion into |
| 562 | * the fs to free some data could cause deadlocks. */ |
| 563 | skb = genlmsg_new(msg_size, GFP_NOFS); |
| 564 | if (!skb) { |
| 565 | printk(KERN_ERR |
| 566 | "VFS: Not enough memory to send quota warning.\n"); |
| 567 | return; |
| 568 | } |
| 569 | msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq), |
| 570 | "a_genl_family, 0, QUOTA_NL_C_WARNING); |
| 571 | if (!msg_head) { |
| 572 | printk(KERN_ERR |
| 573 | "VFS: Cannot store netlink header in quota warning.\n"); |
| 574 | goto err_out; |
| 575 | } |
| 576 | ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type); |
| 577 | if (ret) |
| 578 | goto attr_err_out; |
| 579 | ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id); |
| 580 | if (ret) |
| 581 | goto attr_err_out; |
| 582 | ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype); |
| 583 | if (ret) |
| 584 | goto attr_err_out; |
| 585 | ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev)); |
| 586 | if (ret) |
| 587 | goto attr_err_out; |
| 588 | ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev)); |
| 589 | if (ret) |
| 590 | goto attr_err_out; |
| 591 | ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid()); |
| 592 | if (ret) |
| 593 | goto attr_err_out; |
| 594 | genlmsg_end(skb, msg_head); |
| 595 | |
| 596 | genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS); |
| 597 | return; |
| 598 | attr_err_out: |
| 599 | printk(KERN_ERR "VFS: Not enough space to compose quota message!\n"); |
| 600 | err_out: |
| 601 | kfree_skb(skb); |
| 602 | } |
| 603 | EXPORT_SYMBOL(quota_send_warning); |
| 604 | |
| 605 | static int __init quota_init(void) |
| 606 | { |
| 607 | if (genl_register_family("a_genl_family) != 0) |
| 608 | printk(KERN_ERR |
| 609 | "VFS: Failed to create quota netlink interface.\n"); |
| 610 | return 0; |
| 611 | }; |
| 612 | |
| 613 | module_init(quota_init); |
| 614 | #endif |
| 615 | |