quotaio.c revision a9825cfe945e6dc6e300f89e7e613b80afde0189
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 compute_num_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) 106{ 107 blk64_t *num_blocks = private; 108 109 *num_blocks += 1; 110 return 0; 111} 112 113errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino) 114{ 115 struct ext2_inode inode; 116 errcode_t err; 117 118 if ((err = ext2fs_read_inode(fs, ino, &inode))) 119 return err; 120 121 if ((ino == EXT4_USR_QUOTA_INO) || (ino == EXT4_GRP_QUOTA_INO)) { 122 inode.i_dtime = fs->now ? fs->now : time(0); 123 if (!ext2fs_inode_has_valid_blocks2(fs, &inode)) 124 return 0; 125 err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL); 126 if (err) 127 return err; 128 fs->flags &= ~EXT2_FLAG_SUPER_ONLY; 129 memset(&inode, 0, sizeof(struct ext2_inode)); 130 } else { 131 inode.i_flags &= ~EXT2_IMMUTABLE_FL; 132 } 133 err = ext2fs_write_inode(fs, ino, &inode); 134 return err; 135} 136 137static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino) 138{ 139 blk64_t num_blocks = 0; 140 141 ext2fs_block_iterate3(fs, ino, 142 BLOCK_FLAG_READ_ONLY, 143 NULL, 144 compute_num_blocks_proc, 145 &num_blocks); 146 return num_blocks * fs->blocksize; 147} 148 149/* Functions to read/write quota file. */ 150static unsigned int quota_write_nomount(struct quota_file *qf, 151 ext2_loff_t offset, 152 void *buf, unsigned int size) 153{ 154 ext2_file_t e2_file = qf->e2_file; 155 unsigned int bytes_written = 0; 156 errcode_t err; 157 158 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL); 159 if (err) { 160 log_err("ext2fs_file_llseek failed: %ld", err); 161 return 0; 162 } 163 164 err = ext2fs_file_write(e2_file, buf, size, &bytes_written); 165 if (err) { 166 log_err("ext2fs_file_write failed: %ld", err); 167 return 0; 168 } 169 170 /* Correct inode.i_size is set in end_io. */ 171 return bytes_written; 172} 173 174static unsigned int quota_read_nomount(struct quota_file *qf, 175 ext2_loff_t offset, 176 void *buf, unsigned int size) 177{ 178 ext2_file_t e2_file = qf->e2_file; 179 unsigned int bytes_read = 0; 180 errcode_t err; 181 182 err = ext2fs_file_llseek(e2_file, offset, EXT2_SEEK_SET, NULL); 183 if (err) { 184 log_err("ext2fs_file_llseek failed: %ld", err); 185 return 0; 186 } 187 188 err = ext2fs_file_read(e2_file, buf, size, &bytes_read); 189 if (err) { 190 log_err("ext2fs_file_read failed: %ld", err); 191 return 0; 192 } 193 194 return bytes_read; 195} 196 197/* 198 * Detect quota format and initialize quota IO 199 */ 200errcode_t quota_file_open(struct quota_handle *h, ext2_filsys fs, 201 ext2_ino_t qf_ino, int type, int fmt, int flags) 202{ 203 ext2_file_t e2_file; 204 errcode_t err; 205 206 if (fmt == -1) 207 fmt = QFMT_VFS_V1; 208 209 err = ext2fs_read_bitmaps(fs); 210 if (err) 211 return err; 212 213 log_debug("Opening quota ino=%lu, type=%d", qf_ino, type); 214 err = ext2fs_file_open(fs, qf_ino, flags, &e2_file); 215 if (err) { 216 log_err("ext2fs_file_open failed: %s", error_message(err)); 217 return err; 218 } 219 h->qh_qf.e2_file = e2_file; 220 221 h->qh_qf.fs = fs; 222 h->qh_qf.ino = qf_ino; 223 h->e2fs_write = quota_write_nomount; 224 h->e2fs_read = quota_read_nomount; 225 h->qh_io_flags = 0; 226 h->qh_type = type; 227 h->qh_fmt = fmt; 228 memset(&h->qh_info, 0, sizeof(h->qh_info)); 229 h->qh_ops = "afile_ops_2; 230 231 if (h->qh_ops->check_file && 232 (h->qh_ops->check_file(h, type, fmt) == 0)) { 233 log_err("qh_ops->check_file failed"); 234 ext2fs_file_close(e2_file); 235 return -1; 236 } 237 238 if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) { 239 log_err("qh_ops->init_io failed"); 240 ext2fs_file_close(e2_file); 241 return -1; 242 } 243 244 return 0; 245} 246 247static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino) 248{ 249 struct ext2_inode inode; 250 errcode_t err = 0; 251 252 err = ext2fs_read_inode(fs, ino, &inode); 253 if (err) { 254 log_err("ex2fs_read_inode failed"); 255 return err; 256 } 257 258 if (EXT2_I_SIZE(&inode)) 259 quota_inode_truncate(fs, ino); 260 261 memset(&inode, 0, sizeof(struct ext2_inode)); 262 ext2fs_iblk_set(fs, &inode, 0); 263 inode.i_atime = inode.i_mtime = 264 inode.i_ctime = fs->now ? fs->now : time(0); 265 inode.i_links_count = 1; 266 inode.i_mode = LINUX_S_IFREG | 0600; 267 inode.i_flags |= EXT2_IMMUTABLE_FL; 268 if (fs->super->s_feature_incompat & 269 EXT3_FEATURE_INCOMPAT_EXTENTS) 270 inode.i_flags |= EXT4_EXTENTS_FL; 271 272 err = ext2fs_write_new_inode(fs, ino, &inode); 273 if (err) { 274 log_err("ext2fs_write_new_inode failed: %ld", err); 275 return err; 276 } 277 return err; 278} 279 280/* 281 * Create new quotafile of specified format on given filesystem 282 */ 283errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs, int type, int fmt) 284{ 285 ext2_file_t e2_file; 286 int err; 287 unsigned long qf_inum; 288 289 if (fmt == -1) 290 fmt = QFMT_VFS_V1; 291 292 h->qh_qf.fs = fs; 293 if (type == USRQUOTA) 294 qf_inum = EXT4_USR_QUOTA_INO; 295 else if (type == GRPQUOTA) 296 qf_inum = EXT4_GRP_QUOTA_INO; 297 else 298 return -1; 299 300 err = ext2fs_read_bitmaps(fs); 301 if (err) 302 goto out_err; 303 304 err = quota_inode_init_new(fs, qf_inum); 305 if (err) { 306 log_err("init_new_quota_inode failed"); 307 goto out_err; 308 } 309 h->qh_qf.ino = qf_inum; 310 h->e2fs_write = quota_write_nomount; 311 h->e2fs_read = quota_read_nomount; 312 313 log_debug("Creating quota ino=%lu, type=%d", qf_inum, type); 314 err = ext2fs_file_open(fs, qf_inum, 315 EXT2_FILE_WRITE | EXT2_FILE_CREATE, &e2_file); 316 if (err) { 317 log_err("ext2fs_file_open failed: %d", err); 318 goto out_err; 319 } 320 h->qh_qf.e2_file = e2_file; 321 322 h->qh_io_flags = 0; 323 h->qh_type = type; 324 h->qh_fmt = fmt; 325 memset(&h->qh_info, 0, sizeof(h->qh_info)); 326 h->qh_ops = "afile_ops_2; 327 328 if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) { 329 log_err("qh_ops->new_io failed"); 330 goto out_err1; 331 } 332 333 return 0; 334 335out_err1: 336 ext2fs_file_close(e2_file); 337out_err: 338 339 if (qf_inum) 340 quota_inode_truncate(fs, qf_inum); 341 342 return -1; 343} 344 345/* 346 * Close quotafile and release handle 347 */ 348errcode_t quota_file_close(struct quota_handle *h) 349{ 350 if (h->qh_io_flags & IOFL_INFODIRTY) { 351 if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0) 352 return -1; 353 h->qh_io_flags &= ~IOFL_INFODIRTY; 354 } 355 356 if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0) 357 return -1; 358 if (h->qh_qf.e2_file) { 359 ext2fs_file_flush(h->qh_qf.e2_file); 360 ext2fs_file_set_size2(h->qh_qf.e2_file, 361 compute_inode_size(h->qh_qf.fs, h->qh_qf.ino)); 362 ext2fs_file_close(h->qh_qf.e2_file); 363 } 364 365 return 0; 366} 367 368/* 369 * Create empty quota structure 370 */ 371struct dquot *get_empty_dquot(void) 372{ 373 struct dquot *dquot; 374 375 if (ext2fs_get_memzero(sizeof(struct dquot), &dquot)) { 376 log_err("Failed to allocate dquot"); 377 return NULL; 378 } 379 380 dquot->dq_id = -1; 381 return dquot; 382} 383