quota_v1.c 6.4 KB

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