quota.c 15 KB

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