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> 13 #include <linux/kernel.h> 14 #include <linux/security.h> 15 #include <linux/syscalls.h> 16 #include <linux/buffer_head.h> 17 #include <linux/capability.h> 18 #include <linux/quotaops.h> 19 #include <linux/types.h> 20 #include <linux/writeback.h> 21 22 static int check_quotactl_permission(struct super_block *sb, int type, int cmd, 23 qid_t id) 24 { 25 switch (cmd) { 26 /* these commands do not require any special privilegues */ 27 case Q_GETFMT: 28 case Q_SYNC: 29 case Q_GETINFO: 30 case Q_XGETQSTAT: 31 case Q_XQUOTASYNC: 32 break; 33 /* allow to query information for dquots we "own" */ 34 case Q_GETQUOTA: 35 case Q_XGETQUOTA: 36 if ((type == USRQUOTA && current_euid() == id) || 37 (type == GRPQUOTA && in_egroup_p(id))) 38 break; 39 /*FALLTHROUGH*/ 40 default: 41 if (!capable(CAP_SYS_ADMIN)) 42 return -EPERM; 43 } 44 45 return security_quotactl(cmd, type, id, sb); 46 } 47 48 static int quota_sync_all(int type) 49 { 50 struct super_block *sb; 51 int ret; 52 53 if (type >= MAXQUOTAS) 54 return -EINVAL; 55 ret = security_quotactl(Q_SYNC, type, 0, NULL); 56 if (ret) 57 return ret; 58 59 spin_lock(&sb_lock); 60 restart: 61 list_for_each_entry(sb, &super_blocks, s_list) { 62 if (!sb->s_qcop || !sb->s_qcop->quota_sync) 63 continue; 64 65 sb->s_count++; 66 spin_unlock(&sb_lock); 67 down_read(&sb->s_umount); 68 if (sb->s_root) 69 sb->s_qcop->quota_sync(sb, type, 1); 70 up_read(&sb->s_umount); 71 spin_lock(&sb_lock); 72 if (__put_super_and_need_restart(sb)) 73 goto restart; 74 } 75 spin_unlock(&sb_lock); 76 77 return 0; 78 } 79 80 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id, 81 void __user *addr) 82 { 83 char *pathname; 84 int ret = -ENOSYS; 85 86 pathname = getname(addr); 87 if (IS_ERR(pathname)) 88 return PTR_ERR(pathname); 89 if (sb->s_qcop->quota_on) 90 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0); 91 putname(pathname); 92 return ret; 93 } 94 95 static int quota_getfmt(struct super_block *sb, int type, void __user *addr) 96 { 97 __u32 fmt; 98 99 down_read(&sb_dqopt(sb)->dqptr_sem); 100 if (!sb_has_quota_active(sb, type)) { 101 up_read(&sb_dqopt(sb)->dqptr_sem); 102 return -ESRCH; 103 } 104 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id; 105 up_read(&sb_dqopt(sb)->dqptr_sem); 106 if (copy_to_user(addr, &fmt, sizeof(fmt))) 107 return -EFAULT; 108 return 0; 109 } 110 111 static int quota_getinfo(struct super_block *sb, int type, void __user *addr) 112 { 113 struct if_dqinfo info; 114 int ret; 115 116 if (!sb->s_qcop->get_info) 117 return -ENOSYS; 118 ret = sb->s_qcop->get_info(sb, type, &info); 119 if (!ret && copy_to_user(addr, &info, sizeof(info))) 120 return -EFAULT; 121 return ret; 122 } 123 124 static int quota_setinfo(struct super_block *sb, int type, void __user *addr) 125 { 126 struct if_dqinfo info; 127 128 if (copy_from_user(&info, addr, sizeof(info))) 129 return -EFAULT; 130 if (!sb->s_qcop->set_info) 131 return -ENOSYS; 132 return sb->s_qcop->set_info(sb, type, &info); 133 } 134 135 static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src) 136 { 137 dst->dqb_bhardlimit = src->d_blk_hardlimit; 138 dst->dqb_bsoftlimit = src->d_blk_softlimit; 139 dst->dqb_curspace = src->d_bcount; 140 dst->dqb_ihardlimit = src->d_ino_hardlimit; 141 dst->dqb_isoftlimit = src->d_ino_softlimit; 142 dst->dqb_curinodes = src->d_icount; 143 dst->dqb_btime = src->d_btimer; 144 dst->dqb_itime = src->d_itimer; 145 dst->dqb_valid = QIF_ALL; 146 } 147 148 static int quota_getquota(struct super_block *sb, int type, qid_t id, 149 void __user *addr) 150 { 151 struct fs_disk_quota fdq; 152 struct if_dqblk idq; 153 int ret; 154 155 if (!sb->s_qcop->get_dqblk) 156 return -ENOSYS; 157 ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq); 158 if (ret) 159 return ret; 160 copy_to_if_dqblk(&idq, &fdq); 161 if (copy_to_user(addr, &idq, sizeof(idq))) 162 return -EFAULT; 163 return 0; 164 } 165 166 static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src) 167 { 168 dst->d_blk_hardlimit = src->dqb_bhardlimit; 169 dst->d_blk_softlimit = src->dqb_bsoftlimit; 170 dst->d_bcount = src->dqb_curspace; 171 dst->d_ino_hardlimit = src->dqb_ihardlimit; 172 dst->d_ino_softlimit = src->dqb_isoftlimit; 173 dst->d_icount = src->dqb_curinodes; 174 dst->d_btimer = src->dqb_btime; 175 dst->d_itimer = src->dqb_itime; 176 177 dst->d_fieldmask = 0; 178 if (src->dqb_valid & QIF_BLIMITS) 179 dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD; 180 if (src->dqb_valid & QIF_SPACE) 181 dst->d_fieldmask |= FS_DQ_BCOUNT; 182 if (src->dqb_valid & QIF_ILIMITS) 183 dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD; 184 if (src->dqb_valid & QIF_INODES) 185 dst->d_fieldmask |= FS_DQ_ICOUNT; 186 if (src->dqb_valid & QIF_BTIME) 187 dst->d_fieldmask |= FS_DQ_BTIMER; 188 if (src->dqb_valid & QIF_ITIME) 189 dst->d_fieldmask |= FS_DQ_ITIMER; 190 } 191 192 static int quota_setquota(struct super_block *sb, int type, qid_t id, 193 void __user *addr) 194 { 195 struct fs_disk_quota fdq; 196 struct if_dqblk idq; 197 198 if (copy_from_user(&idq, addr, sizeof(idq))) 199 return -EFAULT; 200 if (!sb->s_qcop->set_dqblk) 201 return -ENOSYS; 202 copy_from_if_dqblk(&fdq, &idq); 203 return sb->s_qcop->set_dqblk(sb, type, id, &fdq); 204 } 205 206 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr) 207 { 208 __u32 flags; 209 210 if (copy_from_user(&flags, addr, sizeof(flags))) 211 return -EFAULT; 212 if (!sb->s_qcop->set_xstate) 213 return -ENOSYS; 214 return sb->s_qcop->set_xstate(sb, flags, cmd); 215 } 216 217 static int quota_getxstate(struct super_block *sb, void __user *addr) 218 { 219 struct fs_quota_stat fqs; 220 int ret; 221 222 if (!sb->s_qcop->get_xstate) 223 return -ENOSYS; 224 ret = sb->s_qcop->get_xstate(sb, &fqs); 225 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) 226 return -EFAULT; 227 return ret; 228 } 229 230 static int quota_setxquota(struct super_block *sb, int type, qid_t id, 231 void __user *addr) 232 { 233 struct fs_disk_quota fdq; 234 235 if (copy_from_user(&fdq, addr, sizeof(fdq))) 236 return -EFAULT; 237 if (!sb->s_qcop->set_dqblk) 238 return -ENOSYS; 239 return sb->s_qcop->set_dqblk(sb, type, id, &fdq); 240 } 241 242 static int quota_getxquota(struct super_block *sb, int type, qid_t id, 243 void __user *addr) 244 { 245 struct fs_disk_quota fdq; 246 int ret; 247 248 if (!sb->s_qcop->get_dqblk) 249 return -ENOSYS; 250 ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq); 251 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq))) 252 return -EFAULT; 253 return ret; 254 } 255 256 /* Copy parameters and call proper function */ 257 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, 258 void __user *addr) 259 { 260 int ret; 261 262 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS)) 263 return -EINVAL; 264 if (!sb->s_qcop) 265 return -ENOSYS; 266 267 ret = check_quotactl_permission(sb, type, cmd, id); 268 if (ret < 0) 269 return ret; 270 271 switch (cmd) { 272 case Q_QUOTAON: 273 return quota_quotaon(sb, type, cmd, id, addr); 274 case Q_QUOTAOFF: 275 if (!sb->s_qcop->quota_off) 276 return -ENOSYS; 277 return sb->s_qcop->quota_off(sb, type, 0); 278 case Q_GETFMT: 279 return quota_getfmt(sb, type, addr); 280 case Q_GETINFO: 281 return quota_getinfo(sb, type, addr); 282 case Q_SETINFO: 283 return quota_setinfo(sb, type, addr); 284 case Q_GETQUOTA: 285 return quota_getquota(sb, type, id, addr); 286 case Q_SETQUOTA: 287 return quota_setquota(sb, type, id, addr); 288 case Q_SYNC: 289 if (!sb->s_qcop->quota_sync) 290 return -ENOSYS; 291 return sb->s_qcop->quota_sync(sb, type, 1); 292 case Q_XQUOTAON: 293 case Q_XQUOTAOFF: 294 case Q_XQUOTARM: 295 return quota_setxstate(sb, cmd, addr); 296 case Q_XGETQSTAT: 297 return quota_getxstate(sb, addr); 298 case Q_XSETQLIM: 299 return quota_setxquota(sb, type, id, addr); 300 case Q_XGETQUOTA: 301 return quota_getxquota(sb, type, id, addr); 302 case Q_XQUOTASYNC: 303 /* caller already holds s_umount */ 304 if (sb->s_flags & MS_RDONLY) 305 return -EROFS; 306 writeback_inodes_sb(sb); 307 return 0; 308 default: 309 return -EINVAL; 310 } 311 } 312 313 /* 314 * look up a superblock on which quota ops will be performed 315 * - use the name of a block device to find the superblock thereon 316 */ 317 static struct super_block *quotactl_block(const char __user *special) 318 { 319 #ifdef CONFIG_BLOCK 320 struct block_device *bdev; 321 struct super_block *sb; 322 char *tmp = getname(special); 323 324 if (IS_ERR(tmp)) 325 return ERR_CAST(tmp); 326 bdev = lookup_bdev(tmp); 327 putname(tmp); 328 if (IS_ERR(bdev)) 329 return ERR_CAST(bdev); 330 sb = get_super(bdev); 331 bdput(bdev); 332 if (!sb) 333 return ERR_PTR(-ENODEV); 334 335 return sb; 336 #else 337 return ERR_PTR(-ENODEV); 338 #endif 339 } 340 341 /* 342 * This is the system call interface. This communicates with 343 * the user-level programs. Currently this only supports diskquota 344 * calls. Maybe we need to add the process quotas etc. in the future, 345 * but we probably should use rlimits for that. 346 */ 347 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special, 348 qid_t, id, void __user *, addr) 349 { 350 uint cmds, type; 351 struct super_block *sb = NULL; 352 int ret; 353 354 cmds = cmd >> SUBCMDSHIFT; 355 type = cmd & SUBCMDMASK; 356 357 /* 358 * As a special case Q_SYNC can be called without a specific device. 359 * It will iterate all superblocks that have quota enabled and call 360 * the sync action on each of them. 361 */ 362 if (!special) { 363 if (cmds == Q_SYNC) 364 return quota_sync_all(type); 365 return -ENODEV; 366 } 367 368 sb = quotactl_block(special); 369 if (IS_ERR(sb)) 370 return PTR_ERR(sb); 371 372 ret = do_quotactl(sb, type, cmds, id, addr); 373 374 drop_super(sb); 375 return ret; 376 } 377