quota.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 <asm/uaccess.h>
  12. #include <linux/compat.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. /* Check validity of generic quotactl commands */
  21. static int generic_quotactl_valid(struct super_block *sb, int type, int cmd,
  22. qid_t id)
  23. {
  24. if (type >= MAXQUOTAS)
  25. return -EINVAL;
  26. if (!sb && cmd != Q_SYNC)
  27. return -ENODEV;
  28. /* Is operation supported? */
  29. if (sb && !sb->s_qcop)
  30. return -ENOSYS;
  31. switch (cmd) {
  32. case Q_GETFMT:
  33. break;
  34. case Q_QUOTAON:
  35. if (!sb->s_qcop->quota_on)
  36. return -ENOSYS;
  37. break;
  38. case Q_QUOTAOFF:
  39. if (!sb->s_qcop->quota_off)
  40. return -ENOSYS;
  41. break;
  42. case Q_SETINFO:
  43. if (!sb->s_qcop->set_info)
  44. return -ENOSYS;
  45. break;
  46. case Q_GETINFO:
  47. if (!sb->s_qcop->get_info)
  48. return -ENOSYS;
  49. break;
  50. case Q_SETQUOTA:
  51. if (!sb->s_qcop->set_dqblk)
  52. return -ENOSYS;
  53. break;
  54. case Q_GETQUOTA:
  55. if (!sb->s_qcop->get_dqblk)
  56. return -ENOSYS;
  57. break;
  58. case Q_SYNC:
  59. if (sb && !sb->s_qcop->quota_sync)
  60. return -ENOSYS;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. /* Is quota turned on for commands which need it? */
  66. switch (cmd) {
  67. case Q_GETFMT:
  68. case Q_GETINFO:
  69. case Q_SETINFO:
  70. case Q_SETQUOTA:
  71. case Q_GETQUOTA:
  72. /* This is just an informative test so we are satisfied
  73. * without the lock */
  74. if (!sb_has_quota_active(sb, type))
  75. return -ESRCH;
  76. }
  77. /* Check privileges */
  78. if (cmd == Q_GETQUOTA) {
  79. if (((type == USRQUOTA && current_euid() != id) ||
  80. (type == GRPQUOTA && !in_egroup_p(id))) &&
  81. !capable(CAP_SYS_ADMIN))
  82. return -EPERM;
  83. }
  84. else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
  85. if (!capable(CAP_SYS_ADMIN))
  86. return -EPERM;
  87. return 0;
  88. }
  89. /* Check validity of XFS Quota Manager commands */
  90. static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd,
  91. qid_t id)
  92. {
  93. if (type >= XQM_MAXQUOTAS)
  94. return -EINVAL;
  95. if (!sb)
  96. return -ENODEV;
  97. if (!sb->s_qcop)
  98. return -ENOSYS;
  99. switch (cmd) {
  100. case Q_XQUOTAON:
  101. case Q_XQUOTAOFF:
  102. case Q_XQUOTARM:
  103. if (!sb->s_qcop->set_xstate)
  104. return -ENOSYS;
  105. break;
  106. case Q_XGETQSTAT:
  107. if (!sb->s_qcop->get_xstate)
  108. return -ENOSYS;
  109. break;
  110. case Q_XSETQLIM:
  111. if (!sb->s_qcop->set_xquota)
  112. return -ENOSYS;
  113. break;
  114. case Q_XGETQUOTA:
  115. if (!sb->s_qcop->get_xquota)
  116. return -ENOSYS;
  117. break;
  118. case Q_XQUOTASYNC:
  119. if (!sb->s_qcop->quota_sync)
  120. return -ENOSYS;
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. /* Check privileges */
  126. if (cmd == Q_XGETQUOTA) {
  127. if (((type == XQM_USRQUOTA && current_euid() != id) ||
  128. (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
  129. !capable(CAP_SYS_ADMIN))
  130. return -EPERM;
  131. } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
  132. if (!capable(CAP_SYS_ADMIN))
  133. return -EPERM;
  134. }
  135. return 0;
  136. }
  137. static int check_quotactl_valid(struct super_block *sb, int type, int cmd,
  138. qid_t id)
  139. {
  140. int error;
  141. if (XQM_COMMAND(cmd))
  142. error = xqm_quotactl_valid(sb, type, cmd, id);
  143. else
  144. error = generic_quotactl_valid(sb, type, cmd, id);
  145. if (!error)
  146. error = security_quotactl(cmd, type, id, sb);
  147. return error;
  148. }
  149. static void quota_sync_sb(struct super_block *sb, int type)
  150. {
  151. int cnt;
  152. sb->s_qcop->quota_sync(sb, type);
  153. if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
  154. return;
  155. /* This is not very clever (and fast) but currently I don't know about
  156. * any other simple way of getting quota data to disk and we must get
  157. * them there for userspace to be visible... */
  158. if (sb->s_op->sync_fs)
  159. sb->s_op->sync_fs(sb, 1);
  160. sync_blockdev(sb->s_bdev);
  161. /*
  162. * Now when everything is written we can discard the pagecache so
  163. * that userspace sees the changes.
  164. */
  165. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  166. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  167. if (type != -1 && cnt != type)
  168. continue;
  169. if (!sb_has_quota_active(sb, cnt))
  170. continue;
  171. mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
  172. I_MUTEX_QUOTA);
  173. truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
  174. mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
  175. }
  176. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  177. }
  178. void sync_dquots(struct super_block *sb, int type)
  179. {
  180. int cnt;
  181. if (sb) {
  182. if (sb->s_qcop->quota_sync)
  183. quota_sync_sb(sb, type);
  184. return;
  185. }
  186. spin_lock(&sb_lock);
  187. restart:
  188. list_for_each_entry(sb, &super_blocks, s_list) {
  189. /* This test just improves performance so it needn't be
  190. * reliable... */
  191. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  192. if (type != -1 && type != cnt)
  193. continue;
  194. if (!sb_has_quota_active(sb, cnt))
  195. continue;
  196. if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
  197. list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
  198. continue;
  199. break;
  200. }
  201. if (cnt == MAXQUOTAS)
  202. continue;
  203. sb->s_count++;
  204. spin_unlock(&sb_lock);
  205. down_read(&sb->s_umount);
  206. if (sb->s_root && sb->s_qcop->quota_sync)
  207. quota_sync_sb(sb, type);
  208. up_read(&sb->s_umount);
  209. spin_lock(&sb_lock);
  210. if (__put_super_and_need_restart(sb))
  211. goto restart;
  212. }
  213. spin_unlock(&sb_lock);
  214. }
  215. /* Copy parameters and call proper function */
  216. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  217. void __user *addr)
  218. {
  219. int ret;
  220. switch (cmd) {
  221. case Q_QUOTAON: {
  222. char *pathname;
  223. pathname = getname(addr);
  224. if (IS_ERR(pathname))
  225. return PTR_ERR(pathname);
  226. ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
  227. putname(pathname);
  228. return ret;
  229. }
  230. case Q_QUOTAOFF:
  231. return sb->s_qcop->quota_off(sb, type, 0);
  232. case Q_GETFMT: {
  233. __u32 fmt;
  234. down_read(&sb_dqopt(sb)->dqptr_sem);
  235. if (!sb_has_quota_active(sb, type)) {
  236. up_read(&sb_dqopt(sb)->dqptr_sem);
  237. return -ESRCH;
  238. }
  239. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  240. up_read(&sb_dqopt(sb)->dqptr_sem);
  241. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  242. return -EFAULT;
  243. return 0;
  244. }
  245. case Q_GETINFO: {
  246. struct if_dqinfo info;
  247. ret = sb->s_qcop->get_info(sb, type, &info);
  248. if (ret)
  249. return ret;
  250. if (copy_to_user(addr, &info, sizeof(info)))
  251. return -EFAULT;
  252. return 0;
  253. }
  254. case Q_SETINFO: {
  255. struct if_dqinfo info;
  256. if (copy_from_user(&info, addr, sizeof(info)))
  257. return -EFAULT;
  258. return sb->s_qcop->set_info(sb, type, &info);
  259. }
  260. case Q_GETQUOTA: {
  261. struct if_dqblk idq;
  262. ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
  263. if (ret)
  264. return ret;
  265. if (copy_to_user(addr, &idq, sizeof(idq)))
  266. return -EFAULT;
  267. return 0;
  268. }
  269. case Q_SETQUOTA: {
  270. struct if_dqblk idq;
  271. if (copy_from_user(&idq, addr, sizeof(idq)))
  272. return -EFAULT;
  273. return sb->s_qcop->set_dqblk(sb, type, id, &idq);
  274. }
  275. case Q_SYNC:
  276. sync_dquots(sb, type);
  277. return 0;
  278. case Q_XQUOTAON:
  279. case Q_XQUOTAOFF:
  280. case Q_XQUOTARM: {
  281. __u32 flags;
  282. if (copy_from_user(&flags, addr, sizeof(flags)))
  283. return -EFAULT;
  284. return sb->s_qcop->set_xstate(sb, flags, cmd);
  285. }
  286. case Q_XGETQSTAT: {
  287. struct fs_quota_stat fqs;
  288. if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
  289. return ret;
  290. if (copy_to_user(addr, &fqs, sizeof(fqs)))
  291. return -EFAULT;
  292. return 0;
  293. }
  294. case Q_XSETQLIM: {
  295. struct fs_disk_quota fdq;
  296. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  297. return -EFAULT;
  298. return sb->s_qcop->set_xquota(sb, type, id, &fdq);
  299. }
  300. case Q_XGETQUOTA: {
  301. struct fs_disk_quota fdq;
  302. ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
  303. if (ret)
  304. return ret;
  305. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  306. return -EFAULT;
  307. return 0;
  308. }
  309. case Q_XQUOTASYNC:
  310. return sb->s_qcop->quota_sync(sb, type);
  311. /* We never reach here unless validity check is broken */
  312. default:
  313. BUG();
  314. }
  315. return 0;
  316. }
  317. /*
  318. * look up a superblock on which quota ops will be performed
  319. * - use the name of a block device to find the superblock thereon
  320. */
  321. static struct super_block *quotactl_block(const char __user *special)
  322. {
  323. #ifdef CONFIG_BLOCK
  324. struct block_device *bdev;
  325. struct super_block *sb;
  326. char *tmp = getname(special);
  327. if (IS_ERR(tmp))
  328. return ERR_CAST(tmp);
  329. bdev = lookup_bdev(tmp);
  330. putname(tmp);
  331. if (IS_ERR(bdev))
  332. return ERR_CAST(bdev);
  333. sb = get_super(bdev);
  334. bdput(bdev);
  335. if (!sb)
  336. return ERR_PTR(-ENODEV);
  337. return sb;
  338. #else
  339. return ERR_PTR(-ENODEV);
  340. #endif
  341. }
  342. /*
  343. * This is the system call interface. This communicates with
  344. * the user-level programs. Currently this only supports diskquota
  345. * calls. Maybe we need to add the process quotas etc. in the future,
  346. * but we probably should use rlimits for that.
  347. */
  348. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  349. qid_t, id, void __user *, addr)
  350. {
  351. uint cmds, type;
  352. struct super_block *sb = NULL;
  353. int ret;
  354. cmds = cmd >> SUBCMDSHIFT;
  355. type = cmd & SUBCMDMASK;
  356. if (cmds != Q_SYNC || special) {
  357. sb = quotactl_block(special);
  358. if (IS_ERR(sb))
  359. return PTR_ERR(sb);
  360. }
  361. ret = check_quotactl_valid(sb, type, cmds, id);
  362. if (ret >= 0)
  363. ret = do_quotactl(sb, type, cmds, id, addr);
  364. if (sb)
  365. drop_super(sb);
  366. return ret;
  367. }
  368. #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
  369. /*
  370. * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
  371. * and is necessary due to alignment problems.
  372. */
  373. struct compat_if_dqblk {
  374. compat_u64 dqb_bhardlimit;
  375. compat_u64 dqb_bsoftlimit;
  376. compat_u64 dqb_curspace;
  377. compat_u64 dqb_ihardlimit;
  378. compat_u64 dqb_isoftlimit;
  379. compat_u64 dqb_curinodes;
  380. compat_u64 dqb_btime;
  381. compat_u64 dqb_itime;
  382. compat_uint_t dqb_valid;
  383. };
  384. /* XFS structures */
  385. struct compat_fs_qfilestat {
  386. compat_u64 dqb_bhardlimit;
  387. compat_u64 qfs_nblks;
  388. compat_uint_t qfs_nextents;
  389. };
  390. struct compat_fs_quota_stat {
  391. __s8 qs_version;
  392. __u16 qs_flags;
  393. __s8 qs_pad;
  394. struct compat_fs_qfilestat qs_uquota;
  395. struct compat_fs_qfilestat qs_gquota;
  396. compat_uint_t qs_incoredqs;
  397. compat_int_t qs_btimelimit;
  398. compat_int_t qs_itimelimit;
  399. compat_int_t qs_rtbtimelimit;
  400. __u16 qs_bwarnlimit;
  401. __u16 qs_iwarnlimit;
  402. };
  403. asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
  404. qid_t id, void __user *addr)
  405. {
  406. unsigned int cmds;
  407. struct if_dqblk __user *dqblk;
  408. struct compat_if_dqblk __user *compat_dqblk;
  409. struct fs_quota_stat __user *fsqstat;
  410. struct compat_fs_quota_stat __user *compat_fsqstat;
  411. compat_uint_t data;
  412. u16 xdata;
  413. long ret;
  414. cmds = cmd >> SUBCMDSHIFT;
  415. switch (cmds) {
  416. case Q_GETQUOTA:
  417. dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
  418. compat_dqblk = addr;
  419. ret = sys_quotactl(cmd, special, id, dqblk);
  420. if (ret)
  421. break;
  422. if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
  423. get_user(data, &dqblk->dqb_valid) ||
  424. put_user(data, &compat_dqblk->dqb_valid))
  425. ret = -EFAULT;
  426. break;
  427. case Q_SETQUOTA:
  428. dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
  429. compat_dqblk = addr;
  430. ret = -EFAULT;
  431. if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
  432. get_user(data, &compat_dqblk->dqb_valid) ||
  433. put_user(data, &dqblk->dqb_valid))
  434. break;
  435. ret = sys_quotactl(cmd, special, id, dqblk);
  436. break;
  437. case Q_XGETQSTAT:
  438. fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
  439. compat_fsqstat = addr;
  440. ret = sys_quotactl(cmd, special, id, fsqstat);
  441. if (ret)
  442. break;
  443. ret = -EFAULT;
  444. /* Copying qs_version, qs_flags, qs_pad */
  445. if (copy_in_user(compat_fsqstat, fsqstat,
  446. offsetof(struct compat_fs_quota_stat, qs_uquota)))
  447. break;
  448. /* Copying qs_uquota */
  449. if (copy_in_user(&compat_fsqstat->qs_uquota,
  450. &fsqstat->qs_uquota,
  451. sizeof(compat_fsqstat->qs_uquota)) ||
  452. get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
  453. put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
  454. break;
  455. /* Copying qs_gquota */
  456. if (copy_in_user(&compat_fsqstat->qs_gquota,
  457. &fsqstat->qs_gquota,
  458. sizeof(compat_fsqstat->qs_gquota)) ||
  459. get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
  460. put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
  461. break;
  462. /* Copying the rest */
  463. if (copy_in_user(&compat_fsqstat->qs_incoredqs,
  464. &fsqstat->qs_incoredqs,
  465. sizeof(struct compat_fs_quota_stat) -
  466. offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
  467. get_user(xdata, &fsqstat->qs_iwarnlimit) ||
  468. put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
  469. break;
  470. ret = 0;
  471. break;
  472. default:
  473. ret = sys_quotactl(cmd, special, id, addr);
  474. }
  475. return ret;
  476. }
  477. #endif