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