summaryrefslogtreecommitdiff
path: root/utils.c
Commit message (Collapse)AuthorAge
...
* btrfs-progs: simplify empty stirngs checkDavid Sterba2015-11-06
| | | | | | We can do a strlen(str) == 0 in a simpler way. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: mkfs: Round device size down to sectorsizeZhao Lei2015-11-03
| | | | | | | | | | | | | | | | | | | | | | | | When do following command in a vm, whose disks are created by qemu-img create -f raw 11 2.6G: # mkfs.btrfs -f /dev/vdd /dev/vde /dev/vdf # btrfs-show-super /dev/vdd /dev/vde /dev/vdf | grep dev_item.total_bytes dev_item.total_bytes 2791727104 dev_item.total_bytes 2791729152 dev_item.total_bytes 2791729152 We can see that the first device's size is little smaller. And it fails xfstests btrfs/011. Reason: First device's size is rounded down to sectorsize in make_btrfs(), but other devices are not. Fix: Round down remain devices' size in btrfs_add_to_fsid(). Reported-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Rename variables in btrfs_add_to_fsidZhao Lei2015-11-03
| | | | | | | | | | | | | | | | | There are two total_bytes in btrfs_add_to_fsid(), local variable of total_bytes means fs_total_bytes, and device->total_bytes means device's total_bytes. And device's total_bytes in argument is named block_count in current code. This patch rename: total_bytes -> fs_total_bytes block_count -> device_total_bytes To make code more readable. Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: fix missing initialization of list head for dev_listAnand Jain2015-11-02
| | | | | Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.com>
* Btrfs-progs: Do not force mixed block group creation unless '-M' option is ↵Chandan Rajendra2015-11-02
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | specified When creating small Btrfs filesystem instances (i.e. filesystem size <= 1GiB), mkfs.btrfs fails if both sectorsize and nodesize are specified on the command line and sectorsize != nodesize, since mixed block groups involves both data and metadata blocks sharing the same block group. This is an incorrect behavior when '-M' option isn't specified on the command line. This commit makes optional the creation of mixed block groups i.e. Mixed block groups are created only when -M option is specified on the command line. Since we now allow small filesystem instances with sectorsize != nodesize to be created, we can end up in the following situation, [root@localhost ~]# mkfs.btrfs -f -n 65536 /dev/loop0 btrfs-progs v3.19-rc2-405-g976307c See http://btrfs.wiki.kernel.org for more information. Performing full device TRIM (512.00MiB) ... Label: (null) UUID: 49fab72e-0c8b-466b-a3ca-d1bfe56475f0 Node size: 65536 Sector size: 4096 Filesystem size: 512.00MiB Block group profiles: Data: single 8.00MiB Metadata: DUP 40.00MiB System: DUP 12.00MiB SSD detected: no Incompat features: extref, skinny-metadata Number of devices: 1 Devices: ID SIZE PATH 1 512.00MiB /dev/loop0 [root@localhost ~]# mount /dev/loop0 /mnt/ mount: mount /dev/loop0 on /mnt failed: No space left on device The ENOSPC occurs during the creation of the UUID tree. This is because of things like large metadata block size, DUP mode used for metadata and global reservation consuming space. Also, large nodesize does not make sense on small filesystems, hence this should not be an issue. Signed-off-by: Chandan Rajendra <chandan@linux.vnet.ibm.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move is_numerical() helper to utils and renameAnand Jain2015-11-02
| | | | | | Signed-off-by: Anand Jain <anand.jain@oracle.com> [ moved to util.c and renamed ] Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: use btrfs_open_dir in open_path_or_dev_mntZhao Lei2015-11-02
| | | | | | | | | | | | | | | | | | | | | | | Use btrfs_open_dir() in open_path_or_dev_mnt() to make the function return error when target is neither block device nor btrfs mount point. Also add "verbose" argument to let function output common error message instead of putting duplicated lines in caller. Before patch: # ./btrfs device stats /mnt/tmp1 ERROR: getting dev info for devstats failed: Inappropriate ioctl for device # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1 ERROR: ioctl(DEV_REPLACE_STATUS) failed on "/mnt/tmp1": Inappropriate ioctl for device After patch: # ./btrfs device stats /mnt/tmp1 ERROR: not a btrfs filesystem: /mnt/tmp1 # ./btrfs replace start /dev/vdd /dev/vde /mnt/tmp1 ERROR: not a btrfs filesystem: /mnt/tmp1 Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: fix is_block_device() return checksAnand Jain2015-10-02
| | | | | | | | | | | | | | it was highlighted to me is_block_device(), returns 1 if the file is a block device, < 0 in case of an error (eg: file not found) 0 otherwise This patch makes proper return checks at all the places where is_block_device() is used. Thanks to Goffredo. Signed-off-by: Anand Jain <anand.jain@oracle.com> Suggested-by: Goffredo Baroncelli <kreijack@inwind.it> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: make sure that is_block_device will return only 0/1David Sterba2015-10-02
| | | | | | The macro return nonzero, we'll test against 0/1 (or < 0). Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: utils: Check nodesize against featuresQu Wenruo2015-10-02
| | | | | | | | | | | Check nodesize against features, not only sectorsize. In fact, one of the btrfs-convert and mkfs differs in the nodesize check. This patch also provides the basis for later btrfs-convert fix. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: provide fail safe for BTRFS_IOC_GET_FSLABEL ioctlAnand Jain2015-10-02
| | | | | | | | | | | | | Old kernels before 3.9 do not provide ioctl BTRFS_IOC_GET_FSLABEL. So we need to provide a fail safe logic for btrfs-progs running on those kernel. In this patch when get_label_mounted() fails on the old kernel it will fail back to the old method and uses get_label_unmounted(), where it will read from the disk directly. Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: cleanup, update function definitions to be ANSI conformantDavid Sterba2015-09-14
| | | | | | | | | | Sparse reports: warning: non-ANSI function declaration of function and we're using func(void) elsewhere. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: handle errors in btrfs_wipe_existing_sbDavid Sterba2015-09-02
| | | | | | | | | Failure during superblock wiping is not always a hard error as we're going to overwrite it anyway but it may catch some errors earlier. The error message is not very descriptive though, because we don't get back much information from blkid. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Introduce get_unit_mode_from_arg for common useZhao Lei2015-08-31
| | | | | | | | | | | | | | | | | | We are using separate code for parsing unit mode in current code, better to use common function. This patch introduces a common function to specify units as arguments and a common help message, to make every tool in btrfs having same unit argument. The benefits are: 1: Unify current tool's arguments for unit 2: Make tools in future easy to implement such argument 3: Changes (enhancement) in common function have effect on all relative tools Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: add memory allocation fail check in btrfs_add_to_fsid()Byongho Lee2015-08-31
| | | | | | | | | | In btrfs_add_to_fsid(), strdup() allocates memory to device->name, but the return value is not checked. So add the return value check and error handling code. And clean-up error handling code for ENOMEM case. Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: Introduce btrfs_open_dir wrapperZhao Lei2015-08-31
| | | | | | | | | | | This patch introduce open_btrfs_dir() to open a dir in btrfs filesystem. It can be used for several tools in btrfs-progs. Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com> [renamed from open_btrfs_dir, adjusted error messages] Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: utils: missing newline in error messages when checking dupDavid Sterba2015-07-14
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: drop unused parameter from make_btrfsDavid Sterba2015-07-01
| | | | Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: move make_btrfs arguments to a structDavid Sterba2015-07-01
| | | | | | | No functional change, just introduce the structure and switch current users. Signed-off-by: David Sterba <dsterba@suse.com>
* btrfs-progs: add helper for copying pathsDavid Sterba2015-06-12
| | | | | | Check the source path length and do the copy. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: properly set up ioctl argumentsDavid Sterba2015-06-12
| | | | | | | | At some places we do not clear the whole ioctl structure and could pass garbage to kernel. Zero the ioctl vol_args and use a helper for copying the path. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: use PATH_MAX instead of BTRFS_PATH_NAME_MAXDavid Sterba2015-06-12
| | | | | | | | | | The path bufferes should be PATH_MAX but BTRFS_PATH_NAME_MAX is shorter due to embedding in 4k aligned structures. The only reason to use BTRFS_PATH_NAME_MAX is for the respective structures btrfs_ioctl_vol_args::name. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: use less memory for pretty_size_mode buffersDavid Sterba2015-06-11
| | | | | | | | | | | | | | | | | | Anand reports that the static buffers used for pertty size strings cause a stack overflow on SPARC. Zach proposed to change the printf format to wrap the number and the suffix into a macro. This would require to change all callsites of pretty_size* and is not very convienient to write. This patch replaces the per-call-site static buffers with a limited number for slots that would be used on each invokation of pretty_size and wrap around. The number of array slots shall be 10 for now, in current codebase there are no more than 2 calls to pretty_size in a single argument list. Reported-by: Anand Jain <Anand.Jain@oracle.com> CC: Zach Brown <zab@zabbo.net> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: print error within test_num_disk_vs_raidDavid Sterba2015-06-11
| | | | | | | | The error string buffer passed as an argument is of a fixed size, though we could print up to PATH_MAX + something bytes. Print the error message directly. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: print error within test_dev_for_mkfsDavid Sterba2015-06-11
| | | | | | | | The error string buffer passed as an argument is of a fixed size, though we could print up to PATH_MAX + something bytes. Print the error message directly. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: cleanup after errors in open_file_or_dir3David Sterba2015-06-09
| | | | | | | | We should zero the dirstream in case we close it ourselves, so the caller does not do it again. Most current callers do not do that and exit immediatelly. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: fix argv0_buf handlingDavid Sterba2015-06-09
| | | | | | | | | | | | | | | | The variable argv0_buf was duplicated and the changes done in utils.c were not propagated to help.c. So if an unknown commandline token was found, the error message did not contain the known part: $ btrfs scrub test : unknown token 'test' instead of $ btrfs scrub test btrfs scrub: uknown token 'test' Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: return the fsid from make_btrfs()Goffredo Baroncelli2015-06-08
| | | | | | | | | | | The function make_btrfs() has as argument the fsid of the filesystem. If this fsid is empty or null make_btrfs() generates a new fsid. However If the buffer is valid (but the string is empty) the generated fsid is copied back to the caller. Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it> Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: add strdup in btrfs_add_to_fsid() to track the device pathGoffredo Baroncelli2015-06-08
| | | | | | | | | When creating a new btrfs_device, copy the path to track it. This path is then used by mkfs.btrfs to list all devices. Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it> Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: mkfs: move info message out of btrfs_add_to_fsidDavid Sterba2015-06-08
| | | | Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: add function to stringify filesystem featuresDavid Sterba2015-06-04
| | | | Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: mkfs: check metadata redundancySam Tygier2015-06-02
| | | | | | | | | | | | | | | | Currently BTRFS allows you to make bad choices of data and metadata levels. For example -d raid1 -m raid0 means you can only use half your total disk space, but will lose everything if 1 disk fails. It should give a warning in these cases. When making a filesystem, check that metadata mode is at least as redundant as the data mode. For example give warning when: -d raid1 -m raid0 Signed-off-by: Sam Tygier <samtygier@yahoo.co.uk> [make the check more visible in mkfs output] Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: btrfs_scan_block_devices is unused function delete itAnand Jain2015-05-14
| | | | | | | | | After Patch: remove BTRFS_SCAN_PROC scan method There isn't any consumer for btrfs_scan_block_devices() so delete it. Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: print fs features filtered by a maskDavid Sterba2015-04-07
| | | | | | | mkfs and convert will not support the same features, -O will print only the list according to the given mask. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: move feature parsing from mkfs to utilsDavid Sterba2015-04-07
| | | | | | | We'll use them in convert as well. Move defines and the interface functions to utils.*. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: mkfs, deprecate leafsize and clean up the codeDavid Sterba2015-04-07
| | | | | | | Using the --leafsize will issue a warning. Replace leafsize with nodesize in the mkfs-related code. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: Allow parse_qgroupid() to resolve subvolume path into qgroupidQu Wenruo2015-03-24
| | | | | | | | | | Now parse_qgroupid() can resolve subvolume path into qgroupid. This is quite handy for handling level 0 qgroupid, and user don't need to resolve rootid by hand now. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> [constify string in __is_subvol] Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: Move parse_qgroupid() to utils.cQu Wenruo2015-03-24
| | | | | | Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> [constified strings in parse_qgroupid] Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: Fix msgs in check_node_or_leaf_sizeSebastian Thorarensen2015-03-23
| | | | | | | | | | check_node_or_leaf_size in utils.c now prints 'nodesize (or leafsize)' instead of 'leafsize (or nodesize)' in the error messages, in order to be less confusing for the user, as leafsize in mkfs is deprecated. 'ERROR: ' is also prepended to be consistent with other error messages. Signed-off-by: Sebastian Thorarensen <sebth@naju.se> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: mkfs: Move out some nodesize codeSebastian Thorarensen2015-03-23
| | | | | | | | | | | Move the constant DEFAULT_MKFS_LEAF_SIZE to utils.h and rename it to BTRFS_MKFS_DEFAULT_NODE_SIZE for consistency. Move the function check_leaf_or_node_size to utils.c and rename it to btrfs_check_node_or_leaf_size. Signed-off-by: Sebastian Thorarensen <sebth@naju.se> [added btrfs_ prefix] Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: don't output operational status information to stderrXing Gu2015-01-27
| | | | | Signed-off-by: Xing Gu <gux.fnst@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: drop feature defines from C files, in favour of CFLAGS definesDimitri John Ledkov2015-01-27
| | | | | | | | | | | | | | | | | | | | | | glibc 2.10+ (5+ years old) enables all the desired features: _XOPEN_SOURCE 700, __XOPEN2K8, POSIX_C_SOURCE, DEFAULT_SOURCE; with a single _GNU_SOURCE define in the makefile alone. For portability to other libc implementations (e.g. dietlibc) _XOPEN_SOURCE=700 is also defined. This also resolves Debian bug report filed by Michael Tautschnig - "Inconsistent use of _XOPEN_SOURCE results in conflicting declarations". Whilst I was not able to reproduce the results, the reported fact is that _XOPEN_SOURCE set to 500 in one set of files (e.g. cmds-filesystem.c) generates/defines different struct stat from other files (cmds-replace.c). This patch thus cleans up all feature defines, and sets them at a consistent level. Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=747969 Signed-off-by: Dimitri John Ledkov <dimitri.j.ledkov@intel.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: Fix a buffer overflow causing segfault in fstests/btrfs/069Qu Wenruo2015-01-09
| | | | | | | | | | | | | | | | | | The newly introduced search_chunk_tree_for_fs_info() won't count devid 0 in fi_arg->num_devices, which will cause buffer overflow since later get_device_info() will fill di_args with devid. This can be trigger by fstests/btrfs/069 and any operations needs to iterate over all the devices like 'fi show' or 'dev stat' while replacing. The fix is do an extra probe specifically for devid 0 after search_chunk_tree_for_fs_info() and change num_devices if needed. Reported-by: Tsutomu Itoh <t-itoh@jp.fujitsu.com> Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: Allow debug-tree to be executed on regular file.Qu Wenruo2015-01-09
| | | | | | | | | | | The commit 1bad43fbe002 ("btrfs-progs: refine btrfs-debug-tree error prompt when a mount point given") add judgement on btrfs-debug-tree to restrict only block device to be executed on, but the command can also be used on regular file, so add regular file support for the judgement. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: safely copy path in btrfs_scan_lblkidDavid Sterba2015-01-03
| | | | | | | We should terminate the string, let's use the wrapper. Resolves-coverity-id: 1127097 Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: cleanup whitespace in is_same_blk_fileDavid Sterba2015-01-03
| | | | Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: use strncpy in is_same_blk_fileDavid Sterba2015-01-03
| | | | | | | Although we're getting strings from the caller, let's be safe. Resolves-coverity-id: 1199475 Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: move check_arg_type() to util.cGui Hecheng2014-12-29
| | | | | | | | | The check_arg_type() function does quite generic thing, move it to utils.c. Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> Reviewed-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: basic support for TREE_SEARCH_V2 ioctlDavid Sterba2014-12-10
| | | | | | Add the interface and helper that checks if the v2 ioctl is supported. Signed-off-by: David Sterba <dsterba@suse.cz>
* btrfs-progs: fix wrong num_devices for btrfs fi show with seed devicesGui Hecheng2014-12-04
| | | | | | | | | The @fi_args->num_devices in @get_fs_info() does not include seed devices. We could just correct it by searching the chunk tree and count how many dev_items there are in total which includes seed devices. Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>