quota_v2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vfsv0 quota IO operations on file
  4. */
  5. #include <linux/errno.h>
  6. #include <linux/fs.h>
  7. #include <linux/mount.h>
  8. #include <linux/dqblk_v2.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/quotaops.h>
  14. #include <asm/byteorder.h>
  15. #include "quota_tree.h"
  16. #include "quotaio_v2.h"
  17. MODULE_AUTHOR("Jan Kara");
  18. MODULE_DESCRIPTION("Quota format v2 support");
  19. MODULE_LICENSE("GPL");
  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 = -EUCLEAN;
  144. /* Some sanity checks of the read headers... */
  145. if ((loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits >
  146. i_size_read(sb_dqopt(sb)->files[type])) {
  147. quota_error(sb, "Number of blocks too big for quota file size (%llu > %llu).",
  148. (loff_t)qinfo->dqi_blocks << qinfo->dqi_blocksize_bits,
  149. i_size_read(sb_dqopt(sb)->files[type]));
  150. goto out_free;
  151. }
  152. if (qinfo->dqi_free_blk >= qinfo->dqi_blocks) {
  153. quota_error(sb, "Free block number too big (%u >= %u).",
  154. qinfo->dqi_free_blk, qinfo->dqi_blocks);
  155. goto out_free;
  156. }
  157. if (qinfo->dqi_free_entry >= qinfo->dqi_blocks) {
  158. quota_error(sb, "Block with free entry too big (%u >= %u).",
  159. qinfo->dqi_free_entry, qinfo->dqi_blocks);
  160. goto out_free;
  161. }
  162. ret = 0;
  163. out_free:
  164. if (ret) {
  165. kfree(info->dqi_priv);
  166. info->dqi_priv = NULL;
  167. }
  168. out:
  169. up_read(&dqopt->dqio_sem);
  170. return ret;
  171. }
  172. /* Write information header to quota file */
  173. static int v2_write_file_info(struct super_block *sb, int type)
  174. {
  175. struct v2_disk_dqinfo dinfo;
  176. struct quota_info *dqopt = sb_dqopt(sb);
  177. struct mem_dqinfo *info = &dqopt->info[type];
  178. struct qtree_mem_dqinfo *qinfo = info->dqi_priv;
  179. ssize_t size;
  180. down_write(&dqopt->dqio_sem);
  181. spin_lock(&dq_data_lock);
  182. info->dqi_flags &= ~DQF_INFO_DIRTY;
  183. dinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
  184. dinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
  185. /* No flags currently supported */
  186. dinfo.dqi_flags = cpu_to_le32(0);
  187. spin_unlock(&dq_data_lock);
  188. dinfo.dqi_blocks = cpu_to_le32(qinfo->dqi_blocks);
  189. dinfo.dqi_free_blk = cpu_to_le32(qinfo->dqi_free_blk);
  190. dinfo.dqi_free_entry = cpu_to_le32(qinfo->dqi_free_entry);
  191. size = sb->s_op->quota_write(sb, type, (char *)&dinfo,
  192. sizeof(struct v2_disk_dqinfo), V2_DQINFOOFF);
  193. up_write(&dqopt->dqio_sem);
  194. if (size != sizeof(struct v2_disk_dqinfo)) {
  195. quota_error(sb, "Can't write info structure");
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. static void v2r0_disk2memdqb(struct dquot *dquot, void *dp)
  201. {
  202. struct v2r0_disk_dqblk *d = dp, empty;
  203. struct mem_dqblk *m = &dquot->dq_dqb;
  204. m->dqb_ihardlimit = le32_to_cpu(d->dqb_ihardlimit);
  205. m->dqb_isoftlimit = le32_to_cpu(d->dqb_isoftlimit);
  206. m->dqb_curinodes = le32_to_cpu(d->dqb_curinodes);
  207. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  208. m->dqb_bhardlimit = v2_qbtos(le32_to_cpu(d->dqb_bhardlimit));
  209. m->dqb_bsoftlimit = v2_qbtos(le32_to_cpu(d->dqb_bsoftlimit));
  210. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  211. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  212. /* We need to escape back all-zero structure */
  213. memset(&empty, 0, sizeof(struct v2r0_disk_dqblk));
  214. empty.dqb_itime = cpu_to_le64(1);
  215. if (!memcmp(&empty, dp, sizeof(struct v2r0_disk_dqblk)))
  216. m->dqb_itime = 0;
  217. }
  218. static void v2r0_mem2diskdqb(void *dp, struct dquot *dquot)
  219. {
  220. struct v2r0_disk_dqblk *d = dp;
  221. struct mem_dqblk *m = &dquot->dq_dqb;
  222. struct qtree_mem_dqinfo *info =
  223. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  224. d->dqb_ihardlimit = cpu_to_le32(m->dqb_ihardlimit);
  225. d->dqb_isoftlimit = cpu_to_le32(m->dqb_isoftlimit);
  226. d->dqb_curinodes = cpu_to_le32(m->dqb_curinodes);
  227. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  228. d->dqb_bhardlimit = cpu_to_le32(v2_stoqb(m->dqb_bhardlimit));
  229. d->dqb_bsoftlimit = cpu_to_le32(v2_stoqb(m->dqb_bsoftlimit));
  230. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  231. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  232. d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
  233. if (qtree_entry_unused(info, dp))
  234. d->dqb_itime = cpu_to_le64(1);
  235. }
  236. static int v2r0_is_id(void *dp, struct dquot *dquot)
  237. {
  238. struct v2r0_disk_dqblk *d = dp;
  239. struct qtree_mem_dqinfo *info =
  240. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  241. if (qtree_entry_unused(info, dp))
  242. return 0;
  243. return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
  244. le32_to_cpu(d->dqb_id)),
  245. dquot->dq_id);
  246. }
  247. static void v2r1_disk2memdqb(struct dquot *dquot, void *dp)
  248. {
  249. struct v2r1_disk_dqblk *d = dp, empty;
  250. struct mem_dqblk *m = &dquot->dq_dqb;
  251. m->dqb_ihardlimit = le64_to_cpu(d->dqb_ihardlimit);
  252. m->dqb_isoftlimit = le64_to_cpu(d->dqb_isoftlimit);
  253. m->dqb_curinodes = le64_to_cpu(d->dqb_curinodes);
  254. m->dqb_itime = le64_to_cpu(d->dqb_itime);
  255. m->dqb_bhardlimit = v2_qbtos(le64_to_cpu(d->dqb_bhardlimit));
  256. m->dqb_bsoftlimit = v2_qbtos(le64_to_cpu(d->dqb_bsoftlimit));
  257. m->dqb_curspace = le64_to_cpu(d->dqb_curspace);
  258. m->dqb_btime = le64_to_cpu(d->dqb_btime);
  259. /* We need to escape back all-zero structure */
  260. memset(&empty, 0, sizeof(struct v2r1_disk_dqblk));
  261. empty.dqb_itime = cpu_to_le64(1);
  262. if (!memcmp(&empty, dp, sizeof(struct v2r1_disk_dqblk)))
  263. m->dqb_itime = 0;
  264. }
  265. static void v2r1_mem2diskdqb(void *dp, struct dquot *dquot)
  266. {
  267. struct v2r1_disk_dqblk *d = dp;
  268. struct mem_dqblk *m = &dquot->dq_dqb;
  269. struct qtree_mem_dqinfo *info =
  270. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  271. d->dqb_ihardlimit = cpu_to_le64(m->dqb_ihardlimit);
  272. d->dqb_isoftlimit = cpu_to_le64(m->dqb_isoftlimit);
  273. d->dqb_curinodes = cpu_to_le64(m->dqb_curinodes);
  274. d->dqb_itime = cpu_to_le64(m->dqb_itime);
  275. d->dqb_bhardlimit = cpu_to_le64(v2_stoqb(m->dqb_bhardlimit));
  276. d->dqb_bsoftlimit = cpu_to_le64(v2_stoqb(m->dqb_bsoftlimit));
  277. d->dqb_curspace = cpu_to_le64(m->dqb_curspace);
  278. d->dqb_btime = cpu_to_le64(m->dqb_btime);
  279. d->dqb_id = cpu_to_le32(from_kqid(&init_user_ns, dquot->dq_id));
  280. d->dqb_pad = 0;
  281. if (qtree_entry_unused(info, dp))
  282. d->dqb_itime = cpu_to_le64(1);
  283. }
  284. static int v2r1_is_id(void *dp, struct dquot *dquot)
  285. {
  286. struct v2r1_disk_dqblk *d = dp;
  287. struct qtree_mem_dqinfo *info =
  288. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv;
  289. if (qtree_entry_unused(info, dp))
  290. return 0;
  291. return qid_eq(make_kqid(&init_user_ns, dquot->dq_id.type,
  292. le32_to_cpu(d->dqb_id)),
  293. dquot->dq_id);
  294. }
  295. static int v2_read_dquot(struct dquot *dquot)
  296. {
  297. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  298. int ret;
  299. down_read(&dqopt->dqio_sem);
  300. ret = qtree_read_dquot(
  301. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
  302. dquot);
  303. up_read(&dqopt->dqio_sem);
  304. return ret;
  305. }
  306. static int v2_write_dquot(struct dquot *dquot)
  307. {
  308. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  309. int ret;
  310. bool alloc = false;
  311. /*
  312. * If space for dquot is already allocated, we don't need any
  313. * protection as we'll only overwrite the place of dquot. We are
  314. * still protected by concurrent writes of the same dquot by
  315. * dquot->dq_lock.
  316. */
  317. if (!dquot->dq_off) {
  318. alloc = true;
  319. down_write(&dqopt->dqio_sem);
  320. } else {
  321. down_read(&dqopt->dqio_sem);
  322. }
  323. ret = qtree_write_dquot(
  324. sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv,
  325. dquot);
  326. if (alloc)
  327. up_write(&dqopt->dqio_sem);
  328. else
  329. up_read(&dqopt->dqio_sem);
  330. return ret;
  331. }
  332. static int v2_release_dquot(struct dquot *dquot)
  333. {
  334. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  335. int ret;
  336. down_write(&dqopt->dqio_sem);
  337. ret = qtree_release_dquot(sb_dqinfo(dquot->dq_sb, dquot->dq_id.type)->dqi_priv, dquot);
  338. up_write(&dqopt->dqio_sem);
  339. return ret;
  340. }
  341. static int v2_free_file_info(struct super_block *sb, int type)
  342. {
  343. kfree(sb_dqinfo(sb, type)->dqi_priv);
  344. return 0;
  345. }
  346. static int v2_get_next_id(struct super_block *sb, struct kqid *qid)
  347. {
  348. struct quota_info *dqopt = sb_dqopt(sb);
  349. int ret;
  350. down_read(&dqopt->dqio_sem);
  351. ret = qtree_get_next_id(sb_dqinfo(sb, qid->type)->dqi_priv, qid);
  352. up_read(&dqopt->dqio_sem);
  353. return ret;
  354. }
  355. static const struct quota_format_ops v2_format_ops = {
  356. .check_quota_file = v2_check_quota_file,
  357. .read_file_info = v2_read_file_info,
  358. .write_file_info = v2_write_file_info,
  359. .free_file_info = v2_free_file_info,
  360. .read_dqblk = v2_read_dquot,
  361. .commit_dqblk = v2_write_dquot,
  362. .release_dqblk = v2_release_dquot,
  363. .get_next_id = v2_get_next_id,
  364. };
  365. static struct quota_format_type v2r0_quota_format = {
  366. .qf_fmt_id = QFMT_VFS_V0,
  367. .qf_ops = &v2_format_ops,
  368. .qf_owner = THIS_MODULE
  369. };
  370. static struct quota_format_type v2r1_quota_format = {
  371. .qf_fmt_id = QFMT_VFS_V1,
  372. .qf_ops = &v2_format_ops,
  373. .qf_owner = THIS_MODULE
  374. };
  375. static int __init init_v2_quota_format(void)
  376. {
  377. int ret;
  378. ret = register_quota_format(&v2r0_quota_format);
  379. if (ret)
  380. return ret;
  381. return register_quota_format(&v2r1_quota_format);
  382. }
  383. static void __exit exit_v2_quota_format(void)
  384. {
  385. unregister_quota_format(&v2r0_quota_format);
  386. unregister_quota_format(&v2r1_quota_format);
  387. }
  388. module_init(init_v2_quota_format);
  389. module_exit(exit_v2_quota_format);