quota.c 12 KB

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