quota_v2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS;
  103. info->dqi_max_ino_limit = 0xffffffff;
  104. } else {
  105. /*
  106. * Used space is stored as unsigned 64-bit value in bytes but
  107. * quota core supports only signed 64-bit values so use that
  108. * as a limit
  109. */
  110. info->dqi_max_spc_limit = 0x7fffffffffffffffLL; /* 2^63-1 */
  111. info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
  112. }
  113. info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
  114. info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
  115. /* No flags currently supported */
  116. info->dqi_flags = 0;
  117. qinfo->dqi_sb = sb;
  118. qinfo->dqi_type = type;
  119. qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
  120. qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
  121. qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
  122. qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
  123. qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
  124. qinfo->dqi_qtree_depth = qtree_depth(qinfo);
  125. if (version == 0) {
  126. qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
  127. qinfo->dqi_ops = &v2r0_qtree_ops;
  128. } else {
  129. qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
  130. qinfo->dqi_ops = &v2r1_qtree_ops;
  131. }
  132. return 0;
  133. }
  134. /* Write information header to quota file */
  135. static int v2_write_file_info(struct super_block *sb, int type)
  136. {
  137. struct v2_disk_dqinfo dinfo;
  138. struct mem_dqinfo *info = sb_dqinfo(sb, type);
  139. struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
  140. ssize_t size;
  141. spin_lock(&dq_data_lock);
  142. info->dqi_flags &= ~DQF_INFO_DIRTY;
  143. dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
  144. dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
  145. /* No flags currently supported */
  146. dinfo.dqi_flags = cpu_to_le32(0);
  147. spin_unlock(&dq_data_lock);
  148. dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
  149. dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
  150. dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
  151. size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
  152. sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
  153. if (size != sizeof(struct v2_disk_dqinfo)) {
  154. quota_error(sb, "Can't write info structure");
  155. return -1;
  156. }
  157. return 0;
  158. }
  159. static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
  160. {
  161. struct v2r0_disk_dqblk *d = dp, empty;
  162. struct mem_dqblk *m = &dquot->dq_dqb;
  163. m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
  164. m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
  165. m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
  166. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  167. m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
  168. m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
  169. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  170. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  171. /* We need to escape back all-zero structure */
  172. memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
  173. empty.dqb_itime = cpu_to_le64(1);
  174. if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
  175. m->dqb_itime = 0;
  176. }
  177. static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
  178. {
  179. struct v2r0_disk_dqblk *d = dp;
  180. struct mem_dqblk *m = &dquot->dq_dqb;
  181. struct qtree_mem_dqinfo *info =
  182. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  183. d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
  184. d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
  185. d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
  186. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  187. d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
  188. d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
  189. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  190. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  191. d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
  192. if (qtree_entry_unused(info, dp))
  193. d->dqb_itime = cpu_to_le64(1);
  194. }
  195. static int v2r0_is_id(void *dp, struct dquot *dquot)
  196. {
  197. struct v2r0_disk_dqblk *d = dp;
  198. struct qtree_mem_dqinfo *info =
  199. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  200. if (qtree_entry_unused(info, dp))
  201. return 0;
  202. return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
  203. le32_to_cpu(d->dqb_id)),
  204. dquot->dq_id);
  205. }
  206. static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
  207. {
  208. struct v2r1_disk_dqblk *d = dp, empty;
  209. struct mem_dqblk *m = &dquot->dq_dqb;
  210. m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
  211. m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
  212. m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
  213. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  214. m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
  215. m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
  216. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  217. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  218. /* We need to escape back all-zero structure */
  219. memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
  220. empty.dqb_itime = cpu_to_le64(1);
  221. if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
  222. m->dqb_itime = 0;
  223. }
  224. static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
  225. {
  226. struct v2r1_disk_dqblk *d = dp;
  227. struct mem_dqblk *m = &dquot->dq_dqb;
  228. struct qtree_mem_dqinfo *info =
  229. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  230. d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
  231. d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
  232. d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
  233. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  234. d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
  235. d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
  236. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  237. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  238. d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
  239. if (qtree_entry_unused(info, dp))
  240. d->dqb_itime = cpu_to_le64(1);
  241. }
  242. static int v2r1_is_id(void *dp, struct dquot *dquot)
  243. {
  244. struct v2r1_disk_dqblk *d = dp;
  245. struct qtree_mem_dqinfo *info =
  246. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  247. if (qtree_entry_unused(info, dp))
  248. return 0;
  249. return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
  250. le32_to_cpu(d->dqb_id)),
  251. dquot->dq_id);
  252. }
  253. static int v2_read_dquot(struct dquot *dquot)
  254. {
  255. return qtree_read_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
  256. }
  257. static int v2_write_dquot(struct dquot *dquot)
  258. {
  259. return qtree_write_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
  260. }
  261. static int v2_release_dquot(struct dquot *dquot)
  262. {
  263. return qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
  264. }
  265. static int v2_free_file_info(struct super_block *sb, int type)
  266. {
  267. kfree(sb_dqinfo(sb, type)->dqi_priv);
  268. return 0;
  269. }
  270. static const struct quota_format_ops v2_format_ops = {
  271. .check_quota_file = v2_check_quota_file,
  272. .read_file_info = v2_read_file_info,
  273. .write_file_info = v2_write_file_info,
  274. .free_file_info = v2_free_file_info,
  275. .read_dqblk = v2_read_dquot,
  276. .commit_dqblk = v2_write_dquot,
  277. .release_dqblk = v2_release_dquot,
  278. };
  279. static struct quota_format_type v2r0_quota_format = {
  280. .qf_fmt_id = QFMT_VFS_V0,
  281. .qf_ops = &v2_format_ops,
  282. .qf_owner = THIS_MODULE
  283. };
  284. static struct quota_format_type v2r1_quota_format = {
  285. .qf_fmt_id = QFMT_VFS_V1,
  286. .qf_ops = &v2_format_ops,
  287. .qf_owner = THIS_MODULE
  288. };
  289. static int __init init_v2_quota_format(void)
  290. {
  291. int ret;
  292. ret = register_quota_format(&v2r0_quota_format);
  293. if (ret)
  294. return ret;
  295. return register_quota_format(&v2r1_quota_format);
  296. }
  297. static void __exit exit_v2_quota_format(void)
  298. {
  299. unregister_quota_format(&v2r0_quota_format);
  300. unregister_quota_format(&v2r1_quota_format);
  301. }
  302. module_init(init_v2_quota_format);
  303. module_exit(exit_v2_quota_format);