Whamcloud - gitweb
e2fsprogs: fix compile error and warnings for old gcc versions
[tools/e2fsprogs.git] / lib / ext2fs / icount.c
index de0b614..1f5f6a2 100644 (file)
@@ -4,11 +4,12 @@
  * Copyright (C) 1997 Theodore Ts'o.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -43,7 +44,7 @@
 
 struct ext2_icount_el {
        ext2_ino_t      ino;
-       __u16   count;
+       __u32           count;
 };
 
 struct ext2_icount {
@@ -56,10 +57,22 @@ struct ext2_icount {
        ext2_ino_t              cursor;
        struct ext2_icount_el   *list;
        struct ext2_icount_el   *last_lookup;
+#ifdef CONFIG_TDB
        char                    *tdb_fn;
        TDB_CONTEXT             *tdb;
+#endif
+       __u16                   *fullmap;
 };
 
+/*
+ * We now use a 32-bit counter field because it doesn't cost us
+ * anything extra for the in-memory data structure, due to alignment
+ * padding.  But there's no point changing the interface if most of
+ * the time we only care if the number is bigger than 65,000 or not.
+ * So use the following translation function to return a 16-bit count.
+ */
+#define icount_16_xlate(x) (((x) > 65500) ? 65500 : (x))
+
 void ext2fs_free_icount(ext2_icount_t icount)
 {
        if (!icount)
@@ -72,12 +85,17 @@ void ext2fs_free_icount(ext2_icount_t icount)
                ext2fs_free_inode_bitmap(icount->single);
        if (icount->multiple)
                ext2fs_free_inode_bitmap(icount->multiple);
+#ifdef CONFIG_TDB
        if (icount->tdb)
                tdb_close(icount->tdb);
        if (icount->tdb_fn) {
-               unlink(icount->tdb_fn);
+               (void) unlink(icount->tdb_fn);
                free(icount->tdb_fn);
        }
+#endif
+
+       if (icount->fullmap)
+               ext2fs_free_mem(&icount->fullmap);
 
        ext2fs_free_mem(&icount);
 }
@@ -93,21 +111,34 @@ static errcode_t alloc_icount(ext2_filsys fs, int flags, ext2_icount_t *ret)
        if (retval)
                return retval;
        memset(icount, 0, sizeof(struct ext2_icount));
+       icount->magic = EXT2_ET_MAGIC_ICOUNT;
+       icount->num_inodes = fs->super->s_inodes_count;
 
-       retval = ext2fs_allocate_inode_bitmap(fs, 0, &icount->single);
+       if ((flags & EXT2_ICOUNT_OPT_FULLMAP) &&
+           (flags & EXT2_ICOUNT_OPT_INCREMENT)) {
+               unsigned sz = sizeof(*icount->fullmap) * icount->num_inodes;
+
+               retval = ext2fs_get_mem(sz, &icount->fullmap);
+               /* If we can't allocate, fall back */
+               if (!retval) {
+                       memset(icount->fullmap, 0, sz);
+                       *ret = icount;
+                       return 0;
+               }
+       }
+
+       retval = ext2fs_allocate_inode_bitmap(fs, "icount", &icount->single);
        if (retval)
                goto errout;
 
        if (flags & EXT2_ICOUNT_OPT_INCREMENT) {
-               retval = ext2fs_allocate_inode_bitmap(fs, 0,
+               retval = ext2fs_allocate_inode_bitmap(fs, "icount_inc",
                                                      &icount->multiple);
                if (retval)
                        goto errout;
-       } else
+       } else {
                icount->multiple = 0;
-
-       icount->magic = EXT2_ET_MAGIC_ICOUNT;
-       icount->num_inodes = fs->super->s_inodes_count;
+       }
 
        *ret = icount;
        return 0;
@@ -117,6 +148,7 @@ errout:
        return(retval);
 }
 
+#ifdef CONFIG_TDB
 struct uuid {
        __u32   time_low;
        __u16   time_mid;
@@ -163,13 +195,19 @@ static void uuid_unparse(void *uu, char *out)
                uuid.node[0], uuid.node[1], uuid.node[2],
                uuid.node[3], uuid.node[4], uuid.node[5]);
 }
+#endif
 
-errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
-                                  int flags, ext2_icount_t *ret)
+errcode_t ext2fs_create_icount_tdb(ext2_filsys fs EXT2FS_NO_TDB_UNUSED,
+                                  char *tdb_dir EXT2FS_NO_TDB_UNUSED,
+                                  int flags EXT2FS_NO_TDB_UNUSED,
+                                  ext2_icount_t *ret EXT2FS_NO_TDB_UNUSED)
 {
+#ifdef CONFIG_TDB
        ext2_icount_t   icount;
        errcode_t       retval;
        char            *fn, uuid[40];
+       ext2_ino_t      num_inodes;
+       mode_t          save_umask;
        int             fd;
 
        retval = alloc_icount(fs, flags,  &icount);
@@ -181,23 +219,40 @@ errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir,
                goto errout;
        uuid_unparse(fs->super->s_uuid, uuid);
        sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid);
+       save_umask = umask(077);
        fd = mkstemp(fn);
-
-       icount->tdb_fn = fn;
-       icount->tdb = tdb_open(fn, 0, TDB_CLEAR_IF_FIRST,
-                              O_RDWR | O_CREAT | O_TRUNC, 0600);
-       if (icount->tdb) {
-               close(fd);
-               *ret = icount;
-               return 0;
+       if (fd < 0) {
+               retval = errno;
+               ext2fs_free_mem(&fn);
+               goto errout;
        }
+       icount->tdb_fn = fn;
+       umask(save_umask);
+       /*
+        * This is an overestimate of the size that we will need; the
+        * ideal value is the number of used inodes with a count
+        * greater than 1.  OTOH the times when we really need this is
+        * with the backup programs that use lots of hard links, in
+        * which case the number of inodes in use approaches the ideal
+        * value.
+        */
+       num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count;
 
-       retval = errno;
+       icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC,
+                              O_RDWR | O_CREAT | O_TRUNC, 0600);
        close(fd);
-
+       if (icount->tdb == NULL) {
+               retval = errno;
+               goto errout;
+       }
+       *ret = icount;
+       return 0;
 errout:
        ext2fs_free_icount(icount);
        return(retval);
+#else
+       return EXT2_ET_UNIMPLEMENTED;
+#endif
 }
 
 errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
@@ -218,6 +273,9 @@ errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
        if (retval)
                return retval;
 
+       if (icount->fullmap)
+               goto successout;
+
        if (size) {
                icount->size = size;
        } else {
@@ -237,7 +295,8 @@ errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
        printf("Icount allocated %u entries, %d bytes.\n",
               icount->size, bytes);
 #endif
-       retval = ext2fs_get_mem(bytes, &icount->list);
+       retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el),
+                        &icount->list);
        if (retval)
                goto errout;
        memset(icount->list, 0, bytes);
@@ -256,6 +315,7 @@ errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size,
                icount->count = hint->count;
        }
 
+successout:
        *ret = icount;
        return 0;
 
@@ -329,9 +389,7 @@ static struct ext2_icount_el *insert_icount_el(ext2_icount_t icount,
 static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
                                            ext2_ino_t ino, int create)
 {
-       float   range;
        int     low, high, mid;
-       ext2_ino_t      lowval, highval;
 
        if (!icount || !icount->list)
                return 0;
@@ -353,31 +411,7 @@ static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
        low = 0;
        high = (int) icount->count-1;
        while (low <= high) {
-#if 0
-               mid = (low+high)/2;
-#else
-               if (low == high)
-                       mid = low;
-               else {
-                       /* Interpolate for efficiency */
-                       lowval = icount->list[low].ino;
-                       highval = icount->list[high].ino;
-
-                       if (ino < lowval)
-                               range = 0;
-                       else if (ino > highval)
-                               range = 1;
-                       else {
-                               range = ((float) (ino - lowval)) /
-                                       (highval - lowval);
-                               if (range > 0.9)
-                                       range = 0.9;
-                               if (range < 0.1)
-                                       range = 0.1;
-                       }
-                       mid = low + ((int) (range * (high-low)));
-               }
-#endif
+               mid = ((unsigned)low + (unsigned)high) >> 1;
                if (ino == icount->list[mid].ino) {
                        icount->cursor = mid+1;
                        return &icount->list[mid];
@@ -397,16 +431,17 @@ static struct ext2_icount_el *get_icount_el(ext2_icount_t icount,
 }
 
 static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
-                                __u16 count)
+                                __u32 count)
 {
        struct ext2_icount_el   *el;
+#ifdef CONFIG_TDB
        TDB_DATA key, data;
 
        if (icount->tdb) {
                key.dptr = (unsigned char *) &ino;
                key.dsize = sizeof(ext2_ino_t);
                data.dptr = (unsigned char *) &count;
-               data.dsize = sizeof(__u16);
+               data.dsize = sizeof(__u32);
                if (count) {
                        if (tdb_store(icount->tdb, key, data, TDB_REPLACE))
                                return tdb_error(icount->tdb) +
@@ -418,6 +453,11 @@ static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
                }
                return 0;
        }
+#endif
+       if (icount->fullmap) {
+               icount->fullmap[ino] = icount_16_xlate(count);
+               return 0;
+       }
 
        el = get_icount_el(icount, ino, 1);
        if (!el)
@@ -428,9 +468,10 @@ static errcode_t set_inode_count(ext2_icount_t icount, ext2_ino_t ino,
 }
 
 static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
-                                __u16 *count)
+                                __u32 *count)
 {
        struct ext2_icount_el   *el;
+#ifdef CONFIG_TDB
        TDB_DATA key, data;
 
        if (icount->tdb) {
@@ -443,10 +484,16 @@ static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
                        return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
                }
 
-               *count = *((__u16 *) data.dptr);
+               *count = *((__u32 *) data.dptr);
                free(data.dptr);
                return 0;
        }
+#endif
+       if (icount->fullmap) {
+               *count = icount->fullmap[ino];
+               return 0;
+       }
+
        el = get_icount_el(icount, ino, 0);
        if (!el) {
                *count = 0;
@@ -457,6 +504,23 @@ static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
        return 0;
 }
 
+int ext2fs_icount_is_set(ext2_icount_t icount, ext2_ino_t ino)
+{
+       __u16 result;
+
+       if (ext2fs_test_inode_bitmap2(icount->single, ino))
+               return 1;
+       else if (icount->multiple) {
+               if (ext2fs_test_inode_bitmap2(icount->multiple, ino))
+                       return 1;
+               return 0;
+       }
+       ext2fs_icount_fetch(icount, ino, &result);
+       if (result)
+               return 1;
+       return 0;
+}
+
 errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
 {
        errcode_t       ret = 0;
@@ -482,35 +546,42 @@ errcode_t ext2fs_icount_validate(ext2_icount_t icount, FILE *out)
 
 errcode_t ext2fs_icount_fetch(ext2_icount_t icount, ext2_ino_t ino, __u16 *ret)
 {
+       __u32   val;
        EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
 
        if (!ino || (ino > icount->num_inodes))
                return EXT2_ET_INVALID_ARGUMENT;
 
-       if (ext2fs_test_inode_bitmap(icount->single, ino)) {
-               *ret = 1;
-               return 0;
-       }
-       if (icount->multiple &&
-           !ext2fs_test_inode_bitmap(icount->multiple, ino)) {
-               *ret = 0;
-               return 0;
+       if (!icount->fullmap) {
+               if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
+                       *ret = 1;
+                       return 0;
+               }
+               if (icount->multiple &&
+                       !ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
+                       *ret = 0;
+                       return 0;
+               }
        }
-       get_inode_count(icount, ino, ret);
+       get_inode_count(icount, ino, &val);
+       *ret = icount_16_xlate(val);
        return 0;
 }
 
 errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
                                  __u16 *ret)
 {
-       __u16                   curr_value;
+       __u32                   curr_value;
 
        EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
 
        if (!ino || (ino > icount->num_inodes))
                return EXT2_ET_INVALID_ARGUMENT;
 
-       if (ext2fs_test_inode_bitmap(icount->single, ino)) {
+       if (icount->fullmap) {
+               curr_value = icount_16_xlate(icount->fullmap[ino] + 1);
+               icount->fullmap[ino] = curr_value;
+       } else if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
                /*
                 * If the existing count is 1, then we know there is
                 * no entry in the list.
@@ -518,14 +589,14 @@ errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
                if (set_inode_count(icount, ino, 2))
                        return EXT2_ET_NO_MEMORY;
                curr_value = 2;
-               ext2fs_unmark_inode_bitmap(icount->single, ino);
+               ext2fs_unmark_inode_bitmap2(icount->single, ino);
        } else if (icount->multiple) {
                /*
                 * The count is either zero or greater than 1; if the
                 * inode is set in icount->multiple, then there should
                 * be an entry in the list, so we need to fix it.
                 */
-               if (ext2fs_test_inode_bitmap(icount->multiple, ino)) {
+               if (ext2fs_test_inode_bitmap2(icount->multiple, ino)) {
                        get_inode_count(icount, ino, &curr_value);
                        curr_value++;
                        if (set_inode_count(icount, ino, curr_value))
@@ -535,7 +606,7 @@ errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
                         * The count was zero; mark the single bitmap
                         * and return.
                         */
-                       ext2fs_mark_inode_bitmap(icount->single, ino);
+                       ext2fs_mark_inode_bitmap2(icount->single, ino);
                        if (ret)
                                *ret = 1;
                        return 0;
@@ -551,26 +622,36 @@ errcode_t ext2fs_icount_increment(ext2_icount_t icount, ext2_ino_t ino,
                        return EXT2_ET_NO_MEMORY;
        }
        if (icount->multiple)
-               ext2fs_mark_inode_bitmap(icount->multiple, ino);
+               ext2fs_mark_inode_bitmap2(icount->multiple, ino);
        if (ret)
-               *ret = curr_value;
+               *ret = icount_16_xlate(curr_value);
        return 0;
 }
 
 errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
                                  __u16 *ret)
 {
-       __u16                   curr_value;
+       __u32                   curr_value;
 
        if (!ino || (ino > icount->num_inodes))
                return EXT2_ET_INVALID_ARGUMENT;
 
        EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
 
-       if (ext2fs_test_inode_bitmap(icount->single, ino)) {
-               ext2fs_unmark_inode_bitmap(icount->single, ino);
+       if (icount->fullmap) {
+               if (!icount->fullmap[ino])
+                       return EXT2_ET_INVALID_ARGUMENT;
+
+               curr_value = --icount->fullmap[ino];
+               if (ret)
+                       *ret = icount_16_xlate(curr_value);
+               return 0;
+       }
+
+       if (ext2fs_test_inode_bitmap2(icount->single, ino)) {
+               ext2fs_unmark_inode_bitmap2(icount->single, ino);
                if (icount->multiple)
-                       ext2fs_unmark_inode_bitmap(icount->multiple, ino);
+                       ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
                else {
                        set_inode_count(icount, ino, 0);
                }
@@ -580,7 +661,7 @@ errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
        }
 
        if (icount->multiple &&
-           !ext2fs_test_inode_bitmap(icount->multiple, ino))
+           !ext2fs_test_inode_bitmap2(icount->multiple, ino))
                return EXT2_ET_INVALID_ARGUMENT;
 
        get_inode_count(icount, ino, &curr_value);
@@ -591,12 +672,12 @@ errcode_t ext2fs_icount_decrement(ext2_icount_t icount, ext2_ino_t ino,
                return EXT2_ET_NO_MEMORY;
 
        if (curr_value == 1)
-               ext2fs_mark_inode_bitmap(icount->single, ino);
+               ext2fs_mark_inode_bitmap2(icount->single, ino);
        if ((curr_value == 0) && icount->multiple)
-               ext2fs_unmark_inode_bitmap(icount->multiple, ino);
+               ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
 
        if (ret)
-               *ret = curr_value;
+               *ret = icount_16_xlate(curr_value);
        return 0;
 }
 
@@ -608,20 +689,23 @@ errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
 
        EXT2_CHECK_MAGIC(icount, EXT2_ET_MAGIC_ICOUNT);
 
+       if (icount->fullmap)
+               return set_inode_count(icount, ino, count);
+
        if (count == 1) {
-               ext2fs_mark_inode_bitmap(icount->single, ino);
+               ext2fs_mark_inode_bitmap2(icount->single, ino);
                if (icount->multiple)
-                       ext2fs_unmark_inode_bitmap(icount->multiple, ino);
+                       ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
                return 0;
        }
        if (count == 0) {
-               ext2fs_unmark_inode_bitmap(icount->single, ino);
+               ext2fs_unmark_inode_bitmap2(icount->single, ino);
                if (icount->multiple) {
                        /*
                         * If the icount->multiple bitmap is enabled,
                         * we can just clear both bitmaps and we're done
                         */
-                       ext2fs_unmark_inode_bitmap(icount->multiple, ino);
+                       ext2fs_unmark_inode_bitmap2(icount->multiple, ino);
                } else
                        set_inode_count(icount, ino, 0);
                return 0;
@@ -629,9 +713,9 @@ errcode_t ext2fs_icount_store(ext2_icount_t icount, ext2_ino_t ino,
 
        if (set_inode_count(icount, ino, count))
                return EXT2_ET_NO_MEMORY;
-       ext2fs_unmark_inode_bitmap(icount->single, ino);
+       ext2fs_unmark_inode_bitmap2(icount->single, ino);
        if (icount->multiple)
-               ext2fs_mark_inode_bitmap(icount->multiple, ino);
+               ext2fs_mark_inode_bitmap2(icount->multiple, ino);
        return 0;
 }
 
@@ -732,9 +816,9 @@ static void setup(void)
        initialize_ext2_error_table();
 
        memset(&param, 0, sizeof(param));
-       param.s_blocks_count = 12000;
+       ext2fs_blocks_count_set(&param, 12000);
 
-       retval = ext2fs_initialize("test fs", 0, &param,
+       retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, &param,
                                   test_io_manager, &test_fs);
        if (retval) {
                com_err("setup", retval,
@@ -758,6 +842,7 @@ int run_test(int flags, int size, char *dir, struct test_program *prog)
        int             problem = 0;
 
        if (dir) {
+#ifdef CONFIG_TDB
                retval = ext2fs_create_icount_tdb(test_fs, dir,
                                                  flags, &icount);
                if (retval) {
@@ -765,6 +850,10 @@ int run_test(int flags, int size, char *dir, struct test_program *prog)
                                "while creating icount using tdb");
                        exit(1);
                }
+#else
+               printf("Skipped\n");
+               return 0;
+#endif
        } else {
                retval = ext2fs_create_icount2(test_fs, flags, size, 0,
                                               &icount);