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