quota_v1.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include <linux/errno.h>
  2. #include <linux/fs.h>
  3. #include <linux/quota.h>
  4. #include <linux/quotaops.h>
  5. #include <linux/dqblk_v1.h>
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <asm/byteorder.h>
  10. #include "quotaio_v1.h"
  11. MODULE_AUTHOR("Jan Kara");
  12. MODULE_DESCRIPTION("Old quota format support");
  13. MODULE_LICENSE("GPL");
  14. #define QUOTABLOCK_BITS 10
  15. #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
  16. static inline qsize_t v1_stoqb(qsize_t space)
  17. {
  18. return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
  19. }
  20. static inline qsize_t v1_qbtos(qsize_t blocks)
  21. {
  22. return blocks << QUOTABLOCK_BITS;
  23. }
  24. static void v1_disk2mem_dqblk(struct mem_dqblk *m, struct v1_disk_dqblk *d)
  25. {
  26. m->dqb_ihardlimit = d->dqb_ihardlimit;
  27. m->dqb_isoftlimit = d->dqb_isoftlimit;
  28. m->dqb_curinodes = d->dqb_curinodes;
  29. m->dqb_bhardlimit = v1_qbtos(d->dqb_bhardlimit);
  30. m->dqb_bsoftlimit = v1_qbtos(d->dqb_bsoftlimit);
  31. m->dqb_curspace = v1_qbtos(d->dqb_curblocks);
  32. m->dqb_itime = d->dqb_itime;
  33. m->dqb_btime = d->dqb_btime;
  34. }
  35. static void v1_mem2disk_dqblk(struct v1_disk_dqblk *d, struct mem_dqblk *m)
  36. {
  37. d->dqb_ihardlimit = m->dqb_ihardlimit;
  38. d->dqb_isoftlimit = m->dqb_isoftlimit;
  39. d->dqb_curinodes = m->dqb_curinodes;
  40. d->dqb_bhardlimit = v1_stoqb(m->dqb_bhardlimit);
  41. d->dqb_bsoftlimit = v1_stoqb(m->dqb_bsoftlimit);
  42. d->dqb_curblocks = v1_stoqb(m->dqb_curspace);
  43. d->dqb_itime = m->dqb_itime;
  44. d->dqb_btime = m->dqb_btime;
  45. }
  46. static int v1_read_dqblk(struct dquot *dquot)
  47. {
  48. int type = dquot->dq_type;
  49. struct v1_disk_dqblk dqblk;
  50. if (!sb_dqopt(dquot->dq_sb)->files[type])
  51. return -EINVAL;
  52. /* Set structure to 0s in case read fails/is after end of file */
  53. memset(&dqblk, 0, sizeof(struct v1_disk_dqblk));
  54. dquot->dq_sb->s_op->quota_read(dquot->dq_sb, type, (char *)&dqblk, sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id));
  55. v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
  56. if (dquot->dq_dqb.dqb_bhardlimit == 0 && dquot->dq_dqb.dqb_bsoftlimit == 0 &&
  57. dquot->dq_dqb.dqb_ihardlimit == 0 && dquot->dq_dqb.dqb_isoftlimit == 0)
  58. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  59. dqstats.reads++;
  60. return 0;
  61. }
  62. static int v1_commit_dqblk(struct dquot *dquot)
  63. {
  64. short type = dquot->dq_type;
  65. ssize_t ret;
  66. struct v1_disk_dqblk dqblk;
  67. v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
  68. if (dquot->dq_id == 0) {
  69. dqblk.dqb_btime = sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
  70. dqblk.dqb_itime = sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
  71. }
  72. ret = 0;
  73. if (sb_dqopt(dquot->dq_sb)->files[type])
  74. ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type, (char *)&dqblk,
  75. sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id));
  76. if (ret != sizeof(struct v1_disk_dqblk)) {
  77. printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
  78. dquot->dq_sb->s_id);
  79. if (ret >= 0)
  80. ret = -EIO;
  81. goto out;
  82. }
  83. ret = 0;
  84. out:
  85. dqstats.writes++;
  86. return ret;
  87. }
  88. /* Magics of new quota format */
  89. #define V2_INITQMAGICS {\
  90. 0xd9c01f11, /* USRQUOTA */\
  91. 0xd9c01927 /* GRPQUOTA */\
  92. }
  93. /* Header of new quota format */
  94. struct v2_disk_dqheader {
  95. __le32 dqh_magic; /* Magic number identifying file */
  96. __le32 dqh_version; /* File version */
  97. };
  98. static int v1_check_quota_file(struct super_block *sb, int type)
  99. {
  100. struct inode *inode = sb_dqopt(sb)->files[type];
  101. ulong blocks;
  102. size_t off;
  103. struct v2_disk_dqheader dqhead;
  104. ssize_t size;
  105. loff_t isize;
  106. static const uint quota_magics[] = V2_INITQMAGICS;
  107. isize = i_size_read(inode);
  108. if (!isize)
  109. return 0;
  110. blocks = isize >> BLOCK_SIZE_BITS;
  111. off = isize & (BLOCK_SIZE - 1);
  112. if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) % sizeof(struct v1_disk_dqblk))
  113. return 0;
  114. /* Doublecheck whether we didn't get file with new format - with old quotactl() this could happen */
  115. size = sb->s_op->quota_read(sb, type, (char *)&dqhead, sizeof(struct v2_disk_dqheader), 0);
  116. if (size != sizeof(struct v2_disk_dqheader))
  117. return 1; /* Probably not new format */
  118. if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
  119. return 1; /* Definitely not new format */
  120. printk(KERN_INFO "VFS: %s: Refusing to turn on old quota format on given file. It probably contains newer quota format.\n", sb->s_id);
  121. return 0; /* Seems like a new format file -> refuse it */
  122. }
  123. static int v1_read_file_info(struct super_block *sb, int type)
  124. {
  125. struct quota_info *dqopt = sb_dqopt(sb);
  126. struct v1_disk_dqblk dqblk;
  127. int ret;
  128. if ((ret = sb->s_op->quota_read(sb, type, (char *)&dqblk, sizeof(struct v1_disk_dqblk), v1_dqoff(0))) != sizeof(struct v1_disk_dqblk)) {
  129. if (ret >= 0)
  130. ret = -EIO;
  131. goto out;
  132. }
  133. ret = 0;
  134. /* limits are stored as unsigned 32-bit data */
  135. dqopt->info[type].dqi_maxblimit = 0xffffffff;
  136. dqopt->info[type].dqi_maxilimit = 0xffffffff;
  137. dqopt->info[type].dqi_igrace = dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
  138. dqopt->info[type].dqi_bgrace = dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
  139. out:
  140. return ret;
  141. }
  142. static int v1_write_file_info(struct super_block *sb, int type)
  143. {
  144. struct quota_info *dqopt = sb_dqopt(sb);
  145. struct v1_disk_dqblk dqblk;
  146. int ret;
  147. dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
  148. if ((ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
  149. sizeof(struct v1_disk_dqblk), v1_dqoff(0))) != sizeof(struct v1_disk_dqblk)) {
  150. if (ret >= 0)
  151. ret = -EIO;
  152. goto out;
  153. }
  154. dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
  155. dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
  156. ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
  157. sizeof(struct v1_disk_dqblk), v1_dqoff(0));
  158. if (ret == sizeof(struct v1_disk_dqblk))
  159. ret = 0;
  160. else if (ret > 0)
  161. ret = -EIO;
  162. out:
  163. return ret;
  164. }
  165. static struct quota_format_ops v1_format_ops = {
  166. .check_quota_file = v1_check_quota_file,
  167. .read_file_info = v1_read_file_info,
  168. .write_file_info = v1_write_file_info,
  169. .free_file_info = NULL,
  170. .read_dqblk = v1_read_dqblk,
  171. .commit_dqblk = v1_commit_dqblk,
  172. };
  173. static struct quota_format_type v1_quota_format = {
  174. .qf_fmt_id = QFMT_VFS_OLD,
  175. .qf_ops = &v1_format_ops,
  176. .qf_owner = THIS_MODULE
  177. };
  178. static int __init init_v1_quota_format(void)
  179. {
  180. return register_quota_format(&v1_quota_format);
  181. }
  182. static void __exit exit_v1_quota_format(void)
  183. {
  184. unregister_quota_format(&v1_quota_format);
  185. }
  186. module_init(init_v1_quota_format);
  187. module_exit(exit_v1_quota_format);