quota_v1.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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,
  55. sizeof(struct v1_disk_dqblk), v1_dqoff(dquot->dq_id));
  56. v1_disk2mem_dqblk(&dquot->dq_dqb, &dqblk);
  57. if (dquot->dq_dqb.dqb_bhardlimit == 0 &&
  58. dquot->dq_dqb.dqb_bsoftlimit == 0 &&
  59. dquot->dq_dqb.dqb_ihardlimit == 0 &&
  60. dquot->dq_dqb.dqb_isoftlimit == 0)
  61. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  62. dqstats.reads++;
  63. return 0;
  64. }
  65. static int v1_commit_dqblk(struct dquot *dquot)
  66. {
  67. short type = dquot->dq_type;
  68. ssize_t ret;
  69. struct v1_disk_dqblk dqblk;
  70. v1_mem2disk_dqblk(&dqblk, &dquot->dq_dqb);
  71. if (dquot->dq_id == 0) {
  72. dqblk.dqb_btime =
  73. sb_dqopt(dquot->dq_sb)->info[type].dqi_bgrace;
  74. dqblk.dqb_itime =
  75. sb_dqopt(dquot->dq_sb)->info[type].dqi_igrace;
  76. }
  77. ret = 0;
  78. if (sb_dqopt(dquot->dq_sb)->files[type])
  79. ret = dquot->dq_sb->s_op->quota_write(dquot->dq_sb, type,
  80. (char *)&dqblk, sizeof(struct v1_disk_dqblk),
  81. v1_dqoff(dquot->dq_id));
  82. if (ret != sizeof(struct v1_disk_dqblk)) {
  83. printk(KERN_WARNING "VFS: dquota write failed on dev %s\n",
  84. dquot->dq_sb->s_id);
  85. if (ret >= 0)
  86. ret = -EIO;
  87. goto out;
  88. }
  89. ret = 0;
  90. out:
  91. dqstats.writes++;
  92. return ret;
  93. }
  94. /* Magics of new quota format */
  95. #define V2_INITQMAGICS {\
  96. 0xd9c01f11, /* USRQUOTA */\
  97. 0xd9c01927 /* GRPQUOTA */\
  98. }
  99. /* Header of new quota format */
  100. struct v2_disk_dqheader {
  101. __le32 dqh_magic; /* Magic number identifying file */
  102. __le32 dqh_version; /* File version */
  103. };
  104. static int v1_check_quota_file(struct super_block *sb, int type)
  105. {
  106. struct inode *inode = sb_dqopt(sb)->files[type];
  107. ulong blocks;
  108. size_t off;
  109. struct v2_disk_dqheader dqhead;
  110. ssize_t size;
  111. loff_t isize;
  112. static const uint quota_magics[] = V2_INITQMAGICS;
  113. isize = i_size_read(inode);
  114. if (!isize)
  115. return 0;
  116. blocks = isize >> BLOCK_SIZE_BITS;
  117. off = isize & (BLOCK_SIZE - 1);
  118. if ((blocks % sizeof(struct v1_disk_dqblk) * BLOCK_SIZE + off) %
  119. sizeof(struct v1_disk_dqblk))
  120. return 0;
  121. /* Doublecheck whether we didn't get file with new format - with old
  122. * quotactl() this could happen */
  123. size = sb->s_op->quota_read(sb, type, (char *)&dqhead,
  124. sizeof(struct v2_disk_dqheader), 0);
  125. if (size != sizeof(struct v2_disk_dqheader))
  126. return 1; /* Probably not new format */
  127. if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type])
  128. return 1; /* Definitely not new format */
  129. printk(KERN_INFO
  130. "VFS: %s: Refusing to turn on old quota format on given file."
  131. " It probably contains newer quota format.\n", sb->s_id);
  132. return 0; /* Seems like a new format file -> refuse it */
  133. }
  134. static int v1_read_file_info(struct super_block *sb, int type)
  135. {
  136. struct quota_info *dqopt = sb_dqopt(sb);
  137. struct v1_disk_dqblk dqblk;
  138. int ret;
  139. ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
  140. sizeof(struct v1_disk_dqblk), v1_dqoff(0));
  141. if (ret != sizeof(struct v1_disk_dqblk)) {
  142. if (ret >= 0)
  143. ret = -EIO;
  144. goto out;
  145. }
  146. ret = 0;
  147. /* limits are stored as unsigned 32-bit data */
  148. dqopt->info[type].dqi_maxblimit = 0xffffffff;
  149. dqopt->info[type].dqi_maxilimit = 0xffffffff;
  150. dqopt->info[type].dqi_igrace =
  151. dqblk.dqb_itime ? dqblk.dqb_itime : MAX_IQ_TIME;
  152. dqopt->info[type].dqi_bgrace =
  153. dqblk.dqb_btime ? dqblk.dqb_btime : MAX_DQ_TIME;
  154. out:
  155. return ret;
  156. }
  157. static int v1_write_file_info(struct super_block *sb, int type)
  158. {
  159. struct quota_info *dqopt = sb_dqopt(sb);
  160. struct v1_disk_dqblk dqblk;
  161. int ret;
  162. dqopt->info[type].dqi_flags &= ~DQF_INFO_DIRTY;
  163. ret = sb->s_op->quota_read(sb, type, (char *)&dqblk,
  164. sizeof(struct v1_disk_dqblk), v1_dqoff(0));
  165. if (ret != sizeof(struct v1_disk_dqblk)) {
  166. if (ret >= 0)
  167. ret = -EIO;
  168. goto out;
  169. }
  170. dqblk.dqb_itime = dqopt->info[type].dqi_igrace;
  171. dqblk.dqb_btime = dqopt->info[type].dqi_bgrace;
  172. ret = sb->s_op->quota_write(sb, type, (char *)&dqblk,
  173. sizeof(struct v1_disk_dqblk), v1_dqoff(0));
  174. if (ret == sizeof(struct v1_disk_dqblk))
  175. ret = 0;
  176. else if (ret > 0)
  177. ret = -EIO;
  178. out:
  179. return ret;
  180. }
  181. static struct quota_format_ops v1_format_ops = {
  182. .check_quota_file = v1_check_quota_file,
  183. .read_file_info = v1_read_file_info,
  184. .write_file_info = v1_write_file_info,
  185. .free_file_info = NULL,
  186. .read_dqblk = v1_read_dqblk,
  187. .commit_dqblk = v1_commit_dqblk,
  188. };
  189. static struct quota_format_type v1_quota_format = {
  190. .qf_fmt_id = QFMT_VFS_OLD,
  191. .qf_ops = &v1_format_ops,
  192. .qf_owner = THIS_MODULE
  193. };
  194. static int __init init_v1_quota_format(void)
  195. {
  196. return register_quota_format(&v1_quota_format);
  197. }
  198. static void __exit exit_v1_quota_format(void)
  199. {
  200. unregister_quota_format(&v1_quota_format);
  201. }
  202. module_init(init_v1_quota_format);
  203. module_exit(exit_v1_quota_format);