quota.c 15 KB

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