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