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