5

The write() call is failing with errno = 28 (ENOSPC), no space left on the device. I am trying to handle this error in the following way. When the disk is full, I am doing lseek() to move the file pointer to the beginning of the file.

I believe that now the write() should not fail, since now the file will be overwritten from the top (File will not exapnd). But still the write() call is failing with the same error. Please explain this behaviour.

  if(errno == ENOSPC)
  {
      curPos = lseek(gi4LogFd, 0, SEEK_SET);
      break;
  }
8
  • Does that free up any space?
    – devnull
    Commented Jul 29, 2013 at 7:09
  • Had any data ever been written to offset 0?
    – alk
    Commented Jul 29, 2013 at 7:13
  • No, it does not free up space. But what i am trying to say is that even though the space is full, I am not trying to expand the file (since the file pointer is at the begging of the file). It is just like overwriting the same file from the top. Why it should need more space? Commented Jul 29, 2013 at 7:13
  • @alk When I call write after setting offest = 0, write() fails again saying no space. Commented Jul 29, 2013 at 7:15
  • This does not answer my question, sry.
    – alk
    Commented Jul 29, 2013 at 7:17

2 Answers 2

8

Just because you write to the beginning of the file doesn't mean that the file system will be writing to the same space on the disk or that the space at the beginning of the file is allocated at all.

You could have a hole in the file, in which case the write will fail anyway. Holes are optimizations that many filesystems do where they pretend that a piece of the file is there while it's actually just lots of zeros, so those parts never get written to disk, it's just bookkeeping saying that a particular part of the file is empty.

You could have overcommitted data to your filesystem (many filesystems don't actually allocate space on the disk until the data is flushed from the buffer cache which could be several seconds, if not minutes after the write is done), in which case the write will fail anyway. The ENOSPC you're getting could actually be because you already filled your filesystem to over 100% of the capacity and the filesystem code didn't discover it until it tried to flush a write you did a while ago.

You could be on a logging/journaling filesystem where the actual block allocation doesn't happen until the log is flushed, in which case the write will fail. Same logic as the buffer cache situation.

You could have run out of some particular preallocated metadata on the filesystem and it will fail with ENOSPC even though it's not even nearly full. This is not nearly as common today as in the past.

Your disk might have discovered that some part of it went bad and told the filesystem to not use those blocks and that took up space.

In short, there's no guarantee that the filesystem will behave like we could naively think it does once it's full. There are other reasons besides this to never fill a filesystem above 95%. Almost all filesystems are notoriously nondeterministic when nearly full.

4
  • What do you mean by "that the space at the beginning of the file is allocated at all." ? Commented Jul 29, 2013 at 7:35
  • @SumitTrehan I mean that just because you can read the file doesn't mean that all parts of it have been placed anywhere on the disk yet (or that they will ever be).
    – Art
    Commented Jul 29, 2013 at 7:38
  • @Art As per my undertsanding the data is stored in the disk in the disk blocks. So there must be a disk block which will have the beggining of the file. So when i will write at the fp with SEEK_SET offset =0, it should write at the same disk block. Why will the filesystem allocate a new block? Commented Jul 29, 2013 at 7:43
  • @SumitTrehan First, "there must be a disk block" is incorrect. There doesn't have to be a block allocated yet. All your file could still be in the buffer cache or the journal. Second, there could be dozens of different reasons the same block won't be reused. All depending on what filesystem you're using. Deduplication, compression, consolidation of fragments, data versioning, inability to write to the block safely because of operation ordering, etc. Once your filesystem is full or close to full all bets are off.
    – Art
    Commented Jul 29, 2013 at 7:59
2

Just because you are seeking to the beginning of the file doesn't mean that the file is truncated. It's possible to do random writes on a file.

Writing down a block on a full file system is going to cause issues. If you want to truncate the file use truncate(2) or ftruncate call on the file BEFORE lseek

try:

    if(errno == ENOSPC) {
        ftruncate(gi4LogFd, 0);
        lseek(gi4LogFd, 0, SEEK_SET);
        break;
    }

Okay so ext3 filesystem with journaling support does not create an issue on a full fs:

Setup:

Create an image file:

   dd if=/dev/zero of=/tmp/img.dsk count=8192

Created an ext3 filesystem on a 4k image file:

mkfs.ext3 /tmp/img.dsk
sudo mount /tmp/img.dsk /mnt/internal
sudo chown masud.users /mnt/internal

touch /mnt/internal/file.bin 

sudo dd if=/dev/urandom of=/mnt/internal/file.bin

here sudo is necessary for dd to make sure that the reserve for superuser is filled up.

so now :

df /mnt/internal/ shows:

/dev/loop/0         3963  3963         0 100% /mnt/internal

Using the following code:

#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

char buf[8192];

int main(int argc, char *argv[])
{

    int rv;
    char *filename;

    if ( argc < 2 ) {
         fprintf(stderr, "provide the filename\n");
          return -1;
    }

    filename = argv[1];
    int rd = open("/dev/urandom", O_RDONLY);
    read(rd, buf, sizeof(buf));
    close(rd);

    int fd = open(filename, O_SYNC|O_RDWR);

    lseek(fd, -sizeof(buf), SEEK_END);
    rv = write(fd, buf, sizeof(buf));

    if ( rv < 0 ) {
        perror(filename);
        goto out;
    }
    lseek(fd, 0, SEEK_SET);

    rv = write(fd, "foo", 3);
    if ( rv < 0 ) {
       perror(filename);
    }
out:

   close(fd);
   return rv;


}

Now: ./foo /mnt/internal/file.bin

Succeeds.

So question is how is this different from your environment?

9
  • Still one should be able to write anywhere in an existing file without truncating it.
    – phoxis
    Commented Jul 29, 2013 at 7:14
  • @phoxis Apparently is he able to write anywhere, it's just that their is no space left: therefore it cannot append anything, event at the beginning of the file, and then fail.
    – Xaqq
    Commented Jul 29, 2013 at 7:16
  • Depends on how the underlying filesystem is treating a newly written block. There is no guarantee that the original inode will be recycled,, it could very-well be that the original inode is marked for cleanup and a new one is written for ensuring journaling or some other bizarre optimization. Also it's possible that other processes are also writing to the filesystem and the super-user filesystem reserve is being calculated using a rough rounding scheme. Commented Jul 29, 2013 at 7:18
  • When the no space is left in a disk, we won't be able to add new data to the file, ie. appending. But still we should be able to overwrite existing data, which can be done by seeking into any location within existing data of the file and overwriting it.
    – phoxis
    Commented Jul 29, 2013 at 7:18
  • @Xaqq I am not trying to append. I have moved the fp to the beginning, dont u think this is a kind of overwrite, not append. Commented Jul 29, 2013 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.