quota.c 12 KB

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