quota_v2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * vfsv0 quota IO operations on file
  3. */
  4. #include <linux/errno.h>
  5. #include <linux/fs.h>
  6. #include <linux/mount.h>
  7. #include <linux/dqblk_v2.h>
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/quotaops.h>
  13. #include <asm/byteorder.h>
  14. #include "quota_tree.h"
  15. #include "quotaio_v2.h"
  16. MODULE_AUTHOR("Jan Kara");
  17. MODULE_DESCRIPTION("Quota format v2 support");
  18. MODULE_LICENSE("GPL");
  19. #define __QUOTA_V2_PARANOIA
  20. static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot);
  21. static void v2r0_disk2memdqb(struct dquot *dquot, void *dp);
  22. static int v2r0_is_id(void *dp, struct dquot *dquot);
  23. static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot);
  24. static void v2r1_disk2memdqb(struct dquot *dquot, void *dp);
  25. static int v2r1_is_id(void *dp, struct dquot *dquot);
  26. static struct qtree_fmt_operations v2r0_qtree_ops = {
  27. .mem2disk_dqblk = v2r0_mem2diskdqb,
  28. .disk2mem_dqblk = v2r0_disk2memdqb,
  29. .is_id = v2r0_is_id,
  30. };
  31. static struct qtree_fmt_operations v2r1_qtree_ops = {
  32. .mem2disk_dqblk = v2r1_mem2diskdqb,
  33. .disk2mem_dqblk = v2r1_disk2memdqb,
  34. .is_id = v2r1_is_id,
  35. };
  36. #define QUOTABLOCK_BITS 10
  37. #define QUOTABLOCK_SIZE (1 << QUOTABLOCK_BITS)
  38. static inline qsize_t v2_stoqb(qsize_t space)
  39. {
  40. return (space + QUOTABLOCK_SIZE - 1) >> QUOTABLOCK_BITS;
  41. }
  42. static inline qsize_t v2_qbtos(qsize_t blocks)
  43. {
  44. return blocks << QUOTABLOCK_BITS;
  45. }
  46. static int v2_read_header(struct super_block *sb, int type,
  47. struct v2_disk_dqheader *dqhead)
  48. {
  49. ssize_t size;
  50. size = sb->s_op->quota_read(sb, type, (char *)dqhead,
  51. sizeof(struct v2_disk_dqheader), 0);
  52. if (size != sizeof(struct v2_disk_dqheader)) {
  53. quota_error(sb, "Failed header read: expected=%zd got=%zd",
  54. sizeof(struct v2_disk_dqheader), size);
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. /* Check whether given file is really vfsv0 quotafile */
  60. static int v2_check_quota_file(struct super_block *sb, int type)
  61. {
  62. struct v2_disk_dqheader dqhead;
  63. static const uint quota_magics[] = V2_INITQMAGICS;
  64. static const uint quota_versions[] = V2_INITQVERSIONS;
  65. if (!v2_read_header(sb, type, &dqhead))
  66. return 0;
  67. if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
  68. le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
  69. return 0;
  70. return 1;
  71. }
  72. /* Read information header from quota file */
  73. static int v2_read_file_info(struct super_block *sb, int type)
  74. {
  75. struct v2_disk_dqinfo dinfo;
  76. struct v2_disk_dqheader dqhead;
  77. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  78. struct qtree_mem_dqinfo *qinfo;
  79. ssize_t size;
  80. unsigned int version;
  81. if (!v2_read_header(sb, type, &dqhead))
  82. return -1;
  83. version = le32_to_cpu(dqhead.dqh_version);
  84. if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
  85. (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1))
  86. return -1;
  87. size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
  88. sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
  89. if (size != sizeof(struct v2_disk_dqinfo)) {
  90. quota_error(sb, "Can't read info structure");
  91. return -1;
  92. }
  93. info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
  94. if (!info->dqi_priv) {
  95. printk(KERN_WARNING
  96. "Not enough memory for quota information structure.\n");
  97. return -ENOMEM;
  98. }
  99. qinfo = info->dqi_priv;
  100. if (version == 0) {
  101. /* limits are stored as unsigned 32-bit data */
  102. info->dqi_maxblimit = 0xffffffff;
  103. info->dqi_maxilimit = 0xffffffff;
  104. } else {
  105. /* used space is stored as unsigned 64-bit value */
  106. info->dqi_maxblimit = 0xffffffffffffffffULL; /* 2^64-1 */
  107. info->dqi_maxilimit = 0xffffffffffffffffULL;
  108. }
  109. info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
  110. info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
  111. info->dqi_flags = le32_to_cpu(dinfo.dqi_flags);
  112. qinfo->dqi_sb = sb;
  113. qinfo->dqi_type = type;
  114. qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
  115. qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
  116. qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
  117. qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
  118. qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
  119. qinfo->dqi_qtree_depth = qtree_depth(qinfo);
  120. if (version == 0) {
  121. qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
  122. qinfo->dqi_ops = &v2r0_qtree_ops;
  123. } else {
  124. qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
  125. qinfo->dqi_ops = &v2r1_qtree_ops;
  126. }
  127. return 0;
  128. }
  129. /* Write information header to quota file */
  130. static int v2_write_file_info(struct super_block *sb, int type)
  131. {
  132. struct v2_disk_dqinfo dinfo;
  133. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  134. struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
  135. ssize_t size;
  136. spin_lock(&dq_data_lock);
  137. info->dqi_flags &= ~DQF_INFO_DIRTY;
  138. dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
  139. dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
  140. dinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK);
  141. spin_unlock(&dq_data_lock);
  142. dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
  143. dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
  144. dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
  145. size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
  146. sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
  147. if (size != sizeof(struct v2_disk_dqinfo)) {
  148. quota_error(sb, "Can't write info structure");
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
  154. {
  155. struct v2r0_disk_dqblk *d = dp, empty;
  156. struct mem_dqblk *m = &dquot->dq_dqb;
  157. m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
  158. m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
  159. m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
  160. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  161. m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
  162. m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
  163. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  164. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  165. /* We need to escape back all-zero structure */
  166. memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
  167. empty.dqb_itime = cpu_to_le64(1);
  168. if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
  169. m->dqb_itime = 0;
  170. }
  171. static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
  172. {
  173. struct v2r0_disk_dqblk *d = dp;
  174. struct mem_dqblk *m = &dquot->dq_dqb;
  175. struct qtree_mem_dqinfo *info =
  176. sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
  177. d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
  178. d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
  179. d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
  180. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  181. d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
  182. d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
  183. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  184. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  185. d->dqb_id = cpu_to_le32(dquot->dq_id);
  186. if (qtree_entry_unused(info, dp))
  187. d->dqb_itime = cpu_to_le64(1);
  188. }
  189. static int v2r0_is_id(void *dp, struct dquot *dquot)
  190. {
  191. struct v2r0_disk_dqblk *d = dp;
  192. struct qtree_mem_dqinfo *info =
  193. sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
  194. if (qtree_entry_unused(info, dp))
  195. return 0;
  196. return le32_to_cpu(d->dqb_id) == dquot->dq_id;
  197. }
  198. static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
  199. {
  200. struct v2r1_disk_dqblk *d = dp, empty;
  201. struct mem_dqblk *m = &dquot->dq_dqb;
  202. m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
  203. m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
  204. m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
  205. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  206. m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
  207. m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
  208. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  209. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  210. /* We need to escape back all-zero structure */
  211. memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
  212. empty.dqb_itime = cpu_to_le64(1);
  213. if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
  214. m->dqb_itime = 0;
  215. }
  216. static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
  217. {
  218. struct v2r1_disk_dqblk *d = dp;
  219. struct mem_dqblk *m = &dquot->dq_dqb;
  220. struct qtree_mem_dqinfo *info =
  221. sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
  222. d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
  223. d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
  224. d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
  225. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  226. d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
  227. d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
  228. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  229. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  230. d->dqb_id = cpu_to_le32(dquot->dq_id);
  231. if (qtree_entry_unused(info, dp))
  232. d->dqb_itime = cpu_to_le64(1);
  233. }
  234. static int v2r1_is_id(void *dp, struct dquot *dquot)
  235. {
  236. struct v2r1_disk_dqblk *d = dp;
  237. struct qtree_mem_dqinfo *info =
  238. sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv;
  239. if (qtree_entry_unused(info, dp))
  240. return 0;
  241. return le32_to_cpu(d->dqb_id) == dquot->dq_id;
  242. }
  243. static int v2_read_dquot(struct dquot *dquot)
  244. {
  245. return qtree_read_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
  246. }
  247. static int v2_write_dquot(struct dquot *dquot)
  248. {
  249. return qtree_write_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
  250. }
  251. static int v2_release_dquot(struct dquot *dquot)
  252. {
  253. return qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_type)->dqi_priv, dquot);
  254. }
  255. static int v2_free_file_info(struct super_block *sb, int type)
  256. {
  257. kfree(sb_dqinfo(sb, type)->dqi_priv);
  258. return 0;
  259. }
  260. static const struct quota_format_ops v2_format_ops = {
  261. .check_quota_file = v2_check_quota_file,
  262. .read_file_info = v2_read_file_info,
  263. .write_file_info = v2_write_file_info,
  264. .free_file_info = v2_free_file_info,
  265. .read_dqblk = v2_read_dquot,
  266. .commit_dqblk = v2_write_dquot,
  267. .release_dqblk = v2_release_dquot,
  268. };
  269. static struct quota_format_type v2r0_quota_format = {
  270. .qf_fmt_id = QFMT_VFS_V0,
  271. .qf_ops = &v2_format_ops,
  272. .qf_owner = THIS_MODULE
  273. };
  274. static struct quota_format_type v2r1_quota_format = {
  275. .qf_fmt_id = QFMT_VFS_V1,
  276. .qf_ops = &v2_format_ops,
  277. .qf_owner = THIS_MODULE
  278. };
  279. static int __init init_v2_quota_format(void)
  280. {
  281. int ret;
  282. ret = register_quota_format(&v2r0_quota_format);
  283. if (ret)
  284. return ret;
  285. return register_quota_format(&v2r1_quota_format);
  286. }
  287. static void __exit exit_v2_quota_format(void)
  288. {
  289. unregister_quota_format(&v2r0_quota_format);
  290. unregister_quota_format(&v2r1_quota_format);
  291. }
  292. module_init(init_v2_quota_format);
  293. module_exit(exit_v2_quota_format);