]> git.hungrycats.org Git - linux/log
linux
11 years agobtrfs: remove unused extent state bits
David Sterba [Tue, 29 Jul 2014 22:03:56 +0000 (00:03 +0200)]
btrfs: remove unused extent state bits

The last users are long gone.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 01d5bc3789f8464abd13cc44e3cd6df9d17f2802)

11 years agoBtrfs: set default max_inline to 8KiB instead of 8MiB
Filipe David Borba Manana [Thu, 8 Aug 2013 21:45:48 +0000 (22:45 +0100)]
Btrfs: set default max_inline to 8KiB instead of 8MiB

8MiB is way too large and likely set by mistake. This is not
a significant issue as in practice the max amount of data
added to an inline extent is also limited by the page cache
and btree leaf sizes.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 95ac567af212db3293af3897ccb521efdf1dd7ff)

11 years agobtrfs: remove blocksize from btrfs_alloc_free_block and rename
David Sterba [Sat, 14 Jun 2014 23:54:12 +0000 (01:54 +0200)]
btrfs: remove blocksize from btrfs_alloc_free_block and rename

Rename to btrfs_alloc_tree_block as it fits to the alloc/find/free +
_tree_block family. The parameter blocksize was set to the metadata
block size, directly or indirectly.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 4d75f8a9c87b843c8ded15b82b8d137b9724cccc)

11 years agobtrfs: remove unused parameter blocksize from btrfs_find_tree_block
David Sterba [Sat, 14 Jun 2014 23:43:40 +0000 (01:43 +0200)]
btrfs: remove unused parameter blocksize from btrfs_find_tree_block

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 0308af4465897c889e32754ef37bb465a1b2b872)

11 years agobtrfs: remove parameter blocksize from read_tree_block
David Sterba [Sat, 14 Jun 2014 23:07:32 +0000 (01:07 +0200)]
btrfs: remove parameter blocksize from read_tree_block

We know the tree block size, no need to pass it around.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit ce86cd59179279a6fe673d2a105d24fb7e70aef3)

11 years agobtrfs: inline code of reada_tree_block and remove it
David Sterba [Sat, 14 Jun 2014 22:51:19 +0000 (00:51 +0200)]
btrfs: inline code of reada_tree_block and remove it

It's trivial with a single user. And remove one pointless BUG_ON.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 453848a05ff984cb3665bad5c7e0024e8cfe87a5)

11 years agobtrfs: return void from readahead_tree_block
David Sterba [Sat, 14 Jun 2014 22:49:36 +0000 (00:49 +0200)]
btrfs: return void from readahead_tree_block

Errors in readahead are not fatal and ignored elsewhere in the code.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 6197d86eabb844c1a9c99956d4e6b0f8eb548ad3)

11 years agobtrfs: remove unused parameter from readahead_tree_block
David Sterba [Sat, 14 Jun 2014 22:29:04 +0000 (00:29 +0200)]
btrfs: remove unused parameter from readahead_tree_block

The parent_transid parameter has been unused since its introduction in
ca7a79ad8dbe2466 ("Pass down the expected generation number when reading
tree blocks").  In reada_tree_block, it was even wrongly set to leafsize.
Transid check is done in the proper read and readahead ignores errors.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 58dc4ce4325108b35425ffd30e6acfad9644d49d)

11 years agobtrfs: remove unlikely from data-dependent branches and slow paths
David Sterba [Mon, 29 Sep 2014 23:33:33 +0000 (01:33 +0200)]
btrfs: remove unlikely from data-dependent branches and slow paths

There are the branch hints that obviously depend on the data being
processed, the CPU predictor will do better job according to the actual
load. It also does not make sense to use the hints in slow paths that do
a lot of other operations like locking, waiting or IO.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit ee39b432b4ac083acdafd7b4f156283722e3bf14)

11 years agobtrfs: remove unlikely from NULL checks
David Sterba [Mon, 29 Sep 2014 17:20:37 +0000 (19:20 +0200)]
btrfs: remove unlikely from NULL checks

Unlikely is implicit for NULL checks of pointers.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 5d99a998f375b7bff7ddff0162a6eed4d4ca1318)

11 years agobtrfs: remove unused variable from btrfs_parse_options
David Sterba [Tue, 29 Jul 2014 15:41:08 +0000 (17:41 +0200)]
btrfs: remove unused variable from btrfs_parse_options

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit 143f363618558478fd7b5156c343d21e74194987)

11 years agobtrfs: defrag, use unsigned type for extent thresh
David Sterba [Tue, 29 Jul 2014 15:32:10 +0000 (17:32 +0200)]
btrfs: defrag, use unsigned type for extent thresh

Signed type mismatches the ioctl structure, all extent calculations are
done on unsigned types.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit aab110abcbbf06b5d52d9974b4a72d3c7cd38537)

11 years agoBtrfs: remove empty block groups automatically
Josef Bacik [Thu, 18 Sep 2014 15:20:02 +0000 (11:20 -0400)]
Btrfs: remove empty block groups automatically

One problem that has plagued us is that a user will use up all of his space with
data, remove a bunch of that data, and then try to create a bunch of small files
and run out of space.  This happens because all the chunks were allocated for
data since the metadata requirements were so low.  But now there's a bunch of
empty data block groups and not enough metadata space to do anything.  This
patch solves this problem by automatically deleting empty block groups.  If we
notice the used count go down to 0 when deleting or on mount notice that a block
group has a used count of 0 then we will queue it to be deleted.

When the cleaner thread runs we will double check to make sure the block group
is still empty and then we will delete it.  This patch has the side effect of no
longer having a bunch of BUG_ON()'s in the chunk delete code, which will be
helpful for both this and relocate.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 47ab2a6c689913db23ccae38349714edf8365e0a)

11 years agoBtrfs: fix fsync race leading to invalid data after log replay
Filipe Manana [Tue, 2 Sep 2014 10:09:58 +0000 (11:09 +0100)]
Btrfs: fix fsync race leading to invalid data after log replay

When the fsync callback (btrfs_sync_file) starts, it first waits for
the writeback of any dirty pages to start and finish without holding
the inode's mutex (to reduce contention). After this it acquires the
inode's mutex and repeats that process via btrfs_wait_ordered_range
only if we're doing a full sync (BTRFS_INODE_NEEDS_FULL_SYNC flag
is set on the inode).

This is not safe for a non full sync - we need to start and wait for
writeback to finish for any pages that might have been made dirty
before acquiring the inode's mutex and after that first step mentioned
before. Why this is needed is explained by the following comment added
to btrfs_sync_file:

  "Right before acquiring the inode's mutex, we might have new
   writes dirtying pages, which won't immediately start the
   respective ordered operations - that is done through the
   fill_delalloc callbacks invoked from the writepage and
   writepages address space operations. So make sure we start
   all ordered operations before starting to log our inode. Not
   doing this means that while logging the inode, writeback
   could start and invoke writepage/writepages, which would call
   the fill_delalloc callbacks (cow_file_range,
   submit_compressed_extents). These callbacks add first an
   extent map to the modified list of extents and then create
   the respective ordered operation, which means in
   tree-log.c:btrfs_log_inode() we might capture all existing
   ordered operations (with btrfs_get_logged_extents()) before
   the fill_delalloc callback adds its ordered operation, and by
   the time we visit the modified list of extent maps (with
   btrfs_log_changed_extents()), we see and process the extent
   map they created. We then use the extent map to construct a
   file extent item for logging without waiting for the
   respective ordered operation to finish - this file extent
   item points to a disk location that might not have yet been
   written to, containing random data - so after a crash a log
   replay will make our inode have file extent items that point
   to disk locations containing invalid data, as we returned
   success to userspace without waiting for the respective
   ordered operation to finish, because it wasn't captured by
   btrfs_get_logged_extents()."

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 669249eea365dd32b793b58891c74281c0aac47e)

11 years agoBtrfs: cleanup the read failure record after write or when the inode is freeing
Miao Xie [Fri, 12 Sep 2014 10:44:04 +0000 (18:44 +0800)]
Btrfs: cleanup the read failure record after write or when the inode is freeing

After the data is written successfully, we should cleanup the read failure record
in that range because
- If we set data COW for the file, the range that the failure record pointed to is
  mapped to a new place, so it is invalid.
- If we set no data COW for the file, and if there is no error during writting,
  the corrupted data is corrected, so the failure record can be removed. And if
  some errors happen on the mirrors, we also needn't worry about it because the
  failure record will be recreated if we read the same place again.

Sometimes, we may fail to correct the data, so the failure records will be left
in the tree, we need free them when we free the inode or the memory leak happens.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit f612496bca664bff6a09a99a9a7506410b6e876e)

11 years agoBtrfs: implement repair function when direct read fails
Miao Xie [Fri, 12 Sep 2014 10:44:03 +0000 (18:44 +0800)]
Btrfs: implement repair function when direct read fails

This patch implement data repair function when direct read fails.

The detail of the implementation is:
- When we find the data is not right, we try to read the data from the other
  mirror.
- When the io on the mirror ends, we will insert the endio work into the
  dedicated btrfs workqueue, not common read endio workqueue, because the
  original endio work is still blocked in the btrfs endio workqueue, if we
  insert the endio work of the io on the mirror into that workqueue, deadlock
  would happen.
- After we get right data, we write it back to the corrupted mirror.
- And if the data on the new mirror is still corrupted, we will try next
  mirror until we read right data or all the mirrors are traversed.
- After the above work, we set the uptodate flag according to the result.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 8b110e393c5a6e72d50fcdf9fa7ed8b647cfdfc9)

11 years agoBtrfs: Set real mirror number for read operation on RAID0/5/6
Miao Xie [Fri, 12 Sep 2014 10:44:02 +0000 (18:44 +0800)]
Btrfs: Set real mirror number for read operation on RAID0/5/6

We need real mirror number for RAID0/5/6 when reading data, or if read error
happens, we would pass 0 as the number of the mirror on which the io error
happens. It is wrong and would cause the filesystem read the data from the
corrupted mirror again.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 28e1cc7d1baf8038ae4ad4681c8f3dc94fcd7c00)

11 years agoBtrfs: modify clean_io_failure and make it suit direct io
Miao Xie [Fri, 12 Sep 2014 10:44:01 +0000 (18:44 +0800)]
Btrfs: modify clean_io_failure and make it suit direct io

We could not use clean_io_failure in the direct IO path because it got the
filesystem information from the page structure, but the page in the direct
IO bio didn't have the filesystem information in its structure. So we need
modify it and pass all the information it need by parameters.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 1203b6813ee84add8b4baa6d75e50ba85517e99c)

11 years agoBtrfs: modify repair_io_failure and make it suit direct io
Miao Xie [Fri, 12 Sep 2014 10:44:00 +0000 (18:44 +0800)]
Btrfs: modify repair_io_failure and make it suit direct io

The original code of repair_io_failure was just used for buffered read,
because it got some filesystem data from page structure, it is safe for
the page in the page cache. But when we do a direct read, the pages in bio
are not in the page cache, that is there is no filesystem data in the page
structure. In order to implement direct read data repair, we need modify
repair_io_failure and pass all filesystem data it need by function
parameters.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit ffdd2018dd0bbfc0d9855ed811dba67201766a2d)

11 years agoBtrfs: split bio_readpage_error into several functions
Miao Xie [Fri, 12 Sep 2014 10:43:59 +0000 (18:43 +0800)]
Btrfs: split bio_readpage_error into several functions

The data repair function of direct read will be implemented later, and some code
in bio_readpage_error will be reused, so split bio_readpage_error into
several functions which will be used in direct read repair later.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 2fe6303e7cd099334cdb09370cece6bc168de131)

11 years agoBtrfs: Cleanup unused variant and argument of IO failure handlers
Miao Xie [Fri, 12 Sep 2014 10:43:58 +0000 (18:43 +0800)]
Btrfs: Cleanup unused variant and argument of IO failure handlers

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 454ff3de42872870ffc3580b69132a9ef40f5cc5)

11 years agoBtrfs: fix missing error handler if submiting re-read bio fails
Miao Xie [Fri, 12 Sep 2014 10:43:57 +0000 (18:43 +0800)]
Btrfs: fix missing error handler if submiting re-read bio fails

We forgot to free failure record and bio after submitting re-read bio failed,
fix it.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 6c387ab20db15f2bd448f7c508e2638101b16ea1)

11 years agoBtrfs: do file data check by sub-bio's self
Miao Xie [Fri, 12 Sep 2014 10:43:56 +0000 (18:43 +0800)]
Btrfs: do file data check by sub-bio's self

Direct IO splits the original bio to several sub-bios because of the limit of
raid stripe, and the filesystem will wait for all sub-bios and then run final
end io process.

But it was very hard to implement the data repair when dio read failure happens,
because at the final end io function, we didn't know which mirror the data was
read from. So in order to implement the data repair, we have to move the file data
check in the final end io function to the sub-bio end io function, in which we can
get the mirror number of the device we access. This patch did this work as the
first step of the direct io data repair implementation.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit c1dc08967f69c6b5067f8302c600f6628123f3bf)

11 years agoBtrfs: cleanup similar code of the buffered data data check and dio read data check
Miao Xie [Fri, 12 Sep 2014 10:43:55 +0000 (18:43 +0800)]
Btrfs: cleanup similar code of the buffered data data check and dio read data check

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit dc380aea5fa4636fc498a351eb720943bc644451)

11 years agoBtrfs: load checksum data once when submitting a direct read io
Miao Xie [Fri, 12 Sep 2014 10:43:54 +0000 (18:43 +0800)]
Btrfs: load checksum data once when submitting a direct read io

The current code would load checksum data for several times when we split
a whole direct read io because of the limit of the raid stripe, it would
make us search the csum tree for several times. In fact, it just wasted time,
and made the contention of the csum tree root be more serious. This patch
improves this problem by loading the data at once.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 23ea8e5a07673127d05cb5cf6f9914d7a53e0847)

11 years agoBtrfs: modify rw_devices counter under chunk_mutex context
Miao Xie [Wed, 3 Sep 2014 13:35:47 +0000 (21:35 +0800)]
Btrfs: modify rw_devices counter under chunk_mutex context

rw_devices counter is often used to tune the profile when doing chunk allocation,
so we should modify it under the chunk_mutex context to avoid getting wrong
chunk profile.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit c3929c3624e1764855e5a452728aec201b18b42a)

11 years agoBtrfs: move the missing device to its own fs device list
Miao Xie [Wed, 3 Sep 2014 13:35:46 +0000 (21:35 +0800)]
Btrfs: move the missing device to its own fs device list

For a missing device, we don't know it belong to which fs before we read its
fsid from the chunk tree. So we add them into the current fs device list at first.
When we get its fsid, we should move them to their own fs device list.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 5f375835694426d00a7a4bc7fef72479578f279f)

11 years agoBtrfs: stop mounting the fs if the non-ENOENT errors happen when opening seed fs
Miao Xie [Wed, 3 Sep 2014 13:35:45 +0000 (21:35 +0800)]
Btrfs: stop mounting the fs if the non-ENOENT errors happen when opening seed fs

When we open a seed filesystem, if the degraded mount option is set, we continue to
mount the fs if we don't find some devices in the seed filesystem. But we should stop
mounting if other errors happen. Fix it

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 416d7b802a8bc044e98dc8bd96b59ce7a02287ac)

11 years agoBtrfs: make the logic of source device removing more clear
Miao Xie [Wed, 3 Sep 2014 13:35:44 +0000 (21:35 +0800)]
Btrfs: make the logic of source device removing more clear

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 82372bc816d75722c24d1abadb11cd8c0a33883a)

11 years agoBtrfs: fix use-after-free problem of the device during device replace
Miao Xie [Wed, 3 Sep 2014 13:35:43 +0000 (21:35 +0800)]
Btrfs: fix use-after-free problem of the device during device replace

The problem is:
Task0(device scan task) Task1(device replace task)
scan_one_device()
mutex_lock(&uuid_mutex)
device = find_device()
mutex_lock(&device_list_mutex)
lock_chunk()
rm_and_free_source_device
unlock_chunk()
mutex_unlock(&device_list_mutex)
check device

Destroying the target device if device replace fails also has the same problem.

We fix this problem by locking uuid_mutex during destroying source device or
target device, just like the device remove operation.

It is a temporary solution, we can fix this problem and make the code more
clear by atomic counter in the future.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 67a2c45ee7f4f250458279a2e1244679c5d9735c)

11 years agoBtrfs: fix unprotected device list access when cloning fs devices
Miao Xie [Wed, 3 Sep 2014 13:35:42 +0000 (21:35 +0800)]
Btrfs: fix unprotected device list access when cloning fs devices

We can build a new filesystem based a seed filesystem, and we need clone
the fs devices when we open the new filesystem. But someone might clear
the seed flag of the seed filesystem, then mount that filesystem and
remove some device. If we mount the new filesystem, we might access
a device list which was being changed when we clone the fs devices.
Fix it.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit adbbb8631beda8e4e5d2c964b8b47e04cfa0a2ae)

11 years agoBtrfs: Fix misuse of chunk mutex
Miao Xie [Wed, 3 Sep 2014 13:35:41 +0000 (21:35 +0800)]
Btrfs: Fix misuse of chunk mutex

There were several problems about chunk mutex usage:
- Lock chunk mutex when updating metadata. It would cause the nested
  deadlock because updating metadata might need allocate new chunks
  that need acquire chunk mutex. We remove chunk mutex at this case,
  because b-tree lock and other lock mechanism can help us.
- ABBA deadlock occured between device_list_mutex and chunk_mutex.
  When we update device status, we must acquire device_list_mutex at the
  beginning, and then we might get chunk_mutex during the device status
  update because we need allocate new chunks for metadata COW. But at
  most place, we acquire chunk_mutex at first and then acquire device list
  mutex. We need change the lock order.
- Some place we needn't acquire chunk_mutex. For example we needn't get
  chunk_mutex when we free a empty seed fs_devices structure.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 2196d6e8a71fc901e31c1d81581fc6cc6c64913e)

11 years agoBtrfs: fix unprotected device list access when getting the fs information
Miao Xie [Wed, 3 Sep 2014 13:35:40 +0000 (21:35 +0800)]
Btrfs: fix unprotected device list access when getting the fs information

When we get the fs information, we forgot to acquire the mutex of device list,
it might cause the problem we might access a device that was removed. Fix
it by acquiring the device list mutex.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 15484377f597ca98ee84de87caa13667ea68bb14)

11 years agoBtrfs: fix unprotected system chunk array insertion
Miao Xie [Wed, 3 Sep 2014 13:35:39 +0000 (21:35 +0800)]
Btrfs: fix unprotected system chunk array insertion

We didn't protect the system chunk array when we added a new
system chunk into it, it would cause the array be corrupted
if someone remove/add some system chunk into array at the same
time. Fix it by chunk lock.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit fe48a5c00f3c8087fc0a447caee2b5f9f97cf238)

11 years agoBtrfs: fix unprotected device's variants on 32bits machine
Miao Xie [Wed, 3 Sep 2014 13:35:38 +0000 (21:35 +0800)]
Btrfs: fix unprotected device's variants on 32bits machine

->total_bytes,->disk_total_bytes,->bytes_used is protected by chunk
lock when we change them, but sometimes we read them without any lock,
and we might get unexpected value. We fix this problem like inode's
i_size.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 7cc8e58d53cd2295c3c1cee7b503bd1790ea4486)

11 years agoBtrfs: update free_chunk_space during allocting a new chunk
Miao Xie [Wed, 3 Sep 2014 13:35:37 +0000 (21:35 +0800)]
Btrfs: update free_chunk_space during allocting a new chunk

We should update free_chunk_space in time when we allocate a new chunk,
not when we deal with the pending device update and block group insertion,
because we need the real free_chunk_space data to calculate the reserved
space, if we don't update it in time, we would consider the disk space which
has be allocated as free space, and would use it to do overcommit reservation.
Fix it.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 1c1161870c8bcb0d966ebbf1aec05a87a79a4175)

11 years agoBtrfs: fix unprotected device->bytes_used update
Miao Xie [Wed, 3 Sep 2014 13:35:36 +0000 (21:35 +0800)]
Btrfs: fix unprotected device->bytes_used update

We should update device->bytes_used in the lock context of
chunk_mutex, or we would get wrong data.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 43530c46cc55cf5a381e788276a93c83cbc95e46)

11 years agoBtrfs: Fix wrong free_chunk_space assignment during removing a device
Miao Xie [Wed, 3 Sep 2014 13:35:35 +0000 (21:35 +0800)]
Btrfs: Fix wrong free_chunk_space assignment during removing a device

During removing a device, we have modified free_chunk_space when we
shrink the device, so we needn't assign a new value to it after
the device shrink. Fix it.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 5d778aaeb013bf819d00e33de7c673c04b5c4886)

11 years agoBtrfs: fix wrong device bytes_used in the super block
Miao Xie [Wed, 3 Sep 2014 13:35:34 +0000 (21:35 +0800)]
Btrfs: fix wrong device bytes_used in the super block

device->bytes_used will be changed when allocating a new chunk, and
disk_total_size will be changed if resizing is successful.
Meanwhile, the on-disk super blocks of the previous transaction
might not be updated. Considering the consistency of the metadata
in the previous transaction, We should use the size in the previous
transaction to check if the super block is beyond the boundary
of the device.

Though it is not big problem because we don't use it now, but anyway
it is better that we make it be consistent with the common metadata,
maybe we will use it in the future.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit ce7213c70c37e3a66bc0b50c45edcbfea505f62f)

11 years agoBtrfs: fix wrong disk size when writing super blocks
Miao Xie [Wed, 3 Sep 2014 13:35:33 +0000 (21:35 +0800)]
Btrfs: fix wrong disk size when writing super blocks

total_size will be changed when resizing a device, and disk_total_size
will be changed if resizing is successful. Meanwhile, the on-disk super
blocks of the previous transaction might not be updated. Considering
the consistency of the metadata in the previous transaction, We should
use the size in the previous transaction to check if the super block is
beyond the boundary of the device. Fix it.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 935e5cc935bcbf9b3d0dd59fed7dbc0f2ebca6bc)

11 years agoBtrfs: fix unprotected assignment of the target device
Miao Xie [Wed, 3 Sep 2014 13:35:32 +0000 (21:35 +0800)]
Btrfs: fix unprotected assignment of the target device

We didn't protect the assignment of the target device, it might cause the
problem that the super block update was skipped because we might find wrong
size of the target device during the assignment. Fix it by moving the
assignment sentences into the initialization function of the target device.
And there is another merit that we can check if the target device is suitable
more early.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 1c43366d3b3f0fa6c6e81aaf3aa18e0550245dad)

11 years agoBtrfs: cleanup double assignment of device->bytes_used when device replace finishes
Miao Xie [Wed, 3 Sep 2014 13:35:31 +0000 (21:35 +0800)]
Btrfs: cleanup double assignment of device->bytes_used when device replace finishes

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit c7662111c741bc04a7192f2a00aad608cbc0b205)

11 years agoBtrfs: cleanup unused num_can_discard in fs_devices
Miao Xie [Wed, 3 Sep 2014 13:35:30 +0000 (21:35 +0800)]
Btrfs: cleanup unused num_can_discard in fs_devices

The member variants - num_can_discard - of fs_devices structure
are set, but no one use them to do anything. so remove them.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 90180da42c38a83b050452aa555d3d2df6ae2ad4)

11 years agobtrfs: remove the wrong comments
Li RongQing [Mon, 8 Sep 2014 12:41:09 +0000 (20:41 +0800)]
btrfs: remove the wrong comments

This comments became wrong after c3c532[bdi: add helper function for
doing init and register of a bdi for a file system], so remove them.

Signed-off-by: Li RongQing <roy.qing.li@gmail.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 82f70d62f7923cc43128e75ae85366f137055b76)

11 years agoBtrfs: fix directory recovery from fsync log
Filipe Manana [Mon, 8 Sep 2014 21:53:18 +0000 (22:53 +0100)]
Btrfs: fix directory recovery from fsync log

When replaying a directory from the fsync log, if a directory entry
exists both in the fs/subvol tree and in the log, the directory's inode
got its i_size updated incorrectly, accounting for the dentry's name
twice.

Reproducer, from a test for xfstests:

    _scratch_mkfs >> $seqres.full 2>&1
    _init_flakey
    _mount_flakey

    touch $SCRATCH_MNT/foo
    sync

    touch $SCRATCH_MNT/bar
    xfs_io -c "fsync" $SCRATCH_MNT
    xfs_io -c "fsync" $SCRATCH_MNT/bar

    _load_flakey_table $FLAKEY_DROP_WRITES
    _unmount_flakey

    _load_flakey_table $FLAKEY_ALLOW_WRITES
    _mount_flakey

    [ -f $SCRATCH_MNT/foo ] || echo "file foo is missing"
    [ -f $SCRATCH_MNT/bar ] || echo "file bar is missing"

    _unmount_flakey
    _check_scratch_fs $FLAKEY_DEV

The filesystem check at the end failed with the message:
"root 5 root dir 256 error".

A test case for xfstests follows.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit a2cc11db245b9d8fbd4e3adbe2a1e7cf60473950)

11 years agoBtrfs: make fiemap not blow when you have lots of snapshots
Josef Bacik [Wed, 10 Sep 2014 20:20:45 +0000 (16:20 -0400)]
Btrfs: make fiemap not blow when you have lots of snapshots

We have been iterating all references for each extent we have in a file when we
do fiemap to see if it is shared.  This is fine when you have a few clones or a
few snapshots, but when you have 5k snapshots suddenly fiemap just sits there
and stares at you.  So add btrfs_check_shared which will use the backref walking
code but will short circuit as soon as it finds a root or inode that doesn't
match the one we currently have.  This makes fiemap on my testbox go from
looking at me blankly for a day to spitting out actual output in a reasonable
amount of time.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit dc046b10c8b7d4f40befe457acb82340bf8b0699)

11 years agoBtrfs: cleanup the same name in end_bio_extent_readpage
Liu Bo [Tue, 19 Aug 2014 15:32:22 +0000 (23:32 +0800)]
Btrfs: cleanup the same name in end_bio_extent_readpage

We've defined a 'offset' out of bio_for_each_segment_all.

This is just a clean rename, no function changes.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit a583c02664eea8796e80dd192a3bcc1d521939e5)

11 years agoBtrfs: shrink further sizeof(struct extent_buffer)
Filipe Manana [Thu, 14 Aug 2014 11:25:14 +0000 (12:25 +0100)]
Btrfs: shrink further sizeof(struct extent_buffer)

The map_start and map_len fields aren't used anywhere, so just remove
them. On a x86_64 system, this reduced sizeof(struct extent_buffer)
from 296 bytes to 280 bytes, and therefore 14 extent_buffer structs can
now fit into a page instead of 13.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 2a39e5980257c77f48b5c31f9fb483a72a03b213)

11 years agoBtrfs: send, lower mem requirements for processing xattrs
Filipe Manana [Wed, 20 Aug 2014 09:45:45 +0000 (10:45 +0100)]
Btrfs: send, lower mem requirements for processing xattrs

Maximum xattr size can be up to nearly the leaf size. For an fs with a
leaf size larger than the page size, using kmalloc requires allocating
multiple pages that are contiguous, which might not be possible if
there's heavy memory fragmentation. Therefore fallback to vmalloc if
we fail to allocate with kmalloc. Also start with a smaller buffer size,
since xattr values typically are smaller than a page.

Reported-by: Chris Murphy <lists@colorremedies.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 4395e0c4da486f007dcb45b0336427be7ec08ab1)

11 years agobtrfs: remove stale define after removing ordered operations
David Sterba [Wed, 20 Aug 2014 15:34:22 +0000 (17:34 +0200)]
btrfs: remove stale define after removing ordered operations

Last user removed in commit "btrfs: disable strict file flushes for
renames and truncates" (8d875f95da43c6a8f18f77869f2ef26e9594fecc).

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit f87c4318af8bd5daec361e436c69f9d71a43b261)

11 years agoBtrfs: improve free space cache management and space allocation
Filipe Manana [Fri, 29 Aug 2014 12:35:13 +0000 (13:35 +0100)]
Btrfs: improve free space cache management and space allocation

While under random IO, a block group's free space cache eventually reaches
a state where it has a mix of extent entries and bitmap entries representing
free space regions.

As later free space regions are returned to the cache, some of them are merged
with existing extent entries if they are contiguous with them. But others are
not merged, because despite the existence of adjacent free space regions in
the cache, the merging doesn't happen because the existing free space regions
are represented in bitmap extents. Even when new free space regions are merged
with existing extent entries (enlarging the free space range they represent),
we create chances of having after an enlarged region that is contiguous with
some other region represented in a bitmap entry.

Both clustered and non-clustered space allocation work by iterating over our
extent and bitmap entries and skipping any that represents a region smaller
then the allocation request (and giving preference to extent entries before
bitmap entries). By having a contiguous free space region that is represented
by 2 (or more) entries (mix of extent and bitmap entries), we end up not
satisfying an allocation request with a size larger than the size of any of
the entries but no larger than the sum of their sizes. Making the caller assume
we're under a ENOSPC condition or force it to allocate multiple smaller space
regions (as we do for file data writes), which adds extra overhead and more
chances of causing fragmentation due to the smaller regions being all spread
apart from each other (more likely when under concurrency).

For example, if we have the following in the cache:

* extent entry representing free space range: [128Mb - 256Kb, 128Mb[

* bitmap entry covering the range [128Mb, 256Mb[, but only with the bits
  representing the range [128Mb, 128Mb + 768Kb[ set - that is, only that
  space in this 128Mb area is marked as free

An allocation request for 1Mb, starting at offset not greater than 128Mb - 256Kb,
would fail before, despite the existence of such contiguous free space area in the
cache. The caller could only allocate up to 768Kb of space at once and later another
256Kb (or vice-versa). In between each smaller allocation request, another task
working on a different file/inode might come in and take that space, preventing the
former task of getting a contiguous 1Mb region of free space.

Therefore this change implements the ability to move free space from bitmap
entries into existing and new free space regions represented with extent
entries. This is done when a space region is added to the cache.

A test was added to the sanity tests that explains in detail the issue too.

Some performance test results with compilebench on a 4 cores machine, with
32Gb of ram and using an HDD follow.

Test: compilebench -D /mnt -i 30 -r 1000 --makej

Before this change:

   intial create total runs 30 avg 69.02 MB/s (user 0.28s sys 0.57s)
   compile total runs 30 avg 314.96 MB/s (user 0.12s sys 0.25s)
   read compiled tree total runs 3 avg 27.14 MB/s (user 1.52s sys 0.90s)
   delete compiled tree total runs 30 avg 3.14 seconds (user 0.15s sys 0.66s)

After this change:

   intial create total runs 30 avg 68.37 MB/s (user 0.29s sys 0.55s)
   compile total runs 30 avg 382.83 MB/s (user 0.12s sys 0.24s)
   read compiled tree total runs 3 avg 27.82 MB/s (user 1.45s sys 0.97s)
   delete compiled tree total runs 30 avg 3.18 seconds (user 0.17s sys 0.65s)

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 200055239604cf4bfaed40d8f404228ea606b4f9)

11 years agobtrfs: rename total_bytes to avoid confusion
Anand Jain [Wed, 20 Aug 2014 02:54:17 +0000 (10:54 +0800)]
btrfs: rename total_bytes to avoid confusion

we are assigning number_devices to the total_bytes,
that's very confusing for a moment

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 3c1dbdf54a31f4f049a33214c3096595988786bf)

11 years agobtrfs: fix typo in the log message
Anand Jain [Wed, 13 Aug 2014 06:24:25 +0000 (14:24 +0800)]
btrfs: fix typo in the log message

there is no matching open parenthesis for the closing parenthesis

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit de4c296f63b43794df453a3fffbb4163ccd1c6af)

11 years agobtrfs: rw_devices shouldn't be incremented for seed fs in btrfs_rm_dev_replace_srcdev()
Anand Jain [Wed, 13 Aug 2014 06:24:24 +0000 (14:24 +0800)]
btrfs: rw_devices shouldn't be incremented for seed fs in btrfs_rm_dev_replace_srcdev()

seed fs devices don't participate as rw_device, so don't increment
rw_devices when the device being handled belongs to a seed fs.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit b2efedca68c15bc6eab6cbe0756f90c3b8f23b8f)

11 years agobtrfs: fix memory leak when there is no more seed device
Anand Jain [Wed, 13 Aug 2014 06:24:23 +0000 (14:24 +0800)]
btrfs: fix memory leak when there is no more seed device

When we replace all the seed device in the system there is
no point in just keeping the btrfs_fs_devices with out
any device

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 8bef8401a04ae1937f3330484c2d7a8c6b9b9897)

11 years agobtrfs: update sprout seed pointer when seed fs is relinquished
Anand Jain [Wed, 13 Aug 2014 06:24:22 +0000 (14:24 +0800)]
btrfs: update sprout seed pointer when seed fs is relinquished

We are not updating sprout fs seed pointer when all seed device
is replaced. This patch will check if all seed device has been
replaced and then update the sprout pointer accordingly.

Same reproducer as in the previous patch would apply here.
And notice that btrfs_close_device will check if seed fs is
present and spits out the error with out this patch.

int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
{
::
                seed_devices = fs_devices->seed;
::
        while (seed_devices) {
                fs_devices = seed_devices;
                seed_devices = fs_devices->seed;
                __btrfs_close_devices(fs_devices);
                free_fs_devices(fs_devices);
        }

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 94d5f0c2ae83c367fe870b49ed0c87025be2956e)

11 years agobtrfs: fix rw_devices miss match after seed replace
Anand Jain [Wed, 13 Aug 2014 06:24:21 +0000 (14:24 +0800)]
btrfs: fix rw_devices miss match after seed replace

reproducer:
    reproducer:
    mount /dev/sdb /btrfs
    btrfs dev add /dev/sdc /btrfs
    btrfs rep start -B /dev/sdb /dev/sdd /btrfs
    umount /btrfs

WARNING: CPU: 0 PID: 3882 at fs/btrfs/volumes.c:892 __btrfs_close_devices+0x1c8/0x200 [btrfs]()

which is

        WARN_ON(fs_devices->rw_devices);

   The problem here is that we did not add one to the rw_devices when
   we replace the seed device with a writable device.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 63dd86fa79db737a50f47488e5249f24e5acebc1)

11 years agobtrfs: replace seed device followed by unmount causes kernel WARNING
Anand Jain [Wed, 20 Aug 2014 02:56:56 +0000 (10:56 +0800)]
btrfs: replace seed device followed by unmount causes kernel WARNING

reproducer:
mount /dev/sdb /btrfs
btrfs dev add /dev/sdc /btrfs
btrfs rep start -B /dev/sdb /dev/sdd /btrfs
umount /btrfs

WARNING: CPU: 0 PID: 12661 at fs/btrfs/volumes.c:891 __btrfs_close_devices+0x1b0/0x200 [btrfs]()
::

__btrfs_close_devices()
::
        WARN_ON(fs_devices->open_devices);

After the seed device has been replaced the new target device
is no more a seed device. So we need to update the device
numbers in the fs_devices as pointed by the fs_info.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 25e8e9113d340f0c32161ced0d4655eff8fed757)

11 years agobtrfs: preparatory to make btrfs_rm_dev_replace_srcdev() seed aware
Anand Jain [Wed, 13 Aug 2014 06:24:19 +0000 (14:24 +0800)]
btrfs: preparatory to make btrfs_rm_dev_replace_srcdev() seed aware

There is no logical change in this patch, just a preparatory patch,
so that changes can be easily reasoned.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit d51908ce4e5d7f7b07867180e36b9935094b78f2)

11 years agobtrfs: Drop stray check of fixup_workers creation
Andrey Utkin [Sat, 9 Aug 2014 11:51:15 +0000 (14:51 +0300)]
btrfs: Drop stray check of fixup_workers creation

The issue was introduced in a79b7d4b3e8118f265dcb4bdf9a572c392f02708,
adding allocation of extent_workers, so this stray check is surely not
meant to be a check of something else.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=82021
Reported-by: Maks Naumov <maksqwe1@ukr.net>
Signed-off-by: Andrey Utkin <andrey.krieger.utkin@gmail.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 56094eecd32cbb80d098eee5a7cbd60f39f4b764)

11 years agoBtrfs: make btrfs_search_forward return with nodes unlocked
Filipe Manana [Mon, 4 Aug 2014 18:37:21 +0000 (19:37 +0100)]
Btrfs: make btrfs_search_forward return with nodes unlocked

None of the uses of btrfs_search_forward() need to have the path
nodes (level >= 1) read locked, only the leaf needs to be locked
while the caller processes it. Therefore make it return a path
with all nodes unlocked, except for the leaf.

This change is motivated by the observation that during a file
fsync we repeatdly call btrfs_search_forward() and process the
returned leaf while upper nodes of the returned path (level >= 1)
are read locked, which unnecessarily blocks other tasks that want
to write to the same fs/subvol btree.
Therefore instead of modifying the fsync code to unlock all nodes
with level >= 1 immediately after calling btrfs_search_forward(),
change btrfs_search_forward() to do it, so that it benefits all
callers.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit f98de9b9c07485f7e21edfd5b2b20c89d662af3c)

11 years agobtrfs: sysfs label interface should check for read only FS
Anand Jain [Wed, 30 Jul 2014 12:04:10 +0000 (20:04 +0800)]
btrfs: sysfs label interface should check for read only FS

Not sure how this escaped many eyes so far

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 79aec2b80db1089f983d60e4a22cb95d8b939844)

11 years agobtrfs: code optimize: BTRFS_ATTR_RW could set the mode
Anand Jain [Wed, 30 Jul 2014 12:04:09 +0000 (20:04 +0800)]
btrfs: code optimize: BTRFS_ATTR_RW could set the mode

BTRFS_ATTR_RW could set the mode and be inline with BTRFS_ATTR

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 20ee0825ecec889afdf12f7c13b9dc3af0bb9f14)

11 years agobtrfs: code optimize: BTRFS_ATTR could handle the mode
Anand Jain [Wed, 30 Jul 2014 12:04:08 +0000 (20:04 +0800)]
btrfs: code optimize: BTRFS_ATTR could handle the mode

All that uses BTRFS_ATTR want mode to be set at 0444 so just do
it at the define.  And few spacing alignments.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 98b3d389eb473f28ef69547c0a9f72041c4c40e6)

11 years agobtrfs: use BTRFS_ATTR instead of btrfs_no_store()
Anand Jain [Wed, 30 Jul 2014 12:04:07 +0000 (20:04 +0800)]
btrfs: use BTRFS_ATTR instead of btrfs_no_store()

we have BTRFS_ATTR define to create sysfs RO file, use that.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 3f4b57e09d6cbd97f92311a801e1b83992506dcd)

11 years agoBtrfs: avoid unnecessary switch of path locks to blocking mode
Filipe Manana [Mon, 28 Jul 2014 18:37:17 +0000 (19:37 +0100)]
Btrfs: avoid unnecessary switch of path locks to blocking mode

If we need to cow a node, increase the write lock level and retry the
tree search, there's no point of changing the node locks in our path
to blocking mode, as we only waste time and unnecessarily wake up other
tasks waiting on the spinning locks (just to block them again shortly
after) because we release our path before repeating the tree search.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 160f4089c8580b32b5805e7fd8ec7b3810f442c1)

11 years agoBtrfs: unlock nodes earlier when inserting items in a btree
Filipe Manana [Mon, 28 Jul 2014 18:34:35 +0000 (19:34 +0100)]
Btrfs: unlock nodes earlier when inserting items in a btree

In ctree.c:setup_items_for_insert(), we can unlock all nodes in our
path before we process the leaf (shift items and data, adjust data
offsets, etc). This allows for better btree concurrency, as we're
often holding a write lock on at least the node at level 1.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 24cdc847d9842bdfd85a005ebc39050bcbc7acba)

11 years agobtrfs: use IS_ALIGNED() for assertion in btrfs_lookup_csums_range() for simplicity
Satoru Takeuchi [Fri, 25 Jul 2014 08:07:48 +0000 (17:07 +0900)]
btrfs: use IS_ALIGNED() for assertion in btrfs_lookup_csums_range() for simplicity

btrfs_lookup_csums_range() uses ALIGN() to check if "start"
and "end + 1" are aligned to "root->sectorsize". It's better to
replace these with IS_ALIGNED() for simplicity.

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit d1b00a4711d5b953b13ccc859bc30c447c96860e)

11 years agobtrfs: add trace for qgroup accounting
Mark Fasheh [Thu, 17 Jul 2014 19:39:00 +0000 (12:39 -0700)]
btrfs: add trace for qgroup accounting

We want this to debug qgroup changes on live systems.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
Reviewed-by: Josef Bacik <jbacik@fb.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit d3982100baeb9bed8cf931545fb0522268f526e5)

11 years agoBtrfs: cleanup unused latest_devid and latest_trans in fs_devices
Miao Xie [Thu, 24 Jul 2014 03:37:15 +0000 (11:37 +0800)]
Btrfs: cleanup unused latest_devid and latest_trans in fs_devices

The member variants - latest_devid and latest_trans - of fs_devices structure
are set, but no one use them to do anything. so remove them.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 443f24fee731703345e2e6eec8177bb7b7d094c6)

11 years agoBtrfs: update the comment of total_bytes and disk_total_bytes of btrfs_devie
Miao Xie [Thu, 24 Jul 2014 03:37:12 +0000 (11:37 +0800)]
Btrfs: update the comment of total_bytes and disk_total_bytes of btrfs_devie

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 6ba40b615f6e2aac40c24b649c0170d43ae19165)

11 years agoBtrfs: Fix the problem that the dirty flag of dev stats is cleared
Miao Xie [Thu, 24 Jul 2014 03:37:11 +0000 (11:37 +0800)]
Btrfs: Fix the problem that the dirty flag of dev stats is cleared

The io error might happen during writing out the device stats, and the
device stats information and dirty flag would be update at that time,
but the current code didn't consider this case, just clear the dirty
flag, it would cause that we forgot to write out the new device stats
information. Fix it.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit addc3fa74e5bcfabc0766f934a8895d1f9f6a67c)

11 years agoBtrfs: make the device lock and its protected data in the same cacheline
Miao Xie [Thu, 24 Jul 2014 03:37:10 +0000 (11:37 +0800)]
Btrfs: make the device lock and its protected data in the same cacheline

The lock in btrfs_device structure was far away from its protected data, it would
make CPU load the cache line twice when we accessed them, move them together.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit d5ee37bcb1dff9c1d1cac5f7e5752309f1ff66b6)

11 years agoBtrfs: fix wrong generation check of super block on a seed device
Miao Xie [Thu, 24 Jul 2014 03:37:09 +0000 (11:37 +0800)]
Btrfs: fix wrong generation check of super block on a seed device

The super block generation of the seed devices is not the same as the
filesystem which sprouted from them because we don't update the super
block on the seed devices when we change that new filesystem. So we
should not use the generation of that new filesystem to check the super
block generation on the seed devices, Fix it.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 5f546063cee93047af90cf2756e023da9f9fca51)

11 years agoBtrfs: fix wrong fsid check of scrub
Miao Xie [Thu, 24 Jul 2014 03:37:08 +0000 (11:37 +0800)]
Btrfs: fix wrong fsid check of scrub

All the metadata in the seed devices has the same fsid as the fsid
of the seed filesystem which is on the seed device, so we should check
them by the current filesystem. Fix it.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 17a9be2f28595945ec9bfac0dd15b86891c1f1de)

11 years agoBtrfs: fix wrong max inline data size limit
Wang Shilong [Thu, 17 Jul 2014 03:44:12 +0000 (11:44 +0800)]
Btrfs: fix wrong max inline data size limit

inline data is stored from offset of @disk_bytenr in
struct btrfs_file_extent_item. So substracting total
size of struct btrfs_file_extent_item is wrong, fix it.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit c01a5c074c0f6f85a3b02e39432b9e5576ab51de)

11 years agoBtrfs: fix off-by-one in cow_file_range_inline()
Wang Shilong [Thu, 17 Jul 2014 03:44:11 +0000 (11:44 +0800)]
Btrfs: fix off-by-one in cow_file_range_inline()

Btrfs could still inline file data if its size is same as
page size, so don't skip max value here.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 354877befa852e9b62ddc92a6cc017210e982d46)

11 years agoBtrfs: fall into nocompression codes quickly if possible
Wang Shilong [Thu, 17 Jul 2014 03:44:10 +0000 (11:44 +0800)]
Btrfs: fall into nocompression codes quickly if possible

If flag NOCOMPRESS is set which means bad compression ratio,
we could avoid call cow_file_range_async() for this case earlier.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 7816030eb48e00a36ecdc23d484b960922feee67)

11 years agoBtrfs: fix wrong skipping compression for an inode
Wang Shilong [Thu, 17 Jul 2014 03:44:09 +0000 (11:44 +0800)]
Btrfs: fix wrong skipping compression for an inode

If a file's compression ratios is bad, we will set NOCOMPRESS
flag for it, and it will skip compression for that inode next time.

However, if we remount fs to COMPRESS_FORCE, it still should try
if we could compress pages for that inode, this patch fix wrong
check for this problem.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit f79707b092caff665a8a77e8e31fe4ab18b4d109)

11 years agoBtrfs: fix sparse warning
Fabian Frederick [Tue, 15 Jul 2014 19:17:17 +0000 (21:17 +0200)]
Btrfs: fix sparse warning

Fix the following sparse warning:
fs/btrfs/send.c:518:51: warning: incorrect type in argument 2 (different address spaces)
fs/btrfs/send.c:518:51:    expected char const [noderef] <asn:1>*<noident>
fs/btrfs/send.c:518:51:    got char *

We can safely use (const char __user *) with set_fs(KERNEL_DS)

__force added to avoid sparse-all warning:
fs/btrfs/send.c:518:40: warning: cast adds address space to expression (<asn:1>)

Signed-off-by: Fabian Frederick <fabf@skynet.be>
Reviewed-by: Zach Brown <zab@zabbo.net>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit d447d0da44cd7d396277d1d8f46b418c721fbc02)

11 years agoBtrfs: use BUG_ON
HIMANGI SARAOGI [Tue, 8 Jul 2014 22:21:41 +0000 (03:51 +0530)]
Btrfs: use BUG_ON

Use BUG_ON(x) rather than if(x) BUG();

The semantic patch that fixes this problem is as follows:

// <smpl>
@@ identifier x; @@
-if (x) BUG();
+BUG_ON(x);
// </smpl>

Signed-off-by: Himangi Saraogi <himangi774@gmail.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 14586651ed8f067e4add72c739f0bd2c2620fd9d)

11 years agobtrfs compression: merge inflate and deflate z_streams
Sergey Senozhatsky [Mon, 7 Jul 2014 14:38:29 +0000 (23:38 +0900)]
btrfs compression: merge inflate and deflate z_streams

`struct workspace' used for zlib compression contains two zlib
z_stream-s: `def_strm' used in zlib_compress_pages(), and `inf_strm'
used in zlib_decompress/zlib_decompress_biovec(). None of these
functions use `inf_strm' and `def_strm' simultaniously, meaning that
for every compress/decompress operation we need only one z_stream
(out of two available).

`inf_strm' and `def_strm' are different in size of ->workspace. For
inflate stream we vmalloc() zlib_inflate_workspacesize() bytes, for
deflate stream - zlib_deflate_workspacesize() bytes. On my system zlib
returns the following workspace sizes, correspondingly: 42312 and 268104
(+ guard pages).

Keep only one `z_stream' in `struct workspace' and use it for both
compression and decompression. Hence, instead of vmalloc() of two
z_stream->worskpace-s, allocate only one of size:
max(zlib_deflate_workspacesize(), zlib_inflate_workspacesize())

Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 7880991344f73647fa2222b198cf5cfc10805ac2)

11 years agoBtrfs: set error return value in btrfs_get_blocks_direct
Filipe Manana [Mon, 7 Jul 2014 11:35:21 +0000 (12:35 +0100)]
Btrfs: set error return value in btrfs_get_blocks_direct

We were returning with 0 (success) because we weren't extracting the
error code from em (PTR_ERR(em)). Fix it.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 555e12864063762964433139dee651c5b859a047)

11 years agoBtrfs: reduce size of struct extent_state
Filipe Manana [Sun, 6 Jul 2014 19:09:59 +0000 (20:09 +0100)]
Btrfs: reduce size of struct extent_state

The tree field of struct extent_state was only used to figure out if
an extent state was connected to an inode's io tree or not. For this
we can just use the rb_node field itself.

On a x86_64 system with this change the sizeof(struct extent_state) is
reduced from 96 bytes down to 88 bytes, meaning that with a page size
of 4096 bytes we can now store 46 extent states per page instead of 42.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 27a3507de91e3dd51a2cf8dca4b33623dd6eaa88)

11 years agobtrfs: use PTR_ERR_OR_ZERO
Fabian Frederick [Fri, 4 Jul 2014 19:10:27 +0000 (21:10 +0200)]
btrfs: use PTR_ERR_OR_ZERO

replace IS_ERR/PTR_ERR

Cc: Chris Mason <clm@fb.com>
Cc: Josef Bacik <jbacik@fb.com>
Cc: linux-btrfs@vger.kernel.org
Signed-off-by: Fabian Frederick <fabf@skynet.be>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 6f84e23646704c93fa878c5b87a4990be8d1ca9c)

11 years agoBtrfs: print btrfs specific info for some fatal error cases
Wang Shilong [Fri, 4 Jul 2014 09:59:06 +0000 (17:59 +0800)]
Btrfs: print btrfs specific info for some fatal error cases

Marc argued that if there are several btrfs filesystems mounted,
while users even don't know which filesystem hit the corrupted
errors something like generation verification failure.

Since @extent_buffer structure has a member @fs_info, let's output
btrfs device info.

Reported-by: Marc MERLIN <marc@merlins.org>
Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 29549aec76bd6f1fc8e1723ed5396d65073d6521)

11 years agoBtrfs: fix writing data into the seed filesystem
Miao Xie [Thu, 3 Jul 2014 10:22:13 +0000 (18:22 +0800)]
Btrfs: fix writing data into the seed filesystem

If we mounted a seed filesystem with degraded option, and then added a new
device into the seed filesystem, then we found adding device failed because
of the IO failure.

Steps to reproduce:
 # mkfs.btrfs -d raid1 -m raid1 <dev0> <dev1>
 # btrfstune -S 1 <dev0>
 # mount <dev0> -o degraded <mnt>
 # btrfs device add -f <dev2> <mnt>

It is because the original didn't set the chunk on the seed device to be
read-only if the degraded flag was set. It was introduced by patch f48b90756,
which fixed the problem the raid1 filesystem became read-only after one device
of it was missing. But this fix method was not right, we should set the read-only
flag according to the number of the missing devices, not the degraded mount
option, if the number of the missing devices is less than the max error number
that the profile of the chunk tolerates, we don't set it to be read-only.

Cc: Josef Bacik <jbacik@fb.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit d20983b40e828fdca920b3d678544c6a0967a446)

11 years agoBtrfs: make defragment work with nodatacow option
Wang Shilong [Thu, 3 Jul 2014 10:22:07 +0000 (18:22 +0800)]
Btrfs: make defragment work with nodatacow option

Btrfs defragment will utilize COW feature, which means this
did not work for nodatacow option, this problem was detected
by xfstests generic/018 with nodatacow mount option.

Fix this problem by forcing cow for a extent with state
@EXTETN_DEFRAG setting.

Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 47059d930f0e002ff851beea87d738146804726d)

11 years agobtrfs: label should not contain return char
Satoru Takeuchi [Tue, 1 Jul 2014 08:00:07 +0000 (17:00 +0900)]
btrfs: label should not contain return char

Rediffed remaining parts of original patch from Anand Jain.  This makes
sure to avoid trailing newlines in the btrfs label output

reproducer.sh:

===============================================================================

TEST_DEV=/dev/vdb
TEST_DIR=/home/sat/mnt

umount /home/sat/mnt

mkfs.btrfs -f $TEST_DEV
UUID=$(btrfs fi show $TEST_DEV | head -1 | sed -e 's/.*uuid: \([-0-9a-z]*\)$/\1/')
mount $TEST_DEV $TEST_DIR
LABELFILE=/sys/fs/btrfs/$UUID/label

echo "Test for empty label..." >&2
LINES="$(cat $LABELFILE | wc -l | awk '{print $1}')"
RET=0

if [ $LINES -eq 0 ] ; then
    echo '[PASS] Trailing \n is removed correctly.' >&2
else
    echo '[FAIL] Trailing \n still exists.' >&2
    RET=1
fi

echo "Test for non-empty label..." >&2

echo testlabel >$LABELFILE
LINES="$(cat $LABELFILE | wc -l | awk '{print $1}')"

if [ $LINES -eq 1 ] ; then
    echo '[PASS] Trailing \n is removed correctly.' >&2
else
    echo '[FAIL] Trailing \n still exists.' >&2
    RET=1
fi

exit $RET
===============================================================================

Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 48fcc3ff7dce0138c053833adf81670494f177f3)

11 years agobtrfs: device delete must be sysloged
Anand Jain [Mon, 30 Jun 2014 16:58:57 +0000 (00:58 +0800)]
btrfs: device delete must be sysloged

as in the disk add patch, disk detached from the volume must be
recorded in the syslog as well for the same reason.

Signed-off-by: Anand Jain <Anand.Jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit ec95d4917b9b5e9c73fd21294f4ff51f3127b5e7)

11 years agobtrfs: device add must be sysloged
Anand Jain [Mon, 30 Jun 2014 16:58:56 +0000 (00:58 +0800)]
btrfs: device add must be sysloged

when we add a new disk to the mounted btrfs we don't record it
as of now, disk add is a critical change of btrfs configuration,
it must be recorded in the syslog to help offline investigations
of customer problems when reported.

Signed-off-by: Anand Jain <Anand.Jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 43d207616886da1d93623ac40bd1d922a79e0d1b)

11 years agoBtrfs: clear compress-force when remounting with compress option
Wang Shilong [Mon, 30 Jun 2014 02:51:25 +0000 (10:51 +0800)]
Btrfs: clear compress-force when remounting with compress option

Steps to reproduce:
 # mkfs.btrfs -f /dev/sdb
 # mount /dev/sdb /mnt -o compress-force=lzo
 # mount /dev/sdb /mnt -o remount,compress=zlib
 # cat /proc/mounts

Remounting from compress-force to compress could not clear compress-force
option. The problem is there is no way for users to clear compress-force
option separately.

Fix this problem by clearing @FORCE_COMPRESS flag when remounting to
compress=xxx.

Suggested-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com>
Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Tested-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 4027e0f4c4b2df28d564560a3c65c179bebae4c8)

11 years agobtrfs: use DIV_ROUND_UP instead of open-coded variants
David Sterba [Wed, 4 Jun 2014 23:59:57 +0000 (01:59 +0200)]
btrfs: use DIV_ROUND_UP instead of open-coded variants

The form

  (value + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT

is equivalent to

  (value + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE

The rest is a simple subsitution, no difference in the generated
assembly code.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit ed6078f70335f158ca79790a0d0708ce558a6e9a)

11 years agobtrfs: clean away stripe_align helper
David Sterba [Wed, 4 Jun 2014 23:39:19 +0000 (01:39 +0200)]
btrfs: clean away stripe_align helper

Only wraps the ALIGN macro.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 4e54b17ad67a2d0d59bda6edcf725dc5b281c253)

11 years agobtrfs: use nodesize everywhere, kill leafsize
David Sterba [Wed, 4 Jun 2014 17:22:26 +0000 (19:22 +0200)]
btrfs: use nodesize everywhere, kill leafsize

The nodesize and leafsize were never of different values. Unify the
usage and make nodesize the one. Cleanup the redundant checks and
helpers.

Shaves a few bytes from .text:

  text    data     bss     dec     hex filename
852418   24560   23112  900090   dbbfa btrfs.ko.before
851074   24584   23112  898770   db6d2 btrfs.ko.after

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 707e8a071528385a87b63a72a37c2322e463c7b8)

11 years agobtrfs: kill the key type accessor helpers
David Sterba [Wed, 4 Jun 2014 16:41:45 +0000 (18:41 +0200)]
btrfs: kill the key type accessor helpers

btrfs_set_key_type and btrfs_key_type are used inconsistently along with
open coded variants. Other members of btrfs_key are accessed directly
without any helpers anyway.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 962a298f35110edd8f326814ae41a3dd306ecb64)

11 years agobtrfs: make close_ctree return void
David Sterba [Wed, 4 Jun 2014 16:10:45 +0000 (18:10 +0200)]
btrfs: make close_ctree return void

There's no user of the return value and we can get rid of the comment in
put_super.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 3abdbd780e9d75f0648b8a502c3789857b1e92ce)

11 years agobtrfs: cleanup ino cache members of btrfs_root
David Sterba [Wed, 5 Feb 2014 01:37:48 +0000 (02:37 +0100)]
btrfs: cleanup ino cache members of btrfs_root

The naming is confusing, generic yet used for a specific cache. Add a
prefix 'ino_' or rename appropriately.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 57cdc8db21bf9cfa6b2e45310d56e74e263e8609)

11 years agobtrfs: clenaup: don't call btrfs_release_path before free_path
David Sterba [Wed, 5 Feb 2014 01:11:25 +0000 (02:11 +0100)]
btrfs: clenaup: don't call btrfs_release_path before free_path

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit c6f83c74fd647e8e31a9a42a76d895bce4a61a08)

11 years agobtrfs: remove obsolete comment in btrfs_clean_one_deleted_snapshot
David Sterba [Wed, 5 Feb 2014 01:03:47 +0000 (02:03 +0100)]
btrfs: remove obsolete comment in btrfs_clean_one_deleted_snapshot

The comment applied when there was a BUG_ON.

Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 32471dc2ba287f0bb8ac9af65c46f089965191f2)