quota.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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_enabled(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. /* This is not very clever (and fast) but currently I don't know about
  150. * any other simple way of getting quota data to disk and we must get
  151. * them there for userspace to be visible... */
  152. if (sb->s_op->sync_fs)
  153. sb->s_op->sync_fs(sb, 1);
  154. sync_blockdev(sb->s_bdev);
  155. /*
  156. * Now when everything is written we can discard the pagecache so
  157. * that userspace sees the changes.
  158. */
  159. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  160. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  161. if (type != -1 && cnt != type)
  162. continue;
  163. if (!sb_has_quota_enabled(sb, cnt))
  164. continue;
  165. mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex, I_MUTEX_QUOTA);
  166. truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
  167. mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
  168. }
  169. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  170. }
  171. void sync_dquots(struct super_block *sb, int type)
  172. {
  173. int cnt, dirty;
  174. if (sb) {
  175. if (sb->s_qcop->quota_sync)
  176. quota_sync_sb(sb, type);
  177. return;
  178. }
  179. spin_lock(&sb_lock);
  180. restart:
  181. list_for_each_entry(sb, &super_blocks, s_list) {
  182. /* This test just improves performance so it needn't be reliable... */
  183. for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
  184. if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
  185. && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
  186. dirty = 1;
  187. if (!dirty)
  188. continue;
  189. sb->s_count++;
  190. spin_unlock(&sb_lock);
  191. down_read(&sb->s_umount);
  192. if (sb->s_root && sb->s_qcop->quota_sync)
  193. quota_sync_sb(sb, type);
  194. up_read(&sb->s_umount);
  195. spin_lock(&sb_lock);
  196. if (__put_super_and_need_restart(sb))
  197. goto restart;
  198. }
  199. spin_unlock(&sb_lock);
  200. }
  201. /* Copy parameters and call proper function */
  202. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
  203. {
  204. int ret;
  205. switch (cmd) {
  206. case Q_QUOTAON: {
  207. char *pathname;
  208. if (IS_ERR(pathname = getname(addr)))
  209. return PTR_ERR(pathname);
  210. ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
  211. putname(pathname);
  212. return ret;
  213. }
  214. case Q_QUOTAOFF:
  215. return sb->s_qcop->quota_off(sb, type, 0);
  216. case Q_GETFMT: {
  217. __u32 fmt;
  218. down_read(&sb_dqopt(sb)->dqptr_sem);
  219. if (!sb_has_quota_enabled(sb, type)) {
  220. up_read(&sb_dqopt(sb)->dqptr_sem);
  221. return -ESRCH;
  222. }
  223. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  224. up_read(&sb_dqopt(sb)->dqptr_sem);
  225. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  226. return -EFAULT;
  227. return 0;
  228. }
  229. case Q_GETINFO: {
  230. struct if_dqinfo info;
  231. if ((ret = sb->s_qcop->get_info(sb, type, &info)))
  232. return ret;
  233. if (copy_to_user(addr, &info, sizeof(info)))
  234. return -EFAULT;
  235. return 0;
  236. }
  237. case Q_SETINFO: {
  238. struct if_dqinfo info;
  239. if (copy_from_user(&info, addr, sizeof(info)))
  240. return -EFAULT;
  241. return sb->s_qcop->set_info(sb, type, &info);
  242. }
  243. case Q_GETQUOTA: {
  244. struct if_dqblk idq;
  245. if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
  246. return ret;
  247. if (copy_to_user(addr, &idq, sizeof(idq)))
  248. return -EFAULT;
  249. return 0;
  250. }
  251. case Q_SETQUOTA: {
  252. struct if_dqblk idq;
  253. if (copy_from_user(&idq, addr, sizeof(idq)))
  254. return -EFAULT;
  255. return sb->s_qcop->set_dqblk(sb, type, id, &idq);
  256. }
  257. case Q_SYNC:
  258. sync_dquots(sb, type);
  259. return 0;
  260. case Q_XQUOTAON:
  261. case Q_XQUOTAOFF:
  262. case Q_XQUOTARM: {
  263. __u32 flags;
  264. if (copy_from_user(&flags, addr, sizeof(flags)))
  265. return -EFAULT;
  266. return sb->s_qcop->set_xstate(sb, flags, cmd);
  267. }
  268. case Q_XGETQSTAT: {
  269. struct fs_quota_stat fqs;
  270. if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
  271. return ret;
  272. if (copy_to_user(addr, &fqs, sizeof(fqs)))
  273. return -EFAULT;
  274. return 0;
  275. }
  276. case Q_XSETQLIM: {
  277. struct fs_disk_quota fdq;
  278. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  279. return -EFAULT;
  280. return sb->s_qcop->set_xquota(sb, type, id, &fdq);
  281. }
  282. case Q_XGETQUOTA: {
  283. struct fs_disk_quota fdq;
  284. if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
  285. return ret;
  286. if (copy_to_user(addr, &fdq, sizeof(fdq)))
  287. return -EFAULT;
  288. return 0;
  289. }
  290. case Q_XQUOTASYNC:
  291. return sb->s_qcop->quota_sync(sb, type);
  292. /* We never reach here unless validity check is broken */
  293. default:
  294. BUG();
  295. }
  296. return 0;
  297. }
  298. /*
  299. * look up a superblock on which quota ops will be performed
  300. * - use the name of a block device to find the superblock thereon
  301. */
  302. static inline struct super_block *quotactl_block(const char __user *special)
  303. {
  304. #ifdef CONFIG_BLOCK
  305. struct block_device *bdev;
  306. struct super_block *sb;
  307. char *tmp = getname(special);
  308. if (IS_ERR(tmp))
  309. return ERR_CAST(tmp);
  310. bdev = lookup_bdev(tmp);
  311. putname(tmp);
  312. if (IS_ERR(bdev))
  313. return ERR_CAST(bdev);
  314. sb = get_super(bdev);
  315. bdput(bdev);
  316. if (!sb)
  317. return ERR_PTR(-ENODEV);
  318. return sb;
  319. #else
  320. return ERR_PTR(-ENODEV);
  321. #endif
  322. }
  323. /*
  324. * This is the system call interface. This communicates with
  325. * the user-level programs. Currently this only supports diskquota
  326. * calls. Maybe we need to add the process quotas etc. in the future,
  327. * but we probably should use rlimits for that.
  328. */
  329. asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
  330. {
  331. uint cmds, type;
  332. struct super_block *sb = NULL;
  333. int ret;
  334. cmds = cmd >> SUBCMDSHIFT;
  335. type = cmd & SUBCMDMASK;
  336. if (cmds != Q_SYNC || special) {
  337. sb = quotactl_block(special);
  338. if (IS_ERR(sb))
  339. return PTR_ERR(sb);
  340. }
  341. ret = check_quotactl_valid(sb, type, cmds, id);
  342. if (ret >= 0)
  343. ret = do_quotactl(sb, type, cmds, id, addr);
  344. if (sb)
  345. drop_super(sb);
  346. return ret;
  347. }
  348. #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
  349. /*
  350. * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
  351. * and is necessary due to alignment problems.
  352. */
  353. struct compat_if_dqblk {
  354. compat_u64 dqb_bhardlimit;
  355. compat_u64 dqb_bsoftlimit;
  356. compat_u64 dqb_curspace;
  357. compat_u64 dqb_ihardlimit;
  358. compat_u64 dqb_isoftlimit;
  359. compat_u64 dqb_curinodes;
  360. compat_u64 dqb_btime;
  361. compat_u64 dqb_itime;
  362. compat_uint_t dqb_valid;
  363. };
  364. /* XFS structures */
  365. struct compat_fs_qfilestat {
  366. compat_u64 dqb_bhardlimit;
  367. compat_u64 qfs_nblks;
  368. compat_uint_t qfs_nextents;
  369. };
  370. struct compat_fs_quota_stat {
  371. __s8 qs_version;
  372. __u16 qs_flags;
  373. __s8 qs_pad;
  374. struct compat_fs_qfilestat qs_uquota;
  375. struct compat_fs_qfilestat qs_gquota;
  376. compat_uint_t qs_incoredqs;
  377. compat_int_t qs_btimelimit;
  378. compat_int_t qs_itimelimit;
  379. compat_int_t qs_rtbtimelimit;
  380. __u16 qs_bwarnlimit;
  381. __u16 qs_iwarnlimit;
  382. };
  383. asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
  384. qid_t id, void __user *addr)
  385. {
  386. unsigned int cmds;
  387. struct if_dqblk __user *dqblk;
  388. struct compat_if_dqblk __user *compat_dqblk;
  389. struct fs_quota_stat __user *fsqstat;
  390. struct compat_fs_quota_stat __user *compat_fsqstat;
  391. compat_uint_t data;
  392. u16 xdata;
  393. long ret;
  394. cmds = cmd >> SUBCMDSHIFT;
  395. switch (cmds) {
  396. case Q_GETQUOTA:
  397. dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
  398. compat_dqblk = addr;
  399. ret = sys_quotactl(cmd, special, id, dqblk);
  400. if (ret)
  401. break;
  402. if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
  403. get_user(data, &dqblk->dqb_valid) ||
  404. put_user(data, &compat_dqblk->dqb_valid))
  405. ret = -EFAULT;
  406. break;
  407. case Q_SETQUOTA:
  408. dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
  409. compat_dqblk = addr;
  410. ret = -EFAULT;
  411. if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
  412. get_user(data, &compat_dqblk->dqb_valid) ||
  413. put_user(data, &dqblk->dqb_valid))
  414. break;
  415. ret = sys_quotactl(cmd, special, id, dqblk);
  416. break;
  417. case Q_XGETQSTAT:
  418. fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
  419. compat_fsqstat = addr;
  420. ret = sys_quotactl(cmd, special, id, fsqstat);
  421. if (ret)
  422. break;
  423. ret = -EFAULT;
  424. /* Copying qs_version, qs_flags, qs_pad */
  425. if (copy_in_user(compat_fsqstat, fsqstat,
  426. offsetof(struct compat_fs_quota_stat, qs_uquota)))
  427. break;
  428. /* Copying qs_uquota */
  429. if (copy_in_user(&compat_fsqstat->qs_uquota,
  430. &fsqstat->qs_uquota,
  431. sizeof(compat_fsqstat->qs_uquota)) ||
  432. get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
  433. put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
  434. break;
  435. /* Copying qs_gquota */
  436. if (copy_in_user(&compat_fsqstat->qs_gquota,
  437. &fsqstat->qs_gquota,
  438. sizeof(compat_fsqstat->qs_gquota)) ||
  439. get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
  440. put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
  441. break;
  442. /* Copying the rest */
  443. if (copy_in_user(&compat_fsqstat->qs_incoredqs,
  444. &fsqstat->qs_incoredqs,
  445. sizeof(struct compat_fs_quota_stat) -
  446. offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
  447. get_user(xdata, &fsqstat->qs_iwarnlimit) ||
  448. put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
  449. break;
  450. ret = 0;
  451. break;
  452. default:
  453. ret = sys_quotactl(cmd, special, id, addr);
  454. }
  455. return ret;
  456. }
  457. #endif