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