Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util.h - unmap region of block device #62

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion doc/tgtadm.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,14 @@ tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 1 \
This parameter only applies to DISK devices.
</para>
<para>
Thin-provisioning only works for LUNs stored on filesystems
Thin-provisioning works for LUNs stored on filesystems
that support FALLOC_FL_PUNCH_HOLE.
</para>
<para>
When using thin-provisioning option with a block device such a
SSD or ZVOL, UNMAP SCSI command discards the region from
the block device.
</para>
</listitem>
</varlistentry>

Expand Down
24 changes: 20 additions & 4 deletions usr/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <string.h>
#include <limits.h>
#include <linux/types.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "be_byteshift.h"

Expand Down Expand Up @@ -208,15 +212,27 @@ void concat_buf_release(struct concat_buf *b);


/* If we have recent enough glibc to support PUNCH HOLE we try to unmap
* the region.
* the region of file.
* If supported BLKDISCARD, try to unmap the region of block device.
*/
static inline int unmap_file_region(int fd, off_t offset, off_t length)
{
struct stat64 st;
if (fstat64(fd, &st) < 0)
return -1;
if (S_ISREG(st.st_mode)) {
#ifdef FALLOC_FL_PUNCH_HOLE
if (fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
offset, length) == 0)
return 0;
if (fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
offset, length) == 0)
return 0;
#endif
} else if (S_ISBLK(st.st_mode)) {
#ifdef BLKDISCARD
uint64_t range[] = { offset, length };
if (ioctl(fd, BLKDISCARD, &range) == 0)
return 0;
#endif
}
return -1;
}

Expand Down