[PATCH] D39464: Define fs::allocate_file which preallocates disk blocks.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 13 15:18:50 PST 2017


Kamil Rytarowski <n54 at gmx.com> writes:

> On 13.11.2017 19:21, Rafael Avila de Espindola wrote:
>> Kamil Rytarowski <n54 at gmx.com> writes:
>> 
>>> On 13.11.2017 18:19, Rafael Avila de Espindola wrote:
>>>> Kamil Rytarowski via Phabricator <reviews at reviews.llvm.org> writes:
>>>>
>>>>> krytarowski added inline comments.
>>>>>
>>>>>
>>>>> ================
>>>>> Comment at: llvm/lib/Support/Unix/Path.inc:436
>>>>>        return std::error_code(Err, std::generic_category());
>>>>> +    return make_error_code(errc::function_not_supported);
>>>>>    }
>>>>> ----------------
>>>>> NetBSD needs `ftruncate`(2) as a fallback for `posix_fallocate`(2).
>>>>>
>>>>> In the default setup `posix_fallocate`() returns EOPNOTSUPP.
>>>>
>>>> Will ftruncate allocate space on netbsd? Note that it is OK for
>>>> allocate_file to fail. The user then has to use a buffer and write(2)
>>>> instead of mmap for output.
>>>>
>>>> Cheers,
>>>> Rafael
>>>>
>>>
>>> ftruncate(2)/NetBSD as an extension extends area and makes it
>>> zero-filled. It's equivalent to lseek(2) + write(2).
>> 
>> Cool, so an mmap of the file after a ftruncate will never crash because
>> of disk full, correct?
>> 
>> If so we could use use ftruncate on netbsd as a special case, similar to
>> the special case we have for OS X.
>> 
>> Cheers,
>> Rafael
>> 
>
> I'm not that sure. Could you please provide a reproducer of a problem to
> double check?
>
> I was trying to create a program that takes the whole space with
> ftruncate(2) and mmap(2) over it works. I cannot take more space than
> available, as I'm getting ENOSPC during ftruncate(2).

Try the following program on a fs with less than 1gb free:

------------------------------------
  #include <assert.h>
  #include <fcntl.h>
  #include <string.h>
  #include <sys/mman.h>
  #include <sys/stat.h>
  #include <sys/types.h>
  #include <unistd.h>

  int main(void) {
    int fd = open("foobar", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    assert(fd != -1);
    size_t size = 1 << 30;
    int r = ftruncate(fd, size);
    assert(r == 0);
    void *addr = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
    assert(addr != MAP_FAILED);
    memset(addr, 42, size);
    r = munmap(addr, size);
    assert(r == 0);
    r = close(fd);
    assert(r == 0);
    return 0;
  }
------------------------------------

Cheers,
Rafael


More information about the llvm-commits mailing list