shmem_quota.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * In memory quota format relies on quota infrastructure to store dquot
  4. * information for us. While conventional quota formats for file systems
  5. * with persistent storage can load quota information into dquot from the
  6. * storage on-demand and hence quota dquot shrinker can free any dquot
  7. * that is not currently being used, it must be avoided here. Otherwise we
  8. * can lose valuable information, user provided limits, because there is
  9. * no persistent storage to load the information from afterwards.
  10. *
  11. * One information that in-memory quota format needs to keep track of is
  12. * a sorted list of ids for each quota type. This is done by utilizing
  13. * an rb tree which root is stored in mem_dqinfo->dqi_priv for each quota
  14. * type.
  15. *
  16. * This format can be used to support quota on file system without persistent
  17. * storage such as tmpfs.
  18. *
  19. * Author: Lukas Czerner <lczerner@redhat.com>
  20. * Carlos Maiolino <cmaiolino@redhat.com>
  21. *
  22. * Copyright (C) 2023 Red Hat, Inc.
  23. */
  24. #include <linux/errno.h>
  25. #include <linux/fs.h>
  26. #include <linux/mount.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/shmem_fs.h>
  33. #include <linux/quotaops.h>
  34. #include <linux/quota.h>
  35. /*
  36. * The following constants define the amount of time given a user
  37. * before the soft limits are treated as hard limits (usually resulting
  38. * in an allocation failure). The timer is started when the user crosses
  39. * their soft limit, it is reset when they go below their soft limit.
  40. */
  41. #define SHMEM_MAX_IQ_TIME 604800 /* (7*24*60*60) 1 week */
  42. #define SHMEM_MAX_DQ_TIME 604800 /* (7*24*60*60) 1 week */
  43. struct quota_id {
  44. struct rb_node node;
  45. qid_t id;
  46. qsize_t bhardlimit;
  47. qsize_t bsoftlimit;
  48. qsize_t ihardlimit;
  49. qsize_t isoftlimit;
  50. };
  51. static int shmem_check_quota_file(struct super_block *sb, int type)
  52. {
  53. /* There is no real quota file, nothing to do */
  54. return 1;
  55. }
  56. /*
  57. * There is no real quota file. Just allocate rb_root for quota ids and
  58. * set limits
  59. */
  60. static int shmem_read_file_info(struct super_block *sb, int type)
  61. {
  62. struct quota_info *dqopt = sb_dqopt(sb);
  63. struct mem_dqinfo *info = &dqopt->info[type];
  64. info->dqi_priv = kzalloc(sizeof(struct rb_root), GFP_NOFS);
  65. if (!info->dqi_priv)
  66. return -ENOMEM;
  67. info->dqi_max_spc_limit = SHMEM_QUOTA_MAX_SPC_LIMIT;
  68. info->dqi_max_ino_limit = SHMEM_QUOTA_MAX_INO_LIMIT;
  69. info->dqi_bgrace = SHMEM_MAX_DQ_TIME;
  70. info->dqi_igrace = SHMEM_MAX_IQ_TIME;
  71. info->dqi_flags = 0;
  72. return 0;
  73. }
  74. static int shmem_write_file_info(struct super_block *sb, int type)
  75. {
  76. /* There is no real quota file, nothing to do */
  77. return 0;
  78. }
  79. /*
  80. * Free all the quota_id entries in the rb tree and rb_root.
  81. */
  82. static int shmem_free_file_info(struct super_block *sb, int type)
  83. {
  84. struct mem_dqinfo *info = &sb_dqopt(sb)->info[type];
  85. struct rb_root *root = info->dqi_priv;
  86. struct quota_id *entry;
  87. struct rb_node *node;
  88. info->dqi_priv = NULL;
  89. node = rb_first(root);
  90. while (node) {
  91. entry = rb_entry(node, struct quota_id, node);
  92. node = rb_next(&entry->node);
  93. rb_erase(&entry->node, root);
  94. kfree(entry);
  95. }
  96. kfree(root);
  97. return 0;
  98. }
  99. static int shmem_get_next_id(struct super_block *sb, struct kqid *qid)
  100. {
  101. struct mem_dqinfo *info = sb_dqinfo(sb, qid->type);
  102. struct rb_node *node;
  103. qid_t id = from_kqid(&init_user_ns, *qid);
  104. struct quota_info *dqopt = sb_dqopt(sb);
  105. struct quota_id *entry = NULL;
  106. int ret = 0;
  107. if (!sb_has_quota_active(sb, qid->type))
  108. return -ESRCH;
  109. down_read(&dqopt->dqio_sem);
  110. node = ((struct rb_root *)info->dqi_priv)->rb_node;
  111. while (node) {
  112. entry = rb_entry(node, struct quota_id, node);
  113. if (id < entry->id)
  114. node = node->rb_left;
  115. else if (id > entry->id)
  116. node = node->rb_right;
  117. else
  118. goto got_next_id;
  119. }
  120. if (!entry) {
  121. ret = -ENOENT;
  122. goto out_unlock;
  123. }
  124. if (id > entry->id) {
  125. node = rb_next(&entry->node);
  126. if (!node) {
  127. ret = -ENOENT;
  128. goto out_unlock;
  129. }
  130. entry = rb_entry(node, struct quota_id, node);
  131. }
  132. got_next_id:
  133. *qid = make_kqid(&init_user_ns, qid->type, entry->id);
  134. out_unlock:
  135. up_read(&dqopt->dqio_sem);
  136. return ret;
  137. }
  138. /*
  139. * Load dquot with limits from existing entry, or create the new entry if
  140. * it does not exist.
  141. */
  142. static int shmem_acquire_dquot(struct dquot *dquot)
  143. {
  144. struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type);
  145. struct rb_node **n;
  146. struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info;
  147. struct rb_node *parent = NULL, *new_node = NULL;
  148. struct quota_id *new_entry, *entry;
  149. qid_t id = from_kqid(&init_user_ns, dquot->dq_id);
  150. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  151. int ret = 0;
  152. mutex_lock(&dquot->dq_lock);
  153. down_write(&dqopt->dqio_sem);
  154. n = &((struct rb_root *)info->dqi_priv)->rb_node;
  155. while (*n) {
  156. parent = *n;
  157. entry = rb_entry(parent, struct quota_id, node);
  158. if (id < entry->id)
  159. n = &(*n)->rb_left;
  160. else if (id > entry->id)
  161. n = &(*n)->rb_right;
  162. else
  163. goto found;
  164. }
  165. /* We don't have entry for this id yet, create it */
  166. new_entry = kzalloc(sizeof(struct quota_id), GFP_NOFS);
  167. if (!new_entry) {
  168. ret = -ENOMEM;
  169. goto out_unlock;
  170. }
  171. new_entry->id = id;
  172. if (dquot->dq_id.type == USRQUOTA) {
  173. new_entry->bhardlimit = sbinfo->qlimits.usrquota_bhardlimit;
  174. new_entry->ihardlimit = sbinfo->qlimits.usrquota_ihardlimit;
  175. } else if (dquot->dq_id.type == GRPQUOTA) {
  176. new_entry->bhardlimit = sbinfo->qlimits.grpquota_bhardlimit;
  177. new_entry->ihardlimit = sbinfo->qlimits.grpquota_ihardlimit;
  178. }
  179. new_node = &new_entry->node;
  180. rb_link_node(new_node, parent, n);
  181. rb_insert_color(new_node, (struct rb_root *)info->dqi_priv);
  182. entry = new_entry;
  183. found:
  184. /* Load the stored limits from the tree */
  185. spin_lock(&dquot->dq_dqb_lock);
  186. dquot->dq_dqb.dqb_bhardlimit = entry->bhardlimit;
  187. dquot->dq_dqb.dqb_bsoftlimit = entry->bsoftlimit;
  188. dquot->dq_dqb.dqb_ihardlimit = entry->ihardlimit;
  189. dquot->dq_dqb.dqb_isoftlimit = entry->isoftlimit;
  190. if (!dquot->dq_dqb.dqb_bhardlimit &&
  191. !dquot->dq_dqb.dqb_bsoftlimit &&
  192. !dquot->dq_dqb.dqb_ihardlimit &&
  193. !dquot->dq_dqb.dqb_isoftlimit)
  194. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  195. spin_unlock(&dquot->dq_dqb_lock);
  196. /* Make sure flags update is visible after dquot has been filled */
  197. smp_mb__before_atomic();
  198. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  199. out_unlock:
  200. up_write(&dqopt->dqio_sem);
  201. mutex_unlock(&dquot->dq_lock);
  202. return ret;
  203. }
  204. static bool shmem_is_empty_dquot(struct dquot *dquot)
  205. {
  206. struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info;
  207. qsize_t bhardlimit;
  208. qsize_t ihardlimit;
  209. if (dquot->dq_id.type == USRQUOTA) {
  210. bhardlimit = sbinfo->qlimits.usrquota_bhardlimit;
  211. ihardlimit = sbinfo->qlimits.usrquota_ihardlimit;
  212. } else if (dquot->dq_id.type == GRPQUOTA) {
  213. bhardlimit = sbinfo->qlimits.grpquota_bhardlimit;
  214. ihardlimit = sbinfo->qlimits.grpquota_ihardlimit;
  215. }
  216. if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
  217. (dquot->dq_dqb.dqb_curspace == 0 &&
  218. dquot->dq_dqb.dqb_curinodes == 0 &&
  219. dquot->dq_dqb.dqb_bhardlimit == bhardlimit &&
  220. dquot->dq_dqb.dqb_ihardlimit == ihardlimit))
  221. return true;
  222. return false;
  223. }
  224. /*
  225. * Store limits from dquot in the tree unless it's fake. If it is fake
  226. * remove the id from the tree since there is no useful information in
  227. * there.
  228. */
  229. static int shmem_release_dquot(struct dquot *dquot)
  230. {
  231. struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type);
  232. struct rb_node *node;
  233. qid_t id = from_kqid(&init_user_ns, dquot->dq_id);
  234. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  235. struct quota_id *entry = NULL;
  236. mutex_lock(&dquot->dq_lock);
  237. /* Check whether we are not racing with some other dqget() */
  238. if (dquot_is_busy(dquot))
  239. goto out_dqlock;
  240. down_write(&dqopt->dqio_sem);
  241. node = ((struct rb_root *)info->dqi_priv)->rb_node;
  242. while (node) {
  243. entry = rb_entry(node, struct quota_id, node);
  244. if (id < entry->id)
  245. node = node->rb_left;
  246. else if (id > entry->id)
  247. node = node->rb_right;
  248. else
  249. goto found;
  250. }
  251. /* We should always find the entry in the rb tree */
  252. WARN_ONCE(1, "quota id %u from dquot %p, not in rb tree!\n", id, dquot);
  253. up_write(&dqopt->dqio_sem);
  254. mutex_unlock(&dquot->dq_lock);
  255. return -ENOENT;
  256. found:
  257. if (shmem_is_empty_dquot(dquot)) {
  258. /* Remove entry from the tree */
  259. rb_erase(&entry->node, info->dqi_priv);
  260. kfree(entry);
  261. } else {
  262. /* Store the limits in the tree */
  263. spin_lock(&dquot->dq_dqb_lock);
  264. entry->bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
  265. entry->bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
  266. entry->ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
  267. entry->isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
  268. spin_unlock(&dquot->dq_dqb_lock);
  269. }
  270. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  271. up_write(&dqopt->dqio_sem);
  272. out_dqlock:
  273. mutex_unlock(&dquot->dq_lock);
  274. return 0;
  275. }
  276. static int shmem_mark_dquot_dirty(struct dquot *dquot)
  277. {
  278. return 0;
  279. }
  280. static int shmem_dquot_write_info(struct super_block *sb, int type)
  281. {
  282. return 0;
  283. }
  284. static const struct quota_format_ops shmem_format_ops = {
  285. .check_quota_file = shmem_check_quota_file,
  286. .read_file_info = shmem_read_file_info,
  287. .write_file_info = shmem_write_file_info,
  288. .free_file_info = shmem_free_file_info,
  289. };
  290. struct quota_format_type shmem_quota_format = {
  291. .qf_fmt_id = QFMT_SHMEM,
  292. .qf_ops = &shmem_format_ops,
  293. .qf_owner = THIS_MODULE
  294. };
  295. const struct dquot_operations shmem_quota_operations = {
  296. .acquire_dquot = shmem_acquire_dquot,
  297. .release_dquot = shmem_release_dquot,
  298. .alloc_dquot = dquot_alloc,
  299. .destroy_dquot = dquot_destroy,
  300. .write_info = shmem_dquot_write_info,
  301. .mark_dirty = shmem_mark_dquot_dirty,
  302. .get_next_id = shmem_get_next_id,
  303. };