1 /** quotaio.c 2 * 3 * Generic IO operations on quotafiles 4 * Jan Kara <jack (at) suse.cz> - sponsored by SuSE CR 5 * Aditya Kali <adityakali (at) google.com> - Ported to e2fsprogs 6 */ 7 8 #include "config.h" 9 #include <stdio.h> 10 #include <errno.h> 11 #include <string.h> 12 #include <unistd.h> 13 #include <stdlib.h> 14 #include <time.h> 15 #include <sys/types.h> 16 #include <sys/stat.h> 17 #include <sys/file.h> 18 #include <assert.h> 19 20 #include "common.h" 21 #include "quotaio.h" 22 23 static const char * const extensions[MAXQUOTAS] = { 24 [USRQUOTA] = "user", 25 [GRPQUOTA] = "group", 26 [PRJQUOTA] = "project", 27 }; 28 static const char * const basenames[] = { 29 "", /* undefined */ 30 "quota", /* QFMT_VFS_OLD */ 31 "aquota", /* QFMT_VFS_V0 */ 32 "", /* QFMT_OCFS2 */ 33 "aquota" /* QFMT_VFS_V1 */ 34 }; 35 36 /* Header in all newer quotafiles */ 37 struct disk_dqheader { 38 __le32 dqh_magic; 39 __le32 dqh_version; 40 } __attribute__ ((packed)); 41 42 /** 43 * Convert type of quota to written representation 44 */ 45 const char *quota_type2name(enum quota_type qtype) 46 { 47 if (qtype >= MAXQUOTAS) 48 return "unknown"; 49 return extensions[qtype]; 50 } 51 52 ext2_ino_t quota_type2inum(enum quota_type qtype, 53 struct ext2_super_block *sb) 54 { 55 switch (qtype) { 56 case USRQUOTA: 57 return EXT4_USR_QUOTA_INO; 58 case GRPQUOTA: 59 return EXT4_GRP_QUOTA_INO; 60 case PRJQUOTA: 61 return sb->s_prj_quota_inum; 62 default: 63 return 0; 64 } 65 return 0; 66 } 67 68 /** 69 * Creates a quota file name for given type and format. 70 */ 71 const char *quota_get_qf_name(enum quota_type type, int fmt, char *buf) 72 { 73 if (!buf) 74 return NULL; 75 snprintf(buf, QUOTA_NAME_LEN, "%s.%s", 76 basenames[fmt], extensions[type]); 77 78 return buf; 79 } 80 81 /* 82 * Set grace time if needed 83 */ 84 void update_grace_times(struct dquot *q) 85 { 86 time_t now; 87 88 time(&now); 89 if (q->dq_dqb.dqb_bsoftlimit && toqb(q->dq_dqb.dqb_curspace) > 90 q->dq_dqb.dqb_bsoftlimit) { 91 if (!q->dq_dqb.dqb_btime) 92 q->dq_dqb.dqb_btime = 93 now + q->dq_h->qh_info.dqi_bgrace; 94 } else { 95 q->dq_dqb.dqb_btime = 0; 96 } 97 98 if (q->dq_dqb.dqb_isoftlimit && q->dq_dqb.dqb_curinodes > 99 q->dq_dqb.dqb_isoftlimit) { 100 if (!q->dq_dqb.dqb_itime) 101 q->dq_dqb.dqb_itime = 102 now + q->dq_h->qh_info.dqi_igrace; 103 } else { 104 q->dq_dqb.dqb_itime = 0; 105 } 106 } 107 108 static int compute_num_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), 109 blk64_t *blocknr EXT2FS_ATTR((unused)), 110 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), 111 blk64_t ref_block EXT2FS_ATTR((unused)), 112 int ref_offset EXT2FS_ATTR((unused)), 113 void *private) 114 { 115 blk64_t *num_blocks = private; 116 117 *num_blocks += 1; 118 return 0; 119 } 120 121 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino) 122 { 123 struct ext2_inode inode; 124 errcode_t err; 125 enum quota_type qtype; 126 127 if ((err = ext2fs_read_inode(fs, ino, &inode))) 128 return err; 129 130 for (qtype = 0; qtype < MAXQUOTAS; qtype++) 131 if (ino == quota_type2inum(qtype, fs->super)) 132 break; 133 134 if (qtype != MAXQUOTAS) { 135 inode.i_dtime = fs->now ? fs->now : time(0); 136 if (!ext2fs_inode_has_valid_blocks2(fs, &inode)) 137 return 0; 138 err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL); 139 if (err) 140 return err; 141 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 142 memset(&inode, 0, sizeof(struct ext2_inode)); 143 } else { 144 inode.i_flags &= ~EXT2_IMMUTABLE_FL; 145 } 146 err = ext2fs_write_inode(fs, ino, &inode); 147 return err; 148 } 149 150 static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino) 151 { 152 blk64_t num_blocks = 0; 153 154 ext2fs_block_iterate3(fs, ino, 155 BLOCK_FLAG_READ_ONLY, 156 NULL, 157 compute_num_blocks_proc, 158 &num_blocks); 159 return num_blocks * fs->blocksize; 160 } 161 162 /* Functions to read/write quota file. */ 163 static unsigned int quota_write_nomount(struct quota_file *qf, 164 ext2_loff_t offset, 165 void *buf, unsigned int size) 166 { 167 ext2_file_t e2_file = qf->e2_file; 168 unsigned int bytes_written = 0; 169 errcode_t err; 170 171 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL); 172 if (err) { 173 log_err("ext2fs_file_llseek failed: %ld", err); 174 return 0; 175 } 176 177 err = ext2fs_file_write(e2_file, buf, size, &bytes_written); 178 if (err) { 179 log_err("ext2fs_file_write failed: %ld", err); 180 return 0; 181 } 182 183 /* Correct inode.i_size is set in end_io. */ 184 return bytes_written; 185 } 186 187 static unsigned int quota_read_nomount(struct quota_file *qf, 188 ext2_loff_t offset, 189 void *buf, unsigned int size) 190 { 191 ext2_file_t e2_file = qf->e2_file; 192 unsigned int bytes_read = 0; 193 errcode_t err; 194 195 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL); 196 if (err) { 197 log_err("ext2fs_file_llseek failed: %ld", err); 198 return 0; 199 } 200 201 err = ext2fs_file_read(e2_file, buf, size, &bytes_read); 202 if (err) { 203 log_err("ext2fs_file_read failed: %ld", err); 204 return 0; 205 } 206 207 return bytes_read; 208 } 209 210 /* 211 * Detect quota format and initialize quota IO 212 */ 213 errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h, 214 ext2_ino_t qf_ino, enum quota_type qtype, 215 int fmt, int flags) 216 { 217 ext2_filsys fs = qctx->fs; 218 ext2_file_t e2_file; 219 errcode_t err; 220 int allocated_handle = 0; 221 222 if (qtype >= MAXQUOTAS) 223 return EINVAL; 224 225 if (fmt == -1) 226 fmt = QFMT_VFS_V1; 227 228 err = ext2fs_read_bitmaps(fs); 229 if (err) 230 return err; 231 232 if (qf_ino == 0) 233 qf_ino = *quota_sb_inump(fs->super, qtype) 234 235 log_debug("Opening quota ino=%lu, type=%d", qf_ino, qtype); 236 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file); 237 if (err) { 238 log_err("ext2fs_file_open failed: %s", error_message(err)); 239 return err; 240 } 241 242 if (!h) { 243 if (qctx->quota_file[qtype]) { 244 h = qctx->quota_file[qtype]; 245 if (((flags & EXT2_FILE_WRITE) == 0) || 246 (h->qh_file_flags & EXT2_FILE_WRITE)) 247 return 0; 248 (void) quota_file_close(qctx, h); 249 } 250 err = ext2fs_get_mem(sizeof(struct quota_handle), &h); 251 if (err) { 252 log_err("Unable to allocate quota handle"); 253 return err; 254 } 255 allocated_handle = 1; 256 } 257 258 h->qh_qf.e2_file = e2_file; 259 h->qh_qf.fs = fs; 260 h->qh_qf.ino = qf_ino; 261 h->e2fs_write = quota_write_nomount; 262 h->e2fs_read = quota_read_nomount; 263 h->qh_file_flags = flags; 264 h->qh_io_flags = 0; 265 h->qh_type = qtype; 266 h->qh_fmt = fmt; 267 memset(&h->qh_info, 0, sizeof(h->qh_info)); 268 h->qh_ops = "afile_ops_2; 269 270 if (h->qh_ops->check_file && 271 (h->qh_ops->check_file(h, qtype, fmt) == 0)) { 272 log_err("qh_ops->check_file failed"); 273 goto errout; 274 } 275 276 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) { 277 log_err("qh_ops->init_io failed"); 278 goto errout; 279 } 280 if (allocated_handle) 281 qctx->quota_file[qtype] = h; 282 283 return 0; 284 errout: 285 ext2fs_file_close(e2_file); 286 if (allocated_handle) 287 ext2fs_free_mem(&h); 288 return -1; 289 } 290 291 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino) 292 { 293 struct ext2_inode inode; 294 errcode_t err = 0; 295 296 err = ext2fs_read_inode(fs, ino, &inode); 297 if (err) { 298 log_err("ex2fs_read_inode failed"); 299 return err; 300 } 301 302 if (EXT2_I_SIZE(&inode)) { 303 err = quota_inode_truncate(fs, ino); 304 if (err) 305 return err; 306 } 307 308 memset(&inode, 0, sizeof(struct ext2_inode)); 309 ext2fs_iblk_set(fs, &inode, 0); 310 inode.i_atime = inode.i_mtime = 311 inode.i_ctime = fs->now ? fs->now : time(0); 312 inode.i_links_count = 1; 313 inode.i_mode = LINUX_S_IFREG | 0600; 314 inode.i_flags |= EXT2_IMMUTABLE_FL; 315 if (ext2fs_has_feature_extents(fs->super)) 316 inode.i_flags |= EXT4_EXTENTS_FL; 317 318 err = ext2fs_write_new_inode(fs, ino, &inode); 319 if (err) { 320 log_err("ext2fs_write_new_inode failed: %ld", err); 321 return err; 322 } 323 return err; 324 } 325 326 /* 327 * Create new quotafile of specified format on given filesystem 328 */ 329 errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs, 330 enum quota_type qtype, int fmt) 331 { 332 ext2_file_t e2_file; 333 errcode_t err; 334 ext2_ino_t qf_inum = 0; 335 336 if (fmt == -1) 337 fmt = QFMT_VFS_V1; 338 339 h->qh_qf.fs = fs; 340 qf_inum = quota_type2inum(qtype, fs->super); 341 if (qf_inum == 0 && qtype == PRJQUOTA) { 342 err = ext2fs_new_inode(fs, EXT2_ROOT_INO, LINUX_S_IFREG | 0600, 343 0, &qf_inum); 344 if (err) 345 return err; 346 ext2fs_inode_alloc_stats2(fs, qf_inum, +1, 0); 347 ext2fs_mark_ib_dirty(fs); 348 } else if (qf_inum == 0) { 349 return EXT2_ET_BAD_INODE_NUM; 350 } 351 352 err = ext2fs_read_bitmaps(fs); 353 if (err) 354 goto out_err; 355 356 err = quota_inode_init_new(fs, qf_inum); 357 if (err) { 358 log_err("init_new_quota_inode failed"); 359 goto out_err; 360 } 361 h->qh_qf.ino = qf_inum; 362 h->qh_file_flags = EXT2_FILE_WRITE | EXT2_FILE_CREATE; 363 h->e2fs_write = quota_write_nomount; 364 h->e2fs_read = quota_read_nomount; 365 366 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type); 367 err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file); 368 if (err) { 369 log_err("ext2fs_file_open failed: %ld", err); 370 goto out_err; 371 } 372 h->qh_qf.e2_file = e2_file; 373 374 h->qh_io_flags = 0; 375 h->qh_type = qtype; 376 h->qh_fmt = fmt; 377 memset(&h->qh_info, 0, sizeof(h->qh_info)); 378 h->qh_ops = "afile_ops_2; 379 380 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) { 381 log_err("qh_ops->new_io failed"); 382 err = EIO; 383 goto out_err1; 384 } 385 386 return 0; 387 388 out_err1: 389 ext2fs_file_close(e2_file); 390 out_err: 391 392 if (qf_inum) 393 quota_inode_truncate(fs, qf_inum); 394 395 return err; 396 } 397 398 /* 399 * Close quotafile and release handle 400 */ 401 errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h) 402 { 403 if (h->qh_io_flags & IOFL_INFODIRTY) { 404 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0) 405 return -1; 406 h->qh_io_flags &= ~IOFL_INFODIRTY; 407 } 408 409 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0) 410 return -1; 411 if (h->qh_qf.e2_file) { 412 __u64 new_size, size; 413 414 new_size = compute_inode_size(h->qh_qf.fs, h->qh_qf.ino); 415 ext2fs_file_flush(h->qh_qf.e2_file); 416 if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &size)) 417 new_size = 0; 418 if (size != new_size) 419 ext2fs_file_set_size2(h->qh_qf.e2_file, new_size); 420 ext2fs_file_close(h->qh_qf.e2_file); 421 } 422 if (qctx->quota_file[h->qh_type] == h) 423 ext2fs_free_mem(&qctx->quota_file[h->qh_type]); 424 return 0; 425 } 426 427 /* 428 * Create empty quota structure 429 */ 430 struct dquot *get_empty_dquot(void) 431 { 432 struct dquot *dquot; 433 434 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) { 435 log_err("Failed to allocate dquot"); 436 return NULL; 437 } 438 439 dquot->dq_id = -1; 440 return dquot; 441 } 442