quota.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. #include <linux/fs.h>
  8. #include <linux/namei.h>
  9. #include <linux/slab.h>
  10. #include <asm/current.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/kernel.h>
  13. #include <linux/security.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/capability.h>
  16. #include <linux/quotaops.h>
  17. #include <linux/types.h>
  18. #include <linux/writeback.h>
  19. static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
  20. qid_t id)
  21. {
  22. switch (cmd) {
  23. /* these commands do not require any special privilegues */
  24. case Q_GETFMT:
  25. case Q_SYNC:
  26. case Q_GETINFO:
  27. case Q_XGETQSTAT:
  28. case Q_XGETQSTATV:
  29. case Q_XQUOTASYNC:
  30. break;
  31. /* allow to query information for dquots we "own" */
  32. case Q_GETQUOTA:
  33. case Q_XGETQUOTA:
  34. if ((type == USRQUOTA && uid_eq(current_euid(), make_kuid(current_user_ns(), id))) ||
  35. (type == GRPQUOTA && in_egroup_p(make_kgid(current_user_ns(), id))))
  36. break;
  37. /*FALLTHROUGH*/
  38. default:
  39. if (!capable(CAP_SYS_ADMIN))
  40. return -EPERM;
  41. }
  42. return security_quotactl(cmd, type, id, sb);
  43. }
  44. static void quota_sync_one(struct super_block *sb, void *arg)
  45. {
  46. if (sb->s_qcop && sb->s_qcop->quota_sync)
  47. sb->s_qcop->quota_sync(sb, *(int *)arg);
  48. }
  49. static int quota_sync_all(int type)
  50. {
  51. int ret;
  52. if (type >= MAXQUOTAS)
  53. return -EINVAL;
  54. ret = security_quotactl(Q_SYNC, type, 0, NULL);
  55. if (!ret)
  56. iterate_supers(quota_sync_one, &type);
  57. return ret;
  58. }
  59. static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
  60. struct path *path)
  61. {
  62. if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_on_meta)
  63. return -ENOSYS;
  64. if (sb->s_qcop->quota_on_meta)
  65. return sb->s_qcop->quota_on_meta(sb, type, id);
  66. if (IS_ERR(path))
  67. return PTR_ERR(path);
  68. return sb->s_qcop->quota_on(sb, type, id, path);
  69. }
  70. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  71. {
  72. __u32 fmt;
  73. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  74. if (!sb_has_quota_active(sb, type)) {
  75. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  76. return -ESRCH;
  77. }
  78. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  79. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  80. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  81. return -EFAULT;
  82. return 0;
  83. }
  84. static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
  85. {
  86. struct if_dqinfo info;
  87. int ret;
  88. if (!sb->s_qcop->get_info)
  89. return -ENOSYS;
  90. ret = sb->s_qcop->get_info(sb, type, &info);
  91. if (!ret && copy_to_user(addr, &info, sizeof(info)))
  92. return -EFAULT;
  93. return ret;
  94. }
  95. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  96. {
  97. struct if_dqinfo info;
  98. if (copy_from_user(&info, addr, sizeof(info)))
  99. return -EFAULT;
  100. if (!sb->s_qcop->set_info)
  101. return -ENOSYS;
  102. return sb->s_qcop->set_info(sb, type, &info);
  103. }
  104. static inline qsize_t qbtos(qsize_t blocks)
  105. {
  106. return blocks << QIF_DQBLKSIZE_BITS;
  107. }
  108. static inline qsize_t stoqb(qsize_t space)
  109. {
  110. return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
  111. }
  112. static void copy_to_if_dqblk(struct if_dqblk *dst, struct qc_dqblk *src)
  113. {
  114. memset(dst, 0, sizeof(*dst));
  115. dst->dqb_bhardlimit = stoqb(src->d_spc_hardlimit);
  116. dst->dqb_bsoftlimit = stoqb(src->d_spc_softlimit);
  117. dst->dqb_curspace = src->d_space;
  118. dst->dqb_ihardlimit = src->d_ino_hardlimit;
  119. dst->dqb_isoftlimit = src->d_ino_softlimit;
  120. dst->dqb_curinodes = src->d_ino_count;
  121. dst->dqb_btime = src->d_spc_timer;
  122. dst->dqb_itime = src->d_ino_timer;
  123. dst->dqb_valid = QIF_ALL;
  124. }
  125. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  126. void __user *addr)
  127. {
  128. struct kqid qid;
  129. struct qc_dqblk fdq;
  130. struct if_dqblk idq;
  131. int ret;
  132. if (!sb->s_qcop->get_dqblk)
  133. return -ENOSYS;
  134. qid = make_kqid(current_user_ns(), type, id);
  135. if (!qid_valid(qid))
  136. return -EINVAL;
  137. ret = sb->s_qcop->get_dqblk(sb, qid, &fdq);
  138. if (ret)
  139. return ret;
  140. copy_to_if_dqblk(&idq, &fdq);
  141. if (copy_to_user(addr, &idq, sizeof(idq)))
  142. return -EFAULT;
  143. return 0;
  144. }
  145. static void copy_from_if_dqblk(struct qc_dqblk *dst, struct if_dqblk *src)
  146. {
  147. dst->d_spc_hardlimit = qbtos(src->dqb_bhardlimit);
  148. dst->d_spc_softlimit = qbtos(src->dqb_bsoftlimit);
  149. dst->d_space = src->dqb_curspace;
  150. dst->d_ino_hardlimit = src->dqb_ihardlimit;
  151. dst->d_ino_softlimit = src->dqb_isoftlimit;
  152. dst->d_ino_count = src->dqb_curinodes;
  153. dst->d_spc_timer = src->dqb_btime;
  154. dst->d_ino_timer = src->dqb_itime;
  155. dst->d_fieldmask = 0;
  156. if (src->dqb_valid & QIF_BLIMITS)
  157. dst->d_fieldmask |= QC_SPC_SOFT | QC_SPC_HARD;
  158. if (src->dqb_valid & QIF_SPACE)
  159. dst->d_fieldmask |= QC_SPACE;
  160. if (src->dqb_valid & QIF_ILIMITS)
  161. dst->d_fieldmask |= QC_INO_SOFT | QC_INO_HARD;
  162. if (src->dqb_valid & QIF_INODES)
  163. dst->d_fieldmask |= QC_INO_COUNT;
  164. if (src->dqb_valid & QIF_BTIME)
  165. dst->d_fieldmask |= QC_SPC_TIMER;
  166. if (src->dqb_valid & QIF_ITIME)
  167. dst->d_fieldmask |= QC_INO_TIMER;
  168. }
  169. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  170. void __user *addr)
  171. {
  172. struct qc_dqblk fdq;
  173. struct if_dqblk idq;
  174. struct kqid qid;
  175. if (copy_from_user(&idq, addr, sizeof(idq)))
  176. return -EFAULT;
  177. if (!sb->s_qcop->set_dqblk)
  178. return -ENOSYS;
  179. qid = make_kqid(current_user_ns(), type, id);
  180. if (!qid_valid(qid))
  181. return -EINVAL;
  182. copy_from_if_dqblk(&fdq, &idq);
  183. return sb->s_qcop->set_dqblk(sb, qid, &fdq);
  184. }
  185. static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
  186. {
  187. __u32 flags;
  188. if (copy_from_user(&flags, addr, sizeof(flags)))
  189. return -EFAULT;
  190. if (!sb->s_qcop->set_xstate)
  191. return -ENOSYS;
  192. return sb->s_qcop->set_xstate(sb, flags, cmd);
  193. }
  194. static int quota_getxstate(struct super_block *sb, void __user *addr)
  195. {
  196. struct fs_quota_stat fqs;
  197. int ret;
  198. if (!sb->s_qcop->get_xstate)
  199. return -ENOSYS;
  200. ret = sb->s_qcop->get_xstate(sb, &fqs);
  201. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  202. return -EFAULT;
  203. return ret;
  204. }
  205. static int quota_getxstatev(struct super_block *sb, void __user *addr)
  206. {
  207. struct fs_quota_statv fqs;
  208. int ret;
  209. if (!sb->s_qcop->get_xstatev)
  210. return -ENOSYS;
  211. memset(&fqs, 0, sizeof(fqs));
  212. if (copy_from_user(&fqs, addr, 1)) /* Just read qs_version */
  213. return -EFAULT;
  214. /* If this kernel doesn't support user specified version, fail */
  215. switch (fqs.qs_version) {
  216. case FS_QSTATV_VERSION1:
  217. break;
  218. default:
  219. return -EINVAL;
  220. }
  221. ret = sb->s_qcop->get_xstatev(sb, &fqs);
  222. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  223. return -EFAULT;
  224. return ret;
  225. }
  226. /*
  227. * XFS defines BBTOB and BTOBB macros inside fs/xfs/ and we cannot move them
  228. * out of there as xfsprogs rely on definitions being in that header file. So
  229. * just define same functions here for quota purposes.
  230. */
  231. #define XFS_BB_SHIFT 9
  232. static inline u64 quota_bbtob(u64 blocks)
  233. {
  234. return blocks << XFS_BB_SHIFT;
  235. }
  236. static inline u64 quota_btobb(u64 bytes)
  237. {
  238. return (bytes + (1 << XFS_BB_SHIFT) - 1) >> XFS_BB_SHIFT;
  239. }
  240. static void copy_from_xfs_dqblk(struct qc_dqblk *dst, struct fs_disk_quota *src)
  241. {
  242. dst->d_spc_hardlimit = quota_bbtob(src->d_blk_hardlimit);
  243. dst->d_spc_softlimit = quota_bbtob(src->d_blk_softlimit);
  244. dst->d_ino_hardlimit = src->d_ino_hardlimit;
  245. dst->d_ino_softlimit = src->d_ino_softlimit;
  246. dst->d_space = quota_bbtob(src->d_bcount);
  247. dst->d_ino_count = src->d_icount;
  248. dst->d_ino_timer = src->d_itimer;
  249. dst->d_spc_timer = src->d_btimer;
  250. dst->d_ino_warns = src->d_iwarns;
  251. dst->d_spc_warns = src->d_bwarns;
  252. dst->d_rt_spc_hardlimit = quota_bbtob(src->d_rtb_hardlimit);
  253. dst->d_rt_spc_softlimit = quota_bbtob(src->d_rtb_softlimit);
  254. dst->d_rt_space = quota_bbtob(src->d_rtbcount);
  255. dst->d_rt_spc_timer = src->d_rtbtimer;
  256. dst->d_rt_spc_warns = src->d_rtbwarns;
  257. dst->d_fieldmask = 0;
  258. if (src->d_fieldmask & FS_DQ_ISOFT)
  259. dst->d_fieldmask |= QC_INO_SOFT;
  260. if (src->d_fieldmask & FS_DQ_IHARD)
  261. dst->d_fieldmask |= QC_INO_HARD;
  262. if (src->d_fieldmask & FS_DQ_BSOFT)
  263. dst->d_fieldmask |= QC_SPC_SOFT;
  264. if (src->d_fieldmask & FS_DQ_BHARD)
  265. dst->d_fieldmask |= QC_SPC_HARD;
  266. if (src->d_fieldmask & FS_DQ_RTBSOFT)
  267. dst->d_fieldmask |= QC_RT_SPC_SOFT;
  268. if (src->d_fieldmask & FS_DQ_RTBHARD)
  269. dst->d_fieldmask |= QC_RT_SPC_HARD;
  270. if (src->d_fieldmask & FS_DQ_BTIMER)
  271. dst->d_fieldmask |= QC_SPC_TIMER;
  272. if (src->d_fieldmask & FS_DQ_ITIMER)
  273. dst->d_fieldmask |= QC_INO_TIMER;
  274. if (src->d_fieldmask & FS_DQ_RTBTIMER)
  275. dst->d_fieldmask |= QC_RT_SPC_TIMER;
  276. if (src->d_fieldmask & FS_DQ_BWARNS)
  277. dst->d_fieldmask |= QC_SPC_WARNS;
  278. if (src->d_fieldmask & FS_DQ_IWARNS)
  279. dst->d_fieldmask |= QC_INO_WARNS;
  280. if (src->d_fieldmask & FS_DQ_RTBWARNS)
  281. dst->d_fieldmask |= QC_RT_SPC_WARNS;
  282. if (src->d_fieldmask & FS_DQ_BCOUNT)
  283. dst->d_fieldmask |= QC_SPACE;
  284. if (src->d_fieldmask & FS_DQ_ICOUNT)
  285. dst->d_fieldmask |= QC_INO_COUNT;
  286. if (src->d_fieldmask & FS_DQ_RTBCOUNT)
  287. dst->d_fieldmask |= QC_RT_SPACE;
  288. }
  289. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  290. void __user *addr)
  291. {
  292. struct fs_disk_quota fdq;
  293. struct qc_dqblk qdq;
  294. struct kqid qid;
  295. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  296. return -EFAULT;
  297. if (!sb->s_qcop->set_dqblk)
  298. return -ENOSYS;
  299. qid = make_kqid(current_user_ns(), type, id);
  300. if (!qid_valid(qid))
  301. return -EINVAL;
  302. copy_from_xfs_dqblk(&qdq, &fdq);
  303. return sb->s_qcop->set_dqblk(sb, qid, &qdq);
  304. }
  305. static void copy_to_xfs_dqblk(struct fs_disk_quota *dst, struct qc_dqblk *src,
  306. int type, qid_t id)
  307. {
  308. memset(dst, 0, sizeof(*dst));
  309. dst->d_version = FS_DQUOT_VERSION;
  310. dst->d_id = id;
  311. if (type == USRQUOTA)
  312. dst->d_flags = FS_USER_QUOTA;
  313. else if (type == PRJQUOTA)
  314. dst->d_flags = FS_PROJ_QUOTA;
  315. else
  316. dst->d_flags = FS_GROUP_QUOTA;
  317. dst->d_blk_hardlimit = quota_btobb(src->d_spc_hardlimit);
  318. dst->d_blk_softlimit = quota_btobb(src->d_spc_softlimit);
  319. dst->d_ino_hardlimit = src->d_ino_hardlimit;
  320. dst->d_ino_softlimit = src->d_ino_softlimit;
  321. dst->d_bcount = quota_btobb(src->d_space);
  322. dst->d_icount = src->d_ino_count;
  323. dst->d_itimer = src->d_ino_timer;
  324. dst->d_btimer = src->d_spc_timer;
  325. dst->d_iwarns = src->d_ino_warns;
  326. dst->d_bwarns = src->d_spc_warns;
  327. dst->d_rtb_hardlimit = quota_btobb(src->d_rt_spc_hardlimit);
  328. dst->d_rtb_softlimit = quota_btobb(src->d_rt_spc_softlimit);
  329. dst->d_rtbcount = quota_btobb(src->d_rt_space);
  330. dst->d_rtbtimer = src->d_rt_spc_timer;
  331. dst->d_rtbwarns = src->d_rt_spc_warns;
  332. }
  333. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  334. void __user *addr)
  335. {
  336. struct fs_disk_quota fdq;
  337. struct qc_dqblk qdq;
  338. struct kqid qid;
  339. int ret;
  340. if (!sb->s_qcop->get_dqblk)
  341. return -ENOSYS;
  342. qid = make_kqid(current_user_ns(), type, id);
  343. if (!qid_valid(qid))
  344. return -EINVAL;
  345. ret = sb->s_qcop->get_dqblk(sb, qid, &qdq);
  346. if (ret)
  347. return ret;
  348. copy_to_xfs_dqblk(&fdq, &qdq, type, id);
  349. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  350. return -EFAULT;
  351. return ret;
  352. }
  353. static int quota_rmxquota(struct super_block *sb, void __user *addr)
  354. {
  355. __u32 flags;
  356. if (copy_from_user(&flags, addr, sizeof(flags)))
  357. return -EFAULT;
  358. if (!sb->s_qcop->rm_xquota)
  359. return -ENOSYS;
  360. return sb->s_qcop->rm_xquota(sb, flags);
  361. }
  362. /* Copy parameters and call proper function */
  363. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  364. void __user *addr, struct path *path)
  365. {
  366. int ret;
  367. if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
  368. return -EINVAL;
  369. if (!sb->s_qcop)
  370. return -ENOSYS;
  371. ret = check_quotactl_permission(sb, type, cmd, id);
  372. if (ret < 0)
  373. return ret;
  374. switch (cmd) {
  375. case Q_QUOTAON:
  376. return quota_quotaon(sb, type, cmd, id, path);
  377. case Q_QUOTAOFF:
  378. if (!sb->s_qcop->quota_off)
  379. return -ENOSYS;
  380. return sb->s_qcop->quota_off(sb, type);
  381. case Q_GETFMT:
  382. return quota_getfmt(sb, type, addr);
  383. case Q_GETINFO:
  384. return quota_getinfo(sb, type, addr);
  385. case Q_SETINFO:
  386. return quota_setinfo(sb, type, addr);
  387. case Q_GETQUOTA:
  388. return quota_getquota(sb, type, id, addr);
  389. case Q_SETQUOTA:
  390. return quota_setquota(sb, type, id, addr);
  391. case Q_SYNC:
  392. if (!sb->s_qcop->quota_sync)
  393. return -ENOSYS;
  394. return sb->s_qcop->quota_sync(sb, type);
  395. case Q_XQUOTAON:
  396. case Q_XQUOTAOFF:
  397. return quota_setxstate(sb, cmd, addr);
  398. case Q_XQUOTARM:
  399. return quota_rmxquota(sb, addr);
  400. case Q_XGETQSTAT:
  401. return quota_getxstate(sb, addr);
  402. case Q_XGETQSTATV:
  403. return quota_getxstatev(sb, addr);
  404. case Q_XSETQLIM:
  405. return quota_setxquota(sb, type, id, addr);
  406. case Q_XGETQUOTA:
  407. return quota_getxquota(sb, type, id, addr);
  408. case Q_XQUOTASYNC:
  409. if (sb->s_flags & MS_RDONLY)
  410. return -EROFS;
  411. /* XFS quotas are fully coherent now, making this call a noop */
  412. return 0;
  413. default:
  414. return -EINVAL;
  415. }
  416. }
  417. #ifdef CONFIG_BLOCK
  418. /* Return 1 if 'cmd' will block on frozen filesystem */
  419. static int quotactl_cmd_write(int cmd)
  420. {
  421. switch (cmd) {
  422. case Q_GETFMT:
  423. case Q_GETINFO:
  424. case Q_SYNC:
  425. case Q_XGETQSTAT:
  426. case Q_XGETQSTATV:
  427. case Q_XGETQUOTA:
  428. case Q_XQUOTASYNC:
  429. return 0;
  430. }
  431. return 1;
  432. }
  433. #endif /* CONFIG_BLOCK */
  434. /*
  435. * look up a superblock on which quota ops will be performed
  436. * - use the name of a block device to find the superblock thereon
  437. */
  438. static struct super_block *quotactl_block(const char __user *special, int cmd)
  439. {
  440. #ifdef CONFIG_BLOCK
  441. struct block_device *bdev;
  442. struct super_block *sb;
  443. struct filename *tmp = getname(special);
  444. if (IS_ERR(tmp))
  445. return ERR_CAST(tmp);
  446. bdev = lookup_bdev(tmp->name);
  447. putname(tmp);
  448. if (IS_ERR(bdev))
  449. return ERR_CAST(bdev);
  450. if (quotactl_cmd_write(cmd))
  451. sb = get_super_thawed(bdev);
  452. else
  453. sb = get_super(bdev);
  454. bdput(bdev);
  455. if (!sb)
  456. return ERR_PTR(-ENODEV);
  457. return sb;
  458. #else
  459. return ERR_PTR(-ENODEV);
  460. #endif
  461. }
  462. /*
  463. * This is the system call interface. This communicates with
  464. * the user-level programs. Currently this only supports diskquota
  465. * calls. Maybe we need to add the process quotas etc. in the future,
  466. * but we probably should use rlimits for that.
  467. */
  468. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  469. qid_t, id, void __user *, addr)
  470. {
  471. uint cmds, type;
  472. struct super_block *sb = NULL;
  473. struct path path, *pathp = NULL;
  474. int ret;
  475. cmds = cmd >> SUBCMDSHIFT;
  476. type = cmd & SUBCMDMASK;
  477. /*
  478. * As a special case Q_SYNC can be called without a specific device.
  479. * It will iterate all superblocks that have quota enabled and call
  480. * the sync action on each of them.
  481. */
  482. if (!special) {
  483. if (cmds == Q_SYNC)
  484. return quota_sync_all(type);
  485. return -ENODEV;
  486. }
  487. /*
  488. * Path for quotaon has to be resolved before grabbing superblock
  489. * because that gets s_umount sem which is also possibly needed by path
  490. * resolution (think about autofs) and thus deadlocks could arise.
  491. */
  492. if (cmds == Q_QUOTAON) {
  493. ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  494. if (ret)
  495. pathp = ERR_PTR(ret);
  496. else
  497. pathp = &path;
  498. }
  499. sb = quotactl_block(special, cmds);
  500. if (IS_ERR(sb)) {
  501. ret = PTR_ERR(sb);
  502. goto out;
  503. }
  504. ret = do_quotactl(sb, type, cmds, id, addr, pathp);
  505. drop_super(sb);
  506. out:
  507. if (pathp && !IS_ERR(pathp))
  508. path_put(pathp);
  509. return ret;
  510. }