quota_v2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 const 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 const 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. if (size < 0)
  56. return size;
  57. return -EIO;
  58. }
  59. return 0;
  60. }
  61. /* Check whether given file is really vfsv0 quotafile */
  62. static int v2_check_quota_file(struct super_block *sb, int type)
  63. {
  64. struct v2_disk_dqheader dqhead;
  65. static const uint quota_magics[] = V2_INITQMAGICS;
  66. static const uint quota_versions[] = V2_INITQVERSIONS;
  67. if (v2_read_header(sb, type, &dqhead))
  68. return 0;
  69. if (le32_to_cpu(dqhead.dqh_magic) != quota_magics[type] ||
  70. le32_to_cpu(dqhead.dqh_version) > quota_versions[type])
  71. return 0;
  72. return 1;
  73. }
  74. /* Read information header from quota file */
  75. static int v2_read_file_info(struct super_block *sb, int type)
  76. {
  77. struct v2_disk_dqinfo dinfo;
  78. struct v2_disk_dqheader dqhead;
  79. struct quota_info *dqopt = sb_dqopt(sb);
  80. struct mem_dqinfo *info = &dqopt->info[type];
  81. struct qtree_mem_dqinfo *qinfo;
  82. ssize_t size;
  83. unsigned int version;
  84. int ret;
  85. down_read(&dqopt->dqio_sem);
  86. ret = v2_read_header(sb, type, &dqhead);
  87. if (ret < 0)
  88. goto out;
  89. version = le32_to_cpu(dqhead.dqh_version);
  90. if ((info->dqi_fmt_id == QFMT_VFS_V0 && version != 0) ||
  91. (info->dqi_fmt_id == QFMT_VFS_V1 && version != 1)) {
  92. ret = -EINVAL;
  93. goto out;
  94. }
  95. size = sb->s_op->quota_read(sb, type, (char *)&dinfo,
  96. sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
  97. if (size != sizeof(struct v2_disk_dqinfo)) {
  98. quota_error(sb, "Can't read info structure");
  99. if (size < 0)
  100. ret = size;
  101. else
  102. ret = -EIO;
  103. goto out;
  104. }
  105. info->dqi_priv = kmalloc(sizeof(struct qtree_mem_dqinfo), GFP_NOFS);
  106. if (!info->dqi_priv) {
  107. ret = -ENOMEM;
  108. goto out;
  109. }
  110. qinfo = info->dqi_priv;
  111. if (version == 0) {
  112. /* limits are stored as unsigned 32-bit data */
  113. info->dqi_max_spc_limit = 0xffffffffLL << QUOTABLOCK_BITS;
  114. info->dqi_max_ino_limit = 0xffffffff;
  115. } else {
  116. /*
  117. * Used space is stored as unsigned 64-bit value in bytes but
  118. * quota core supports only signed 64-bit values so use that
  119. * as a limit
  120. */
  121. info->dqi_max_spc_limit = 0x7fffffffffffffffLL; /* 2^63-1 */
  122. info->dqi_max_ino_limit = 0x7fffffffffffffffLL;
  123. }
  124. info->dqi_bgrace = le32_to_cpu(dinfo.dqi_bgrace);
  125. info->dqi_igrace = le32_to_cpu(dinfo.dqi_igrace);
  126. /* No flags currently supported */
  127. info->dqi_flags = 0;
  128. qinfo->dqi_sb = sb;
  129. qinfo->dqi_type = type;
  130. qinfo->dqi_blocks = le32_to_cpu(dinfo.dqi_blocks);
  131. qinfo->dqi_free_blk = le32_to_cpu(dinfo.dqi_free_blk);
  132. qinfo->dqi_free_entry = le32_to_cpu(dinfo.dqi_free_entry);
  133. qinfo->dqi_blocksize_bits = V2_DQBLKSIZE_BITS;
  134. qinfo->dqi_usable_bs = 1 << V2_DQBLKSIZE_BITS;
  135. qinfo->dqi_qtree_depth = qtree_depth(qinfo);
  136. if (version == 0) {
  137. qinfo->dqi_entry_size = sizeof(struct v2r0_disk_dqblk);
  138. qinfo->dqi_ops = &v2r0_qtree_ops;
  139. } else {
  140. qinfo->dqi_entry_size = sizeof(struct v2r1_disk_dqblk);
  141. qinfo->dqi_ops = &v2r1_qtree_ops;
  142. }
  143. ret = 0;
  144. out:
  145. up_read(&dqopt->dqio_sem);
  146. return ret;
  147. }
  148. /* Write information header to quota file */
  149. static int v2_write_file_info(struct super_block *sb, int type)
  150. {
  151. struct v2_disk_dqinfo dinfo;
  152. struct quota_info *dqopt = sb_dqopt(sb);
  153. struct mem_dqinfo *info = &dqopt->info[type];
  154. struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
  155. ssize_t size;
  156. down_write(&dqopt->dqio_sem);
  157. spin_lock(&dq_data_lock);
  158. info->dqi_flags &= ~DQF_INFO_DIRTY;
  159. dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
  160. dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
  161. /* No flags currently supported */
  162. dinfo.dqi_flags = cpu_to_le32(0);
  163. spin_unlock(&dq_data_lock);
  164. dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
  165. dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
  166. dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
  167. size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
  168. sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
  169. up_write(&dqopt->dqio_sem);
  170. if (size != sizeof(struct v2_disk_dqinfo)) {
  171. quota_error(sb, "Can't write info structure");
  172. return -1;
  173. }
  174. return 0;
  175. }
  176. static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
  177. {
  178. struct v2r0_disk_dqblk *d = dp, empty;
  179. struct mem_dqblk *m = &dquot->dq_dqb;
  180. m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
  181. m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
  182. m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
  183. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  184. m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
  185. m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
  186. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  187. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  188. /* We need to escape back all-zero structure */
  189. memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
  190. empty.dqb_itime = cpu_to_le64(1);
  191. if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
  192. m->dqb_itime = 0;
  193. }
  194. static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
  195. {
  196. struct v2r0_disk_dqblk *d = dp;
  197. struct mem_dqblk *m = &dquot->dq_dqb;
  198. struct qtree_mem_dqinfo *info =
  199. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  200. d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
  201. d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
  202. d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
  203. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  204. d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
  205. d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
  206. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  207. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  208. d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
  209. if (qtree_entry_unused(info, dp))
  210. d->dqb_itime = cpu_to_le64(1);
  211. }
  212. static int v2r0_is_id(void *dp, struct dquot *dquot)
  213. {
  214. struct v2r0_disk_dqblk *d = dp;
  215. struct qtree_mem_dqinfo *info =
  216. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  217. if (qtree_entry_unused(info, dp))
  218. return 0;
  219. return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
  220. le32_to_cpu(d->dqb_id)),
  221. dquot->dq_id);
  222. }
  223. static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
  224. {
  225. struct v2r1_disk_dqblk *d = dp, empty;
  226. struct mem_dqblk *m = &dquot->dq_dqb;
  227. m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
  228. m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
  229. m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
  230. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  231. m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
  232. m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
  233. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  234. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  235. /* We need to escape back all-zero structure */
  236. memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
  237. empty.dqb_itime = cpu_to_le64(1);
  238. if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
  239. m->dqb_itime = 0;
  240. }
  241. static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
  242. {
  243. struct v2r1_disk_dqblk *d = dp;
  244. struct mem_dqblk *m = &dquot->dq_dqb;
  245. struct qtree_mem_dqinfo *info =
  246. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  247. d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
  248. d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
  249. d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
  250. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  251. d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
  252. d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
  253. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  254. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  255. d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
  256. if (qtree_entry_unused(info, dp))
  257. d->dqb_itime = cpu_to_le64(1);
  258. }
  259. static int v2r1_is_id(void *dp, struct dquot *dquot)
  260. {
  261. struct v2r1_disk_dqblk *d = dp;
  262. struct qtree_mem_dqinfo *info =
  263. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  264. if (qtree_entry_unused(info, dp))
  265. return 0;
  266. return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
  267. le32_to_cpu(d->dqb_id)),
  268. dquot->dq_id);
  269. }
  270. static int v2_read_dquot(struct dquot *dquot)
  271. {
  272. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  273. int ret;
  274. down_read(&dqopt->dqio_sem);
  275. ret = qtree_read_dquot(
  276. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
  277. dquot);
  278. up_read(&dqopt->dqio_sem);
  279. return ret;
  280. }
  281. static int v2_write_dquot(struct dquot *dquot)
  282. {
  283. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  284. int ret;
  285. bool alloc = false;
  286. /*
  287. * If space for dquot is already allocated, we don't need any
  288. * protection as we'll only overwrite the place of dquot. We are
  289. * still protected by concurrent writes of the same dquot by
  290. * dquot->dq_lock.
  291. */
  292. if (!dquot->dq_off) {
  293. alloc = true;
  294. down_write(&dqopt->dqio_sem);
  295. } else {
  296. down_read(&dqopt->dqio_sem);
  297. }
  298. ret = qtree_write_dquot(
  299. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
  300. dquot);
  301. if (alloc)
  302. up_write(&dqopt->dqio_sem);
  303. else
  304. up_read(&dqopt->dqio_sem);
  305. return ret;
  306. }
  307. static int v2_release_dquot(struct dquot *dquot)
  308. {
  309. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  310. int ret;
  311. down_write(&dqopt->dqio_sem);
  312. ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
  313. up_write(&dqopt->dqio_sem);
  314. return ret;
  315. }
  316. static int v2_free_file_info(struct super_block *sb, int type)
  317. {
  318. kfree(sb_dqinfo(sb, type)->dqi_priv);
  319. return 0;
  320. }
  321. static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
  322. {
  323. struct quota_info *dqopt = sb_dqopt(sb);
  324. int ret;
  325. down_read(&dqopt->dqio_sem);
  326. ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
  327. up_read(&dqopt->dqio_sem);
  328. return ret;
  329. }
  330. static const struct quota_format_ops v2_format_ops = {
  331. .check_quota_file = v2_check_quota_file,
  332. .read_file_info = v2_read_file_info,
  333. .write_file_info = v2_write_file_info,
  334. .free_file_info = v2_free_file_info,
  335. .read_dqblk = v2_read_dquot,
  336. .commit_dqblk = v2_write_dquot,
  337. .release_dqblk = v2_release_dquot,
  338. .get_next_id = v2_get_next_id,
  339. };
  340. static struct quota_format_type v2r0_quota_format = {
  341. .qf_fmt_id = QFMT_VFS_V0,
  342. .qf_ops = &v2_format_ops,
  343. .qf_owner = THIS_MODULE
  344. };
  345. static struct quota_format_type v2r1_quota_format = {
  346. .qf_fmt_id = QFMT_VFS_V1,
  347. .qf_ops = &v2_format_ops,
  348. .qf_owner = THIS_MODULE
  349. };
  350. static int __init init_v2_quota_format(void)
  351. {
  352. int ret;
  353. ret = register_quota_format(&v2r0_quota_format);
  354. if (ret)
  355. return ret;
  356. return register_quota_format(&v2r1_quota_format);
  357. }
  358. static void __exit exit_v2_quota_format(void)
  359. {
  360. unregister_quota_format(&v2r0_quota_format);
  361. unregister_quota_format(&v2r1_quota_format);
  362. }
  363. module_init(init_v2_quota_format);
  364. module_exit(exit_v2_quota_format);