summaryrefslogtreecommitdiff
path: root/utils.c
Commit message (Collapse)AuthorAge
* btrfs-progs: pass superblock flags to mount check helpersDavid Sterba2018-08-06
| | | | | | | | Extend check_mounted_where so we can pass additional flags that would allow us to open filesystem in some specific state. This will be used for a filesystem that has a partially changed uuid. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: fix regression preventing send -p with subvolumes mounted on "/"Axel Burri2018-06-07
| | | | | | | | | | | | Fix subvol_strip_mountpoint for mnt="/" (len=1). In this case, skip check for trailing slash on full_path (leading slash on full_path is already asserted by strncmp). Issue: #122 Pull-request: #138 Fixes: c5dc299aff6b ("btrfs-progs: prevent incorrect use of subvol_strip_mountpoint") Signed-off-by: Axel Burri <axel@tty0.ch> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Rename OPEN_CTREE_FS_PARTIAL to OPEN_CTREE_TEMPORARY_SUPERQu Wenruo2018-04-24
| | | | | | | | | | | | | | | | | | | | | | | | The old flag OPEN_CTREE_FS_PARTIAL is in fact quite easy to be confused with OPEN_CTREE_PARTIAL, which allow btrfs-progs to open damaged filesystem (like corrupted extent/csum tree). However OPEN_CTREE_FS_PARTIAL, unlike its name, is just allowing btrfs-progs to open fs with temporary superblocks (which only has 6 basic trees on SINGLE meta/sys chunks). The usage of FS_PARTIAL is really confusing here. So rename OPEN_CTREE_FS_PARTIAL to OPEN_CTREE_TEMPORARY_SUPER, and add extra comment for its behavior. Also rename BTRFS_MAGIC_PARTIAL to BTRFS_MAGIC_TEMPORARY to keep the naming consistent. And with above comment, the usage of FS_PARTIAL in dump-tree is obviously incorrect, fix it. Fixes: 8698a2b9ba89 ("btrfs-progs: Allow inspect dump-tree to show specified tree block even some tree roots are corrupted") Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: replace test_issubvolume() with btrfs_util_is_subvolume()Omar Sandoval2018-03-06
| | | | | | | | | This gets the remaining occurrences that weren't covered by previous conversions. Signed-off-by: Omar Sandoval <osandov@fb.com> [ fixup test_issubvolume due to removed dependency patch ] Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: use libbtrfsutil for subvol showOmar Sandoval2018-03-06
| | | | | | | | Now implemented with btrfs_util_subvolume_path(), btrfs_util_subvolume_info(), and subvolume iterators. Signed-off-by: Omar Sandoval <osandov@fb.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: prevent incorrect use of subvol_strip_mountpointAxel Burri2018-02-19
| | | | | | | | | | | | | | | | | | | Add additional bound checks to prevent memory corruption on incorrect usage of subvol_strip_mountpoint. Assert sane return value by properly comparing the mount point to the full_path before stripping it off. Mitigates issue: "btrfs send -p" fails if source and parent subvolumes are on different mountpoints (memory corruption): https://github.com/kdave/btrfs-progs/issues/96 Note that this does not properly fix this bug, but prevents a possible security issue by unexpected usage of "btrfs send -p". Issue: #96 Pull-request: #98 Signed-off-by: Axel Burri <axel@tty0.ch> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: treewide: Replace strerror(errno) with %m.Rosen Penev2018-01-31
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | As btrfs is specific to Linux, %m can be used instead of strerror(errno) in format strings. This has some size reduction benefits for embedded systems. glibc, musl, and uclibc-ng all support %m as a modifier to printf. A quick glance at the BIONIC libc source indicates that it has support for %m as well. BSDs and Windows do not but I do believe them to be beyond the scope of btrfs-progs. Compiled sizes on Ubuntu 16.04: Before: 3916512 btrfs 233688 libbtrfs.so.0.1 4899 bcp 2367672 btrfs-convert 2208488 btrfs-corrupt-block 13302 btrfs-debugfs 2152160 btrfs-debug-tree 2136024 btrfs-find-root 2287592 btrfs-image 2144600 btrfs-map-logical 2130760 btrfs-select-super 2152608 btrfstune 2131760 btrfs-zero-log 2277752 mkfs.btrfs 9166 show-blocks After: 3908744 btrfs 233256 libbtrfs.so.0.1 4899 bcp 2366560 btrfs-convert 2207432 btrfs-corrupt-block 13302 btrfs-debugfs 2151104 btrfs-debug-tree 2134968 btrfs-find-root 2281864 btrfs-image 2143536 btrfs-map-logical 2129704 btrfs-select-super 2151552 btrfstune 2130696 btrfs-zero-log 2276272 mkfs.btrfs 9166 show-blocks Total savings: 23928 (24 kilo)bytes Signed-off-by: Rosen Penev <rosenp@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: don't clobber errno in close_file_or_dirDavid Sterba2018-01-31
| | | | | | | | Preserve the errno value for the caller in case closing happens in the middle of eg. an ioctl and reporing the failure. The errors that could happen in close/closedir do not bother us. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Replace usage of list_for_each with list_for_each_entryNikolay Borisov2018-01-31
| | | | | | | | | | | | | There are a couple of places where instead of the more succinct list_for_each_entry the code uses list_for_each. This results in slightly more code with no additional benefit as well as no coherent pattern. This patch makes the code uniform. No functional changes. Signed-off-by: Nikolay Borisov <nborisov@suse.com> Reviewed-by: Qu Wenruo <wqu@suse.com> [ remove unused variable in uuid_search ] Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Factor out common print_device_infoNikolay Borisov2018-01-31
| | | | | | | | | This function has been copied twice in chunk-recover and super-recover. Factor it out into utils.c/h and use it. No functional changes. Signed-off-by: Nikolay Borisov <nborisov@suse.com> Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: Use the whole file or block device to mkfs for rootdirQu Wenruo2018-01-08
| | | | | | | | | | | | | | | For --rootdir, even for large existing file or block device, it will always shrink the resulting filesystem. The problem is, mkfs.btrfs will try to calculate the dir size, and use it as @block_count to mkfs, which makes the filesystem shrunk. Fix it by trying to get the original block device or file size as @block_count, so mkfs.btrfs can use the full file/block device for --rootdir option. Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: fix regression preventing --rootdir to create fileQu Wenruo2018-01-08
| | | | | | | | | | | | | | | | Commit 460e93f25754 ("btrfs-progs: mkfs: check the status of file at mkfs") will try to check the file state before creating fs on it. The check is mostly fine for normal mkfs case, while for --rootdir option, it's allowed to create a new file if the destination file doesn't exist. Fix it by allowing non-existent file if --rootdir is specified. Fixes: 460e93f25754 ("btrfs-progs: mkfs: check the status of file at mkfs") Signed-off-by: Qu Wenruo <wqu@suse.com> Reviewed-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: extent-cache: actually cache extent buffersJeff Mahoney2017-10-06
| | | | | | | | | | | | | | We have the infrastructure to cache extent buffers but we don't actually do the caching. As soon as the last reference is dropped, the buffer is dropped. This patch keeps the extent buffers around until the max cache size is reached (defaults to 25% of memory) and then it drops the last 10% of the LRU to free up cache space for reallocation. The cache size is configurable (for use by e.g. lowmem) when the cache is initialized. Signed-off-by: Jeff Mahoney <jeffm@suse.com> [ update codingstyle, switch total_memory to bytes ] Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: change seen_fsid to hold fd and DIRMisono, Tomohiro2017-10-06
| | | | | | | | | Change seen_fsid to hold fd and DIR in order to keep access to each fs. This will be used for 'subvol delete --commit-after'. Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com> Reviewed-by: Qu Wenruo <quwenruo.btrfs@gmx.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move seen_fsid to utils.cMisono, Tomohiro2017-10-06
| | | | | | | | | Move is_seen_fsid()/add_seen_fsid()/free_seen_fsid() to common functions. This will be used for 'subvol delete --commit-after'. Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com> Reviewed-by: Qu Wenruo <quwenruo.btrfs@gmx.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move get_fsid() to utils.cMisono, Tomohiro2017-10-06
| | | | | | | | | Make get_fsid() to a common functions. This will be used for 'subvol delete --commit-after'. Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com> Reviewed-by: Qu Wenruo <quwenruo.btrfs@gmx.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: inspect rootid: Allow a file to be specifiedMisono, Tomohiro2017-09-08
| | | | | | | | | | | | | Since cmd_inspect_rootid() calls btrfs_open_dir(), it rejects a file to be specified. But as the document says, a file should be supported. This patch introduces btrfs_open_file_or_dir(), which is a counterpart of btrfs_open_dir(), to safely check and open btrfs file or directory. The original btrfs_open_dir() content is moved to btrfs_open() and shared by both function. Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: add crude error handling when transaction start failsDavid Sterba2017-09-08
| | | | | | | | | | Currently transaction bugs out insided btrfs_start_transaction in case of error, we want to lift the error handling to the callers. This patch adds the BUG_ON anywhere it's been missing so far. This is not the best way of course. Transforming BUG_ON to a proper error handling highly depends on the caller and should be dealt with case by case. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: subvol show: add support to search subvolume by rootid or uuidAnand Jain2017-07-20
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Unless the top level is mounted there is no way to know the details of all the subvolume. For example: mount -o subvol=sv1/newsv1 /dev/sdb /btrfs btrfs su list /btrfs ID 257 gen 12 top level 5 path sv1 ID 258 gen 9 top level 257 path sv1/snap ID 259 gen 11 top level 257 path sv1/newsv1 You can't subvol show for sv1 and sv1/snap as its paths aren't accessible to the user unless the its top level is mounted. This patch adds two new options to the existing btrfs subvol show cli. They are --rootid/-r or --uuid/-u, with this now the user will be able to look for a subvolume using the rootid OR the uuid. ./btrfs su show -r 257 /btrfs sv1 Name: sv1 UUID: 30129358-c69d-3e4a-a662-29509cc69c95 Parent UUID: - Received UUID: - Creation time: 2017-07-11 20:32:57 +0800 Subvolume ID: 257 Generation: 12 Gen at creation: 7 Parent ID: 5 Top level ID: 5 Flags: - Snapshot(s): sv1/snap Signed-off-by: Anand Jain <anand.jain@oracle.com> [ minor adjustments in the help text ] Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Refactor btrfs_add_device() to use btrfs_fs_infoQu Wenruo2017-07-12
| | | | | | | | BTW, there is a duplicated definition of btrfs_add_device() in volumes.h, also remove it. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Refactor nodesize users in utils.cQu Wenruo2017-07-03
| | | | Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
* btrfs-progs: move prefixcmp helper to utilsDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move utils code out of headerDavid Sterba2017-03-08
| | | | | | | There are not performance critical static inlines, we can do the normal declaration/definition split for various helpers. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move mkfs helper implementation out of utilsDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move more mkfs declarations to the common headerDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move fs features implementation to own fileDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move fs features declarations to own header from utilsDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: convert: move common api implementation to own fileDavid Sterba2017-03-08
| | | | | | Lots of moved code but no actual changes. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: move common api implementation to own fileDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move convert definitions to own headerDavid Sterba2017-03-08
| | | | | | | Create a header for filesystem conversion API, the config and the main entry function. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move mkfs definitions to own headerDavid Sterba2017-03-08
| | | | | | | Create a header for filesystem creation API, the config and the main entry function. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move help implemetnation to own fileDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move help defines to own headerDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Introduce kernel sizes to cleanup large intermediate numberQu Wenruo2017-03-08
| | | | | | | | | | | Large numbers like (1024 * 1024 * 1024) may cost reader/reviewer to waste one second to convert to 1G. Introduce kernel include/linux/sizes.h to replace any intermediate number larger than 4096 (not including 4096) to SZ_*. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: introduce global configDavid Sterba2017-03-08
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs/convert: separate the convert part from make_btrfsDavid Sterba2017-01-27
| | | | | | | The regulare mkfs_btrfs does not anything special about convert and just passes the arguments. Make that two functions. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: print device name while trimmingDavid Sterba2017-01-25
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: make negative number pretty printing optionalDavid Sterba2017-01-25
| | | | | | | Add a unit mode that will interpret the input number as a signed 64bit, optionally and not by default for all numbers. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: utils: negative numbers are more plausible than sizes over 8 EiBZygo Blaxell2017-01-25
| | | | | | | | | | | | | | | | | I got tired of seeing "16.00EiB" whenever btrfs-progs encounters a negative size value, e.g. during resize: Unallocated: /dev/mapper/datamd18 16.00EiB This version is much more useful: Unallocated: /dev/mapper/datamd18 -26.29GiB Signed-off-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org> Reviewed-by: Omar Sandoval <osandov@fb.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Use helper function to access ↵Chandan Rajendra2016-12-14
| | | | | | | | | | | | | btrfs_super_block->sys_chunk_array_size btrfs_super_block->sys_chunk_array_size is stored as le32 data on disk. However insert_temp_chunk_item() writes sys_chunk_array_size in host cpu order. This commit fixes this by using super block access helper functions to read and write btrfs_super_block->sys_chunk_array_size field. Signed-off-by: Chandan Rajendra <chandan@linux.vnet.ibm.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move lookup_path_rootid to library utilsDavid Sterba2016-11-24
| | | | | | | | | | | The refactoring in commit 1c85c3de5aab997ff66ea95cb0f2c9f79726ec40 has broken use of libbtrfs that does not exhibit during build but at the run time. Fixes: 1c85c3de5aab997ff66ea95cb0f2c9f79726ec40 Bugzilla: https://bugs.gentoo.org/show_bug.cgi?id=600078 Reported-by: Mike Gilbert <floppymaster@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: utils: Fix NULL pointer derefernces in string_is_numericalQu Wenruo2016-11-23
| | | | | | | | | | | | | | In get_running_kernel_version() function, we directly pass return pointer from strtok_r() to string_is_numerical(). Return pointer from strok_r() can be NULL, but string_is_numerical() can't handle it and will cause NULL pointer derefernces. Fix it by check if it's a NULL pointer first. Resolves-Coverity-CID: 1374097 Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: add names of matching sysfs feature namesDavid Sterba2016-11-11
| | | | | | The mkfs parameters do not match file names exported through sysfs. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: enhance feature tableDavid Sterba2016-11-11
| | | | | | | Add string and numeric version for later use so we don't have to convert all the time. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: extend mkfs features with compat, safe and default versionsDavid Sterba2016-11-11
| | | | | | | Enhance the mkfs_features list with the minimum kernel versions that will allow for turning on compatible and/or safe options. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: add temporary variable for fs featuresDavid Sterba2016-11-11
| | | | | | A minor cleanup before other changes. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: use correct type for device id iteration in get_fs_infoDavid Sterba2016-11-09
| | | | | | | | Coverity reports (CID 1374096) that there's return value overflow of ret from get_device_info, but this most likely cannot happen. There's another minor issue where we use int to iterate over devids. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: fix search tree v2 ioctl detectionDavid Sterba2016-11-09
| | | | | | | The result of the test applies per-filesystem, so we can't simply cache it. The function hasn't been used yet. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: utils: remove useless check in make_btrfsDavid Sterba2016-11-09
| | | | | | | A leftover when fs_uuid was dynamically allocated, the condition was always true, no functional change. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: store creation time of the toplevel subvolumeDavid Sterba2016-11-09
| | | | | | | For unkonwn reasons, the creation time of the toplevel subvolume inode item hasn't been set. Signed-off-by: David Sterba <dsterba@suse.com>