quota_v1.c 5.9 KB

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