[PATCH] btrfs: Fix a C compliance issue

2018-06-15 Thread Bart Van Assche
The C programming language does not allow to use preprocessor statements
inside macro arguments (pr_info() is defined as a macro). Hence rework
the pr_info() statement in btrfs_print_mod_info() such that it becomes
compliant. This patch allows tools like sparse to analyze the BTRFS
source code.

Fixes: 62e855771dac ("btrfs: convert printk(KERN_* to use pr_* calls")
Signed-off-by: Bart Van Assche 
Cc: Jeff Mahoney 
Cc: David Sterba 
---
 fs/btrfs/super.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 81107ad49f3a..dd4980df5b8e 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -2369,7 +2369,7 @@ static __cold void btrfs_interface_exit(void)
 
 static void __init btrfs_print_mod_info(void)
 {
-   pr_info("Btrfs loaded, crc32c=%s"
+   static const char fmt[] = KERN_INFO "Btrfs loaded, crc32c=%s"
 #ifdef CONFIG_BTRFS_DEBUG
", debug=on"
 #endif
@@ -2382,8 +2382,8 @@ static void __init btrfs_print_mod_info(void)
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
", ref-verify=on"
 #endif
-   "\n",
-   crc32c_impl());
+   "\n";
+   printk(fmt, crc32c_impl());
 }
 
 static int __init init_btrfs_fs(void)
-- 
2.17.0

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH] btrfs-progs: detect zoned disks and prevent their raw use

2018-06-15 Thread Luis R. Rodriguez
On Fri, Jun 15, 2018 at 01:59:37PM -0700, Luis R. Rodriguez wrote:
> +static int is_zoned_disk(char *file)
> +{
> + char str[PATH_MAX];
> + char *devname = basename(file);
> + FILE *f;
> + int len;
> +
> + len = snprintf(str, sizeof(str), "/sys/block/%s/queue/zoned", devname);
> +
> + /* Indicates truncation */
> + if (len >= PATH_MAX) {
> + errno = ENAMETOOLONG;
> + return -1;
> + }
> +
> + f = fopen(str, "r");
> + if (!f)
> + return 0;
> +
> + fclose(f);
> +
> + return 1;
> +}
> +

Seems this needs to be extended to ensure this is host-managed only, will spin
a v2 later.

  Luis
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH] btrfs-progs: detect zoned disks and prevent their raw use

2018-06-15 Thread Luis R. Rodriguez
Using raw zoned disks by filesystems requires special handling, only
f2fs currently supports this. All other filesystems do not support
dealing with zoned disks directly.

As such using raw zoned disks is not supported by btrfs-progs, to use them you
need to use dm-zoned-tools, format them with dzadm, set the scheduler to
deadline, and then setup a dmsetup with zoned type, and somehow set
this up on every boot to live a semi-happy life for now.

Even if you use dmsetup on every boot, the zoned disk is still exposed,
and a user may still think they have to run mkfs.btrfs on it instead
of the /dev/mapper/ disk, and then mount it by mistake.

In either case you may seem to believe your disk works and only eventually
end up with alignmet issues and perhaps lose you data. For instance the
below was observed with XFS but its expected btrfs users would see
the same:

[10869.959501] device-mapper: zoned reclaim: (sda): Align zone 865 wp 28349 to 
30842 (wp+2493) blocks failed -5
[10870.014488] sd 0:0:0:0: [sda] tag#0 FAILED Result: hostbyte=DID_OK 
driverbyte=DRIVER_SENSE
[10870.016137] sd 0:0:0:0: [sda] tag#0 Sense Key : Illegal Request [current]
[10870.017696] sd 0:0:0:0: [sda] tag#0 Add. Sense: Unaligned write command

We have to prevent these mistakes by avoiding mkfs use on zoned disks.

Note that this not enough yet, if users are on old AHCI controllers,
the disks may not be detected as zoned. More work through udev may be
required to detect this situation old old parent PCI IDs for zoned
disks, and then prevent their use somehow.

If you are stuck on using btrfs there is a udev rule out there [0], this is
far from perfect, and not fully what we want done upstream on Linux
distributions long term but it should at least help developers for now
enjoy their shiny big fat zoned disks with btrfs.

This check should help avoid having folks shoot themselves in the foot
for now with zoned disks. If you make the mistake to use btrfs
on a zoned disk, you will now get:

 # mkfs.btrfs -f /dev/sda
btrfs-progs v4.17
See http://btrfs.wiki.kernel.org for more information.

ERROR: /dev/sda: zoned disk detected, refer to dm-zoned-tools for how to use 
with btrfs

[0] https://lkml.kernel.org/r/20180614001147.1545-1-mcg...@kernel.org

Cc: Damien Le Moal 
Cc: Bart Van Assche 
Signed-off-by: Luis R. Rodriguez 
---
 mkfs/main.c | 37 +
 1 file changed, 37 insertions(+)

diff --git a/mkfs/main.c b/mkfs/main.c
index b76462a7..165e6a38 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -481,6 +481,30 @@ static int is_ssd(const char *file)
return rotational == '0';
 }
 
+static int is_zoned_disk(char *file)
+{
+   char str[PATH_MAX];
+   char *devname = basename(file);
+   FILE *f;
+   int len;
+
+   len = snprintf(str, sizeof(str), "/sys/block/%s/queue/zoned", devname);
+
+   /* Indicates truncation */
+   if (len >= PATH_MAX) {
+   errno = ENAMETOOLONG;
+   return -1;
+   }
+
+   f = fopen(str, "r");
+   if (!f)
+   return 0;
+
+   fclose(f);
+
+   return 1;
+}
+
 static int _cmp_device_by_id(void *priv, struct list_head *a,
 struct list_head *b)
 {
@@ -912,6 +936,19 @@ int main(int argc, char **argv)
file = argv[optind++];
ssd = is_ssd(file);
 
+   ret = is_zoned_disk(file);
+
+   if (ret < 0) {
+   error("%s: error opening %s\n", file, strerror(errno));
+   goto error;
+   }
+
+   if (ret == 1) {
+   error("%s: zoned disk detected, refer to dm-zoned-tools for "
+ "how to use with btrfs\n", file);
+   goto error;
+   }
+
/*
* Set default profiles according to number of added devices.
* For mixed groups defaults are single/single.
-- 
2.17.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: ntfs -> qemu -> raw-image -> btrfs -> iscsi

2018-06-15 Thread ein
On 06/15/2018 08:10 PM, Austin S. Hemmelgarn wrote:
> On 2018-06-15 13:40, Chris Murphy wrote:
>> On Fri, Jun 15, 2018 at 5:33 AM, ein  wrote:
>>> Hello group,
>>>
>>> does anyone have had any luck with hosting qemu kvm images resided on BTRFS 
>>> filesystem while serving
>>> the volume via iSCSI?
>>>
>>> I encouraged some unidentified problem and I am able to replicate it. 
>>> Basically the NTFS filesystem
>>> inside RAW image gets corrupted every time when Windows guest boots. What 
>>> is weired is that changing
>>> filesystem for ext4 or xfs solves the issue.
>>>
>>> The problem replication looks as follows:
>>> 1) run chkdsk on the guest to make sure the filesystem structure is in good 
>>> shape,
>>> 2) shut down the VM via libvirtd,
>>> 3) rsync changes between source and backup image,
>>> 4) generate SHA1 for backup and original and compare it,
>>> 5) try to run guest on the backup image, I was able to boot windows once 
>>> for ten times, every time
>>> after reboot NTFS' chkdsk finds problems with filesystem and the VM is 
>>> unable to boot again.
>>>
>>> What am I missing?
>>>
>>> VM disk config:
>>>
>>>  
>>>    
>>
>>
>>
>> cache=none uses O_DIRECT, and that's the source of the issue with VM
>> images on Btrfs. Details are in the list archive.
>>
>> I'm not really sure what you want to use with Windows in this
>> particular case, probably not cache=unsafe though. I'd say give
>> writethrough a shot and see how it affects performance and fixes this
>> problem.
>>
> cache=writethrough is probably going to be the best option, unless you want 
> to switch to
> cache=writeback and disable write caching in Windows (which from what I hear 
> can actually give
> better performance than using cache=none).

Thank you to each and everyone of you, I'll give it a shot. Performance is not 
a problem, recovery
purposes only, I'd like to run the VM once to dump some configuration data.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: ntfs -> qemu -> raw-image -> btrfs -> iscsi

2018-06-15 Thread Austin S. Hemmelgarn

On 2018-06-15 13:40, Chris Murphy wrote:

On Fri, Jun 15, 2018 at 5:33 AM, ein  wrote:

Hello group,

does anyone have had any luck with hosting qemu kvm images resided on BTRFS 
filesystem while serving
the volume via iSCSI?

I encouraged some unidentified problem and I am able to replicate it. Basically 
the NTFS filesystem
inside RAW image gets corrupted every time when Windows guest boots. What is 
weired is that changing
filesystem for ext4 or xfs solves the issue.

The problem replication looks as follows:
1) run chkdsk on the guest to make sure the filesystem structure is in good 
shape,
2) shut down the VM via libvirtd,
3) rsync changes between source and backup image,
4) generate SHA1 for backup and original and compare it,
5) try to run guest on the backup image, I was able to boot windows once for 
ten times, every time
after reboot NTFS' chkdsk finds problems with filesystem and the VM is unable 
to boot again.

What am I missing?

VM disk config:

 
   




cache=none uses O_DIRECT, and that's the source of the issue with VM
images on Btrfs. Details are in the list archive.

I'm not really sure what you want to use with Windows in this
particular case, probably not cache=unsafe though. I'd say give
writethrough a shot and see how it affects performance and fixes this
problem.

cache=writethrough is probably going to be the best option, unless you 
want to switch to cache=writeback and disable write caching in Windows 
(which from what I hear can actually give better performance than using 
cache=none).

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: ntfs -> qemu -> raw-image -> btrfs -> iscsi

2018-06-15 Thread Chris Murphy
On Fri, Jun 15, 2018 at 5:33 AM, ein  wrote:
> Hello group,
>
> does anyone have had any luck with hosting qemu kvm images resided on BTRFS 
> filesystem while serving
> the volume via iSCSI?
>
> I encouraged some unidentified problem and I am able to replicate it. 
> Basically the NTFS filesystem
> inside RAW image gets corrupted every time when Windows guest boots. What is 
> weired is that changing
> filesystem for ext4 or xfs solves the issue.
>
> The problem replication looks as follows:
> 1) run chkdsk on the guest to make sure the filesystem structure is in good 
> shape,
> 2) shut down the VM via libvirtd,
> 3) rsync changes between source and backup image,
> 4) generate SHA1 for backup and original and compare it,
> 5) try to run guest on the backup image, I was able to boot windows once for 
> ten times, every time
> after reboot NTFS' chkdsk finds problems with filesystem and the VM is unable 
> to boot again.
>
> What am I missing?
>
> VM disk config:
>
> 
>   



cache=none uses O_DIRECT, and that's the source of the issue with VM
images on Btrfs. Details are in the list archive.

I'm not really sure what you want to use with Windows in this
particular case, probably not cache=unsafe though. I'd say give
writethrough a shot and see how it affects performance and fixes this
problem.





-- 
Chris Murphy
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 2/2] Btrfs: sync log after logging new name

2018-06-15 Thread Filipe Manana
On Fri, Jun 15, 2018 at 4:54 PM, David Sterba  wrote:
> On Mon, Jun 11, 2018 at 07:24:28PM +0100, fdman...@kernel.org wrote:
>> From: Filipe Manana 
>> Fixes: 12fcfd22fe5b ("Btrfs: tree logging unlink/rename fixes")
>> Reported-by: Vijay Chidambaram 
>> Signed-off-by: Filipe Manana 
>
> There are some warnings and possible lock up caused by this patch, the
> 1/2 alone is ok but 1/2 + 2/2 leads to the following warnings. I checked
> twice, the patch base was the pull request ie. without any other 4.18
> stuff.

Are you sure it's this patch?
On top of for-4.18 it didn't cause any problems here, plus the trace
below has nothing to do with renames, hard links or fsync at all -
everything seems stuck on waiting for IO from dev replace.

>
> It's a qemu with 4 cpus and 2g of memory.
>
> btrfs/011
>
> [  876.705586] watchdog: BUG: soft lockup - CPU#2 stuck for 77s!
> [od:12857]
> [  876.708167] Modules linked in: btrfs libcrc32c xor 
> zstd_decompresszstd_compress xxhash raid6_pq loop
> [  876.710717] CPU: 2 PID: 12857 Comm: od Not tainted4.17.0-rc7-default+ #143
> [  876.712007] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996),BIOS 
> 1.0.0-prebuilt.qemu-project.org 04/01/2014
> [  876.71] RIP: 0010:copy_user_generic_string+0x2c/0x40
> [  876.714586] RSP: 0018:a7968344bc68 EFLAGS: 00010246 
> ORIG_RAX:ff13
> [  876.716411] RAX: 55b128b4a370 RBX: 1000 
> RCX:0200
> [  876.718023] RDX:  RSI: 55b128b49370 
> RDI:88bb1b60
> [  876.719681] RBP: 1000 R08: 88bb1b60 
> R09:88bb
> [  876.721568] R10: 1000 R11: 0030 
> R12:1000
> [  876.723123] R13: 88bb1b601000 R14: a7968344be58 
> R15:
> [  876.724087] FS:  7f417c596540() 
> GS:88bb7fd0()knlGS:
> [  876.725165] CS:  0010 DS:  ES:  CR0: 80050033
> [  876.726375] CR2: 55c99ecbf298 CR3: 68436000 
> CR4:06e0
> [  876.728080] Call Trace:
> [  876.728849]  copyin+0x22/0x30
> [  876.729704]  iov_iter_copy_from_user_atomic+0x19a/0x410
> [  876.730789]  ? ptep_clear_flush+0x40/0x40
> [  876.731391]  btrfs_copy_from_user+0xab/0x120 [btrfs]
> [  876.732058]  __btrfs_buffered_write+0x367/0x710 [btrfs]
> [  876.732747]  btrfs_file_write_iter+0x2b8/0x5d0 [btrfs]
> [  876.733507]  ? touch_atime+0x27/0xb0
> [  876.734257]  __vfs_write+0xd4/0x130
> [  876.734860]  vfs_write+0xad/0x1e0
> [  876.735346]  ksys_write+0x42/0x90
> [  876.735858]  do_syscall_64+0x4f/0xe0
> [  876.736515]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
> [  876.737690] RIP: 0033:0x7f417c0a3c94
> [  876.738565] RSP: 002b:7ffc07af7c48 EFLAGS: 0246 
> ORIG_RAX:0001
> [  876.740186] RAX: ffda RBX: 1000 
> RCX:7f417c0a3c94
> [  876.741263] RDX: 1000 RSI: 55b128b49370 
> RDI:0001
> [  876.742819] RBP: 55b128b49370 R08: 7f417c372760 
> R09:
> [  876.744157] R10: 07d0 R11: 0246 
> R12:7f417c372760
> [  876.745507] R13: 1000 R14: 7f417c36d760 
> R15:1000
> [ 1463.260071] INFO: task kworker/u8:12:9261 blocked for more than 480seconds.
> [ 1463.264100]   Tainted: G L4.17.0-rc7-default+#143
> [ 1463.267639] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs"disables 
> this message.
> [ 1463.272086] kworker/u8:12   D0  9261  2 0x8000
> [ 1463.274224] Workqueue: btrfs-submit btrfs_submit_helper [btrfs]
> [ 1463.275442] Call Trace:
> [ 1463.276221]  ? __schedule+0x268/0x7f0
> [ 1463.277348]  schedule+0x33/0x90
> [ 1463.278105]  io_schedule+0x16/0x40
> [ 1463.279053]  wbt_wait+0x19b/0x310
> [ 1463.280085]  ? finish_wait+0x80/0x80
> [ 1463.281153]  blk_mq_make_request+0xba/0x6f0
> [ 1463.282354]  generic_make_request+0x173/0x3d0
> [ 1463.283617]  ? submit_bio+0x6c/0x140
> [ 1463.284678]  submit_bio+0x6c/0x140
> [ 1463.285705]  run_scheduled_bios+0x18e/0x480 [btrfs]
> [ 1463.287121]  ? normal_work_helper+0x6a/0x330 [btrfs]
> [ 1463.288543]  normal_work_helper+0x6a/0x330 [btrfs]
> [ 1463.289937]  process_one_work+0x16d/0x380
> [ 1463.291119]  worker_thread+0x2e/0x380
> [ 1463.292205]  ? process_one_work+0x380/0x380
> [ 1463.293420]  kthread+0x111/0x130
> [ 1463.294377]  ? kthread_create_worker_on_cpu+0x50/0x50
> [ 1463.295782]  ret_from_fork+0x1f/0x30
> [ 1463.296827] INFO: task btrfs-transacti:5799 blocked for more than 480 
> seconds.
> [ 1463.298874]   Tainted: G L4.17.0-rc7-default+ #143
> [ 1463.300596] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables 
> this message.
> [ 1463.302784] btrfs-transacti D0  5799  2 0x8000
> [ 1463.305441] Call Trace:
> [ 1463.306176]  ? __schedule+0x268/0x7f0
> [ 1463.307207]  schedule+0x33/0x90
> [ 1463.308134]  wait_for_commit+0x2c/0x60 [btrfs]
> [ 1463.309359]  ? finish_wait+0x80/0x80
> [ 1463.310389]  

Re: [PATCH 2/2] Btrfs: sync log after logging new name

2018-06-15 Thread David Sterba
On Mon, Jun 11, 2018 at 07:24:28PM +0100, fdman...@kernel.org wrote:
> From: Filipe Manana 
> Fixes: 12fcfd22fe5b ("Btrfs: tree logging unlink/rename fixes")
> Reported-by: Vijay Chidambaram 
> Signed-off-by: Filipe Manana 

There are some warnings and possible lock up caused by this patch, the
1/2 alone is ok but 1/2 + 2/2 leads to the following warnings. I checked
twice, the patch base was the pull request ie. without any other 4.18
stuff.

It's a qemu with 4 cpus and 2g of memory.

btrfs/011

[  876.705586] watchdog: BUG: soft lockup - CPU#2 stuck for 77s!
[od:12857]
[  876.708167] Modules linked in: btrfs libcrc32c xor 
zstd_decompresszstd_compress xxhash raid6_pq loop
[  876.710717] CPU: 2 PID: 12857 Comm: od Not tainted4.17.0-rc7-default+ #143
[  876.712007] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996),BIOS 
1.0.0-prebuilt.qemu-project.org 04/01/2014
[  876.71] RIP: 0010:copy_user_generic_string+0x2c/0x40
[  876.714586] RSP: 0018:a7968344bc68 EFLAGS: 00010246 
ORIG_RAX:ff13
[  876.716411] RAX: 55b128b4a370 RBX: 1000 RCX:0200
[  876.718023] RDX:  RSI: 55b128b49370 RDI:88bb1b60
[  876.719681] RBP: 1000 R08: 88bb1b60 R09:88bb
[  876.721568] R10: 1000 R11: 0030 R12:1000
[  876.723123] R13: 88bb1b601000 R14: a7968344be58 R15:
[  876.724087] FS:  7f417c596540() 
GS:88bb7fd0()knlGS:
[  876.725165] CS:  0010 DS:  ES:  CR0: 80050033
[  876.726375] CR2: 55c99ecbf298 CR3: 68436000 CR4:06e0
[  876.728080] Call Trace:
[  876.728849]  copyin+0x22/0x30
[  876.729704]  iov_iter_copy_from_user_atomic+0x19a/0x410
[  876.730789]  ? ptep_clear_flush+0x40/0x40
[  876.731391]  btrfs_copy_from_user+0xab/0x120 [btrfs]
[  876.732058]  __btrfs_buffered_write+0x367/0x710 [btrfs]
[  876.732747]  btrfs_file_write_iter+0x2b8/0x5d0 [btrfs]
[  876.733507]  ? touch_atime+0x27/0xb0
[  876.734257]  __vfs_write+0xd4/0x130
[  876.734860]  vfs_write+0xad/0x1e0
[  876.735346]  ksys_write+0x42/0x90
[  876.735858]  do_syscall_64+0x4f/0xe0
[  876.736515]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[  876.737690] RIP: 0033:0x7f417c0a3c94
[  876.738565] RSP: 002b:7ffc07af7c48 EFLAGS: 0246 
ORIG_RAX:0001
[  876.740186] RAX: ffda RBX: 1000 RCX:7f417c0a3c94
[  876.741263] RDX: 1000 RSI: 55b128b49370 RDI:0001
[  876.742819] RBP: 55b128b49370 R08: 7f417c372760 R09:
[  876.744157] R10: 07d0 R11: 0246 R12:7f417c372760
[  876.745507] R13: 1000 R14: 7f417c36d760 R15:1000
[ 1463.260071] INFO: task kworker/u8:12:9261 blocked for more than 480seconds.
[ 1463.264100]   Tainted: G L4.17.0-rc7-default+#143
[ 1463.267639] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs"disables this 
message.
[ 1463.272086] kworker/u8:12   D0  9261  2 0x8000
[ 1463.274224] Workqueue: btrfs-submit btrfs_submit_helper [btrfs]
[ 1463.275442] Call Trace:
[ 1463.276221]  ? __schedule+0x268/0x7f0
[ 1463.277348]  schedule+0x33/0x90
[ 1463.278105]  io_schedule+0x16/0x40
[ 1463.279053]  wbt_wait+0x19b/0x310
[ 1463.280085]  ? finish_wait+0x80/0x80
[ 1463.281153]  blk_mq_make_request+0xba/0x6f0
[ 1463.282354]  generic_make_request+0x173/0x3d0
[ 1463.283617]  ? submit_bio+0x6c/0x140
[ 1463.284678]  submit_bio+0x6c/0x140
[ 1463.285705]  run_scheduled_bios+0x18e/0x480 [btrfs]
[ 1463.287121]  ? normal_work_helper+0x6a/0x330 [btrfs]
[ 1463.288543]  normal_work_helper+0x6a/0x330 [btrfs]
[ 1463.289937]  process_one_work+0x16d/0x380
[ 1463.291119]  worker_thread+0x2e/0x380
[ 1463.292205]  ? process_one_work+0x380/0x380
[ 1463.293420]  kthread+0x111/0x130
[ 1463.294377]  ? kthread_create_worker_on_cpu+0x50/0x50
[ 1463.295782]  ret_from_fork+0x1f/0x30
[ 1463.296827] INFO: task btrfs-transacti:5799 blocked for more than 480 
seconds.
[ 1463.298874]   Tainted: G L4.17.0-rc7-default+ #143
[ 1463.300596] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this 
message.
[ 1463.302784] btrfs-transacti D0  5799  2 0x8000
[ 1463.305441] Call Trace:
[ 1463.306176]  ? __schedule+0x268/0x7f0
[ 1463.307207]  schedule+0x33/0x90
[ 1463.308134]  wait_for_commit+0x2c/0x60 [btrfs]
[ 1463.309359]  ? finish_wait+0x80/0x80
[ 1463.310389]  btrfs_commit_transaction+0x18c/0x920 [btrfs]
[ 1463.311852]  ? start_transaction+0xa1/0x3f0 [btrfs]
[ 1463.313209]  transaction_kthread+0x151/0x180 [btrfs]
[ 1463.314541]  ? btrfs_cleanup_transaction+0x4e0/0x4e0 [btrfs]
[ 1463.316024]  kthread+0x111/0x130
[ 1463.316868]  ? kthread_create_worker_on_cpu+0x50/0x50
[ 1463.318134]  ret_from_fork+0x1f/0x30
[ 1463.319116] INFO: task btrfs:12859 blocked for more than 480 seconds.
[ 1463.320402]   Tainted: G L4.17.0-rc7-default+ #143
[ 

Btrfs progs release 4.17

2018-06-15 Thread David Sterba
Hi,

btrfs-progs version 4.17 have been released. There's no change since rc1 and
released one day later becasue Friday is the perfect day for a release.

Changes:

  * check
* many lowmem mode improvements
* properly report qgroup mismatch errors
* check symlinks with append/immutable flags
  * fi usage
* correctly calculate allocated/unallocated for raid10
* minor output updates
  * mkfs
* detect ENOSPC on thinly provisioned devices
* fix spurious EEXIST during directory traversal
  * restore: fix relative path for restore target
  * dump-tree: print symbolic tree names for backrefs
  * send: fix regression preventing send -p with subvolumes mounted on "/"
  * corrupt-tree: refactoring and command line updates
  * build
* make it work with e2fsprogs < 1.42 again
* restore support for autoconf 2.63
* detect if -std=gnu90 is supported
  * other
* new tests
* cleanups

Tarballs: https://www.kernel.org/pub/linux/kernel/people/kdave/btrfs-progs/
Git: git://git.kernel.org/pub/scm/linux/kernel/git/kdave/btrfs-progs.git

Shortlog:

Axel Burri (1):
  btrfs-progs: fix regression preventing send -p with subvolumes mounted on 
"/"

David Sterba (4):
  btrfs-progs: tests: update log markers
  btrfs-progs: tests: fix fsck-tests/031 when on NFS
  btrfs-progs: update CHANGES for v4.17
  Btrfs progs v4.17

Gu JinXiang (1):
  btrfs-progs: Add DEBUG_CFLAGS_INTERNAL for libbtrfsutil

Hans van Kranenburg (1):
  btrfs-progs: fi-usage: fix RAID10 raw disk usage

James Harvey (1):
  btrfs-progs: map-logical: fix spelling of a variable

Jeff Mahoney (3):
  btrfs-progs: convert: fix support for e2fsprogs < 1.42
  btrfs-progs: build: autoconf 2.63 compatibility
  btrfs-progs: build: detect whether -std=gnu90 is supported

Lu Fengqi (3):
  btrfs-progs: qgroup: swap the argument in the caller of 
update_qgroup_relation
  btrfs-progs: tests: check btrfs qgroup parent-child relation output
  btrfs-progs: print-tree: remove dead code from btrfs_print_tree

Matthias Benkard (1):
  btrfs-progs: mkfs: traverse_directory: Reset error code on continue

Misono Tomohiro (5):
  btrfs-progs: fi usage: change warning message more appropriately
  btrfs-progs: fi usage: change to output more info without root privilege
  btrfs-progs: ins: dump-tree: Use %lld for extent data backref offset
  btrfs-progs: ins: dump-tree: Print tree name for extent data/tree block 
backref
  btrfs-progs: check: Initialize all filed of btrfs_inode_item in 
insert_inode_item()

Nikolay Borisov (37):
  btrfs-progs: Remove devid parameter from btrfs_rmap_block
  btrfs-progs: Remove add_cache_extent2
  btrfs-progs: Remove objectid argument from alloc_cache_extent
  btrfs-progs: Use exclude_super_stripes instead of account_super_bytes
  btrfs-progs: check: fix DIR_ITEM checking in lowmem
  btrfs-progs: tests: Add test for collision DIR_ITEM handling
  btrfs-progs: check: Drop ext_ref parameter from find_inode_ref
  btrfs-progs: check: Drop ext_ref param from check_dir_item
  btrfs-progs: check: Drop ext_ref argument from check_inode_item
  btrfs-progs: check: Drop unused ext_ref parameter from process_one_leaf
  btrfs-progs: check: Remove ext_ref param from check_fs_first_inode
  btrfs-progs: check: Remove ext_ref param from walk_down_tree
  btrfs-progs: check: Drop ext_ref param from check_fs_first_inode
  btrfs-progs: check: Drop ext_ref arument from check_fs_root
  btrfs-progs: check: Remove ext_ref local variable from 
check_fs_roots_lowmem
  btrfs-progs: Make __btrfs_fs_incompat return bool
  btrfs-progs: check: Remove root argument from delete_extent_records
  btrfs-progs: check: Remove root parameter from btrfs_fix_block_accounting
  btrfs-progs: check: Remove root parameter from del_pending_extents
  btrfs-progs: check: Remove root argument from finish_current_insert
  btrfs-progs: check: Make update_pinned_extents take btrfs_fs_info
  btrfs-progs: Remove unused argument from clean_tree_block
  btrfs-progs: check: Remove unused root argument from btrfs_extent_post_op
  btrfs-progs: Change btrfs_root to btrfs_fs_info argument in 
btrfs_lookup_extent_info
  btrfs-progs: Remove root argument from btrfs_set_block_flags
  btrfs-progs: Remove root argument from write_one_cache_group
  btrfs-progs: Remove fs_info argument from write_ctree_super
  btrfs-progs: Use symbolic names for read ahead behavior
  btrfs-progs: corrupt-block: Factor out specific-root code
  btrfs-progs: corrupt-block: Correctly handle -r when passing -I
  btrfs-progs: corrupt-block: Factor out key parsing function
  btrfs-progs: corrupt-block: Change -I flag parameter format
  btrfs-progs: corrupt-block: Convert -K flag argument handling to common 
function
  btrfs-progs: corrupt-block: Factor out common "-r" handling code
  

ntfs -> qemu -> raw-image -> btrfs -> iscsi

2018-06-15 Thread ein
Hello group,

does anyone have had any luck with hosting qemu kvm images resided on BTRFS 
filesystem while serving
the volume via iSCSI?

I encouraged some unidentified problem and I am able to replicate it. Basically 
the NTFS filesystem
inside RAW image gets corrupted every time when Windows guest boots. What is 
weired is that changing
filesystem for ext4 or xfs solves the issue.

The problem replication looks as follows:
1) run chkdsk on the guest to make sure the filesystem structure is in good 
shape,
2) shut down the VM via libvirtd,
3) rsync changes between source and backup image,
4) generate SHA1 for backup and original and compare it,
5) try to run guest on the backup image, I was able to boot windows once for 
ten times, every time
after reboot NTFS' chkdsk finds problems with filesystem and the VM is unable 
to boot again.

What am I missing?

VM disk config:

    
  
  
  
  
  
    

Initiator side:

root@initiator:~# btrfs version
btrfs-progs v4.13.3
root@initiator:~# uname -a
Linux initiator 4.15.0-0.bpo.2-amd64 #1 SMP Debian 4.15.11-1~bpo9+1 
(2018-04-07) x86_64 GNU/Linux
root@initiator:~# btrfs version
btrfs-progs v4.13.3
root@initiator:~# cat /etc/debian_version
9.4
root@initiator:~# iscsid -v
iscsid version 2.0-874

BTRFS mount options:
_netdev,noatime,nodiratime,compress=lzo

Target side:

root@target:~# dpkg -l | grep iscsi
ii  iscsitarget 1.4.20.2-10.1  armel    
iSCSI Enterprise
Target userland tools
root@target:~# uname -a
Linux target 2.6.31.8 Mon Nov 23 04:30:20 CET 2015 v0.0.9 Mon Nov 23 04:30:20 
CET 2015 armv5tel
GNU/Linux

PS. There's nothing interesting in dmsg, besides (target side):
iscsi_trgt: scsi_cmnd_start(1045) Unsupported 85
iscsi_trgt: cmnd_skip_pdu(459) 5e00 1c 85 0
Which seems to be harmless and is probably generated by smartd.




--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 0/6] Freespace tree repair support

2018-06-15 Thread Nikolay Borisov
Hello, 

This patchset adds support to btrfs check to repair corrupted freespace tree. 
Once this has been merged then we can start thinking of making the freespace
tree the default freespace cache and deprecate the existing v1 cache in new 
filesystems. 

This series depend on the already sent delayed refs. In order to facilitate 
testing the 2 series have been integrated and posted at :

   https://github.com/lorddoskias/btrfs-progs.git dev/delayed-refs-fst

The main thing with this code is that the repair will always regenerate the 
FST with extents since it I haven't ported the code which deals with 
detecting that bitmaps are more space efficient. This is not a big deal since 
the kernel will do it.

The first patch is here for completeness' sake and was only used in a previous 
incarnation of this code but since I've re-worked the way the code is supposed 
to work it can be dropped. I don't have strong opinions either way. It just 
teaches btrfs_read_fs_root to return the freespace root

Patch 2 adds some low-level bit manipulation primitives which are necessary to 
support bitmap based FST. My initial idea was to completely ommit the bitmap 
support but in testing it turned out to be less pain to just include it from 
the get-go. Otherwise the filesystem created in the test case in patch 6 
couldn't be worked on. 

Patch 3 is mostly copy/paste from the kernel code bringin in necessary 
functions to repair the freespace tree. I've only omitted locking code since 
in userspace we are always single-threaded, also there is no support currently 
when regenerating FST to convert it to bitmap.

Patch 4 updates the compat_ro bitmask to allow reading an FST filesystem in 
repair mode.

Patch 5 adds the core code which utilizes everything added up until this point. 
It's not that big and should be fairly easy to review. 

Patch 6 is a test-case which creates a filesystem with FST enabled, populates 
it 
with some files and then corrupts a bitmap and an extent records and tries to 
repair them. It's passing for me. 


Nikolay Borisov (6):
  btrfs-progs: Add support for freespace tree in btrfs_read_fs_root
  btrfs-progs: Add extent buffer bitmap manipulation infrastructure
  btrfs-progs: Pull free space tree related code from kernel
  btrfs-progs: Add freespace tree as compat_ro supported feature
  btrfs-progs: check: Add support for freespace tree fixing
  btrfs-progs: tests: Test for FST corruption detection/repair

 check/main.c  |  61 +-
 ctree.c   |  77 ++
 ctree.h   |   8 +-
 disk-io.c |   3 +
 extent-tree.c |   9 +
 extent_io.c   |  39 +
 extent_io.h   |  15 +
 free-space-tree.c | 893 +-
 free-space-tree.h |  10 +-
 tests/fsck-tests/035-freespacetree-repair/test.sh |  79 ++
 10 files changed, 1163 insertions(+), 31 deletions(-)
 create mode 100755 tests/fsck-tests/035-freespacetree-repair/test.sh

-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 1/6] btrfs-progs: Add support for freespace tree in btrfs_read_fs_root

2018-06-15 Thread Nikolay Borisov
For completeness sake add code to btrfs_read_fs_root so that it can
handle the freespace tree.

Signed-off-by: Nikolay Borisov 
---
 disk-io.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/disk-io.c b/disk-io.c
index 8da6e3ce5fc8..9ad826b83b3e 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -664,6 +664,9 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info 
*fs_info,
if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
return fs_info->quota_enabled ? fs_info->quota_root :
ERR_PTR(-ENOENT);
+   if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
+return fs_info->free_space_root ? fs_info->free_space_root :
+  ERR_PTR(-ENOENT);
 
BUG_ON(location->objectid == BTRFS_TREE_RELOC_OBJECTID ||
   location->offset != (u64)-1);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 5/6] btrfs-progs: check: Add support for freespace tree fixing

2018-06-15 Thread Nikolay Borisov
Now that all the prerequisite code for proper support of free space
tree repair is in, it's time to wire it in. This is achieved by first
hooking the freespace tree to the __free_extent/alloc_reserved_tree_block
functions. And then introducing a wrapper function to contains the
existing check_space_cache and the newly introduced repair code.
Finally, it's important to note that FST repair code first clears the
existing FST in case of any problem found and rebuilds it from scratch.

Signed-off-by: Nikolay Borisov 
---
 check/main.c  | 61 +--
 extent-tree.c |  9 +
 2 files changed, 47 insertions(+), 23 deletions(-)

diff --git a/check/main.c b/check/main.c
index 3a5efaf615a9..44d734ff4254 100644
--- a/check/main.c
+++ b/check/main.c
@@ -5321,19 +5321,6 @@ static int check_space_cache(struct btrfs_root *root)
int ret;
int error = 0;
 
-   if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
-   btrfs_super_generation(root->fs_info->super_copy) !=
-   btrfs_super_cache_generation(root->fs_info->super_copy)) {
-   printf("cache and super generation don't match, space cache "
-  "will be invalidated\n");
-   return 0;
-   }
-
-   if (ctx.progress_enabled) {
-   ctx.tp = TASK_FREE_SPACE;
-   task_start(ctx.info);
-   }
-
while (1) {
cache = btrfs_lookup_first_block_group(root->fs_info, start);
if (!cache)
@@ -5383,11 +5370,11 @@ static int check_space_cache(struct btrfs_root *root)
}
}
 
-   task_stop(ctx.info);
 
return error ? -EINVAL : 0;
 }
 
+
 /*
  * Check data checksum for [@bytenr, @bytenr + @num_bytes).
  *
@@ -9338,7 +9325,6 @@ static int do_clear_free_space_cache(struct btrfs_fs_info 
*fs_info,
ret = 1;
goto close_out;
}
-   printf("Clearing free space cache\n");
ret = clear_free_space_cache(fs_info);
if (ret) {
error("failed to clear free space cache");
@@ -9365,6 +9351,41 @@ static int do_clear_free_space_cache(struct 
btrfs_fs_info *fs_info,
return ret;
 }
 
+static int validate_free_space_cache(struct btrfs_root *root)
+{
+
+   int ret;
+
+   if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
+   btrfs_super_generation(root->fs_info->super_copy) !=
+   btrfs_super_cache_generation(root->fs_info->super_copy)) {
+   printf("cache and super generation don't match, space cache "
+  "will be invalidated\n");
+   return 0;
+   }
+
+   if (ctx.progress_enabled) {
+   ctx.tp = TASK_FREE_SPACE;
+   task_start(ctx.info);
+   }
+
+   ret = check_space_cache(root);
+   if (ret && btrfs_fs_compat_ro(global_info, FREE_SPACE_TREE)
+   && repair) {
+   ret = do_clear_free_space_cache(global_info, 2);
+   if (ret)
+   goto out;
+
+   ret = btrfs_create_free_space_tree(global_info);
+   if (ret)
+   error("couldn't repair freespace tree");
+   }
+
+out:
+   task_stop(ctx.info);
+   return ret ? -EINVAL : 0;
+}
+
 const char * const cmd_check_usage[] = {
"btrfs check [options] ",
"Check structural integrity of a filesystem (unmounted).",
@@ -9768,15 +9789,9 @@ int cmd_check(int argc, char **argv)
else
fprintf(stderr, "checking free space cache\n");
}
-   ret = check_space_cache(root);
+
+   ret = validate_free_space_cache(root);
err |= !!ret;
-   if (ret) {
-   if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
-   error("errors found in free space tree");
-   else
-   error("errors found in free space cache");
-   goto out;
-   }
 
/*
 * We used to have to have these hole extents in between our real
diff --git a/extent-tree.c b/extent-tree.c
index b9d51b388c9a..40117f81352e 100644
--- a/extent-tree.c
+++ b/extent-tree.c
@@ -29,6 +29,7 @@
 #include "crc32c.h"
 #include "volumes.h"
 #include "free-space-cache.h"
+#include "free-space-tree.h"
 #include "utils.h"
 
 #define PENDING_EXTENT_INSERT 0
@@ -2292,6 +2293,11 @@ static int __free_extent(struct btrfs_trans_handle 
*trans,
BUG_ON(ret);
}
 
+   ret = add_to_free_space_tree(trans, bytenr, num_bytes);
+   if (ret) {
+   goto fail;
+   }
+
update_block_group(trans->fs_info, bytenr, num_bytes, 0,
   mark_free);
}
@@ -2630,6 +2636,9 @@ static int alloc_reserved_tree_block(struct 
btrfs_trans_handle 

[PATCH 6/6] btrfs-progs: tests: Test for FST corruption detection/repair

2018-06-15 Thread Nikolay Borisov
Simple test case which preps a filesystem, then corrupts the FST and
finally repairs it. Tests both extent based and bitmap based FSTs.

Signed-off-by: Nikolay Borisov 
---
 tests/fsck-tests/035-freespacetree-repair/test.sh | 79 +++
 1 file changed, 79 insertions(+)
 create mode 100755 tests/fsck-tests/035-freespacetree-repair/test.sh

diff --git a/tests/fsck-tests/035-freespacetree-repair/test.sh 
b/tests/fsck-tests/035-freespacetree-repair/test.sh
new file mode 100755
index ..e19d997b3d6a
--- /dev/null
+++ b/tests/fsck-tests/035-freespacetree-repair/test.sh
@@ -0,0 +1,79 @@
+#!/bin/bash
+# Corrupt a filesystem that is using freespace tree and then ensure that 
+# btrfs check is able to repair it. This tests correct detection/repair of 
+# both a FREE_SPACE_EXTENT based FST and a FREE_SPACE_BITMAP based FST. 
+
+source "$TEST_TOP/common"
+
+repair_and_verify()
+{
+   # since repairing entails allocating a block, which in turn implies 
+   # FST modification another btrfs check is required to ensure that 
+   # FST modification logic is correct. 
+   run_check $SUDO_HELPER "$TOP/btrfs" check --repair "$TEST_DEV"
+   run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV"
+}
+
+# wrapper for btrfs-corrupt-item
+# $1: Type of item we want to corrupt - extent or bitmap
+corrupt_fst_item()
+{
+   local type
+   local objectid
+   local offset
+   type="$1"
+
+   if [[ $type == "bitmap" ]]; then 
+   type=200
+   objectid=$("$TOP/btrfs" inspect-internal dump-tree -t 10 
"$TEST_DEV" | \
+   grep -o "[[:digit:]]* FREE_SPACE_BITMAP [[:digit:]]*" | 
\
+   cut -d' ' -f1 | tail -2 | head -1)
+   offset=$("$TOP/btrfs" inspect-internal dump-tree -t 10 
"$TEST_DEV" | \
+   grep -o "[[:digit:]]* FREE_SPACE_BITMAP [[:digit:]]*" | 
\
+   cut -d' ' -f3 |tail -2 | head -1)
+   echo "Corrupting $objectid,FREE_SPACE_BITMAP,$offset"
+   elif [[ $type == "extent" ]]; then
+   type=199
+   objectid=$("$TOP/btrfs" inspect-internal dump-tree -t 10 
"$TEST_DEV" | \
+   grep -o "[[:digit:]]* FREE_SPACE_EXTENT [[:digit:]]*" | 
\
+   cut -d' ' -f1 | tail -2 | head -1)
+   offset=$("$TOP/btrfs" inspect-internal dump-tree -t 10 
"$TEST_DEV" | \
+   grep -o "[[:digit:]]* FREE_SPACE_EXTENT [[:digit:]]*" | 
\
+   cut -d' ' -f3 | tail -2 | head -1)
+   echo "Corrupting $objectid,FREE_SPACE_EXTENT,$offset"
+   else
+   _fail "Unknown item type for corruption"
+   fi
+   
+
+   run_check "$TOP/btrfs-corrupt-block" -r 10 -K "$objectid,$type,$offset" 
\
+   -f offset "$TEST_DEV"
+}
+
+check_prereq btrfs
+check_prereq mkfs.btrfs
+check_global_prereq grep
+check_global_prereq tail 
+check_global_prereq head
+check_global_prereq cut
+
+setup_root_helper
+prepare_test_dev 256M
+
+run_check "$TOP/mkfs.btrfs" -n 4k -f "$TEST_DEV"
+run_check_mount_test_dev -oclear_cache,space_cache=v2
+
+#create files which will populate the FST
+for i in {1..3000}; do
+   fallocate -l 4k "$TEST_MNT/file.$i" 
+done
+
+run_check_umount_test_dev
+
+#now corrupt one of the bitmap items 
+corrupt_fst_item "bitmap"
+check_image "$TEST_DEV"
+
+#now corrupt an extent 
+corrupt_fst_item "extent"
+check_image "$TEST_DEV"
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 4/6] btrfs-progs: Add freespace tree as compat_ro supported feature

2018-06-15 Thread Nikolay Borisov
The RO_FREE_SPACE_TREE(_VALID) flags are required in order to be able
to open an FST filesystem in repair mode. Add them to
BTRFS_FEATURE_COMPAT_RO_SUPP.

Signed-off-by: Nikolay Borisov 
---
 ctree.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/ctree.h b/ctree.h
index ade883fecbd6..ef05e8122982 100644
--- a/ctree.h
+++ b/ctree.h
@@ -497,7 +497,9 @@ struct btrfs_super_block {
  * added here until read-write support for the free space tree is implemented 
in
  * btrfs-progs.
  */
-#define BTRFS_FEATURE_COMPAT_RO_SUPP   0ULL
+#define BTRFS_FEATURE_COMPAT_RO_SUPP   \
+   (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |  \
+BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID)
 
 #define BTRFS_FEATURE_INCOMPAT_SUPP\
(BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 3/6] btrfs-progs: Pull free space tree related code from kernel

2018-06-15 Thread Nikolay Borisov
To help implement free space tree checker in user space some kernel
function are necessary, namely iterating/deleting/adding freespace
items, some internal search functions. Functions to populate a block
group based on the extent tree. The code is largely copy/paste from
the kernel with locking eliminated (i.e free_space_lock). It supports
reading/writing of both bitmap and extent based FST trees.

Signed-off-by: Nikolay Borisov 
---
 ctree.c   |  77 +
 ctree.h   |   4 +
 free-space-tree.c | 893 +-
 free-space-tree.h |  10 +-
 4 files changed, 977 insertions(+), 7 deletions(-)

diff --git a/ctree.c b/ctree.c
index d8a6883aa85f..aa1568620205 100644
--- a/ctree.c
+++ b/ctree.c
@@ -1226,6 +1226,83 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, 
struct btrfs_root
 }
 
 /*
+ * helper to use instead of search slot if no exact match is needed but
+ * instead the next or previous item should be returned.
+ * When find_higher is true, the next higher item is returned, the next lower
+ * otherwise.
+ * When return_any and find_higher are both true, and no higher item is found,
+ * return the next lower instead.
+ * When return_any is true and find_higher is false, and no lower item is 
found,
+ * return the next higher instead.
+ * It returns 0 if any item is found, 1 if none is found (tree empty), and
+ * < 0 on error
+ */
+int btrfs_search_slot_for_read(struct btrfs_root *root,
+   const struct btrfs_key *key,
+   struct btrfs_path *p, int find_higher,
+   int return_any)
+{
+int ret;
+struct extent_buffer *leaf;
+
+again:
+ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
+if (ret <= 0)
+return ret;
+/*
+ * a return value of 1 means the path is at the position where the
+ * item should be inserted. Normally this is the next bigger item,
+ * but in case the previous item is the last in a leaf, path points
+ * to the first free slot in the previous leaf, i.e. at an invalid
+ * item.
+ */
+leaf = p->nodes[0];
+
+if (find_higher) {
+if (p->slots[0] >= btrfs_header_nritems(leaf)) {
+ret = btrfs_next_leaf(root, p);
+if (ret <= 0)
+return ret;
+if (!return_any)
+return 1;
+/*
+ * no higher item found, return the next
+ * lower instead
+ */
+return_any = 0;
+find_higher = 0;
+btrfs_release_path(p);
+goto again;
+}
+} else {
+if (p->slots[0] == 0) {
+ret = btrfs_prev_leaf(root, p);
+if (ret < 0)
+return ret;
+if (!ret) {
+leaf = p->nodes[0];
+if (p->slots[0] == btrfs_header_nritems(leaf))
+p->slots[0]--;
+return 0;
+}
+if (!return_any)
+return 1;
+/*
+ * no lower item found, return the next
+ * higher instead
+ */
+return_any = 0;
+find_higher = 1;
+btrfs_release_path(p);
+goto again;
+} else {
+--p->slots[0];
+}
+}
+return 0;
+}
+
+/*
  * adjust the pointers going up the tree, starting at level
  * making sure the right key of each node is points to 'key'.
  * This is used after shifting pointers to the left, so it stops
diff --git a/ctree.h b/ctree.h
index 3e9ca2ca8432..ade883fecbd6 100644
--- a/ctree.h
+++ b/ctree.h
@@ -2619,6 +2619,10 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,
 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  *root, struct btrfs_key *key, struct btrfs_path *p, int
  ins_len, int cow);
+int btrfs_search_slot_for_read(struct btrfs_root *root,
+   const struct btrfs_key *key,
+   struct btrfs_path *p, int find_higher,
+   int return_any);
 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
u64 iobjectid, u64 ioff, u8 key_type,
struct btrfs_key *found_key);
diff --git a/free-space-tree.c b/free-space-tree.c
index 139a031e8483..6acb71378110 100644
--- 

[PATCH 2/6] btrfs-progs: Add extent buffer bitmap manipulation infrastructure

2018-06-15 Thread Nikolay Borisov
Those functions are in preparation for adding the freespace tree
repair code since it needs to be able to deal with bitmap based fsts.
This patch adds extent_buffer_bitmap_set and extent_buffer_bitmap_clear
functions. Since in userspace we don't have to deal with page mappings
their implementation is vastly simplified by simply setting each bit in
the passed range.

Signed-off-by: Nikolay Borisov 
---
 extent_io.c | 39 +++
 extent_io.h | 15 +++
 2 files changed, 54 insertions(+)

diff --git a/extent_io.c b/extent_io.c
index 198492699438..568a12f7084b 100644
--- a/extent_io.c
+++ b/extent_io.c
@@ -204,6 +204,45 @@ static int clear_state_bit(struct extent_io_tree *tree,
return ret;
 }
 
+/**
+ * extent_buffer_bitmap_set - set an area of a bitmap
+ * @eb: the extent buffer
+ * @start: offset of the bitmap item in the extent buffer
+ * @pos: bit number of the first bit
+ * @len: number of bits to set
+ */
+void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
+  unsigned long pos, unsigned long len)
+{
+u8 *kaddr = (u8 *)eb->data + start;
+
+while (len) {
+   le_set_bit(pos, kaddr);
+   pos++;
+   len--;
+}
+}
+
+
+/**
+ * extent_buffer_bitmap_clear - clear an area of a bitmap
+ * @eb: the extent buffer
+ * @start: offset of the bitmap item in the extent buffer
+ * @pos: bit number of the first bit
+ * @len: number of bits to clear
+ */
+void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
+unsigned long pos, unsigned long len)
+{
+u8 *kaddr = (u8 *)eb->data + start;
+
+while (len) {
+   le_clear_bit(pos, kaddr);
+   pos++;
+   len--;
+}
+}
+
 /*
  * clear some bits on a range in the tree.
  */
diff --git a/extent_io.h b/extent_io.h
index d407d93d617e..f9097911f5ef 100644
--- a/extent_io.h
+++ b/extent_io.h
@@ -68,6 +68,17 @@ static inline int le_test_bit(int nr, const u8 *addr)
return 1U & (addr[BIT_BYTE(nr)] >> (nr & (BITS_PER_BYTE-1)));
 }
 
+
+static inline void le_set_bit(int nr, u8 *addr)
+{
+   addr[BIT_BYTE(nr)] |= (1U << (nr & (BITS_PER_BYTE-1)));
+}
+
+static inline void le_clear_bit(int nr, u8 *addr)
+{
+   addr[BIT_BYTE(nr)] &= ~(1U << (nr & (BITS_PER_BYTE-1)));
+}
+
 struct btrfs_fs_info;
 
 struct extent_io_tree {
@@ -175,4 +186,8 @@ int read_data_from_disk(struct btrfs_fs_info *info, void 
*buf, u64 offset,
u64 bytes, int mirror);
 int write_data_to_disk(struct btrfs_fs_info *info, void *buf, u64 offset,
   u64 bytes, int mirror);
+void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
+unsigned long pos, unsigned long len);
+void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
+  unsigned long pos, unsigned long len);
 #endif
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html