| 1 | |
| 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 <[email protected]>
|
| 20 | * Carlos Maiolino <[email protected]>
|
| 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/magic.h>
|
| 34 |
|
| 35 | #include <linux/quotaops.h>
|
| 36 | #include <linux/quota.h>
|
| 37 |
|
| 38 | |
| 39 | * The following constants define the amount of time given a user
|
| 40 | * before the soft limits are treated as hard limits (usually resulting
|
| 41 | * in an allocation failure). The timer is started when the user crosses
|
| 42 | * their soft limit, it is reset when they go below their soft limit.
|
| 43 |
|
| 44 | #define SHMEM_MAX_IQ_TIME 604800
|
| 45 | #define SHMEM_MAX_DQ_TIME 604800
|
| 46 |
|
| 47 | struct quota_id {
|
| 48 | struct rb_node node;
|
| 49 | qid_t id;
|
| 50 | qsize_t bhardlimit;
|
| 51 | qsize_t bsoftlimit;
|
| 52 | qsize_t ihardlimit;
|
| 53 | qsize_t isoftlimit;
|
| 54 | };
|
| 55 |
|
| 56 | static int shmem_check_quota_file(struct super_block *sb, int type)
|
| 57 | {
|
| 58 |
|
| 59 | if (sb->s_magic != TMPFS_MAGIC)
|
| 60 | return 0;
|
| 61 |
|
| 62 |
|
| 63 | return 1;
|
| 64 | }
|
| 65 |
|
| 66 | |
| 67 | * There is no real quota file. Just allocate rb_root for quota ids and
|
| 68 | * set limits
|
| 69 |
|
| 70 | static int shmem_read_file_info(struct super_block *sb, int type)
|
| 71 | {
|
| 72 | struct quota_info *dqopt = sb_dqopt(sb);
|
| 73 | struct mem_dqinfo *info = &dqopt->info[type];
|
| 74 |
|
| 75 | info->dqi_priv = kzalloc_obj(struct rb_root, GFP_NOFS);
|
| 76 | if (!info->dqi_priv)
|
| 77 | return -ENOMEM;
|
| 78 |
|
| 79 | info->dqi_max_spc_limit = SHMEM_QUOTA_MAX_SPC_LIMIT;
|
| 80 | info->dqi_max_ino_limit = SHMEM_QUOTA_MAX_INO_LIMIT;
|
| 81 |
|
| 82 | info->dqi_bgrace = SHMEM_MAX_DQ_TIME;
|
| 83 | info->dqi_igrace = SHMEM_MAX_IQ_TIME;
|
| 84 | info->dqi_flags = 0;
|
| 85 |
|
| 86 | return 0;
|
| 87 | }
|
| 88 |
|
| 89 | static int shmem_write_file_info(struct super_block *sb, int type)
|
| 90 | {
|
| 91 |
|
| 92 | return 0;
|
| 93 | }
|
| 94 |
|
| 95 | |
| 96 | * Free all the quota_id entries in the rb tree and rb_root.
|
| 97 |
|
| 98 | static int shmem_free_file_info(struct super_block *sb, int type)
|
| 99 | {
|
| 100 | struct mem_dqinfo *info = &sb_dqopt(sb)->info[type];
|
| 101 | struct rb_root *root = info->dqi_priv;
|
| 102 | struct quota_id *entry;
|
| 103 | struct rb_node *node;
|
| 104 |
|
| 105 | info->dqi_priv = NULL;
|
| 106 | node = rb_first(root);
|
| 107 | while (node) {
|
| 108 | entry = rb_entry(node, struct quota_id, node);
|
| 109 | node = rb_next(&entry->node);
|
| 110 |
|
| 111 | rb_erase(&entry->node, root);
|
| 112 | kfree(entry);
|
| 113 | }
|
| 114 |
|
| 115 | kfree(root);
|
| 116 | return 0;
|
| 117 | }
|
| 118 |
|
| 119 | static int shmem_get_next_id(struct super_block *sb, struct kqid *qid)
|
| 120 | {
|
| 121 | struct mem_dqinfo *info = sb_dqinfo(sb, qid->type);
|
| 122 | struct rb_node *node;
|
| 123 | qid_t id = from_kqid(&init_user_ns, *qid);
|
| 124 | struct quota_info *dqopt = sb_dqopt(sb);
|
| 125 | struct quota_id *entry = NULL;
|
| 126 | int ret = 0;
|
| 127 |
|
| 128 | if (!sb_has_quota_active(sb, qid->type))
|
| 129 | return -ESRCH;
|
| 130 |
|
| 131 | down_read(&dqopt->dqio_sem);
|
| 132 | node = ((struct rb_root *)info->dqi_priv)->rb_node;
|
| 133 | while (node) {
|
| 134 | entry = rb_entry(node, struct quota_id, node);
|
| 135 |
|
| 136 | if (id < entry->id)
|
| 137 | node = node->rb_left;
|
| 138 | else if (id > entry->id)
|
| 139 | node = node->rb_right;
|
| 140 | else
|
| 141 | goto got_next_id;
|
| 142 | }
|
| 143 |
|
| 144 | if (!entry) {
|
| 145 | ret = -ENOENT;
|
| 146 | goto out_unlock;
|
| 147 | }
|
| 148 |
|
| 149 | if (id > entry->id) {
|
| 150 | node = rb_next(&entry->node);
|
| 151 | if (!node) {
|
| 152 | ret = -ENOENT;
|
| 153 | goto out_unlock;
|
| 154 | }
|
| 155 | entry = rb_entry(node, struct quota_id, node);
|
| 156 | }
|
| 157 |
|
| 158 | got_next_id:
|
| 159 | *qid = make_kqid(&init_user_ns, qid->type, entry->id);
|
| 160 | out_unlock:
|
| 161 | up_read(&dqopt->dqio_sem);
|
| 162 | return ret;
|
| 163 | }
|
| 164 |
|
| 165 | |
| 166 | * Load dquot with limits from existing entry, or create the new entry if
|
| 167 | * it does not exist.
|
| 168 |
|
| 169 | static int shmem_acquire_dquot(struct dquot *dquot)
|
| 170 | {
|
| 171 | struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type);
|
| 172 | struct rb_node **n;
|
| 173 | struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info;
|
| 174 | struct rb_node *parent = NULL, *new_node = NULL;
|
| 175 | struct quota_id *new_entry, *entry;
|
| 176 | qid_t id = from_kqid(&init_user_ns, dquot->dq_id);
|
| 177 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
|
| 178 | int ret = 0;
|
| 179 |
|
| 180 | mutex_lock(&dquot->dq_lock);
|
| 181 |
|
| 182 | down_write(&dqopt->dqio_sem);
|
| 183 | n = &((struct rb_root *)info->dqi_priv)->rb_node;
|
| 184 |
|
| 185 | while (*n) {
|
| 186 | parent = *n;
|
| 187 | entry = rb_entry(parent, struct quota_id, node);
|
| 188 |
|
| 189 | if (id < entry->id)
|
| 190 | n = &(*n)->rb_left;
|
| 191 | else if (id > entry->id)
|
| 192 | n = &(*n)->rb_right;
|
| 193 | else
|
| 194 | goto found;
|
| 195 | }
|
| 196 |
|
| 197 |
|
| 198 | new_entry = kzalloc_obj(struct quota_id, GFP_NOFS);
|
| 199 | if (!new_entry) {
|
| 200 | ret = -ENOMEM;
|
| 201 | goto out_unlock;
|
| 202 | }
|
| 203 |
|
| 204 | new_entry->id = id;
|
| 205 | if (dquot->dq_id.type == USRQUOTA) {
|
| 206 | new_entry->bhardlimit = sbinfo->qlimits.usrquota_bhardlimit;
|
| 207 | new_entry->ihardlimit = sbinfo->qlimits.usrquota_ihardlimit;
|
| 208 | } else if (dquot->dq_id.type == GRPQUOTA) {
|
| 209 | new_entry->bhardlimit = sbinfo->qlimits.grpquota_bhardlimit;
|
| 210 | new_entry->ihardlimit = sbinfo->qlimits.grpquota_ihardlimit;
|
| 211 | }
|
| 212 |
|
| 213 | new_node = &new_entry->node;
|
| 214 | rb_link_node(new_node, parent, n);
|
| 215 | rb_insert_color(new_node, (struct rb_root *)info->dqi_priv);
|
| 216 | entry = new_entry;
|
| 217 |
|
| 218 | found:
|
| 219 |
|
| 220 | spin_lock(&dquot->dq_dqb_lock);
|
| 221 | dquot->dq_dqb.dqb_bhardlimit = entry->bhardlimit;
|
| 222 | dquot->dq_dqb.dqb_bsoftlimit = entry->bsoftlimit;
|
| 223 | dquot->dq_dqb.dqb_ihardlimit = entry->ihardlimit;
|
| 224 | dquot->dq_dqb.dqb_isoftlimit = entry->isoftlimit;
|
| 225 |
|
| 226 | if (!dquot->dq_dqb.dqb_bhardlimit &&
|
| 227 | !dquot->dq_dqb.dqb_bsoftlimit &&
|
| 228 | !dquot->dq_dqb.dqb_ihardlimit &&
|
| 229 | !dquot->dq_dqb.dqb_isoftlimit)
|
| 230 | set_bit(DQ_FAKE_B, &dquot->dq_flags);
|
| 231 | spin_unlock(&dquot->dq_dqb_lock);
|
| 232 |
|
| 233 |
|
| 234 | smp_mb__before_atomic();
|
| 235 | set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
|
| 236 | out_unlock:
|
| 237 | up_write(&dqopt->dqio_sem);
|
| 238 | mutex_unlock(&dquot->dq_lock);
|
| 239 | return ret;
|
| 240 | }
|
| 241 |
|
| 242 | static bool shmem_is_empty_dquot(struct dquot *dquot)
|
| 243 | {
|
| 244 | struct shmem_sb_info *sbinfo = dquot->dq_sb->s_fs_info;
|
| 245 | qsize_t bhardlimit;
|
| 246 | qsize_t ihardlimit;
|
| 247 |
|
| 248 | if (dquot->dq_id.type == USRQUOTA) {
|
| 249 | bhardlimit = sbinfo->qlimits.usrquota_bhardlimit;
|
| 250 | ihardlimit = sbinfo->qlimits.usrquota_ihardlimit;
|
| 251 | } else if (dquot->dq_id.type == GRPQUOTA) {
|
| 252 | bhardlimit = sbinfo->qlimits.grpquota_bhardlimit;
|
| 253 | ihardlimit = sbinfo->qlimits.grpquota_ihardlimit;
|
| 254 | }
|
| 255 |
|
| 256 | if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
|
| 257 | (dquot->dq_dqb.dqb_curspace == 0 &&
|
| 258 | dquot->dq_dqb.dqb_curinodes == 0 &&
|
| 259 | dquot->dq_dqb.dqb_bhardlimit == bhardlimit &&
|
| 260 | dquot->dq_dqb.dqb_ihardlimit == ihardlimit))
|
| 261 | return true;
|
| 262 |
|
| 263 | return false;
|
| 264 | }
|
| 265 | |
| 266 | * Store limits from dquot in the tree unless it's fake. If it is fake
|
| 267 | * remove the id from the tree since there is no useful information in
|
| 268 | * there.
|
| 269 |
|
| 270 | static int shmem_release_dquot(struct dquot *dquot)
|
| 271 | {
|
| 272 | struct mem_dqinfo *info = sb_dqinfo(dquot->dq_sb, dquot->dq_id.type);
|
| 273 | struct rb_node *node;
|
| 274 | qid_t id = from_kqid(&init_user_ns, dquot->dq_id);
|
| 275 | struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
|
| 276 | struct quota_id *entry = NULL;
|
| 277 |
|
| 278 | mutex_lock(&dquot->dq_lock);
|
| 279 |
|
| 280 | if (dquot_is_busy(dquot))
|
| 281 | goto out_dqlock;
|
| 282 |
|
| 283 | down_write(&dqopt->dqio_sem);
|
| 284 | node = ((struct rb_root *)info->dqi_priv)->rb_node;
|
| 285 | while (node) {
|
| 286 | entry = rb_entry(node, struct quota_id, node);
|
| 287 |
|
| 288 | if (id < entry->id)
|
| 289 | node = node->rb_left;
|
| 290 | else if (id > entry->id)
|
| 291 | node = node->rb_right;
|
| 292 | else
|
| 293 | goto found;
|
| 294 | }
|
| 295 |
|
| 296 |
|
| 297 | WARN_ONCE(1, "quota id %u from dquot %p, not in rb tree!\n", id, dquot);
|
| 298 | up_write(&dqopt->dqio_sem);
|
| 299 | mutex_unlock(&dquot->dq_lock);
|
| 300 | return -ENOENT;
|
| 301 |
|
| 302 | found:
|
| 303 | if (shmem_is_empty_dquot(dquot)) {
|
| 304 |
|
| 305 | rb_erase(&entry->node, info->dqi_priv);
|
| 306 | kfree(entry);
|
| 307 | } else {
|
| 308 |
|
| 309 | spin_lock(&dquot->dq_dqb_lock);
|
| 310 | entry->bhardlimit = dquot->dq_dqb.dqb_bhardlimit;
|
| 311 | entry->bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit;
|
| 312 | entry->ihardlimit = dquot->dq_dqb.dqb_ihardlimit;
|
| 313 | entry->isoftlimit = dquot->dq_dqb.dqb_isoftlimit;
|
| 314 | spin_unlock(&dquot->dq_dqb_lock);
|
| 315 | }
|
| 316 |
|
| 317 | clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
|
| 318 | up_write(&dqopt->dqio_sem);
|
| 319 |
|
| 320 | out_dqlock:
|
| 321 | mutex_unlock(&dquot->dq_lock);
|
| 322 | return 0;
|
| 323 | }
|
| 324 |
|
| 325 | static int shmem_mark_dquot_dirty(struct dquot *dquot)
|
| 326 | {
|
| 327 | return 0;
|
| 328 | }
|
| 329 |
|
| 330 | static int shmem_dquot_write_info(struct super_block *sb, int type)
|
| 331 | {
|
| 332 | return 0;
|
| 333 | }
|
| 334 |
|
| 335 | static const struct quota_format_ops shmem_format_ops = {
|
| 336 | .check_quota_file = shmem_check_quota_file,
|
| 337 | .read_file_info = shmem_read_file_info,
|
| 338 | .write_file_info = shmem_write_file_info,
|
| 339 | .free_file_info = shmem_free_file_info,
|
| 340 | };
|
| 341 |
|
| 342 | struct quota_format_type shmem_quota_format = {
|
| 343 | .qf_fmt_id = QFMT_SHMEM,
|
| 344 | .qf_ops = &shmem_format_ops,
|
| 345 | .qf_owner = THIS_MODULE
|
| 346 | };
|
| 347 |
|
| 348 | const struct dquot_operations shmem_quota_operations = {
|
| 349 | .acquire_dquot = shmem_acquire_dquot,
|
| 350 | .release_dquot = shmem_release_dquot,
|
| 351 | .alloc_dquot = dquot_alloc,
|
| 352 | .destroy_dquot = dquot_destroy,
|
| 353 | .write_info = shmem_dquot_write_info,
|
| 354 | .mark_dirty = shmem_mark_dquot_dirty,
|
| 355 | .get_next_id = shmem_get_next_id,
|
| 356 | };
|