quota_v1.c 5.7 KB

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