quota.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. #include <linux/writeback.h>
  21. #include <net/netlink.h>
  22. #include <net/genetlink.h>
  23. static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
  24. qid_t id)
  25. {
  26. switch (cmd) {
  27. /* these commands do not require any special privilegues */
  28. case Q_GETFMT:
  29. case Q_SYNC:
  30. case Q_GETINFO:
  31. case Q_XGETQSTAT:
  32. case Q_XQUOTASYNC:
  33. break;
  34. /* allow to query information for dquots we "own" */
  35. case Q_GETQUOTA:
  36. case Q_XGETQUOTA:
  37. if ((type == USRQUOTA && current_euid() == id) ||
  38. (type == GRPQUOTA && in_egroup_p(id)))
  39. break;
  40. /*FALLTHROUGH*/
  41. default:
  42. if (!capable(CAP_SYS_ADMIN))
  43. return -EPERM;
  44. }
  45. return security_quotactl(cmd, type, id, sb);
  46. }
  47. static int quota_sync_all(int type)
  48. {
  49. struct super_block *sb;
  50. int cnt;
  51. int ret;
  52. if (type >= MAXQUOTAS)
  53. return -EINVAL;
  54. ret = security_quotactl(Q_SYNC, type, 0, NULL);
  55. if (ret)
  56. return ret;
  57. spin_lock(&sb_lock);
  58. restart:
  59. list_for_each_entry(sb, &super_blocks, s_list) {
  60. if (!sb->s_qcop || !sb->s_qcop->quota_sync)
  61. continue;
  62. /* This test just improves performance so it needn't be
  63. * reliable... */
  64. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  65. if (type != -1 && type != cnt)
  66. continue;
  67. if (!sb_has_quota_active(sb, cnt))
  68. continue;
  69. if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
  70. list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
  71. continue;
  72. break;
  73. }
  74. if (cnt == MAXQUOTAS)
  75. continue;
  76. sb->s_count++;
  77. spin_unlock(&sb_lock);
  78. down_read(&sb->s_umount);
  79. if (sb->s_root)
  80. sb->s_qcop->quota_sync(sb, type, 1);
  81. up_read(&sb->s_umount);
  82. spin_lock(&sb_lock);
  83. if (__put_super_and_need_restart(sb))
  84. goto restart;
  85. }
  86. spin_unlock(&sb_lock);
  87. return 0;
  88. }
  89. static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
  90. void __user *addr)
  91. {
  92. char *pathname;
  93. int ret = -ENOSYS;
  94. pathname = getname(addr);
  95. if (IS_ERR(pathname))
  96. return PTR_ERR(pathname);
  97. if (sb->s_qcop->quota_on)
  98. ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
  99. putname(pathname);
  100. return ret;
  101. }
  102. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  103. {
  104. __u32 fmt;
  105. down_read(&sb_dqopt(sb)->dqptr_sem);
  106. if (!sb_has_quota_active(sb, type)) {
  107. up_read(&sb_dqopt(sb)->dqptr_sem);
  108. return -ESRCH;
  109. }
  110. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  111. up_read(&sb_dqopt(sb)->dqptr_sem);
  112. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  113. return -EFAULT;
  114. return 0;
  115. }
  116. static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
  117. {
  118. struct if_dqinfo info;
  119. int ret;
  120. if (!sb_has_quota_active(sb, type))
  121. return -ESRCH;
  122. if (!sb->s_qcop->get_info)
  123. return -ENOSYS;
  124. ret = sb->s_qcop->get_info(sb, type, &info);
  125. if (!ret && copy_to_user(addr, &info, sizeof(info)))
  126. return -EFAULT;
  127. return ret;
  128. }
  129. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  130. {
  131. struct if_dqinfo info;
  132. if (copy_from_user(&info, addr, sizeof(info)))
  133. return -EFAULT;
  134. if (!sb_has_quota_active(sb, type))
  135. return -ESRCH;
  136. if (!sb->s_qcop->set_info)
  137. return -ENOSYS;
  138. return sb->s_qcop->set_info(sb, type, &info);
  139. }
  140. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  141. void __user *addr)
  142. {
  143. struct if_dqblk idq;
  144. int ret;
  145. if (!sb_has_quota_active(sb, type))
  146. return -ESRCH;
  147. if (!sb->s_qcop->get_dqblk)
  148. return -ENOSYS;
  149. ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
  150. if (ret)
  151. return ret;
  152. if (copy_to_user(addr, &idq, sizeof(idq)))
  153. return -EFAULT;
  154. return 0;
  155. }
  156. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  157. void __user *addr)
  158. {
  159. struct if_dqblk idq;
  160. if (copy_from_user(&idq, addr, sizeof(idq)))
  161. return -EFAULT;
  162. if (!sb_has_quota_active(sb, type))
  163. return -ESRCH;
  164. if (!sb->s_qcop->set_dqblk)
  165. return -ENOSYS;
  166. return sb->s_qcop->set_dqblk(sb, type, id, &idq);
  167. }
  168. static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
  169. {
  170. __u32 flags;
  171. if (copy_from_user(&flags, addr, sizeof(flags)))
  172. return -EFAULT;
  173. if (!sb->s_qcop->set_xstate)
  174. return -ENOSYS;
  175. return sb->s_qcop->set_xstate(sb, flags, cmd);
  176. }
  177. static int quota_getxstate(struct super_block *sb, void __user *addr)
  178. {
  179. struct fs_quota_stat fqs;
  180. int ret;
  181. if (!sb->s_qcop->get_xstate)
  182. return -ENOSYS;
  183. ret = sb->s_qcop->get_xstate(sb, &fqs);
  184. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  185. return -EFAULT;
  186. return ret;
  187. }
  188. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  189. void __user *addr)
  190. {
  191. struct fs_disk_quota fdq;
  192. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  193. return -EFAULT;
  194. if (!sb->s_qcop->set_xquota)
  195. return -ENOSYS;
  196. return sb->s_qcop->set_xquota(sb, type, id, &fdq);
  197. }
  198. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  199. void __user *addr)
  200. {
  201. struct fs_disk_quota fdq;
  202. int ret;
  203. if (!sb->s_qcop->get_xquota)
  204. return -ENOSYS;
  205. ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
  206. if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
  207. return -EFAULT;
  208. return ret;
  209. }
  210. /* Copy parameters and call proper function */
  211. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  212. void __user *addr)
  213. {
  214. int ret;
  215. if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
  216. return -EINVAL;
  217. if (!sb->s_qcop)
  218. return -ENOSYS;
  219. ret = check_quotactl_permission(sb, type, cmd, id);
  220. if (ret < 0)
  221. return ret;
  222. switch (cmd) {
  223. case Q_QUOTAON:
  224. return quota_quotaon(sb, type, cmd, id, addr);
  225. case Q_QUOTAOFF:
  226. if (!sb->s_qcop->quota_off)
  227. return -ENOSYS;
  228. return sb->s_qcop->quota_off(sb, type, 0);
  229. case Q_GETFMT:
  230. return quota_getfmt(sb, type, addr);
  231. case Q_GETINFO:
  232. return quota_getinfo(sb, type, addr);
  233. case Q_SETINFO:
  234. return quota_setinfo(sb, type, addr);
  235. case Q_GETQUOTA:
  236. return quota_getquota(sb, type, id, addr);
  237. case Q_SETQUOTA:
  238. return quota_setquota(sb, type, id, addr);
  239. case Q_SYNC:
  240. if (!sb->s_qcop->quota_sync)
  241. return -ENOSYS;
  242. return sb->s_qcop->quota_sync(sb, type, 1);
  243. case Q_XQUOTAON:
  244. case Q_XQUOTAOFF:
  245. case Q_XQUOTARM:
  246. return quota_setxstate(sb, cmd, addr);
  247. case Q_XGETQSTAT:
  248. return quota_getxstate(sb, addr);
  249. case Q_XSETQLIM:
  250. return quota_setxquota(sb, type, id, addr);
  251. case Q_XGETQUOTA:
  252. return quota_getxquota(sb, type, id, addr);
  253. case Q_XQUOTASYNC:
  254. /* caller already holds s_umount */
  255. if (sb->s_flags & MS_RDONLY)
  256. return -EROFS;
  257. writeback_inodes_sb(sb);
  258. return 0;
  259. default:
  260. return -EINVAL;
  261. }
  262. }
  263. /*
  264. * look up a superblock on which quota ops will be performed
  265. * - use the name of a block device to find the superblock thereon
  266. */
  267. static struct super_block *quotactl_block(const char __user *special)
  268. {
  269. #ifdef CONFIG_BLOCK
  270. struct block_device *bdev;
  271. struct super_block *sb;
  272. char *tmp = getname(special);
  273. if (IS_ERR(tmp))
  274. return ERR_CAST(tmp);
  275. bdev = lookup_bdev(tmp);
  276. putname(tmp);
  277. if (IS_ERR(bdev))
  278. return ERR_CAST(bdev);
  279. sb = get_super(bdev);
  280. bdput(bdev);
  281. if (!sb)
  282. return ERR_PTR(-ENODEV);
  283. return sb;
  284. #else
  285. return ERR_PTR(-ENODEV);
  286. #endif
  287. }
  288. /*
  289. * This is the system call interface. This communicates with
  290. * the user-level programs. Currently this only supports diskquota
  291. * calls. Maybe we need to add the process quotas etc. in the future,
  292. * but we probably should use rlimits for that.
  293. */
  294. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  295. qid_t, id, void __user *, addr)
  296. {
  297. uint cmds, type;
  298. struct super_block *sb = NULL;
  299. int ret;
  300. cmds = cmd >> SUBCMDSHIFT;
  301. type = cmd & SUBCMDMASK;
  302. /*
  303. * As a special case Q_SYNC can be called without a specific device.
  304. * It will iterate all superblocks that have quota enabled and call
  305. * the sync action on each of them.
  306. */
  307. if (!special) {
  308. if (cmds == Q_SYNC)
  309. return quota_sync_all(type);
  310. return -ENODEV;
  311. }
  312. sb = quotactl_block(special);
  313. if (IS_ERR(sb))
  314. return PTR_ERR(sb);
  315. ret = do_quotactl(sb, type, cmds, id, addr);
  316. drop_super(sb);
  317. return ret;
  318. }
  319. #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
  320. /*
  321. * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
  322. * and is necessary due to alignment problems.
  323. */
  324. struct compat_if_dqblk {
  325. compat_u64 dqb_bhardlimit;
  326. compat_u64 dqb_bsoftlimit;
  327. compat_u64 dqb_curspace;
  328. compat_u64 dqb_ihardlimit;
  329. compat_u64 dqb_isoftlimit;
  330. compat_u64 dqb_curinodes;
  331. compat_u64 dqb_btime;
  332. compat_u64 dqb_itime;
  333. compat_uint_t dqb_valid;
  334. };
  335. /* XFS structures */
  336. struct compat_fs_qfilestat {
  337. compat_u64 dqb_bhardlimit;
  338. compat_u64 qfs_nblks;
  339. compat_uint_t qfs_nextents;
  340. };
  341. struct compat_fs_quota_stat {
  342. __s8 qs_version;
  343. __u16 qs_flags;
  344. __s8 qs_pad;
  345. struct compat_fs_qfilestat qs_uquota;
  346. struct compat_fs_qfilestat qs_gquota;
  347. compat_uint_t qs_incoredqs;
  348. compat_int_t qs_btimelimit;
  349. compat_int_t qs_itimelimit;
  350. compat_int_t qs_rtbtimelimit;
  351. __u16 qs_bwarnlimit;
  352. __u16 qs_iwarnlimit;
  353. };
  354. asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
  355. qid_t id, void __user *addr)
  356. {
  357. unsigned int cmds;
  358. struct if_dqblk __user *dqblk;
  359. struct compat_if_dqblk __user *compat_dqblk;
  360. struct fs_quota_stat __user *fsqstat;
  361. struct compat_fs_quota_stat __user *compat_fsqstat;
  362. compat_uint_t data;
  363. u16 xdata;
  364. long ret;
  365. cmds = cmd >> SUBCMDSHIFT;
  366. switch (cmds) {
  367. case Q_GETQUOTA:
  368. dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
  369. compat_dqblk = addr;
  370. ret = sys_quotactl(cmd, special, id, dqblk);
  371. if (ret)
  372. break;
  373. if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
  374. get_user(data, &dqblk->dqb_valid) ||
  375. put_user(data, &compat_dqblk->dqb_valid))
  376. ret = -EFAULT;
  377. break;
  378. case Q_SETQUOTA:
  379. dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
  380. compat_dqblk = addr;
  381. ret = -EFAULT;
  382. if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
  383. get_user(data, &compat_dqblk->dqb_valid) ||
  384. put_user(data, &dqblk->dqb_valid))
  385. break;
  386. ret = sys_quotactl(cmd, special, id, dqblk);
  387. break;
  388. case Q_XGETQSTAT:
  389. fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
  390. compat_fsqstat = addr;
  391. ret = sys_quotactl(cmd, special, id, fsqstat);
  392. if (ret)
  393. break;
  394. ret = -EFAULT;
  395. /* Copying qs_version, qs_flags, qs_pad */
  396. if (copy_in_user(compat_fsqstat, fsqstat,
  397. offsetof(struct compat_fs_quota_stat, qs_uquota)))
  398. break;
  399. /* Copying qs_uquota */
  400. if (copy_in_user(&compat_fsqstat->qs_uquota,
  401. &fsqstat->qs_uquota,
  402. sizeof(compat_fsqstat->qs_uquota)) ||
  403. get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
  404. put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
  405. break;
  406. /* Copying qs_gquota */
  407. if (copy_in_user(&compat_fsqstat->qs_gquota,
  408. &fsqstat->qs_gquota,
  409. sizeof(compat_fsqstat->qs_gquota)) ||
  410. get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
  411. put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
  412. break;
  413. /* Copying the rest */
  414. if (copy_in_user(&compat_fsqstat->qs_incoredqs,
  415. &fsqstat->qs_incoredqs,
  416. sizeof(struct compat_fs_quota_stat) -
  417. offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
  418. get_user(xdata, &fsqstat->qs_iwarnlimit) ||
  419. put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
  420. break;
  421. ret = 0;
  422. break;
  423. default:
  424. ret = sys_quotactl(cmd, special, id, addr);
  425. }
  426. return ret;
  427. }
  428. #endif
  429. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  430. /* Netlink family structure for quota */
  431. static struct genl_family quota_genl_family = {
  432. .id = GENL_ID_GENERATE,
  433. .hdrsize = 0,
  434. .name = "VFS_DQUOT",
  435. .version = 1,
  436. .maxattr = QUOTA_NL_A_MAX,
  437. };
  438. /**
  439. * quota_send_warning - Send warning to userspace about exceeded quota
  440. * @type: The quota type: USRQQUOTA, GRPQUOTA,...
  441. * @id: The user or group id of the quota that was exceeded
  442. * @dev: The device on which the fs is mounted (sb->s_dev)
  443. * @warntype: The type of the warning: QUOTA_NL_...
  444. *
  445. * This can be used by filesystems (including those which don't use
  446. * dquot) to send a message to userspace relating to quota limits.
  447. *
  448. */
  449. void quota_send_warning(short type, unsigned int id, dev_t dev,
  450. const char warntype)
  451. {
  452. static atomic_t seq;
  453. struct sk_buff *skb;
  454. void *msg_head;
  455. int ret;
  456. int msg_size = 4 * nla_total_size(sizeof(u32)) +
  457. 2 * nla_total_size(sizeof(u64));
  458. /* We have to allocate using GFP_NOFS as we are called from a
  459. * filesystem performing write and thus further recursion into
  460. * the fs to free some data could cause deadlocks. */
  461. skb = genlmsg_new(msg_size, GFP_NOFS);
  462. if (!skb) {
  463. printk(KERN_ERR
  464. "VFS: Not enough memory to send quota warning.\n");
  465. return;
  466. }
  467. msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
  468. &quota_genl_family, 0, QUOTA_NL_C_WARNING);
  469. if (!msg_head) {
  470. printk(KERN_ERR
  471. "VFS: Cannot store netlink header in quota warning.\n");
  472. goto err_out;
  473. }
  474. ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
  475. if (ret)
  476. goto attr_err_out;
  477. ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
  478. if (ret)
  479. goto attr_err_out;
  480. ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
  481. if (ret)
  482. goto attr_err_out;
  483. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
  484. if (ret)
  485. goto attr_err_out;
  486. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
  487. if (ret)
  488. goto attr_err_out;
  489. ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
  490. if (ret)
  491. goto attr_err_out;
  492. genlmsg_end(skb, msg_head);
  493. genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
  494. return;
  495. attr_err_out:
  496. printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
  497. err_out:
  498. kfree_skb(skb);
  499. }
  500. EXPORT_SYMBOL(quota_send_warning);
  501. static int __init quota_init(void)
  502. {
  503. if (genl_register_family(&quota_genl_family) != 0)
  504. printk(KERN_ERR
  505. "VFS: Failed to create quota netlink interface.\n");
  506. return 0;
  507. };
  508. module_init(quota_init);
  509. #endif