| 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> | 
 | 13 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/security.h> | 
 | 15 | #include <linux/syscalls.h> | 
| Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> | 
| Adrian Bunk | be586ba | 2005-11-07 00:59:35 -0800 | [diff] [blame] | 17 | #include <linux/quotaops.h> | 
| Vasily Tarasov | b716395 | 2007-07-15 23:41:12 -0700 | [diff] [blame] | 18 | #include <linux/types.h> | 
| Christoph Hellwig | 8c4e4ac | 2010-02-16 03:44:51 -0500 | [diff] [blame] | 19 | #include <linux/writeback.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 |  | 
| Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 21 | static int check_quotactl_permission(struct super_block *sb, int type, int cmd, | 
 | 22 | 				     qid_t id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { | 
| Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 24 | 	switch (cmd) { | 
 | 25 | 	/* these commands do not require any special privilegues */ | 
 | 26 | 	case Q_GETFMT: | 
 | 27 | 	case Q_SYNC: | 
 | 28 | 	case Q_GETINFO: | 
 | 29 | 	case Q_XGETQSTAT: | 
 | 30 | 	case Q_XQUOTASYNC: | 
 | 31 | 		break; | 
 | 32 | 	/* allow to query information for dquots we "own" */ | 
 | 33 | 	case Q_GETQUOTA: | 
 | 34 | 	case Q_XGETQUOTA: | 
 | 35 | 		if ((type == USRQUOTA && current_euid() == id) || | 
 | 36 | 		    (type == GRPQUOTA && in_egroup_p(id))) | 
 | 37 | 			break; | 
 | 38 | 		/*FALLTHROUGH*/ | 
 | 39 | 	default: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | 		if (!capable(CAP_SYS_ADMIN)) | 
 | 41 | 			return -EPERM; | 
 | 42 | 	} | 
 | 43 |  | 
| Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 44 | 	return security_quotactl(cmd, type, id, sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } | 
 | 46 |  | 
| Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 47 | static void quota_sync_one(struct super_block *sb, void *arg) | 
 | 48 | { | 
 | 49 | 	if (sb->s_qcop && sb->s_qcop->quota_sync) | 
 | 50 | 		sb->s_qcop->quota_sync(sb, *(int *)arg, 1); | 
 | 51 | } | 
 | 52 |  | 
| Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 53 | static int quota_sync_all(int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { | 
| Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 55 | 	int ret; | 
 | 56 |  | 
 | 57 | 	if (type >= MAXQUOTAS) | 
 | 58 | 		return -EINVAL; | 
 | 59 | 	ret = security_quotactl(Q_SYNC, type, 0, NULL); | 
| Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 60 | 	if (!ret) | 
 | 61 | 		iterate_supers(quota_sync_one, &type); | 
 | 62 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } | 
 | 64 |  | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 65 | static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id, | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 66 | 		         struct path *path) | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 67 | { | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 68 | 	if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_on_meta) | 
 | 69 | 		return -ENOSYS; | 
 | 70 | 	if (sb->s_qcop->quota_on_meta) | 
 | 71 | 		return sb->s_qcop->quota_on_meta(sb, type, id); | 
 | 72 | 	if (IS_ERR(path)) | 
 | 73 | 		return PTR_ERR(path); | 
 | 74 | 	return sb->s_qcop->quota_on(sb, type, id, path); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 75 | } | 
 | 76 |  | 
 | 77 | static int quota_getfmt(struct super_block *sb, int type, void __user *addr) | 
 | 78 | { | 
 | 79 | 	__u32 fmt; | 
 | 80 |  | 
 | 81 | 	down_read(&sb_dqopt(sb)->dqptr_sem); | 
 | 82 | 	if (!sb_has_quota_active(sb, type)) { | 
 | 83 | 		up_read(&sb_dqopt(sb)->dqptr_sem); | 
 | 84 | 		return -ESRCH; | 
 | 85 | 	} | 
 | 86 | 	fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id; | 
 | 87 | 	up_read(&sb_dqopt(sb)->dqptr_sem); | 
 | 88 | 	if (copy_to_user(addr, &fmt, sizeof(fmt))) | 
 | 89 | 		return -EFAULT; | 
 | 90 | 	return 0; | 
 | 91 | } | 
 | 92 |  | 
 | 93 | static int quota_getinfo(struct super_block *sb, int type, void __user *addr) | 
 | 94 | { | 
 | 95 | 	struct if_dqinfo info; | 
 | 96 | 	int ret; | 
 | 97 |  | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 98 | 	if (!sb->s_qcop->get_info) | 
 | 99 | 		return -ENOSYS; | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 100 | 	ret = sb->s_qcop->get_info(sb, type, &info); | 
 | 101 | 	if (!ret && copy_to_user(addr, &info, sizeof(info))) | 
 | 102 | 		return -EFAULT; | 
 | 103 | 	return ret; | 
 | 104 | } | 
 | 105 |  | 
 | 106 | static int quota_setinfo(struct super_block *sb, int type, void __user *addr) | 
 | 107 | { | 
 | 108 | 	struct if_dqinfo info; | 
 | 109 |  | 
 | 110 | 	if (copy_from_user(&info, addr, sizeof(info))) | 
 | 111 | 		return -EFAULT; | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 112 | 	if (!sb->s_qcop->set_info) | 
 | 113 | 		return -ENOSYS; | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 114 | 	return sb->s_qcop->set_info(sb, type, &info); | 
 | 115 | } | 
 | 116 |  | 
| Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 117 | static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src) | 
 | 118 | { | 
 | 119 | 	dst->dqb_bhardlimit = src->d_blk_hardlimit; | 
 | 120 | 	dst->dqb_bsoftlimit = src->d_blk_softlimit; | 
 | 121 | 	dst->dqb_curspace = src->d_bcount; | 
 | 122 | 	dst->dqb_ihardlimit = src->d_ino_hardlimit; | 
 | 123 | 	dst->dqb_isoftlimit = src->d_ino_softlimit; | 
 | 124 | 	dst->dqb_curinodes = src->d_icount; | 
 | 125 | 	dst->dqb_btime = src->d_btimer; | 
 | 126 | 	dst->dqb_itime = src->d_itimer; | 
 | 127 | 	dst->dqb_valid = QIF_ALL; | 
 | 128 | } | 
 | 129 |  | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 130 | static int quota_getquota(struct super_block *sb, int type, qid_t id, | 
 | 131 | 			  void __user *addr) | 
 | 132 | { | 
| Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 133 | 	struct fs_disk_quota fdq; | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 134 | 	struct if_dqblk idq; | 
 | 135 | 	int ret; | 
 | 136 |  | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 137 | 	if (!sb->s_qcop->get_dqblk) | 
 | 138 | 		return -ENOSYS; | 
| Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 139 | 	ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 140 | 	if (ret) | 
 | 141 | 		return ret; | 
| Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 142 | 	copy_to_if_dqblk(&idq, &fdq); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 143 | 	if (copy_to_user(addr, &idq, sizeof(idq))) | 
 | 144 | 		return -EFAULT; | 
 | 145 | 	return 0; | 
 | 146 | } | 
 | 147 |  | 
| Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 148 | static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src) | 
 | 149 | { | 
 | 150 | 	dst->d_blk_hardlimit = src->dqb_bhardlimit; | 
 | 151 | 	dst->d_blk_softlimit  = src->dqb_bsoftlimit; | 
 | 152 | 	dst->d_bcount = src->dqb_curspace; | 
 | 153 | 	dst->d_ino_hardlimit = src->dqb_ihardlimit; | 
 | 154 | 	dst->d_ino_softlimit = src->dqb_isoftlimit; | 
 | 155 | 	dst->d_icount = src->dqb_curinodes; | 
 | 156 | 	dst->d_btimer = src->dqb_btime; | 
 | 157 | 	dst->d_itimer = src->dqb_itime; | 
 | 158 |  | 
 | 159 | 	dst->d_fieldmask = 0; | 
 | 160 | 	if (src->dqb_valid & QIF_BLIMITS) | 
 | 161 | 		dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD; | 
 | 162 | 	if (src->dqb_valid & QIF_SPACE) | 
 | 163 | 		dst->d_fieldmask |= FS_DQ_BCOUNT; | 
 | 164 | 	if (src->dqb_valid & QIF_ILIMITS) | 
 | 165 | 		dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD; | 
 | 166 | 	if (src->dqb_valid & QIF_INODES) | 
 | 167 | 		dst->d_fieldmask |= FS_DQ_ICOUNT; | 
 | 168 | 	if (src->dqb_valid & QIF_BTIME) | 
 | 169 | 		dst->d_fieldmask |= FS_DQ_BTIMER; | 
 | 170 | 	if (src->dqb_valid & QIF_ITIME) | 
 | 171 | 		dst->d_fieldmask |= FS_DQ_ITIMER; | 
 | 172 | } | 
 | 173 |  | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 174 | static int quota_setquota(struct super_block *sb, int type, qid_t id, | 
 | 175 | 			  void __user *addr) | 
 | 176 | { | 
| Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 177 | 	struct fs_disk_quota fdq; | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 178 | 	struct if_dqblk idq; | 
 | 179 |  | 
 | 180 | 	if (copy_from_user(&idq, addr, sizeof(idq))) | 
 | 181 | 		return -EFAULT; | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 182 | 	if (!sb->s_qcop->set_dqblk) | 
 | 183 | 		return -ENOSYS; | 
| Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 184 | 	copy_from_if_dqblk(&fdq, &idq); | 
 | 185 | 	return sb->s_qcop->set_dqblk(sb, type, id, &fdq); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 186 | } | 
 | 187 |  | 
 | 188 | static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr) | 
 | 189 | { | 
 | 190 | 	__u32 flags; | 
 | 191 |  | 
 | 192 | 	if (copy_from_user(&flags, addr, sizeof(flags))) | 
 | 193 | 		return -EFAULT; | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 194 | 	if (!sb->s_qcop->set_xstate) | 
 | 195 | 		return -ENOSYS; | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 196 | 	return sb->s_qcop->set_xstate(sb, flags, cmd); | 
 | 197 | } | 
 | 198 |  | 
 | 199 | static int quota_getxstate(struct super_block *sb, void __user *addr) | 
 | 200 | { | 
 | 201 | 	struct fs_quota_stat fqs; | 
 | 202 | 	int ret; | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 203 |  | 
 | 204 | 	if (!sb->s_qcop->get_xstate) | 
 | 205 | 		return -ENOSYS; | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 206 | 	ret = sb->s_qcop->get_xstate(sb, &fqs); | 
 | 207 | 	if (!ret && copy_to_user(addr, &fqs, sizeof(fqs))) | 
 | 208 | 		return -EFAULT; | 
 | 209 | 	return ret; | 
 | 210 | } | 
 | 211 |  | 
 | 212 | static int quota_setxquota(struct super_block *sb, int type, qid_t id, | 
 | 213 | 			   void __user *addr) | 
 | 214 | { | 
 | 215 | 	struct fs_disk_quota fdq; | 
 | 216 |  | 
 | 217 | 	if (copy_from_user(&fdq, addr, sizeof(fdq))) | 
 | 218 | 		return -EFAULT; | 
| Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 219 | 	if (!sb->s_qcop->set_dqblk) | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 220 | 		return -ENOSYS; | 
| Christoph Hellwig | c472b43 | 2010-05-06 17:05:17 -0400 | [diff] [blame] | 221 | 	return sb->s_qcop->set_dqblk(sb, type, id, &fdq); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 222 | } | 
 | 223 |  | 
 | 224 | static int quota_getxquota(struct super_block *sb, int type, qid_t id, | 
 | 225 | 			   void __user *addr) | 
 | 226 | { | 
 | 227 | 	struct fs_disk_quota fdq; | 
 | 228 | 	int ret; | 
 | 229 |  | 
| Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 230 | 	if (!sb->s_qcop->get_dqblk) | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 231 | 		return -ENOSYS; | 
| Christoph Hellwig | b9b2dd3 | 2010-05-06 17:04:58 -0400 | [diff] [blame] | 232 | 	ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 233 | 	if (!ret && copy_to_user(addr, &fdq, sizeof(fdq))) | 
 | 234 | 		return -EFAULT; | 
 | 235 | 	return ret; | 
 | 236 | } | 
 | 237 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* Copy parameters and call proper function */ | 
| Jan Kara | 268157b | 2009-01-27 15:47:22 +0100 | [diff] [blame] | 239 | static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 240 | 		       void __user *addr, struct path *path) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { | 
| Christoph Hellwig | c988afb | 2010-02-16 03:44:50 -0500 | [diff] [blame] | 242 | 	int ret; | 
 | 243 |  | 
 | 244 | 	if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS)) | 
 | 245 | 		return -EINVAL; | 
 | 246 | 	if (!sb->s_qcop) | 
 | 247 | 		return -ENOSYS; | 
 | 248 |  | 
 | 249 | 	ret = check_quotactl_permission(sb, type, cmd, id); | 
 | 250 | 	if (ret < 0) | 
 | 251 | 		return ret; | 
 | 252 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | 	switch (cmd) { | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 254 | 	case Q_QUOTAON: | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 255 | 		return quota_quotaon(sb, type, cmd, id, path); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 256 | 	case Q_QUOTAOFF: | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 257 | 		if (!sb->s_qcop->quota_off) | 
 | 258 | 			return -ENOSYS; | 
| Christoph Hellwig | 307ae18 | 2010-05-19 07:16:43 -0400 | [diff] [blame] | 259 | 		return sb->s_qcop->quota_off(sb, type); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 260 | 	case Q_GETFMT: | 
 | 261 | 		return quota_getfmt(sb, type, addr); | 
 | 262 | 	case Q_GETINFO: | 
 | 263 | 		return quota_getinfo(sb, type, addr); | 
 | 264 | 	case Q_SETINFO: | 
 | 265 | 		return quota_setinfo(sb, type, addr); | 
 | 266 | 	case Q_GETQUOTA: | 
 | 267 | 		return quota_getquota(sb, type, id, addr); | 
 | 268 | 	case Q_SETQUOTA: | 
 | 269 | 		return quota_setquota(sb, type, id, addr); | 
 | 270 | 	case Q_SYNC: | 
| Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 271 | 		if (!sb->s_qcop->quota_sync) | 
 | 272 | 			return -ENOSYS; | 
| Christoph Hellwig | 5fb324a | 2010-02-16 03:44:52 -0500 | [diff] [blame] | 273 | 		return sb->s_qcop->quota_sync(sb, type, 1); | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 274 | 	case Q_XQUOTAON: | 
 | 275 | 	case Q_XQUOTAOFF: | 
 | 276 | 	case Q_XQUOTARM: | 
 | 277 | 		return quota_setxstate(sb, cmd, addr); | 
 | 278 | 	case Q_XGETQSTAT: | 
 | 279 | 		return quota_getxstate(sb, addr); | 
 | 280 | 	case Q_XSETQLIM: | 
 | 281 | 		return quota_setxquota(sb, type, id, addr); | 
 | 282 | 	case Q_XGETQUOTA: | 
 | 283 | 		return quota_getxquota(sb, type, id, addr); | 
 | 284 | 	case Q_XQUOTASYNC: | 
| Christoph Hellwig | 8c4e4ac | 2010-02-16 03:44:51 -0500 | [diff] [blame] | 285 | 		if (sb->s_flags & MS_RDONLY) | 
 | 286 | 			return -EROFS; | 
| Christoph Hellwig | 4b217ed | 2012-02-20 02:28:18 +0000 | [diff] [blame] | 287 | 		/* XFS quotas are fully coherent now, making this call a noop */ | 
| Christoph Hellwig | 8c4e4ac | 2010-02-16 03:44:51 -0500 | [diff] [blame] | 288 | 		return 0; | 
| Christoph Hellwig | c411e5f | 2010-02-16 03:44:47 -0500 | [diff] [blame] | 289 | 	default: | 
| Christoph Hellwig | f450d4f | 2010-02-16 03:44:48 -0500 | [diff] [blame] | 290 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } | 
 | 293 |  | 
| Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 294 | /* Return 1 if 'cmd' will block on frozen filesystem */ | 
 | 295 | static int quotactl_cmd_write(int cmd) | 
 | 296 | { | 
 | 297 | 	switch (cmd) { | 
 | 298 | 	case Q_GETFMT: | 
 | 299 | 	case Q_GETINFO: | 
 | 300 | 	case Q_SYNC: | 
 | 301 | 	case Q_XGETQSTAT: | 
 | 302 | 	case Q_XGETQUOTA: | 
 | 303 | 	case Q_XQUOTASYNC: | 
 | 304 | 		return 0; | 
 | 305 | 	} | 
 | 306 | 	return 1; | 
 | 307 | } | 
 | 308 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | /* | 
| David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 310 |  * look up a superblock on which quota ops will be performed | 
 | 311 |  * - use the name of a block device to find the superblock thereon | 
 | 312 |  */ | 
| Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 313 | static struct super_block *quotactl_block(const char __user *special, int cmd) | 
| David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 314 | { | 
 | 315 | #ifdef CONFIG_BLOCK | 
 | 316 | 	struct block_device *bdev; | 
 | 317 | 	struct super_block *sb; | 
 | 318 | 	char *tmp = getname(special); | 
 | 319 |  | 
 | 320 | 	if (IS_ERR(tmp)) | 
| David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 321 | 		return ERR_CAST(tmp); | 
| David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 322 | 	bdev = lookup_bdev(tmp); | 
 | 323 | 	putname(tmp); | 
 | 324 | 	if (IS_ERR(bdev)) | 
| David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 325 | 		return ERR_CAST(bdev); | 
| Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 326 | 	if (quotactl_cmd_write(cmd)) | 
 | 327 | 		sb = get_super_thawed(bdev); | 
 | 328 | 	else | 
 | 329 | 		sb = get_super(bdev); | 
| David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 330 | 	bdput(bdev); | 
 | 331 | 	if (!sb) | 
 | 332 | 		return ERR_PTR(-ENODEV); | 
 | 333 |  | 
 | 334 | 	return sb; | 
 | 335 | #else | 
 | 336 | 	return ERR_PTR(-ENODEV); | 
 | 337 | #endif | 
 | 338 | } | 
 | 339 |  | 
 | 340 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 |  * This is the system call interface. This communicates with | 
 | 342 |  * the user-level programs. Currently this only supports diskquota | 
 | 343 |  * calls. Maybe we need to add the process quotas etc. in the future, | 
 | 344 |  * but we probably should use rlimits for that. | 
 | 345 |  */ | 
| Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 346 | SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special, | 
 | 347 | 		qid_t, id, void __user *, addr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { | 
 | 349 | 	uint cmds, type; | 
 | 350 | 	struct super_block *sb = NULL; | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 351 | 	struct path path, *pathp = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | 	int ret; | 
 | 353 |  | 
 | 354 | 	cmds = cmd >> SUBCMDSHIFT; | 
 | 355 | 	type = cmd & SUBCMDMASK; | 
 | 356 |  | 
| Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 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; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | 	} | 
 | 367 |  | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 368 | 	/* | 
 | 369 | 	 * Path for quotaon has to be resolved before grabbing superblock | 
 | 370 | 	 * because that gets s_umount sem which is also possibly needed by path | 
 | 371 | 	 * resolution (think about autofs) and thus deadlocks could arise. | 
 | 372 | 	 */ | 
 | 373 | 	if (cmds == Q_QUOTAON) { | 
| Trond Myklebust | 815d405 | 2011-09-26 20:36:09 -0400 | [diff] [blame] | 374 | 		ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path); | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 375 | 		if (ret) | 
 | 376 | 			pathp = ERR_PTR(ret); | 
 | 377 | 		else | 
 | 378 | 			pathp = &path; | 
 | 379 | 	} | 
 | 380 |  | 
| Jan Kara | dcdbed8 | 2012-02-10 11:03:01 +0100 | [diff] [blame] | 381 | 	sb = quotactl_block(special, cmds); | 
| Jan Kara | 0aaa618 | 2011-10-10 18:32:06 +0200 | [diff] [blame] | 382 | 	if (IS_ERR(sb)) { | 
 | 383 | 		ret = PTR_ERR(sb); | 
 | 384 | 		goto out; | 
 | 385 | 	} | 
| Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 386 |  | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 387 | 	ret = do_quotactl(sb, type, cmds, id, addr, pathp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 |  | 
| Christoph Hellwig | 6ae0957 | 2010-02-16 03:44:49 -0500 | [diff] [blame] | 389 | 	drop_super(sb); | 
| Jan Kara | 0aaa618 | 2011-10-10 18:32:06 +0200 | [diff] [blame] | 390 | out: | 
| Jan Kara | f00c9e4 | 2010-09-15 17:38:58 +0200 | [diff] [blame] | 391 | 	if (pathp && !IS_ERR(pathp)) | 
 | 392 | 		path_put(pathp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | 	return ret; | 
 | 394 | } |