quota_v2.c 10 KB

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