quota.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 <net/netlink.h>
  21. #include <net/genetlink.h>
  22. static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
  23. qid_t id)
  24. {
  25. switch (cmd) {
  26. /* these commands do not require any special privilegues */
  27. case Q_GETFMT:
  28. case Q_SYNC:
  29. case Q_GETINFO:
  30. case Q_XGETQSTAT:
  31. case Q_XQUOTASYNC:
  32. break;
  33. /* allow to query information for dquots we "own" */
  34. case Q_GETQUOTA:
  35. case Q_XGETQUOTA:
  36. if ((type == USRQUOTA && current_euid() == id) ||
  37. (type == GRPQUOTA && in_egroup_p(id)))
  38. break;
  39. /*FALLTHROUGH*/
  40. default:
  41. if (!capable(CAP_SYS_ADMIN))
  42. return -EPERM;
  43. }
  44. return security_quotactl(cmd, type, id, sb);
  45. }
  46. #ifdef CONFIG_QUOTA
  47. void sync_quota_sb(struct super_block *sb, int type)
  48. {
  49. int cnt;
  50. if (!sb->s_qcop->quota_sync)
  51. return;
  52. sb->s_qcop->quota_sync(sb, type);
  53. if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
  54. return;
  55. /* This is not very clever (and fast) but currently I don't know about
  56. * any other simple way of getting quota data to disk and we must get
  57. * them there for userspace to be visible... */
  58. if (sb->s_op->sync_fs)
  59. sb->s_op->sync_fs(sb, 1);
  60. sync_blockdev(sb->s_bdev);
  61. /*
  62. * Now when everything is written we can discard the pagecache so
  63. * that userspace sees the changes.
  64. */
  65. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  66. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  67. if (type != -1 && cnt != type)
  68. continue;
  69. if (!sb_has_quota_active(sb, cnt))
  70. continue;
  71. mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
  72. I_MUTEX_QUOTA);
  73. truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
  74. mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
  75. }
  76. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  77. }
  78. #endif
  79. static int quota_sync_all(int type)
  80. {
  81. struct super_block *sb;
  82. int cnt;
  83. int ret;
  84. if (type >= MAXQUOTAS)
  85. return -EINVAL;
  86. ret = security_quotactl(Q_SYNC, type, 0, NULL);
  87. if (ret)
  88. return ret;
  89. spin_lock(&sb_lock);
  90. restart:
  91. list_for_each_entry(sb, &super_blocks, s_list) {
  92. /* This test just improves performance so it needn't be
  93. * reliable... */
  94. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  95. if (type != -1 && type != cnt)
  96. continue;
  97. if (!sb_has_quota_active(sb, cnt))
  98. continue;
  99. if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
  100. list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
  101. continue;
  102. break;
  103. }
  104. if (cnt == MAXQUOTAS)
  105. continue;
  106. sb->s_count++;
  107. spin_unlock(&sb_lock);
  108. down_read(&sb->s_umount);
  109. if (sb->s_root)
  110. sync_quota_sb(sb, type);
  111. up_read(&sb->s_umount);
  112. spin_lock(&sb_lock);
  113. if (__put_super_and_need_restart(sb))
  114. goto restart;
  115. }
  116. spin_unlock(&sb_lock);
  117. return 0;
  118. }
  119. static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
  120. void __user *addr)
  121. {
  122. char *pathname;
  123. int ret = -ENOSYS;
  124. pathname = getname(addr);
  125. if (IS_ERR(pathname))
  126. return PTR_ERR(pathname);
  127. if (sb->s_qcop->quota_on)
  128. ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
  129. putname(pathname);
  130. return ret;
  131. }
  132. static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
  133. {
  134. __u32 fmt;
  135. down_read(&sb_dqopt(sb)->dqptr_sem);
  136. if (!sb_has_quota_active(sb, type)) {
  137. up_read(&sb_dqopt(sb)->dqptr_sem);
  138. return -ESRCH;
  139. }
  140. fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
  141. up_read(&sb_dqopt(sb)->dqptr_sem);
  142. if (copy_to_user(addr, &fmt, sizeof(fmt)))
  143. return -EFAULT;
  144. return 0;
  145. }
  146. static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
  147. {
  148. struct if_dqinfo info;
  149. int ret;
  150. if (!sb_has_quota_active(sb, type))
  151. return -ESRCH;
  152. if (!sb->s_qcop->get_info)
  153. return -ENOSYS;
  154. ret = sb->s_qcop->get_info(sb, type, &info);
  155. if (!ret && copy_to_user(addr, &info, sizeof(info)))
  156. return -EFAULT;
  157. return ret;
  158. }
  159. static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
  160. {
  161. struct if_dqinfo info;
  162. if (copy_from_user(&info, addr, sizeof(info)))
  163. return -EFAULT;
  164. if (!sb_has_quota_active(sb, type))
  165. return -ESRCH;
  166. if (!sb->s_qcop->set_info)
  167. return -ENOSYS;
  168. return sb->s_qcop->set_info(sb, type, &info);
  169. }
  170. static int quota_getquota(struct super_block *sb, int type, qid_t id,
  171. void __user *addr)
  172. {
  173. struct if_dqblk idq;
  174. int ret;
  175. if (!sb_has_quota_active(sb, type))
  176. return -ESRCH;
  177. if (!sb->s_qcop->get_dqblk)
  178. return -ENOSYS;
  179. ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
  180. if (ret)
  181. return ret;
  182. if (copy_to_user(addr, &idq, sizeof(idq)))
  183. return -EFAULT;
  184. return 0;
  185. }
  186. static int quota_setquota(struct super_block *sb, int type, qid_t id,
  187. void __user *addr)
  188. {
  189. struct if_dqblk idq;
  190. if (copy_from_user(&idq, addr, sizeof(idq)))
  191. return -EFAULT;
  192. if (!sb_has_quota_active(sb, type))
  193. return -ESRCH;
  194. if (!sb->s_qcop->set_dqblk)
  195. return -ENOSYS;
  196. return sb->s_qcop->set_dqblk(sb, type, id, &idq);
  197. }
  198. static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
  199. {
  200. __u32 flags;
  201. if (copy_from_user(&flags, addr, sizeof(flags)))
  202. return -EFAULT;
  203. if (!sb->s_qcop->set_xstate)
  204. return -ENOSYS;
  205. return sb->s_qcop->set_xstate(sb, flags, cmd);
  206. }
  207. static int quota_getxstate(struct super_block *sb, void __user *addr)
  208. {
  209. struct fs_quota_stat fqs;
  210. int ret;
  211. if (!sb->s_qcop->get_xstate)
  212. return -ENOSYS;
  213. ret = sb->s_qcop->get_xstate(sb, &fqs);
  214. if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
  215. return -EFAULT;
  216. return ret;
  217. }
  218. static int quota_setxquota(struct super_block *sb, int type, qid_t id,
  219. void __user *addr)
  220. {
  221. struct fs_disk_quota fdq;
  222. if (copy_from_user(&fdq, addr, sizeof(fdq)))
  223. return -EFAULT;
  224. if (!sb->s_qcop->set_xquota)
  225. return -ENOSYS;
  226. return sb->s_qcop->set_xquota(sb, type, id, &fdq);
  227. }
  228. static int quota_getxquota(struct super_block *sb, int type, qid_t id,
  229. void __user *addr)
  230. {
  231. struct fs_disk_quota fdq;
  232. int ret;
  233. if (!sb->s_qcop->get_xquota)
  234. return -ENOSYS;
  235. ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
  236. if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
  237. return -EFAULT;
  238. return ret;
  239. }
  240. /* Copy parameters and call proper function */
  241. static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
  242. void __user *addr)
  243. {
  244. int ret;
  245. if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
  246. return -EINVAL;
  247. if (!sb->s_qcop)
  248. return -ENOSYS;
  249. ret = check_quotactl_permission(sb, type, cmd, id);
  250. if (ret < 0)
  251. return ret;
  252. switch (cmd) {
  253. case Q_QUOTAON:
  254. return quota_quotaon(sb, type, cmd, id, addr);
  255. case Q_QUOTAOFF:
  256. if (!sb->s_qcop->quota_off)
  257. return -ENOSYS;
  258. return sb->s_qcop->quota_off(sb, type, 0);
  259. case Q_GETFMT:
  260. return quota_getfmt(sb, type, addr);
  261. case Q_GETINFO:
  262. return quota_getinfo(sb, type, addr);
  263. case Q_SETINFO:
  264. return quota_setinfo(sb, type, addr);
  265. case Q_GETQUOTA:
  266. return quota_getquota(sb, type, id, addr);
  267. case Q_SETQUOTA:
  268. return quota_setquota(sb, type, id, addr);
  269. case Q_SYNC:
  270. if (!sb->s_qcop->quota_sync)
  271. return -ENOSYS;
  272. sync_quota_sb(sb, type);
  273. return 0;
  274. case Q_XQUOTAON:
  275. case Q_XQUOTAOFF:
  276. case Q_XQUOTARM:
  277. return quota_setxstate(sb, cmd, addr);
  278. case Q_XGETQSTAT:
  279. return quota_getxstate(sb, addr);
  280. case Q_XSETQLIM:
  281. return quota_setxquota(sb, type, id, addr);
  282. case Q_XGETQUOTA:
  283. return quota_getxquota(sb, type, id, addr);
  284. case Q_XQUOTASYNC:
  285. if (!sb->s_qcop->quota_sync)
  286. return -ENOSYS;
  287. return sb->s_qcop->quota_sync(sb, type);
  288. default:
  289. return -EINVAL;
  290. }
  291. }
  292. /*
  293. * look up a superblock on which quota ops will be performed
  294. * - use the name of a block device to find the superblock thereon
  295. */
  296. static struct super_block *quotactl_block(const char __user *special)
  297. {
  298. #ifdef CONFIG_BLOCK
  299. struct block_device *bdev;
  300. struct super_block *sb;
  301. char *tmp = getname(special);
  302. if (IS_ERR(tmp))
  303. return ERR_CAST(tmp);
  304. bdev = lookup_bdev(tmp);
  305. putname(tmp);
  306. if (IS_ERR(bdev))
  307. return ERR_CAST(bdev);
  308. sb = get_super(bdev);
  309. bdput(bdev);
  310. if (!sb)
  311. return ERR_PTR(-ENODEV);
  312. return sb;
  313. #else
  314. return ERR_PTR(-ENODEV);
  315. #endif
  316. }
  317. /*
  318. * This is the system call interface. This communicates with
  319. * the user-level programs. Currently this only supports diskquota
  320. * calls. Maybe we need to add the process quotas etc. in the future,
  321. * but we probably should use rlimits for that.
  322. */
  323. SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
  324. qid_t, id, void __user *, addr)
  325. {
  326. uint cmds, type;
  327. struct super_block *sb = NULL;
  328. int ret;
  329. cmds = cmd >> SUBCMDSHIFT;
  330. type = cmd & SUBCMDMASK;
  331. /*
  332. * As a special case Q_SYNC can be called without a specific device.
  333. * It will iterate all superblocks that have quota enabled and call
  334. * the sync action on each of them.
  335. */
  336. if (!special) {
  337. if (cmds == Q_SYNC)
  338. return quota_sync_all(type);
  339. return -ENODEV;
  340. }
  341. sb = quotactl_block(special);
  342. if (IS_ERR(sb))
  343. return PTR_ERR(sb);
  344. ret = do_quotactl(sb, type, cmds, id, addr);
  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
  458. #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
  459. /* Netlink family structure for quota */
  460. static struct genl_family quota_genl_family = {
  461. .id = GENL_ID_GENERATE,
  462. .hdrsize = 0,
  463. .name = "VFS_DQUOT",
  464. .version = 1,
  465. .maxattr = QUOTA_NL_A_MAX,
  466. };
  467. /**
  468. * quota_send_warning - Send warning to userspace about exceeded quota
  469. * @type: The quota type: USRQQUOTA, GRPQUOTA,...
  470. * @id: The user or group id of the quota that was exceeded
  471. * @dev: The device on which the fs is mounted (sb->s_dev)
  472. * @warntype: The type of the warning: QUOTA_NL_...
  473. *
  474. * This can be used by filesystems (including those which don't use
  475. * dquot) to send a message to userspace relating to quota limits.
  476. *
  477. */
  478. void quota_send_warning(short type, unsigned int id, dev_t dev,
  479. const char warntype)
  480. {
  481. static atomic_t seq;
  482. struct sk_buff *skb;
  483. void *msg_head;
  484. int ret;
  485. int msg_size = 4 * nla_total_size(sizeof(u32)) +
  486. 2 * nla_total_size(sizeof(u64));
  487. /* We have to allocate using GFP_NOFS as we are called from a
  488. * filesystem performing write and thus further recursion into
  489. * the fs to free some data could cause deadlocks. */
  490. skb = genlmsg_new(msg_size, GFP_NOFS);
  491. if (!skb) {
  492. printk(KERN_ERR
  493. "VFS: Not enough memory to send quota warning.\n");
  494. return;
  495. }
  496. msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
  497. &quota_genl_family, 0, QUOTA_NL_C_WARNING);
  498. if (!msg_head) {
  499. printk(KERN_ERR
  500. "VFS: Cannot store netlink header in quota warning.\n");
  501. goto err_out;
  502. }
  503. ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
  504. if (ret)
  505. goto attr_err_out;
  506. ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
  507. if (ret)
  508. goto attr_err_out;
  509. ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
  510. if (ret)
  511. goto attr_err_out;
  512. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
  513. if (ret)
  514. goto attr_err_out;
  515. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
  516. if (ret)
  517. goto attr_err_out;
  518. ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
  519. if (ret)
  520. goto attr_err_out;
  521. genlmsg_end(skb, msg_head);
  522. genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
  523. return;
  524. attr_err_out:
  525. printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
  526. err_out:
  527. kfree_skb(skb);
  528. }
  529. EXPORT_SYMBOL(quota_send_warning);
  530. static int __init quota_init(void)
  531. {
  532. if (genl_register_family(&quota_genl_family) != 0)
  533. printk(KERN_ERR
  534. "VFS: Failed to create quota netlink interface.\n");
  535. return 0;
  536. };
  537. module_init(quota_init);
  538. #endif