Whamcloud - gitweb
Prevent i_dtime from being mistaken for an inode number post-2038 wraparound
[tools/e2fsprogs.git] / lib / support / quotaio.c
index ba3a422..f5f2c7f 100644 (file)
@@ -44,7 +44,7 @@ struct disk_dqheader {
  */
 const char *quota_type2name(enum quota_type qtype)
 {
-       if (qtype < 0 || qtype >= MAXQUOTAS)
+       if (qtype >= MAXQUOTAS)
                return "unknown";
        return extensions[qtype];
 }
@@ -105,19 +105,6 @@ void update_grace_times(struct dquot *q)
        }
 }
 
-static int compute_num_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
-                                  blk64_t *blocknr EXT2FS_ATTR((unused)),
-                                  e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
-                                  blk64_t ref_block EXT2FS_ATTR((unused)),
-                                  int ref_offset EXT2FS_ATTR((unused)),
-                                  void *private)
-{
-       blk64_t *num_blocks = private;
-
-       *num_blocks += 1;
-       return 0;
-}
-
 errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
 {
        struct ext2_inode inode;
@@ -132,7 +119,7 @@ errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
                        break;
 
        if (qtype != MAXQUOTAS) {
-               inode.i_dtime = fs->now ? fs->now : time(0);
+               ext2fs_set_dtime(fs, &inode);
                if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
                        return 0;
                err = ext2fs_punch(fs, ino, &inode, NULL, 0, ~0ULL);
@@ -147,18 +134,6 @@ errcode_t quota_inode_truncate(ext2_filsys fs, ext2_ino_t ino)
        return err;
 }
 
-static ext2_off64_t compute_inode_size(ext2_filsys fs, ext2_ino_t ino)
-{
-       blk64_t num_blocks = 0;
-
-       ext2fs_block_iterate3(fs, ino,
-                             BLOCK_FLAG_READ_ONLY,
-                             NULL,
-                             compute_num_blocks_proc,
-                             &num_blocks);
-       return num_blocks * fs->blocksize;
-}
-
 /* Functions to read/write quota file. */
 static unsigned int quota_write_nomount(struct quota_file *qf,
                                        ext2_loff_t offset,
@@ -230,9 +205,9 @@ errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
                return err;
 
        if (qf_ino == 0)
-               qf_ino = *quota_sb_inump(fs->super, qtype)
+               qf_ino = *quota_sb_inump(fs->super, qtype);
 
-       log_debug("Opening quota ino=%lu, type=%d", qf_ino, qtype);
+       log_debug("Opening quota ino=%u, type=%d", qf_ino, qtype);
        err = ext2fs_file_open(fs, qf_ino, flags, &e2_file);
        if (err) {
                log_err("ext2fs_file_open failed: %s", error_message(err));
@@ -243,13 +218,16 @@ errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
                if (qctx->quota_file[qtype]) {
                        h = qctx->quota_file[qtype];
                        if (((flags & EXT2_FILE_WRITE) == 0) ||
-                           (h->qh_file_flags & EXT2_FILE_WRITE))
+                           (h->qh_file_flags & EXT2_FILE_WRITE)) {
+                               ext2fs_file_close(e2_file);
                                return 0;
+                       }
                        (void) quota_file_close(qctx, h);
                }
                err = ext2fs_get_mem(sizeof(struct quota_handle), &h);
                if (err) {
                        log_err("Unable to allocate quota handle");
+                       ext2fs_file_close(e2_file);
                        return err;
                }
                allocated_handle = 1;
@@ -270,11 +248,13 @@ errcode_t quota_file_open(quota_ctx_t qctx, struct quota_handle *h,
        if (h->qh_ops->check_file &&
            (h->qh_ops->check_file(h, qtype, fmt) == 0)) {
                log_err("qh_ops->check_file failed");
+               err = EIO;
                goto errout;
        }
 
        if (h->qh_ops->init_io && (h->qh_ops->init_io(h) < 0)) {
                log_err("qh_ops->init_io failed");
+               err = EIO;
                goto errout;
        }
        if (allocated_handle)
@@ -285,13 +265,14 @@ errout:
        ext2fs_file_close(e2_file);
        if (allocated_handle)
                ext2fs_free_mem(&h);
-       return -1;
+       return err;
 }
 
 static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
 {
        struct ext2_inode inode;
        errcode_t err = 0;
+       time_t now;
 
        err = ext2fs_read_inode(fs, ino, &inode);
        if (err) {
@@ -299,13 +280,18 @@ static errcode_t quota_inode_init_new(ext2_filsys fs, ext2_ino_t ino)
                return err;
        }
 
-       if (EXT2_I_SIZE(&inode))
-               quota_inode_truncate(fs, ino);
+       if (EXT2_I_SIZE(&inode)) {
+               err = quota_inode_truncate(fs, ino);
+               if (err)
+                       return err;
+       }
 
        memset(&inode, 0, sizeof(struct ext2_inode));
        ext2fs_iblk_set(fs, &inode, 0);
-       inode.i_atime = inode.i_mtime =
-               inode.i_ctime = fs->now ? fs->now : time(0);
+       now = fs->now ? fs->now : time(0);
+       ext2fs_inode_xtime_set(&inode, i_atime, now);
+       ext2fs_inode_xtime_set(&inode, i_ctime, now);
+       ext2fs_inode_xtime_set(&inode, i_mtime, now);
        inode.i_links_count = 1;
        inode.i_mode = LINUX_S_IFREG | 0600;
        inode.i_flags |= EXT2_IMMUTABLE_FL;
@@ -327,8 +313,8 @@ errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
                            enum quota_type qtype, int fmt)
 {
        ext2_file_t e2_file;
-       int err;
-       unsigned long qf_inum = 0;
+       errcode_t err;
+       ext2_ino_t qf_inum = 0;
 
        if (fmt == -1)
                fmt = QFMT_VFS_V1;
@@ -339,11 +325,11 @@ errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
                err = ext2fs_new_inode(fs, EXT2_ROOT_INO, LINUX_S_IFREG | 0600,
                                       0, &qf_inum);
                if (err)
-                       return -1;
+                       return err;
                ext2fs_inode_alloc_stats2(fs, qf_inum, +1, 0);
                ext2fs_mark_ib_dirty(fs);
        } else if (qf_inum == 0) {
-               return -1;
+               return EXT2_ET_BAD_INODE_NUM;
        }
 
        err = ext2fs_read_bitmaps(fs);
@@ -360,10 +346,10 @@ errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
        h->e2fs_write = quota_write_nomount;
        h->e2fs_read = quota_read_nomount;
 
-       log_debug("Creating quota ino=%lu, type=%d", qf_inum, type);
+       log_debug("Creating quota ino=%u, type=%d", qf_inum, qtype);
        err = ext2fs_file_open(fs, qf_inum, h->qh_file_flags, &e2_file);
        if (err) {
-               log_err("ext2fs_file_open failed: %d", err);
+               log_err("ext2fs_file_open failed: %ld", err);
                goto out_err;
        }
        h->qh_qf.e2_file = e2_file;
@@ -376,6 +362,7 @@ errcode_t quota_file_create(struct quota_handle *h, ext2_filsys fs,
 
        if (h->qh_ops->new_io && (h->qh_ops->new_io(h) < 0)) {
                log_err("qh_ops->new_io failed");
+               err = EIO;
                goto out_err1;
        }
 
@@ -388,7 +375,7 @@ out_err:
        if (qf_inum)
                quota_inode_truncate(fs, qf_inum);
 
-       return -1;
+       return err;
 }
 
 /*
@@ -398,23 +385,14 @@ errcode_t quota_file_close(quota_ctx_t qctx, struct quota_handle *h)
 {
        if (h->qh_io_flags & IOFL_INFODIRTY) {
                if (h->qh_ops->write_info && h->qh_ops->write_info(h) < 0)
-                       return -1;
+                       return EIO;
                h->qh_io_flags &= ~IOFL_INFODIRTY;
        }
 
        if (h->qh_ops->end_io && h->qh_ops->end_io(h) < 0)
-               return -1;
-       if (h->qh_qf.e2_file) {
-               __u64 new_size, size;
-
-               new_size = compute_inode_size(h->qh_qf.fs, h->qh_qf.ino);
-               ext2fs_file_flush(h->qh_qf.e2_file);
-               if (ext2fs_file_get_lsize(h->qh_qf.e2_file, &size))
-                       new_size = 0;
-               if (size != new_size)
-                       ext2fs_file_set_size2(h->qh_qf.e2_file, new_size);
+               return EIO;
+       if (h->qh_qf.e2_file)
                ext2fs_file_close(h->qh_qf.e2_file);
-       }
        if (qctx->quota_file[h->qh_type] == h)
                ext2fs_free_mem(&qctx->quota_file[h->qh_type]);
        return 0;