quota_v1.c 6.6 KB

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