Whamcloud - gitweb
libext2fs: unix_io: fix_potential error path deadlock in flush_cached_blocks()
[tools/e2fsprogs.git] / lib / ext2fs / unix_io.c
index 2df53e5..353d85a 100644 (file)
@@ -94,6 +94,7 @@ struct unix_cache {
        int                     access_time;
        unsigned                dirty:1;
        unsigned                in_use:1;
+       unsigned                write_err:1;
 };
 
 #define CACHE_SIZE 8
@@ -218,6 +219,8 @@ static errcode_t raw_read_blk(io_channel channel,
        int             actual = 0;
        unsigned char   *buf = bufv;
        ssize_t         really_read = 0;
+       unsigned long long aligned_blk;
+       int             align_size, offset;
 
        size = (count < 0) ? -count : (ext2_loff_t) count * channel->block_size;
        mutex_lock(data, STATS_MTX);
@@ -225,18 +228,14 @@ static errcode_t raw_read_blk(io_channel channel,
        mutex_unlock(data, STATS_MTX);
        location = ((ext2_loff_t) block * channel->block_size) + data->offset;
 
-       if (data->flags & IO_FLAG_FORCE_BOUNCE) {
-               if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
-                       retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
-                       goto error_out;
-               }
+       if (data->flags & IO_FLAG_FORCE_BOUNCE)
                goto bounce_read;
-       }
 
 #ifdef HAVE_PREAD64
        /* Try an aligned pread */
        if ((channel->align == 0) ||
            (IS_ALIGNED(buf, channel->align) &&
+            IS_ALIGNED(location, channel->align) &&
             IS_ALIGNED(size, channel->align))) {
                actual = pread64(data->dev, buf, size, location);
                if (actual == size)
@@ -248,6 +247,7 @@ static errcode_t raw_read_blk(io_channel channel,
        if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
            ((channel->align == 0) ||
             (IS_ALIGNED(buf, channel->align) &&
+             IS_ALIGNED(location, channel->align) &&
              IS_ALIGNED(size, channel->align)))) {
                actual = pread(data->dev, buf, size, location);
                if (actual == size)
@@ -256,13 +256,15 @@ static errcode_t raw_read_blk(io_channel channel,
        }
 #endif /* HAVE_PREAD */
 
-       if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
-               retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
-               goto error_out;
-       }
        if ((channel->align == 0) ||
            (IS_ALIGNED(buf, channel->align) &&
+            IS_ALIGNED(location, channel->align) &&
             IS_ALIGNED(size, channel->align))) {
+               mutex_lock(data, BOUNCE_MTX);
+               if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
+                       retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
+                       goto error_unlock;
+               }
                actual = read(data->dev, buf, size);
                if (actual != size) {
                short_read:
@@ -271,9 +273,9 @@ static errcode_t raw_read_blk(io_channel channel,
                                actual = 0;
                        } else
                                retval = EXT2_ET_SHORT_READ;
-                       goto error_out;
+                       goto error_unlock;
                }
-               return 0;
+               goto success_unlock;
        }
 
 #ifdef ALIGN_DEBUG
@@ -286,28 +288,48 @@ static errcode_t raw_read_blk(io_channel channel,
         * to the O_DIRECT rules, so we need to do this the hard way...
         */
 bounce_read:
+       if (channel->align == 0)
+               channel->align = 1;
+       if ((channel->block_size > channel->align) &&
+           (channel->block_size % channel->align) == 0)
+               align_size = channel->block_size;
+       else
+               align_size = channel->align;
+       aligned_blk = location / align_size;
+       offset = location % align_size;
+
+       mutex_lock(data, BOUNCE_MTX);
+       if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
+               retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
+               goto error_unlock;
+       }
        while (size > 0) {
-               mutex_lock(data, BOUNCE_MTX);
-               actual = read(data->dev, data->bounce, channel->block_size);
-               if (actual != channel->block_size) {
+               actual = read(data->dev, data->bounce, align_size);
+               if (actual != align_size) {
                        mutex_unlock(data, BOUNCE_MTX);
                        actual = really_read;
                        buf -= really_read;
                        size += really_read;
                        goto short_read;
                }
-               actual = size;
-               if (size > channel->block_size)
-                       actual = channel->block_size;
-               memcpy(buf, data->bounce, actual);
+               if ((actual + offset) > align_size)
+                       actual = align_size - offset;
+               if (actual > size)
+                       actual = size;
+               memcpy(buf, (char *)data->bounce + offset, actual);
+
                really_read += actual;
                size -= actual;
                buf += actual;
-               mutex_unlock(data, BOUNCE_MTX);
+               offset = 0;
+               aligned_blk++;
        }
+success_unlock:
+       mutex_unlock(data, BOUNCE_MTX);
        return 0;
 
-error_out:
+error_unlock:
+       mutex_unlock(data, BOUNCE_MTX);
        if (actual >= 0 && actual < size)
                memset((char *) buf+actual, 0, size-actual);
        if (channel->read_error)
@@ -316,16 +338,21 @@ error_out:
        return retval;
 }
 
+#define RAW_WRITE_NO_HANDLER   1
+
 static errcode_t raw_write_blk(io_channel channel,
                               struct unix_private_data *data,
                               unsigned long long block,
-                              int count, const void *bufv)
+                              int count, const void *bufv,
+                              int flags)
 {
        ssize_t         size;
        ext2_loff_t     location;
        int             actual = 0;
        errcode_t       retval;
        const unsigned char *buf = bufv;
+       unsigned long long aligned_blk;
+       int             align_size, offset;
 
        if (count == 1)
                size = channel->block_size;
@@ -341,18 +368,14 @@ static errcode_t raw_write_blk(io_channel channel,
 
        location = ((ext2_loff_t) block * channel->block_size) + data->offset;
 
-       if (data->flags & IO_FLAG_FORCE_BOUNCE) {
-               if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
-                       retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
-                       goto error_out;
-               }
+       if (data->flags & IO_FLAG_FORCE_BOUNCE)
                goto bounce_write;
-       }
 
 #ifdef HAVE_PWRITE64
        /* Try an aligned pwrite */
        if ((channel->align == 0) ||
            (IS_ALIGNED(buf, channel->align) &&
+            IS_ALIGNED(location, channel->align) &&
             IS_ALIGNED(size, channel->align))) {
                actual = pwrite64(data->dev, buf, size, location);
                if (actual == size)
@@ -363,6 +386,7 @@ static errcode_t raw_write_blk(io_channel channel,
        if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
            ((channel->align == 0) ||
             (IS_ALIGNED(buf, channel->align) &&
+             IS_ALIGNED(location, channel->align) &&
              IS_ALIGNED(size, channel->align)))) {
                actual = pwrite(data->dev, buf, size, location);
                if (actual == size)
@@ -370,15 +394,17 @@ static errcode_t raw_write_blk(io_channel channel,
        }
 #endif /* HAVE_PWRITE */
 
-       if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
-               retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
-               goto error_out;
-       }
-
        if ((channel->align == 0) ||
            (IS_ALIGNED(buf, channel->align) &&
+            IS_ALIGNED(location, channel->align) &&
             IS_ALIGNED(size, channel->align))) {
+               mutex_lock(data, BOUNCE_MTX);
+               if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
+                       retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
+                       goto error_unlock;
+               }
                actual = write(data->dev, buf, size);
+               mutex_unlock(data, BOUNCE_MTX);
                if (actual < 0) {
                        retval = errno;
                        goto error_out;
@@ -400,45 +426,67 @@ static errcode_t raw_write_blk(io_channel channel,
         * to the O_DIRECT rules, so we need to do this the hard way...
         */
 bounce_write:
+       if (channel->align == 0)
+               channel->align = 1;
+       if ((channel->block_size > channel->align) &&
+           (channel->block_size % channel->align) == 0)
+               align_size = channel->block_size;
+       else
+               align_size = channel->align;
+       aligned_blk = location / align_size;
+       offset = location % align_size;
+
        while (size > 0) {
+               int actual_w;
+
                mutex_lock(data, BOUNCE_MTX);
-               if (size < channel->block_size) {
+               if (size < align_size || offset) {
+                       if (ext2fs_llseek(data->dev, aligned_blk * align_size,
+                                         SEEK_SET) < 0) {
+                               retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
+                               goto error_unlock;
+                       }
                        actual = read(data->dev, data->bounce,
-                                     channel->block_size);
-                       if (actual != channel->block_size) {
+                                     align_size);
+                       if (actual != align_size) {
                                if (actual < 0) {
-                                       mutex_unlock(data, BOUNCE_MTX);
                                        retval = errno;
-                                       goto error_out;
+                                       goto error_unlock;
                                }
                                memset((char *) data->bounce + actual, 0,
-                                      channel->block_size - actual);
+                                      align_size - actual);
                        }
                }
                actual = size;
-               if (size > channel->block_size)
-                       actual = channel->block_size;
-               memcpy(data->bounce, buf, actual);
-               if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
+               if ((actual + offset) > align_size)
+                       actual = align_size - offset;
+               if (actual > size)
+                       actual = size;
+               memcpy(((char *)data->bounce) + offset, buf, actual);
+               if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
                        retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
-                       goto error_out;
+                       goto error_unlock;
                }
-               actual = write(data->dev, data->bounce, channel->block_size);
+               actual_w = write(data->dev, data->bounce, align_size);
                mutex_unlock(data, BOUNCE_MTX);
-               if (actual < 0) {
+               if (actual_w < 0) {
                        retval = errno;
                        goto error_out;
                }
-               if (actual != channel->block_size)
+               if (actual_w != align_size)
                        goto short_write;
                size -= actual;
                buf += actual;
                location += actual;
+               aligned_blk++;
+               offset = 0;
        }
        return 0;
 
+error_unlock:
+       mutex_unlock(data, BOUNCE_MTX);
 error_out:
-       if (channel->write_error)
+       if (((flags & RAW_WRITE_NO_HANDLER) == 0) && channel->write_error)
                retval = (channel->write_error)(channel, block, count, buf,
                                                size, actual, retval);
        return retval;
@@ -532,16 +580,27 @@ static struct unix_cache *find_cached_block(struct unix_private_data *data,
 /*
  * Reuse a particular cache entry for another block.
  */
-static void reuse_cache(io_channel channel, struct unix_private_data *data,
-                struct unix_cache *cache, unsigned long long block)
+static errcode_t reuse_cache(io_channel channel,
+               struct unix_private_data *data, struct unix_cache *cache,
+               unsigned long long block)
 {
-       if (cache->dirty && cache->in_use)
-               raw_write_blk(channel, data, cache->block, 1, cache->buf);
+       if (cache->dirty && cache->in_use) {
+               errcode_t retval;
+
+               retval = raw_write_blk(channel, data, cache->block, 1,
+                                      cache->buf, RAW_WRITE_NO_HANDLER);
+               if (retval) {
+                       cache->write_err = 1;
+                       return retval;
+               }
+       }
 
        cache->in_use = 1;
        cache->dirty = 0;
+       cache->write_err = 0;
        cache->block = block;
        cache->access_time = ++data->access_time;
+       return 0;
 }
 
 #define FLUSH_INVALIDATE       0x01
@@ -555,31 +614,66 @@ static errcode_t flush_cached_blocks(io_channel channel,
                                     int flags)
 {
        struct unix_cache       *cache;
-       errcode_t               retval, retval2;
+       errcode_t               retval, retval2 = 0;
        int                     i;
+       int                     errors_found = 0;
 
-       retval2 = 0;
        if ((flags & FLUSH_NOLOCK) == 0)
                mutex_lock(data, CACHE_MTX);
        for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
-               if (!cache->in_use)
+               if (!cache->in_use || !cache->dirty)
                        continue;
-
-               if (flags & FLUSH_INVALIDATE)
-                       cache->in_use = 0;
-
-               if (!cache->dirty)
-                       continue;
-
                retval = raw_write_blk(channel, data,
-                                      cache->block, 1, cache->buf);
-               if (retval)
+                                      cache->block, 1, cache->buf,
+                                      RAW_WRITE_NO_HANDLER);
+               if (retval) {
+                       cache->write_err = 1;
+                       errors_found = 1;
                        retval2 = retval;
-               else
+               } else {
                        cache->dirty = 0;
+                       cache->write_err = 0;
+                       if (flags & FLUSH_INVALIDATE)
+                               cache->in_use = 0;
+               }
        }
        if ((flags & FLUSH_NOLOCK) == 0)
                mutex_unlock(data, CACHE_MTX);
+retry:
+       while (errors_found) {
+               if ((flags & FLUSH_NOLOCK) == 0)
+                       mutex_lock(data, CACHE_MTX);
+               errors_found = 0;
+               for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
+                       if (!cache->in_use || !cache->write_err)
+                               continue;
+                       errors_found = 1;
+                       if (cache->write_err && channel->write_error) {
+                               char *err_buf = NULL;
+                               unsigned long long err_block = cache->block;
+
+                               cache->dirty = 0;
+                               cache->in_use = 0;
+                               cache->write_err = 0;
+                               if (io_channel_alloc_buf(channel, 0,
+                                                        &err_buf))
+                                       err_buf = NULL;
+                               else
+                                       memcpy(err_buf, cache->buf,
+                                              channel->block_size);
+                               mutex_unlock(data, CACHE_MTX);
+                               (channel->write_error)(channel, err_block,
+                                       1, err_buf, channel->block_size, -1,
+                                       retval2);
+                               if (err_buf)
+                                       ext2fs_free_mem(&err_buf);
+                               goto retry;
+                       } else
+                               cache->write_err = 0;
+               }
+               if ((flags & FLUSH_NOLOCK) == 0)
+                       mutex_unlock(data, CACHE_MTX);
+       }
        return retval2;
 }
 #endif /* NO_IO_CACHE */
@@ -822,7 +916,7 @@ static errcode_t unixfd_open(const char *str_fd, int flags,
 #if defined(HAVE_FCNTL)
        fd_flags = fcntl(fd, F_GETFD);
        if (fd_flags == -1)
-               return -EBADF;
+               return EBADF;
 
        flags = 0;
        if (fd_flags & O_RDWR)
@@ -913,8 +1007,11 @@ static errcode_t unix_set_blksize(io_channel channel, int blksize)
                mutex_lock(data, CACHE_MTX);
                mutex_lock(data, BOUNCE_MTX);
 #ifndef NO_IO_CACHE
-               if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK)))
+               if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK))){
+                       mutex_unlock(data, BOUNCE_MTX);
+                       mutex_unlock(data, CACHE_MTX);
                        return retval;
+               }
 #endif
 
                channel->block_size = blksize;
@@ -930,8 +1027,8 @@ static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
                               int count, void *buf)
 {
        struct unix_private_data *data;
-       struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
-       errcode_t       retval = 0;
+       struct unix_cache *cache;
+       errcode_t       retval;
        char            *cp;
        int             i, j;
 
@@ -958,7 +1055,7 @@ static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
        mutex_lock(data, CACHE_MTX);
        while (count > 0) {
                /* If it's in the cache, use it! */
-               if ((cache = find_cached_block(data, block, &reuse[0]))) {
+               if ((cache = find_cached_block(data, block, NULL))) {
 #ifdef DEBUG
                        printf("Using cached block %lu\n", block);
 #endif
@@ -968,46 +1065,59 @@ static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
                        cp += channel->block_size;
                        continue;
                }
-               if (count == 1) {
-                       /*
-                        * Special case where we read directly into the
-                        * cache buffer; important in the O_DIRECT case
-                        */
-                       cache = reuse[0];
-                       reuse_cache(channel, data, cache, block);
-                       if ((retval = raw_read_blk(channel, data, block, 1,
-                                                  cache->buf))) {
-                               cache->in_use = 0;
-                               break;
-                       }
-                       memcpy(cp, cache->buf, channel->block_size);
-                       retval = 0;
-                       break;
-               }
 
                /*
                 * Find the number of uncached blocks so we can do a
                 * single read request
                 */
                for (i=1; i < count; i++)
-                       if (find_cached_block(data, block+i, &reuse[i]))
+                       if (find_cached_block(data, block+i, NULL))
                                break;
 #ifdef DEBUG
                printf("Reading %d blocks starting at %lu\n", i, block);
 #endif
+               mutex_unlock(data, CACHE_MTX);
                if ((retval = raw_read_blk(channel, data, block, i, cp)))
-                       break;
+                       return retval;
+               mutex_lock(data, CACHE_MTX);
 
                /* Save the results in the cache */
                for (j=0; j < i; j++) {
+                       if (!find_cached_block(data, block, &cache)) {
+                               retval = reuse_cache(channel, data,
+                                                    cache, block);
+                               if (retval)
+                                       goto call_write_handler;
+                               memcpy(cache->buf, cp, channel->block_size);
+                       }
                        count--;
-                       cache = reuse[j];
-                       reuse_cache(channel, data, cache, block++);
-                       memcpy(cache->buf, cp, channel->block_size);
+                       block++;
                        cp += channel->block_size;
                }
        }
        mutex_unlock(data, CACHE_MTX);
+       return 0;
+
+call_write_handler:
+       if (cache->write_err && channel->write_error) {
+               char *err_buf = NULL;
+               unsigned long long err_block = cache->block;
+
+               cache->dirty = 0;
+               cache->in_use = 0;
+               cache->write_err = 0;
+               if (io_channel_alloc_buf(channel, 0, &err_buf))
+                       err_buf = NULL;
+               else
+                       memcpy(err_buf, cache->buf, channel->block_size);
+               mutex_unlock(data, CACHE_MTX);
+               (channel->write_error)(channel, err_block, 1, err_buf,
+                                      channel->block_size, -1,
+                                      retval);
+               if (err_buf)
+                       ext2fs_free_mem(&err_buf);
+       } else
+               mutex_unlock(data, CACHE_MTX);
        return retval;
 #endif /* NO_IO_CACHE */
 }
@@ -1032,10 +1142,10 @@ static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
        EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
 
 #ifdef NO_IO_CACHE
-       return raw_write_blk(channel, data, block, count, buf);
+       return raw_write_blk(channel, data, block, count, buf, 0);
 #else
        if (data->flags & IO_FLAG_NOCACHE)
-               return raw_write_blk(channel, data, block, count, buf);
+               return raw_write_blk(channel, data, block, count, buf, 0);
        /*
         * If we're doing an odd-sized write or a very large write,
         * flush out the cache completely and then do a direct write.
@@ -1044,7 +1154,7 @@ static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
                if ((retval = flush_cached_blocks(channel, data,
                                                  FLUSH_INVALIDATE)))
                        return retval;
-               return raw_write_blk(channel, data, block, count, buf);
+               return raw_write_blk(channel, data, block, count, buf, 0);
        }
 
        /*
@@ -1054,15 +1164,19 @@ static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
         */
        writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
        if (writethrough)
-               retval = raw_write_blk(channel, data, block, count, buf);
+               retval = raw_write_blk(channel, data, block, count, buf, 0);
 
        cp = buf;
        mutex_lock(data, CACHE_MTX);
        while (count > 0) {
                cache = find_cached_block(data, block, &reuse);
                if (!cache) {
+                       errcode_t err;
+
                        cache = reuse;
-                       reuse_cache(channel, data, cache, block);
+                       err = reuse_cache(channel, data, cache, block);
+                       if (err)
+                               goto call_write_handler;
                }
                if (cache->buf != cp)
                        memcpy(cache->buf, cp, channel->block_size);
@@ -1073,6 +1187,28 @@ static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
        }
        mutex_unlock(data, CACHE_MTX);
        return retval;
+
+call_write_handler:
+       if (cache->write_err && channel->write_error) {
+               char *err_buf = NULL;
+               unsigned long long err_block = cache->block;
+
+               cache->dirty = 0;
+               cache->in_use = 0;
+               cache->write_err = 0;
+               if (io_channel_alloc_buf(channel, 0, &err_buf))
+                       err_buf = NULL;
+               else
+                       memcpy(err_buf, cache->buf, channel->block_size);
+               mutex_unlock(data, CACHE_MTX);
+               (channel->write_error)(channel, err_block, 1, err_buf,
+                                      channel->block_size, -1,
+                                      retval);
+               if (err_buf)
+                       ext2fs_free_mem(&err_buf);
+       } else
+               mutex_unlock(data, CACHE_MTX);
+       return retval;
 #endif /* NO_IO_CACHE */
 }