quota.c 12 KB

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