paul@181 | 1 | /* |
paul@181 | 2 | * mkquota.c --- create quota files for a filesystem |
paul@181 | 3 | * |
paul@181 | 4 | * Aditya Kali <adityakali@google.com> |
paul@181 | 5 | */ |
paul@181 | 6 | #include "config.h" |
paul@181 | 7 | #include <sys/types.h> |
paul@181 | 8 | #include <sys/stat.h> |
paul@181 | 9 | #include <unistd.h> |
paul@181 | 10 | #include <errno.h> |
paul@181 | 11 | #include <string.h> |
paul@181 | 12 | #include <fcntl.h> |
paul@181 | 13 | |
paul@181 | 14 | #include "ext2fs/ext2_fs.h" |
paul@181 | 15 | #include "ext2fs/ext2fs.h" |
paul@181 | 16 | #include "e2p/e2p.h" |
paul@181 | 17 | |
paul@181 | 18 | #include "quotaio.h" |
paul@181 | 19 | #include "quotaio_v2.h" |
paul@181 | 20 | #include "quotaio_tree.h" |
paul@181 | 21 | #include "common.h" |
paul@181 | 22 | #include "dict.h" |
paul@181 | 23 | |
paul@181 | 24 | /* Needed for architectures where sizeof(int) != sizeof(void *) */ |
paul@181 | 25 | #define UINT_TO_VOIDPTR(val) ((void *)(intptr_t)(val)) |
paul@181 | 26 | #define VOIDPTR_TO_UINT(ptr) ((unsigned int)(intptr_t)(ptr)) |
paul@181 | 27 | |
paul@181 | 28 | #if DEBUG_QUOTA |
paul@181 | 29 | static void print_inode(struct ext2_inode *inode) |
paul@181 | 30 | { |
paul@181 | 31 | if (!inode) |
paul@181 | 32 | return; |
paul@181 | 33 | |
paul@181 | 34 | fprintf(stderr, " i_mode = %d\n", inode->i_mode); |
paul@181 | 35 | fprintf(stderr, " i_uid = %d\n", inode->i_uid); |
paul@181 | 36 | fprintf(stderr, " i_size = %d\n", inode->i_size); |
paul@181 | 37 | fprintf(stderr, " i_atime = %d\n", inode->i_atime); |
paul@181 | 38 | fprintf(stderr, " i_ctime = %d\n", inode->i_ctime); |
paul@181 | 39 | fprintf(stderr, " i_mtime = %d\n", inode->i_mtime); |
paul@181 | 40 | fprintf(stderr, " i_dtime = %d\n", inode->i_dtime); |
paul@181 | 41 | fprintf(stderr, " i_gid = %d\n", inode->i_gid); |
paul@181 | 42 | fprintf(stderr, " i_links_count = %d\n", inode->i_links_count); |
paul@181 | 43 | fprintf(stderr, " i_blocks = %d\n", inode->i_blocks); |
paul@181 | 44 | fprintf(stderr, " i_flags = %d\n", inode->i_flags); |
paul@181 | 45 | |
paul@181 | 46 | return; |
paul@181 | 47 | } |
paul@181 | 48 | |
paul@181 | 49 | static void print_dquot(const char *desc, struct dquot *dq) |
paul@181 | 50 | { |
paul@181 | 51 | if (desc) |
paul@181 | 52 | fprintf(stderr, "%s: ", desc); |
paul@181 | 53 | fprintf(stderr, "%u %lld:%lld:%lld %lld:%lld:%lld\n", |
paul@181 | 54 | dq->dq_id, (long long) dq->dq_dqb.dqb_curspace, |
paul@181 | 55 | (long long) dq->dq_dqb.dqb_bsoftlimit, |
paul@181 | 56 | (long long) dq->dq_dqb.dqb_bhardlimit, |
paul@181 | 57 | (long long) dq->dq_dqb.dqb_curinodes, |
paul@181 | 58 | (long long) dq->dq_dqb.dqb_isoftlimit, |
paul@181 | 59 | (long long) dq->dq_dqb.dqb_ihardlimit); |
paul@181 | 60 | } |
paul@181 | 61 | #else |
paul@181 | 62 | static void print_dquot(const char *desc EXT2FS_ATTR((unused)), |
paul@181 | 63 | struct dquot *dq EXT2FS_ATTR((unused))) |
paul@181 | 64 | { |
paul@181 | 65 | } |
paul@181 | 66 | #endif |
paul@181 | 67 | |
paul@181 | 68 | /* |
paul@181 | 69 | * Returns 0 if not able to find the quota file, otherwise returns its |
paul@181 | 70 | * inode number. |
paul@181 | 71 | */ |
paul@181 | 72 | int quota_file_exists(ext2_filsys fs, enum quota_type qtype) |
paul@181 | 73 | { |
paul@181 | 74 | char qf_name[256]; |
paul@181 | 75 | errcode_t ret; |
paul@181 | 76 | ext2_ino_t ino; |
paul@181 | 77 | |
paul@181 | 78 | if (qtype >= MAXQUOTAS) |
paul@181 | 79 | return -EINVAL; |
paul@181 | 80 | |
paul@181 | 81 | quota_get_qf_name(qtype, QFMT_VFS_V1, qf_name); |
paul@181 | 82 | |
paul@181 | 83 | ret = ext2fs_lookup(fs, EXT2_ROOT_INO, qf_name, strlen(qf_name), 0, |
paul@181 | 84 | &ino); |
paul@181 | 85 | if (ret) |
paul@181 | 86 | return 0; |
paul@181 | 87 | |
paul@181 | 88 | return ino; |
paul@181 | 89 | } |
paul@181 | 90 | |
paul@181 | 91 | /* |
paul@181 | 92 | * Set the value for reserved quota inode number field in superblock. |
paul@181 | 93 | */ |
paul@181 | 94 | void quota_set_sb_inum(ext2_filsys fs, ext2_ino_t ino, enum quota_type qtype) |
paul@181 | 95 | { |
paul@181 | 96 | ext2_ino_t *inump; |
paul@181 | 97 | |
paul@181 | 98 | inump = quota_sb_inump(fs->super, qtype); |
paul@181 | 99 | |
paul@181 | 100 | log_debug("setting quota ino in superblock: ino=%u, type=%d", ino, |
paul@181 | 101 | qtype); |
paul@181 | 102 | *inump = ino; |
paul@181 | 103 | ext2fs_mark_super_dirty(fs); |
paul@181 | 104 | } |
paul@181 | 105 | |
paul@181 | 106 | errcode_t quota_remove_inode(ext2_filsys fs, enum quota_type qtype) |
paul@181 | 107 | { |
paul@181 | 108 | ext2_ino_t qf_ino; |
paul@181 | 109 | errcode_t retval; |
paul@181 | 110 | |
paul@181 | 111 | retval = ext2fs_read_bitmaps(fs); |
paul@181 | 112 | if (retval) { |
paul@181 | 113 | log_debug("Couldn't read bitmaps: %s", error_message(retval)); |
paul@181 | 114 | return retval; |
paul@181 | 115 | } |
paul@181 | 116 | |
paul@181 | 117 | qf_ino = *quota_sb_inump(fs->super, qtype); |
paul@181 | 118 | if (qf_ino == 0) |
paul@181 | 119 | return 0; |
paul@181 | 120 | retval = quota_inode_truncate(fs, qf_ino); |
paul@181 | 121 | if (retval) |
paul@181 | 122 | return retval; |
paul@181 | 123 | if (qf_ino >= EXT2_FIRST_INODE(fs->super)) { |
paul@181 | 124 | struct ext2_inode inode; |
paul@181 | 125 | |
paul@181 | 126 | retval = ext2fs_read_inode(fs, qf_ino, &inode); |
paul@181 | 127 | if (!retval) { |
paul@181 | 128 | memset(&inode, 0, sizeof(struct ext2_inode)); |
paul@181 | 129 | ext2fs_write_inode(fs, qf_ino, &inode); |
paul@181 | 130 | } |
paul@181 | 131 | ext2fs_inode_alloc_stats2(fs, qf_ino, -1, 0); |
paul@181 | 132 | ext2fs_mark_ib_dirty(fs); |
paul@181 | 133 | |
paul@181 | 134 | } |
paul@181 | 135 | quota_set_sb_inum(fs, 0, qtype); |
paul@181 | 136 | |
paul@181 | 137 | ext2fs_mark_super_dirty(fs); |
paul@181 | 138 | fs->flags &= ~EXT2_FLAG_SUPER_ONLY; |
paul@181 | 139 | retval = ext2fs_write_bitmaps(fs); |
paul@181 | 140 | if (retval) { |
paul@181 | 141 | log_debug("Couldn't write bitmaps: %s", error_message(retval)); |
paul@181 | 142 | return retval; |
paul@181 | 143 | } |
paul@181 | 144 | return 0; |
paul@181 | 145 | } |
paul@181 | 146 | |
paul@181 | 147 | static void write_dquots(dict_t *dict, struct quota_handle *qh) |
paul@181 | 148 | { |
paul@181 | 149 | dnode_t *n; |
paul@181 | 150 | struct dquot *dq; |
paul@181 | 151 | |
paul@181 | 152 | for (n = dict_first(dict); n; n = dict_next(dict, n)) { |
paul@181 | 153 | dq = dnode_get(n); |
paul@181 | 154 | if (dq) { |
paul@181 | 155 | print_dquot("write", dq); |
paul@181 | 156 | dq->dq_h = qh; |
paul@181 | 157 | update_grace_times(dq); |
paul@181 | 158 | qh->qh_ops->commit_dquot(dq); |
paul@181 | 159 | } |
paul@181 | 160 | } |
paul@181 | 161 | } |
paul@181 | 162 | |
paul@181 | 163 | errcode_t quota_write_inode(quota_ctx_t qctx, unsigned int qtype_bits) |
paul@181 | 164 | { |
paul@181 | 165 | int retval = 0; |
paul@181 | 166 | enum quota_type qtype; |
paul@181 | 167 | dict_t *dict; |
paul@181 | 168 | ext2_filsys fs; |
paul@181 | 169 | struct quota_handle *h = NULL; |
paul@181 | 170 | int fmt = QFMT_VFS_V1; |
paul@181 | 171 | |
paul@181 | 172 | if (!qctx) |
paul@181 | 173 | return 0; |
paul@181 | 174 | |
paul@181 | 175 | fs = qctx->fs; |
paul@181 | 176 | retval = ext2fs_get_mem(sizeof(struct quota_handle), &h); |
paul@181 | 177 | if (retval) { |
paul@181 | 178 | log_debug("Unable to allocate quota handle: %s", |
paul@181 | 179 | error_message(retval)); |
paul@181 | 180 | goto out; |
paul@181 | 181 | } |
paul@181 | 182 | |
paul@181 | 183 | retval = ext2fs_read_bitmaps(fs); |
paul@181 | 184 | if (retval) { |
paul@181 | 185 | log_debug("Couldn't read bitmaps: %s", error_message(retval)); |
paul@181 | 186 | goto out; |
paul@181 | 187 | } |
paul@181 | 188 | |
paul@181 | 189 | for (qtype = 0; qtype < MAXQUOTAS; qtype++) { |
paul@181 | 190 | if (((1 << qtype) & qtype_bits) == 0) |
paul@181 | 191 | continue; |
paul@181 | 192 | |
paul@181 | 193 | dict = qctx->quota_dict[qtype]; |
paul@181 | 194 | if (!dict) |
paul@181 | 195 | continue; |
paul@181 | 196 | |
paul@181 | 197 | retval = quota_file_create(h, fs, qtype, fmt); |
paul@181 | 198 | if (retval) { |
paul@181 | 199 | log_debug("Cannot initialize io on quotafile: %s", |
paul@181 | 200 | error_message(retval)); |
paul@181 | 201 | goto out; |
paul@181 | 202 | } |
paul@181 | 203 | |
paul@181 | 204 | write_dquots(dict, h); |
paul@181 | 205 | retval = quota_file_close(qctx, h); |
paul@181 | 206 | if (retval) { |
paul@181 | 207 | log_debug("Cannot finish IO on new quotafile: %s", |
paul@181 | 208 | strerror(errno)); |
paul@181 | 209 | if (h->qh_qf.e2_file) |
paul@181 | 210 | ext2fs_file_close(h->qh_qf.e2_file); |
paul@181 | 211 | (void) quota_inode_truncate(fs, h->qh_qf.ino); |
paul@181 | 212 | goto out; |
paul@181 | 213 | } |
paul@181 | 214 | |
paul@181 | 215 | /* Set quota inode numbers in superblock. */ |
paul@181 | 216 | quota_set_sb_inum(fs, h->qh_qf.ino, qtype); |
paul@181 | 217 | ext2fs_mark_super_dirty(fs); |
paul@181 | 218 | ext2fs_mark_bb_dirty(fs); |
paul@181 | 219 | fs->flags &= ~EXT2_FLAG_SUPER_ONLY; |
paul@181 | 220 | } |
paul@181 | 221 | |
paul@181 | 222 | retval = ext2fs_write_bitmaps(fs); |
paul@181 | 223 | if (retval) { |
paul@181 | 224 | log_debug("Couldn't write bitmaps: %s", error_message(retval)); |
paul@181 | 225 | goto out; |
paul@181 | 226 | } |
paul@181 | 227 | out: |
paul@181 | 228 | if (h) |
paul@181 | 229 | ext2fs_free_mem(&h); |
paul@181 | 230 | return retval; |
paul@181 | 231 | } |
paul@181 | 232 | |
paul@181 | 233 | /******************************************************************/ |
paul@181 | 234 | /* Helper functions for computing quota in memory. */ |
paul@181 | 235 | /******************************************************************/ |
paul@181 | 236 | |
paul@212 | 237 | static int dict_uint_cmp(const void *cmp_ctx, const void *a, const void *b) |
paul@181 | 238 | { |
paul@181 | 239 | unsigned int c, d; |
paul@181 | 240 | |
paul@181 | 241 | c = VOIDPTR_TO_UINT(a); |
paul@181 | 242 | d = VOIDPTR_TO_UINT(b); |
paul@181 | 243 | |
paul@181 | 244 | if (c == d) |
paul@181 | 245 | return 0; |
paul@181 | 246 | else if (c > d) |
paul@181 | 247 | return 1; |
paul@181 | 248 | else |
paul@181 | 249 | return -1; |
paul@181 | 250 | } |
paul@181 | 251 | |
paul@181 | 252 | static inline int project_quota_valid(quota_ctx_t qctx) |
paul@181 | 253 | { |
paul@181 | 254 | return (EXT2_INODE_SIZE(qctx->fs->super) > EXT2_GOOD_OLD_INODE_SIZE); |
paul@181 | 255 | } |
paul@181 | 256 | |
paul@181 | 257 | static inline qid_t get_qid(struct ext2_inode_large *inode, enum quota_type qtype) |
paul@181 | 258 | { |
paul@181 | 259 | unsigned int inode_size; |
paul@181 | 260 | |
paul@181 | 261 | switch (qtype) { |
paul@181 | 262 | case USRQUOTA: |
paul@181 | 263 | return inode_uid(*inode); |
paul@181 | 264 | case GRPQUOTA: |
paul@181 | 265 | return inode_gid(*inode); |
paul@181 | 266 | case PRJQUOTA: |
paul@181 | 267 | inode_size = EXT2_GOOD_OLD_INODE_SIZE + |
paul@181 | 268 | inode->i_extra_isize; |
paul@181 | 269 | if (inode_includes(inode_size, i_projid)) |
paul@181 | 270 | return inode_projid(*inode); |
paul@181 | 271 | return 0; |
paul@181 | 272 | default: |
paul@181 | 273 | return 0; |
paul@181 | 274 | } |
paul@181 | 275 | |
paul@181 | 276 | return 0; |
paul@181 | 277 | } |
paul@181 | 278 | |
paul@181 | 279 | static void quota_dnode_free(dnode_t *node, |
paul@181 | 280 | void *context EXT2FS_ATTR((unused))) |
paul@181 | 281 | { |
paul@181 | 282 | void *ptr = node ? dnode_get(node) : 0; |
paul@181 | 283 | |
paul@181 | 284 | ext2fs_free_mem(&ptr); |
paul@181 | 285 | free(node); |
paul@181 | 286 | } |
paul@181 | 287 | |
paul@181 | 288 | /* |
paul@181 | 289 | * Set up the quota tracking data structures. |
paul@181 | 290 | */ |
paul@181 | 291 | errcode_t quota_init_context(quota_ctx_t *qctx, ext2_filsys fs, |
paul@181 | 292 | unsigned int qtype_bits) |
paul@181 | 293 | { |
paul@181 | 294 | errcode_t err; |
paul@181 | 295 | dict_t *dict; |
paul@181 | 296 | quota_ctx_t ctx; |
paul@181 | 297 | enum quota_type qtype; |
paul@181 | 298 | |
paul@181 | 299 | err = ext2fs_get_mem(sizeof(struct quota_ctx), &ctx); |
paul@181 | 300 | if (err) { |
paul@181 | 301 | log_debug("Failed to allocate quota context"); |
paul@181 | 302 | return err; |
paul@181 | 303 | } |
paul@181 | 304 | |
paul@181 | 305 | memset(ctx, 0, sizeof(struct quota_ctx)); |
paul@181 | 306 | for (qtype = 0; qtype < MAXQUOTAS; qtype++) { |
paul@181 | 307 | ctx->quota_file[qtype] = NULL; |
paul@181 | 308 | if (qtype_bits) { |
paul@181 | 309 | if (((1 << qtype) & qtype_bits) == 0) |
paul@181 | 310 | continue; |
paul@181 | 311 | } else { |
paul@181 | 312 | if (*quota_sb_inump(fs->super, qtype) == 0) |
paul@181 | 313 | continue; |
paul@181 | 314 | } |
paul@181 | 315 | err = ext2fs_get_mem(sizeof(dict_t), &dict); |
paul@181 | 316 | if (err) { |
paul@181 | 317 | log_debug("Failed to allocate dictionary"); |
paul@181 | 318 | quota_release_context(&ctx); |
paul@181 | 319 | return err; |
paul@181 | 320 | } |
paul@181 | 321 | ctx->quota_dict[qtype] = dict; |
paul@181 | 322 | dict_init(dict, DICTCOUNT_T_MAX, dict_uint_cmp); |
paul@181 | 323 | dict_set_allocator(dict, NULL, quota_dnode_free, NULL); |
paul@181 | 324 | } |
paul@181 | 325 | |
paul@181 | 326 | ctx->fs = fs; |
paul@181 | 327 | *qctx = ctx; |
paul@181 | 328 | return 0; |
paul@181 | 329 | } |
paul@181 | 330 | |
paul@181 | 331 | void quota_release_context(quota_ctx_t *qctx) |
paul@181 | 332 | { |
paul@181 | 333 | errcode_t err; |
paul@181 | 334 | dict_t *dict; |
paul@181 | 335 | enum quota_type qtype; |
paul@181 | 336 | quota_ctx_t ctx; |
paul@181 | 337 | |
paul@181 | 338 | if (!qctx) |
paul@181 | 339 | return; |
paul@181 | 340 | |
paul@181 | 341 | ctx = *qctx; |
paul@181 | 342 | for (qtype = 0; qtype < MAXQUOTAS; qtype++) { |
paul@181 | 343 | dict = ctx->quota_dict[qtype]; |
paul@181 | 344 | ctx->quota_dict[qtype] = 0; |
paul@181 | 345 | if (dict) { |
paul@181 | 346 | dict_free_nodes(dict); |
paul@181 | 347 | free(dict); |
paul@181 | 348 | } |
paul@181 | 349 | if (ctx->quota_file[qtype]) { |
paul@181 | 350 | err = quota_file_close(ctx, ctx->quota_file[qtype]); |
paul@181 | 351 | if (err) { |
paul@181 | 352 | log_err("Cannot close quotafile: %s", |
paul@181 | 353 | strerror(errno)); |
paul@181 | 354 | ext2fs_free_mem(&ctx->quota_file[qtype]); |
paul@181 | 355 | } |
paul@181 | 356 | } |
paul@181 | 357 | } |
paul@181 | 358 | *qctx = NULL; |
paul@181 | 359 | free(ctx); |
paul@181 | 360 | } |
paul@181 | 361 | |
paul@181 | 362 | static struct dquot *get_dq(dict_t *dict, __u32 key) |
paul@181 | 363 | { |
paul@181 | 364 | struct dquot *dq; |
paul@181 | 365 | dnode_t *n; |
paul@181 | 366 | |
paul@181 | 367 | n = dict_lookup(dict, UINT_TO_VOIDPTR(key)); |
paul@181 | 368 | if (n) |
paul@181 | 369 | dq = dnode_get(n); |
paul@181 | 370 | else { |
paul@181 | 371 | if (ext2fs_get_mem(sizeof(struct dquot), &dq)) { |
paul@181 | 372 | log_err("Unable to allocate dquot"); |
paul@181 | 373 | return NULL; |
paul@181 | 374 | } |
paul@181 | 375 | memset(dq, 0, sizeof(struct dquot)); |
paul@181 | 376 | dict_alloc_insert(dict, UINT_TO_VOIDPTR(key), dq); |
paul@181 | 377 | dq->dq_id = key; |
paul@181 | 378 | } |
paul@181 | 379 | return dq; |
paul@181 | 380 | } |
paul@181 | 381 | |
paul@181 | 382 | |
paul@181 | 383 | /* |
paul@181 | 384 | * Called to update the blocks used by a particular inode |
paul@181 | 385 | */ |
paul@181 | 386 | void quota_data_add(quota_ctx_t qctx, struct ext2_inode_large *inode, |
paul@181 | 387 | ext2_ino_t ino EXT2FS_ATTR((unused)), |
paul@181 | 388 | qsize_t space) |
paul@181 | 389 | { |
paul@181 | 390 | struct dquot *dq; |
paul@181 | 391 | dict_t *dict; |
paul@181 | 392 | enum quota_type qtype; |
paul@181 | 393 | |
paul@181 | 394 | if (!qctx) |
paul@181 | 395 | return; |
paul@181 | 396 | |
paul@181 | 397 | log_debug("ADD_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino, |
paul@181 | 398 | inode_uid(*inode), |
paul@181 | 399 | inode_gid(*inode), space); |
paul@181 | 400 | for (qtype = 0; qtype < MAXQUOTAS; qtype++) { |
paul@181 | 401 | if (qtype == PRJQUOTA && !project_quota_valid(qctx)) |
paul@181 | 402 | continue; |
paul@181 | 403 | dict = qctx->quota_dict[qtype]; |
paul@181 | 404 | if (dict) { |
paul@181 | 405 | dq = get_dq(dict, get_qid(inode, qtype)); |
paul@181 | 406 | if (dq) |
paul@181 | 407 | dq->dq_dqb.dqb_curspace += space; |
paul@181 | 408 | } |
paul@181 | 409 | } |
paul@181 | 410 | } |
paul@181 | 411 | |
paul@181 | 412 | /* |
paul@181 | 413 | * Called to remove some blocks used by a particular inode |
paul@181 | 414 | */ |
paul@181 | 415 | void quota_data_sub(quota_ctx_t qctx, struct ext2_inode_large *inode, |
paul@181 | 416 | ext2_ino_t ino EXT2FS_ATTR((unused)), |
paul@181 | 417 | qsize_t space) |
paul@181 | 418 | { |
paul@181 | 419 | struct dquot *dq; |
paul@181 | 420 | dict_t *dict; |
paul@181 | 421 | enum quota_type qtype; |
paul@181 | 422 | |
paul@181 | 423 | if (!qctx) |
paul@181 | 424 | return; |
paul@181 | 425 | |
paul@181 | 426 | log_debug("SUB_DATA: Inode: %u, UID/GID: %u/%u, space: %ld", ino, |
paul@181 | 427 | inode_uid(*inode), |
paul@181 | 428 | inode_gid(*inode), space); |
paul@181 | 429 | for (qtype = 0; qtype < MAXQUOTAS; qtype++) { |
paul@181 | 430 | if (qtype == PRJQUOTA && !project_quota_valid(qctx)) |
paul@181 | 431 | continue; |
paul@181 | 432 | dict = qctx->quota_dict[qtype]; |
paul@181 | 433 | if (dict) { |
paul@181 | 434 | dq = get_dq(dict, get_qid(inode, qtype)); |
paul@181 | 435 | dq->dq_dqb.dqb_curspace -= space; |
paul@181 | 436 | } |
paul@181 | 437 | } |
paul@181 | 438 | } |
paul@181 | 439 | |
paul@181 | 440 | /* |
paul@181 | 441 | * Called to count the files used by an inode's user/group |
paul@181 | 442 | */ |
paul@181 | 443 | void quota_data_inodes(quota_ctx_t qctx, struct ext2_inode_large *inode, |
paul@181 | 444 | ext2_ino_t ino EXT2FS_ATTR((unused)), int adjust) |
paul@181 | 445 | { |
paul@181 | 446 | struct dquot *dq; |
paul@181 | 447 | dict_t *dict; |
paul@181 | 448 | enum quota_type qtype; |
paul@181 | 449 | |
paul@181 | 450 | if (!qctx) |
paul@181 | 451 | return; |
paul@181 | 452 | |
paul@181 | 453 | log_debug("ADJ_INODE: Inode: %u, UID/GID: %u/%u, adjust: %d", ino, |
paul@181 | 454 | inode_uid(*inode), |
paul@181 | 455 | inode_gid(*inode), adjust); |
paul@181 | 456 | for (qtype = 0; qtype < MAXQUOTAS; qtype++) { |
paul@181 | 457 | if (qtype == PRJQUOTA && !project_quota_valid(qctx)) |
paul@181 | 458 | continue; |
paul@181 | 459 | dict = qctx->quota_dict[qtype]; |
paul@181 | 460 | if (dict) { |
paul@181 | 461 | dq = get_dq(dict, get_qid(inode, qtype)); |
paul@181 | 462 | dq->dq_dqb.dqb_curinodes += adjust; |
paul@181 | 463 | } |
paul@181 | 464 | } |
paul@181 | 465 | } |
paul@181 | 466 | |
paul@181 | 467 | errcode_t quota_compute_usage(quota_ctx_t qctx) |
paul@181 | 468 | { |
paul@181 | 469 | ext2_filsys fs; |
paul@181 | 470 | ext2_ino_t ino; |
paul@181 | 471 | errcode_t ret; |
paul@181 | 472 | struct ext2_inode_large *inode; |
paul@181 | 473 | int inode_size; |
paul@181 | 474 | qsize_t space; |
paul@181 | 475 | ext2_inode_scan scan; |
paul@181 | 476 | |
paul@181 | 477 | if (!qctx) |
paul@181 | 478 | return 0; |
paul@181 | 479 | |
paul@181 | 480 | fs = qctx->fs; |
paul@181 | 481 | ret = ext2fs_open_inode_scan(fs, 0, &scan); |
paul@181 | 482 | if (ret) { |
paul@181 | 483 | log_err("while opening inode scan. ret=%ld", ret); |
paul@181 | 484 | return ret; |
paul@181 | 485 | } |
paul@181 | 486 | inode_size = fs->super->s_inode_size; |
paul@181 | 487 | inode = malloc(inode_size); |
paul@181 | 488 | if (!inode) { |
paul@181 | 489 | ext2fs_close_inode_scan(scan); |
paul@181 | 490 | return ENOMEM; |
paul@181 | 491 | } |
paul@181 | 492 | while (1) { |
paul@181 | 493 | ret = ext2fs_get_next_inode_full(scan, &ino, |
paul@181 | 494 | EXT2_INODE(inode), inode_size); |
paul@181 | 495 | if (ret) { |
paul@181 | 496 | log_err("while getting next inode. ret=%ld", ret); |
paul@181 | 497 | ext2fs_close_inode_scan(scan); |
paul@181 | 498 | free(inode); |
paul@181 | 499 | return ret; |
paul@181 | 500 | } |
paul@181 | 501 | if (ino == 0) |
paul@181 | 502 | break; |
paul@181 | 503 | if (inode->i_links_count && |
paul@181 | 504 | (ino == EXT2_ROOT_INO || |
paul@181 | 505 | ino >= EXT2_FIRST_INODE(fs->super))) { |
paul@212 | 506 | space = ext2fs_get_stat_i_blocks(fs, |
paul@212 | 507 | EXT2_INODE(inode)) << 9; |
paul@181 | 508 | quota_data_add(qctx, inode, ino, space); |
paul@181 | 509 | quota_data_inodes(qctx, inode, ino, +1); |
paul@181 | 510 | } |
paul@181 | 511 | } |
paul@181 | 512 | |
paul@181 | 513 | ext2fs_close_inode_scan(scan); |
paul@181 | 514 | free(inode); |
paul@181 | 515 | return 0; |
paul@181 | 516 | } |
paul@181 | 517 | |
paul@181 | 518 | struct scan_dquots_data { |
paul@181 | 519 | dict_t *quota_dict; |
paul@181 | 520 | int update_limits; /* update limits from disk */ |
paul@181 | 521 | int update_usage; |
paul@181 | 522 | int check_consistency; |
paul@181 | 523 | int usage_is_inconsistent; |
paul@181 | 524 | }; |
paul@181 | 525 | |
paul@181 | 526 | static int scan_dquots_callback(struct dquot *dquot, void *cb_data) |
paul@181 | 527 | { |
paul@181 | 528 | struct scan_dquots_data *scan_data = cb_data; |
paul@181 | 529 | dict_t *quota_dict = scan_data->quota_dict; |
paul@181 | 530 | struct dquot *dq; |
paul@181 | 531 | |
paul@181 | 532 | dq = get_dq(quota_dict, dquot->dq_id); |
paul@181 | 533 | dq->dq_id = dquot->dq_id; |
paul@181 | 534 | dq->dq_flags |= DQF_SEEN; |
paul@181 | 535 | |
paul@181 | 536 | print_dquot("mem", dq); |
paul@181 | 537 | print_dquot("dsk", dquot); |
paul@181 | 538 | |
paul@181 | 539 | /* Check if there is inconsistency */ |
paul@181 | 540 | if (scan_data->check_consistency && |
paul@181 | 541 | (dq->dq_dqb.dqb_curspace != dquot->dq_dqb.dqb_curspace || |
paul@181 | 542 | dq->dq_dqb.dqb_curinodes != dquot->dq_dqb.dqb_curinodes)) { |
paul@181 | 543 | scan_data->usage_is_inconsistent = 1; |
paul@181 | 544 | fprintf(stderr, "[QUOTA WARNING] Usage inconsistent for ID %u:" |
paul@181 | 545 | "actual (%lld, %lld) != expected (%lld, %lld)\n", |
paul@181 | 546 | dq->dq_id, (long long) dq->dq_dqb.dqb_curspace, |
paul@181 | 547 | (long long) dq->dq_dqb.dqb_curinodes, |
paul@181 | 548 | (long long) dquot->dq_dqb.dqb_curspace, |
paul@181 | 549 | (long long) dquot->dq_dqb.dqb_curinodes); |
paul@181 | 550 | } |
paul@181 | 551 | |
paul@181 | 552 | if (scan_data->update_limits) { |
paul@181 | 553 | dq->dq_dqb.dqb_ihardlimit = dquot->dq_dqb.dqb_ihardlimit; |
paul@181 | 554 | dq->dq_dqb.dqb_isoftlimit = dquot->dq_dqb.dqb_isoftlimit; |
paul@181 | 555 | dq->dq_dqb.dqb_bhardlimit = dquot->dq_dqb.dqb_bhardlimit; |
paul@181 | 556 | dq->dq_dqb.dqb_bsoftlimit = dquot->dq_dqb.dqb_bsoftlimit; |
paul@181 | 557 | } |
paul@181 | 558 | |
paul@181 | 559 | if (scan_data->update_usage) { |
paul@181 | 560 | dq->dq_dqb.dqb_curspace = dquot->dq_dqb.dqb_curspace; |
paul@181 | 561 | dq->dq_dqb.dqb_curinodes = dquot->dq_dqb.dqb_curinodes; |
paul@181 | 562 | } |
paul@181 | 563 | |
paul@181 | 564 | return 0; |
paul@181 | 565 | } |
paul@181 | 566 | |
paul@181 | 567 | /* |
paul@181 | 568 | * Read all dquots from quota file into memory |
paul@181 | 569 | */ |
paul@181 | 570 | static errcode_t quota_read_all_dquots(struct quota_handle *qh, |
paul@181 | 571 | quota_ctx_t qctx, |
paul@181 | 572 | int update_limits EXT2FS_ATTR((unused))) |
paul@181 | 573 | { |
paul@181 | 574 | struct scan_dquots_data scan_data; |
paul@181 | 575 | |
paul@181 | 576 | scan_data.quota_dict = qctx->quota_dict[qh->qh_type]; |
paul@181 | 577 | scan_data.check_consistency = 0; |
paul@181 | 578 | scan_data.update_limits = 0; |
paul@181 | 579 | scan_data.update_usage = 1; |
paul@181 | 580 | |
paul@181 | 581 | return qh->qh_ops->scan_dquots(qh, scan_dquots_callback, &scan_data); |
paul@181 | 582 | } |
paul@181 | 583 | |
paul@181 | 584 | /* |
paul@181 | 585 | * Write all memory dquots into quota file |
paul@181 | 586 | */ |
paul@181 | 587 | #if 0 /* currently unused, but may be useful in the future? */ |
paul@181 | 588 | static errcode_t quota_write_all_dquots(struct quota_handle *qh, |
paul@181 | 589 | quota_ctx_t qctx) |
paul@181 | 590 | { |
paul@181 | 591 | errcode_t err; |
paul@181 | 592 | |
paul@181 | 593 | err = ext2fs_read_bitmaps(qctx->fs); |
paul@181 | 594 | if (err) |
paul@181 | 595 | return err; |
paul@181 | 596 | write_dquots(qctx->quota_dict[qh->qh_type], qh); |
paul@181 | 597 | ext2fs_mark_bb_dirty(qctx->fs); |
paul@181 | 598 | qctx->fs->flags &= ~EXT2_FLAG_SUPER_ONLY; |
paul@181 | 599 | ext2fs_write_bitmaps(qctx->fs); |
paul@181 | 600 | return 0; |
paul@181 | 601 | } |
paul@181 | 602 | #endif |
paul@181 | 603 | |
paul@181 | 604 | /* |
paul@181 | 605 | * Updates the in-memory quota limits from the given quota inode. |
paul@181 | 606 | */ |
paul@181 | 607 | errcode_t quota_update_limits(quota_ctx_t qctx, ext2_ino_t qf_ino, |
paul@181 | 608 | enum quota_type qtype) |
paul@181 | 609 | { |
paul@181 | 610 | struct quota_handle *qh; |
paul@181 | 611 | errcode_t err; |
paul@181 | 612 | |
paul@181 | 613 | if (!qctx) |
paul@181 | 614 | return 0; |
paul@181 | 615 | |
paul@181 | 616 | err = ext2fs_get_mem(sizeof(struct quota_handle), &qh); |
paul@181 | 617 | if (err) { |
paul@181 | 618 | log_debug("Unable to allocate quota handle"); |
paul@181 | 619 | return err; |
paul@181 | 620 | } |
paul@181 | 621 | |
paul@181 | 622 | err = quota_file_open(qctx, qh, qf_ino, qtype, -1, 0); |
paul@181 | 623 | if (err) { |
paul@181 | 624 | log_debug("Open quota file failed"); |
paul@181 | 625 | goto out; |
paul@181 | 626 | } |
paul@181 | 627 | |
paul@181 | 628 | quota_read_all_dquots(qh, qctx, 1); |
paul@181 | 629 | |
paul@181 | 630 | err = quota_file_close(qctx, qh); |
paul@181 | 631 | if (err) { |
paul@181 | 632 | log_debug("Cannot finish IO on new quotafile: %s", |
paul@181 | 633 | strerror(errno)); |
paul@181 | 634 | if (qh->qh_qf.e2_file) |
paul@181 | 635 | ext2fs_file_close(qh->qh_qf.e2_file); |
paul@181 | 636 | } |
paul@181 | 637 | out: |
paul@181 | 638 | ext2fs_free_mem(&qh); |
paul@181 | 639 | return err; |
paul@181 | 640 | } |
paul@181 | 641 | |
paul@181 | 642 | /* |
paul@181 | 643 | * Compares the measured quota in qctx->quota_dict with that in the quota inode |
paul@181 | 644 | * on disk and updates the limits in qctx->quota_dict. 'usage_inconsistent' is |
paul@181 | 645 | * set to 1 if the supplied and on-disk quota usage values are not identical. |
paul@181 | 646 | */ |
paul@181 | 647 | errcode_t quota_compare_and_update(quota_ctx_t qctx, enum quota_type qtype, |
paul@181 | 648 | int *usage_inconsistent) |
paul@181 | 649 | { |
paul@181 | 650 | struct quota_handle qh; |
paul@181 | 651 | struct scan_dquots_data scan_data; |
paul@181 | 652 | struct dquot *dq; |
paul@181 | 653 | dnode_t *n; |
paul@181 | 654 | dict_t *dict = qctx->quota_dict[qtype]; |
paul@181 | 655 | errcode_t err = 0; |
paul@181 | 656 | |
paul@181 | 657 | if (!dict) |
paul@181 | 658 | goto out; |
paul@181 | 659 | |
paul@181 | 660 | err = quota_file_open(qctx, &qh, 0, qtype, -1, 0); |
paul@181 | 661 | if (err) { |
paul@181 | 662 | log_debug("Open quota file failed"); |
paul@181 | 663 | goto out; |
paul@181 | 664 | } |
paul@181 | 665 | |
paul@181 | 666 | scan_data.quota_dict = qctx->quota_dict[qtype]; |
paul@181 | 667 | scan_data.update_limits = 1; |
paul@181 | 668 | scan_data.update_usage = 0; |
paul@181 | 669 | scan_data.check_consistency = 1; |
paul@181 | 670 | scan_data.usage_is_inconsistent = 0; |
paul@181 | 671 | err = qh.qh_ops->scan_dquots(&qh, scan_dquots_callback, &scan_data); |
paul@181 | 672 | if (err) { |
paul@181 | 673 | log_debug("Error scanning dquots"); |
paul@212 | 674 | *usage_inconsistent = 1; |
paul@181 | 675 | goto out_close_qh; |
paul@181 | 676 | } |
paul@181 | 677 | |
paul@181 | 678 | for (n = dict_first(dict); n; n = dict_next(dict, n)) { |
paul@181 | 679 | dq = dnode_get(n); |
paul@181 | 680 | if (!dq) |
paul@181 | 681 | continue; |
paul@181 | 682 | if ((dq->dq_flags & DQF_SEEN) == 0) { |
paul@181 | 683 | fprintf(stderr, "[QUOTA WARNING] " |
paul@181 | 684 | "Missing quota entry ID %d\n", dq->dq_id); |
paul@181 | 685 | scan_data.usage_is_inconsistent = 1; |
paul@181 | 686 | } |
paul@181 | 687 | } |
paul@181 | 688 | *usage_inconsistent = scan_data.usage_is_inconsistent; |
paul@181 | 689 | |
paul@181 | 690 | out_close_qh: |
paul@181 | 691 | err = quota_file_close(qctx, &qh); |
paul@181 | 692 | if (err) { |
paul@181 | 693 | log_debug("Cannot close quotafile: %s", error_message(errno)); |
paul@181 | 694 | if (qh.qh_qf.e2_file) |
paul@181 | 695 | ext2fs_file_close(qh.qh_qf.e2_file); |
paul@181 | 696 | } |
paul@181 | 697 | out: |
paul@181 | 698 | return err; |
paul@181 | 699 | } |
paul@181 | 700 | |
paul@181 | 701 | int parse_quota_opts(const char *opts, int (*func)(char *)) |
paul@181 | 702 | { |
paul@181 | 703 | char *buf, *token, *next, *p; |
paul@181 | 704 | int len; |
paul@181 | 705 | int ret = 0; |
paul@181 | 706 | |
paul@181 | 707 | len = strlen(opts); |
paul@181 | 708 | buf = malloc(len + 1); |
paul@181 | 709 | if (!buf) { |
paul@181 | 710 | fprintf(stderr, |
paul@181 | 711 | "Couldn't allocate memory to parse quota options!\n"); |
paul@181 | 712 | return -ENOMEM; |
paul@181 | 713 | } |
paul@181 | 714 | strcpy(buf, opts); |
paul@181 | 715 | for (token = buf; token && *token; token = next) { |
paul@181 | 716 | p = strchr(token, ','); |
paul@181 | 717 | next = 0; |
paul@181 | 718 | if (p) { |
paul@181 | 719 | *p = 0; |
paul@181 | 720 | next = p + 1; |
paul@181 | 721 | } |
paul@181 | 722 | ret = func(token); |
paul@181 | 723 | if (ret) |
paul@181 | 724 | break; |
paul@181 | 725 | } |
paul@181 | 726 | free(buf); |
paul@181 | 727 | return ret; |
paul@181 | 728 | } |